Commit Graph

6453 Commits

Author SHA1 Message Date
Jez Ng
0f8224c210 [lld-macho][nfc] Remove %T from headerpad.s
The llvm-lit docs indicate that it is deprecated.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93605
2020-12-21 14:44:08 -05:00
Jez Ng
a817594de9 [lld-macho][nfc] Clean up tests
* Migrate most of our tests to use `split-file` instead of `echo`
* Remove individual `rm -f %t/libfoo.a` commands in favor of a top-level `rm -rf %t`
* Remove unused `Inputs/libfunction.s`

Reviewed By: #lld-macho, compnerd

Differential Revision: https://reviews.llvm.org/D93604
2020-12-21 14:44:08 -05:00
Fangrui Song
fb3c1b3de5 [ELF] Reject local-exec TLS relocations for -shared
For x86-64, D33100 added a diagnostic for local-exec TLS relocations referencing a preemptible symbol.

This patch generalizes it to non-preemptible symbols (see `-Bsymbolic` in `tls.s`)
on all targets.

Local-exec TLS relocations resolve to offsets relative to a fixed point within
the static TLS block, which are only meaningful for the executable.

With this change, `clang -fpic -shared -fuse-ld=bfd a.c` on the following example will be flagged for AArch64/ARM/i386/x86-64/RISC-V

```
static __attribute__((tls_model("local-exec"))) __thread long TlsVar = 42;
long bump() { return ++TlsVar; }
```

Note, in GNU ld, at least arm, riscv and x86's ports have the similar
diagnostics, but aarch64 and ppc64 do not error.

Differential Revision: https://reviews.llvm.org/D93331
2020-12-21 08:47:04 -08:00
Fangrui Song
e25afcfa51 [ELF][PPC64] Detect missing R_PPC64_TLSGD/R_PPC64_TLSLD and disable TLS relaxation
Alternative to D91611.

The TLS General Dynamic/Local Dynamic code sequences need to mark
`__tls_get_addr` with R_PPC64_TLSGD or R_PPC64_TLSLD, e.g.

```
addis r3, r2, x@got@tlsgd@ha # R_PPC64_GOT_TLSGD16_HA
addi r3, r3, x@got@tlsgd@l   # R_PPC64_GOT_TLSGD16_LO
bl __tls_get_addr(x@tlsgd)   # R_PPC64_TLSGD followed by R_PPC64_REL24
nop
```

However, there are two deviations form the above:

1. direct call to `__tls_get_addr`. This is essential to implement ld.so in glibc/musl/FreeBSD.

```
bl __tls_get_addr
nop
```

This is only used in a -shared link, and thus not subject to the GD/LD to IE/LE
relaxation issue below.

2. Missing R_PPC64_TLSGD/R_PPC64_TLSGD for compiler generated TLS references

According to Stefan Pintille, "In the early days of the transition from the
ELFv1 ABI that is used for big endian PowerPC Linux distributions to the ELFv2
ABI that is used for little endian PowerPC Linux distributions, there was some
ambiguity in the specification of the relocations for TLS. The GNU linker has
implemented support for correct handling of calls to __tls_get_addr with a
missing relocation.  Unfortunately, we didn't notice that the IBM XL compiler
did not handle TLS according to the updated ABI until we tried linking XL
compiled libraries with LLD."

In short, LLD needs to work around the old IBM XL compiler issue.
Otherwise, if the object file is linked in -no-pie or -pie mode,
the result will be incorrect because the 4 instructions are partially
rewritten (the latter 2 are not changed).

Work around the compiler bug by disable General Dynamic/Local Dynamic to
Initial Exec/Local Exec relaxation. Note, we also disable Initial Exec
to Local Exec relaxation for implementation simplicity, though technically it can be kept.

ppc64-tls-missing-gdld.s demonstrates the updated behavior.

Reviewed By: #powerpc, stefanp, grimar

Differential Revision: https://reviews.llvm.org/D92959
2020-12-21 08:45:41 -08:00
Jez Ng
64e4757200 [lld-macho] Have order files support filtering by archive member paths
Also remove iteration over ArchiveFile symbols in buildInputSectionPriorities --
that was rendered unnecessary after D92539, which included ObjFiles from
ArchiveFiles inside the `inputFiles` vector.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D93569
2020-12-20 13:49:18 -05:00
Jez Ng
5f9896d3b2 [lld-macho] Support Obj-C symbols in order files
Obj-C symbols may have spaces and colons, which our previous order file
parser would be confused by. The order file format has made the very unfortunate
choice of using colons for its delimiters, which means that we have to use
heuristics to determine if a given colon is part of a symbol or not...

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93567
2020-12-20 13:49:18 -05:00
Jez Ng
e2863357de [lld-macho][nfc] Use split-file in order file test
Makes it much easier to edit / maintain.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D93566
2020-12-20 13:49:18 -05:00
Greg McGary
99930719c6 Handle overflow beyond the 127 common encodings limit
The common encodings table holds only 127 entries. The encodings index for compact entries is 8 bits wide, and indexes 127..255 are stored locally to each second-level page. Prior to this diff, lld would `fatal()` if encodings overflowed the 127 limit.

