Commit Graph

14702 Commits

Author SHA1 Message Date
Igor Kudrin
ce25eb12dd [ELF] Do not report undefined weak references in shared libraries
This fixes an issue introduced in D101996.

A weak reference in a shared library could be incorrectly reported if
there is another library that has a strong reference to the same symbol.

Differential Revision: https://reviews.llvm.org/D115041
2021-12-07 10:10:51 +07:00
Chris Davis
e4eb6216c2 Enable pdbpagesize to allow support for PDB file sizes > 4GB
Enable the pdbpagesize flag to allow linking of PDB files > 4GB.
Also includes a couple small fixes to change to uint64_t to support the
larger file sizes.  I updated the max file size check in MSFBuilder.cpp
to take into account the page size.

Differential Revision: https://reviews.llvm.org/D115051
2021-12-06 18:22:08 -05:00
Jez Ng
1b44364714 [lld-macho] Unreferenced weak dylib symbols shouldn't fetch archive symbols
We were fetching archive symbols too eagerly, bloating binary size as well as
just screwing up binaries that expected to look up certain symbols only at
runtime.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D115092
2021-12-05 15:11:44 -05:00
Kristina Bessonova
0ac75e82ff Reland [DwarfDebug] Move emission of global vars, types and imports to endModule()
This patch proposes to move emission of global variables, types,
imported entities, etc from DwarfDebug::beginModule() to DwarfDebug::endModule().
Effectively, this changes nothing but the order of debug entities which
will be as follows:
* subprograms (including related context, local variables/labels,
  local imported entities; related types can be created as a part of
  the emission of local entities of an abstract subprogram);
* global variables (including related context and types);
* retained types and enums;
* non-local-scoped imported entities;
* basic types;
* other types left (as a part of local variables attributes emission).

Note that the order of emitted compile units may also be changed as now we emit
units that contain subprograms first and then all other non-empty units.

The motivation behind this change is the following:
(1) DwarfDebug::beginModule() is run at the very beginning of backend's pipeline,
    from this time IR can be significantly changed by target-specific passes.
    If it happens for debug metadata of global entities, those changes will not
    be reflected in the emitted DWARF.
(2) imported subprogram names should refer to an abstract subprogram if it exists,
    but it isn't known in DwarfDebug::beginModule() (it's possible to make some
    guesses based on location info, but it's not quite reliable);
