Commit Graph

13732 Commits

Author SHA1 Message Date
Fangrui Song a5f95887d0 [ELF][test] Add some tests for versioned symbols in object files
Test the symbol resolution related to

* defined foo@@v1 and foo@v1 in object files/shared objects
* undefined foo@v1
* weak foo@@v1 and foo@v1
* visibility
* interaction with --wrap.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D92258
2020-12-01 08:49:14 -08:00
Nico Weber 4431c212a0 lld/ELF: Make three rarely-used flags work with --reproduce
All three use readFile() for their argument so their argument file is
already copied to the tar, but we weren't rewriting the argument to
point to the path used in the tar file.

No test because the change is trivial (several other flags in
createResponseFile() also aren't tested, likely for the same reason.)

Differential Revision: https://reviews.llvm.org/D92356
2020-12-01 09:20:29 -05:00
Wei Wang 3acda91742 [Remarks][1/2] Expand remarks hotness threshold option support in more tools
This is the #1 of 2 changes that make remarks hotness threshold option
available in more tools. The changes also allow the threshold to sync with
hotness threshold from profile summary with special value 'auto'.

This change modifies the interface of lto::setupLLVMOptimizationRemarks() to
accept remarks hotness threshold. Update all the tools that use it with remarks
hotness threshold options:

* lld: '--opt-remarks-hotness-threshold='
* llvm-lto2: '--pass-remarks-hotness-threshold='
* llvm-lto: '--lto-pass-remarks-hotness-threshold='
* gold plugin: '-plugin-opt=opt-remarks-hotness-threshold='

Differential Revision: https://reviews.llvm.org/D85809
2020-11-30 21:55:49 -08:00
Amy Huang efd1ec0dec Recommit "[llvm-symbolizer] Switch to using native symbolizer by default on Windows"
This reverts commit 1b63177a56.
2020-11-30 17:36:12 -08:00
Amy Huang 8cdf4920c4 [llvm-symbolizer] Fix typo in llvm-symbolizer test from a previous commit.
(Commit was 00bbef2bb2)
2020-11-30 15:08:11 -08:00
Amy Huang 00bbef2bb2 [llvm-symbolizer] Fix native symbolization on windows for inline sites.
The existing code handles this correctly and I checked that the code
in NativeInlineSiteSymbol also handles this correctly, but it was
wrong in the NativeFunctionSymbol code.

Differential Revision: https://reviews.llvm.org/D92134
2020-11-30 14:27:35 -08:00
Nico Weber 78c04fe99e [lld/mac] Don't warn on -bundle and -execute flags
They've been implemented since D87856 but since they still were
HelpHidden, the driver still warned claiming they were implemented.
Remove HelpHidden.

Use -fatal_warnings to test that the flags now don't warn. The
test depends on D91894 and D91891 to pass.

Differential Revision: https://reviews.llvm.org/D91971
2020-11-30 16:07:58 -05:00
Nico Weber ebac710009 [lld-macho] Don't warn on non-existent system libraries
Now, new mach-o lld no longer warns if the isysroot has just
usr/lib and System/Library/Frameworks but is missing usr/local/lib
and System/Frameworks.

This matches ld64 and old mach-o lld and fixes a regression from D85992.

It also fixes the only test failure in `check-lld` when running it
on an M1 Mac.

Differential Revision: https://reviews.llvm.org/D91891
2020-11-30 16:07:20 -05:00
Fangrui Song 589e10f858 [ELF] Don't relax R_X86_64_GOTPCRELX if addend != -4
clang may produce `movl x@GOTPCREL+4(%rip), %eax` when loading the high 32 bits
of the address of a global variable in -fpic/-fpie mode.

If assembled by GNU as, the fixup emits an R_X86_64_GOTPCRELX with an
addend != -4. The instruction loads from the GOT entry with an offset
and thus it is incorrect to relax the instruction.

If assembled by the integrated assembler, we emit R_X86_64_GOTPCREL for
relocations that definitely cannot be relaxed (D92114), so this patch is not
needed.

This patch disables the relaxation, which is compatible with the implementation in GNU ld
("Add R_X86_64_[REX_]GOTPCRELX support to gas and ld").

Reviewed By: grimar, jhenderson

Differential Revision: https://reviews.llvm.org/D91993
2020-11-30 08:30:19 -08:00
Nico Weber c0e4020c92 [lld-macho] Implement -fatal_warnings
Differential Revision: https://reviews.llvm.org/D91894
2020-11-30 09:29:21 -05:00
Nico Weber 83e60f5a55 [lld/mac] Add --reproduce option
This adds support for ld.lld's --reproduce / lld-link's /reproduce:
flag to the MachO port. This flag can be added to a link command
to make the link write a tar file containing all inputs to the link
and a response file containing the link command. This can be used
to reproduce the link on another machine, which is useful for sharing
bug report inputs or performance test loads.

Since the linker is usually called through the clang driver and
adding linker flags can be a bit cumbersome, setting the env var
`LLD_REPRODUCE=foo.tar` triggers the feature as well.

The file response.txt in the archive can be used with
`ld64.lld.darwinnew $(cat response.txt)` as long as the contents are
smaller than the command-line limit, or with `ld64.lld.darwinnew
@response.txt` once D92149 is in.

The support in this patch is sufficient to create a tar file for
Chromium's base_unittests that can link after unpacking on a different
machine.

Differential Revision: https://reviews.llvm.org/D92274
2020-11-30 08:40:21 -05:00
Nico Weber d20abb1ec3 [mac/lld] Add support for response files
ld64 learned about them in Xcode 12, so we should too.

Differential Revision: https://reviews.llvm.org/D92149
2020-11-30 08:23:58 -05:00
Fangrui Song dfcf1acf13 [ELF] Improve 2 SmallVector<*, N> usage
For --gc-sections, SmallVector<InputSection *, 256> -> SmallVector<InputSection *, 0> because the code bloat (1296 bytes) is not worthwhile (the saved reallocation is negligible).
For OutputSection::compressedData, N=1 is useless (for a compressed .debug_*, the size is always larger than 1).
2020-11-29 14:01:32 -08:00
Fangrui Song 048b16f7fb [ELF] Check --orphan-handling=place (default value) early
The function took 1% (161MiB clang) to 1.7% (an 4.9GiB executable) time.
2020-11-29 12:36:27 -08:00
Nico Weber a0994cbe27 lld-link: Let LLD_REPRODUCE control /reproduce:, like in ld.lld
Also sync help texts for the option between elf and coff ports.

Decisions:
- Do this even if /lldignoreenv is passed. /reproduce: does not affect
  the main output, and this makes the env var more convenient to use.
  (On the other hand, it's now possible to set this env var and forget
  about it, and all future builds in the same shell will be much slower.
  That's true for ld.lld, but posix shells have an easy way to set an
  env var for a single command; in cmd.exe this is not possible without
  contortions. Then again, lld-link runs in posix shells too.)

Original patch rebased across D68378 and D68381.

Differential Revision: https://reviews.llvm.org/D67707
2020-11-27 13:33:55 -05:00
Sam Clegg 48ddf5e182 [lld][WebAssembly] Ensure stub symbols always get address 0
Without this extra flag we can't distingish between stub functions and
functions that happen to have address 0 (relative to __table_base).

Adding this flag bit the base symbol class actually avoids growing the
SymbolUnion struct which would not be true if we added it to the
FunctionSymbol subclass (due to bitbacking).

The previous approach of setting it's table index to zero worked for
normal static relocations but not for `-fPIC` code.

See https://github.com/emscripten-core/emscripten/issues/12819

Differential Revision: https://reviews.llvm.org/D92038
2020-11-25 18:26:34 -08:00
Nico Weber da0aaedcd0 [gn build] (manually) port b534beabee 2020-11-25 20:19:46 -05:00
Amy Huang 1363dfaf31 [CodeView] Avoid emitting empty debug globals subsection.
In https://reviews.llvm.org/D89072 I added static const data members
to the debug subsection for globals. It skipped emitting an S_CONSTANT if it
didn't have a value, which meant the subsection could be empty.

This patch fixes the empty subsection issue.

Differential Revision: https://reviews.llvm.org/D92049
2020-11-25 16:13:32 -08:00
Paul Robinson cf1c774d6a [FastISel] Flush local value map on ever instruction
Local values are constants or addresses that can't be folded into
the instruction that uses them. FastISel materializes these in a
"local value" area that always dominates the current insertion
point, to try to avoid materializing these values more than once
(per block).

https://reviews.llvm.org/D43093 added code to sink these local
value instructions to their first use, which has two beneficial
effects. One, it is likely to avoid some unnecessary spills and
reloads; two, it allows us to attach the debug location of the
user to the local value instruction. The latter effect can
improve the debugging experience for debuggers with a "set next
statement" feature, such as the Visual Studio debugger and PS4
debugger, because instructions to set up constants for a given
statement will be associated with the appropriate source line.

There are also some constants (primarily addresses) that could be
produced by no-op casts or GEP instructions; the main difference
from "local value" instructions is that these are values from
separate IR instructions, and therefore could have multiple users
across multiple basic blocks. D43093 avoided sinking these, even
though they were emitted to the same "local value" area as the
other instructions. The patch comment for D43093 states:

  Local values may also be used by no-op casts, which adds the
  register to the RegFixups table. Without reversing the RegFixups
  map direction, we don't have enough information to sink these
  instructions.

This patch undoes most of D43093, and instead flushes the local
value map after(*) every IR instruction, using that instruction's
debug location. This avoids sometimes incorrect locations used
previously, and emits instructions in a more natural order.

This does mean materialized values are not re-used across IR
instruction boundaries; however, only about 5% of those values
were reused in an experimental self-build of clang.

(*) Actually, just prior to the next instruction. It seems like
it would be cleaner the other way, but I was having trouble
getting that to work.

Differential Revision: https://reviews.llvm.org/D91734
2020-11-25 13:05:00 -05:00
Fangrui Song 50564ca075 [ELF] Rename adjustRelaxExpr to adjustTlsExpr and delete the unused `data` parameter. NFC
Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D91995
2020-11-25 09:00:55 -08:00
Fangrui Song 572d18397c [ELF] Add TargetInfo::adjustGotPcExpr for `R_GOT_PC` relaxations. NFC
With this change, `TargetInfo::adjustRelaxExpr` is only related to TLS
relaxations and a subsequent clean-up can delete the `data` parameter.

Differential Revision: https://reviews.llvm.org/D92079
2020-11-25 08:43:26 -08:00
Andy Wingo 1933c9d41a [WebAssembly] Factor out WasmTableType in binary format
This commit factors out a WasmTableType definition from WasmTable, as is
the case for WasmGlobal and other data types.  Also add support for
extracting the SymbolName for a table from the linking section's symbol
table.

Differential Revision: https://reviews.llvm.org/D91849
2020-11-25 08:00:08 -08:00
Teresa Johnson 07f234be1c [lld] Add --no-lto-whole-program-visibility
Enables overriding earlier --lto-whole-program-visibility.

Variant of D91583 while discussing alternate ways to identify and
handle the --export-dynamic case.

Differential Revision: https://reviews.llvm.org/D92060
2020-11-24 16:46:08 -08:00
Nico Weber 11b7625833 [lld/mac] Implement basic typo correction for flags
Also use "unknown flag 'flag'" instead of "unknown flag: flag" for
consistency with the other ports.

Differential Revision: https://reviews.llvm.org/D91970
2020-11-24 11:33:39 -05:00
Nico Weber c8414fa941 lld: Fix darwinnew symlink name added in e16c0a9a68 2020-11-24 11:06:51 -05:00
Nico Weber e16c0a9a68 clang+lld: Improve clang+ld.darwinnew.lld interaction, pass -demangle
This patch:
- adds an ld64.lld.darwinnew symlink for lld, to go with f2710d4b57,
  so that `clang -fuse-ld=lld.darwinnew` can be used to test new
  Mach-O lld while it's in bring-up. (The expectation is that we'll
  remove this again once new Mach-O lld is the defauld and only Mach-O
  lld.)
- lets the clang driver know if the linker is lld (currently
  only triggered if `-fuse-ld=lld` or `-fuse-ld=lld.darwinnew` is
  passed). Currently only used for the next point, but could be used
  to implement other features that need close coordination between
  compiler and linker, e.g. having a diag for calling `clang++` instead
  of `clang` when link errors are caused by a missing C++ stdlib.
- lets the clang driver pass `-demangle` to Mach-O lld (both old and
  new), in addition to ld64
- implements -demangle for new Mach-O lld
- changes demangleItanium() to accept _Z, __Z, ___Z, ____Z prefixes
  (and updates one test added in D68014). Mach-O has an extra
  underscore for symbols, and the three (or, on Mach-O, four)
  underscores are used for block names.

Differential Revision: https://reviews.llvm.org/D91884
2020-11-24 08:51:58 -05:00
Martin Storsjö 0b2d84fba8 [LLD] [COFF] Allow wrapping dllimported functions
GNU ld doesn't seem to do this though, but it looks like a reasonable
use case, is easy to implement, and was requested in
https://bugs.llvm.org/show_bug.cgi?id=47384.

Differential Revision: https://reviews.llvm.org/D91689
2020-11-24 10:15:20 +02:00
Amy Huang 1b63177a56 Revert "[llvm-symbolizer] Switch to using native symbolizer by default on Windows"
Breaks some asan tests on the buildbot.

This reverts commit c74b427cb2.
2020-11-23 16:29:45 -08:00
Amy Huang c74b427cb2 [llvm-symbolizer] Switch to using native symbolizer by default on Windows
llvm-symbolizer used to use the DIA SDK for symbolization on
Windows; this patch switches to using native symbolization, which was
implemented recently.

Users can still make the symbolizer use DIA by adding the `-dia` flag
in the LLVM_SYMBOLIZER_OPTS environment variable.

Differential Revision: https://reviews.llvm.org/D91814
2020-11-23 15:57:08 -08:00
Georgii Rymar 9a99d23a1b [lib/Object] - Generalize the RelocationResolver API.
This allows to reuse the RelocationResolver from the code
that doesn't want to deal with `RelocationRef` class.

I am going to use it in llvm-readobj. See the description
of D91530 for more details.

