506 Commits

Author SHA1 Message Date
Fangrui Song
875d24c230 [ELF] Avoid list initialization with incomplete unique_ptr<OutputSection> member to work around clang < 16
Commit 5b1b6a62b8bd986adc711d0c0be5b6a8182be263 introduced the following
issue for older clang with libstdc++

```
In file included from /home/ray/llvm/lld/ELF/EhFrame.cpp:18:
In file included from /home/ray/llvm/lld/ELF/EhFrame.h:12:
In file included from /home/ray/llvm/lld/include/lld/Common/LLVM.h:21:
In file included from /home/ray/llvm/llvm/include/llvm/Support/Casting.h:20:
In file included from /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/memory:78:
/usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bits/unique_ptr.h:91:16: error: invalid application of 'sizeof' to an incomplete type 'lld:🧝:OutputSection'
        static_assert(sizeof(_Tp)>0,
                      ^~~~~~~~~~~
/usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../include/c++/14.2.1/bits/unique_ptr.h:398:4: note: in instantiation of member function 'std::default_delete<lld:🧝:OutputSection>::operator()' requested here
          get_deleter()(std::move(__ptr));
          ^
/home/ray/llvm/lld/ELF/Config.h:574:19: note: in instantiation of member function 'std::unique_ptr<lld:🧝:OutputSection>::~unique_ptr' requested here
  OutSections out{};
                  ^
```
2024-11-17 10:16:01 -08:00
Fangrui Song
5b1b6a62b8 [ELF] Make elfHeader/programHeaders unique_ptr
This removes some SpecificAlloc instantiations, makes lld smaller, and
drops the small memory waste due to the separate BumpPtrAllocator.
2024-11-17 00:25:42 -08:00
Fangrui Song
53dc4e7600 [ELF] createSyntheticSections: replace some make<> with unique_ptr
This removes some SpecificAlloc instantiations and makes lld smaller.
This drops the small memory waste due to the separate BumpPtrAllocator.
2024-11-17 00:09:06 -08:00
Fangrui Song
dbf37e956a [ELF] Move InputFile storage from make<> to LinkerDriver::files 2024-11-16 23:50:35 -08:00
Fangrui Song
2991a4e209 [ELF] Replace functions bAlloc/saver/uniqueSaver with member access 2024-11-16 22:34:13 -08:00
Fangrui Song
8f238f662c [ELF] Make Ctx inherit from CommonLinkerContext
link calls `new CommonLinkerContext`. Now that `Ctx ctx` is a local
variable, we can make it inherit from CommonLinkerContext.
2024-11-16 22:28:55 -08:00
Fangrui Song
e57331ec63 [ELF] Move global relocMutex/undefs into Ctx 2024-11-16 19:22:11 -08:00
Fangrui Song
73bb022b46 [ELF] Remove global ctx
This commit completes the work that eliminates global variables like
config/target/inputSections/symTab from lld/ELF.

Key changes:

* Introduced `lld:🧝:ctx` to encapsulate global state.
* Moved global variables into `Ctx lld:🧝:ctx`
* Updated many functions to accept `Ctx &ctx`
* Made `ctx` a local variable (this commit)

