Commit Graph

15321 Commits

Author SHA1 Message Date
Fangrui Song 1a590232f4 [ELF] Optimize "Strip sections"
If SHT_LLVM_SYMPART is unused, don't iterate over inputSections.
If neither --strip-debug/--strip-all, don't iterate over inputSections.
2022-03-15 23:15:43 -07:00
Fangrui Song 7c7702b318 [ELF] Move section assignment from initializeSymbols to postParse
https://discourse.llvm.org/t/parallel-input-file-parsing/60164

initializeSymbols currently sets Defined::section and handles non-prevailing
COMDAT groups. Move the code to the parallel postParse to reduce work from the
single-threading code path and make parallel section initialization infeasible.

Postpone reporting duplicate symbol errors so that the messages have the
section information. (`Defined::section` is assigned in postParse and another
thread may not have the information).

* duplicated-synthetic-sym.s: BinaryFile duplicate definition (very rare) now
  has no section information
* comdat-binding: `%t/w.o %t/g.o` leads to an undesired undefined symbol. This
  is not ideal but we report a diagnostic to inform that this is unsupported.
  (See release note)
* comdat-discarded-lazy.s: %tdef.o is unextracted. The new behavior (discarded
  section error) makes more sense
* i386-comdat.s: switched to a better approach working around
  .gnu.linkonce.t.__x86.get_pc_thunk.bx in glibc<2.32 for x86-32.
  Drop the ancient no-longer-relevant workaround for __i686.get_pc_thunk.bx

Depends on D120640

Differential Revision: https://reviews.llvm.org/D120626
2022-03-15 19:24:41 -07:00
Fangrui Song 9b61fff0eb Revert D120626 "[ELF] Move section assignment from initializeSymbols to postParse"
This reverts commit c30e6447c0.
It exposed brittle support for __x86.get_pc_thunk.bx.
Need to think a bit how to support __x86.get_pc_thunk.bx.
2022-03-15 19:00:54 -07:00
Fangrui Song 48a02152ab [ELF][test] Improve i386-linkonce.s
Make it behave like the glibc<2.32 .gnu.linkonce usage that we want to work around.
2022-03-15 18:47:52 -07:00
Sam Clegg 4690bf2ed3 [lld][WebAssembly] Take advantage of extended const expressions when available
In particular we use these in two places:

1. When building PIC code we no longer need to combine output segments
   into a single segment that can be initialized at `__memory_base`.
   Instead each segment can encode its offset from `__memory_base` in
   its initializer.  e.g.

```
(i32.add (global.get __memory_base) (i32.const offset)
```

2. When building PIC code we no longer need to relocation internalized
   global addresses.  We can just initialize them with their correct
   offsets.

Differential Revision: https://reviews.llvm.org/D121420
2022-03-15 17:50:05 -07:00
Jez Ng 8ce3750ff6 [lld-macho] Set FinalDefinitionInLinkageUnit on most LTO externs
Since Mach-O has a two-level namespace (unlike ELF), we can usually set
this property to true.

(I believe this setting is only available in the new LTO backend, so I
can't really use ld64 / libLTO's behavior as a reference here... I'm
just doing what I think is correct.)

See {D119294} for the work done to calculate the `interposable` used in
this diff.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D119506
2022-03-15 20:25:06 -04:00
Fangrui Song c1d4c67718 [ELF] Suppress duplicate symbol error for __x86.get_pc_thunk.bx 2022-03-15 17:20:29 -07:00
Sam Clegg 86c90f9bfd [lld][WebAssembly] Add --unresolved-symbols=import-dynamic
This is a new mode for handling unresolved symbols that allows all
symbols to be imported in the same that they would be in the case of
`-fpie` or `-shared`, but generting an otherwise fixed/non-relocatable
binary.

Code linked in this way should still be compiled with `-fPIC` so that
data symbols can be resolved via imports.

This essentially allows the building of static binaries that have
dynamic imports.  See:
https://github.com/emscripten-core/emscripten/issues/12682

As with other uses of the experimental dynamic linking ABI, this
behaviour will produce a warning unless run with `--experimental-pic`.

Differential Revision: https://reviews.llvm.org/D91577
2022-03-15 15:10:21 -07:00
Fangrui Song 6be457c14d [ELF] Work around not-fully-supported .gnu.linkonce.t.__x86.get_pc_thunk.bx 2022-03-15 14:48:29 -07:00
Jez Ng ceff23c6e3 [lld-macho] -flat_namespace for dylibs should make all externs interposable
All references to interposable symbols can be redirected at runtime to
point to a different symbol definition (with the same name). For
example, if both dylib A and B define symbol _foo, and we load A before
B at runtime, then all references to _foo within dylib B will point to
the definition in dylib A.

ld64 makes all extern symbols interposable when linking with
`-flat_namespace`.

TODO 1: Support `-interposable` and `-interposable_list`, which should
just be a matter of parsing those CLI flags and setting the
`Defined::interposable` bit.

TODO 2: Set Reloc::FinalDefinitionInLinkageUnit correctly with this info
(we are currently not setting it at all, so we're erring on the
conservative side, but we should help the LTO backend generate more
optimal code.)

Reviewed By: modimo, MaskRay

Differential Revision: https://reviews.llvm.org/D119294
2022-03-14 22:18:32 -04:00
Jez Ng 7f3ddf8443 [lld-macho][nfc] Allow Defined symbols to be placed in binding sections
Previously, we only allowed this for DylibSymbols. However, in order to
properly support `-flat_namespace` as well as `-interposable`, we need
to allow this for Defined symbols too. Therefore we hoist the
`lazyBindOffset` and the `stubsHelperIndex` into the parent Symbol
class.

The actual change to support interposition under `-flat_namespace` is in
{D119294}; the NFC changes here have been split out for easier review.

Perf regression isn't stat sig on my 3.2 GHz 16-Core Intel Xeon W linking
chromium_framework:

             base           diff           difference (95% CI)
  sys_time   1.227 ± 0.021  1.234 ± 0.031  [  -0.3% ..   +1.5%]
  user_time  3.665 ± 0.036  3.674 ± 0.035  [  -0.2% ..   +0.7%]
  wall_time  4.596 ± 0.055  4.609 ± 0.064  [  -0.3% ..   +0.9%]
  samples    34             47

Max RSS regression is barely stat sig:

           base                           diff                           difference (95% CI)
  time     1003664356.324 ± 15404053.912  1010380403.613 ± 10578309.455  [  +0.0% ..   +1.3%]
  samples  37                             31

Reviewed By: modimo

Differential Revision: https://reviews.llvm.org/D121351
2022-03-14 22:18:32 -04:00
Vy Nguyen 0d5e27623a Reland "[lld-macho] Avoid using bump-alloc in TrieBuider""
This reverts commit ee7a286cd3.
2022-03-14 19:33:13 -04:00
Sterling Augustine ee7a286cd3 Revert "[lld-macho] Avoid using bump-alloc in TrieBuider"
This reverts commit e049a87f04.

That commit breaks the build with errors of the form:

/usr/local/google/home/saugustine/llvm/llvm-project/lld/MachO/ExportTrie.cpp:148:11: error: definition of implicitly declared destructor
TrieNode::~TrieNode() {
2022-03-14 15:23:04 -07:00
Vy Nguyen e049a87f04 [lld-macho] Avoid using bump-alloc in TrieBuider
The code can be used in multi-threads and the allocator is not thread safe.

fixes PR/54378

Reviewed By: int3, #lld-macho

Differential Revision: https://reviews.llvm.org/D121638
2022-03-14 17:22:53 -04:00
Fangrui Song c30e6447c0 [ELF] Move section assignment from initializeSymbols to postParse
https://discourse.llvm.org/t/parallel-input-file-parsing/60164

initializeSymbols currently sets Defined::section and handles non-prevailing
COMDAT groups. Move the code to the parallel postParse to reduce work from the
single-threading code path and make parallel section initialization infeasible.

Postpone reporting duplicate symbol errors so that the messages have the
section information. (`Defined::section` is assigned in postParse and another
thread may not have the information).

* duplicated-synthetic-sym.s: BinaryFile duplicate definition (very rare) now
  has no section information
* comdat-binding: `%t/w.o %t/g.o` leads to an undesired undefined symbol. This
  is not ideal but we report a diagnostic to inform that this is unsupported.
  (See release note)
* comdat-discarded-lazy.s: %tdef.o is unextracted. The new behavior (discarded
  section error) makes more sense

Depends on D120640

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D120626
2022-03-14 14:13:41 -07:00
Fangrui Song c7cf960d85 [ELF] Set the priority of STB_GNU_UNIQUE the same as STB_WEAK
In GCC -fgnu-unique output, STB_GNU_UNIQUE symbols are always defined relative
to a section in a COMDAT group. Currently `other` cannot be STB_GNU_UNIQUE for
valid input, so this patch is NFC.

If we switch to the model that ignores COMDAT resolution when performing symbol
resolution (D120626), this will fix bogus `relocation refers to a symbol in a
discarded section` errors when mixing -fno-gnu-unique objects with -fgnu-unique
objects.

Differential Revision: https://reviews.llvm.org/D120640
2022-03-14 12:00:15 -07:00
Sam Clegg 9504ab32b7 [WebAssembly] Second phase of implemented extended const proposal
This change continues to lay the ground work for supporting extended
const expressions in the linker.

The included test covers object file reading and writing and the YAML
representation.

Differential Revision: https://reviews.llvm.org/D121349
2022-03-14 08:55:47 -07:00
Nico Weber 17414150cf [lld-link] Tweak winsysroottest.test to have passing links on happy path
Previously, the test checked for a "undefined symbol" error
(instead of the "could not open std*.lib" which would happen without
the flag).

Instead, use /entry: so that the link succeeds.

No behavior change, but maybe makes the test a bit easier to understand.

Differential Revision: https://reviews.llvm.org/D121553
2022-03-14 10:44:26 -04:00
Fangrui Song 7b8fbb796c [ELF] Simplify addCopyRelSymbol with invokeELFT. NFC 2022-03-12 14:08:10 -08:00
Petr Hosek 0c0f6cfb7b [CMake] Rename TARGET_TRIPLE to LLVM_TARGET_TRIPLE
This clarifies that this is an LLVM specific variable and avoids
potential conflicts with other projects.

Differential Revision: https://reviews.llvm.org/D119918
2022-03-11 15:43:01 -08:00
Jez Ng 9b7b21d2f7 [lld-macho] Don't allocate memory in parallelForEach
... since BumpPtrAllocator isn't thread-safe.

Reviewed By: #lld-macho, Roger

Differential Revision: https://reviews.llvm.org/D121458
2022-03-11 13:32:24 -05:00
Fangrui Song 4a8de2832a [ELF] Add -z pack-relative-relocs
GNU ld 2.38 added -z pack-relative-relocs which is similar to
--pack-dyn-relocs=relr but synthesizes the `GLIBC_ABI_DT_RELR` version
dependency if a shared object named `libc.so.*` has a `GLIBC_2.*` version
dependency.

This is used to implement the (as some glibc folks call) version lockout
mechanism. Add this option, because glibc does not want to support
--pack-dyn-relocs=relr which does not add `GLIBC_ABI_DT_RELR`.
See https://maskray.me/blog/2021-10-31-relative-relocations-and-relr for
detail.

Close https://github.com/llvm/llvm-project/issues/53775

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D120701
2022-03-10 19:54:21 -08:00
Jez Ng fc968bcba4
[lld-macho][nfc] Fix formatting in ld64-vs-lld.rst 2022-03-10 18:33:18 -05:00
Jez Ng 4308f031cd [lld-macho] Align cstrings less conservatively
Previously, we aligned every cstring to 16 bytes as a temporary hack to
deal with https://github.com/llvm/llvm-project/issues/50135. However, it
was highly wasteful in terms of binary size.

To recap, in contrast to ELF, which puts strings that need different
alignments into different sections, `clang`'s Mach-O backend puts them
all in one section.  Strings that need to be aligned have the .p2align
directive emitted before them, which simply translates into zero padding
in the object file. In other words, we have to infer the alignment of
the cstrings from their addresses.

We differ slightly from ld64 in how we've chosen to align these
cstrings. Both LLD and ld64 preserve the number of trailing zeros in
each cstring's address in the input object files. When deduplicating
identical cstrings, both linkers pick the cstring whose address has more
trailing zeros, and preserve the alignment of that address in the final
binary. However, ld64 goes a step further and also preserves the offset
of the cstring from the last section-aligned address.  I.e. if a cstring
is at offset 18 in the input, with a section alignment of 16, then both
LLD and ld64 will ensure the final address is 2-byte aligned (since
`18 == 16 + 2`). But ld64 will also ensure that the final address is of
the form 16 * k + 2 for some k (which implies 2-byte alignment).

Note that ld64's heuristic means that a dedup'ed cstring's final address is
dependent on the order of the input object files. E.g. if in addition to the
cstring at offset 18 above, we have a duplicate one in another file with a
`.cstring` section alignment of 2 and an offset of zero, then ld64 will pick
the cstring from the object file earlier on the command line (since both have
the same number of trailing zeros in their address). So the final cstring may
either be at some address `16 * k + 2` or at some address `2 * k`.

I've opted not to follow this behavior primarily for implementation
simplicity, and secondarily to save a few more bytes. It's not clear to me
that preserving the section alignment + offset is ever necessary, and there
are many cases that are clearly redundant. In particular, if an x86_64 object
file contains some strings that are accessed via SIMD instructions, then the
.cstring section in the object file will be 16-byte-aligned (since SIMD
requires its operand addresses to be 16-byte aligned). However, there will
typically also be other cstrings in the same file that aren't used via SIMD
and don't need this alignment. They will be emitted at some arbitrary address
`A`, but ld64 will treat them as being 16-byte aligned with an offset of
`16 % A`.

I have verified that the two repros in https://github.com/llvm/llvm-project/issues/50135
work well with the new alignment behavior.

Fixes https://github.com/llvm/llvm-project/issues/54036.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D121342
2022-03-10 15:18:15 -05:00
serge-sans-paille f06d487dd6 Cleanup includes: WindowsDriver & WindowsManifest
Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D121330
2022-03-10 17:19:06 +01:00
Nico Weber a278250b0f Revert "Cleanup codegen includes"
This reverts commit 7f230feeea.
Breaks CodeGenCUDA/link-device-bitcode.cu in check-clang,
and many LLVM tests, see comments on https://reviews.llvm.org/D121169
2022-03-10 07:59:22 -05:00
serge-sans-paille 7f230feeea Cleanup codegen includes
after:  1061034926
before: 1063332844

Differential Revision: https://reviews.llvm.org/D121169
2022-03-10 10:00:30 +01:00
Fangrui Song 72bedf46c7 [ELF] Inline InputSection::getParent. NFC
Combined with the previous change, lld executable is ~2K smaller and some code
paths using InputSection::getParent are more efficient.

The fragmented headers lead to a design limitation that OutputSection has to be
incomplete, so we cannot use static_cast.
2022-03-08 11:26:12 -08:00
Fangrui Song 6c814931bc [ELF] Don't use multiple inheritance for OutputSection. NFC
Add an OutputDesc class inheriting from SectionCommand. An OutputDesc wraps an
OutputSection. This change allows InputSection::getParent to be inlined.

Differential Revision: https://reviews.llvm.org/D120650
2022-03-08 11:23:42 -08:00
Jez Ng ce2ae38124 [lld-macho] Deduplicate the `__objc_classrefs` section contents
ld64 breaks down `__objc_classrefs` on a per-word level and deduplicates
them. This greatly reduces the number of bind entries emitted (and
therefore the amount of work `dyld` has to do at runtime). For
chromium_framework, this change to LLD cuts the number of (non-lazy)
binds from 912 to 190, getting us to parity with ld64 in this aspect.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D121053
2022-03-08 08:34:04 -05:00
Jez Ng 8ec1033933 [lld-macho] Deduplicate CFStrings during ICF
`__cfstring` has embedded addends that foil ICF's hashing / equality
checks. (We can ignore embedded addends when doing ICF because the same
information gets recorded in our Reloc structs.) Therefore, in order to
properly dedup CFStrings, we create a mutable copy of the CFString and
zero out the embedded addends before performing any hashing / equality
checks.

(We did in fact have a partial implementation of CFString deduplication
already. However, it only worked when the cstrings they point to are at
identical offsets in their object files.)

I anticipate this approach can be extended to other similar
statically-allocated struct sections in the future.

In addition, we previously treated all references with differing addends
as unequal. This is not true when the references are to literals:
different addends may point to the same literal in the output binary. In
particular, `__cfstring` has such references to `__cstring`. I've
adjusted ICF's `equalsConstant` logic accordingly, and I've added a few
more tests to make sure the addend-comparison code path is adequately
covered.

Fixes https://github.com/llvm/llvm-project/issues/51281.

Reviewed By: #lld-macho, Roger

Differential Revision: https://reviews.llvm.org/D120137
2022-03-08 08:34:03 -05:00
Jez Ng 0405920c5f Re-land [lld-macho][nfc] Don't use `stubsHelperIndex` in ICF hash
Previous attempt was commit 112135e774 and
reverted in d86d431814.
2022-03-07 16:58:00 -05:00
Nico Weber d86d431814 Revert "[lld-macho][nfc] Don't use `stubsHelperIndex` in ICF hash"
This reverts commit 112135e774.
Breaks lld/test/MachO/{icf.s,cfstring-dedup.s,invalid/cfstring.s}
2022-03-07 13:50:38 -05:00
Jez Ng ad1c32e9b3 [lld-macho][nfc] Reduce size of icfEqClass hash
... from a `uint64_t` to a `uint32_t`. (LLD-ELF uses a `uint32_t` too.)

About a 1.7% reduction in peak RSS when linking chromium_framework on my
3.2 GHz 16-Core Intel Xeon W Mac Pro, and no stat sig change in wall
time.

           </Users/jezng/test2.sh ["before"]>  </Users/jezng/test2.sh ["after"]>  difference (95% CI)
  RSS      1003036672.000 ± 9891065.259        985539505.231 ± 10272748.749       [  -2.3% ..   -1.2%]
  samples  27                                  26

             base           diff           difference (95% CI)
  sys_time   1.277 ± 0.023  1.277 ± 0.024  [  -0.9% ..   +0.9%]
  user_time  6.682 ± 0.046  6.598 ± 0.043  [  -1.6% ..   -0.9%]
  wall_time  5.904 ± 0.062  5.895 ± 0.063  [  -0.7% ..   +0.4%]
  samples    46             28

No appreciable change (~0.01%) in number of `equals` comparisons either:

Before:

  ld64.lld: ICF needed 8 iterations
  ld64.lld: equalsConstant() called 701643 times
  ld64.lld: equalsVariable() called 3438526 times

After:

  ld64.lld: ICF needed 8 iterations
  ld64.lld: equalsConstant() called 701729 times
  ld64.lld: equalsVariable() called 3438526 times

Reviewed By: #lld-macho, MaskRay, thakis

Differential Revision: https://reviews.llvm.org/D121052
2022-03-07 12:36:28 -05:00
Jez Ng 112135e774 [lld-macho][nfc] Don't use `stubsHelperIndex` in ICF hash
The existing hashing of stubsHelperIndex has mostly been a no-op* for
some time now (ever since we made ICF run before dylib symbols get their
stubs indices assigned). I guess we could consider hashing the name +
filename of the DylibSymbol instead, but I'm not sure the overhead's
worth it... moreover, LLD/ELF only hashes their Defined symbols as well.

*: Technically it does change the hash value since stubsHelperIndex is
initialized to `UINT32_MAX` by default. But since all stubsHelperIndex
values are the same at when ICF runs, they don't add any useful
information to the hash.
2022-03-07 12:36:28 -05:00
Jez Ng 7028799ca3 [lld-macho][nfc] Rename isec -> referentIsec to avoid shadowing
I found the shadowing a bit confusing
2022-03-07 12:36:28 -05:00
Jez Ng 64cc719766 [lld-macho][nfc] Track # of ICF calls to `equals*` methods
This is debug code that is disabled by default. It'll provide a easy way
to figure out the impact (if any) of tweaking ICF's hashing algorithm
(since a poor quality hash will result in many more `equals*` calls).

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D121051
2022-03-07 12:36:27 -05:00
Jez Ng 53e7eef43f [lld-macho][nfc] Use llvm::function_ref instead of std::function 2022-03-07 12:36:27 -05:00
Jez Ng c416f3fafd [lld-macho][nfc] Remove file statics from ICF.cpp
This gets us closer to the [LLD-as-a-library goal][1].

[1]: https://lists.llvm.org/pipermail/llvm-dev/2021-June/151184.html

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D121050
2022-03-07 12:36:26 -05:00
Fangrui Song a815424cc5 Reland D119909 [ELF] Parallelize initializeLocalSymbols
ObjFile::parse combines symbol initialization and resolution. Many tasks
unrelated to symbol resolution can be postponed and parallelized. This patch
extracts local symbol initialization and parallelizes it.

Technically the new function initializeLocalSymbols can be merged into
ObjFile::postParse, but functions like getSrcMsg may access the
uninitialized (all nullptr) local part of InputFile::symbols.

Linking chrome: 1.02x as fast with glibc malloc, 1.04x as fast with mimalloc

Depends on f456c3ae3f and D119908

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D119909
2022-03-04 19:00:10 -08:00
Fangrui Song f456c3ae3f [ELF] Move addWrappedSymbols before postParseObjectFile
addWrappedSymbols may trigger archive extraction: split stack implementation
uses --wrap=pthread_create, which extracts libgcc.a(generic-morestack-thread.o).

This fixes the regression caused by 09602d3b47 by
making the invariant satisfied: no more non-compileBitcodeFiles object file is
produced at postParseObjectFile.
2022-03-04 18:56:37 -08:00
Jorge Gorbe Moya 449b649fec Revert "[ELF] Parallelize initializeLocalSymbols"
This reverts commit 09602d3b47.
2022-03-04 15:01:17 -08:00
Jez Ng 72c5b26f3d [lld-macho][nfc] Use %X in mapfile test
LLD (and ld64) emits uppercase hex addresses in the mapfile. The
map-file.s test passes right now because the addresses we emit happen
not to include any alphabets, but that can easily change.

I noticed this while dealing with
https://github.com/llvm/llvm-project/issues/54184.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D120941
2022-03-04 14:21:17 -05:00
Jez Ng 984197612c [lld-macho][nfc] Rename some tests for consistency
Now all the tests that cover symbol resolution / precedence have
"resolution" in their filename.

I also added a couple of extra comments.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D120938
2022-03-04 14:21:16 -05:00
Jez Ng 070af48d13 [lld-macho][nfc] Decouple tapi-link.s test from libSystem
If we fix https://github.com/llvm/llvm-project/issues/54184, we will end
up including libSystem in every %lld invocation, which would break
tapi-link.s as it assumes that libSystem isn't directly linked (instead
it goes through libReexportSystem).