Differential revision: https://reviews.llvm.org/D91533
2020-11-20 10:32:49 +03:00
Sam Clegg f7f0fe6184 [lld][WebAssembly] Convert more tests to asm format. NFC.
Differential Revision: https://reviews.llvm.org/D91681
2020-11-19 16:57:00 -08:00
Gabriel Hjort Åkerlund 2d1f471e45 [Mach0] Fix unused-variable warnings
Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D91519
2020-11-19 10:51:15 +01:00
Sam Clegg 1827005cfc [WebAssembly] Add support for named globals in the object format.
Differential Revision: https://reviews.llvm.org/D91769
2020-11-19 00:17:22 -08:00
Nico Weber c519bc7e16 lld/MachO: Move MachOOptTable to DriverUtils.cpp, remove DriverUtils.h
This makes lld/MachO look more like lld/COFF and lld/ELF, as discussed
in D91640.
2020-11-18 12:33:15 -05:00
Nico Weber 27e73816d6 lld: Make tests depend on llvm-symbolizer after bc98034040
Fixes test failures when building just `check-lld` in a clean build dir.
2020-11-18 11:43:44 -05:00
Georgii Rymar 9aa7898200 Reland "[lib/Support/YAMLTraits] - Don't print leading zeroes when dumping Hex8/Hex16/Hex32 types." (https://reviews.llvm.org/D90930).
This reverts reverting commit fc40a03323
and fixes LLD (MachO/wasm) tests that failed previously.
2020-11-18 13:08:46 +03:00
Andrew Paverd 0139c8af8d [CFGuard] Add address-taken IAT tables and delay-load support
This patch adds support for creating Guard Address-Taken IAT Entry Tables (.giats$y sections) in object files, matching the behavior of MSVC. These contain lists of address-taken imported functions, which are used by the linker to create the final GIATS table.
Additionally, if any DLLs are delay-loaded, the linker must look through the .giats tables and add the respective load thunks of address-taken imports to the GFIDS table, as these are also valid call targets.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D87544
2020-11-17 18:24:45 -08:00
Sam Clegg 206884bf90 [lld][WebAssembly] Implement --unresolved-symbols
This is a more full featured version of ``--allow-undefined``.
The semantics of the different methods are as follows:

report-all:

   Report all unresolved symbols.  This is the default.  Normally the
   linker will generate an error message for each reported unresolved
   symbol but the option ``--warn-unresolved-symbols`` can change this
   to a warning.

ignore-all:

   Resolve all undefined symbols to zero.  For data and function
   addresses this is trivial.  For direct function calls, the linker
   will generate a trapping stub function in place of the undefined
   function.

import-functions:

   Generate WebAssembly imports for any undefined functions.  Undefined
   data symbols are resolved to zero as in `ignore-all`.  This
   corresponds to the legacy ``--allow-undefined`` flag.

The plan is to followup with a new mode called `import-dynamic` which
allows for statically linked binaries to refer to both data and
functions symbols from the embedder.

Differential Revision: https://reviews.llvm.org/D79248
2020-11-17 16:27:06 -08:00
Amy Huang bc98034040 [llvm-symbolizer] Add inline stack traces for Windows.
This adds inline stack frames for symbolizing on Windows.

Differential Revision: https://reviews.llvm.org/D88988
2020-11-17 13:19:13 -08:00
Fangrui Song 55d310adc0 [ELF] Fix interaction between --unresolved-symbols= and --[no-]allow-shlib-undefined
As mentioned in https://reviews.llvm.org/D67479#1667256 ,

* `--[no-]allow-shlib-undefined` control the diagnostic for an unresolved symbol in a shared object
* `-z defs/-z undefs` control the diagnostic for an unresolved symbol in a regular object file
* `--unresolved-symbols=` controls both bits.

In addition, make --warn-unresolved-symbols affect --no-allow-shlib-undefined.

This patch makes the behavior match GNU ld.

Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D91510
2020-11-17 12:20:57 -08:00
Nico Weber baa2aa28f5 lld: Add --color-diagnostic to MachO port, harmonize others
This adds `--[no-]color-diagnostics[=auto,never,always]` to
the MachO port and harmonizes the flag in the other ports:
- Consistently use MetaVarName
- Consistently document the non-eq version as alias of the eq version
- Use B<> in the ports that have it (no-op, shorter)
- Fix oversight in COFF port that made the --no flag have the wrong
  prefix

Differential Revision: https://reviews.llvm.org/D91640
2020-11-17 12:58:30 -05:00
Fangrui Song 3f90918886 [ELF] --gc-sections: collect unused .gcc_except_table in section groups and associated text sections
`try ... catch` in an inline function produces `.gcc_except_table.*` in a COMDAT
group with GCC or newer Clang (since D83655). For --gc-sections, currently we
scan `.eh_frame` pieces and mark liveness of such a `.gcc_except_table.*` and
then the associated `.text.*` (if a member in a section group is retained, the
others should be retained as well).

Essentially all `.text.*` and `.gcc_except_table.*` compiled from inline
functions with `try ... catch` cannot be discarded by the imprecise
--gc-sections.  Compared with the state before D83655, the output
`.gcc_except_table` is smaller (non-prevailing copies in COMDAT groups can now
be discarded) but `.text` may be larger, i.e. size regression.

This patch teaches the .eh_frame piece scanning code to not mark
`.gcc_except_table` in a section group, thus allow unused `.text.*` and
`.gcc_except_table.*` in a section group to be discarded.

Note, non-group `.gcc_except_table` can still not be discarded. That is the status quo.

Reviewed By: grimar, echristo

Differential Revision: https://reviews.llvm.org/D91579
2020-11-17 09:11:20 -08:00
Nico Weber f2710d4b57 lld/mach-o: Infer darwinnew from filename ld64.lld.darwinnew too
`-flavor` is difficult to use through the clang driver since it
must be the first argument.

clang's `-fuse-ld=foo` looks for `ld64.foo` when targeting darwin,
so it's easiest if darwinnew accepts some `ld64.foo`. Let's go with
`ld64.lld.darwinnew`, so that `clang -fuse-ld=lld.darwinnew` does
the right thing (assuming a symlink with the name `ld64.ld.darwinnew
exists in the right place).

This is temporary until darwinnew replaces ld64.lld, and it only
exists to make testing the new lld port easier.
2020-11-16 15:23:03 -05:00
Mikhail Goncharov 47c17bcd0e [lld] Use %t file in test
Otherwise it fails in some setups when creation of "out.wasm" is not possible.

Differential Revision: https://reviews.llvm.org/D91521
2020-11-16 10:49:38 +01:00
Wouter van Oortmerssen 16f02431dc [WebAssembly] Added R_WASM_FUNCTION_OFFSET_I64 for use with DWARF DW_AT_low_pc
Needed for wasm64, see discussion in https://reviews.llvm.org/D91203

Differential Revision: https://reviews.llvm.org/D91395
2020-11-13 09:32:31 -08:00
Sam Clegg a28a466210 [WebAssembly] Add new relocation type for TLS data symbols
These relocations represent offsets from the __tls_base symbol.

Previously we were just using normal MEMORY_ADDR relocations and relying
on the linker to select a segment-offset rather and absolute value in
Symbol::getVirtualAddress().  Using an explicit relocation type allows
allow us to clearly distinguish absolute from relative relocations based
on the relocation information alone.

One place this is useful is being able to reject absolute relocation in
the PIC case, but still accept TLS relocations.

Differential Revision: https://reviews.llvm.org/D91276
2020-11-13 07:59:29 -08:00
Sam Clegg b646e8b154 [lld][WebAssembly] Add test for TLS BSS data. NFC.
Differential Revision: https://reviews.llvm.org/D91231
2020-11-13 07:52:18 -08:00
Fangrui Song 8df4e60945 [ELF] Don't consider SHF_ALLOC ".debug*" sections debug sections
Fixes PR48071

* The Rust compiler produces SHF_ALLOC `.debug_gdb_scripts` (which normally does not have the flag)
* `.debug_gdb_scripts` sections are removed from `inputSections` due to --strip-debug/--strip-all
* When processing --gc-sections, pieces of a SHF_MERGE section can be marked live separately

`=>` segfault when marking liveness of a `.debug_gdb_scripts` which is not split into pieces (because it is not in `inputSections`)

This patch circumvents the problem by not treating SHF_ALLOC ".debug*" as debug sections (to prevent --strip-debug's stripping)
(which is still useful on its own).

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D91291
2020-11-12 09:59:43 -08:00
Fangrui Song 40a42f9f3f [ELF] Make SORT_INIT_PRIORITY support .ctors.N
Input sections `.ctors/.ctors.N` may go to either the output section `.init_array` or the output section `.ctors`:

* output `.ctors`: currently we sort them by name. This patch changes to sort by priority from high to low. If N in `.ctors.N` is in the form of %05u, there is no semantic difference. Actually GCC and Clang do use %05u. (In the test `ctors_dtors_priority.s` and Gold's test `gold/testsuite/script_test_14.s`, we can see %03u, but they are not really produced by compilers.)
* output `.init_array`: users can provide an input section description `SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)` to mix `.init_array.*` and `.ctors.*`. This can make .init_array.N and .ctors.(65535-N) interchangeable.

With this change, users can mix `.ctors.N` and `.init_array.N` in `.init_array` (PR44698 and PR48096) with linker scripts. As an example:

```
SECTIONS {
  .init_array : {
    *(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*))
    *(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)
  }
} INSERT AFTER .fini_array;
SECTIONS {
  .fini_array : {
    *(SORT_BY_INIT_PRIORITY(.fini_array.* .dtors.*))
    *(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)
  }
} INSERT BEFORE .init_array;
```

Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D91187
2020-11-12 08:56:12 -08:00
Fangrui Song 73d01a80ce [ELF] Sort by input order within an input section description
According to
https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html#Input-Section-Basics
for `*(.a .b)`, the order should match the input order:

* for `ld 1.o 2.o`, sections from 1.o precede sections from 2.o
* within a file, `.a` and `.b` appear in the section header table order

This patch implements the behavior. The interaction with `SORT*` and --sort-section is:

Matched sections are ordered by radix sort with the keys being `(SORT*, --sort-section, input order)`,
where `SORT*` (if present) is most significant.

> Note, multiple `SORT*` within an input section description has undocumented and
> confusing behaviors in GNU ld:
> https://sourceware.org/pipermail/binutils/2020-November/114083.html
> Therefore multiple `SORT*` is not the focus for this patch but
> this patch still strives to have an explainable behavior.

As an example, we partition `SORT(a.*) b.* c.* SORT(d.*)`, into
`SORT(a.*) | b.* c.* | SORT(d.*)` and perform sorting within groups. Sections
matched by patterns between two `SORT*` are sorted by input order.  If
--sort-alignment is given, they are sorted by --sort-alignment, breaking tie by
input order.

This patch also allows a section to be matched by multiple patterns, previously
duplicated sections could occupy more space in the output and had erroneous zero bytes.

The patch is in preparation for support for
`*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array .ctors)`,
which will allow LLD to mix .ctors*/.init_array* like GNU ld (gold's --ctors-in-init-array)
PR44698 and PR48096

Reviewed By: grimar, psmith

Differential Revision: https://reviews.llvm.org/D91127
2020-11-12 08:53:11 -08:00
Fangrui Song 2a9aed0e8b [ELF] Support multiple SORT in an input section description
The second `SORT` in `*(SORT(...) SORT(...))` is incorrectly parsed as a file pattern.
Fix the bug by stopping at `SORT*` in `readInputSectionsList`.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D91180
2020-11-12 08:46:53 -08:00
Alexander Kornienko a196e8092a [lld] Use temporary directory to create test outputs 2020-11-12 14:24:05 +01:00
Alexandre Ganea 45b8a741fb [LLD][COFF] When using LLD-as-a-library, always prevent re-entrance on failures
This is a follow-up for D70378 (Cover usage of LLD as a library).

While debugging an intermittent failure on a bot, I recalled this scenario which
causes the issue:

1.When executing lld/test/ELF/invalid/symtab-sh-info.s L45, we reach
  lld:🧝:Obj-File::ObjFile() which goes straight into its base ELFFileBase(),
  then ELFFileBase::init().
2.At that point fatal() is thrown in lld/ELF/InputFiles.cpp L381, leaving a
  half-initialized ObjFile instance.
3.We then end up in lld::exitLld() and since we are running with LLD_IN_TEST, we
  hapily restore the control flow to CrashRecoveryContext::RunSafely() then back
  in lld::safeLldMain().
4.Before this patch, we called errorHandler().reset() just after, and this
  attempted to reset the associated SpecificAlloc<ObjFile<ELF64LE>>. That tried
  to free the half-initialized ObjFile instance, and more precisely its
  ObjFile::dwarf member.

Sometimes that worked, sometimes it failed and was catched by the
CrashRecoveryContext. This scenario was the reason we called
errorHandler().reset() through a CrashRecoveryContext.

But in some rare cases, the above repro somehow corrupted the heap, creating a
stack overflow. When the CrashRecoveryContext's filter (that is,
__except (ExceptionFilter(GetExceptionInformation()))) tried to handle the
exception, it crashed again since the stack was exhausted -- and that took the
whole application down. That is the issue seen on the bot. Locally it happens
about 1 times out of 15.

Now this situation can happen anywhere in LLD. Since catching stack overflows is
not a reliable scenario ATM when using CrashRecoveryContext, we're now
preventing further re-entrance when such failures occur, by signaling
lld::SafeReturn::canRunAgain=false. When running with LLD_IN_TEST=2 (or above),
only one iteration will be executed, instead of two.

Differential Revision: https://reviews.llvm.org/D88348
2020-11-12 08:14:43 -05:00
Hans Wennborg 418f18c6cd Revert "Reland [CFGuard] Add address-taken IAT tables and delay-load support"
This broke both Firefox and Chromium (PR47905) due to what seems like dllimport
function not being handled correctly.

> This patch adds support for creating Guard Address-Taken IAT Entry Tables (.giats$y sections) in object files, matching the behavior of MSVC. These contain lists of address-taken imported functions, which are used by the linker to create the final GIATS table.
> Additionally, if any DLLs are delay-loaded, the linker must look through the .giats tables and add the respective load thunks of address-taken imports to the GFIDS table, as these are also valid call targets.
>
> Reviewed By: rnk
>
> Differential Revision: https://reviews.llvm.org/D87544

This reverts commit cfd8481da1.
2020-11-11 16:03:33 +01:00
Sam Clegg 29a3056bb5 [lld][WebAssembly] Allow references to __tls_base without shared memory
Previously we limited the use of atomics and TLS to programs
linked with `--shared-memory`.

However, as of https://reviews.llvm.org/D79530 we now allow
programs that use atomic to be linked without `--shared-memory`.
For this to be useful we also want to all TLS usage in such
programs.  In this case, since we know we are single threaded
we simply include the TLS data as a regular active segment
and create an immutable `__tls_base` global that point to the
start of this segment.

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

Differential Revision: https://reviews.llvm.org/D91115
2020-11-10 17:58:06 -08:00
Jez Ng 21f831134c [lld-macho] Add very basic support for LTO
Just enough to consume some bitcode files and link them. There's more
to be done around the symbol resolution API and the LTO config, but I don't yet
understand what all the various LTO settings do...

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

Differential Revision: https://reviews.llvm.org/D90663
2020-11-10 12:19:28 -08:00
Jez Ng 6cf244327b [lld-macho][easy] Fix segment max protection
We should have maxprot == initprot for all non-i386 architectures, which
is what ld64 does.

Reviewed By: #lld-macho, compnerd

Differential Revision: https://reviews.llvm.org/D89420
2020-11-10 12:19:28 -08:00
Jez Ng b86908171e [lld-macho] Implement LC_UUID
Apple devtools use this to locate the dSYM files for a given
binary.

The UUID is computed based on an MD5 hash of the binary's contents. In order to
hash the contents, we must first write them, but LC_UUID itself must be part of
the written contents in order for all the offsets to be calculated correctly.
We resolve this circular paradox by first writing an LC_UUID with an all-zero
UUID, then updating the UUID with its real value later.

I'm not sure there's a good way to test that the value of the UUID is
"as expected", so I've just checked that it's present.

Reviewed By: #lld-macho, compnerd, smeenai

Differential Revision: https://reviews.llvm.org/D89418
2020-11-10 12:19:28 -08:00
Jez Ng 2e8e1bdb89 [lld-macho] Support linking against stub dylibs
Stub dylibs differ from "real" dylibs in that they lack any content in
their sections. What they do have are export tries and symbol tables,
which means we can still link against them. I am unclear how to
properly create these stub dylibs; XCode 11.3's `lipo` is able to create
stub dylibs, but those lack LC_ID_DYLIB load commands and are considered
invalid by most tooling. Newer versions of `lipo` aren't able to create
stub dylibs at all.  However, recent SDKs in XCode still come with valid
stub dylibs, so it still seems worthwhile to support them. The YAML in
this diff's test was generated by taking a non-stub dylib and editing
the appropriate fields.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D89012
2020-11-10 12:19:27 -08:00
Sam Clegg 504cb2730c [lld][WebAssembly] Convert TLS tests to asm format
Fix a corresponding bug in WasmAsmParser around parsing.tdata sections.

Differential Revision: https://reviews.llvm.org/D91113
2020-11-10 11:38:53 -08:00
James Henderson d2f7f775ca [lld][ELF][test] Add additional --symbol-ordering-file testing
This covers a few cases that aren't otherwise tested:
1) Non-ascii symbol names are ordered.
2) Comments, whitespace and blank lines are trimmed.
3) Missing order files result in an error.

Reviewed by: MaskRay, grimar

Differential Revision: https://reviews.llvm.org/D90933
2020-11-10 10:28:47 +00:00
James Henderson 439341b9bf [lld][ELF] Add additional time trace categories
I noticed when running a large link with the --time-trace option that
there were several areas which were missing any specific time trace
categories (aside from the generic link/ExecuteLinker categories). This
patch adds new categories to fill most of the "gaps", or to provide more
detail than was previously provided.

Reviewed by: MaskRay, grimar, russell.gallop

Differential Revision: https://reviews.llvm.org/D90686
2020-11-10 10:28:46 +00:00
Fangrui Song b22317705d [ELF] Special case static_assert for _WIN32
I don't have a Windows machine. Hope someone can test why its InputSection is
still larger.
2020-11-09 10:08:44 -08:00
Fangrui Song 2eccde4a2b [ELF] Make InputSection smaller
On LP64/Windows platforms, this decreases sizeof(InputSection) from 208 (larger
on Windows) to 184.

For a large executable (7.6GiB, inputSections.size()=5105122,
make<InputSection> called 4835760 times), this decreases cgroup
memory.max_usage_in_bytes by 0.6%

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D91018
2020-11-09 09:55:09 -08:00
Sebastian Neubauer a022b1ccd8 [AMDGPU] Add amdgpu_gfx calling convention
Add a calling convention called amdgpu_gfx for real function calls
within graphics shaders. For the moment, this uses the same calling
convention as other calls in amdgpu, with registers excluded for return
address, stack pointer and stack buffer descriptor.

Differential Revision: https://reviews.llvm.org/D88540
2020-11-09 16:51:44 +01:00
serge-sans-paille 1e70ec10eb [lld] Provide a hook to customize undefined symbols error handling
This is a follow up to https://reviews.llvm.org/D87758, implementing the missing
symbol part, as done by binutils.

Differential Revision: https://reviews.llvm.org/D89687
2020-11-09 13:28:48 +01:00
Fangrui Song 3ba3342232 [ELF] --warn-backrefs-exclude: use toString to match the documentation
The pattern should patch `a.a(a.o)` instead of `a.a`
2020-11-07 20:19:21 -08:00
Fangrui Song ec52408dec [ELF] Test R_*_SIZE for non-SHF_ALLOC sections 2020-11-07 20:19:21 -08:00
David Zarzycki 179d91b376 [lld testing] Unbreak read-only source builds
Tests must not modify the source tree.
2020-11-06 07:13:55 -05:00
rojamd b79e990f40 [lld][COFF] Add command line options for LTO with new pass manager
This is more or less a port of rL329598 (D45275) to the COFF linker.
Since there were already LTO-related settings under -opt:, I added
them there instead of new flags.

Differential Revision: https://reviews.llvm.org/D90624
2020-11-05 14:41:35 -05:00
Edd Dawson 1f78ab0ae6 [lld][ELF][test] test LTO-removed symbols are not in symtab
Differential Revision: https://reviews.llvm.org/D90680
2020-11-04 20:06:20 +00:00
serge-sans-paille 1c068a0103 Fix 'default label in switch which covers all enumeration values' warning 2020-11-03 12:58:15 +01:00
serge-sans-paille 3bdeb2ac2e [lld] missing doc entry for error handling script
Fix http://lab.llvm.org:8011/#/builders/69/builds/67
2020-11-03 11:16:02 +01:00
serge-sans-paille cfc32267e2 Provide a hook to customize missing library error handling
Make it possible for lld users to provide a custom script that would help to
find missing libraries. A possible scenario could be:

    % clang /tmp/a.c -fuse-ld=lld -loauth -Wl,--error-handling-script=/tmp/addLibrary.py
    unable to find library -loauth
    looking for relevant packages to provides that library

        liboauth-0.9.7-4.el7.i686
        liboauth-devel-0.9.7-4.el7.i686
        liboauth-0.9.7-4.el7.x86_64
        liboauth-devel-0.9.7-4.el7.x86_64
        pix-1.6.1-3.el7.x86_64

Where addLibrary would be called with the missing library name as first argument
(in that case addLibrary.py oauth)

Differential Revision: https://reviews.llvm.org/D87758
2020-11-03 11:01:29 +01:00
Peter Penzin e59726220f [LLD] [COFF] Align all debug directories
Match MSVC linker output - align all debug directories on four bytes,
while removing debug directory alignment. This would have the same
effect on CETCOMPAT support as D89919.

Chromium bug: https://crbug.com/1136664

Differential Revision: https://reviews.llvm.org/D89921
2020-11-02 10:47:51 -08:00
Fangrui Song 2fc704a0a5 [ELF] --emit-relocs: fix st_value of STT_SECTION in the presence of a gap before the first input section
In the presence of a gap, the st_value field of a STT_SECTION symbol is the
address of the first input section (incorrect if there is a gap). Set it to the
output section address instead.

In -r mode, this bug can cause an incorrect non-zero st_value of a STT_SECTION
symbol (while output sections have zero addresses, input sections may have
non-zero outSecOff).  The non-zero st_value can cause the final link to have
incorrect relocation computation (both GNU ld and LLD add st_value of the
STT_SECTION symbol to the output section address).

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D90520
2020-11-02 08:37:15 -08:00
Sam Clegg 1800b44651 [lld][WebAssembly] Remove bad-reloc test
This test was checking behaviour that only exists in the debug
configuration so will fail in release builds.

Perhaps there is way to keep this test around and only run
it in debug builds but for now I'm removing so fix the
release builders.

Differential Revision: https://reviews.llvm.org/D90542
2020-10-31 16:42:55 -07:00
Reid Kleckner 09662eeb46 Fix lld/wasm test portability issue, and XFAIL the test
I don't see any warnings from lld.wasm locally. Needs more
investigation.
2020-10-31 11:19:28 -07:00
Reid Kleckner 32cc962ef3 [COFF] Move ghash timers under the "add objects" timer
I had envisioned the ghash step as a big up front step, but as currently
written, the timers are nested, and we are notionally adding types from
objects, so we might as well arrange the timers this way.
2020-10-31 11:08:59 -07:00
Ali Tamur ca55c99d56 [lld][WebAssembly] Do not specify temporary file name in tests.
bad-reloc.yaml test introduced at 9d1409df87 uses a name (out.wasm) to specify a
temporary output file name, which causes breakage in our system.
2020-10-30 18:27:28 -07:00
Sam Clegg 9d1409df87 [lld][WebAssembly] Give better warnings on bad relocation sites
Differential Revision: https://reviews.llvm.org/D90443
2020-10-30 10:11:04 -07:00
Wouter van Oortmerssen b8c2d60df5 [WebAssembly] Improved LLD error messages in case of mixed wasm32/wasm64 object files
Differential Revision: https://reviews.llvm.org/D90428
2020-10-29 17:15:59 -07:00
Marcel Hlopko 9bb9b737c5 Remove HAVE_VCS_VERSION_INC, not needed
This preprocessor define was meant to be used to conditionally include VCSVersion.inc. However, the define was always set, and it was the content of the header that was conditionally generated. Therefore HAVE_VCS_VERSION_INC should be cleaned up.

Reviewed By: gribozavr2, MaskRay

Differential Revision: https://reviews.llvm.org/D84623
2020-10-29 13:09:05 -07:00
Fangrui Song ae73091f30 [ELF] -r: don't crash when a non-SHF_LINK_ORDER orphan is added before a SHF_LINK_ORDER orphan
Fixes https://github.com/ClangBuiltLinux/linux/issues/1186

If a non-SHF_LINK_ORDER orphan is added first, `firstIsec->flags & SHF_LINK_ORDER`
will be zero and we currently assert when calling `getLinkOrderDep`.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D90200
2020-10-28 08:56:42 -07:00
Sam Clegg 84129150ce [lld][WebAssembly] Fix memory size in dylink section for -pie exectuables
This field to represents the amount of static data needed by
an dynamic library or executable it should not include things
like heap or stack areas, which in the case of `-pie` are
not determined until runtime (e.g. __stack_pointer is imported).

Differential Revision: https://reviews.llvm.org/D90261
2020-10-27 16:05:52 -07:00
Benjamin Kramer 85e2af7ffe [lld][ELF] Don't write output to the test directory. NFC. 2020-10-26 18:10:31 +01:00
Fangrui Song 398b81067c [ELF] Don't crash on R_X86_64_GOTPCRELX for test/binop instructions
While MC did not produce R_X86_64_GOTPCRELX for test/binop instructions
(movl/adcl/addl/andl/...) before the previous commit, this code path has been
exercised by -fno-integrated-as for GNU as since 2016: -no-pie relaxing
may incorrectly access loc[-3] and produce a corrupted instruction.

Simply handle test/binop R_X86_64_GOTPCRELX like R_X86_64_GOTPCREL.
2020-10-24 15:14:17 -07:00
Fangrui Song 9267caebfa [ELF] Don't error on R_PPC64_REL24/R_PPC64_REL24_NOTOC referencing __tls_get_addr for missing R_PPC64_TLSGD/R_PPC64_TLSLD
This partially reverts D85994.

In glibc, elf/dl-sym.c calls the raw `__tls_get_addr` by specifying the
tls_index parameter. Such a call does not have a pairing R_PPC64_TLSGD/R_PPC64_TLSLD.
This is legitimate. Since we cannot distinguish the benign case from cases due
to toolchain issues, we have to be permissive.

Acked by Stefan Pintilie
2020-10-23 10:38:07 -07:00
Stefan Pintilie c6561ccfd9 [PowerPC][LLD] Support for PC Relative TLS for Local Dynamic
Add support to LLD for PC Relative Thread Local Storage for Local Dynamic.
This patch adds support for two relocations: R_PPC64_GOT_TLSLD_PCREL34 and
R_PPC64_DTPREL34.

The Local Dynamic code is:
```
pla r3, x@got@tlsld@pcrel        R_PPC64_GOT_TLSLD_PCREL34
bl __tls_get_addr@notoc(x@tlsld) R_PPC64_TLSLD
                                 R_PPC64_REL24_NOTOC
