16860 Commits

Author SHA1 Message Date
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
Fangrui Song
a7e8bddfc1 [ELF] Respect --sysroot for INCLUDE
If an included script is under the sysroot directory, when it opens an
absolute path file (`INPUT` or `GROUP`), add sysroot before the absolute
path. When the included script ends, the `isUnderSysroot` state is
restored.
2024-07-28 11:43:27 -07:00
Fangrui Song
30fa011413 [ELF,test] Improve --sysroot and GROUP tests
3i.t (INCLUDE "%t.dir/3a.t") describes a behavior difference from GNU
ld, which will be fixed by the next change.
2024-07-28 11:40:14 -07:00
Fangrui Song
a4921f10e0 [ELF] Output section phdr: support quoted names 2024-07-27 17:40:51 -07:00
Fangrui Song
9c16a4a2dc [ELF] INSERT [AFTER|BEFORE]: support quoted names 2024-07-27 17:34:37 -07:00
Fangrui Song
8f72b0cb08 [ELF] Fix INCLUDE cycle detection
Fix #93947: the cycle detection mechanism added by
https://reviews.llvm.org/D37524 also disallowed including a file twice,
which is an unnecessary limitation.

Now that we have an include stack #100493, supporting multiple inclusion
is trivial. Note: a filename can be referenced with many different
paths, e.g. a.lds, ./a.lds, ././a.lds. We don't attempt to detect the
cycle in the earliest point.
2024-07-27 17:25:13 -07:00
Fangrui Song
aad2238f78 [ELF] Improve INCLUDE cycle tests
And demonstrate the incorrect diagnostic when a linker script is
included multiple times (#93947).
2024-07-27 17:14:53 -07:00
Fangrui Song
4ad3deeefc [ELF] PHDRS: test EOF without ; 2024-07-27 16:56:27 -07:00
Fangrui Song
dbd65a07f2 [ELF] OUTPUT_ARCH: report unclosed error 2024-07-27 16:52:47 -07:00
Fangrui Song
74f843d05f [ELF] Replace unquote(next()) with readName. NFC 2024-07-27 16:47:18 -07:00
Fangrui Song
0d8bc10acb [ELF] Memory region: support quoted names 2024-07-27 16:39:15 -07:00
Fangrui Song
e689515491 [ELF] OVERLAY: support quoted output section names 2024-07-27 16:33:18 -07:00
Fangrui Song
74ef53a01a [ELF] REGION_ALIAS: support quoted names 2024-07-27 16:29:43 -07:00
Fangrui Song
c89566f317 [ELF] Replace unquote(next()) with readName. NFC 2024-07-27 16:27:05 -07:00
Fangrui Song
30ec2bf58d [ELF] PROVIDE: allow quoted names to be discarded
Extend commit ebb326a51fec37b5a47e5702e8ea157cd4f835cd for (#74771) to
support quoted names, e.g. `PROVIDE("f1" = f2 + f3);`.
2024-07-27 16:19:57 -07:00
Fangrui Song
edcc60e403 [ELF] Simplify readAssignment
After #100493, the `=` support from
fe0de25b2195b66d1ebac5d3ebdb18f9e1e776da can be simplified.
2024-07-27 16:04:38 -07:00
Fangrui Song
9328c20cc8 [ELF] Track line number precisely
`getLineNumber` is both imprecise (when `INCLUDE` is used) and
inefficient (see https://reviews.llvm.org/D104137). Track line number
precisely now that we have `struct Buffer` abstraction from #100493.
2024-07-27 14:46:41 -07:00
Hongyu Chen
f1a7d146e0
[ELF] Updated some while conditions with till (#100893)
This change is based on
[commit](b32c38ab5b)
for a cleaner API usage. Thanks to @MaskRay !
2024-07-27 14:16:12 -07:00
Fangrui Song
c7231e4909 [ELF] Use invokeOnRelocs. NFC 2024-07-27 10:58:27 -07:00
Fangrui Song
6efc3774bd [ELF] Add Relocs and invokeOnRelocs. NFC
Relocs is to simplify CREL support (#98115) while invokeOnRelocs
simplifies some relsOrRelas call sites that will use the CREL iterator.
2024-07-27 10:55:17 -07:00
Fangrui Song
b32c38ab5b [ELF] Replace some while (peek() != ")" && !atEOF()) with till 2024-07-26 17:25:23 -07:00
Fangrui Song
10bb296dfc [ELF] Replace some while (peek() != ")" && !atEOF()) with till 2024-07-26 17:19:04 -07:00
Fangrui Song
2a89356d64 [ELF] Add till and rewrite while (... consume("}"))
After #100493, the idiom `while (!errorCount() && !consume("}"))` could
lead to inaccurate diagnostics or dead loops. Introduce till to change
the code pattern.
2024-07-26 17:13:37 -07:00
Fangrui Song
6cf1ea99c6 [ELF,test] Improve unclosed tests 2024-07-26 16:51:42 -07:00
Fangrui Song
4f5ad22b95 [ELF,test] Improve PHDRS tests 2024-07-26 15:55:01 -07:00
Fangrui Song
1978c21d96
[ELF] ScriptLexer: generate tokens lazily
The current tokenize-whole-file approach has a few limitations.

* Lack of state information: `maybeSplitExpr` is needed to parse
  expressions. It's infeasible to add new states to behave more like GNU
  ld.
* `readInclude` may insert tokens in the middle, leading to a time
  complexity issue with N-nested `INCLUDE`.
* line/column information for diagnostics are inaccurate, especially
  after an `INCLUDE`.
* `getLineNumber` cannot be made more efficient without significant code
  complexity and memory consumption. https://reviews.llvm.org/D104137

The patch switches to a traditional lexer that generates tokens lazily.

* `atEOF` behavior is modified: we need to call `peek` to determine EOF.
* `peek` and `next` cannot call `setError` upon `atEOF`.
* Since `consume` no longer reports an error upon `atEOF`, the idiom `while (!errorCount() && !consume(")"))`
  would cause a dead loop. Use `while (peek() != ")" && !atEOF()) { ... } expect(")")` instead.
* An include stack is introduced to handle `readInclude`. This can be
  utilized to address #93947 properly.
* `tokens` and `pos` are removed.
* `commandString` is reimplemented. Since it is used in -Map output,
  `\n` needs to be replaced with space.

Pull Request: https://github.com/llvm/llvm-project/pull/100493
2024-07-26 14:26:38 -07:00
wanglei
0057a969a2
[lld][ELF][LoongArch] Support R_LARCH_TLS_{LD,GD,DESC}_PCREL_S2
Reviewed By: MaskRay, SixWeining

Pull Request: https://github.com/llvm/llvm-project/pull/100105
2024-07-26 14:38:36 +08:00
Fangrui Song
8644a2aa0f [ELF,test] Improve negative linker script tests 2024-07-25 17:11:52 -07:00
Fangrui Song
026972af9c [ELF] Remove obsoleted comment after #99567 2024-07-25 16:45:09 -07:00
Fangrui Song
364ee4ee36 [lld-macho,test] Avoid writing to CWD
Don't write to `filelist` in the current working directory, whigh might
be read-only in some testing environmens.
2024-07-25 15:12:39 -07:00
Scott Todd
4f8050806e
[lld] Add explicit conversion for enum to Twine. (#100627)
This fixes `error: ambiguous conversion for functional-style cast from
'lld::macho::InputSection::Kind' to 'llvm::Twine'`, observed when
building with clang-9 and reported here:
https://github.com/llvm/llvm-project/pull/96268#discussion_r1691909931.
2024-07-25 11:37:31 -07:00
David Blaikie
74e14605d7 Fix test to write to %T instead of the current working directory (which may not be writeable) 2024-07-24 22:00:59 +00:00
Sam Elliott
ea222be0d9
[MC] Honour alignment directive fill value for non-intel (#100136)
As reported in https://llvm.org/PR30955, `.balign` with a fill-value of 0 did
not actually align using zeroes, on non-x86 targets.

This is because the check of whether to use the code alignment routines
or whether to just use the fill value was checking whether the fill
value was equal to `TextAlignFillValue`, which has not been changed from
its default of 0 on most targets (it has been changed for x86). However,
most targets do not set the fill value because it doesn't entirely make
sense -- i.e. on AArch64 there's no reasonable byte value to use for
alignment, as instructions are word-sized and have to be well-aligned.

I think the check at the end `AsmParser::parseDirectiveAlign` is
suspicious even on x86 - if you use `.balign <align>, 0x90` in a code
section, you don't end up with a block of `0x90` repeated, you end up
with a block of NOPs of various widths. This functionality is never
tested.

The fix here is to modify the check to ignore the default text align
fill value when choosing to do code alignment or not.

Fixes #30303
2024-07-24 18:24:39 +01:00
Hongyu Chen
2ae862b74b
[ELF] Remove consumeLabel in ScriptLexer (#99567)
This commit removes `consumeLabel` since we can just use consume
function to have the same functionalities.
2024-07-23 22:03:46 -07:00
Reid Kleckner
7aa6598a24 [COFF] Add help text for /time flag to make it visible in /? output
Part of #99011
2024-07-23 22:11:53 +00:00
Fangrui Song
298a9223a5
[ARM] Create mapping symbols with non-unique names
Similar to #99836 for AArch64.

Non-unique names save .strtab space and match GNU assembler.

Pull Request: https://github.com/llvm/llvm-project/pull/99906
2024-07-23 09:44:00 -07:00
Ellis Hoag
e3b30bc553
[lld][InstrProf] Profile guided function order (#96268)
Add the lld flags `--irpgo-profile-sort=<profile>` and
`--compression-sort={function,data,both}` to order functions to improve
startup time, and functions or data to improve compressed size,
respectively.

We use Balanced Partitioning to determine the best section order using
traces from IRPGO profiles (see
https://discourse.llvm.org/t/rfc-temporal-profiling-extension-for-irpgo/68068
for details) to improve startup time and using hashes of section
contents to improve compressed size.

In our recent LLVM talk (https://www.youtube.com/watch?v=yd4pbSTjwuA),
we showed that this can reduce page faults during startup by 40% on a
large iOS app and we can reduce compressed size by 0.8-3%.

More details can be found in https://dl.acm.org/doi/10.1145/3660635

---------

Co-authored-by: Vincent Lee <thevinster@users.noreply.github.com>
2024-07-23 08:34:40 -07:00
Leonard Grey
58f3c5e696
[lld-macho] Fix thunks for non-__text TEXT sections (#99052)
This supersedes https://github.com/llvm/llvm-project/pull/87818 and
fixes https://github.com/llvm/llvm-project/issues/52767

When calculating arm64 thunks, we make a few assumptions that may not
hold when considering code sections outside of `__text`:

1. That a section needs thunks only if its size is larger than the
branch range.
2. That any calls into `__stubs` are necessarily forward jumps (that is,
the section with the jump is ordered before `__stubs`)

Sections like this exist in the wild, most prominently the
`__lcxx_overrides` section introduced in
https://github.com/llvm/llvm-project/pull/69498

This change:
- Ensures that if one section in `__TEXT` gets thunks, all of them do.
- Makes all code sections in `__TEXT` contiguous (and guaranteed to be
placed before `__stubs`)
2024-07-23 11:02:55 -04:00
Tobias Hieta
10c6d6349e
Clear release notes for upcoming LLVM 20 dev cycle 2024-07-23 11:04:06 +02:00
Daniel Bertalan
9a8b0407fc
Reapply "[lld] enable fixup chains by default (#79894)" (#99255)
This reverts commit f55b79f59a77b4be586d649e9ced9f8667265011.

The known issues with chained fixups have been addressed by #98913,
#98305, #97156 and #95171.

Compared to the original commit, support for xrOS (which postdates
chained fixups' introduction) was added and an unnecessary test change
was removed.

----------
Original commit message:

Enable chained fixups in lld when all platform and version criteria are
met. This is an attempt at simplifying the logic used in ld 907:


93d74eafc3/src/ld/Options.cpp (L5458-L5549)

Some changes were made to simplify the logic:
- only enable chained fixups for macOS from 13.0 to avoid the arch check
- only enable chained fixups for iphonesimulator from 16.0 to avoid the
arch check
- don't enable chained fixups for not specifically listed platforms
- don't enable chained fixups for arm64_32
2024-07-22 22:03:32 +02:00
Fangrui Song
4010ddf780
[MC,AArch64] Create mapping symbols with non-unique names
Add `createLocalSymbol` to create a local, non-temporary symbol.
Different from `createRenamableSymbol`, the `Used` bit is ignored,
therefore multiple local symbols might share the same name.

Utilizing `createLocalSymbol` in AArch64 allows for efficient mapping
symbol creation with non-unique names, saving .strtab space.
The behavior matches GNU assembler.

Pull Request: https://github.com/llvm/llvm-project/pull/99836
2024-07-22 09:03:05 -07:00
Vikash Gupta
45c0decdda
[LLD] [COFF] Zero-intialization & proper constructor invocation in COFF's Symbol (#98447)
It happened due to lld's COFF linker multiple regression tests failure.
It got reliably reproduced after the needed intialization of
isUsedinRegularObject bit in the Symbol's ctor, but not handled at
replaceSymbol API properly while creating a specific symbol to insert in
symbol table.

So, now while creating the specific symbol using replaceSymbol, by
explicitly setting the value of isUsedinRegularObject to newly created
symbol around the ctor call of symbol would solve the regression failure
2024-07-22 15:42:15 +05:30
Brandon Wu
2873edd286
[lld][RISCV] Add break to nested switch in mergeAtomic (#99762)
This prevent the warnings from compiler.
2024-07-22 14:31:28 +08:00
Daniel Bertalan
f18fd6e3f9
[lld-macho] Use parallel algorithms in favor of ThreadPool (#99471)
In https://reviews.llvm.org/D115416, it was decided that an explicit
thread pool should be used instead of the simpler fork-join model of the
`parallelFor*` family of functions. Since then, more parallelism has
been added to LLD, but these changes always used the latter strategy,
similarly to other ports of LLD.

This meant that we ended up spawning twice the requested amount of
threads; one set for the `llvm/Support/Parallel.h` executor, and one for
the thread pool.

Since that decision, 3b4d800911 has landed, which allows us to
explicitly enqueue jobs on the executor pool of the parallel algorithms,
which should be enough to achieve sharded output writing and
parallelized input file parsing. Now only the construction of the map
file is left that should be done *concurrently* with different linking
steps, this commit proposes explicitly spawning a dedicated worker
thread for it.
2024-07-22 08:13:07 +02:00
Hongyu Chen
b828c13f3c
[ELF] Delete peek2 in Lexer (#99790)
Thanks to Fangrui's change

28045ceab0
so peek2 can be removed.
2024-07-20 16:35:38 -07:00
Fangrui Song
ba9b5ff4f7 [ELF,test] Fix RUN line issue in defsym.s 2024-07-20 16:10:57 -07:00
Fangrui Song
efa833dd0f [ELF] Simplify readExpr. NFC 2024-07-20 14:36:55 -07:00
Fangrui Song
c93554b82a [ELF] Simplify ScriptLexer::consume. NFC 2024-07-20 14:23:54 -07:00