If we don't count `static std::mutex`, this is the last major global
state within lld/ELF (minor ones like `SharedFile::vernauxNum`
(33ff9e43b4c5bdc3da31c6b11ad51d35a69bec5f) might not all be eliminated
yet).
2024-11-16 17:24:11 -08:00
Fangrui Song
33ff9e43b4 [ELF] Move SharedFile::vernauxNum to Ctx 2024-11-16 17:00:51 -08:00
Fangrui Song
6f87d1437e [ELF] Initialize Ctx members
Prevent use of uninitialized memory when `ctx` becomes a local variable.
2024-11-16 16:22:59 -08:00
Fangrui Song
3b75a5c4c8 [ELF] Replace message(...) with Msg(ctx) 2024-11-16 15:34:42 -08:00
Fangrui Song
a626eb2a2f [ELF] Pass ctx to bAlloc/saver/uniqueSaver 2024-11-16 15:20:21 -08:00
Fangrui Song
38870fe124 [ELF] Remove unneeded toString(Error) when using ELFSyncStream 2024-11-16 13:22:06 -08:00
Fangrui Song
be5dad012e [ELF] Replace internalLinkerError(getErrorLoc(ctx, buf) + ...) with InternalErr(ctx, buf)
and simplify `+ toStr(ctx, x)` to `<< x`.
The trailing '\n' << llvm::getBugReportMsg() is not very useful and
therefore removed.
2024-11-16 13:07:17 -08:00
Fangrui Song
58a971f42f [ELF] Replace contex-less toString(x) with toStr(ctx, x)
so that we can remove the global `ctx` from toString implementations.
Rename lld::toString (to lld:🧝:toStr) to simplify name lookup (we
have many llvm::toString and another lld::toString(const llvm::opt::Arg
&)).
2024-11-16 11:58:10 -08:00
Fangrui Song
d82422f69c [ELF] Remove errorOrWarn 2024-11-15 09:37:38 -08:00
Fangrui Song
09c2c5e1e9 [ELF] Replace error(...) with ErrAlways or Err
Most are migrated to ErrAlways mechanically.
In the future we should change most to Err.
2024-11-06 22:04:52 -08:00
Fangrui Song
63c6fe4a0b [ELF] Replace fatal(...) with Fatal or Err 2024-11-06 21:17:26 -08:00
Fangrui Song
201d7607f8
[ELF] Add context-aware diagnostic functions (#112319)
The current diagnostic functions log/warn/error/fatal lack a context
argument and call the global `lld::errorHandler()`, which prevents
multiple lld instances in one process.

This patch introduces context-aware replacements:

* log => Log(ctx)
* warn => Warn(ctx)
* errorOrWarn => Err(ctx)
* error => ErrAlways(ctx)
* fatal => Fatal(ctx)

Example: `errorOrWarn(toString(f) + "xxx")` => `Err(ctx) << f << "xxx"`.
(`toString(f)` is shortened to `f` as a bonus and may access `ctx`
without accessing the global variable (see `Target.cpp`)).

`ctx.e = &context->e;` can be replaced with a non-global Errorhandler
when `ctx` becomes a local variable.

(For the ELF port, the long term goal is to eliminate `error`. Most can
be straightforwardly converted to `Err(ctx)`.)
2024-11-06 08:25:58 -08:00
Fangrui Song
e1a073c9d9 [ELF] Change Ctx::target to unique_ptr (#111260)
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo`
and change it to set `ctx.target`. This ensures that when `ctx` becomes
a local variable, two lld invocations will not reuse the function-local
static variable.

---

Reland after commit c35214c131c0bc7f54dc18ceb75c75cba89f58ee
([ELF] Initialize TargetInfo members).
2024-10-07 23:14:02 -07:00
Paul Kirth
2ca850111f
Revert "[ELF] Change Ctx::target to unique_ptr (#111260)" (#111449)
This patch seems to be breaking the windows build bots.
https://lab.llvm.org/buildbot/#/builders/63/builds/1953

We also see this in Fuchsia's Linux CI:
https://fxbug.dev/372010530

This reverts commit 4ec06b17435e32ece5e1aa2bc8a6d26dbf0bb312.
2024-10-07 15:43:01 -07:00
Fangrui Song
4ec06b1743
[ELF] Change Ctx::target to unique_ptr (#111260)
also rename `TargetInfo *getXXXTargetInfo` to `void setXXXTargetInfo`
and change it to set `ctx.target`. This ensures that when `ctx` becomes
a local variable, two lld invocations will not reuse the function-local
static variable.
2024-10-06 21:47:13 -07:00
Fangrui Song
cfd3289a1f [ELF] Pass Ctx & to some free functions 2024-10-06 19:36:21 -07:00
Fangrui Song
f2b0133858 [ELF] Move static nextGroupId isInGroup to LinkerDriver 2024-10-06 17:38:35 -07:00
Fangrui Song
079b8327ec [ELF] Pass Ctx & to InputFiles and SyntheticSections 2024-09-29 16:06:47 -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
Fangrui Song
e1a1f18022 [ELF] Make Config config part of Ctx ctx 2024-09-22 18:18:27 -07:00
Fangrui Song
6f482010ae [ELF] Replace config-> with ctx.arg. 2024-09-21 22:46:13 -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
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
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
Oliver Stannard
a1c6467bd9
[lld][ARM] Fix assertion when mixing ARM and Thumb objects (#101985)
Previously, we selected the Thumb2 PLT sequences if any input object is
marked as not supporting the ARM ISA, which then causes assertion
failures when calls from ARM code in other objects are seen. I think the
intention here was to only use Thumb PLTs when the target does not have
the ARM ISA available, signalled by no objects being marked as having it
available. To do that we need to track which ISAs we have seen as we
parse the build attributes, and defer the decision about PLTs until all
input objects have been parsed.

This bug was triggered by real code in picolibc, which have some
versions of string.h functions built with Thumb2-only build attributes,
so that they are compatible with v7-A, v7-R and v7-M.

Fixes #99008.
2024-08-07 10:20:26 +01: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
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
PiJoules
025394fa0d
Reapply "[lld] Support thumb PLTs" (#93631) (#93644)
This reverts commit 7832769d329ead264aff238c06dce086b3a74922.

This was reverted prior due to a test failure on the windows builder. I
think this was because we didn't specify the triple and assumed windows.
The other tests use the full triple specifying linux, so we follow suite
here.

---

We are using PLTs for cortex-m33 which only supports thumb. More
specifically, this is for a very restricted use case. There's no MMU so
there's no sharing of virtual addresses between two processes, but this
is fine. The MCU is used for running [chre
nanoapps](https://android.googlesource.com/platform/system/chre/+/HEAD/doc/nanoapp_overview.md)
for android. Each nanoapp is a shared library (but effectively acts as
an executable containing a test suite) that is loaded and run on the MCU
one binary at a time and there's only one process running at a time, so
we ensure that the same text segment cannot be shared by two different
running executables. GNU LD supports thumb PLTs but we want to migrate
to a clang toolchain and use LLD, so thumb PLTs are needed.
2024-05-29 13:28:32 -07:00
Mehdi Amini
7832769d32
Revert "[lld] Support thumb PLTs" (#93631)
Reverts llvm/llvm-project#86223

windows pre-merge is broken.
2024-05-28 19:46:23 -06:00
PiJoules
760c2aa55f
[lld] Support thumb PLTs (#86223)
We are using PLTs for cortex-m33 which only supports thumb. More
specifically, this is for a very restricted use case. There's no MMU so
there's no sharing of virtual addresses between two processes, but this
is fine. The MCU is used for running [chre
nanoapps](https://android.googlesource.com/platform/system/chre/+/HEAD/doc/nanoapp_overview.md)
for android. Each nanoapp is a shared library (but effectively acts as
an executable containing a test suite) that is loaded and run on the MCU
one binary at a time and there's only one process running at a time, so
we ensure that the same text segment cannot be shared by two different
running executables. GNU LD supports thumb PLTs but we want to migrate
to a clang toolchain and use LLD, so thumb PLTs are needed.
2024-05-28 15:37:03 -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