...
paddi r9, r3, x@dtprel           R_PPC64_DTPREL34
```

After relaxation to Local Exec:
```
paddi r3, r13, 0x1000
nop
...
paddi r9, r3, x@dtprel          R_PPC64_DTPREL34
```

Reviewed By: NeHuang, sfertile

Differential Revision: https://reviews.llvm.org/D87504
2020-10-23 08:23:56 -05:00
James Henderson 342040bf00 [lld][ELF][test] Add additional test coverage for LTO
These are all inspired by existing test coverage we have in an internal
testsuite.

Reviewed by: grimar, MaskRay

Differential Revision: https://reviews.llvm.org/D89775
2020-10-23 09:51:30 +01:00
Fangrui Song ce3c5dae06 [ELF] --warn-backrefs: save the referenced InputFile *
For a diagnostic `A refers to B` where B refers to a bitcode file, if the
symbol gets optimized out, the user may see `A refers to <internal>`; if the
symbol is retained, the user may see `A refers to lto.tmp`.

Save the reference InputFile * in the DenseMap so that the original filename is
available in reportBackrefs().
2020-10-22 15:27:19 -07:00
Fangrui Song a8f9f08018 [ELF] Set SHF_INFO_LINK for .rel[a].plt and .rel[a].dyn
The ELF spec says

> If the sh_flags field for this section header includes the attribute SHF_INFO_LINK, then this member represents a section header table index.

Set SHF_INFO_LINK so that binary manipulation tools know that sh_info is
a section header table index instead of (the number of local symbols in the case of SHT_SYMTAB/SHT_DYNSYM).
We have already added SHF_INFO_LINK for --emit-relocs retained SHT_REL[A].

For example, we can teach llvm-objcopy to preserve the section index of the sh_info referenced section if
SHF_INFO_LINK is set. (GNU objcopy recognizes .rel[a].plt and updates
sh_info even if SHF_INFO_LINK is not set).

Reviewed By: grimar, psmith

Differential Revision: https://reviews.llvm.org/D89828
2020-10-22 09:48:19 -07:00
Fangrui Song b6e4aae2cc [ELF] --gc-sections: retain dependent sections of non-SHF_ALLOC sections
Fix http://lists.llvm.org/pipermail/llvm-dev/2020-October/145908.html

Currently non-SHF_ALLOC SHT_REL[A] (due to --emit-relocs) and SHF_LINK_ORDER are not
marked live.

Reviewed By: grimar, psmith

Differential Revision: https://reviews.llvm.org/D89841
2020-10-21 10:11:26 -07:00
Sylvestre Ledru 0784e17f1b Remove .svn from exclude list as we moved to git
Reviewed By: emaste

Differential Revision: https://reviews.llvm.org/D89859
2020-10-21 16:09:21 +02:00
Fangrui Song 38b632c16e [ELF] --gdb-index: support --icf={safe,all}
The combination has not been tested before. In the case of ICF,
`e.section->getVA(0)` equals the start address of the output section.

This can cause incorrect overlapping with the actual function at the
start of the output section and potentially trigger a GDB internal error
in `dw2_find_pc_sect_compunit_symtab` (presumably because:
if a short address range incorrectly starts at the start address of the
output section, GDB may pick it instead of the correct longer address
range. When mapping an address within the long address range but
out of the scope of the short address range, the routine may find
nothing - while the code asserts that it can find something).

Note that in the case of ICF there may be duplicate address range entries,
but GDB appears to be fine with them.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D89751
2020-10-20 09:35:32 -07:00
Georgii Rymar 6487ffafd1 Reland "[yaml2obj][ELF] - Simplify the code that performs sections validation."
This reverts commit 1b589f4d4d and relands the D89463
with the fix: update `MappingTraits<FileFilter>::validate()` in ClangTidyOptions.cpp to
match the new signature (change the return type to "std::string" from "StringRef").

Original commit message:

This:

Changes the return type of MappingTraits<T>>::validate to std::string
instead of StringRef. It allows to create more complex error messages.

It introduces std::vector<std::pair<StringRef, bool>> getEntries():
a new virtual method of Section, which is the base class for all sections.
It returns names of special section specific keys (e.g. "Entries") and flags that says if them exist in a YAML.
The code in validate() uses this list of entries descriptions to generalize validation.
This approach was discussed in the D89039 thread.

Differential revision: https://reviews.llvm.org/D89463
2020-10-20 16:25:33 +03:00
Georgii Rymar 1b589f4d4d Revert "[yaml2obj][ELF] - Simplify the code that performs sections validation."
This reverts commit b9e2b59680.
2020-10-20 15:16:56 +03:00
Georgii Rymar b9e2b59680 [yaml2obj][ELF] - Simplify the code that performs sections validation.
This:
1) Changes the return type of `MappingTraits<T>>::validate` to `std::string`
instead of `StringRef`. It allows to create more complex error messages.

2) It introduces std::vector<std::pair<StringRef, bool>> getEntries():
a new virtual method of Section, which is the base class for all sections.
It returns names of special section specific keys (e.g. "Entries") and flags that
says if them exist in a YAML. The code in validate() uses this list of entries
descriptions to generalize validation.
This approach was discussed in the D89039 thread.

Differential revision: https://reviews.llvm.org/D89463
2020-10-20 11:28:23 +03:00
Martin Storsjö 3785a413fe Reapply [LLD] [COFF] Implement a GNU/ELF like -wrap option
Add a simple forwarding option in the MinGW frontend, and implement
the private -wrap option in the COFF linker.

The feature in lld-link isn't gated by the -lldmingw option, but
the option is left as a private, undocumented option primarily
used by the MinGW driver.

The implementation is significantly based on the support for --wrap
in the ELF linker, but many small nuance details are different
between the ELF and COFF linkers, ending up with more than a few
implementation differences.

This fixes https://bugs.llvm.org/show_bug.cgi?id=47384.

Differential Revision: https://reviews.llvm.org/D89004

Reapplied with the bitfield member canInline fixed so it doesn't break
builds targeting windows.
2020-10-15 22:14:02 +03:00
Arthur Eubanks 3d338f6813 Revert "[LLD] [COFF] Implement a GNU/ELF like -wrap option"
This reverts commit a012c704b5.

Breaks Windows builds.

C:\src\llvm-mint\lld\COFF\Symbols.cpp(26,1): error: static_assert failed due to requirement 'sizeof(lld::coff::SymbolUnion) <= 48' "symbols should be optimized for memory usage"
static_assert(sizeof(SymbolUnion) <= 48,
2020-10-15 10:27:25 -07:00
Martin Storsjö a012c704b5 [LLD] [COFF] Implement a GNU/ELF like -wrap option
Add a simple forwarding option in the MinGW frontend, and implement
the private -wrap option in the COFF linker.

The feature in lld-link isn't gated by the -lldmingw option, but
the option is left as a private, undocumented option primarily
used by the MinGW driver.

The implementation is significantly based on the support for --wrap
in the ELF linker, but many small nuance details are different
between the ELF and COFF linkers, ending up with more than a few
implementation differences.

This fixes https://bugs.llvm.org/show_bug.cgi?id=47384.

Differential Revision: https://reviews.llvm.org/D89004
2020-10-15 18:34:02 +03:00
Martin Storsjö 9803cf57d6 [LLD] [COFF] Fix a condition that was missed in 7f0e6c31c2. NFC.
This should fix cases when e.g. auto import is enabled without
mingw mode in total being enabled.

Differential Revision: https://reviews.llvm.org/D89006
2020-10-15 18:34:01 +03:00
Andrew Ng 88ce27c39c [LLD][ELF] Improve ICF for relocations to ineligible sections via "aliases"
ICF was not able to merge equivalent sections because of relocations to
sections ineligible for ICF that use alternative symbols, e.g. symbol
aliases or section relative relocations.

Merging in this scenario has been enabled by giving the sections that
are ineligible for ICF a unique ID, i.e. an equivalence class of their
own. This approach also provides another benefit as it improves the
hashing that is used to perform the initial equivalance grouping for
ICF. This is because the ICF ineligible sections can now contribute a
unique value towards the hashes instead of the same value of zero. This
has been seen to reduce link time with ICF by ~68% for objects compiled
with -fprofile-instr-generate.

In order to facilitate this use of a unique ID, the existing
inconsistent approach to the setting of the InputSection eqClass in ICF
has been changed so that there is a clear distinction between the
eqClass values of ICF eligible sections and those of the ineligible
sections that have a unique ID. This inconsistency could have caused
incorrect equivalence class equality in the past, although it appears
that no issues were encountered in actual use.

Differential Revision: https://reviews.llvm.org/D88830
2020-10-15 12:43:14 +01:00
Luqman Aden 6a73d6564a [LLD] Set alignment as part of Characteristics in TLS table.
Fixes https://bugs.llvm.org/show_bug.cgi?id=46473

LLD wasn't previously specifying any specific alignment in the TLS table's Characteristics field so the loader would just assume the default value (16 bytes). This works most of the time except if you have thread locals that want specific higher alignments (e.g. 32 as in the bug) *even* if they specify an alignment on the thread local. This change updates LLD to take the max alignment from tls section.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D88637
2020-10-15 00:22:40 -07:00
Luqman Aden f87c98def8 Revert "[LLD] Set alignment as part of Characteristics in TLS table."
Revert individual wip commits and will instead follow up with a
single commit with all the changes. Makes cherry-picking easier
and will contain all the right tags.

This reverts commit 32a4ad3b6c.
This reverts commit 7fe13af676.
This reverts commit 51fbc1bef6.
This reverts commit f80950a8bb.
This reverts commit 0778cad9f3.
This reverts commit 8b70d527d7.
2020-10-15 00:21:36 -07:00
Luqman Aden 32a4ad3b6c [LLD] Set alignment as part of Characteristics in TLS table.
Fixes https://bugs.llvm.org/show_bug.cgi?id=46473

LLD wasn't previously specifying any specific alignment in the TLS table's Characteristics field so the loader would just assume the default value (16 bytes). This works most of the time except if you have thread locals that want specific higher alignments (e.g. 32 as in the bug) *even* if they specify an alignment on the thread local. This change updates LLD to take the max alignment from tls section.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D88637
2020-10-14 19:41:03 -07:00
Luqman Aden 7fe13af676 Nit: Use early return to reduce indentation. 2020-10-14 19:34:32 -07:00
Luqman Aden f80950a8bb Update tests. 2020-10-14 19:34:32 -07:00
Luqman Aden 8b70d527d7 [LLD] Set alignment as part of Characteristics in TLS table.
Differential Revision: https://reviews.llvm.org/D88637
2020-10-14 19:34:31 -07:00
Konstantin Zhuravlyov 3fdf3b1539 AMDGPU: Update AMDHSA code object version handling
Differential Revision: https://reviews.llvm.org/D89076
2020-10-14 13:04:27 -04:00
jasonliu f85bcc21dd [AIX] Turn -fdata-sections on by default in Clang
Summary:

This patch does the following:
1. Make InitTargetOptionsFromCodeGenFlags() accepts Triple as a
 parameter, because some options' default value is triple dependant.
2. DataSections is turned on by default on AIX for llc.
3. Test cases change accordingly because of the default behaviour change.
4. Clang Driver passes in -fdata-sections by default on AIX.

Reviewed By: MaskRay, DiggerLin

Differential Revision: https://reviews.llvm.org/D88737
2020-10-14 15:58:31 +00:00
Luqman Aden dc128e5968 [test][lld] Mark TLS tests as REQUIRES: x86.
Fixes http://lab.llvm.org:8011/#/builders/119/builds/92
2020-10-14 00:29:06 -07:00
Luqman Aden 6b7738e204 [LLD] Add baseline test for TLS alignment. NFC.
Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D88646
2020-10-13 20:53:32 -07:00
Alexandre Ganea 617d64f6c5 Re-land [ThinLTO] Re-order modules for optimal multi-threaded processing
This reverts 9b5b305023 and fixes the unwanted re-ordering when generating ThinLTO indexes.

The goal of this patch is to better balance thread utilization during ThinLTO in-process linking (in llvm-lto2 or in LLD). Before this patch, large modules would often be scheduled late during execution, taking a long time to complete, thus starving the thread pool.

We now sort modules in descending order, based on each module's bitcode size, so that larger modules are processed first. By doing so, smaller modules have a better chance to keep the thread pool active, and thus avoid starvation when the bitcode compilation is almost complete.

In our case (on dual Intel Xeon Gold 6140, Windows 10 version 2004, two-stage build), this saves 15 sec when linking `clang.exe` with LLD & -flto=thin, /opt:lldltojobs=all, no ThinLTO cache, -DLLVM_INTEGRATED_CRT_ALLOC=d:\git\rpmalloc.

Before patch: 100 sec
After patch: 85 sec

Inspired by the work done by David Callahan in D60495.

Differential Revision: https://reviews.llvm.org/D87966
2020-10-13 21:54:15 -04:00
Andrew Paverd cfd8481da1 Reland [CFGuard] Add address-taken IAT tables and delay-load support
This patch adds support for creating Guard Address-Taken IAT Entry Tables (.giats$y sections) in object files, matching the behavior of MSVC. These contain lists of address-taken imported functions, which are used by the linker to create the final GIATS table.
Additionally, if any DLLs are delay-loaded, the linker must look through the .giats tables and add the respective load thunks of address-taken imports to the GFIDS table, as these are also valid call targets.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D87544
2020-10-13 13:20:52 -07:00
Konstantin Zhuravlyov f218652a36 LLD/AMDGPU: Infer os abi based on input llvm bitcode
Differential Revision: https://reviews.llvm.org/D89042
2020-10-13 12:20:28 -04:00
Paulo Matos 388fb67b0d [WebAssembly] Added .tabletype to asm and multiple table support in obj files
Adds more testing in basic-assembly.s and a new test tables.s.
Adds support to yaml reading and writing of tables as well.

Differential Revision: https://reviews.llvm.org/D88815
2020-10-13 07:52:23 -07:00
Sam Clegg b3b4cda104 [lld][WebAssembly] Don't GC library objects under `--whole-archive`
Followup on https://reviews.llvm.org/D85062 which ignores
entire library objects when no symbols are used within them.
This is shouldn't apply with `--whole-archive` since this
is specified to treat them like direct object inputs.

Differential Revision: https://reviews.llvm.org/D89290
2020-10-12 21:19:19 -07:00
Dan Gohman 950ae43091 [WebAssembly] GC constructor functions in otherwise unused archive objects
This allows `__wasilibc_populate_libpreopen` to be GC'd in more cases
where it isn't needed, including when linked from Rust's libstd.

Differential Revision: https://reviews.llvm.org/D85062
2020-10-12 18:54:57 -07:00
Sam Clegg 2513407d39 [lld][WebAssembly] Add support for -Bsymbolic flag
This flag works in a similar way to the ELF linker in that it
will resolve any defined symbols to their local definition with
a shared library or -pie executable.

This flag has no effect on static linking.

Differential Revision: https://reviews.llvm.org/D89152
2020-10-12 17:25:04 -07:00
Martin Storsjö d77d727339 [LLD] [COFF] Fix a ubsan error in pdb-type-server-missing.yaml
This error has been present since 5519e4da83.

Differential Revision: https://reviews.llvm.org/D89027
2020-10-12 23:28:23 +03:00
Christian Iversen a9cefc3dee [ELF] Fix broken bitstream linking with lld when e_machine > 255
In ELF/InputFiles.cpp, getBitcodeMachineKind() is limited to uint8_t return
type. This works as long as EM_xxx is < 256, which is true for common
architectures, but not for some newly assigned or unofficial EM_* values.

The corresponding ELF field (e_machine) can hold uint16_t.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D89185
2020-10-11 14:19:25 -07:00
Martin Storsjö 1dbfd87319 [LLD] [ELF] Fix the help listing for the wrap option. NFC.
This option just takes a single symbol name per invocation of the
option.

Differential Revision: https://reviews.llvm.org/D89007
2020-10-09 15:32:00 +03:00
Fangrui Song db1988f038 [ELF] Don't change binding to STB_WEAK for an undefined specified by -u
Similar to D66992.
In GNU ld, a -u specified symbol is a STB_DEFAULT undefined.
It cannot be changed to STB_WEAK by a later STB_WEAK undefined in a regular object file.

The behavior is consistent with our model because -u means "we need to fetch a lazy definition".
It should not be altered just because there is also a STB_WEAK undefined.

Note, our -u semantics are still different from GNU ld (https://github.com/ClangBuiltLinux/linux/issues/515):
we don't force the specified symbol to appear in .symtab This is a deliberate decision.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D88945
2020-10-08 08:31:34 -07:00
Mateusz Mikuła 9b58b0c06e [LLD] Ignore ELF tests when ld.lld defaults to MinGW
Follow-up to D87418.

Differential Revision: https://reviews.llvm.org/D88991
2020-10-08 09:34:46 +03:00
Martin Storsjö 9b2b32743d [LLD] [ELF] Fix up a comment regarding the --wrap option. NFC.
Add missing leading underscores to the __wrap_<symbol> and
__real_<symbol> names.

Differential Revision: https://reviews.llvm.org/D89008
2020-10-08 09:33:23 +03:00
Martin Storsjö 6e6a5acf00 [LLD] [MinGW] Move an option definitions to alphabetical order, wrap a line. NFC. 2020-10-07 15:14:07 +03:00
Martin Storsjö 61e2f9fa2e [LLD] [MinGW] Support setting the subsystem version via the subsystem argument
If a version is specified both with --{major,minor}-subsystem-version and
with --subsystem <name>:<version>, the one specified last (that actually
sets a version) takes precedance in GNU ld; thus doing the same here.

Differential Revision: https://reviews.llvm.org/D88804
2020-10-05 23:08:08 +03:00
Martin Storsjö bc8f3b424c [LLD] [MinGW] Simplify handling of os/subsystem version
As they can be set independently after D88802, we can get rid of a bit
of extra code - simplifying the logic here before adding more
complication to it later.

Differential Revision: https://reviews.llvm.org/D88803
2020-10-05 23:08:02 +03:00
Martin Storsjö 45c4c54003 [LLD] [COFF] Add a private option for setting the os version separately from subsystem version
The MinGW driver has separate options for OS and subsystem version.
Having this available in lld-link allows the MinGW driver to both match
GNU ld better and simplifies the code for merging two (potentially
mismatching) arguments into one.

Differential Revision: https://reviews.llvm.org/D88802
2020-10-05 23:08:01 +03:00
Martin Storsjö 19e86336ef [LLD] [COFF] Fix parsing version numbers with leading zeros
Parse the components as decimal, instead of decuding the base from
the string. This avoids ambiguity if the second number contains leading
zeros, which previously were parsed as indicating an octal number.

MS link.exe doesn't support hexadecimal numbers in the version numbers,
neither in /version nor in /subsystem.

Differential Revision: https://reviews.llvm.org/D88801
2020-10-05 23:08:00 +03:00
Alexandre Ganea fe1f0a1a19 [LLD] Fix /time formatting for very long runs. NFC. 2020-10-02 09:53:43 -04:00
Alexandre Ganea 55b97a6d2a [LLD][COFF] Add more type record information to /summary
This adds the following two new lines to /summary:

      21351 Input OBJ files (expanded from all cmd-line inputs)
         61 PDB type server dependencies
         38 Precomp OBJ dependencies
 1420669231 Input type records         <<<<
78665073382 Input type records bytes   <<<<
    8801393 Merged TPI records
    3177158 Merged IPI records
      59194 Output PDB strings
   71576766 Global symbol records
   25416935 Module symbol records
    2103431 Public symbol records

Differential Revision: https://reviews.llvm.org/D88703
2020-10-02 09:36:11 -04:00
Alexandre Ganea 4140f0744f [LLD][COFF] Fix crash with /summary and PCH input files
Before this patch /summary was crashing with some .PCH.OBJ files, because tpiMap[srcIdx++] was reading at the wrong location. When the TpiSource depends on a .PCH.OBJ file, the types should be offset by the previously merged PCH.OBJ set of indices.

Differential Revision: https://reviews.llvm.org/D88678
2020-10-01 17:08:35 -04:00
Fangrui Song 88f2fe5cad Raland D87318 [LLD][PowerPC] Add support for R_PPC64_GOT_TLSGD_PCREL34 used in TLS General Dynamic
Add Thread Local Storage support for the 34 bit relocation R_PPC64_GOT_TLSGD_PCREL34 used in General Dynamic.

The compiler will produce code that looks like:
```
pla r3, x@got@tlsgd@pcrel            R_PPC64_GOT_TLSGD_PCREL34
bl __tls_get_addr@notoc(x@tlsgd)     R_PPC64_TLSGD
                                     R_PPC64_REL24_NOTOC
