Commit Graph

691 Commits

Author SHA1 Message Date
Andrew Ng
cf20a2685e [DTLTO][Clang][LLD] Fix DTLTO for multi-call LLVM driver toolchain (#162456)
Add DTLTO linker option `--thinlto-remote-compiler-prepend-arg` to
enable support for the multi-call LLVM driver that requires an
additional option to specify the subcommand, e.g. "llvm clang ...".

Fixes https://github.com/llvm/llvm-project/issues/159125.
2025-10-23 16:34:28 +01:00
Andrew Ng
d68f0c2e1c [DTLTO][LLD] Tidy up DTLTO related options (NFC) (#161675)
Change LLD DTLTO option definitions to match actual option name.
2025-10-02 18:30:51 +01:00
Nico Weber
b5299210f2 [lld-link] Do not assert when reporting error about non-thin archive (#159828)
Follow-up to https://reviews.llvm.org/D57974, which added calls to
Archive::Child::getFullName() to produce strings in errors.

But getFullName() is only valid on thin archives, and should only be
used to open the file the archive points to. For diagnostics, getName()
is better: It works for both thin and non-thin files, and it doesn't
make a very long string for thin files. And we already prepend the name
of the parent archive file anyways.
2025-09-20 14:13:46 -04:00
kkent030315
67244da08e [LLD][COFF] Add /nodbgdirmerge to control debug directory section (#159235)
Resolves #141712.

As described in the issue, this PR adds support for `/nodbgdirmerge`
flag in LLD to align with MS link. When the flag is specified, the
linker will emit the debug directory section in `.cvinfo` section,
instead of merging it to the `.rdata`. The flag will be ignored on
MinGW.

---------

Co-authored-by: namazso <admin@namazso.eu>
2025-09-20 00:20:32 +03:00
Alexandre Ganea
cb00c2d74b [LLD][COFF] Display the size of all consumed inputs with /summary (take 2) (#157284)
When `/summary` is used, we now also display the cumulative size of all
input OBJ files, including those pulled from archives. Lazy OBJ files
that were not pulled in are not accounted for.

Also added separators between digit groups, to make the output more
bearable.

Example output:
```
> lld-link ... /summary
                                    Summary
--------------------------------------------------------------------------------
               4,958 Input OBJ files (expanded from all cmd-line inputs)
      46,715,790,512 Size of all consumed OBJ files (non-lazy), in bytes
                  42 PDB type server dependencies
                   0 Precomp OBJ dependencies
         293,910,064 Input type records
      16,931,361,928 Size of all input type records, in bytes
          11,201,549 Merged TPI records
           2,765,494 Merged IPI records
              38,649 Output PDB strings
          21,512,230 Global symbol records
          82,380,837 Module symbol records
             715,313 Public symbol records
```
I've skipped over the exact amounts for "Size of all consumed inputs
(non-lazy)" in the unit tests, since the sizes of OBJ files can
fluctuate between compilers.

_(this is a reopening of
https://github.com/llvm/llvm-project/pull/157279 which wasa committed by
mistake)_
2025-09-08 08:51:00 -04:00
Alexandre Ganea
c062fa64a6 Revert "[LLD][COFF] Display the size of all consumed inputs with /summary" (#157282)
Reverts llvm/llvm-project#157279
2025-09-06 16:09:06 +00:00
kkent030315
1c0ca8afc6 [LLD][COFF] Add more variety of CET and hotpatch flags (#150761)
Those are all MS link.exe compatible flags.

### CET (Control-flow Enforcement Technology) family
- Added LLD test that covers `/cetcompat[:no]`
- Added `/cetcompatstrict[:no]` flag in LLD/COFF
- Added `/cetipvalidationrelaxed[:no]` flag in LLD/COFF
- Added `/cetdynamicapisinproc[:no]` flag in LLD/COFF

### Misc
- Added `/hotpatchcompatible[:no]` flag in LLD/COFF
- This flag requires at least 6 bytes of function padding
(`/functionpadmin:#`) as per link.exe
2025-09-06 11:56:01 -04:00
Alexandre Ganea
7eb889a8c6 [LLD][COFF] Display the size of all consumed inputs with /summary (#157279)
When `/summary` is used, we now also display the cumulative size of all
input OBJ files, including those pulled from archives. Lazy OBJ files
that were not pulled in are not accounted for.

Also added separators between digit groups, to make the output more
bearable.

Example output:
```
> lld-link ... /summary
                                    Summary
--------------------------------------------------------------------------------
               4,958 Input OBJ files (expanded from all cmd-line inputs)
      46,715,790,512 Size of all consumed OBJ files (non-lazy), in bytes
                  42 PDB type server dependencies
                   0 Precomp OBJ dependencies
         293,910,064 Input type records
      16,931,361,928 Size of all input type records, in bytes
          11,201,549 Merged TPI records
           2,765,494 Merged IPI records
              38,649 Output PDB strings
          21,512,230 Global symbol records
          82,380,837 Module symbol records
             715,313 Public symbol records
```

We skip over the exact amounts for "Size of all consumed inputs
(non-lazy)" in the unit tests, since the sizes of OBJ files can
fluctuate between compilers.
2025-09-06 15:53:34 +00:00
kkent030315
119d507afc [LLD][COFF] Add support for custom section layout (#152779)
MS link.exe provides the `/sectionlayout:@` option to specify the order
of output sections at the granularity of individual sections. LLD/COFF
currently does not have capability for user-controlled ordering of one
or more output sections (as LLD/COFF does not support linker scripts),
and this PR adds the option to align with MS link.exe.

The option accepts only a file that specifies the order of sections, one
per line. For example, `mylayout.txt` could emit the `.text` section
after all other sections while preserving the original relative order of
the remaining sections.

```
.data
.rdata
.pdata
.rsrc
.reloc
.text
```

```bash
echo 'int main() { return 0; }' > main.c
cl main.c /link /entry:main /sectionlayout:@mylayout.txt
llvm-readobj --sections main.exe
```
2025-09-04 23:06:38 +03:00
Jacek Caban
7223f67ffc [LLD][COFF] Don't resolve weak aliases when performing local import (#152000)
Fixes crashes reported in #151255.

The alias may have already been stored for later resolution, which can
lead to treating a resolved alias as if it were still undefined.
Instead, use the alias target directly for the import.

Also extended the test to make reproducing the problem more likely, and
added an assert that catches the issue.
2025-08-05 00:21:04 +02:00
Jacek Caban
dcc71f22ca [LLD][COFF] Add support for ARM64X same-address thunks (#151255)
Fixes MSVC CRT thread-local constructors support on hybrid ARM64X
targets.

`-arm64xsameaddress` is an undocumented option that ensures the
specified function has the same address in both native and EC views of
hybrid images. To achieve this, the linker emits additional thunks and
replaces the symbols
of those functions with the thunk symbol (the same thunk is used in both
views). The thunk code jumps to the native function (similar to range
extension thunks), but additional ARM64X relocations are emitted to
replace the target with the EC function in the EC view.

MSVC appears to generate thunks even for non-hybrid ARM64EC images. As a
side effect, the native symbol is pulled in. Since this is used in the
CRT for thread-local constructors, it results in the image containing
unnecessary native code. Because these thunks do not appear to be useful
in that context, we limit this behavior to actual hybrid targets. This
may change if compatibility requires it.

The tricky part is that thunks should be skipped if the symbol is not
live in either view, and symbol replacement must be reflected in weak
aliases. This requires thunk generation to happen before resolving weak
aliases but after the GC pass. To enable this, the `markLive` call was
moved earlier, and the final weak alias resolution was postponed until
afterward. This requires more code to be aware of weak aliases, which
previously could assume they were already resolved.
2025-07-31 13:17:36 +02:00
Jacek Caban
aac70d69f6 [LLD][COFF] Introduce Symbol::getDefined helper. (NFC) (#151253) 2025-07-31 12:58:45 +02:00
Jacek Caban
38cd66a6ce [LLD][COFF] Move resolving alternate names to SymbolTable (NFC) (#149495) 2025-07-28 17:02:49 +02:00
bd1976bris
9f733f4324 [LLD][COFF] Make /wholearchive thin-archive member identifiers consistent (#145487)
A thin archive is an archive/library format where the archive itself
contains only references to member object files on disk, rather than
embedding the file contents.

For the non-/wholearchive case, we use the path to the archive member as
the identifier for thin-archive members (see comments in
`enqueueArchiveMember`). This patch modifies the /wholearchive path to
behave the same way.

Apart from consistency, my motivation for fixing this is DTLTO
(#126654), where having the member identifier be the path on disk allows
distribution of bitcode members during ThinLTO.
2025-07-22 16:57:20 +01:00
Alexandre Ganea
fcacd4e880 [LLD][COFF] Follow up comments on pr146610 (#147152)
This is a follow-up PR for post-commit comments in
https://github.com/llvm/llvm-project/pull/146610

- Changed "exporteddllmain" references to "importeddllmain".
- Add support for x86 target and test coverage.
- Changed a comment to better express why we're skipping importing
`DllMain`.
2025-07-21 16:12:37 -04:00
bd1976bris
bbbbc093fe [DTLTO][LLD][COFF] Add support for Integrated Distributed ThinLTO (#148594)
This patch introduces support for Integrated Distributed ThinLTO (DTLTO)
in COFF LLD.

DTLTO enables the distribution of ThinLTO backend compilations via
external distribution systems, such as Incredibuild, during the
traditional link step: https://llvm.org/docs/DTLTO.html.

Note: Bitcode members of non-thin archives are not currently supported.
This will be addressed in a future change. This patch is sufficient to
allow for self-hosting an LLVM build with DTLTO if thin archives are
used.

Testing:
- LLD `lit` test coverage has been added, using a mock distributor to
avoid requiring Clang.
- Cross-project `lit` tests cover integration with Clang.

For the design discussion of the DTLTO feature, see:
https://github.com/llvm/llvm-project/pull/126654
2025-07-20 14:47:00 +01:00
Alexandre Ganea
e63de82d90 [LLD][COFF] Disallow importing DllMain from import libraries (#146610)
This is a workaround for
https://github.com/llvm/llvm-project/issues/82050 by skipping the `DllMain` symbol if seen in aimport library. If this situation occurs, after this commit a warning will also be displayed. The warning can be silenced with `/ignore:exporteddllmain`
2025-07-02 08:53:18 -04:00
Fabrice de Gans
85d250c96e Use the Windows SDK arguments over the environment (#144805)
If any of the Windows SDK (and MSVC)-related argument is passed in the
command line, they should take priority over the environment variables
like `INCLUDE` or `LIB` set by vcvarsall from the Visual Studio
Developer Environment on Windows.

These changes ensure that all of the arguments related to VC Tools and
the Windows SDK cause the driver to ignore the environment.
2025-06-24 09:39:11 -07:00
Kazu Hirata
19f00c0570 [lld] Remove unused includes (NFC) (#141421) 2025-05-25 10:55:39 -07:00
Jacek Caban
d5da557782 [LLD][COFF] Allow -arm64xsameaddress in ARM64EC directives (#139631)
Make it a no-op for now, which is sufficient for non-hybrid images.

Fixes #131712.
2025-05-15 12:28:18 +02:00
Jacek Caban
5b0572875c [LLD][COFF] Add support for including native ARM64 objects in ARM64EC images (#137653)
MSVC linker accepts native ARM64 object files as input with
`-machine:arm64ec`, similar to `-machine:arm64x`. Its usefulness is very
limited; for example, both exports and imports are not reflected in the
PE structures and can't work. However, their symbol tables are otherwise
functional.

Since we already have handling of multiple symbol tables implemented for
ARM64X, the required changes are mostly about adjusting relevant checks
to account for them on the ARM64EC target.

Delay-load helper handling is a bit of a shortcut. The patch never pulls
it for native object files and just ensures that the code is fine with
that. In general, I think it would be nice to adjust the driver to pull
it only when it's actually referenced, which would allow applying the
same logic to the native symbol table on ARM64EC without worrying about
pulling too much.
2025-05-15 11:38:24 +02:00
jeremyd2019
f66f2fe0e5 [LLD][COFF] add __{data,bss}_{start,end}__ symbols for Cygwin support (#136180)
Cygwin requires these symbols for its fork emulation to know what data
to copy into the child. GNU ld defines these symbols for MinGW targets
also, so do the same here.

Cygwin also has the `.data_cygwin_nocopy` section, which is merged into
`.data` outside the `__data_start__` to `__data_end__` range. This
excludes it from fork's copying. AFAIK it's only used by the Cygwin DLL
itself (which requires a custom linker script to link, that's not
supported by LLD), but the section is included in GNU ld's default
linker script so handle it here too.

Signed-off-by: Jeremy Drake <github@jdrake.com>
2025-05-02 10:58:14 +03:00
Jacek Caban
61f80db9a4 [LLD][COFF] Remove no longer needed symtabEC from COFFLinkerContext (NFC) (#135094)
With #135093, we may just use `symtab` instead.
2025-04-11 18:50:16 +02:00
Jacek Caban
1c05c6183d [LLD][COFF] Swap the meaning of symtab and hybridSymtab in hybrid images (#135093)
Originally, the intent behind symtab was to represent the symbol table
seen in the PE header (without applying ARM64X relocations). However, in
most cases outside of `writeHeader()`, the code references either both
symbol tables or only the EC one, for example, `mainSymtab` in
`linkerMain()` maps to `hybridSymtab` on ARM64X.

MSVC's link.exe allows pure ARM64EC images to include native ARM64
files. This patch prepares LLD to support the same, which will require
`hybridSymtab` to be available even for ARM64EC. At that point,
`writeHeader()` will need to use the EC symbol table, and the original
reasoning for keeping it in `hybridSymtab` no longer applies.

Given this, it seems cleaner to treat the EC symbol table as the “main”
one, assigning it to `symtab`, and use `hybridSymtab` for the native
symbol table instead. Since `writeHeader()` will need to be conditional
anyway, this change simplifies the rest of the code by allowing other
parts to consistently treat `ctx.symtab` as the main symbol table.

As a further simplification, this also allows us to eliminate `symtabEC`
and use `symtab` directly; I’ll submit that as a separate PR.

The map file now uses the EC symbol table for printed entry points and
exports, matching MSVC behavior.
2025-04-11 15:53:25 +02:00
Alexandre Ganea
c75eac7c03 [LLD][COFF] Don't dllimport from static libraries (#134443)
This reverts commit 6a1bdd9 and re-instate behavior that matches what
MSVC link.exe does, that is, error out when trying to dllimport a symbol
from a static library.

A hint is now displayed in stdout, mentioning that we should rather dllimport the symbol
from a import library.

Fixes https://github.com/llvm/llvm-project/issues/131807
2025-04-07 11:34:24 -04:00
Kazu Hirata
1ff74917b5 [lld] Use *Set::insert_range (NFC) (#133565) 2025-03-29 10:29:45 -07:00
A2uria
90a08fb4b7 [LLD][COFF] Update nodefaultlibs after updating search paths (#128813)
Fix #107346
2025-03-12 08:47:01 +02:00
Jacek Caban
f2473bc31e [LLD][COFF] Support -aligncomm directives on ARM64X (#129513) 2025-03-03 22:48:20 +01:00
Jacek Caban
2d0eb5df4f [LLD][COFF] Add support for -includeglob on ARM64X (#129515)
Include symbols from both symbol tables.
2025-03-03 22:39:42 +01:00
Jacek Caban
d403f33886 [LLD][COFF] Mark personality functions as live in both symbol tables on ARM64X (#129295) 2025-03-02 13:37:51 +01:00
Jacek Caban
c6598f6ddf [LLD][COFF] Add support for autoimports on ARM64X (#129282) 2025-03-02 13:10:50 +01:00
Jacek Caban
8616c87335 [LLD][COFF] Support alternate names in both symbol tables on ARM64X (#127619)
The `.drectve` directive applies only to the namespace in which it is
defined, while the command-line argument applies only to the EC
namespace.
2025-02-21 12:52:28 +01:00
Jacek Caban
94d956367e [LLD][MinGW] Add support for wrapped symbols on ARM64X (#126296)
Apply `-wrap` arguments to both symbol tables.
2025-02-10 22:52:11 +01:00
Jacek Caban
6536579d80 [LLD][COFF] Add support for -includeoptional on ARM64X (#126300)
Include symbols from both symbol tables.
2025-02-10 22:01:53 +01:00
Jacek Caban
f729477657 [LLD][COFF] Add support for MinGW auto-export on ARM64X (#125862)
Export all symbols from both EC and native symbol tables. If an explicit
export is present in either symbol table, auto-export is disabled for
both.
2025-02-06 22:25:53 +01:00
Jacek Caban
e596387ebe [LLD][COFF] Use EC symbol table for output DEF file on ARM64X (#125531)
For consistency with input def handling.
2025-02-05 12:16:02 +01:00
Jacek Caban
e902cf2df1 [LLD][COFF] Write both native and EC export symbols to the import library on ARM64X (#124833) 2025-01-29 09:57:11 +01:00
Jacek Caban
97aa56ada5 [LLD][COFF] Move delayLoadHelper and tailMergeUnwindInfoChunk to SymbolTable (NFC) (#124729)
In preparation for ARM64X delay-load import support (#124600).
2025-01-28 12:07:35 +01:00
Jacek Caban
4e9d5a3a30 [LLD][COFF] Add support for the -defArm64Native argument (#123850)
MSVC ignores the `/defArm64Native` argument on non-ARM64X targets.
It is also ignored if the `/def` option is not specified.
2025-01-22 23:32:18 +01:00
Jacek Caban
a2c683b665 [LLD][COFF] Use EC symbol table for exports defined in module definition files (#123849) 2025-01-22 23:30:23 +01:00
Jacek Caban
455b3d6df2 [LLD][COFF] Separate EC and native exports for ARM64X (#123652)
Store exports in SymbolTable instead of Configuration.
2025-01-21 10:41:15 +01:00
kkent030315
fb974e8909 [LLD][COFF] Add support for custom DOS stub (#122561)
This change implements support for the /stub flag to align with MS
link.exe. This option is useful when a program needs to optimize the DOS
program that executes when the PE runs on DOS, avoiding the traditional
hardcoded DOS program in LLD.
2025-01-20 23:38:59 +02:00
Jacek Caban
b068f2fd0f [LLD][COFF] Process bitcode files separately for each symbol table on ARM64X (#123194) 2025-01-17 11:36:12 +01:00
Jacek Caban
1bd5f34d76 [LLD][COFF] Move getChunk to LinkerDriver (NFC) (#123103)
The `getChunk` function returns all chunks, not just those specific to a
symbol table. Move it out of the `SymbolTable` class to clarify its
scope.
2025-01-16 12:55:12 +01:00
Jacek Caban
d004947ac5 [LLD][COFF] Add support for hybrid ARM64X entry points (#123096)
Store the entry symbol in SymbolTable instead of Configuration, as it
differs between symbol tables.
2025-01-16 12:53:48 +01:00
Jacek Caban
0360f8170a [LLD][COFF] Infer subsystem from EC symbol table for ARM64X (#122838) 2025-01-15 17:50:19 +01:00
Jacek Caban
80084e9cb6 [LLD][COFF] Pull _load_config_used symbol from both symbol tables on ARM64X (#122837) 2025-01-15 17:43:33 +01:00
Jacek Caban
f22af59336 [LLD][COFF] Move symbol mangling and lookup helpers to SymbolTable class (NFC) (#122836)
This refactor prepares for further ARM64X hybrid support, where these
helpers will need to work with either the native or EC symbol table
based on context.
2025-01-15 15:21:06 +01:00
Jacek Caban
251ef3f503 [LLD][COFF] Use appropriate symbol table for -include argument on ARM64X (#122554)
Move `LinkerDriver::addUndefined` to` SymbolTable` to allow its use with
both symbol tables on ARM64X and rename it to `addGCRoot` to clarify its
distinct role compared to the existing `SymbolTable::addUndefined`.

Command-line `-include` arguments now apply to the EC symbol table, with
`mainSymtab` introduced in `linkerMain`. There will be more similar
cases. For `.drectve` sections, the corresponding symbol table is used
based on the context.
2025-01-13 23:16:57 +01:00
Haohai Wen
2d9d291da0 [LLD] Do not combine cg_profile from obj and ordering file (#121325)
cg_profile in object is from CGProfilePass and it is often inaccurate.
While call-graph-ordering-file is provided by user. It is weird to
aggregate them together especially when call-graph-ordering-file is
accurate enough.
2025-01-05 10:38:14 +08:00