Commit Graph

14687 Commits

Author SHA1 Message Date
Fangrui Song
ad45df91ad [ELF][PPC64] Remove unneeded PPC64PCRelLongBranchThunk
This reverts the PPC64PCRelLongBranchThunk part from D86706.
PPC64PCRelLongBranchThunk is the same as PPC64R12SetupStub.

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

Reviewed By: NeHuang, nemanjai

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

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

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

Reviewed By: jrtc27, peter.smith

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

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

Reviewed By: markj

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

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

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

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

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

Reviewed By: peter.smith, MaskRay

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

Also improve --help messages and the manpage.

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

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

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

The name "sectionCommands" is used in both LinkerScript::sectionCommands and
OutputSection::sectionCommands, which may lead to confusion.
"commands" in OutputSection has no ambiguity because there are no other types
of commands.
2021-11-25 16:47:07 -08:00
Fangrui Song
ff0d9e6cfa [ELF] Remove redundant part.dynSymTab creation. NFC 2021-11-25 14:42:22 -08:00
Fangrui Song
5ca54c6686 [ELF] Simplify GnuHashSection::write. NFC 2021-11-25 14:23:25 -08:00
Fangrui Song
55c14d6dbf [ELF] Simplify DynamicSection content computation. NFC
The new code computes the content twice, but avoides the tricky
std::function<uint64_t()>. Removed 13KiB code in a Release build.
2021-11-25 14:12:34 -08:00
Fangrui Song
6ca8fde226 [ELF] Emit DF_STATIC_TLS only for -shared
This matches GNU ld and saves 2 words for executables.
2021-11-24 23:17:13 -08:00
Fangrui Song
5922dd91f8 [ELF] Rename hasStaticTlsModel to hasTlsIe
and remove unneeded atomic.
2021-11-24 21:06:04 -08:00
Fangrui Song
371290dfd4 [ELF] Remove unneeded DF_STATIC_TLS for EM_386 local-exec TLS
which is also untested.
2021-11-24 20:43:58 -08:00
Igor Kudrin
8cdf1c1edb [ELF] Support the "read-only" memory region attribute
The attribute 'r' allows (or disallows for the negative case) read-only
sections, i.e. ones without the SHF_WRITE flag, to be assigned to the
memory region. Before the patch, lld could put a section in the wrong
region or fail with "error: no memory region specified for section".

Differential Revision: https://reviews.llvm.org/D113771
2021-11-24 12:17:09 +07:00
Fangrui Song
38ed1db7e8 [ELF] Support non-RAX/non-adjacent R_X86_64_GOTPC32_TLSDESC/R_X86_64_TLSDESC_CALL
The current TLSDESC optimization code assumes:
```
leaq x@tlsdesc(%rip), %rax
call *x@tlscall(%rax)       # adjacent
```

From https://gitlab.freedesktop.org/mesa/mesa/-/issues/5665 , it seems that the
two instructions may not be adjacent in GCC 10's output:
```
leaq x@tlsdesc(%rip), %rax
something else
call *x@tlscall(%rax)
```

This patch supports the case. While here, support non-RAX registers for
R_X86_64_GOTPC32_TLSDESC, in case the compiler generates inefficient:

```
leaq x@tlsdesc(%rip), %rcx  # or %rdx, %rbx, %rdi, ...
movq %rcx, %rax
call *x@tlscall(%rax)       # GNU ld/gold error for non-RAX
```

Differential Revision: https://reviews.llvm.org/D114416
2021-11-23 10:30:11 -08:00
Martin Storsjö
d703b92296 [LLD] [COFF] Omit section symbols and IMAGE_SYM_CLASS_LABEL from the PE symbol table
The section symbols aren't of much practical use when looking at
a linked image. This shrinks one observed mingw style unstripped
binary by 14%.

IMAGE_SYM_CLASS_LABEL is in spirit the same as a temporary assembler
label that isn't emitted on the object file level at all.

Differential Revision: https://reviews.llvm.org/D113866
2021-11-23 10:17:04 +02:00
Martin Storsjö
7c15da6761 [LLD] [COFF] Interpret the immediate in ARM64 adr/adrp relocations as signed 21 bit
This matches how MS link.exe interprets this relocation.

Differential Revision: https://reviews.llvm.org/D114347
2021-11-23 10:13:01 +02:00
Shoaib Meenai
2f5d6a0ea5 [MachO] Fix struct size assertion
std::vector can have different sizes depending on the STL's debug level,
so account for its size separately. (You could argue that we should be
accounting for all the other members separately as well, but that would
be very unergonomic, and std::vector is the only one that's caused
problems so far.)
2021-11-22 15:02:30 -08:00
Fangrui Song
7aafe467d2 [ELF] Simplify a condition with config->copyRelocs. NFC 2021-11-22 13:59:23 -08:00
Vy Nguyen
944071eca2 [lld-macho] Don't replace local personality symbol with LazySymbol
Follup-up to D107533, where we replaced local syms with non-local.
It doesn't make sense to replace local symbol with lazy.

Differential Revision: https://reviews.llvm.org/D110040
2021-11-22 14:09:54 -05:00
Igor Kudrin
a05b694b1e [ELF][NFC] Do not pass region name to expandMemoryRegion()
The name can be easily got on-site.

Differential Revision: https://reviews.llvm.org/D114228
2021-11-22 14:19:07 +07:00
Fangrui Song
648157b05a [ELF] Move getOutputSectionName from Writer.cpp to LinkerScript.cpp. NFC
and internalize it.
2021-11-20 22:18:09 -08:00
Fangrui Song
2997441b85 [ELF] Support discarding .got.plt
Fix a null pointer dereference when .got.plt is discarded.

This also adds a test for discarding `.plt`.

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D114180
2021-11-19 10:50:53 -08:00
Nico Weber
bc20bcb39e [lld/mac] Crash even less on undefined symbols with --icf=all
Follow-up to https://reviews.llvm.org/D112643. Even after that change, we were
still asserting if two separate functions that are eligible for ICF (same size,
same data, same number of relocs, same reloc types, ...) referred to
Undefineds. This fixes that oversight.

Differential Revision: https://reviews.llvm.org/D114195
2021-11-19 09:23:19 -05:00
Andrew Ng
47eb3f155f [ELF] Ensure output section is not discarded in addStartEndSymbols()
Fixes https://bugs.llvm.org/show_bug.cgi?id=52534.

Differential Revision: https://reviews.llvm.org/D114179
2021-11-19 11:45:58 +00:00
Konstantin Schwarz
8c18719bae [ELF] Expand LMA region if output section alignment introduces padding
When aligning the start address of an output section introduces a gap between the current dot pointer
and the new aligned address, we were already properly expanding the memory region, if available.

D74286 introduced a new behavior to also align the LMA address if an LMA region is specified.
However, this did not expand the corresponding LMA region.
Now, we also expand the LMA region if it is set.

This fixes PR52510.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D114166
2021-11-19 11:27:21 +01:00
Vincent Lee
adfbb5411b [lld-macho] Add warn flags to enable/disable warnings on -install_name
ld64 doesn't warn on builds using `-install_name` if it's a bundle. But, the
current warning is nice to have because `install_name` only works with dylib.
To prevent an overflow of warnings in build logs and have parity with ld64,
create a `--warn-dylib-install-name` and `--warn-no-dylib-install-name` flag
that enables this LLD specific warning.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D113534
2021-11-17 16:18:14 -08:00