7639 Commits

Author SHA1 Message Date
Fangrui Song
c790d6f53f [ELF] isCompatile: avoid a toStr and 2 ErrAlways 2024-11-24 11:30:21 -08:00
Fangrui Song
d8495ede01 [ELF] Change getLocation to use ELFSyncStream. NFC 2024-11-24 11:16:52 -08:00
Fangrui Song
ff97b28334 [ELF] Simplif reportUndefinedSymbol. NFC 2024-11-24 10:34:11 -08:00
Fangrui Song
eb4d2f24a7 [ELF] Simplify reportMissingFeature. NFC 2024-11-23 22:49:28 -08:00
Fangrui Song
1a2cc2bce8 [ELF] Exclude sizeof(InputSection) to _WIN32
Structs with delicate packing are often larger in MSVC than Itanium.
099a52fd2f3723db6b0550c99a1adc12d2d8d909 did not make
sizeof(InputSection) smaller for MSVC. Just exclude MSVC.
2024-11-23 21:42:22 -08:00
Fangrui Song
a87f776c1c [ELF] Avoid make in elf::writeARMCmseImportLib 2024-11-23 17:28:28 -08:00
Fangrui Song
e4e5206050 [ELF] Make OutputDesc unique_ptr
Store them in LinkerScript::descPool. This removes a SpecificAlloc
instantiation, makes lld smaller, and drops the small memory waste due
to the separate BumpPtrAllocator.
2024-11-23 17:14:43 -08:00
Fangrui Song
099a52fd2f [ELF] Reorder SectionBase/InputSectionBase members
Move `sectionKind` outside the bitfield and move bss/keepUnique to
InputSectionBase.

* sizeof(InputSection) decreases from 160 to 152 on 64-bit systems.
* The numerous `sectionKind` accesses are faster.
2024-11-23 16:34:21 -08:00
Fangrui Song
a5af6214dd [ELF] Make SyntheticSection parameter order match InputSection
And rename `/*alignment=*/` to `/*addralign=*/`
2024-11-23 15:16:36 -08:00
Fangrui Song
d4bed617f4 [ELF] -r: keep sh_entsize for SHF_MERGE sections with relocations
Follow-up to the NFC refactoring
43e3871a327b8e2ff57e16b46d5bc44beb430d91 and test cleanup
3cecf17065919da0a7fa9b38f37592e5462c2f85.

SHF_MERGE sections with relocations are handled as InputSection (without
duplicate elimination). The output section retains the original
sh_entsize in non-relocatable links. This patch ports the behavior for
relocatable links as well.