(3) aforementioned entities if they are scoped within a bracketed block
    (subject of D113741) couldn't be emitted in DwarfDebug::beginModule()
    (they need parent emitted first). Another problem is if to try to gather
    some information about local entities and defer their emission
    (till subprogram's processing or DwarfDebug::endModule()) all the gathered
    details might be irrelevant / invalid by the time the entities are being
    emitted (because of (1)).

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D114705
2021-12-05 13:56:45 +02:00
Leonard Grey
134275d994 [Support] Use final filename for Caching buffer identifier
Mach-O LLD uses the buffer identifier of the memory buffer backing an object
file to generate stabs which are used by `dsymutil` to find the object file for
dSYM generation.

When using thinLTO, these buffers are provided by the cache which initially
saves them to disk as temporary files beginning with "Thin-" but renames them
to persistent files beginning with "llvmcache-" before the buffer is provided
to the cache user.

However, the buffer is created before the file is renamed and is given the temp
file's name as an identifier. This causes the generated stabs to point to
nonexistent files.

This change names the buffer with the eventual persistent filename. I think
this is safe because failing to rename the temp file is a fatal error.

Differential Revision: https://reviews.llvm.org/D115055
2021-12-04 22:25:49 -05:00
Kristina Bessonova
a961604819 Revert "[DwarfDebug] Support emitting function-local declaration for a lexical block"
This reverts commits
* ee691970a9 (D113741),
* 79d3132998 (D114705)

due to lldb and dexter test failures.
2021-12-04 18:06:57 +02:00
Kristina Bessonova
79d3132998 [DwarfDebug] Move emission of global vars, types and imports to endModule()
This patch proposes to move emission of global variables, types,
imported entities, etc from DwarfDebug::beginModule() to DwarfDebug::endModule().
Effectively, this changes nothing but the order of debug entities which
will be as follows:
* subprograms (including related context, local variables/labels,
  local imported entities; related types can be created as a part of
  the emission of local entities of an abstract subprogram);
* global variables (including related context and types);
* retained types and enums;
* non-local-scoped imported entities;
* basic types;
* other types left (as a part of local variables attributes emission).

Note that the order of emitted compile units may also be changed as now we emit
units that contain subprograms first and then all other non-empty units.

The motivation behind this change is the following:
(1) DwarfDebug::beginModule() is run at the very beginning of backend's pipeline,
    from this time IR can be significantly changed by target-specific passes.
    If it happens for debug metadata of global entities, those changes will not
    be reflected in the emitted DWARF.
(2) imported subprogram names should refer to an abstract subprogram if it exists,
    but it isn't known in DwarfDebug::beginModule() (it's possible to make some
    guesses based on location info, but it's not quite reliable);
(3) aforementioned entities if they are scoped within a bracketed block
    (subject of D113741) couldn't be emitted in DwarfDebug::beginModule()
    (they need parent emitted first). Another problem is if to try to gather
    some information about local entities and defer their emission
    (till subprogram's processing or DwarfDebug::endModule()) all the gathered
    details might be irrelevant / invalid by the time the entities are being
    emitted (because of (1)).

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D114705
2021-12-04 14:10:01 +02:00
Fangrui Song
9bd6f6f6d5 [ELF][test] Fix typo in aarch64-cortex-a53-843419-recognize.s 2021-12-03 14:38:56 -08:00
George Koehler
885fb9a257 [ELF][PPC32] Make R_PPC32_PLTREL retain .got
PLT usage needs the first 12 bytes of the .got section. We need to keep .got and
DT_GOT_PPC even if .got/_GLOBAL_OFFSET_TABLE_ are not referenced (large PIC code
may only reference .got2), which is the case in OpenBSD's ld.so, leading
to a misleading error, "unsupported insecure BSS PLT object".

Fix this by adding R_PPC32_PLTREL to the list of hasGotOffRel.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D114982
2021-12-02 15:28:37 -08:00
Fangrui Song
353fe72ca3 [ELF] Hint -z nostart-stop-gc for __start_ undefined references
Make users aware what to do with ld.lld 13.0.0 / GNU ld<2015-10 --gc-sections
behavior.

Differential Revision: https://reviews.llvm.org/D114830
2021-12-02 11:58:25 -08:00
Keith Smiley
9e3552523e [lld-macho] Remove old macho darwin lld
During the llvm round table it was generally agreed that the newer macho
lld implementation is feature complete enough to replace the old
implementation entirely. This will reduce confusion for new users who
aren't aware of the history.

Differential Revision: https://reviews.llvm.org/D114842
2021-12-02 11:04:49 -08:00
Reid Kleckner
8270ff86a1 [ELF] Fix driver.test after 8c3641d0 when cwd is readonly 2021-12-02 10:25:04 -08:00
Sam Clegg
6f5c5cbe5f [lld][WebAssembly] Fix for debug relocations against undefined function symbols
This is very similar to https://reviews.llvm.org/D103557 but applies to
symbols which are undefined at link time rather than compile time.

We already have code that handles symbols which were defined at link
time but dead stripped by `--gc-sections` (See
`test/wasm/debug-removed-fn.ll`). In that case the symbols are not live
(!isLive()).  However, we can also have live symbols (which are
references by the program) but which are undefined at link time and are
imported by the linker.

In the test case here the symbol `undef` is used but is not defined
in the program but is imported by the linker due to the
`--import-undefined` flag.

Fixes: https://github.com/emscripten-core/emscripten/issues/15528

Differential Revision: https://reviews.llvm.org/D114921
2021-12-02 08:36:28 -08:00
Fangrui Song
c5bfffed48 [ELF] Discard input .note.gnu.build-id even with default --build-id=none
binutils 2.38 will adopt this behavior
https://sourceware.org/bugzilla/show_bug.cgi?id=28639

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D114910
2021-12-02 09:50:59 +00:00
Igor Kudrin
b0ac68ccb7 [ELF] Prevent internalizing used comdat symbol
When a comdat symbol is defined in both bitcode and regular object
files, which are contained in the same archive, the linker could lose
the flag that the symbol is used in the regular object file and allow
LTO to internalize it, which led to "error: undefined symbol".

The issue was introduced in D79300.

Differential Revision: https://reviews.llvm.org/D114801
2021-12-02 12:10:06 +07:00
Fangrui Song
ad45df91ad [ELF][PPC64] Remove unneeded PPC64PCRelLongBranchThunk
This reverts the PPC64PCRelLongBranchThunk part from D86706.
PPC64PCRelLongBranchThunk is the same as PPC64R12SetupStub.

Use `__gep_setup_` instead of `__long_branch_pcrel_` for the stub symbol name
as it more closely indicates the operation.
(Note: GNU ld uses `*.long_branch.*` and `*.plt_branch.*`).

Reviewed By: NeHuang, nemanjai

Differential Revision: https://reviews.llvm.org/D114656
2021-11-30 11:33:17 -08:00
Fangrui Song
8c3641d03e [ELF] Change -z unknown from error to warning
There is a trend of having more optional options (usually security
hardening related) like -z cet-report=, -z bti-report=, -z force-bti.
If ld.lld 14.0.0 uses a warning, in 15/16/17/... timeframe when people
add new options to software, they can worry less about linker errors on ld.lld 14.0.0.

In some cases `-z foo` does essential work where a silent ignore can be
problematic, but the user has received a warning. From my observation, the
doing-essential-work `-z foo` is much fewer than the converse. In addition,
the user who cares can use `--fatal-warnings` (Note: GNU ld doesn't upgrade warnings to errors).
It is unclear whether we need something like `clang -Wunknown-warning-option`.

If we ever run into unfortunate transition like `-z start-stop-gc`, the
affected software (e.g. ldc is a compiler which passes linker options to the underlying ld)
can blindly add the `-z` option, without worrying it may cause a linker error to LLD 14.0.0.

Reviewed By: jrtc27, peter.smith

Differential Revision: https://reviews.llvm.org/D114748
2021-11-30 11:06:28 -08:00
Vy Nguyen
74cbd71072 [lld-macho] Mark dylib symbols coming from -weak_framework as weak-ref.
PR:52564

Differential Revision: https://reviews.llvm.org/D114397
2021-11-30 09:54:59 -05:00
Fangrui Song
5188f55d32 [ELF] Move ObjFile<ELFT>::{getLocalSymbols,getGlobalSymbols} to non-template ELFFileBase. NFC 2021-11-30 00:50:19 -08:00
Fangrui Song
5047e3a3ba [ELF] Move GOT/PLT relocation code closer. NFC 2021-11-29 23:10:04 -08:00
Fangrui Song
1ce51a5f35 [ELF] --cref: If -Map is specified, print to the map file
PR48282: This behavior matches GNU ld and gold.

Reviewed By: markj

Differential Revision: https://reviews.llvm.org/D114663
2021-11-29 14:14:53 -08:00
Fangrui Song
4709bacf18 [ELF] Avoid std::stable_partition which may allocate memory. NFC 2021-11-28 21:47:56 -08:00
Fangrui Song
99a2d940dd [ELF] Speed up/simplify removeUnusedSyntheticSections. NFC
Make one change: when the OutputSection is nullptr (due to /DISCARD/ or garbage
collected BssSection (replaceCommonSymbols)), discard the SyntheticSection as well.
2021-11-28 21:07:34 -08:00
Fangrui Song
286c11165e [ELF] Decrease InputSectionBase::entsize to uint32_t
While here, change the sh_addralign argument to uint32_t (InputSection ctor's
argument and the member are uint32_t); add constexpr.
2021-11-28 19:50:33 -08:00
Fangrui Song
e652f3f04a [ELF] Simplify some ctx->outSec with sec. NFC 2021-11-28 19:08:27 -08:00
Fangrui Song
89c0f4553e [ELF] Simplify/remove LinkerScript::switchTo. NFC 2021-11-28 19:05:15 -08:00
Fangrui Song
11291326cd [ELF] Support --oformat= beside Separate --oformat
Both GNU ld's manpage and ours use --oformat= as the canonical form.
It's odd that we do not support it...
2021-11-28 18:44:23 -08:00
Fangrui Song
b5f1fa3e5c [ELF][test] --oformat binary: Check that SIZEOF_HEADERS==0 2021-11-28 18:34:36 -08:00
Fangrui Song
1164c4b375 [ELF] Simplify/remove LinkerScript::output and advance. NFC 2021-11-28 16:58:06 -08:00
Fangrui Song
e80a0b353c [ELF] Remove unneeded getOutputSectionVA. NFC
I attempted to remove it 1 or 2 year ago but kept it just to have a good
diagnostic in case the output section is nullptr (should be impossible).
It is long enough that we haven't seen such a case.
2021-11-28 16:17:10 -08:00
Fangrui Song
85e50c1080 [ELF] Inline InputSection::getOffset into callers and remove it. NFC
This is an unneeded abstraction which may cause confusion:
SectionBase::getOffset has the same name but hard codes -1 as the size of OutputSection.
2021-11-28 16:09:04 -08:00
Fangrui Song
7ea662e2dd [ELF] Replace one make_unique from r316378 with a stack object. NFC 2021-11-28 15:32:29 -08:00
Fangrui Song
25c7ec4fc6 [ELF] Simplify OutputSection::sectionIndex assignment. NFC
And improve comments.
2021-11-28 14:56:29 -08:00
Fangrui Song
d060cc1f98 [ELF] Fix out-of-bounds write in memset(&Out::first, ...)
Fix r285764: there is no guarantee that Out::first is placed before other
static data members of `struct Out`. After `bufferStart` was introduced, this
out-of-bounds write is destined in many compilers. It is likely benign, though.

And move `Out::elfHeader->size` assignment beside `Out::elfHeader->sectionIndex`
2021-11-28 14:47:57 -08:00
Fangrui Song
cecc6893a0 [ELF] Simplify assignFileOffsets
There is a difference with non-SHF_ALLOC SHT_NOBITS when off%sh_addralign!=0
which doesn't happen/matter in practice.
2021-11-28 13:44:42 -08:00
Fangrui Song
f9a4d9aa03 [ELF] -z separate-*: Use max-page-size instead of common-page-size for text/non-SHF_ALLOC transition and writeTrapInstr
For -z separate-code and -z separate-loadable-segments:

When RW is present, the RX to RW transition is aligned with max-page-size.
When RW is absent, the RX to non-SHF_ALLOC transition should use max-page-size as well.
2021-11-28 12:47:50 -08:00
Fangrui Song
6c1c2313d1 [ELF] Simplify assignFileOffsets. NFC 2021-11-28 11:43:59 -08:00
Ard Biesheuvel
da66263b6e [ARM] implement support for ALU/LDR PC-relative group relocations
Currently, LLD does not support the complete set of ARM group relocations.
Given that I intend to start using these in the Linux kernel [0], let's add
support for these.

This implements the group processing as documented in the ELF psABI. Notably,
this means support is dropped for very far symbol references that also carry a
small component, where the immediate is rotated in such a way that only part of
it wraps to the other end of the 32-bit word. To me, it seems unlikely that
this is something anyone could be relying on, but of course I could be wrong.

[0] https://lore.kernel.org/r/20211122092816.2865873-8-ardb@kernel.org/

Reviewed By: peter.smith, MaskRay

Differential Revision: https://reviews.llvm.org/D114172
2021-11-27 10:26:37 +01:00
Fangrui Song
6fa8f7beb1 [ELF][test] Test that .o definition does not inherit .so STV_PROTECTED
Test %t2.so %t.o beside %t.o %t2.so
2021-11-26 15:00:10 -08:00
Fangrui Song
f1ba48d508 [ELF] Simplify Symbol::extract. NFC 2021-11-26 14:10:55 -08:00
Fangrui Song
3b4dd68de5 [ELF][PPC64] Make --power10-stubs/--no-power10-stubs proper aliases for --power10-stubs={auto,no}
This allows --power10-stubs= and --[no-]power10-stubs to override each other
(they are position dependent in GNU ld).

Also improve --help messages and the manpage.

Note: GNU ld's default "auto" mode uses heuristics to decide whether Power10
instructions are used. Arguably it is a design mistake of R_PPC64_REL24_NOTOC
(acked by the relevant folks on a libc-alpha discussion). We don't implement
"auto", so the default --power10-stubs is the same as "yes".
2021-11-26 11:51:45 -08:00
Fangrui Song
09401dfcf1 [ELF] Rename fetch to extract
The canonical term is "extract" (GNU ld documentation, Solaris's `-z *extract`
options). Avoid inventing a term and match --why-extract. (ld64 prefers "load"
but the word is overloaded too much)

Mostly MFC, except for --help messages and the header row in
--print-archive-stats output.
2021-11-26 10:58:50 -08:00
Fangrui Song
7051aeef7a [ELF] Rename BaseCommand to SectionCommand. NFC
BaseCommand was picked when PHDRS/INSERT/etc were not implemented. Rename it to
SectionCommand to match `sectionCommands` and make it clear that the commands
are used in SECTIONS (except a special case for SymbolAssignment).

Also, improve naming of some BaseCommand variables (base -> cmd).
2021-11-25 20:24:23 -08:00
Fangrui Song
e40e17fcaf [ELF] Make ExprValue smaller. NFC' 2021-11-25 16:55:06 -08:00
Fangrui Song
6188fd4957 [ELF] Rename OutputSection::sectionCommands to commands. NFC
This partially reverts r315409: the description applies to LinkerScript, but not
to OutputSection.

The name "sectionCommands" is used in both LinkerScript::sectionCommands and
OutputSection::sectionCommands, which may lead to confusion.
"commands" in OutputSection has no ambiguity because there are no other types
of commands.
2021-11-25 16:47:07 -08:00
Fangrui Song
ff0d9e6cfa [ELF] Remove redundant part.dynSymTab creation. NFC 2021-11-25 14:42:22 -08:00
Fangrui Song
5ca54c6686 [ELF] Simplify GnuHashSection::write. NFC 2021-11-25 14:23:25 -08:00
Fangrui Song
55c14d6dbf [ELF] Simplify DynamicSection content computation. NFC
The new code computes the content twice, but avoides the tricky
std::function<uint64_t()>. Removed 13KiB code in a Release build.
2021-11-25 14:12:34 -08:00
Fangrui Song
6ca8fde226 [ELF] Emit DF_STATIC_TLS only for -shared
This matches GNU ld and saves 2 words for executables.
2021-11-24 23:17:13 -08:00
Fangrui Song
5922dd91f8 [ELF] Rename hasStaticTlsModel to hasTlsIe
and remove unneeded atomic.
2021-11-24 21:06:04 -08:00