Let's remove this unnecessary coupling, as well as use `split-file`
instead of having a separate file under `Inputs`.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D120939
2022-03-03 19:48:59 -05:00
Jez Ng dd29597e10 [LTO] Initialize canAutoHide() using canBeOmittedFromSymbolTable()
Per discussion on
https://reviews.llvm.org/D59709#inline-1148734, this seems like the
right course of action. `canBeOmittedFromSymbolTable()` subsumes and
generalizes the previous logic. In addition to handling `linkonce_odr`
`unnamed_addr` globals, we now also internalize `linkonce_odr` +
`local_unnamed_addr` constants.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D120173
2022-03-03 19:04:11 -05:00
Jez Ng 5c268743da [lld-macho][nfc] Use %lld-watchos substitution in bind-opcodes.s
Previously, we were using a syslibroot that pointed to macos while
linking against arch arm64_32, which didn't really make sense. It isn't
currently an issue, but will be if we add the `-lSystem` as part of
dealing with https://github.com/llvm/llvm-project/issues/54184.
2022-03-03 19:00:28 -05:00
Jez Ng f7547558c9 [lld-macho][nfc] Avoid using absolute addresses in cgprofile-icf.s
If we fix https://github.com/llvm/llvm-project/issues/54184, the
`dyld_stub_binder` symbol will get included in every output dylib. This
would cause the addresses of the other symbols to shift, breaking the
test as it currently stands. Let's make the test more flexible.

Reviewed By: lgrey

Differential Revision: https://reviews.llvm.org/D120940
2022-03-03 19:00:28 -05:00
Martin Storsjö 4c3b74b7f5 [LLD] [COFF] Order .debug_* sections at the end, to avoid leaving gaps if stripped
So far, we sort all discardable sections at the end, with only some
extra logic to make sure that the .reloc section is at the start
of that group of sections. But if there are other discardable
sections, other than .reloc, they must also be ordered before
.debug_* sections, to avoid leaving gaps if the executable is
stripped.

(Stripping executables doesn't remove all discardable sections,
only the ones named .debug_*).

Rust binaries seem to include a .rmeta section, which is marked
discardable. This fixes stripping such binaries if built with
dwarf debug info included.

This fixes issues observed in MSYS2 in
https://github.com/msys2/MINGW-packages/pull/10555.

Differential Revision: https://reviews.llvm.org/D120805
2022-03-03 10:08:51 +02:00
Douglas Yung e81e5d788c Add "REQUIRES: x86" to test as it calls llc with an x86_64 triple. 2022-03-02 11:12:41 -08:00
Sam Clegg 1cf6ebc0e9 [lld][WebAssembly] Improve error reporting for bad ar archive members
Show the name of of the archive in the error message as well as the name
of the object within it.

Differential Revision: https://reviews.llvm.org/D120689
2022-03-01 15:21:53 -08:00
Zequan Wu 5c9e20d7d0 [PDB] Add char8_t type
Differential Revision: https://reviews.llvm.org/D120690
2022-03-01 13:39:51 -08:00
Martin Storsjö 9ffeaaa0ea [LLD] [COFF] Use StringTableBuilder to optimize the string table
This does tail merging (and deduplication) of the strings.

On a statically linked clang.exe, this shrinks the ~17 MB string
table by around 0.5 MB. This adds ~160 ms to the linking time
which originally was around 950 ms.

For cases where `-debug:symtab` or `-debug:dwarf` isn't set, the
string table is only used for long section names, where this
shouldn't make any difference at all.

Differential Revision: https://reviews.llvm.org/D120677
2022-03-01 18:44:03 +02:00
Martin Storsjö 9dd2d50984 [LLD] [COFF] Use the new encodeSectionName() helper for long section names
The previous code used an unbounded sprintf, which in theory can
overflow, writing either the null terminator or the last digits
into the next struct member.

In practice, in LLD, all long section names are written sequentially
first at the start of the string table, followed by all the long
symbol names. Due to this, even if the total string table would
end up large, the long section names have fairly short offsets,
which is why this hasn't been an issue in practice.

I don't think it's worth trying to write a test that produces an
executable with enough long section names to make the section names
themselves exceed 10^6 bytes, which is currently necessary to trigger
faults with the previous form.

Differential Revision: https://reviews.llvm.org/D120676
2022-03-01 11:33:02 +02:00
Fangrui Song 87034ad2a4 [ELF] isKnownZFlag: move known literal flags to an array. NFC
The chain of == comparisons is a bit unwieldy to update.

While here, sort the entries alphabetically.
2022-02-28 23:23:33 -08:00
Jez Ng a552fb2a86 [lld-macho] Have relocation address included in range-check error message
This makes it easier to debug those errors. See e.g. https://github.com/llvm/llvm-project/issues/52767#issuecomment-1028713943

We take the approach of 'reverse-engineering' the InputSection from the
output buffer offset. This provides for a cleaner Target API, and is
similar to LLD-ELF's implementation of getErrorPlace().

Reviewed By: #lld-macho, Roger

Differential Revision: https://reviews.llvm.org/D118903
2022-02-28 21:56:38 -05:00
Fangrui Song 9e9c86fd67 [ELF] Change some non-null pointer parameters to references. NFC
To decrease difference for D120650. Also, rename some `OutputSection *sec` (and
`cmd`) to the more common `osec`.
2022-02-28 11:19:00 -08:00
Fangrui Song b07ef4d566 [ELF] Rename Symbol::compare to shouldReplace. NFC
The return value is not a boolean instead of a tri-state.
Suggested by Peter Smith in D120640.
2022-02-28 18:25:21 +00:00
Fangrui Song 8d01ac75e7 [ELF] Replace an unneeded dyn_cast_or_null with dyn_cast. NFC 2022-02-28 00:50:06 -08:00
Fangrui Song fee78961f5 [ELF] Optimize SectionBase::Kind values to make isa<InputSection> more efficient. NFC
Surprisingly my lld executable is 1.5KiB smaller.
2022-02-28 00:24:25 -08:00
Fangrui Song bb3eeac773 [ELF] Make InputSection::classof inline. NFC 2022-02-28 00:16:45 -08:00
Fangrui Song 4976d1fe58 [ELF] Move SyntheticSection check from InputSection::writeTo to OutputSection::writeTo. NFC
Simplify code and make the heavyweight operation to the call site so that it is
clearer how to improve the inefficient scheduling in the future.
2022-02-27 23:28:52 -08:00
Fangrui Song d07ff99591 [ELF] Enforce double-dash form --error-limit
It's ld.lld specific and by convention we enforce the double-dash form to avoid
collision with the short option -e (--entry).
2022-02-27 20:49:36 +00:00
Fangrui Song 87e6251d66 [ELF] Use --error-limit instead of -error-limit 2022-02-27 20:47:37 +00:00
Fangrui Song d14d8664e3 [ELF] Change global variable backwardReferences to a LinkerDriver member variable. NFC
Similar to whyExtract.
2022-02-27 20:33:28 +00:00
Fangrui Song 7fd3849b35 [ELF] Move --print-archive-stats= and --why-extract= beside --warn-backrefs report
So that early errors don't suppress their output.
2022-02-27 20:23:09 +00:00
Fangrui Song bd448f01a6 [ELF] BitcodeFile: resolve defined symbols before undefined symbols
This ports D95985 for ELF relocatable object files to BitcodeFile.
2022-02-27 05:37:08 +00:00
Joao Moreira 9d7001eba9 [ELF][X86] Don't create IBT .plt if there is no PLT entry
https://github.com/ClangBuiltLinux/linux/issues/1606
When GNU_PROPERTY_X86_FEATURE_1_IBT is enabled, ld.lld will create .plt output
section even if there is no PLT entry. Fix this by implementing
IBTPltSection::isNeeded instead of using the default code path (which always
returns true).

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D120600
2022-02-26 03:55:40 +00:00
Fangrui Song 767e64fc11 [ELF] Support some absolute/PC-relative relocation types for REL format
ctfconvert seems to use REL-format `.rel.SUNW_dof` for 32-bit architectures.
```
Binary file usr/ports/lang/perl5.32/work/perl-5.32.1/dtrace_mini.o matches
[alfredo.junior@dell-a ~/tmp/llvm-bug]$ readelf -r dtrace_mini.o

Relocation section (.rel.SUNW_dof):
r_offset r_info   r_type              st_value st_name
00000184 0000281a R_PPC_REL32         00000000 $dtrace1772974259.Perl_dtrace_probe_load
```