This diff populates a per-second-level-page encodings table as needed. When the per-page encodings table hits its limit, we must terminate the page. If such early termination would consume fewer entries than a regular (non-compact) encoding page, then we prefer the regular format.

Caveat: one reason the common-encoding table might overflow is because of DWARF debug-info references, which are not yet implemented and will come with a later diff.

Differential Revision: https://reviews.llvm.org/D93267
2020-12-19 14:54:37 -08:00
Harald van Dijk
adc55b5a5a
[X86] Avoid generating invalid R_X86_64_GOTPCRELX relocations
We need to make sure not to emit R_X86_64_GOTPCRELX relocations for
instructions that use a REX prefix. If a REX prefix is present, we need to
instead use a R_X86_64_REX_GOTPCRELX relocation. The existing logic for
CALL64m, JMP64m, etc. already handles this by checking the HasREX parameter
and using it to determine which relocation type to use. Do this for all
instructions that can use relaxed relocations.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D93561
2020-12-18 23:38:38 +00:00
Georgii Rymar
8590b5ccd5 [libObject, llvm-readobj] - Reimplement ELFFile<ELFT>::getEntry.
Currently, `ELFFile<ELFT>::getEntry` does not check an index of
an entry. Because of that the code might read past the end of the symbol
table silently. I've added a test to `llvm-readobj\ELF\relocations.test`
to demonstrate the possible issue. Also, I've added a unit test for
this method.

After this change, `getEntry` stops reporting the section index and
reuses the `getSectionContentsAsArray` method, which already has
all the validation needed. Our related warnings now provide
more and better context sometimes.

Differential revision: https://reviews.llvm.org/D93209
2020-12-18 16:52:27 +03:00
Greg McGary
cc1cf6332a [lld-macho] Implement option: -undefined TREATMENT
TREATMENT can be `error`, `warning`, `suppress`, or `dynamic_lookup`
The `dymanic_lookup` remains unimplemented for now.

Differential Revision: https://reviews.llvm.org/D93263
2020-12-17 17:40:50 -08:00
Amy Huang
7e13694ac7 [llvm-symbolizer][Windows] Add start line when searching in line table sections.
Fixes issue where if a line section doesn't start with a line number
then the addresses at the beginning of the section don't have line numbers.

For example, for a line section like this
```
  0001:00000010-00000014, line/column/addr entries = 1
     7 00000013 !
```
a line number wouldn't be found for addresses from 10 to 12.

This matches behavior when using the DIA SDK.

Differential Revision: https://reviews.llvm.org/D93306
2020-12-17 07:57:36 -08:00
Adhemerval Zanella
978eb3b87b [lld] [ELF] AArch64: Handle DT_AARCH64_VARIANT_PCS
As indicated by AArch64 ELF specification, symbols with st_other
marked with STO_AARCH64_VARIANT_PCS indicates it may follow a variant
procedure call standard with different register usage convention
(for instance SVE calls).

Static linkers must preserve the marking and propagate it to the dynamic
symbol table if any reference or definition of the symbol is marked with
STO_AARCH64_VARIANT_PCS, and add a DT_AARCH64_VARIANT_PCS dynamic tag if
there are R_<CLS>_JUMP_SLOT relocations that reference that symbols.

It implements https://bugs.llvm.org/show_bug.cgi?id=48368.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D93045
2020-12-17 11:09:55 -03:00
Jez Ng
4c8276cdc1 [lld-macho] Use LC_LOAD_WEAK_DYLIB for dylibs with only weakrefs
Note that dylibs without *any* refs will still be loaded in the usual
(strong) fashion.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93435
2020-12-17 08:49:17 -05:00
Jez Ng
811444d7a1 [lld-macho] Add support for weak references
Weak references need not necessarily be satisfied at runtime (but they must
still be satisfied at link time). So symbol resolution still works as per usual,
but we now pass around a flag -- ultimately emitting it in the bind table -- to
indicate if a given dylib symbol is a weak reference.

