1269 Commits

Author SHA1 Message Date
Fangrui Song
0206181ada [ELF] Pass Ctx & to Driver and Writer 2024-09-23 21:28:14 -07:00
Fangrui Song
df0864e761 [ELF] Move elf::symtab into Ctx
Remove the global variable `symtab` and add a member variable
(`std::unique_ptr<SymbolTable>`) to `Ctx` instead.

This is one step toward eliminating global states.

Pull Request: https://github.com/llvm/llvm-project/pull/109612
2024-09-23 10:33:43 -07:00
Kazu Hirata
9ed46fbe9f
[lld] Use StringRef idioms (NFC) (#109584) 2024-09-22 20:45:25 -07:00
Fangrui Song
e1a1f18022 [ELF] Make Config config part of Ctx ctx 2024-09-22 18:18:27 -07:00
Fangrui Song
b8248dacad [ELF] Replace remnant config-> with ctx.arg. 2024-09-22 18:03:33 -07:00
Fangrui Song
777329d32f [ELF] Replace config-> with ctx.arg. in Driver.cpp and Target.h 2024-09-22 17:48:48 -07:00
Fangrui Song
49ec508596 [ELF] Pass Ctx & to Writer. NFC 2024-09-21 12:00:21 -07:00
Fangrui Song
cf57a670bb [ELF] ScriptParser: pass Ctx to ScriptParser and ScriptLexer. NFC 2024-09-21 11:06:06 -07:00
Fangrui Song
bb0a6f252f [ELF] Pass Ctx to LinkerScript. NFC 2024-09-21 10:22:11 -07:00
Fangrui Song
6b56a27349 [ELF] Replace config-> with ctx.arg. in LTO.cpp 2024-09-21 10:11:37 -07:00
Fangrui Song
da0b9db26f [ELF] Replace config-> with ctx.arg. NFC 2024-09-17 18:30:10 -07:00
Fangrui Song
d9045420ae [ELF] Add Config &Ctx::arg. NFC
And migrate LinkerDriver member functions to use `ctx.arg.x` instead of
`config->x`.
2024-09-16 16:44:53 -07:00
Fangrui Song
bffb26f153 [ELF] Add LinkerDriver::ctx. NFC 2024-09-16 16:25:28 -07:00
Fangrui Song
cf70a1ee81 [ELF] .llvm.sympart: support CREL
When both CREL and the experimental lld partitions feature are enabled,
the relocation section may look like .crel.llvm_sympart.f1, and
`rels.relas` is empty. While here, support relocation sections with zero
entry.
2024-09-16 13:12:45 -07:00
Fangrui Song
e88b7ff016 [ELF] Move InStruct into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

llvm/Support/thread.h includes <thread>, which transitively includes
sstream in libc++ and uses ios_base::in, so we cannot use `#define in ctx.sec`.

`symtab, config, ctx` are now the only variables using
LLVM_LIBRARY_VISIBILITY.
2024-09-15 22:15:02 -07:00
Vitaly Buka
a248ec3178 Revert "[ELF] Move InStruct into Ctx. NFC"
The define breaks `std::in`.

https://lab.llvm.org/buildbot/#/builders/169/builds/3253

This reverts commit 2531b46264cd066d51f2571d134a63998d13710f.
2024-09-15 18:22:42 -07:00
Fangrui Song
2531b46264 [ELF] Move InStruct into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

`#define in ctx.sec` is used for now to avoid migrating `in.xxx`.
2024-09-15 16:59:28 -07:00
Fangrui Song
40e8e4ddcb [ELF] Move partitions into ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-09-15 14:52:56 -07:00
JOE1994
4b27b5800f [lld] Nits on uses of raw_string_ostream (NFC)
* Don't call raw_string_ostream::flush(), which is essentially a no-op.
* Strip calls to raw_string_ostream::str(), to avoid excess layer of indirection.
2024-09-15 04:23:11 -04:00
Fangrui Song
b4feb26606 [ELF] Move target to Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

Follow-up to driver (2022-10) and script (2024-08).
2024-08-21 23:53:36 -07:00
Fangrui Song
4629aa1797 [ELF] Move script into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.

We now use default-initialization for `LinkerScript` and should pay
attention to non-class types (e.g. `dot` is initialized by commit
503907dc505db1e439e7061113bf84dd105f2e35).
2024-08-21 21:23:28 -07:00
Fangrui Song
c62fa63ff1 [ELF] Move mainPart to Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-08-21 20:08:11 -07:00
Fangrui Song
89b1468345 [ELF] Move ppc64noTocRelax to Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-08-21 00:10:31 -07:00
Fangrui Song
0766a59be3 [ELF] .llvm.call-graph-profile: support CREL
https://reviews.llvm.org/D105217 added RELA support. This patch adds
CREL support.
2024-08-08 00:57:43 -07:00
Fangrui Song
2fe3bbdf67 [ELF] Move outputSections into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons.
2024-08-03 11:50:48 -07:00
Fangrui Song
03be619d94 [ELF] Move ElfSym into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons. ctx's hidden visibility optimizes generated instructions.

This change fixes a pitfall: certain ElfSym members (e.g.
globalOffsetTable, tlsModuleBase) were not zeroed and might be stale
when lld:🧝:link was invoked the second time.
2024-08-03 11:20:32 -07:00
Fangrui Song
09dd0febbb [ELF] Move Out into Ctx. NFC
Ctx was introduced in March 2022 as a more suitable place for such
singletons. ctx's hidden visibility optimizes generated instructions.

bufferStart and tlsPhdr, which are not OutputSection, can now be moved
outside of `Out`.
2024-08-03 11:00:11 -07:00
Fangrui Song
5d972c582a
[ELF] Add -z nosectionheader
GNU ld since 2.41 supports this option, which is mildly useful. It omits
the section header table and non-ALLOC sections (including
.symtab/.strtab (--strip-all)).

This option is simple to implement and might be used by LLDB to test
program headers parsing without the section header table (#100900).

-z sectionheader, which is the default, is also added.

Pull Request: https://github.com/llvm/llvm-project/pull/101286
2024-07-31 12:57:23 -07:00
Fangrui Song
8e2476e102 [ELF] Move SymbolAux into Ctx. NFC
The number of uses is modest.
2024-07-28 20:51:33 -07:00
Fangrui Song
fd791f0fe5 [ELF] Move TarWriter into Ctx. NFC
Similar to e980f16d52196fb2bc672ecb87e0f622253addec.
2024-07-28 15:32:22 -07:00
Fangrui Song
ff7f97a819 [ELF] --defsym: support quoted LHS
and move = splitting from Driver.cpp to ScriptParser.cpp.
2024-07-28 12:38:10 -07:00
Joseph Huber
615b7eeaa9 Reapply "[LLVM][LTO] Factor out RTLib calls and allow them to be dropped (#98512)"
This reverts commit 740161a9b98c9920dedf1852b5f1c94d0a683af5.

I moved the `ISD` dependencies into the CodeGen portion of the handling,
it's a little awkward but it's the easiest solution I can think of for
now.
2024-07-20 09:29:31 -05:00
NAKAMURA Takumi
740161a9b9 Revert "[LLVM][LTO] Factor out RTLib calls and allow them to be dropped (#98512)"
This reverts commit c05126bdfc3b02daa37d11056fa43db1a6cdef69.
(llvmorg-19-init-17714-gc05126bdfc3b)
See #99610
2024-07-20 12:36:57 +09:00
Brian Cain
9078036685
[lld] Add emulation support for hexagon (#98857) 2024-07-16 15:01:27 -05:00
Joseph Huber
c05126bdfc
[LLVM][LTO] Factor out RTLib calls and allow them to be dropped (#98512)
Summary:
The LTO pass and LLD linker have logic in them that forces extraction
and prevent internalization of needed runtime calls. However, these
currently take all RTLibcalls into account, even if the target does not
support them. The target opts-out of a libcall if it sets its name to
nullptr. This patch pulls this logic out into a class in the header so
that LTO / lld can use it to determine if a symbol actually needs to be
kept.

This is important for targets like AMDGPU that want to be able to use
`lld` to perform the final link step, but does not want the overhead of
uncalled functions. (This adds like a second to the link time trivially)
2024-07-16 06:22:09 -05:00
Daniel Thornburgh
5b82741a01
[lld][ELF] Error when deplibs adds new input file after LTO (#98565)
Parsing the new input file's symbols might invalidate LTO codegen, but
the semantics of deplibs require them to be parsed. Accordingly, report
an error unless the file had already been added to the link.

Fixes #56070
2024-07-12 12:43:53 -07:00
Alexander Qi
1b3e376ec0
[lld][ELF] --wrap: Make X weak when __real_X is to avoid undefined symbol errors (#98297)
Fix #98294.

When you specify --wrap=foo, sometimes foo is undefined in any context.
If you declare __real_foo as weak, GNU ld will not attempt to find the
strong symbol foo, instead, it generates a weak undefined symbol.

This pull request imitates this behavior by copying the binding
attribute from __real_foo to foo.
2024-07-10 18:38:52 -07:00
Fangrui Song
649cdfc4b6
[ELF] Reinstate the former spelling in the version message
With `LLVM_APPEND_VC_REV=off`, the new version message after #97323
looks like:
```
% /tmp/out/custom2/bin/ld.lld --version
LLD 19.0.0, compatible with GNU linkers
```

A trailing comma after the version string might cause issues with
version detection tools that don't strip it, as seen in the Linux
kernel's scripts/ld-version.sh script.

Pull Request: https://github.com/llvm/llvm-project/pull/97942
2024-07-07 10:30:33 -07:00
Hongyu Chen
0f9fbbb63c
[lld][ELF] Support LLVM repository and LLVM revision information (#97323)
Added LLVM repository and LLVM revision information for
`lld::getLLDVersion()`

Before this change:
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (compatible with GNU linkers) 
```

After this change with LLVM_APPEND_VC_REV=on
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0 (https://github.com/yugier/llvm-project.git 4134b33c6a362cb462b335177d6d9e8235f04309), compatible with GNU linkers
```

with LLVM_APPEND_VC_REV=off
```
hongyuchy@hongyuchy:~/llvm-project/.build_lld_version$ bin/ld.lld --version
LLD 19.0.0, compatible with GNU linkers
```
2024-07-02 20:32:45 -07:00
Joseph Huber
93e0ffa790
[lld] Add --lto-emit-asm and alias --plugin-opt=emit-llvm to it (#97469)
Summary:
The LTO pass currently supporting emitting LTO via the
`--plugin-opt=emit-llvm` option. However, there is a very similar option
called `--lto-emit-asm`. This patch just makes the usage more
consistent and more obvious that emitting LLVM-IR is supported.
2024-07-02 15:35:51 -05:00
Fangrui Song
5f1743cd07
[ELF] Infer EI_OSABI from object files
The first object file whose EI_OSABI is not ELFOSABI_NONE is selected.
This is useful for some OSes to identify themselves. This achieves
similar effects to BFD emulations `ld.lld -m *_fbsd` but is more
lightweight.

Pull Request: https://github.com/llvm/llvm-project/pull/97144
2024-07-02 11:00:42 -07:00
Fangrui Song
8cc6a2469c [ELF] -r: force -Bstatic
In GNU ld, -r forces -Bstatic and has precedence over -Bdynamic: -lfoo
probes libfoo.a but not libfoo.so, even if -Bdynamic is in effect. Our
behavior currently matches gold and probes libfoo.so. Since we don't
have strong opinion on the exact behavior, let's just follow GNU ld and
also unify the reason we report the "attempted static link of dynamic
object " error.

Close #94958
2024-06-14 23:20:50 -07:00
Fangrui Song
4d9020ca0b
[ELF] Implement --force-group-allocation
GNU ld's relocatable linking behaviors:

* Sections with the `SHF_GROUP` flag are handled like sections matched
  by the `--unique=pattern` option. They are processed like orphan
  sections and ignored by input section descriptions.
* Section groups' (usually named `.group`) content is updated as the
  section indexes are updated. Section groups can be discarded with
  `/DISCARD/ : { *(.group) }`.

`-r --force-group-allocation` discards section groups and allows
sections with the `SHF_GROUP` flag to be matched like normal sections.
If two section group members are placed into the same output section,
their relocation sections (if present) are combined as well.
This behavior can be useful when -r output is used as a pseudo shared
object (e.g., FreeBSD's amd64 kernel modules, CHERIoT compartments).

This patch implements --force-group-allocation:

* Input SHT_GROUP sections are discarded.
* Input sections do not get the SHF_GROUP flag, so `addInputSec`
  will combine relocation sections if their relocated section group
  members are combined.

The default behavior is:

* Input SHT_GROUP sections are retained.
* Input SHF_GROUP sections can be matched (unlike GNU ld)
* Input SHF_GROUP sections keep the SHF_GROUP flag, so `addInputSec`
  will create different OutputDesc copies.

GNU ld provides the `FORCE_GROUP_ALLOCATION` command, which is not
implemented.

Pull Request: https://github.com/llvm/llvm-project/pull/94704
2024-06-07 14:19:06 -07:00
John Brawn
cfeb25cd7e
[lld][AArch64] Add support for GCS (#90732)
This adds the -z gcs and -z gcs-report options, which behave similarly
to -z shtk and -z cet-report, except that -z gcs accepts a parameter:
* -z gcs=implicit is the default behaviour, where the GCS bit is
inferred from the input objects.
 * -z gcs=never clears the GCS bit, ignoring the input objects.
 * -z gcs=always sets the GCS bit, ignoring the input objects.

This is so that there's a means of explicitly disabling GCS even when
all input objects have the GCS bit set.
2024-05-21 17:34:17 +01:00
Daniel Thornburgh
66466ff151 Reland: [LLD] Implement --enable-non-contiguous-regions (#90007)
When enabled, input sections that would otherwise overflow a memory
region are instead spilled to the next matching output section.

This feature parallels the one in GNU LD, but there are some differences
from its documented behavior:

- /DISCARD/ only matches previously-unmatched sections (i.e., the flag
does not affect it).

- If a section fails to fit at any of its matches, the link fails
instead of discarding the section.

- The flag --enable-non-contiguous-regions-warnings is not implemented,
as it exists to warn about such occurrences.

The implementation places stubs at possible spill locations, and
replaces them with the original input section when effecting spills.
Spilling decisions occur after address assignment. Sections are spilled
in reverse order of assignment, with each spill naively decreasing the
size of the affected memory regions. This continues until the memory
regions are brought back under size. Spilling anything causes another
pass of address assignment, and this continues to fixed point.

Spilling after rather than during assignment allows the algorithm to
consider the size effects of unspillable input sections that appear
later in the assignment. Otherwise, such sections (e.g. thunks) may
force an overflow, even if spilling something earlier could have avoided
it.

A few notable feature interactions occur:

- Stubs affect alignment, ONLY_IF_RO, etc, broadly as if a copy of the
input section were actually placed there.

- SHF_MERGE synthetic sections use the spill list of their first
contained input section (the one that gives the section its name).

- ICF occurs oblivious to spill sections; spill lists for merged-away
sections become inert and are removed after assignment.

- SHF_LINK_ORDER and .ARM.exidx are ordered according to the final
section ordering, after all spilling has completed.

- INSERT BEFORE/AFTER and OVERWRITE_SECTIONS are explicitly disallowed.
2024-05-13 11:06:54 -07:00
Daniel Thornburgh
81f34afa5c
Revert "[LLD] Implement --enable-non-contiguous-regions" (#92005)
Reverts llvm/llvm-project#90007

Broke in merging I think.
2024-05-13 10:38:40 -07:00
Daniel Thornburgh
673114447b
[LLD] Implement --enable-non-contiguous-regions (#90007)
When enabled, input sections that would otherwise overflow a memory
region are instead spilled to the next matching output section.

This feature parallels the one in GNU LD, but there are some differences
from its documented behavior:

- /DISCARD/ only matches previously-unmatched sections (i.e., the flag
does not affect it).

- If a section fails to fit at any of its matches, the link fails
instead of discarding the section.

- The flag --enable-non-contiguous-regions-warnings is not implemented,
as it exists to warn about such occurrences.

The implementation places stubs at possible spill locations, and
replaces them with the original input section when effecting spills.
Spilling decisions occur after address assignment. Sections are spilled
in reverse order of assignment, with each spill naively decreasing the
size of the affected memory regions. This continues until the memory
regions are brought back under size. Spilling anything causes another
pass of address assignment, and this continues to fixed point.

Spilling after rather than during assignment allows the algorithm to
consider the size effects of unspillable input sections that appear
later in the assignment. Otherwise, such sections (e.g. thunks) may
force an overflow, even if spilling something earlier could have avoided
it.

A few notable feature interactions occur:

- Stubs affect alignment, ONLY_IF_RO, etc, broadly as if a copy of the
input section were actually placed there.

- SHF_MERGE synthetic sections use the spill list of their first
contained input section (the one that gives the section its name).

- ICF occurs oblivious to spill sections; spill lists for merged-away
sections become inert and are removed after assignment.

- SHF_LINK_ORDER and .ARM.exidx are ordered according to the final
section ordering, after all spilling has completed.

- INSERT BEFORE/AFTER and OVERWRITE_SECTIONS are explicitly disallowed.
2024-05-13 10:30:50 -07:00
Fangrui Song
65c9b8460b [Driver] Remove elf::script indirection. NFC
There are 100+ references.
Use a wrapper similar to a623a4c8b4d5cc933ebc1856ce30aac935f62b18
2024-05-10 19:45:42 -07:00
Fangrui Song
6d44a1ef55
[ELF] Adjust --compress-sections to support compression level
zstd excels at scaling from low-ratio-very-fast to
high-ratio-pretty-slow. Some users prioritize speed and prefer disk read
speed, while others focus on achieving the highest compression ratio
possible, similar to traditional high-ratio codecs like LZMA.

Add an optional `level` to `--compress-sections` (#84855) to cater to
these diverse needs. While we initially aimed for a one-size-fits-all
approach, this no longer seems to work.
(https://richg42.blogspot.com/2015/11/the-lossless-decompression-pareto.html)

When --compress-debug-sections is used together, make
--compress-sections take precedence since --compress-sections is usually
more specific.

Remove the level distinction between -O/-O1 and -O2 for
--compress-debug-sections=zlib for a more consistent user experience.

Pull Request: https://github.com/llvm/llvm-project/pull/90567
2024-05-01 11:40:46 -07:00
Fangrui Song
f02a27df2f
[ELF] Add --default-script/-dT
GNU ld added --default-script (alias: -dT) in 2007. The option specifies
a default script that is processed if --script/-T is not specified. -dT
can be used to override GNU ld's internal linker script, but only when
the application does not specify -T.
In addition, dynamorio's CMakeLists.txt may use -dT.

The implementation is simple and the feature can be useful to dabble
with different section layouts.

Pull Request: https://github.com/llvm/llvm-project/pull/89327
2024-04-19 09:09:41 -07:00