https://github.com/ClangBuiltLinux/linux/issues/2057
2024-11-23 14:32:32 -08:00
Fangrui Song
43e3871a32 [ELF] Make section member orders consistent
SectionBase, InputSectionBase, InputSection, MergeInputSection, and
OutputSection have different member orders. Make them consistent and
adopt the order similar to the raw Elf64_Shdr.
2024-11-23 14:22:24 -08:00
Fangrui Song
1490f38b22 [ELF] Avoid make<ArmCmseSGVeneer>
Store them as unique_ptr in sgVeneers instead.
2024-11-21 23:16:05 -08:00
Peter Smith
27923f7e1a
[LLD][AArch64][ARM] Delay adding long thunk mapping symbols (#116975)
When we create a thunk we don't know whether it will be short or long.
Move the emission of the long thunk mapping symbol to when we transition
to a long thunk. This improves disassembly and binary analysis as tools
like BOLT identify thunks by disassembly.

This removes a FIXME added in #108989 aarch64-thunk-bti-multipass.s
which had a corrupt disassembly due to missing mapping symbols.
2024-11-21 14:26:25 +00:00
Feng Zou
1ca853b2ee
[X86][MC,LLD][NFC] Rename R_X86_64_REX2_GOTPCRELX (#116737)
Rename R_X86_64_REX2_GOTPCRELX to R_X86_64_CODE_4_GOTPCRELX, to align
with GCC/binutils and ABI.

GCC/binutils:
3d5a60de52
and
4a54cb0658
ABI:
357de358ba
2024-11-20 21:02:44 +08:00
Fangrui Song
37e39667cc [ELF] Make ThunkCreator take ownership of thunks
This removes many SpecificAlloc instantiations and makes my lld (x86-64
Release+Assertions) smaller by ~36k.
2024-11-19 23:16:35 -08:00
Fangrui Song
a52032448e [ELF] Avoid make<PhdrEntry>
Store them in Partition::phdrs instead.
2024-11-19 22:38:14 -08:00
Fangrui Song
fa4d1860d2 [ELF] Move PhdrEntry to SyntheticSections
The next change will change Partition::phdrs to a unique_ptr vector,
which requires PhdrEntry to be a complete type.

And make OutputSection::getLMA out-of-line, since it should not include
either SyntheticSections.h or Writer.h.
2024-11-19 21:59:47 -08:00
Alexey Karyakin
64e3466fd0
[lld][Hexagon] Fix R_HEX_B22_PCREL range checks (#115925)
Range checks for R_HEX_B22_PCREL did not account for the fact that
offset is measured in instructions, not bytes.

Add a test for all range-checked relocations.
2024-11-19 09:27:01 -06:00
Fangrui Song
4092c0deef [ELF,ARM] Move global sectionMap into the ARM class
Otherwise, LLD_IN_TEST=2 testing arm-plt-reloc.s crashes.

Follow-up to https://reviews.llvm.org/D150870
2024-11-18 09:08:30 -08:00
Daniil Kovalev
1c4f335ec2
[PAC][lld] Use braa instr in PAC PLT sequence with valid PAuth core info (#113945)
Assume PAC instructions being supported with PAuth core info different from (0,0). The (0,0) value means that an ELF file is incompatible with PAuth - see https://github.com/ARM-software/abi-aa/blob/2024Q3/pauthabielf64/pauthabielf64.rst#core-information. With PAC non-hint instructions supported, `autia1716; br x17` can be replaced with `braa x17, x16; nop`, where `braa` is an authenticated branch instruction using IA key, discriminator from x16 and signed target address from x17.
2024-11-18 08:07:33 +03:00
Fangrui Song
6d98f11f3b [ELF] Work around extra "warning: $" with MSVC 14.41.34120 2024-11-17 11:08:32 -08:00
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
834457a134 [ELF] Simplify relocateNonAlloc diagnostic 2024-11-16 20:38:27 -08:00
Fangrui Song
483516fd83 [ELF] Remove unneeded Twine() 2024-11-16 20:32:44 -08:00
Fangrui Song
c1a6defd9f [ELF] Make RelType a struct type
otherwise operator<<(const ELFSyncStream &s, RelType type) applies to
non-reloc-type uint32_t, which can be confusing.
2024-11-16 20:26:34 -08:00
Fangrui Song
e57331ec63 [ELF] Move global relocMutex/undefs into Ctx 2024-11-16 19:22:11 -08:00
Fangrui Song
9664ce6d59 [ELF] Simplify complex diagnostics 2024-11-16 19:11:58 -08:00
Fangrui Song
dc6229bd66 [lld] Use context-aware outs() and errs() 2024-11-16 18:25:51 -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
7379a194d5 [ELF] Replace PPC64 writeSequence static variables with bAlloc
to reduce global state.
2024-11-16 17:14:29 -08:00
Fangrui Song
33ff9e43b4 [ELF] Move SharedFile::vernauxNum to Ctx 2024-11-16 17:00:51 -08:00
Fangrui Song
abbb0d9c94 [ELF] Remove unneeded toStr(ctx, x) when using ELFSyncStream
This patch removes the last use of the global `elf::ctx` outside of
elf::link.
2024-11-16 16:28:48 -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
7d13775486 [ELF] Pass ctx & 2024-11-16 15:48:01 -08:00
Fangrui Song
4a67b93fe8 [ELF] Make checkError context-aware 2024-11-16 15:43:05 -08:00
Fangrui Song
3b75a5c4c8 [ELF] Replace message(...) with Msg(ctx) 2024-11-16 15:34:42 -08:00
Fangrui Song
ce13dd1f7e [ELF] Replace fatal with Fatal(ctx) 2024-11-16 15:24:53 -08:00
Fangrui Song
a626eb2a2f [ELF] Pass ctx to bAlloc/saver/uniqueSaver 2024-11-16 15:20:21 -08:00
Fangrui Song
24c7d97cff [ELF] Replace context-less errorHandler() and error() with ctx.errHandler 2024-11-16 14:14:54 -08:00
Fangrui Song
4a6f59ac3c [ELF] Replace lld::warn with Warn(ctx) 2024-11-16 13:38:43 -08:00
Fangrui Song
6c19fa4bfc [ELF] Remove unneeded toString(Error) when using ELFSyncStream 2024-11-16 13:31:05 -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
a6755bdad1 [ELF] Replace global ctx with getCtx() 2024-11-16 12:11:00 -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