Commit Graph

16622 Commits

Author SHA1 Message Date
Mingming Liu
dda73336ad [ThinLTO]Record import type in GlobalValueSummary::GVFlags (#87597)
The motivating use case is to support import the function declaration
across modules to construct call graph edges for indirect calls [1]
when importing the function definition costs too much compile time
(e.g., the function is too large has no `noinline` attribute).
1. Currently, when the compiled IR module doesn't have a function
definition but its postlink combined summary contains the function
summary or a global alias summary with this function as aliasee, the
function definition will be imported from source module by IRMover. The
implementation is in FunctionImporter::importFunctions [2]
2. In order for FunctionImporter to import a declaration of a function,
both function summary and alias summary need to carry the def / decl
state. Specifically, all existing summary fields doesn't differ across
import modules, but the def / decl state of is decided by
`<ImportModule, Function>`.

This change encodes the def/decl state in `GlobalValueSummary::GVFlags`.

In the subsequent changes
1. The indexing step `computeImportForModule` [3]
will compute the set of definitions and the set of declarations for each
module, and passing on the information to bitcode writer.
2. Bitcode writer will look up the def/decl state and sets the state
when it writes out the flag value. This is demonstrated in
https://github.com/llvm/llvm-project/pull/87600
3. Function importer will read the def/decl state when reading the
combined summary to figure out two sets of global values, and IRMover
will be updated to import the declaration (aka linkGlobalValuePrototype [4])
into the destination module.

- The next change is https://github.com/llvm/llvm-project/pull/87600

[1] mentioned in rfc https://discourse.llvm.org/t/rfc-for-better-call-graph-sort-build-a-more-complete-call-graph-by-adding-more-indirect-call-edges/74029#support-cross-module-function-declaration-import-5
[2] 3b337242ee/llvm/lib/Transforms/IPO/FunctionImport.cpp (L1608-L1764)
[3] 3b337242ee/llvm/lib/Transforms/IPO/FunctionImport.cpp (L856)
[4] 3b337242ee/llvm/lib/Linker/IRMover.cpp (L605)
2024-04-10 19:46:01 -07:00
Fangrui Song
c258f57398 [ELF] Move createSyntheticSections from Writer.cpp to SyntheticSections.cpp. NFC
SyntheticSections.cpp is more appropriate. This change enables
elimination of many explicit template instantiations.

Due to `make<SymbolTableSection<ELFT>>(*strtab)` in Arch/ARM.cpp,
we do not remove explicit template instantiations for SymbolTableSection.
2024-04-10 13:42:51 -07:00
Fangrui Song
ca6b8469c1 [ELF] Avoid unneeded config->isLE and config->wordsize. NFC 2024-04-10 12:20:28 -07:00
Jacek Caban
a2bdbc6f0d [LLD][COFF] Check machine types in ICF::equalsConstant. (#88140)
Avoid replacing replacing a chunk with one from a different type. It's
mostly a concern for ARM64X, where we don't want to merge aarch64 and
arm64ec chunks, but it may also in theory happen between arm64ec and
 x86_64 chunks.
2024-04-10 14:37:18 +02:00
Fangrui Song
ee284d2da0 [ELF] Avoid make<GdbIndexSection>. NFC 2024-04-09 21:32:37 -07:00
Fangrui Song
4afcfd7498 [ELF] Sort DWARF.h sections. NFC
to make it clear whether .debug_names should be added.
And include llvm/ADT/STLFunctionalExtras.h for IWYU.
2024-04-09 14:13:45 -07:00
Daniil Kovalev
3989e22457 [test][lld][ELF] Enhance pack-dyn-relocs.s test (#87756)
Use symbol `.data` (STT_SECTION) instead of `__ehdr_start` for some
relocation entries. See discussion in

[72714/#discussion_r1517127619](https://github.com/llvm/llvm-project/pull/72714/#discussion_r1517127619)
2024-04-05 21:50:29 +03:00
Fangrui Song
e915b7d816 [ELF,test] Add test for R_AARCH64_* implicit addends
to support certain static relocations in the REL format. See #87328 for
the armasm need.

Note: `R_AARCH64_{ABS64,PREL32,PREL64}` have been implemented by https://reviews.llvm.org/D120535

Pull Request: https://github.com/llvm/llvm-project/pull/87733
2024-04-05 10:52:10 -07:00
Jacek Caban
c0211ff206 [LLD][COFF] Add ARM64EC support to findLineTable. (#87240)
Fixes asserts in error messages. Use chunk machine type (instead of
global one) to support x86_64 objects and treat ARM64EC objects like
ARM64 ones.
2024-04-05 13:14:15 +02:00
David Spickett
aff197ff21 Reland "[flang][clang] Add Visibility specific help text for options (#81869)"
This reverts commit 67d20412b4.

This includes fixes for clanginstallapi.
2024-04-05 08:27:59 +00:00
David Spickett
67d20412b4 Revert "[flang][clang] Add Visibility specific help text for options (#81869)"
This reverts commit 7e958f64ef.

Failing on multiple bots.
2024-04-05 08:15:35 +00:00
David Spickett
7e958f64ef [flang][clang] Add Visibility specific help text for options (#81869)
And use it to print the correct default OpenMP version for flang and
flang -fc1.

This change adds an optional `HelpTextsForVariants` to options. This
allows you to change the help text that gets shown in documentation and
`--help` based on the program its being generated for.

As `OptTable` needs to be constexpr compatible, I have used a std::array
of help text variants. Each entry is:
(list of visibilities) - > help text string

So for the OpenMP version we have (flang, fc1) -> "OpenMP version for
flang is...".

So you can have multiple visibilities use the same string. The number of
entries is currently set to 1, and the number of visibilities per entry
is 2, because that's the maximum we need for now. The code is written so
we can increase these numbers later, and the unused elements will be initialised.

I have not applied this to group descriptions just because I don't know
of one that needs changing. It could easily be enabled for those too if
needed. There are minor changes to them just to get it all to compile.

This approach of storing many help strings per option in the 1 driver
library seemed preferable to making a whole new library for Flang (even
if that would mostly be including stuff from Clang).
2024-04-05 09:03:16 +01:00
Fangrui Song
dcc45faa30 [ELF] PROVIDE: fix spurious "symbol not found"
When archive member extraction involving ENTRY happens after
`addScriptReferencedSymbolsToSymTable`,
`addScriptReferencedSymbolsToSymTable` may fail to define some PROVIDE
symbols used by ENTRY. This is an edge case that regressed after #84512.
(The interaction with PROVIDE and ENTRY-in-archive was not considered
before).

While here, also ensure that --undefined-glob extracted object files
are parsed before `addScriptReferencedSymbolsToSymTable`.

Fixes: ebb326a51f

Pull Request: https://github.com/llvm/llvm-project/pull/87530
2024-04-04 09:38:01 -07:00
Jacek Caban
8f9903db8a [LLD][COFF][NFC] Use getMachineArchType helper. (#87495)
It's similar to #87370, but for lld-link.
2024-04-04 14:41:50 +02:00
Jacek Caban
ff56584ee9 [LLD][COFF] Use getMachineArchType in LinkerDriver::getArch. (#87499)
Adds support for ARM64EC, which should use the same search paths as
ARM64.

It's similar to #87370 and #87495. The test is based on the existing x86
test. Generally ARM64EC libraries are shipped together with native ARM64
libraries (using ECSYMBOLS section mechanism).

getMachineArchType uses Triple::thumb, while the existing
implementation uses Triple::arm. It's ultimately passed to
MSVCPaths.cpp functions, so modify them to accept both forms.
2024-04-04 14:25:54 +02:00
Daniil Kovalev
cca9115b1c [lld][AArch64][ELF][PAC] Support AUTH relocations and AUTH ELF marking (#72714)
This patch adds lld support for:

- Dynamic R_AARCH64_AUTH_* relocations (without including RELR compressed AUTH
relocations) as described here:
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#auth-variant-dynamic-relocations

- .note.AARCH64-PAUTH-ABI-tag section as defined here
https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#elf-marking

Depends on #72713 and #85231

---------

Co-authored-by: Peter Collingbourne <peter@pcc.me.uk>
Co-authored-by: Fangrui Song <i@maskray.me>
2024-04-04 12:38:09 +03:00
Fangrui Song
93c16e75b8 [ELF] Actually sort IRELATIVE by offset
The unstable partition in partitionRels might reverse IRELATIVE
relocations, so stable_partition in computeRels would lead to IRELATIVE
relocations ordered by decreasing offset. Use stable_partition in
partitionRels to get IRELATIVE relocations ordered by increasing offset.
2024-04-02 19:34:14 -07:00
Fangrui Song
01e227487f [ELF] Sort IRELATIVE by offset
Improve the test gnu-ifunc-nonpreemptible.s to check IRELATIVE offsets.
Ensure that IRELATIVE offsets are ordered to improve locality.
2024-04-02 18:47:49 -07:00
Fangrui Song
df54f627fa [ELF] Enhance --no-allow-shlib-undefined for non-exported definitions
For a DSO with all DT_NEEDED entries accounted for, if it contains an
undefined non-weak symbol that shares a name with a non-exported
definition (hidden visibility or localized by a version script), and
there is no DSO definition, we should report an error.

#70769 implemented the error when we see `ref.so def-hidden.so`. This patch
implementes the error when we see `def-hidden.so ref.so`, matching GNU
ld.

Close #86777
2024-03-29 23:36:50 -07:00
Fangrui Song
09403a66c0 [ELF,test] Improve "non-exported symbol" tests
Test that the error is not triggered when we see the non-exported
definition first (#86777).
2024-03-29 23:26:29 -07:00
Leandro Lupori
6b95747087 [lld-macho] Disable objc test on 32-bit ARM targets
lld/test/MachO/objc-relative-method-lists-simple.s fails on AArch64
hosts running 32-bit ARM binaries, such as
armv8l-unknown-linux-gnueabihf.

Disable the test on the failing targets for now, to keep the
buildbots passing.
2024-03-28 20:35:25 +00:00
Keith Smiley
39fe729502 [lld-macho] Ignore -no_warn_duplicate_libraries flag (#86303)
This is a new ld64 flag (along with `-warn_duplicate_libraries`), where
the warning is enabled by default, and it can be useful to ignore since
it can be hard to dedup library flags across large builds. This doesn't
ignore the enabling version since if someone manually passed that and
lld didn't respect it, we probably want the user to know that.
2024-03-28 09:41:08 -07:00
Fangrui Song
2763353891 [Object,ELFType] Rename TargetEndianness to Endianness (#86604)
`TargetEndianness` is long and unwieldy. "Target" in the name is confusing. Rename it to "Endianness".

I cannot find noticeable out-of-tree users of `TargetEndianness`, but
keep `TargetEndianness` to make this patch safer. `TargetEndianness`
will be removed by a subsequent change.
2024-03-28 09:10:34 -07:00
Dmitri Gribenko
28b196e7fc [llvm] Write temporary test files into %t
... instead of the source tree
2024-03-28 11:55:46 +01:00
Fangrui Song
070d7af0c5 [ELF] --export-dynamic: don't create dynamic sections for non-PIC static links
The CloudABI (removed from Clang Driver) change from
https://reviews.llvm.org/D29982 does not make sense. GNU ld and gold
don't create dynamic sections for a non-PIC static link when
--export-dynamic is specified.

Creating dynamic sections is harmful in this scenario because we would
consider undefined weak symbols preemptible and generate GLOB_DAT
relocations, breaking the expectation that non-PIC static links only
contain IRELATIVE relocations.

In addition, there are other options that export symbols
(--export-dynamic-symbol, --dynamic-list, etc). It does not make sense
to special case --export-dynamic.
2024-03-27 20:04:59 -07:00
Fangrui Song
443baed56c [ELF,test] Update tests that depend on --export-dynamic creating dynamic sections
The CloudABI change from https://reviews.llvm.org/D30175 does not make sense.
Update tests not to rely on the --export-dynamic behavior.
2024-03-27 20:01:30 -07:00
alx32
bbfa50696e [lld-macho] Fix bug in makeSyntheticInputSection when -dead_strip flag is specified (#86878)
Previously, `makeSyntheticInputSection` would create a new
`ConcatInputSection` without setting `live` explicitly for it. Without
`-dead_strip` this would be OK since `live` would default to `true`.
However, with `-dead_strip`, `live` would default to false, and it would
remain set to `false`.
This hasn't resulted in any issues so far since no code paths that
exposed this issue were present.
However a recent change - ObjC relative method lists
(https://github.com/llvm/llvm-project/pull/86231) exposes this issue by
creating relocations to the `SyntheticInputSection`.
When these relocations are attempted to be written, this ends up with a
crash(assert), since the `SyntheticInputSection` they refer to is marked
as dead (`live` = `false`).

With this change, we set the correct behavior - `live` will always be
`true`. We add a test case that before this change would trigger an
assert in the linker.
2024-03-27 17:27:51 -07:00
alx32
742a82a729 [lld-macho] Implement support for ObjC relative method lists (#86231)
The MachO format supports relative offsets for ObjC method lists. This
support is present already in ld64. With this change we implement this
support in lld also.

Relative method lists can be identified by a specific flag (0x80000000)
in the method list header. When this flag is present, the method list
will contain 32-bit relative offsets to the current Program Counter
(PC), instead of absolute pointers.
Additionally, when relative method lists are used, the offset to the
selector name will now be relative and point to the selector reference
(selref) instead of the name itself.
2024-03-27 14:34:27 -07:00
Fangrui Song
acdba090f2 [ELF] Change duplicate symbol errors to use errorOrWarn
so that --noinhibit-exec downgrades the error to a warning, which
matches GNU ld. Most recoverable errors should use errorOrWarn.
2024-03-27 14:08:59 -07:00
Fangrui Song
c335accb07 [ELF] --pack-dyn-relocs=android+relr: place IRELATIVE in .rela.plt (#86751)
Current Bionic processes relocations in this order:

* DT_ANDROID_REL[A]
* DT_RELR
* DT_REL[A]
* DT_JMPREL

If an IRELATIVE relocation is in DT_ANDROID_REL[A], it would read
unrelocated (incorrect) global variables associated with RELR when
--pack-dyn-relocs=android+relr is enabled. Work around this by placing
IRELATIVE in .rel[a].plt (DT_JMPREL).

Link: https://r.android.com/3014185
2024-03-27 09:47:16 -07:00
Jacek Caban
cc23ee8250 [LLD][COFF] Add support for EXPORTAS import name type. (#86541)
#78772 added similar support for .def file parser and import library
writer. This PR adds missing bits in LLD to propagate EXPORTAS name and
allow it in `/export` parser. This is syntax is used by MSVC for ARM64EC
`__declspec(dllexport)` handling.
2024-03-27 11:37:02 +01:00
Jacek Caban
603db7425f [LLD][COFF] Preserve all attributes from forwarding exports from parsed .def files. (#86564)
It's similar to #86535, but for export specified in .def files.
2024-03-26 13:18:09 +01:00
Jacek Caban
bfb12ef33a [LLD][COFF] Allow additional attributes in forwarding exports. (#86535)
Testing with MSVC link.exe showed that it respects such options, while
LLD currently discards them.
2024-03-26 13:07:13 +01:00
Sam Clegg
3e046ee0c9 [lld][ELF] Add --why-extract for bitcode libcalls (#78781)
The Wasm linker already records these and its seems useful to do so.
2024-03-25 21:50:00 -07:00
Fangrui Song
7196518f1f [ELF,test] Improve -r incompatibility tests 2024-03-25 21:46:57 -07:00
Parth Arora
ebb326a51f [ELF] Fix unnecessary inclusion of unreferenced provide symbols
Previously, linker was unnecessarily including a PROVIDE symbol which
was referenced by another unused PROVIDE symbol. For example, if a
linker script contained the below code and 'not_used_sym' provide symbol
is not included, then linker was still unnecessarily including 'foo' PROVIDE
symbol because it was referenced by 'not_used_sym'. This commit fixes
this behavior.

PROVIDE(not_used_sym = foo)
PROVIDE(foo = 0x1000)

This commit fixes this behavior by using dfs-like algorithm to find
all the symbols referenced in provide expressions of included provide
symbols.

This commit also fixes the issue of unused section not being garbage-collected
if a symbol of the section is referenced by an unused PROVIDE symbol.

Closes #74771
Closes #84730

Co-authored-by: Fangrui Song <i@maskray.me>
2024-03-25 16:11:21 -07:00
Fangrui Song
f1f06f31b8 [ELF] Move parse files from Driver.cpp to InputFiles.cpp. NFC
Fixes: 36146d2b6b

When `doParseFile template defintion` in InputFiles.cpp is optimized
out, we will get a link failure. Actually, we can move the file parsing
loop from Driver.too to InputFiles.cpp and merge it with
parseArmCMSEImportLib.
2024-03-25 16:02:34 -07:00
Fangrui Song
dd7151094f [ELF] Simplify parseArmCMSEImportLib. NFC 2024-03-25 15:46:52 -07:00
Daniil Kovalev
f3f594c6b5 [NFC][lld] Pass sym as const reference to GotSection's methods (#86481) 2024-03-25 20:25:42 +03:00
alx32
e1a003dbbd [lld-macho][NFC] Refactor ObjCSelRefsSection => ObjCSelRefsHelper (#86456)
In a previous PR: https://github.com/llvm/llvm-project/pull/83878, the
intent was to make no functional changes, just refactor out the code for
reuse.
However, by creating `ObjCSelRefsSection` as a `SyntheticSection` - this
slightly changed the functionality of the application as the
`SyntheticSection` constructor registers the `SyntheticSection` as a
functional one - with an associated `SyntheticInputSection`.

With this change we remove this unintended consequence by making the
code not use a `SyntheticSection` as base, but just by having it be a
static helper.
2024-03-25 06:55:11 -07:00
Fangrui Song
18a49f03aa [ELF] Merge relaIplt into relaDyn
`relaIplt` was added so that IRELATIVE relocations are placed at the end
of .rela.dyn (since https://reviews.llvm.org/D65651) or .rela.plt
(--pack-dyn-relocs=android[+relr]). Unfortunately, handling `relaIplt`
requires special cases all over the code base. We can extend
partitionRels/computeRels to partition both RELATIVE and IRELATIVE
relocations, rendering `relaIplt` unneeded.

The change allows IRELATIVE relocations in the DT_ANDROID_REL[A] table
(untested?!), which may be processed before other types of relocations.
This seems acceptable for Bionic's DEFINE_IFUNC_FOR use cases.

In addition, this change simplies changing .rel[a].dyn to a compact
relocation format (CREL).

SHF_INFO_LINK is removed from .rel[a].dyn with IRELATIVE relocations.
(See https://reviews.llvm.org/D89828).
2024-03-24 14:07:09 -07:00
Arthur Eubanks
4804805132 [lld/ELF][X86] Respect outSecOff when checking if GOTPCREL can be relaxed (#86334)
The existing implementation didn't handle when the input text section
was some offset from the output section.

This resulted in an assert in relaxGot() with an lld built with asserts
for some large binaries, or even worse, a silently broken binary with an
lld without asserts.
2024-03-24 11:43:15 -06:00
Fangrui Song
691b97c884 [ELF] Remove zero-value DT_JMPREL when IPLT is present while PLT isn't
The zero-value DT_JMPREL is benign but not needed.
This is also code simplification available after https://reviews.llvm.org/D65651
2024-03-23 01:02:08 -07:00
Paul Kirth
dfe4ca9b7f [RISCV][lld] Set the type of TLSDESC relocation's referenced local symbol to STT_NOTYPE
When adding fixups for RISCV_TLSDESC_ADD_LO and RISCV_TLSDESC_LOAD_LO,
the local label added for RISCV TLSDESC relocations have STT_TLS set,
which is incorrect. Instead, these labels should have `STT_NOTYPE`.

This patch stops adding such fixups and avoid setting the STT_TLS on
these symbols. Failing to do so can cause LLD to emit an error `has an
STT_TLS symbol but doesn't have an SHF_TLS section`. We additionally,
adjust how LLD services these relocations to avoid errors with
incompatible relocation and symbol types.

Reviewers: topperc, MaskRay

Reviewed By: MaskRay

Pull Request: https://github.com/llvm/llvm-project/pull/85817
2024-03-22 12:27:41 -07:00
Amir Ayupov
f66d631bf8 Revert "[BOLT] Add BB index to BAT (#86044)"
This reverts commit 3b3de48fd8.
2024-03-22 08:38:40 -07:00
Amir Ayupov
3b3de48fd8 [BOLT] Add BB index to BAT (#86044) 2024-03-22 06:07:17 -07:00
alx32
e4a672ef85 [lld][macho] Fix gcc category merging warning (#86091)
Fixing gcc warning regarding creating non-null-terminated string:
```
../../lld/MachO/ObjC.cpp:1226:10: warning: 'char* strncpy(char*, const char*, size_t)' output truncated before terminating nul copying as many bytes from a string as its length [-Wstringop-truncation]
 1226 |   strncpy(strData, str, len);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~
../../lld/MachO/ObjC.cpp: In member function 'void {anonymous}::ObjcCategoryMerger::emitAndLinkPointerList(lld::macho::Defined*, uint32_t, const {anonymous}::ObjcCategoryMerger::ClassExtensionInfo&, const {anonymous}::ObjcCategoryMerger::PointerListInfo&)':
../../lld/MachO/ObjC.cpp:1223:24: note: length computed here
 1223 |   uint32_t len = strlen(str);
      |                  ~~~~~~^~~~~
```
This is not actually a bug, as `newSectionData` returns a
zero-initialized memory region, so the null terminator will be there.
2024-03-21 14:53:09 -07:00
alx32
b609a4d7ea [lld-macho][NFC] Refactor insertions into inputSections (#85692)
Before this change, after `InputSection` objects are created, they need
to be added to the appropriate container for tracking.
The logic for selecting the appropriate container lives in `Driver.cpp`
/ `gatherInputSections`, where the `InputSection` is added to the
matching container depending on the input config and the type of
`InputSection`.

Also, multiple other locations also insert directly into `inputSections`
array - assuming that that is the appropriate container for the
`InputSection`'s they create. Currently this is the correct assumption,
however an upcoming feature will change this.

For an upcoming feature (relative method lists), we need to route
`InputSection`'s either to `inputSections` array or to a synthetic
section, depending on weather the relative method list optimization is
enabled or not.

We can achieve the above either by duplicating some of the logic or
refactoring the routing and `InputSection`'s and reusing that.

The refactoring & code sharing approach seems the correct way to go - as
such this diff performs the refactoring while not introducing any
functional changes. Later on we can just call `addInputSection` and not
have to worry about routing logic.

---------
2024-03-21 14:50:44 -07:00
chrulski-intel
276283d864 [LLD] [MinGW] Implement the -lto-sample-profile option (#85841)
This has been a supported option for ELF and is added to the COFF Linker
in #85701
2024-03-21 21:34:01 +08:00
Paul Kirth
f6f474c4ef [llvm][lld] Pre-commit tests for RISCV TLSDESC symbols
Currently, we mistakenly mark the local labels used in RISC-V TLSDESC as
TLS symbols, when they should not be. This patch adds tests with the
current incorrect behavior, and subsequent patches will address the
issue.

Reviewers: MaskRay, topperc

Reviewed By: MaskRay

Pull Request: https://github.com/llvm/llvm-project/pull/85816
2024-03-20 13:39:39 -07:00