```
LLD should be able to correctly compute the relocation for  R_PPC64_GOT_TLSGD_PCREL34 as well as do the following two relaxations where possible:
General Dynamic to Local Exec:
```
paddi r3, r13, x@tprel
nop
```
and General Dynamic to Initial Exec:
```
pld r3, x@got@tprel@pcrel
add r3, r3, r13
```
Note:
This patch adds support for the PC Relative (no TOC) version of General Dynamic on top of the existing support for the TOC version of General Dynamic.
The ABI does not provide any way to tell by looking only at the relocation `R_PPC64_TLSGD` when it is being used in a TOC instruction sequence or and when it is being used in a no TOC sequence. The TOC sequence should always be 4 byte aligned. This patch adds one to the offset of the relocation when it is being used in a no TOC sequence. In this way LLD can tell by looking at the alignment of the offset of `R_PPC64_TLSGD` whether or not it is being used as part of a TOC or no TOC sequence.

Reviewed By: NeHuang, sfertile, MaskRay

Differential Revision: https://reviews.llvm.org/D87318
2020-10-01 12:36:33 -07:00
Reid Kleckner 5d46d7e8b2 [PDB] Use one func id DenseMap instead of per-source maps, NFC
This avoids some DenseMap copies when /Zi is in use, and results in
fewer data structures.

Differential Revision: https://reviews.llvm.org/D88617
2020-10-01 12:22:27 -07:00
Arthur Eubanks 499260c03b Revert "[CFGuard] Add address-taken IAT tables and delay-load support"
This reverts commit ef4e971e5e.
2020-10-01 11:29:54 -07:00
Stefan Pintilie 5f3e565f59 Revert "[LLD][PowerPC] Add support for R_PPC64_GOT_TLSGD_PCREL34 used in TLS General Dynamic"
This reverts commit 79122868f9.
2020-10-01 13:28:35 -05:00
Stefan Pintilie 79122868f9 [LLD][PowerPC] Add support for R_PPC64_GOT_TLSGD_PCREL34 used in TLS General Dynamic
Add Thread Local Storage support for the 34 bit relocation R_PPC64_GOT_TLSGD_PCREL34 used in General Dynamic.

The compiler will produce code that looks like:
```
pla r3, x@got@tlsgd@pcrel            R_PPC64_GOT_TLSGD_PCREL34
bl __tls_get_addr@notoc(x@tlsgd)     R_PPC64_TLSGD
                                     R_PPC64_REL24_NOTOC