ld64's behavior for symbols that have both weak and strong references is
a bit bizarre. For non-function symbols, it will emit a weak import. For
function symbols (those referenced by BRANCH relocs), it will emit a
regular import. I'm not sure what value there is in that behavior, and
since emulating it will make our implementation more complex, I've
decided to treat regular weakrefs like function symbol ones for now.

Fixes PR48511.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93369
2020-12-17 08:49:16 -05:00
Fangrui Song
66bcbdbc9c [AArch64InstPrinter] Change printADRPLabel to print the target address in hexadecimal form
Similar to D77853. Change ADRP to print the target address in hex, instead of the raw immediate.
The behavior is similar to GNU objdump but we also include `0x`.

Note: GNU objdump is not consistent whether or not to emit `0x` for different architectures. We try emitting 0x consistently for all targets.

```
GNU objdump:       adrp x16, 10000000
Old llvm-objdump:  adrp x16, #0
New llvm-objdump:  adrp x16, 0x10000000
```

`adrp Xd, 0x...` assembles to a relocation referencing `*ABS*+0x10000` which is not intended. We need to use a linker or use yaml2obj.
The main test is `test/tools/llvm-objdump/ELF/AArch64/pcrel-address.yaml`

Differential Revision: https://reviews.llvm.org/D93241
2020-12-16 09:20:55 -08:00
Fangrui Song
16cb7910f5 [ELF] --emit-relocs: fix a crash if .rela.dyn is an empty output section
Fix PR48357: If .rela.dyn appears as an output section description, its type may
be SHT_RELA (due to the empty synthetic .rela.plt) while there is no input
section. The empty .rela.dyn may be retained due to a reference in a linker
script. Don't crash.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D93367
2020-12-16 08:59:38 -08:00
Nico Weber
abc08d5ec7 [mac/lld] fix amend mishap from ec88746a05 2020-12-15 19:41:00 -05:00
Nico Weber
ec88746a05 [lld/mac] fill in current and compatibility version for LC_LOAD_(WEAK_)DYLIB
Not sure if anything actually depends on this, but it makes `otool -L`
output look nicer.

Differential Revision: https://reviews.llvm.org/D93332
2020-12-15 19:34:59 -05:00
Nico Weber
09edd9df6e [mac/lld] simplify code using PackedVersion instead of VersionTuple
PackedVersion already does the correct range checks.

No behavior change.

Differential Revision: https://reviews.llvm.org/D93338
2020-12-15 19:23:07 -05:00
Jez Ng
3aa8e071dd [lld-macho] Add implicit dylib support for frameworks
{D93000} applied to frameworks. Partial fix for PR48511.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93277
2020-12-15 15:58:26 -05:00
Jez Ng
8a5e068823 [lld-macho] Support -sub_umbrella
From what I can tell, it's essentially identical to
`-sub_library`, but it doesn't match files ending in ".dylib".

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93276
2020-12-15 15:58:26 -05:00
Jez Ng
3184519909 [lld-macho] Don't emit rebase opcodes for relocs in TLV sections
Their addresses are already encoded as section-relative offsets, so
there's no need to rebase them at runtime. {D85080} has some context
on the weirdness of TLV sections.

Fixes llvm.org/PR48491.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D93257
2020-12-15 15:58:26 -05:00
Jez Ng
544148ae70 [lld-macho] -weak_{library,framework} should always take priority
We were not setting forceWeakImport for file paths given by
`-weak_library` if we had already loaded the file. This diff fixes that
by having `loadDylib` return a cached DylibFile instance even if we have
already loaded that file.

We still avoid emitting multiple LC_LOAD_DYLIBs, but we achieve this by
making inputFiles a SetVector instead of relying on the `loadedDylibs`
cache.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D93255
2020-12-15 15:58:26 -05:00
Nico Weber
601f0fb846 [lld/mac] Set ordinal on dynamic undefined symbols in symbol table
This lets `nm -m` print "(from libfoo)" in its output, which is more
accessible than dumping the bind table.

See https://reviews.llvm.org/D57190#2455761 for the somewhat
surprising `AltEntry` that appears in symtab.s.