Support R_PPC_REL32 to fix `ld.lld: error: drti.c:(.SUNW_dof+0x4E4): internal linker error: cannot read addend for relocation R_PPC_REL32`.
While here, add some common relocation types for AArch64, PPC, and PPC64.
We perform minimum tests.

Reviewed By: adalava, arichardson

Differential Revision: https://reviews.llvm.org/D120535
2022-02-25 19:25:18 +00:00
Sam Clegg 4c75521ce0 [MC][WebAssembly] Fix crash when relocation addend underlows U32
For the object file writer we need to allow the underflow (ar write
zero), but for the final linker output we should probably generate an
error (I've left that as a TODO for now).

Fixes: https://github.com/llvm/llvm-project/issues/54012

Differential Revision: https://reviews.llvm.org/D120522
2022-02-25 07:13:15 -08:00
Fangrui Song 09602d3b47 [ELF] Parallelize initializeLocalSymbols
ObjFile::parse combines symbol initialization and resolution. Many tasks
unrelated to symbol resolution can be postponed and parallelized. This patch
extracts local symbol initialization and parallelizes it.

Technically the new function initializeLocalSymbols can be merged into
ObjFile::postParse, but functions like getSrcMsg may access the
uninitialized (all nullptr) local part of InputFile::symbols.

Linking chrome: 1.02x as fast with glibc malloc, 1.04x as fast with mimalloc

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D119909
2022-02-24 20:05:59 -08:00
Fangrui Song 19e37a7415 [ELF] Update comment. NFC 2022-02-24 14:09:00 -08:00
Fangrui Song 6d94340809 [ELF] Simplify resolveDefined and resolveCommon
This is NFC for valid input (COMMON symbols cannot be weak or versioned).
2022-02-24 14:08:06 -08:00
Reid Kleckner da11f17e90 [lld/MachO] Fix +asserts build after recent change 2022-02-24 13:12:48 -08:00
Fangrui Song b6a71d9e12 [ELF][test] Remove invalid weak COMMON tests
GNU as reports `Error: symbol `foo' can not be both weak and common`,
though LLVM integrated assembler does not report an error yet.
2022-02-24 12:54:16 -08:00
Jez Ng 850592ec14 [lld-macho] Implement -why_live (without perf overhead)
This was based off @thakis' draft in {D103517}. I employed templates to ensure
the support for `-why_live` wouldn't slow down the regular non-why-live code
path.

No stat sig perf difference on my 3.2 GHz 16-Core Intel Xeon W:

             base           diff           difference (95% CI)
  sys_time   1.195 ± 0.015  1.199 ± 0.022  [  -0.4% ..   +1.0%]
  user_time  3.716 ± 0.022  3.701 ± 0.025  [  -0.7% ..   -0.1%]
  wall_time  4.606 ± 0.034  4.597 ± 0.046  [  -0.6% ..   +0.2%]
  samples    44             37

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D120377
2022-02-24 15:49:36 -05:00
Fangrui Song 15617cdb55 [ELF] Simplify --fortran-common. NFC 2022-02-24 12:21:40 -08:00
Fangrui Song 4129890dd8 [ELF] De-template Symbol::resolveLazy. NFC 2022-02-24 12:20:05 -08:00
Fangrui Song 5bc4e15c6e [ELF] Set config->exportDynamic to true if config->shared. NFC 2022-02-24 11:31:58 -08:00
Fangrui Song 9f9ac3464e [ELF] Symbols.h: remove #include "InputFiles.h" 2022-02-23 21:36:45 -08:00
Fangrui Song 8ca46bba23 [ELF] Move isUsedInRegularObj assignment from ctor to call sites. NFC
This removes the tricky
`isUsedInRegularObj(!file || file->kind() == InputFile::ObjKind)`
and the copy from `Symbol::mergeProperties`.
2022-02-23 21:32:50 -08:00
Fangrui Song 00b6d2106b [ELF][test] Avoid race on a.out 2022-02-23 20:48:49 -08:00
Fangrui Song 38fbedab32 [ELF] Don't rely on Symbols.h's transitive inclusion of InputFiles.h. NFC 2022-02-23 20:44:34 -08:00
Fangrui Song ba061713d3 [ELF] Move TLS mismatch error from Symbol::replace to postParse
* detect `def_tls.o undef_nontls.o` violation
* place error checking code (checking duplicate symbol) together
* allow `--defsym tls1=tls2 def_tls.o`

As a degraded error checking, `--defsym tls1=42` violation will not be detected.
2022-02-23 20:34:48 -08:00
Fangrui Song b01430a04f [ELF] Don't rely on Symbols.h's transitive inclusion of InputFiles.h. NFC 2022-02-23 19:18:24 -08:00
Fangrui Song 47d18be58b [ELF] Remove SharedSymbol::getFile. NFC
Symbol.h depends on InputFiles.h. This change moves us toward dropping the
weird dependency.

The call sites will become slightly uglier (`cast<SharedFile>(s->file)`), but
the compromise is acceptable.
2022-02-23 17:57:52 -08:00
Fangrui Song 53c5bd9da2 [ELF][test] Fix edata-etext.s 2022-02-23 13:29:21 -08:00
Fangrui Song fc0aa8424c [ELF] Check COMMON symbols for PROVIDE and don't redefine COMMON symbols edata/end/etext
In GNU ld, the definition precedence is: regular symbol assignment > relocatable object definition > `PROVIDE` symbol assignment.

GNU ld's internal linker scripts define the non-reserved (by C and C++)
edata/end/etext with `PROVIDE` so the relocatable object definition takes
precedence. This makes sense because `int end;` is valid.

We currently redefine such symbols if they are COMMON, but not if they are
regular definitions, so `int end;` with -fcommon is essentially a UB in ld.lld.
Fix this (also improve consistency and match GNU ld) by using the
`isDefined` code path for `isCommon`. In GNU ld, reserved identifiers like
`__ehdr_start` do not use `PROVIDE`, while we treat them all as `PROVIDE`, this
seems fine.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D120389
2022-02-23 10:15:42 -08:00
Jez Ng e42ad84ba0 [lld-macho][nfc] Refactor MarkLive
This mirrors the code structure in `lld/ELF`. It also paves the way for
an upcoming diff where I templatize things.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D120376
2022-02-23 08:58:26 -05:00
Jez Ng 8386eb23bf [lld-macho][nfc] Move ICF-specific logic into ICF.cpp
This mirrors the code organization in `lld/ELF`.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D120378
2022-02-23 08:58:25 -05:00
serge-sans-paille eb4c860811 Cleanup llvm/DebugInfo/PDB headers
accumulated preprocessed size:
before: 1065515095
after: 1065629059

Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D120195
2022-02-23 10:31:34 +01:00
Fangrui Song b96fc4860f [ELF][test] Fix CU address_size in some gdb-index tests
Revert 251640ab57 which fixed the wrong thing.

While here, add `2>&1 | count 0` to assert no warning from lib/DebugInfo/DWARF.
2022-02-22 21:42:15 -08:00
Fangrui Song 251640ab57 [ELF][test] Terminate .debug_info with a null entry to fix warnings 2022-02-22 19:20:56 -08:00
Jez Ng 606cb8548a [lld] Require C++14 in LLD standalone build
This is what the Clang standalone build does too. And setting this
seems to be required to get the standalone build to work on my Mac.

Reviewed By: #lld-macho, MaskRay, Ericson2314, smeenai

Differential Revision: https://reviews.llvm.org/D120269
2022-02-22 18:15:29 -05:00
Nico Weber 746bd89000 fix comment typo to cycle bots 2022-02-22 16:25:51 -05:00
Fangrui Song 88d66f6ed1 [ELF] Move duplicate symbol check after input file parsing
https://discourse.llvm.org/t/parallel-input-file-parsing/60164

To decouple symbol initialization and section initialization, `Defined::section`
assignment should be postponed after input file parsing. To avoid spurious
duplicate definition error due to two definitions in COMDAT groups of the same
signature, we should postpone the duplicate symbol check.

The function is called postScan instead of a more specific name like
checkDuplicateSymbols, because we may merge Symbol::mergeProperties into
postScan. It is placed after compileBitcodeFiles to apply to ET_REL files
produced by LTO. This causes minor diagnostic regression
for skipLinkedOutput configurations: ld.lld --thinlto-index-only a.bc b.o
(bitcode definition prevails) won't detect duplicate symbol error. I think this
is an acceptable compromise. The important cases where (a) both files are
bitcode or (b) --thinlto-index-only is unused are still detected.

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D119908
2022-02-22 10:07:58 -08:00
Fangrui Song ae1ba6194f [ELF] Replace uncompressed InputSectionBase::data() with rawData. NFC
In many call sites we know uncompression cannot happen (non-SHF_ALLOC, or the
data (even if compressed) must have been uncompressed by a previous pass).
Prefer rawData in these cases. data() increases code size and prevents
optimization on rawData.
2022-02-21 00:39:26 -08:00
Sam Clegg 70aa11187e [lld][WebAssembly] Convert a bunch more tests to asm. NFC
Differential Revision: https://reviews.llvm.org/D120060
2022-02-18 16:30:08 -08:00
Fangrui Song c12d49c4e2 [ELF] Remove .strtab deduplication
D118577: the 0.1~1.1% .strtab size reduction does not justify the 3~6%
link time increase. Just remove it even for -O2. release/14.x
has D118577 and the release note mentioned that this may be removed.

Fix https://github.com/ClangBuiltLinux/linux/issues/1578
caused by D118577 (empty string not in stringMap).
2022-02-18 14:54:10 -08:00
Fangrui Song 93e2b59c07 [ELF][test] Avoid non-portable |& in notest.s 2022-02-18 12:32:27 -08:00
Fangrui Song cb0a4bb5be [ELF] Change (NOLOAD) section type mismatch error to warning
Making a (NOLOAD) section SHT_PROGBITS is fishy (the user may expect all-zero
content, but the linker does not check that), but some projects (e.g. Linux
kernel https://github.com/ClangBuiltLinux/linux/issues/1597) traditionally rely
on the behavior. Issue a warning to not break them.
2022-02-18 11:20:36 -08:00
Jez Ng fd3669c256 [lld-macho] Improve hiding of unnamed_addr symbols
Symbols for which `canBeOmittedFromSymbolTable()` is true should be
treated as private externs. This diff tries to do that by unsetting the
ExportDynamic bit. It seems to mostly work with the FullLTO backend, but
with the ThinLTO backend, the `local_unnamed_addr` symbols still fail to
be properly hidden. Nonetheless, this is a step in the right direction.

I've documented all the remaining differences between our behavior and
LD64's in the lto-internalized-unnamed-addr.ll test.

See also https://discourse.llvm.org/t/mach-o-lto-handling-of-linkonce-odr-unnamed-addr/60015

Reviewed By: #lld-macho, thevinster

Differential Revision: https://reviews.llvm.org/D119767
2022-02-18 12:09:38 -05:00
Fangrui Song 66f8ac8d36 [ELF] Support (TYPE=<value>) to customize the output section type
The current output section type allows to set the ELF section type to
SHT_PROGBITS or SHT_NOLOAD. This patch allows an arbitrary section value
to be specified. Some common SHT_* literal names are supported as well.

```
SECTIONS {
  note (TYPE=SHT_NOTE) : { BYTE(8) *(note) }
  init_array ( TYPE=14 ) : { QUAD(14) }
  fini_array (TYPE = SHT_FINI_ARRAY) : { QUAD(15) }
}
```

When `sh_type` is specified, it is an error if an input section has a different type.

Our syntax is compatible with GNU ld 2.39 (https://sourceware.org/bugzilla/show_bug.cgi?id=28841).

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D118840
2022-02-17 12:10:58 -08:00
Fangrui Song 941f06282a [lld] Make error handling functions opaque
The inline `lld::error` expands to two function calls `errorHandler` and `error`
where the latter is opaque. Move the functions to .cpp files to decrease code
size.

My x86-64 lld executable is 9KiB smaller.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D120002
2022-02-17 11:54:57 -08:00
Leonard Grey a52b9102d1 [lld-macho] Allow order files and call graph sorting to be used together
If both an order file and a call graph profile are present, the edges of the
call graph which use symbols present in the order file are not used. All of
the symbols in the order file will appear at the beginning of the section just
as they do currently. In other words, the highest priority derived from the
call graph will be below the lowest priority derived from the order file.

Practically, this change renames CallGraphSort.{h,cpp} to SectionPriorities.{h,cpp},
and most order file and call graph profile related code is moved into the new
file to reduce duplication.

Differential Revision: https://reviews.llvm.org/D117354
2022-02-17 14:19:34 -05:00
Jez Ng 69297cf639 [lld-macho] Don't include CommandFlags.h in CommonLinkerContext.h
Main motivation: including `llvm/CodeGen/CommandFlags.h` in
`CommonLinkerContext.h` means that the declaration of `llvm::Reloc` is
visible in any file that includes `CommonLinkerContext.h`. Since our
cpp files have both `using namespace llvm` and `using namespace
lld::macho`, this results in conflicts with `lld::macho::Reloc`.

I suppose we could put `llvm::Reloc` into a nested namespace, but in general,
I think we should avoid transitively including too many header files in
a very widely used header like `CommonLinkerContext.h`.

RegisterCodeGenFlags' ctor initializes a bunch of function-`static`
structures and does nothing else, so it should be fine to "initialize"
it as a temporary stack variable rather than as a file static.

Reviewed By: aganea

Differential Revision: https://reviews.llvm.org/D119913
2022-02-16 20:05:07 -05:00
Sam Clegg dabbab6861 [lld][WebAssembly] Apply global relocs before data relocs
Since the code for apply data relocations can sometimes use
the values stored in he globals, they need to be relocated
before the data relocations can be run.

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

Differential Revision: https://reviews.llvm.org/D119666
2022-02-16 14:30:39 -08:00
Arthur Eubanks b5c9512df2 [test] Mark archive-as-start-lib.s as unsupported on Windows
gnuwin32 tail does not support the `tail -c +9` syntax.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D119956
2022-02-16 10:27:43 -08:00
Fangrui Song ae62aaa171 [ELF][test] Add --undefine-glob test to lto/duplicated.ll 2022-02-16 09:40:55 -08:00
Peter Kasting c5fb05f663 Reland: Make lld-link work in a non-MSVC shell, add /winsysroot:
This relands 73e585e44d (and 0574b5fc65), with a fix for
the failing test (by using Optional<StringRef>s instead of
making StringRef::empty() mean absence of value).

Differential Revision: https://reviews.llvm.org/D118070
2022-02-16 09:22:39 -05:00
Nemanja Ivanovic d32b875dbc [ELF][test] Fix build break after 20bdd3e232
The added run lines build a bitcode file for x86 and an object file
for whatever the default target is that is running the test. This
causes an incompatibility between the files.
Add the triple to the llvm-mc invocation.
2022-02-16 05:56:25 -06:00
Jez Ng aa108fffec [lld-macho][nfc] Clean up trailing spaces and tabs 2022-02-15 21:33:26 -05:00
Jez Ng 94c28d289a [lld-macho][nfc] Factor out callgraph parsing code
`parseSections()` is a getting a bit large unwieldy, let's factor out
logic where we can.

Other minor changes in this diff:
* `"__cg_profile"` is now a global constexpr
* We now use `checkError()` instead of `fatal()`-ing without handling
  the Error
* Check for `callGraphProfileSort` before checking the section name,
  since the boolean comparison is likely cheaper

Reviewed By: #lld-macho, lgrey, oontvoo

Differential Revision: https://reviews.llvm.org/D119892
2022-02-15 21:13:55 -05:00
Fangrui Song 20bdd3e232 [ELF][test] Improve LTO duplicate symbol test 2022-02-15 17:54:38 -08:00
Sam Clegg d2a0ef9844 [lld][WebAssembly] Don't force the export symbols assiged internal/dummy GOT entries
Symbols with regular GOT entries do need to be exported, but those that
are internalized (and have dymmy/internal GOT entries) need not be
exported.

This happens to fix the failures on the emscripten waterfall where extra
symbols were being exported by the linker (and then later removed by
wasm-opt).

Differential Revision: https://reviews.llvm.org/D119902
2022-02-15 17:29:45 -08:00
Fangrui Song 132553b8c7 [ELF] --exclude-libs: skip local symbols for ET_REL. NFC
Beside the optimization, this will avoid accessing nullptr entries with my
planned change to parallelize initializeLocalSymbols.
2022-02-15 17:02:56 -08:00
Sam Clegg faab70b783 [lld][WebAssemlby] Warn on unknown -z flags
This code mirrors that in lld/ELF/Driver.cpp, as does the new test code.

Differential Revision: https://reviews.llvm.org/D119888
2022-02-15 14:42:04 -08:00
Fangrui Song 53b59fdc52 [ELF][PPC64] Fix assertion failure for branches to hidden undefined weak for -no-pie
Reported by Stefan Pintilie in D119773.

For a branch to a hidden undefined weak symbol, there is an
`assert(sym->getVA());` failure in PPC64LongBranchTargetSection::writeTo for a
-no-pie link. The root cause is that we unnecessarily create the thunk for the
-no-pie link.

Fix this by changing the condition to just `s.isUndefined()`. See the inline
comment.

Rename ppc64-weak-undef-call.s to ppc64-undefined-weak.s to be consistent with
other architectures.

Reviewed By: sfertile, stefanp

Differential Revision: https://reviews.llvm.org/D119787
2022-02-15 12:57:27 -08:00
Fangrui Song 467e1b3aaa [ELF] reportDuplicate: change Symbol * to const Symbol &. NFC 2022-02-15 11:18:31 -08:00
Fangrui Song 3d85424096 [ELF] Parse archives as --start-lib object files
https://maskray.me/blog/2022-01-16-archives-and-start-lib

For every definition in an extracted archive member, we intern the symbol twice,
once for the archive index entry, once for the .o symbol table after extraction.
This is inefficient.

Symbols in a --start-lib ObjFile/BitcodeFile are only interned once because the
result is cached in symbols[i].

Just handle an archive using the --start-lib code path. We can therefore remove
ArchiveFile and LazyArchive. For many projects, archive member extraction ratio
is high and it is a net performance win. Linking a Release build of clang is
1.01x as fast.

Note: --start-lib scans symbols in the same order that llvm-ar adds them to the
index, so in the common case the semantics should be identical. If the archive
symbol table was created in a different order, or is incomplete, this strategy
may have different semantics. Such cases are considered user error.

The `is neither ET_REL nor LLVM bitcode` error is changed to a warning.
Previously an archive may have such members without a diagnostic. Using a
warning prevents breakage.

* For some tests, the diagnostics get improved where we did not consider
  the archive member name: `b.a:` => `b.a(b.o):`.
* `no-obj.s`: the link is now allowed, matching GNU ld
* `archive-no-index.s`: the `is neither ET_REL nor LLVM bitcode` diagnostic is
  demoted to a warning.
* `incompatible.s`: even when an archive is unextracted, we may report an
  "incompatible with" error.

---

I recently decreased sizeof(SymbolUnion) by 8 and decreased memory usage quite a
bit, so retaining `symbols` for un-extracted archive members should not cause a
memory usage problem.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D119074
2022-02-15 09:38:00 -08:00
Sam Clegg 37f422f4ac [WebAssembly] Use GeneralDynamic TLS for exception handling builtins.
These global TLS symbols are shared across all shared libraries and
therefor should not be assumed to be local to the current module.

Also add new error in the linker when TLS relocations are used against
undefined symbols.  TLS relocations are offsets into the current modules
tls data segment, and don't make sense for undefined symbols which are
modeled as global imports.

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

Differential Revision: https://reviews.llvm.org/D119630
2022-02-14 14:08:32 -08:00
Fangrui Song fb40a61b2f [ELF][docs] Document "Output section type" 2022-02-14 09:52:20 -08:00
Fangrui Song f2fd1587bc [ELF] Fix dead initialization. NFC
Reported by scan-build.
2022-02-14 09:27:42 -08:00
Fangrui Song 8b01b638d0 [ELF] demoteSharedSymbols: make binding more appropriate for lazy symbols. NFC
The binding will matter if we remove the `sym->replace(und)` kludge from
initializeSymbols.
While here, rename the function to be more appropriate.
2022-02-12 20:43:40 -08:00
Douglas Yung 437d4e01fe Revert "try to fix windows build after 73e585e44d" and
Revert "Reland "[lld/coff] Make lld-link work in a non-MSVC shell, add /winsysroot:""

This reverts commit 0574b5fc65 and 73e585e44d.

This change is causing the test Driver/cl-options.c to fail on Windows buildbots.
https://lab.llvm.org/staging/#/builders/204/builds/1343
2022-02-11 23:47:53 -08:00
Jez Ng 103e1d934a [lld-macho] Unset ExportDynamic where possible for LTO
By unsetting this property, we are now able to internalize more symbols
during LTO. I compared the output of `-save-temps` for both LLD and
ld64, and we now match ld64's behavior as far as `lto-internalize.ll` is
concerned.

(Thanks @smeenai for working on an initial version of this diff!)

Fixes https://github.com/llvm/llvm-project/issues/50574.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D119372
2022-02-11 22:26:19 -05:00
Roger Kim dafe4c0b5c [Mach-O][NFC] Reorder map file tests
We are just grouping the files and the tests together.

Reviewed By: int3, #lld-macho

Differential Revision: https://reviews.llvm.org/D119456
2022-02-11 19:42:20 -05:00
Roger Kim 4f2c46c35c Print C-string literals in mapfile
This diff has the C-string literals printed into the mapfile in the symbol table like how ld64 does.

Here is what ld64's mapfile looks like with C-string literals:
```
# Path: out
# Arch: x86_64
# Object files:
[  0] linker synthesized
[  1] foo.o
# Sections:
# Address       Size            Segment Section
0x100003F7D     0x0000001D      __TEXT  __text
0x100003F9A     0x0000001E      __TEXT  __cstring
0x100003FB8     0x00000048      __TEXT  __unwind_info
# Symbols:
# Address       Size            File  Name
0x100003F7D     0x0000001D      [  1] _main
0x100003F9A     0x0000000E      [  1] literal string: Hello world!\n
0x100003FA8     0x00000010      [  1] literal string: Hello, it's me\n
0x100003FB8     0x00000048      [  0] compact unwind info
```

Here is what the new lld's Mach-O mapfile looks like:
```
# Path: /Users/rgr/local/llvm-project/build/Debug/tools/lld/test/MachO/Output/map-file.s.tmp/c-string-liter
al-out
# Arch: x86_64
# Object files:
[  0] linker synthesized
[  1] /Users/rgr/local/llvm-project/build/Debug/tools/lld/test/MachO/Output/map-file.s.tmp/c-string-literal
.o
# Sections:
# Address       Size            Segment Section
0x1000002E0     0x0000001D      __TEXT  __text
0x1000002FD     0x0000001D      __TEXT  __cstring
# Symbols:
# Address           File  Name
0x1000002E0     [  1] _main
0x1000002FD     [  1] literal string: Hello world!\n
0x10000030B     [  1] literal string: Hello, it's me\n
```

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D118077
2022-02-11 19:42:20 -05:00
Nico Weber 73e585e44d Reland "[lld/coff] Make lld-link work in a non-MSVC shell, add /winsysroot:"
This relands commit b3b2538df1, except that the new files in Support
are instead in a new library WindowsDriver.
2022-02-11 17:07:33 -05:00
Adrian Prantl baac665adf Revert "[lld/coff] Make lld-link work in a non-MSVC shell, add /winsysroot:"
This reverts commit b3b2538df1,
it introduced a cycklic module depenency that broke the -DLLVM_ENABLE_MODULES=1 build.
2022-02-11 13:07:23 -08:00
Peter Kasting b3b2538df1 [lld/coff] Make lld-link work in a non-MSVC shell, add /winsysroot:
Makes lld-link work in a non-MSVC shell by autodetecting MSVC toolchain. Also
adds support for /winsysroot and a few other switches.

All this is done by refactoring to share code with clang-cl's existing support
for the same.

Differential Revision: https://reviews.llvm.org/D118070
2022-02-11 13:55:18 -05:00
Jez Ng 4490a26a3e [lld-macho][nfc] Rename %no_fatal_warnings_lld in tests
... to use hyphens instead of underscores, making it consistent with
our other substitutions like %no-arg-lld and %lld-watchos.

Reviewed By: keith

Differential Revision: https://reviews.llvm.org/D119513
2022-02-11 10:06:38 -05:00
Vincent Lee ef764ee207 [lld-macho][nfc] Centralize usages of ld64.lld in tests
We have a mix of substituted lld (`%lld`) and hard-coded lld (`ld64.lld`) commands.
When testing with different versions of LLD, this would require going into every place
where lld is hard-coded and changing that. If we centralize it, this'll only  require us
to modify it in only one place and will make it easy to run the same test suite. Plus,
this will make it be consistent with how we write other tests.

Reviewed By: #lld-macho, int3, oontvoo

Differential Revision: https://reviews.llvm.org/D119394
2022-02-10 17:27:07 -08:00
Krzysztof Drewniak 1ce314ce6b [MLIR][GPU][lld] Use LLD bundled in ROCm, removing workaround
Having clarified that executing the SerializeToHsaco pass can
depend on a ROCm installation, switch from calling lld as a library to
using the copy of lld guaranteed to be included in a ROCm install.

This removes the workaround introduced in D119277

Reviewed By: whchung

Differential Revision: https://reviews.llvm.org/D119463
2022-02-10 19:37:30 +00:00
Ben Dunbobbin 666aa43cbf Fix comment after upstream: 9e08e92980 - [ELF] Allow STV_PROTECTED shared definition to set exportDynamic? 2022-02-09 23:51:31 +00:00
Fangrui Song 4631cba10b [ELF][docs] Remove ignore -dc from ld.lld.1 2022-02-09 10:38:36 -08:00
Fangrui Song ce45c95694 [ELF] Remove obscure -dp and GNU ld incompatible --[no-]define-common, ignore -d/-dc
https://maskray.me/blog/2022-02-06-all-about-common-symbols#no-define-common

In GNU ld, -dc only affects -r links and causes COMMON symbols to be allocated.
--no-define-common is defined to make COMMON symbols undefined for -shared.
AIUI --no-define-common is a workaround around glibc 2.1 time and not really useful.

gold confuses --define-common with -d/FORCE_COMMON_ALLOCATION and implements
--define-common with -d semantics. Its --no-define-common is incompatible with
GNU ld.

In ld.lld, b2a23cf3c0 fixed the default -r
behavior for COMMON symbols but ported the incompatible gold
--[no-]define-common. To the best of my knowledge, no project uses -dp
--[no-]define-common. So just remove these options.

-d/-dc are used by the following projects:

* grub grub-core/genmod.sh.in uses -Wl,-r,-d (https://lists.gnu.org/archive/html/grub-devel/2022-02/msg00088.html)
* FreeBSD crunchgen uses -Wl,-dc (https://reviews.freebsd.org/D34215)

A no-op implementation works for them. Only when a program inspects relocatable
output by itself and does not recognize COMMON symbols, there may be a problem.
This is an extremely unlikely case.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D119108
2022-02-09 10:35:53 -08:00
Fangrui Song 99580e29d8 [ELF] --warn-backrefs: suppress warnings for backward references within the archive 2022-02-08 21:45:55 -08:00
Alexandre Ganea bb8be26a7e [LLD] Fix issue in HIP due to unspecified order of evaluation of the function object
This fixes the issue raised in https://reviews.llvm.org/D108850#3303452

Before C++17, the function object is evaluated in a unspecified order. In the following example: https://godbolt.org/z/8ao4vdsr7 the function object is either evaluated before or after the arguments, depending on the compiler. With MSVC and /std:c++14 the function object is evaluated after the arguments; with clang and gcc, it is evaluated before. With C++17, the function object is guaranteed to be evaluated before the arguments, see: https://riptutorial.com/cplusplus/example/19369/evaluation-order-of-function-arguments

In our case, the issue was that the `args` conversion to `ArrayRef` was evaluated before the lambda call `link`, which internally was calling `parseFlavor()`, which in turned modified `args`. We ended with an `ArrayRef` argument that reflected the previous contents of `args`.

Add coverage for `-flavor` which we didn't have before.

Differential Revision: https://reviews.llvm.org/D119278
2022-02-08 19:12:15 -05:00
Alexandre Ganea 1e661e583d [MLIR] Temporary workaround for calling the LLD ELF driver as-a-lib
This fixes the situation described in https://github.com/llvm/llvm-project/issues/53475 with a repro exposed by https://github.com/ROCmSoftwarePlatform/D108850-lld-bug-reproduction

This is purposely just a workaround to unblock users. This could be transplanted to the release/14.x branch if need be. A proper fix will later be provided in https://reviews.llvm.org/D119049.

Differential Revision: https://reviews.llvm.org/D119277
2022-02-08 19:12:15 -05:00
Fangrui Song f237ab0dd1 [ELF] AArch64ErrataFix: replace std::map with DenseMap. NFC
There is now no <map> in lld/ELF.
2022-02-07 22:02:25 -08:00
Fangrui Song 27bb799095 [ELF] Clean up headers. NFC 2022-02-07 21:53:34 -08:00
Jez Ng 06f863ac5e [lld-macho] Include address offsets in error messages
This makes it easier to pinpoint the source of the problem.

TODO: Have more relocation error messages make use of this
functionality.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D118798
2022-02-07 21:06:18 -05:00
Fangrui Song cb03ac0b5d [ELF] Move Symbol::needsTlsLd to config->needsTlsLd
to decrease sizeof(SymbolUnion) from 72 to 64 on ELF64 platforms.

Use a dummy `Undefined` to prevent null pointer dereference (though unused)
`*rel.sym` in InputSectionBase::relocateAlloc.

The relocation order may shuffle a bit, but otherwise there is no behavior
difference.
2022-02-07 10:26:16 -08:00
Alexander Kornienko ec8a693717 Revert "[ELF] Move Symbol::needsTlsLd to config->needsTlsLd. NFC"
This reverts commit f9e3ca542e.

The commit results in internal test failures. Test case provided offline.
2022-02-07 19:00:09 +01:00
Mariusz Ceier e8bff9ae54 Fix lld standalone build
lld/ELF/OutputSections.cpp includes llvm/Config/config.h for
LLVM_ENABLE_ZLIB definition, but llvm/Config/config.h doesn't exist in
standalone build.

To fix this, this patch moves LLVM_ENABLE_ZLIB from config.h to
llvm-config.h and updates OutputSections.cpp to include llvm-config.h
instead of config.h

Reviewed By: MaskRay, mgorny

Differential Revision: https://reviews.llvm.org/D119058
2022-02-07 09:20:03 -08:00
Jared Irwin 31626cc111 [lld-macho] Add -pagezero_size
Adds `-pagezero_size`. `-pagezero_size` commonly used for kernel development.
`-pagezero_size` changes the `__PAGEZERO` size, removing that segment if it is set to zero.

One of the four flags from {D118570}
Now with error messages and tests.

Differential Revision: https://reviews.llvm.org/D118724
2022-02-06 13:15:16 -05:00
Fangrui Song bad1b7fbb0 [ELF] Fix crash when an input is incompatible with a lazy object file
The diagnostic is concise. It is ok because the case is rare.
2022-02-05 23:34:14 -08:00
Fangrui Song 5ad2aae244 [ELF] SharedFile::parse: move verdefIndex assignment outside of ctor. NFC
SharedSymbol::SharedSymbol initializes verdefIndex and Symbol::replace
copies verdefIndex.

By move verdefIndex assignment outside of ctor, Symbol::replace can be changed
to not copy verdefIndex. This can be used to decrease work for for
ObjKind/BitcodeKind.
2022-02-05 20:43:51 -08:00
Fangrui Song 977a1a523c [ELF] Symbol::replace: use the old nameData/nameSize. NFC
Currently `this->getName() == newSym.getName()`.
By keeping the old nameData/nameSize, newSym's nameData/nameSize will be
ignored. The call sites can avoid calling getName().

printTraceSymbol needs to take the symbol name since `other`'s name is empty.
2022-02-05 16:34:02 -08:00
Fangrui Song 50460b8004 [ELF] Don't access other eSym members it st_shndx == SHN_UNDEF. NFC 2022-02-05 15:25:23 -08:00
Fangrui Song 9af90e205a [ELF] De-template reportUndefinedSymbols. NFC
My x86-64 lld executable is 16KiB smaller.
2022-02-05 15:03:56 -08:00
Fangrui Song f9e3ca542e [ELF] Move Symbol::needsTlsLd to config->needsTlsLd. NFC
to decrease sizeof(SymbolUnion) from 72 to 64 on ELF64 platforms.
2022-02-05 14:40:15 -08:00
Fangrui Song 73f55fba76 [ELF] Reorder Symbol members to improve access locality. NFC
* partition and isPreemptible are frequently used. Move it to the front
* move used beside isUsedInRegularObj. They are similar and accessed together in .symtab finalizing
* move auxIdx/dynsymIndex/verdefIndex to the end.

This decreases code size.
2022-02-05 14:11:37 -08:00
Fangrui Song 7c675923c7 [ELF] Merge canInline into scriptDefined
They perform similar tasks and are essentially the same after
d28c26bbdd.
2022-02-05 12:00:34 -08:00
Fangrui Song 764cd491b1 [ELF] Simplify shouldKeepInSymtab after Symbol::used is false by default. NFC 2022-02-05 11:21:44 -08:00
Fangrui Song 38e6361d84 [ELF] Simplify includeInSymtab. NFC 2022-02-05 11:18:08 -08:00
Fangrui Song bb4eacdb70 [ELF] Refactor how Symbol::used is set. NFC 2022-02-05 11:09:40 -08:00
Fangrui Song ac2911e738 [ELF] Refactor how exportDynamic is set. NFC 2022-02-05 10:25:25 -08:00
Fangrui Song 7288b85cc8 [ELF] --wrap: don't copy exportDynamic
For -no-pie/-pie, when `__real_foo` is interposable in a shared object, `foo` is
exported. This rule does not match GNU ld and is unneeded because:

* the exported `foo` does not interpose `__real_foo` at run-time
* the similar `__wrap_foo` <-> `foo` relation does not have the rule
2022-02-05 09:56:29 -08:00
Fangrui Song 9e08e92980 [ELF] Allow STV_PROTECTED shared definition to set exportDynamic
A STV_PROTECTED shared definition does not set exportDynamic of a defined
symbol. This is on the basis that a protected definition cannot be preempted so
the export is unnecessary. However, the condition is imperfect because we don't
know whether the shared object was built with a symbolic option. Since dropping
the condition simplifies code and matches GNU ld, let's do it.
2022-02-05 01:10:43 -08:00
Shoaib Meenai 997f2a56de [ELF] Avoid wrapping unreferenced lazy symbols
There's a couple of motivations here:
* LLD 12 (which I was originally testing with) was adding an undefined
  symbol to the symbol table if you attempted to wrap an unreferenced
  lazy symbol, which would later break `--no-allow-shlib-undefined`. LLD
  on main actually produces a weak undefined symbol, so this doesn't
  break anyway, but it's cleaner to not have the weak undefined symbol
  as well. The new behavior also matches bfd and gold.
* PROVIDE in a linker script referencing a wrapped symbol would think
  that an otherwise-unreferenced lazy symbol which was wrapped was
  actually referenced, and therefore proceed with the definition, which
  goes against expectations. The new behavior also matches bfd and gold.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D118756
2022-02-04 18:09:37 -08:00
Fangrui Song 53fc5d9b9a [ELF] Support R_PPC_NONE/R_PPC64_NONE in getImplicitAddend
Similar to f457863ae3
2022-02-04 15:13:37 -08:00
Fangrui Song 3fd30958b7 [ELF][test] Test getImplicitAddend for R_ARM_NONE/R_RISCV_NONE/R_386_NONE/R_X86_64_NONE
Similar to f457863ae3
2022-02-04 13:59:18 -08:00
Byoungchan Lee da08d50fd6 [lld][macho] Add more skip platform check for libSystem re-exports
Xcode 13 comes with a mismatched platform in libcompiler_rt.dylib,
so this creates a linker error on mac catalyst.
Fix it by adding it to the skip list.

Reviewed By: MaskRay, #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D117925
2022-02-04 16:34:56 -05:00
Fangrui Song f457863ae3 [ELF] Support REL-format R_AARCH64_NONE relocation
-fprofile-use=/-fprofile-sample-use= compiles may produce REL-format
.rel.llvm.call-graph-profile even if the prevailing format is RELA on AArch64.
Add R_AARCH64_NONE to getImplicitAddend to fix this linker error:

```
ld.lld: error: internal linker error: cannot read addend for relocation R_AARCH64_NONE
PLEASE submit a bug report to https://crbug.com and run tools/clang/scripts/process_crashreports.py (only works inside Google) which will upload a report and include the crash backtrace.
```
2022-02-04 13:20:49 -08:00
Jez Ng 2b78ef06c2 [lld-macho][nfc] Eliminate InputSection::Shared
Earlier in LLD's evolution, I tried to create the illusion that
subsections were indistinguishable from "top-level" sections. Thus, even
though the subsections shared many common field values, I hid those
common values away in a private Shared struct (see D105305). More
recently, however, @gkm added a public `Section` struct in D113241 that
served as an explicit way to store values that are common to an entire
set of subsections (aka InputSections). Now that we have another "common
value" struct, `Shared` has been rendered redundant. All its fields can
be moved into `Section` instead, and the pointer to `Shared` can be replaced
with a pointer to `Section`.

This `Section` pointer also has the advantage of letting us inspect other
subsections easily, simplifying the implementation of {D118798}.

P.S. I do think that having both `Section` and `InputSection` makes for
a slightly confusing naming scheme. I considered renaming `InputSection`
to `Subsection`, but that would break the symmetry with `OutputSection`.
It would also make us deviate from LLD-ELF's naming scheme.

This change is perf-neutral on my 3.2 GHz 16-Core Intel Xeon W machine:

             base           diff           difference (95% CI)
  sys_time   1.258 ± 0.031  1.248 ± 0.023  [  -1.6% ..   +0.1%]
  user_time  3.659 ± 0.047  3.658 ± 0.041  [  -0.5% ..   +0.4%]
  wall_time  4.640 ± 0.085  4.625 ± 0.063  [  -1.0% ..   +0.3%]
  samples    49             61

There's also no stat sig change in RSS (as measured by `time -l`):

           base                         diff                           difference (95% CI)
  time     998038627.097 ± 13567305.958 1003327715.556 ± 15210451.236  [  -0.2% ..   +1.2%]
  samples  31                           36

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D118797
2022-02-03 19:55:42 -05:00
Vy Nguyen 36f32ffd2a [lld-macho] Minor clean up: use .find() to check for key existence rather than [], which would create a new entry.
Differential Revision: https://reviews.llvm.org/D118945
2022-02-03 17:17:14 -05:00
John Ericson e0eeae9a44 [lld][clang][cmake] Clean up a few things
- If not using `llvm-config`, `LLVM_MAIN_SRC_DIR` now has a sane default

- `LLVM_CONFIG_PATH` will continue to work for LLD for back compat.

- More quoting of paths in an abundance of caution.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D118792
2022-02-03 20:01:28 +00:00
Keith Smiley 187ce07a06 [lld-macho] Fix duplicate symbols with relocatable objects
In the case your framework bundles contain relocatable objects, and your
objects include LC_LINKER_OPTIONs for the framework, previously they
would not be deduplicated like they would have if they were static
archives. This was also the case if you passed `-framework` for the
framework as well.

Reviewed By: #lld-macho, thakis, oontvoo

Differential Revision: https://reviews.llvm.org/D114841
2022-02-02 14:54:10 -08:00
Jez Ng 9408b75ec3 [lld-macho][nfc] Hoist out creation of Section in parseSections()
Simplifies the code slightly.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D118796
2022-02-02 17:09:14 -05:00
Tom Stellard a2601c9887 Bump the trunk major version to 15 2022-02-01 23:54:52 -08:00
Tom Stellard e80c52986e [docs] Remove hard-coded version numbers from sphinx configs
This updates all the non-runtime project release notes to use the
version number from CMake instead of the hard-coded version numbers
in conf.py.

It also hides warnings about pre-releases when the git suffix
is dropped from the LLVM version in CMake.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D112181
2022-02-01 23:14:12 -08:00
Alexander Shaposhnikov 4450a2a23d [lld][ELF] Add support for ADRP+ADD optimization for AArch64
This diff adds support for ADRP+ADD optimization for AArch64 described in
d2ca58c54b
i.e. under appropriate constraints

ADRP  x0, symbol
ADD   x0, x0, :lo12: symbol

can be turned into

NOP
ADR   x0, symbol

Test plan: make check-all

Differential revision: https://reviews.llvm.org/D117614
2022-02-02 06:09:55 +00:00
Jez Ng 3e951808d5 [lld-macho][nfc] Comments and style fixes
Added some comments (particularly around finalize() and
finalizeContents()) as well as doing some rephrasing / grammar fixes for
existing comments.

Also did some minor style fixups, such as by putting methods together in
a class definition and having fields of similar types next to each
other.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D118714
2022-02-01 13:45:59 -05:00
Fangrui Song fbf2f66400 [ELF] Update flag propagation rule to ignore discarded output sections
See the updated insert-before.test for the effects: many synthetic
sections are SHF_ALLOC|SHF_WRITE. If they are discarded, we don't want
to propagate their flags to subsequent output section descriptions.

`getFirstInputSection(sec) == nullptr` can technically be merged into
`isDiscardable` but I'd like to postpone that as not sharing code may give more
refactoring opportunity.

Depends on D118529.

Reviewed By: peter.smith, bluca

Differential Revision: https://reviews.llvm.org/D118530
2022-02-01 10:19:30 -08:00
Fangrui Song a0318711c8 [ELF] Rename adjustSectionsBeforeSorting to adjustOutputSections and make it affect INSERT commands
adjustSectionsBeforeSorting updates some output section attributes
(alignment/flags) and removes discardable empty sections. When it is called,
INSERT commands have not been processed. Therefore the flags propagation rule
may not affect output sections defined in an INSERT command properly.

Fix this by moving processInsertCommands before adjustSectionsBeforeSorting.

adjustSectionsBeforeSorting is somewhat misnamed. The order between it and
sortInputSections does not matter. With the pass shuffle, the name of
adjustSectionsBeforeSorting becomes wrong. Therefore rename it. The new
name is not set into stone. The function mixes several tasks and the
code may be refactored in a way that we may give them more meaningful
names.

With this patch, I think the behavior of attribute propagation becomes more
reasonable. In particular, in the absence of non-INSERT SECTIONS,
inserting a section after a SHF_ALLOC one will give us a SHF_ALLOC section,
not a non-SHF_ALLOC one (see linkerscript/insert-after.test).

Reviewed By: peter.smith, bluca

Differential Revision: https://reviews.llvm.org/D118529
2022-02-01 10:16:12 -08:00
Fangrui Song 0c3704fdbd [ELF] Deduplicate names of local symbols only with -O2
The deduplication requires a DenseMap of the same size of the local part of
.strtab . I optimized it in e205445434 but it is
still quite slow.

For Release build of clang, deduplication makes .strtab 1.1% smaller and makes the link 3% slower.
For chrome, deduplication makes .strtab 0.1% smaller and makes the link 6% slower.

I suggest that we only perform the optimization with -O2 (default is -O1).
Not deduplicating local symbol names will simplify parallel symbol table write.

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D118577
2022-02-01 10:10:22 -08:00
Fangrui Song 17a39aecd1 [ELF] Simplify code with invokeELFT. NFC 2022-02-01 09:53:29 -08:00
Fangrui Song 7518d38f0a [ELF] De-template LinkerDriver::link. NFC
Replace `f<ELFT>(x)` with `InvokeELFT(f, x)`.
The size reduction comes from turning `link` from 4 specializations into 1.

My x86-64 lld executable is 26KiB smaller.

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D118551
2022-02-01 09:47:56 -08:00
Alexander Shaposhnikov 7244901ef6 [lld][MachO] Fix typo in rename.s 2022-02-01 11:57:04 +00:00
Alexander Shaposhnikov f131d4d0d0 [lld][ELF] Add missing RUN in aarch64-adrp-ldr-got.s 2022-02-01 11:25:16 +00:00
Fangrui Song 4d38d7684c [ELF] Change vector<Symbol *> to SmallVector. NFC 2022-02-01 00:16:42 -08:00
Fangrui Song 196aedb843 [ELF] Change vector<InputSection *> to SmallVector. NFC
My x86-64 lld executable is 8KiB smaller.
2022-02-01 00:14:21 -08:00
Fangrui Song d97749fabc [ELF] Switch split-stack to use SmallVector. NFC
My x86-64 lld executable is 1.1KiB smaller.
2022-02-01 00:09:30 -08:00
Jez Ng 96fb7d059d [lld-macho][test] Add test for UUID format
Reviewed By: keith

Differential Revision: https://reviews.llvm.org/D118646
2022-01-31 23:52:42 -05:00
Fangrui Song 7aaf024dac [BitcodeWriter] Fix cases of some functions
`WriteIndexToFile` is used by external projects so I do not touch it.
2022-01-31 16:46:11 -08:00
Fangrui Song 457273fda5 [ELF] splitStrings: replace entSize==1 special case with manual loop unswitch. NFC
My x86-64 lld executable is actually smaller.
2022-01-30 17:15:45 -08:00
Fangrui Song 7cd0c45364 [ELF] Simplify SectionBase::partition handling and make it live by default. NFC
Previously an InputSectionBase is dead (`partition==0`) by default.
SyntheticSection calls markLive and BssSection overrides that with markDead.

It is more natural to make InputSectionBase live by default and let
--gc-sections mark InputSectionBase dead.

When linking a Release build of clang:

* --no-gc-sections:, the removed `inputSections` loop decreases markLive time from 4ms to 1ms.
* --gc-sections: the extra `inputSections` loop increases markLive time from 0.181296s to 0.188526s.
  This is as of we lose the removing one `inputSections` loop optimization (4374824ccf).
  I believe the loss can be mitigated if we refactor markLive.
2022-01-30 15:12:09 -08:00
Fangrui Song 73fd7d2304 [ELF] Change splitSections to objectFiles based parallelForEach. NFC
The work is more balanced.
2022-01-30 13:34:27 -08:00
Keith Smiley a6298fb160 [lld-macho] Add support for -add_empty_section
This is a ld64 option equivalent to `-sectcreate seg sect /dev/null`
that's useful for creating sections like the RESTRICT section.

Differential Revision: https://reviews.llvm.org/D117749
2022-01-30 10:03:41 -08:00
Keith Smiley 0ab09a9009 [test][lld-macho] Improve LC_FUNCTION_STARTS test coverage
Previously functions that aren't included in the symtab were also
excluded from the function starts. Symbols missing from function starts
degrades the debugger experience in the case you don't have debug info
for them.

Differential Revision: https://reviews.llvm.org/D114275
2022-01-30 09:46:36 -08:00
Fangrui Song 5a2020d069 [ELF] copyShtGroup: replace unordered_set<uint32_t> with DenseSet<uint32_t>. NFC
We don't need to support the empty/tombstone key section index.
2022-01-30 01:18:41 -08:00
Fangrui Song f318fd9bf8 [ELF] crtbegin/crtend test: replace std::regex with hand-written matcher. NFC
My x86-64 lld executable is 18KiB smaller.
2022-01-30 01:11:19 -08:00
Fangrui Song a7f9c002cd [ELF][test] Test {crtbegin,crtend}{S,T}.o 2022-01-30 01:08:10 -08:00
Fangrui Song fcd8817da5 [ELF] Simplify maybeCompress with lld::split. NFC 2022-01-30 00:44:19 -08:00
Fangrui Song bc1369fae3 [ELF] Optimize MergeInputSection::splitNonStrings with resize_for_overwrite. NFC 2022-01-30 00:10:52 -08:00
Fangrui Song 988a03c585 [ELF] Add some Mips*Section to InStruct and change make<Mips*Section> to std::make_unique
Similar to D116143. My x86-64 lld executable is 20+KiB smaller.
2022-01-29 23:55:29 -08:00
Fangrui Song c0b986aa0c [ELF] Remove make<std::unique_ptr<MemoryBuffer>>. NFC 2022-01-29 23:35:15 -08:00
Fangrui Song 8d8fce87bb [ELF] De-template getErrorPlace. NFC 2022-01-29 23:05:54 -08:00