```
LLD should be able to correctly compute the relocation for  R_PPC64_GOT_TLSGD_PCREL34 as well as do the following two relaxations where possible:
General Dynamic to Local Exec:
```
paddi r3, r13, x@tprel
nop
```
and General Dynamic to Initial Exec:
```
pld r3, x@got@tprel@pcrel
add r3, r3, r13
```
Note:
This patch adds support for the PC Relative (no TOC) version of General Dynamic on top of the existing support for the TOC version of General Dynamic.
The ABI does not provide any way to tell by looking only at the relocation `R_PPC64_TLSGD` when it is being used in a TOC instruction sequence or and when it is being used in a no TOC sequence. The TOC sequence should always be 4 byte aligned. This patch adds one to the offset of the relocation when it is being used in a no TOC sequence. In this way LLD can tell by looking at the alignment of the offset of `R_PPC64_TLSGD` whether or not it is being used as part of a TOC or no TOC sequence.

Reviewed By: NeHuang, sfertile, MaskRay

Differential Revision: https://reviews.llvm.org/D87318
2020-10-01 13:00:37 -05:00
James Henderson a20168d030 [Archive] Don't throw away errors for malformed archive members
When adding an archive member with a problem, e.g. a new bitcode with an
old archiver, containing an unsupported attribute, or an ELF file with a
malformed symbol table, the archiver would throw away the error and
simply add the member to the archive without any symbol entries. This
meant that the resultant archive could be silently unusable when not
using --whole-archive, and result in unexpected undefined symbols.

This change fixes this issue by addressing two FIXMEs and only throwing
away not-an-object errors. However, this meant that some LLD tests which
didn't need symbol tables and were using invalid members deliberately to
test the linker's malformed input handling no longer worked, so this
patch also stops the archiver from looking for symbols in an object if
it doesn't require a symbol table, and updates the tests accordingly.

Differential Revision: https://reviews.llvm.org/D88288

Reviewed by: grimar, rupprecht, MaskRay
2020-10-01 14:03:34 +01:00
Andrew Paverd ef4e971e5e [CFGuard] Add address-taken IAT tables and delay-load support
This patch adds support for creating Guard Address-Taken IAT Entry Tables (.giats$y sections) in object files, matching the behavior of MSVC. These contain lists of address-taken imported functions, which are used by the linker to create the final GIATS table.
Additionally, if any DLLs are delay-loaded, the linker must look through the .giats tables and add the respective load thunks of address-taken imports to the GFIDS table, as these are also valid call targets.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D87544
2020-10-01 12:45:07 +01:00
Fangrui Song 4e9277eda1 [ELF] --wrap: don't unnecessarily expose __real_
The routing rules are:

sym -> __wrap_sym
__real_sym -> sym

__wrap_sym and sym are routing targets, so they need to be exposed to the symbol
table. __real_sym is not and can be eliminated if not used by regular object.
2020-09-30 20:09:25 -07:00
Dan Gohman 6cd8511e59 [WebAssembly] New-style command support
This adds support for new-style command support. In this mode, all exports
are considered command entrypoints, and the linker inserts calls to
`__wasm_call_ctors` and `__wasm_call_dtors` for all such entrypoints.

This enables support for:

 - Command entrypoints taking arguments other than strings and return values
   other than `int`.
 - Multicall executables without requiring on the use of string-based
   command-line arguments.

This new behavior is disabled when the input has an explicit call to
`__wasm_call_ctors`, indicating code not expecting new-style command
support.

This change does mean that wasm-ld no longer supports DCE-ing the
`__wasm_call_ctors` function when there are no calls to it. If there are no
calls to it, and there are ctors present, we assume it's wasm-ld's job to
insert the calls. This seems ok though, because if there are ctors present,
the program is expecting them to be called. This change affects the
init-fini-gc.ll test.
2020-09-30 19:02:40 -07:00
Sam Clegg 3c45a06f26 [lld][WebAssembly] Allow exporting of mutable globals
In particular allow explict exporting of `__stack_pointer` but
exclud this from `--export-all` to avoid requiring the mutable
globals feature whenenve `--export-all` is used.

This uncovered a bug in populateTargetFeatures regarding checking
if the mutable-globals feature is allowed.

See: https://github.com/WebAssembly/binaryen/issues/2934

Differential Revision: https://reviews.llvm.org/D88506
2020-09-30 17:53:27 -07:00
Reid Kleckner 5519e4da83 Re-land "[PDB] Merge types in parallel when using ghashing"
Stored Error objects have to be checked, even if they are success
values.

This reverts commit 8d250ac3cd.
Relands commit 49b3459930655d879b2dc190ff8fe11c38a8be5f..

Original commit message:
-----------------------------------------

This makes type merging much faster (-24% on chrome.dll) when multiple
threads are available, but it slightly increases the time to link (+10%)
when /threads:1 is passed. With only one more thread, the new type
merging is faster (-11%). The output PDB should be identical to what it
was before this change.

To give an idea, here is the /time output placed side by side:
                              BEFORE    | AFTER
  Input File Reading:           956 ms  |  968 ms
  Code Layout:                  258 ms  |  190 ms
  Commit Output File:             6 ms  |    7 ms
  PDB Emission (Cumulative):   6691 ms  | 4253 ms
    Add Objects:               4341 ms  | 2927 ms
      Type Merging:            2814 ms  | 1269 ms  -55%!
      Symbol Merging:          1509 ms  | 1645 ms
    Publics Stream Layout:      111 ms  |  112 ms
    TPI Stream Layout:          764 ms  |   26 ms  trivial
    Commit to Disk:            1322 ms  | 1036 ms  -300ms
----------------------------------------- --------
Total Link Time:               8416 ms    5882 ms  -30% overall

The main source of the additional overhead in the single-threaded case
is the need to iterate all .debug$T sections up front to check which
type records should go in the IPI stream. See fillIsItemIndexFromDebugT.
With changes to the .debug$H section, we could pre-calculate this info
and eliminate the need to do this walk up front. That should restore
single-threaded performance back to what it was before this change.

This change will cause LLD to be much more parallel than it used to, and
for users who do multiple links in parallel, it could regress
performance. However, when the user is only doing one link, it's a huge
improvement. In the future, we can use NT worker threads to avoid
oversaturating the machine with work, but for now, this is such an
improvement for the single-link use case that I think we should land
this as is.

Algorithm
----------

Before this change, we essentially used a
DenseMap<GloballyHashedType, TypeIndex> to check if a type has already
been seen, and if it hasn't been seen, insert it now and use the next
available type index for it in the destination type stream. DenseMap
does not support concurrent insertion, and even if it did, the linker
must be deterministic: it cannot produce different PDBs by using
different numbers of threads. The output type stream must be in the same
order regardless of the order of hash table insertions.

In order to create a hash table that supports concurrent insertion, the
table cells must be small enough that they can be updated atomically.
The algorithm I used for updating the table using linear probing is
described in this paper, "Concurrent Hash Tables: Fast and General(?)!":
https://dl.acm.org/doi/10.1145/3309206

The GHashCell in this change is essentially a pair of 32-bit integer
indices: <sourceIndex, typeIndex>. The sourceIndex is the index of the
TpiSource object, and it represents an input type stream. The typeIndex
is the index of the type in the stream. Together, we have something like
a ragged 2D array of ghashes, which can be looked up as:
  tpiSources[tpiSrcIndex]->ghashes[typeIndex]

By using these side tables, we can omit the key data from the hash
table, and keep the table cell small. There is a cost to this: resolving
hash table collisions requires many more loads than simply looking at
the key in the same cache line as the insertion position. However, most
supported platforms should have a 64-bit CAS operation to update the
cell atomically.

To make the result of concurrent insertion deterministic, the cell
payloads must have a priority function. Defining one is pretty
straightforward: compare the two 32-bit numbers as a combined 64-bit
number. This means that types coming from inputs earlier on the command
line have a higher priority and are more likely to appear earlier in the
final PDB type stream than types from an input appearing later on the
link line.

After table insertion, the non-empty cells in the table can be copied
out of the main table and sorted by priority to determine the ordering
of the final type index stream. At this point, item and type records
must be separated, either by sorting or by splitting into two arrays,
and I chose sorting. This is why the GHashCell must contain the isItem
bit.

Once the final PDB TPI stream ordering is known, we need to compute a
mapping from source type index to PDB type index. To avoid starting over
from scratch and looking up every type again by its ghash, we save the
insertion position of every hash table insertion during the first
insertion phase. Because the table does not support rehashing, the
insertion position is stable. Using the array of insertion positions
indexed by source type index, we can replace the source type indices in
the ghash table cells with the PDB type indices.

Once the table cells have been updated to contain PDB type indices, the
mapping for each type source can be computed in parallel. Simply iterate
the list of cell positions and replace them with the PDB type index,
since the insertion positions are no longer needed.

Once we have a source to destination type index mapping for every type
source, there are no more data dependencies. We know which type records
are "unique" (not duplicates), and what their final type indices will
be. We can do the remapping in parallel, and accumulate type sizes and
type hashes in parallel by type source.

Lastly, TPI stream layout must be done serially. Accumulate all the type
records, sizes, and hashes, and add them to the PDB.

Differential Revision: https://reviews.llvm.org/D87805
2020-09-30 15:44:38 -07:00
Reid Kleckner 8d250ac3cd Revert "[PDB] Merge types in parallel when using ghashing"
This reverts commit 49b3459930.
2020-09-30 14:55:32 -07:00
Reid Kleckner 49b3459930 [PDB] Merge types in parallel when using ghashing
This makes type merging much faster (-24% on chrome.dll) when multiple
threads are available, but it slightly increases the time to link (+10%)
when /threads:1 is passed. With only one more thread, the new type
merging is faster (-11%). The output PDB should be identical to what it
was before this change.

To give an idea, here is the /time output placed side by side:
                              BEFORE    | AFTER
  Input File Reading:           956 ms  |  968 ms
  Code Layout:                  258 ms  |  190 ms
  Commit Output File:             6 ms  |    7 ms
  PDB Emission (Cumulative):   6691 ms  | 4253 ms
    Add Objects:               4341 ms  | 2927 ms
      Type Merging:            2814 ms  | 1269 ms  -55%!
      Symbol Merging:          1509 ms  | 1645 ms
    Publics Stream Layout:      111 ms  |  112 ms
    TPI Stream Layout:          764 ms  |   26 ms  trivial
    Commit to Disk:            1322 ms  | 1036 ms  -300ms
----------------------------------------- --------
Total Link Time:               8416 ms    5882 ms  -30% overall

The main source of the additional overhead in the single-threaded case
is the need to iterate all .debug$T sections up front to check which
type records should go in the IPI stream. See fillIsItemIndexFromDebugT.
With changes to the .debug$H section, we could pre-calculate this info
and eliminate the need to do this walk up front. That should restore
single-threaded performance back to what it was before this change.

This change will cause LLD to be much more parallel than it used to, and
for users who do multiple links in parallel, it could regress
performance. However, when the user is only doing one link, it's a huge
improvement. In the future, we can use NT worker threads to avoid
oversaturating the machine with work, but for now, this is such an
improvement for the single-link use case that I think we should land
this as is.

Algorithm
----------

Before this change, we essentially used a
DenseMap<GloballyHashedType, TypeIndex> to check if a type has already
been seen, and if it hasn't been seen, insert it now and use the next
available type index for it in the destination type stream. DenseMap
does not support concurrent insertion, and even if it did, the linker
must be deterministic: it cannot produce different PDBs by using
different numbers of threads. The output type stream must be in the same
order regardless of the order of hash table insertions.

In order to create a hash table that supports concurrent insertion, the
table cells must be small enough that they can be updated atomically.
The algorithm I used for updating the table using linear probing is
described in this paper, "Concurrent Hash Tables: Fast and General(?)!":
https://dl.acm.org/doi/10.1145/3309206

The GHashCell in this change is essentially a pair of 32-bit integer
indices: <sourceIndex, typeIndex>. The sourceIndex is the index of the
TpiSource object, and it represents an input type stream. The typeIndex
is the index of the type in the stream. Together, we have something like
a ragged 2D array of ghashes, which can be looked up as:
  tpiSources[tpiSrcIndex]->ghashes[typeIndex]

By using these side tables, we can omit the key data from the hash
table, and keep the table cell small. There is a cost to this: resolving
hash table collisions requires many more loads than simply looking at
the key in the same cache line as the insertion position. However, most
supported platforms should have a 64-bit CAS operation to update the
cell atomically.

To make the result of concurrent insertion deterministic, the cell
payloads must have a priority function. Defining one is pretty
straightforward: compare the two 32-bit numbers as a combined 64-bit
number. This means that types coming from inputs earlier on the command
line have a higher priority and are more likely to appear earlier in the
final PDB type stream than types from an input appearing later on the
link line.

After table insertion, the non-empty cells in the table can be copied
out of the main table and sorted by priority to determine the ordering
of the final type index stream. At this point, item and type records
must be separated, either by sorting or by splitting into two arrays,
and I chose sorting. This is why the GHashCell must contain the isItem
bit.

Once the final PDB TPI stream ordering is known, we need to compute a
mapping from source type index to PDB type index. To avoid starting over
from scratch and looking up every type again by its ghash, we save the
insertion position of every hash table insertion during the first
insertion phase. Because the table does not support rehashing, the
insertion position is stable. Using the array of insertion positions
indexed by source type index, we can replace the source type indices in
the ghash table cells with the PDB type indices.

Once the table cells have been updated to contain PDB type indices, the
mapping for each type source can be computed in parallel. Simply iterate
the list of cell positions and replace them with the PDB type index,
since the insertion positions are no longer needed.

Once we have a source to destination type index mapping for every type
source, there are no more data dependencies. We know which type records
are "unique" (not duplicates), and what their final type indices will
be. We can do the remapping in parallel, and accumulate type sizes and
type hashes in parallel by type source.

Lastly, TPI stream layout must be done serially. Accumulate all the type
records, sizes, and hashes, and add them to the PDB.

Differential Revision: https://reviews.llvm.org/D87805
2020-09-30 14:22:48 -07:00
Fangrui Song 259bb61c11 [ELF] Fix multiple -mllvm after D70378
Fixes https://reviews.llvm.org/D70378#2299569 Multiple -mllvm is intended to be supported.

We don't have a proper test for `-plugin-opt=-`. This patch adds the test as well.

Differential Revision: https://reviews.llvm.org/D88461
2020-09-29 10:26:58 -07:00
Benjamin Kramer b59dff4b16 [wasm] Move WasmTraits.h to BinaryFormat
There's no dependency on Object in there and this avoids a cyclic
dependency between libMC and libObject.
2020-09-28 22:07:28 +02:00
Fangrui Song 20e9c36c01 Internalize functions from various tools. NFC
And internalize some classes if I noticed them:)
2020-09-26 15:57:13 -07:00
Jez Ng 2c2a749448 [lld-macho] Ignore a few more undocumented flags
Reviewed By: #lld-macho, compnerd

Differential Revision: https://reviews.llvm.org/D88268
2020-09-25 11:28:37 -07:00
Jez Ng 643ec67a64 [lld-macho] Always include custom syslibroot when running tests
This greatly reduces the amount of boilerplate in our tests.

Reviewed By: #lld-macho, compnerd

Differential Revision: https://reviews.llvm.org/D87960
2020-09-25 11:28:36 -07:00
Jez Ng 62a3f0c984 [lld-macho] Support absolute symbols
They operate like Defined symbols but with no associated InputSection.

Note that `ld64` seems to treat the weak definition flag like a no-op for
absolute symbols, so I have replicated that behavior.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D87909
2020-09-25 11:28:35 -07:00
Jez Ng c7c9776f77 [lld-macho] Allow the entry symbol to be dynamically bound
Apparently this is used in real programs. I've handled this by reusing
the logic we already have for branch (function call) relocations.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D87852
2020-09-25 11:28:33 -07:00
Jez Ng f23f512691 [lld-macho] Support -bundle
Not 100% sure but it appears that bundles are almost identical to
dylibs, aside from the fact that they do not contain `LC_ID_DYLIB`. ld64's code
seems to treat bundles and dylibs identically in most places.

Supporting bundles allows us to run e.g. XCTests, as all test suites are
compiled into bundles which get dynamically loaded by the `xctest` test runner.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D87856
2020-09-25 11:28:32 -07:00
Jez Ng e4e673e75a [lld-macho] Implement support for PIC
* Implement rebase opcodes. Rebase opcodes tell dyld where absolute
  addresses have been encoded in the binary. If the binary is not loaded
  at its preferred address, dyld has to rebase these addresses by adding
  an offset to them.
* Support `-pie` and use it to test rebase opcodes.

This is necessary for absolute address references in dylibs, bundles etc
to work.

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D87199
2020-09-25 11:28:31 -07:00
Stefan Pintilie 8c53282d64 [PowerPC][NFC] Merged two switch entries.
Two switch entries did exactly the same thing. This patch merges them.
2020-09-25 09:49:13 -05:00
Stefan Pintilie d224175230 [PowerPC][LLD] Extend R2 save stub to support offsets of more than 26 bits
The R2 save stub will now support offsets up to 64 bits.

There are three cases that will be used.
1) The offset fits in 26 bits.
```
b <26 bit offset>
```
2) The offset does not fit in 26 bits but fits in 34 bits.
```
paddi r12, 0, <34 bit offset>, 1
mtctr r12
bctr
```
3) The offset does not fit in 34 bits. Since this is an R2 save stub we can use
the TOC in R2. We are not loading the offset but the actual address we want to
branch to.
```
addis r12, r2, <address in TOC lo>
ld r12 <address in TOC hi>(r12)
mtctr r12
bctr
```

In case 1) the stub is only 8 bytes while in cases 2) and 3) the stub will be
20 bytes.

Reviewed By: MaskRay, sfertile, NeHuang

Differential Revision: https://reviews.llvm.org/D87916
2020-09-25 06:39:14 -05:00
Thomas Lively 15a5e86fb3 [lld][WebAssembly] Allow `atomics` feature with unshared memory
https://github.com/WebAssembly/threads/issues/144 updated the
WebAssembly threads proposal to make atomic operations on unshared memories
valid. This change updates the feature checking in the linker accordingly.
Production WebAssembly engines have recently been updated to allow this
behvaior, but after this change users who accidentally use atomics with unshared
memories on older versions of the engines will get validation errors at runtime
rather than link errors.

Differential Revision: https://reviews.llvm.org/D79530
2020-09-24 20:35:29 -07:00
Fangrui Song 1ca6bd261e [lld] Clean up in lld::{coff,elf}::link after D70378
Library users should not need to call errorHandler().reset() explicitly.

google/iree calls lld:🧝:link and without the patch some global
variables are not cleaned up in the next invocation.
2020-09-24 18:02:45 -07:00
Snehasish Kumar 070555c6c0 [lld] Make -z keep-text-section-prefix recognize .text.split. as a prefix.
".text.split." holds symbols which are split out from functions in
other input sections. For example, with -fsplit-machine-functions,
placing the cold parts in .text.split instead of .text.unlikely mitigates
against poor profile inaccuracy. Techniques such as hugepage remapping can
make conservative decisions at the section granularity.

Differential Revision: https://reviews.llvm.org/D87840
2020-09-24 15:02:48 -07:00
Jez Ng 5213576fa2 [lld-macho][re-land] Implement and test resolution of common symbols
Earlier build break fixed in c32e69b2ce.

This reverts commit c367f93e85.
2020-09-24 15:00:56 -07:00
Jez Ng c32e69b2ce [lld-macho][re-land] Initial support for common symbols
Fix earlier build break via a static_cast.

This reverts commit 8112d494d3.

Differential Revision: https://reviews.llvm.org/D86909
2020-09-24 15:00:20 -07:00
Alexandre Ganea f2efb5742c [LLD][COFF] Cover usage of LLD-as-a-library in tests
In lit tests, we run each LLD invocation twice (LLD_IN_TEST=2), without shutting down the process in-between. This ensures a full cleanup is properly done between runs.
Only active for the COFF driver for now. Other drivers still use LLD_IN_TEST=1 which executes just one iteration with full cleanup, like before.
When the environment variable LLD_IN_TEST is unset, a shortcut is taken, only one iteration is executed, no cleanup for faster exit, like before.
A public API, lld::safeLldMain(), is also available when using LLD as a library.

Differential Revision: https://reviews.llvm.org/D70378
2020-09-24 15:07:50 -04:00
Alexandre Ganea 55624237be [LLD][COFF] Avoid overwriting inputs in tests
Before this patch, these two tests were emitting both a .DLL and .LIB. The output .LIB file name also happens to be an input .LIB file name. This prevented the test from executing a second time when LLD is re-entrant (LLD_IN_TEST=2).

This is a support patch for https://reviews.llvm.org/D70378.
2020-09-24 15:01:25 -04:00
Nico Weber 0389eff404 lld: Try to fix check-lld on incremental builds after 8f2c31f22b 2020-09-24 09:33:57 -04:00
James Henderson a4e42601d4 [lld][ELF][test] Add a couple of test cases for LTO behaviour
This patch expands two LTO test cases to check other aspects.

1) weak.ll has been expanded to show that it doesn't matter whether the
   first appearance of a weak symbol appears in a bitcode file or native
   object - that one is picked.
2) reproduce-lto.ll has been expanded to show that the bitcode files are
   stored in the reproduce package and that intermediate files (such as
   the LTO-compiled object) are not.

Differential Revision: https://reviews.llvm.org/D88094

Reviewed by: grimar, MaskRay
2020-09-24 11:49:20 +01:00
Muhammad Omair Javaid 8112d494d3 Revert "[lld-macho] Initial support for common symbols"
This reverts commit 63ace77962.

Breaks LLDB Arm build:
http://lab.llvm.org:8011/builders/lldb-arm-ubuntu/builds/4409
2020-09-24 12:26:40 +05:00
Muhammad Omair Javaid c367f93e85 Revert "[lld-macho] Implement and test resolution of common symbols"
This reverts commit cd7cb0c303.
Break lldb Arm build:
http://lab.llvm.org:8011/builders/lldb-arm-ubuntu/builds/4409
2020-09-24 12:25:47 +05:00
Jez Ng 9c70281497 [lld-macho][NFC] Make `!= nullptr` implicit 2020-09-23 20:09:49 -07:00
Jez Ng ca8752a793 [lld-macho][NFC] Refactor syslibroot / library path lookup
* Move computation of systemLibraryRoots into a separate function, so we
  can add more functionality to it without things becoming unwieldy
* Have `getSearchPaths` and related functions return by value instead of
  by output parameter. NRVO should ensure that performance is unaffected.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D87959
2020-09-23 19:26:41 -07:00
Jez Ng 98f03908d0 [lld-macho] Support -weak_lx, -weak_library, -weak_framework
They cause their corresponding libraries / frameworks to be loaded via
`LC_LOAD_WEAK_DYLIB` instead of `LC_LOAD_DYLIB`.

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D87929
2020-09-23 19:26:41 -07:00
Jez Ng 79412d6ca7 [lld-macho] Ignore `-mllvm` and its argument
Test Plan:

Reviewed By: #lld-macho, compnerd, MaskRay

Differential Revision: https://reviews.llvm.org/D87803
2020-09-23 19:26:40 -07:00
Jez Ng 5d26bd3b75 [lld-macho] Emit indirect symbol table
Makes it a little easier to read objdump's disassembly.

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D87178
2020-09-23 19:26:40 -07:00
Jez Ng cd7cb0c303 [lld-macho] Implement and test resolution of common symbols
Handle the case where there are both common and non-common definitions
of the same symbol. Add a bunch of tests to ensure compatibility with ld64.

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D86910
2020-09-23 19:26:40 -07:00
Jez Ng 63ace77962 [lld-macho] Initial support for common symbols
On Unix, it is traditionally allowed to write variable definitions without
initialization expressions (such as "int foo;") to header files. These are
called tentative definitions.

The compiler creates common symbols when it sees tentative definitions. When
linking the final binary, if there are remaining common symbols after name
resolution is complete, the linker converts them to regular defined symbols in
a `__common` section.

This diff implements most of that functionality, though we do not yet handle
the case where there are both common and non-common definitions of the same
symbol.

Reviewed By: #lld-macho, gkm

Differential Revision: https://reviews.llvm.org/D86909
2020-09-23 19:26:40 -07:00
Greg McGary 8f2c31f22b [lld-macho] handle options -search_paths_first, -search_dylibs_first
Differential Revision: https://reviews.llvm.org/D88054
2020-09-23 14:56:33 -07:00
Greg McGary fa5f945212 [lld-macho] cleanup unimplemented-option warnings
Remove all spurious `HelpHidden` flags from  `lld/MachO/Options.td`. Add test for `HelpHidden` to `warnIfUnimplementedOption()` so that the empty `// handled elsewhere` case is unnecessary.