Differential Revision: https://reviews.llvm.org/D93318
2020-12-15 14:39:36 -05:00
Fangrui Song
c8da71b53f [ELF] Error for out-of-range R_X86_64_[REX_]GOTPCRELX
Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D93259
2020-12-15 09:20:07 -08:00
Nico Weber
d058b69b1c [lld/mac] implement -compatibility_version, -current_version
Differential Revision: https://reviews.llvm.org/D93237
2020-12-14 18:41:36 -05:00
LemonBoy
92c6141ce6 lld/ELF: Parse MSP430 BFD/emulation names
Follow the naming set by TI's own GCC-based toolchain.
Also, force the `osabi` field to `ELFOSABI_STANDALONE`, this matches GNU LD's output (the patching is done in `elf32_msp430_post_process_headers`).

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D92931
2020-12-14 09:38:12 -08:00
Georgii Rymar
98a4289810 [llvm-readobj] - For SHT_REL relocations, don't display an addend.
This is https://bugs.llvm.org/show_bug.cgi?id=44257.

In LLVM style we always print `0` as addend when dumping
SHT_REL relocations. It is confusing, this patch stops
printing it as the first comment on the bug page suggests.

Differential revision: https://reviews.llvm.org/D93033
2020-12-14 12:03:00 +03:00
Fangrui Song
5d1c723b73 [ELF][test] Rewrite st_value=0 copy relocation tests
The original tests have unneeded symbols and copy-relocation-zero-abs-addr.s
does not actually test anything.

Rewrite them and add copy-relocation-zero-addr.s instead.

Add --soname=b so that the address 0x203400 will be stable.  (When linking an
executable with %t.so, the path %t.so will be recorded in the DT_NEEDED entry if
%t.so doesn't have DT_SONAME. .dynstr will have varying lengths on different
systems.)
2020-12-12 16:50:25 -08:00
Jinsong Ji
9b05f1889a [lld][PowerPC][test] Avoid flaky failures
This test may fail if there is a new changes to this tests.

The archives are not deleted so the contents from the previous test run
may affect the contents for the current run,
so this will require cleaning up the Output dir or force build of buildbot.

The fix is to put all the objects in the temporary dir that we cleanup every run,
 to avoid run-2-run flaky failures.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D93128
2020-12-11 19:46:51 +00:00
Jez Ng
349d5c9cf7 [lld-macho] Disable some tests that are failing on Windows
lto-object-path.ll, like stabs.s, is disabled on Windows as the path
separators make it difficult to write a test that works across
platforms.

This diff also disables implicit-dylibs.s on Windows as we seem to emit
LC_LOAD_DYLIBs in a different order on that platform. This seems like a
bug in LLD that needs to be addressed (in a future diff).
2020-12-10 17:54:23 -08:00
Jez Ng
553284be2d [lld-macho] Don't include absolute address value in expected test output
Should fix the mattrs.ll failure introduced by rG29d3b0e47113.
2020-12-10 17:54:23 -08:00
Derek Schuff
dd6412c05c [WebAssembly][lld] Exclude COMDAT sections
Allow exclusion/discarding of custom sections with COMDAT groups.
It piggybacks on the existing COMDAT-handling code, but applies to custom sections as well.

Differential Revision: https://reviews.llvm.org/D92950
2020-12-10 17:47:41 -08:00
Sam Clegg
e52881a287 [lld][WebAssembly] Split __wasm_apply_relocs function in two
We have two types of relocations that we apply on startup:
1. Relocations that apply to wasm globals
2. Relocations that apply to wasm memory

The first set of relocations use only the `__memory_base` import to
update a set of internal globals.  Because wasm globals are thread local
these need to run on each thread.  Memory relocations, like static
constructors, must only be run once.

To ensure global relocations run on all threads and because the only
depend on the immutable `__memory_base` import we can run them during
the WebAssembly start functions, instead of waiting until the
post-instantiation __wasm_call_ctors.

Differential Revision: https://reviews.llvm.org/D93066
2020-12-10 17:07:39 -08:00
Nico Weber
5dad062d7e fix typo to cycle bots 2020-12-10 19:20:09 -05:00
Jez Ng
76c36c11a9 [lld-macho] Don't load dylibs more than once
Also remove `DylibFile::reexported` since it's unused.

Fixes llvm.org/PR48393.

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D93001
2020-12-10 15:57:52 -08:00
Jez Ng
6a348f6158 [lld-macho] Implement -no_implicit_dylibs
Dylibs that are "public" -- i.e. top-level system libraries -- are considered
implicitly linked when another library re-exports them. That is, we should load
them & bind directly to their symbols instead of via their re-exporting
umbrella library. This diff implements that behavior by default, as well as an
opt-out flag.

In theory, this is just a performance optimization, but in practice it seems
that it's needed for correctness.