Reviewed By: #lld-macho, int3, smeenai

Differential Revision: https://reviews.llvm.org/D88160
2020-09-23 14:38:23 -07:00
Greg McGary ab903560a4 [lld-maco] fix build breakage 2020-09-22 20:42:23 -07:00
Greg McGary 1a3ef0417c [lld-macho] In the context of relocs, s/target/referent/ for sections & symbols
The word "target" is overloaded, so lighten its load by using another word to denote the symbol or section to which a reloc points. While more stilted than "target", "referent" is rather less pompous than "designatum" or "denotatum". :P

Along the way, make a few neighboring variable names more descriptive.

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D87584
2020-09-22 20:31:01 -07:00
Greg McGary 145ce86dba [lld-macho] handle option -headerpad_max_install_names
Differential Revision: https://reviews.llvm.org/D88064
2020-09-22 17:24:19 -07:00
Greg McGary 703d3f2597 [lld-macho] Make lld::getInteger() tolerate leading "0x"/"0X" when base is 16
ld64 is cool with leading `0x` for hex command-line args, and we should be also.

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D88065
2020-09-22 08:56:20 -07:00
Greg McGary 7afbf3192d [lld-macho] minimally handle option -dynamic
Stifle the warning for unimplemented option `-dyamic`, since it is already the default. Add `Config::staticLink` and skeletal support for altering the flag, but otherwise leave the option `-static` as hidden and its warning in place.