Fixes llvm.org/PR48395.

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D93000
2020-12-10 15:57:52 -08:00
Jez Ng
74d799926e [lld-macho] Initialize AsmParsers earlier
We need to initialize AsmParsers before any calls to `addFile`, as
bitcode files may require them. Otherwise we trigger `Assertion T &&
T->hasMCAsmParser()' failed`.

Reviewed By: #lld-macho, compnerd

Differential Revision: https://reviews.llvm.org/D92913
2020-12-10 15:57:52 -08:00
Jez Ng
29d3b0e471 [lld-macho] Add support for -mcpu, -mattr, -code-model in LTO
`-mcpu` and `-code-model` tests were copied from similar ones in
LLD-ELF.

There doesn't seem to be an equivalent test for `-mattr` in LLD-ELF, so
I've verified our behavior by cribbing a test from
CodeGen/X86/recip-fastmath.ll.

Reviewed By: #lld-macho, compnerd, MaskRay

Differential Revision: https://reviews.llvm.org/D92912
2020-12-10 15:57:51 -08:00
Jez Ng
863f7a745e [lld-macho] Don't attempt to emit rebase opcodes for debug sections
This was causing a crash as we were attempting to look up the
nonexistent parent OutputSection of the debug sections. We didn't detect
it earlier because there was no test for PIEs with debug info (PIEs
require us to emit rebases for X86_64_RELOC_UNSIGNED).

This diff filters out the debug sections while loading the ObjFiles. In
addition to fixing the above problem, it also lets us avoid doing
redundant work -- we no longer parse / apply relocations / attempt to
emit dyld opcodes for these sections that we don't emit.

Fixes llvm.org/PR48392.

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D92904
2020-12-10 15:57:51 -08:00
Jez Ng
95831a56d0 [lld-macho] Implement -object_path_lto
This makes it possible for STABS entries to reference the debug info
contained in the LTO-compiled output.

I'm not sure how to test the file mtime within llvm-lit -- GNU and BSD
`stat` take different command-line arguments. I've omitted the check for
now.

Reviewed By: clayborg

Differential Revision: https://reviews.llvm.org/D92537
2020-12-10 15:57:51 -08:00
Sam Clegg
199497086e [lld][WebAssembly] Delay creation of internal __wasm_memory_init function
This also allows for its creation to be conditional so it is completely
elided when not needed.

Differential Revision: https://reviews.llvm.org/D93035
2020-12-10 10:47:18 -08:00
Sam Clegg
d8ed639a6a [lld][WebAssembly] Don't emit names for data segments that we omit
Followup to https://reviews.llvm.org/D92909

Differential Revision: https://reviews.llvm.org/D92997
2020-12-09 20:59:26 -08:00
Fangrui Song
7d38861ce3 [ELF] Rename --[no-]lto-new-pass-manager to --[no-]lto-legacy-pass-manager
Normally we should not delete options. However, the Clang driver passes
`-plugin-opt={new,legacy}-pass-manager` instead of
`--[no-]lto-legacy-pass-manager` (`-plugin-opt=new-pass-manager` has been used
since 7.0), and it is unlikely anyone will use the `--lto-*` style options directly.

So let's rename them to be consistent with the Clang driver option names.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D92988
2020-12-09 17:53:37 -08:00
Fangrui Song
7adcacda06 Rename -plugin-opt=no-new-pass-manager to -plugin-opt=legacy-pass-manager 2020-12-09 16:43:30 -08:00
Fangrui Song
68ff3b3376 [LLD][gold] Add -plugin-opt=no-new-pass-manager
-DENABLE_EXPERIMENTAL_NEW_PASS_MANAGER=on configured LLD and LLVMgold.so
will use the new pass manager by default. Add an option to
use the legacy pass manager. This will also be used by the Clang driver
when -fno-new-pass-manager (D92915) / -fno-experimental-new-pass-manager is set.

Reviewed By: aeubanks, tejohnson

Differential Revision: https://reviews.llvm.org/D92916
2020-12-09 13:31:03 -08:00
Sam Clegg
9a72d3e3e4 [WebAssembly] Add support for named data sections in wasm binaries
Followup to https://reviews.llvm.org/D91769 which added support
for names globals.

Differential Revision: https://reviews.llvm.org/D92909
2020-12-09 12:57:07 -08:00
Fangrui Song
baef18dffb [ELF] Reorganize "is only supported on" tests and fix some diagnostics 2020-12-09 12:14:00 -08:00
Yvan Roux
03a77d04b4 [LLD][ELF] Fix typo in relocation-model-pic.ll
Should fix non-x86 bot failures.
2020-12-09 15:38:50 +01:00