Differential Revision: https://reviews.llvm.org/D88045
2020-09-22 08:03:44 -07:00
Victor Huang 967e29ff8c [LLD][PowerPC][test] Update thunk range error report for PPC64PCRelLongBranchThunk
Update the thunk range error report for PPC64PCRelLongBranchThunk and add a range
error test case for PPC64R12SetupStub.

Differential Revision: https://reviews.llvm.org/D87381
2020-09-22 07:37:54 -05:00
Stefan Pintilie c0071862bb [PowerPC] Add support for R_PPC64_GOT_TPREL_PCREL34 used in TLS Initial Exec
Add Thread Local Storage Initial Exec support to LLD.

This patch adds the computation for the relocations as well as the relaxation from Initial Exec to Local Exec.

Initial Exec:
```
pld r9, x@got@tprel@pcrel
add r9, r9, x@tls@pcrel
```
or
```
pld r9, x@got@tprel@pcrel
lbzx r10, r9, x@tls@pcrel
```
Note that @tls@pcrel is actually encoded as R_PPC64_TLS with a one byte displacement.

For the above examples relaxing Intitial Exec to Local Exec:
```
paddi r9, r9, x@tprel
nop
```
or
```
paddi r9, r13, x@tprel
lbz r10, 0(r9)
```

Reviewed By: nemanjai, MaskRay, #powerpc

Differential Revision: https://reviews.llvm.org/D86893
2020-09-22 05:48:43 -05:00
Fangrui Song 6d637fa560 [ELF][test] Delete large temporary files and make some temporary files smaller with two text segments
Large files are cumbersome on some filesystems and can more easily trigger ENOSPC.

Some tests use two text sections with output section addresses to test branch ranges.
Use two text segments to prevent LLD from filling the gap and unnecessarily increasing the output size.

With this change, there is no test/ELF temporary file larger than 100MiB.

Reviewed By: psmith

Differential Revision: https://reviews.llvm.org/D88037
2020-09-21 12:09:17 -07:00
Edd Dawson 0a6860521e [LLD][ELF][test] Fix CHECKs in map-file test
A repeated typo in lld/test/ELF/map-file.s prevented a number of checks from being executed.

CHECk-NEXT -> CHECK-NEXT
    ^             ^

After correcting the typo, a small adjustment was needed to match the size of the synthetic .comment section (which always contains "LLD 1.0" in the test environment).

Differential revision: https://reviews.llvm.org/D88023
2020-09-21 18:38:19 +03:00
James Henderson fa6da90aef [lld][ELF][test] Add additional LTO testing
The additional testing is testing we previously had in a downstream test
suite.

Reviewed by: grimar, MaskRay

Differential Revision: https://reviews.llvm.org/D87824
2020-09-21 10:18:09 +01:00
Jez Ng abd70fb398 [lld-macho] Export trie addresses should be relative to the image base
We didn't notice this earlier this we were only testing the export trie
encoded in a dylib, whose image base starts at zero. But a regular
executable contains `__PAGEZERO`, which means it has a non-zero image
base. This bug was discovered after attempting to run some programs that
performed `dlopen` on an executable.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D87780
2020-09-20 20:43:15 -07:00
Jez Ng 0a7e56f74c [lld-macho] Mark weak symbols in symbol table
Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86908
2020-09-20 20:43:14 -07:00
Greg McGary cba45514fb align __TEXT,__unwind_info to 8 byte boundary 2020-09-19 12:43:30 -07:00
Nico Weber e22a4fd59d lld/mach-o: Make tool scripts from 2124ca1d5c py2.7-compatible 2020-09-19 09:17:02 -04:00
Greg McGary 2124ca1d5c [lld-macho] create __TEXT,__unwind_info from __LD,__compact_unwind
Digest the input `__LD,__compact_unwind` and produce the output `__TEXT,__unwind_info`. This is the initial commit with the major functionality.

Successor commits will add handling for ...
* `__TEXT,__eh_frame`
* personalities & LSDA
* `-r` pass-through

Differential Revision: https://reviews.llvm.org/D86805
2020-09-18 22:01:03 -07:00
Fangrui Song 51b75b87db [lld][WebAssembly] Fix -Wunused-variable after D87663 2020-09-18 16:10:39 -07:00
Reid Kleckner 1e5b7e91aa [PDB] Split TypeServerSource and extend type index map lifetime
Extending the lifetime of these type index mappings does increase memory
usage (+2% in my case), but it decouples type merging from symbol
merging. This is a pre-requisite for two changes that I have in mind:
- parallel type merging: speeds up slow type merging
- defered symbol merging: avoid heap allocating (relocating) all symbols

This eliminates CVIndexMap and moves its data into TpiSource. The maps
are also split into a SmallVector and ArrayRef component, so that the
ipiMap can alias the tpiMap for /Z7 object files, and so that both maps
can simply alias the PDB type server maps for /Zi files.

Splitting TypeServerSource establishes that all input types to be merged
can be identified with two 32-bit indices:
- The index of the TpiSource object
- The type index of the record
This is useful, because this information can be stored in a single
64-bit atomic word to enable concurrent hashtable insertion.

One last change is that now all object files with debugChunks get a
TpiSource, even if they have no type info. This avoids some null checks
and special cases.

Differential Revision: https://reviews.llvm.org/D87736
2020-09-17 11:53:10 -07:00
Jianzhou Zhao 11201315d5 Flush bitcode incrementally for LTO output
Bitcode writer does not flush buffer until the end by default. This is
fine to small bitcode files. When -flto,--plugin-opt=emit-llvm,-gmlt are
used, the final bitcode file is large, for example, >8G. Keeping all
data in memory consumes a lot of memory.

This change allows bitcode writer flush data to disk early when buffered
data size is above some threshold. This is only enabled when lld emits
LLVM bitcode.

One issue to address is backpatching bitcode: subblock length, function
body indexes, meta data indexes need to backfill. If buffer can be
flushed partially, we introduced raw_fd_stream that supports
read/seek/write, and enables backpatching bitcode flushed in disk.

Reviewed-by: tejohnson, MaskRay

Differential Revision: https://reviews.llvm.org/D86905
2020-09-17 03:32:31 +00:00
Fangrui Song 15f0ad2fa2 [ELF] Bump the limit of thunk creation passes from 10 to 15
I have noticed that a 374MiB powerpc64le 'ld.lld' requires 11 passes to link.
There is a ThunkSection (whose parent OutputSection is ".text" of 169MiB) with 12867 thunks.
2020-09-16 14:05:22 -07:00
Andrew Ng 77152a6b7a [LLD][ELF] Optimize linker script filename glob pattern matching NFC
Optimize the filename glob pattern matching in
LinkerScript::computeInputSections() and LinkerScript::shouldKeep().

Add InputFile::getNameForScript() which gets and if required caches the
Inputfile's name used for linker script matching. This avoids the
overhead of name creation that was in getFilename() in LinkerScript.cpp.

Add InputSectionDescription::matchesFile() and
SectionPattern::excludesFile() which perform the glob pattern matching
for an InputFile and make use of a cache of the previous result. As both
computeInputSections() and shouldKeep() process sections in order and
the sections of the same InputFile are contiguous, these single entry
caches can significantly speed up performance for more complex glob
patterns.

These changes have been seen to reduce link time with --gc-sections by
up to ~40% with linker scripts that contain KEEP filename glob patterns
such as "*crtbegin*.o".

Differential Revision: https://reviews.llvm.org/D87469
2020-09-16 10:26:11 +01:00
Reid Kleckner 1b88845ce1 [PDB] Drop LF_PRECOMP from debugTypes earlier
This is a minor simplification to avoid firing up a BinaryStreamReader
and CVType parser.
2020-09-15 18:50:37 -07:00
Petr Hosek 9c73e55510 Revert "[DebugInfo] Remove dots from getFilenameByIndex return value"
This is failing on Windows bots due to path separator normalization.

This reverts commit 042c235068.
2020-09-15 10:06:47 -07:00
Stefan Pintilie 65f6810d3a [LLD][PowerPC] Add support for R_PPC64_TPREL34 used in TLS Local Exec
Add Thread Local Storage Local Exec support to LLD. This is to support PC Relative addressing of Local Exec.
The patch teaches LLD to handle:
```
paddi r9, r13, x1@tprel
```
The relocation is:
```
R_PPC_TPREL34
```

Reviewed By: NeHuang, MaskRay

Differential Revision: https://reviews.llvm.org/D86608
2020-09-15 09:06:19 -05:00
Sam Clegg 3f411e9773 [lld][WebAssembly] Fix --export-all when __stack_pointer is present
With https://reviews.llvm.org/D87537 we made it an error
to import or export a mutable global with the +mutable-globals
feature present.  However the scan was of the entire symbol
table rather than just the imports or exports and the filter
didn't match exaclyt meaning the `__stack_pointer` (a mutable
global) was always triggering with error when the `--export-all`
flag was used.

This also revealed that we didn't have any test coverage for
the `--export-all` flag.

This change fixes the current breakage on the emscripten-releases
roller.

Differential Revision: https://reviews.llvm.org/D87663
2020-09-15 06:17:01 -07:00
Petr Hosek 58938b544b [NFC][DebugInfo] Use consistent regex group spelling
This is a follow up to c1f2fb5184.
2020-09-15 01:49:42 -07:00
Georgii Rymar 4845531fa8 [lib/Object] - Refine interface of ELFFile<ELFT>. NFCI.
`ELFFile<ELFT>` has many methods that take pointers,
though they assume that arguments are never null and
hence could take references instead.

This patch performs such clean-up.

Differential revision: https://reviews.llvm.org/D87385
2020-09-15 11:38:31 +03:00
Petr Hosek c1f2fb5184 [DebugInfo] Support both forward and backward slashes in tests
This addresses test failure revealed by 042c235068.
2020-09-15 00:59:58 -07:00
Mateusz Mikuła 61e0b2b4c5 [LLD] Allow configuring default ld.lld backend
The motivation for this is ld.lld --help targeting MinGW which
currently prints help for the ELF backend unless -m i386pe{,p} is
added. This confuses build systems that grep through linker help to
find supported flags.

This matches LD from Binutils which always prints help for MinGW
when configured to target it.

After this change, the backend can still be overridden to any
supported ELF/MinGW target by using correct -m <arch>.

Differential Revision: https://reviews.llvm.org/D87418
2020-09-15 08:50:02 +03:00
Sam Clegg 2c12b056be [lld][WebAssembly] Allow globals imports via import_name/import_module
This feature already exists but was limited to function
symbols.

Differential Revision: https://reviews.llvm.org/D87666
2020-09-14 20:35:03 -07:00
Fangrui Song f6f34024e9 [ELF] Add documentation for --warn-backrefs: a GNU ld compatibility checking tool (and lesser of layering detection)
Differential Revision: https://reviews.llvm.org/D86762
2020-09-14 12:31:22 -07:00
Fangrui Song 94921e9f8a [ELF] Define a reportRangeError() overload for thunks and tidy up recent PPC64 thunk range errors
Prefer `errorOrWarn` to `fatal` for recoverable errors and graceful degradation
when --noinhibit-exec is specified.

Mention the destination symbol, otherwise the diagnostic is not really actionable.
Two errors are not tested but the patch does not intend to add the coverage.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D87486
2020-09-14 09:55:59 -07:00
Sam Clegg cc2da5554b [lld][WebAssembly] Add initial support for -Map/--print-map
Differential Revision: https://reviews.llvm.org/D77187
2020-09-12 16:10:51 -07:00
Sam Clegg 04febd30a8 [lld][WebAssembly] Error on import/export of mutable global without `mutable-globals` feature
Also add the +mutable-globals features in clang when
building with `-fPIC` since the linker will generate mutable
globals imports and exports in that case.

Differential Revision: https://reviews.llvm.org/D87537
2020-09-12 14:28:14 -07:00
Sam Clegg e3e3d6eecf [lld][WebAssembly] Convert a objyaml-using test to assembly
Differential Revision: https://reviews.llvm.org/D87536
2020-09-11 14:48:37 -07:00
Sam Clegg fa2a8acc71 [WebAssembly] Add assembly syntax for mutable globals
This adds and optional ", immutable" to the end of a `.globaltype`
declaration.  I would have prefered to match the `.wat` syntax
where immutable is the default and `mut` is the signifier for
mutable globals.  Sadly changing the default would break backwards
compat with existing assembly in the wild so I think its best
to stick with this approach.

Differential Revision: https://reviews.llvm.org/D87515
2020-09-11 11:11:02 -07:00
Fangrui Song 560188ddcc [ELF][PowerPC] Define NOP as 0x60000000 to tidy up code. NFC
Reviewed By: nemanjai

Differential Revision: https://reviews.llvm.org/D87483
2020-09-11 09:20:24 -07:00
Fangrui Song 485f3f35cc [ELF] Make two PPC64.cpp variables constexpr. NFC
Why are they mutable? :)
2020-09-10 14:31:10 -07:00
Petr Hosek f7941d9809 [lit] Use correct variable name for libxml2
This addresses an issue introduced in c4d7536136.
2020-09-09 22:04:25 -07:00
Petr Hosek c4d7536136 [CMake] Simplify CMake handling for libxml2
This matches the changes made to handling of zlib done in 10b1b4a
where we rely on find_package and the imported target rather than
manually appending the library and include paths. The use of
LLVM_LIBXML2_ENABLED has been replaced by LLVM_ENABLE_LIBXML2
thus reducing the number of variables.

Differential Revision: https://reviews.llvm.org/D84563
2020-09-09 21:44:44 -07:00
Andrew Ng 863aa0a37b [LLD][ELF] Fix performance of MarkLive::scanEhFrameSection
MarkLive::scanEhFrameSection is used to retain personality/LSDA
functions when --gc-sections is enabled.

Improve its performance by only iterating over the .eh_frame relocations
that need to be resolved for an EhSectionPiece. This optimization makes
the same assumption as elsewhere in LLD that the .eh_frame relocations
are sorted by r_offset.

This appears to be a performance regression introduced in commit
e6c24299d2 (https://reviews.llvm.org/D59800).

This change has been seen to reduce link time by up to ~50%.

Differential Revision: https://reviews.llvm.org/D87245
2020-09-08 19:32:34 +01:00
Raul Tambre 86bd8f82cc [CMake] Remove dead FindPythonInterp code
LLVM has bumped the minimum required CMake version to 3.13.4, so this has become dead code.

Reviewed By: #libc, ldionne

Differential Revision: https://reviews.llvm.org/D87189
2020-09-08 15:23:23 +03:00
Fangrui Song e59d9df774 [ELF] --symbol-ordering-file: optimize a loop 2020-09-07 21:47:30 -07:00
Jessica Clarke bef38e86b4 [ELF] Handle SHT_RISCV_ATTRIBUTES similarly to SHT_ARM_ATTRIBUTES
Currently we treat SHT_RISCV_ATTRIBUTES like a normal section and
concatenate all such input sections, yielding invalid output unless only
a single attributes section is present in the input. Instead, pick the
first as with SHT_ARM_ATTRIBUTES. We do not currently need to condition
our behaviour on the contents, unlike Arm. In future, we should both do
stricter validation of the input and merge all sections together to
ensure we have, for example, the full arch string requirement, but this
rudimentary implementation is good enough for most common cases.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D86309
2020-09-05 18:36:23 +01:00
Daniel Sanders 8760048384 [lld] Test corrections after 3f1a9b7eca added segment names to objdump output 2020-09-04 10:26:29 -07:00
Victor Huang db367ff2d8 [LLD][PowerPC][test] Fix out-of-memory issue running ppc64-pcrel-long-branch-error.s
Following 97febb1, fix the out-of-memory error associated with buffering the output
in-memory by writing to an allocated file with the minimum offset and running it
on ppc system-linux only.

Peer reviewed by: nemanjai
2020-09-03 09:08:24 -05:00
Greg McGary f434f7dabc Add code owners of new MachO port 2020-09-02 19:32:12 -07:00
Martin Storsjö a54919e0c1 [LLD] [COFF] Error out if creating a DLL with too many exported symbols
The PE/DLL format has a limit on 64k exported symbols per DLL; make
sure to check this.

Differential Revision: https://reviews.llvm.org/D86701
2020-08-31 21:15:13 +03:00
Hubert Tong 97febb139d [LLD][PowerPC][test] Disable ELF/ppc64-pcrel-long-branch-error.s
Following 0becc27ebf, `ppc64-pcrel-long-branch-error.s` fails in some
environments with out-of-memory errors associated with buffering the
output in-memory. Since the alternative of writing to an allocated file
is also known to cause problems, we will disable the test
unconditionally (pending a mechanism to disable the test selectively).
2020-08-29 00:08:52 -04:00
Victor Huang 0becc27ebf [LLD][PowerPC] Remove redundant file write out in the test cases
Fix the time out test failure on lld-x86_64-freebsd build bot.

Peer reviewed by: stefanp, nemanjai
2020-08-28 15:09:17 -05:00
Victor Huang bfc7636612 [LLD][PowerPC] Add a pc-rel based long branch thunk
In this patch, a pc-rel based long branch thunk is added for the local
call protocol that caller and callee does not use TOC.

Reviewed By: sfertile, nemanjai

Differential Revision: https://reviews.llvm.org/D86706
2020-08-28 10:40:48 -05:00
Jez Ng ae8fa1d8a6 [lld-macho][NFC] Define isHidden() in LinkEditSection
Since it's always true

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86749
2020-08-27 17:44:18 -07:00
Jez Ng ccbacdded4 [lld-macho] Weak locals should be relaxed too
Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86746
2020-08-27 17:44:17 -07:00
Jez Ng 0407197711 [lld-macho] Support GOT relocations to __dso_handle
Found such a relocation while testing some real world programs.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86642
2020-08-27 17:44:17 -07:00
Jez Ng 7083363c05 [lld-macho] Implement GOT_LOAD relaxation
We can have GOT_LOAD relocations that reference `__dso_handle`.
However, our binding opcode encoder doesn't support binding to the DSOHandle
symbol. Instead of adding support for that, I decided it would be cleaner to
implement GOT_LOAD relaxation since `__dso_handle`'s location is always
statically known.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86641
2020-08-27 17:44:17 -07:00
Jez Ng 2a38dba7dd [lld-macho] Emit binding opcodes for defined symbols that override weak dysyms
These opcodes tell dyld to coalesce the overridden weak dysyms to this
particular symbol definition.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86575
2020-08-27 17:44:16 -07:00
Jez Ng 3da2130e45 [lld-macho] Emit the right header flags for weak bindings/symbols
Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86574
2020-08-27 17:44:16 -07:00
Jez Ng e263287c79 [lld-macho] Implement weak binding for branch relocations
Since there is no "weak lazy" lookup, function calls to weak symbols are
always non-lazily bound. We emit both regular non-lazy bindings as well
as weak bindings, in order that the weak bindings may overwrite the
non-lazy bindings if an appropriate symbol is found at runtime. However,
the bound addresses will still be written (non-lazily) into the
LazyPointerSection.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D86573
2020-08-27 17:44:15 -07:00
Jez Ng d2b845dd6c [lld-macho] Disable invalid/stub-link.s test for Mac
It seems to be failing on some Google Buildbots.

This diff also includes a minor fix for the install name of one of
libSystem's re-exports. I don't think it's the cause of the test
failure, though. The wrong install name just meant that the symbol
lookup failure would still happen, but it would have been caused by the
re-export not being found, instead of the arch failing to match.

Differential Revision: https://reviews.llvm.org/D86728
2020-08-27 11:24:22 -07:00
Martin Storsjö df8f3bf626 [LLD] [COFF] Check the aux section definition size for IMAGE_COMDAT_SELECT_SAME_SIZE
Binutils generated sections seem to be padded to a multiple of 16 bytes,
but the aux section definition contains the original, unpadded section
length.

The size check used for IMAGE_COMDAT_SELECT_SAME_SIZE previously
only checked the size of the section itself. When checking the
currently processed object file against the previously chosen
comdat section, we easily have access to the aux section definition
of the currently processed section, but we have to iterate over the
symbols of the previously selected object file to find the section
definition of the previously picked section. (We don't want to
inflate SectionChunk to carry more data, for something that is only
needed in corner cases.) Only do this when the mingw flag is set.

This fixes statically linking clang-built C++ object files against
libstdc++ built with GCC, if the object files contain e.g. typeinfo.

Differential Revision: https://reviews.llvm.org/D86659
2020-08-27 15:08:57 +03:00
Martin Storsjö e72403f96d [LLD] [MinGW] Enable dynamicbase by default
This matches lld-link's own default.

Add a new command line option --no-dynamicbase for disabling it.
(Unfortunately, GNU ld doesn't yet have a matching --no-dynamicbase
option, as that's the default there.)

Differential Revision: https://reviews.llvm.org/D86654
2020-08-27 15:08:53 +03:00
Andrew Ng d4e2e2852a [ELF][test] Add test coverage of TLS to gc-sections.s
Differential Revision: https://reviews.llvm.org/D86639
2020-08-27 12:28:51 +01:00
James Henderson d2385f6d2f [lld][ELF][test] Expand testing of symbols in mergeable sections
Whilst reviewing some internal testing, I noticed a couple of holes in
coverage of mergeable sections containing symbols. This patch addresses
these holes:
1) Show that mid-piece symbols have their values updated properly when
   pieces are merged.
2) Show the behaviour of symbols in mergeable pieces when --gc-sections
   is enabled.

Reviewed by: grimar, MaskRay

Differential Revision: https://reviews.llvm.org/D86543
2020-08-27 10:38:42 +01:00
Jez Ng eec3500e50 [lld-macho] Fix objc.s test
Summary: It was passing on my local machine due to previously-written
files cached in the test output folder.
2020-08-26 22:59:31 -07:00
Jez Ng 62b39b3a0c [lld-macho] Implement -all_load
Differential Revision: https://reviews.llvm.org/D86640
2020-08-26 19:21:17 -07:00
Jez Ng cbe27316ef [lld-macho] Implement weak bindings for GOT/TLV
Previously, we were only emitting regular bindings to weak
dynamic symbols; this diff adds support for the weak bindings too, which
can overwrite the regular bindings at runtime. We also treat weak
defined global symbols similarly -- since they can also be interposed at
runtime, they need to be treated as potentially dynamic symbols.

Note that weak bindings differ from regular bindings in that they do not
specify the dylib to do the lookup in (i.e. weak symbol lookup happens
in a flat namespace.)

Differential Revision: https://reviews.llvm.org/D86572
2020-08-26 19:21:09 -07:00
Jez Ng b84d72d893 [lld-macho][NFC] Handle GOT bindings and regular bindings more uniformly
Previously, the BindingEntry struct could only store bindings to offsets
within InputSections. Since the GOTSection and TLVPointerSections are
OutputSections, I handled those in a separate code path. However, this
makes it awkward to support weak bindings properly without code
duplication. This diff allows BindingEntries to point directly to
OutputSections, simplifying the upcoming weak binding implementation.

Along the way, I also converted a bunch of functions taking references
to symbols to take pointers instead. Given how much casting we do for
Symbol (especially in the upcoming weak binding diffs), it's cleaner
this way.

Differential Revision: https://reviews.llvm.org/D86571
2020-08-26 19:21:04 -07:00
Jez Ng cf918c809b [lld-macho] Implement -ObjC
It's roughly like -force_load with some filtering.

Differential Revision: https://reviews.llvm.org/D86181
2020-08-26 19:20:55 -07:00
Jez Ng 7394460d87 [lld-macho] Handle TAPI and regular re-exports uniformly
The re-exports list in a TAPI document can either refer to other inlined
TAPI documents, or to on-disk files (which may themselves be TBD or
regular files.) Similarly, the re-exports of a regular dylib can refer
to a TBD file.

Differential Revision: https://reviews.llvm.org/D85404
2020-08-26 19:20:48 -07:00
Jez Ng 6336c042f6 [lld-macho] Make it possible to re-export .tbd files
Two things needed fixing for that to work:

1. getName() no longer returns null for DylibFiles constructed from TAPIs
2. markSubLibrary() now accepts .tbd as a possible extension

Differential Revision: https://reviews.llvm.org/D86180
2020-08-26 19:20:42 -07:00
Jez Ng 3e7a86e366 [lld-macho] Fall back to raw path if we don't find anything under syslibroot
This matches ld64's behavior

Differential Revision: https://reviews.llvm.org/D85992
2020-08-26 19:20:35 -07:00
Mateusz Mikuła c82078b5d7 [LLD][MinGW] Handle allow-multiple-definition flag
Basically copied from ELF driver.

Differential Revision: https://reviews.llvm.org/D86512
2020-08-26 09:38:11 +03:00
Mateusz Mikuła dcb1ce61b8 [LLD][MinGW] Cleanup Options.td file. NFC.
Based on ELF driver Options.td.

Differential Revision: https://reviews.llvm.org/D86509
2020-08-26 09:38:11 +03:00