671 Commits

Author SHA1 Message Date
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
Jacek Caban
8435225374
[LLD][COFF] Move addFile implementation to LinkerDriver (NFC) (#121342)
The addFile implementation does not rely on the SymbolTable object. With
#119294, the symbol table for input files is determined during the
construction of the objects representing them. To clarify that
relationship, this change moves the implementation from the SymbolTable
class to the LinkerDriver class.
2025-01-01 19:42:49 +01:00
Jacek Caban
db7123fbbc
[LLD][COFF] Use EC symbol table for CHPE metadata (#120328)
Copy CHPE metadata pointer from EC load config to native configuration.
2024-12-29 14:04:00 +01:00
Jacek Caban
ff29f38c02
[LLD][COFF] Store and validate load config in SymbolTable (#120324)
Improve diagnostics for invalid load configurations.
2024-12-29 11:43:45 +01:00
Jacek Caban
21a6dbd400
[LLD][COFF] Report undefined EC symbols on ARM64X (#120311) 2024-12-20 19:56:02 +01:00
Jacek Caban
16ef239520 [LLD][COFF] Introduce hybrid symbol table for EC input files on ARM64X (#119294) 2024-12-17 21:19:01 +01:00
Jacek Caban
9c8214ff31
[LLD][COFF] Create COFFObjectFile instance when constructing ObjFile (NFC) (#120144)
This change moves the creation of COFFObjectFile to the construction of
ObjFile, instead of delaying it until parsing.
2024-12-17 19:26:13 +01:00
Nico Weber
34a44b2088
[lld/COFF] Handle -start-lib / -end-lib better in /reproduce: output (#119752)
Previously, we'd collect all input files in Driver::filePaths, and then
write filePaths after all other flags in
createResponseFile(). This meant that `-start-lib foo.obj -end-lib`
would be written as `-start-lib -end-lib foo.obj`, changing semantics.

Instead, remove Driver::filePaths, and handle things that fed into it
directly:

* OPT_INPUT is now handled in the same way as other flags, so that we
now get `-start-lib foo.obj -end-lib` in response.txt as desired. Add a
test for -start-lib / -end-lib and /reproduce:.

* OPT_wholearchive_file needs explicit handling now -- but before, this
was buggy as well: We'd put the flag without a rewritten path in
response.txt, but also the rewritten input file without wholearchive
semantics via filePaths. So this commit makes --whole-archive work with
/reproduce: too, and adds test coverage.

* /defaultlib:foo is now written as /defaultlib:foo into response.txt,
instead of writing the resolved path previously. While response.txt
looks slightly differently, both should have the same semantics, and
this should be mostly a no-op. (It does require updating a test.)

* /defaultlib: from .drectve sections are no longer recorded in
response.txt. This seems like a progression -- in the non-repro case
they come from .obj files, so they should come (only) from there in the
repro case too. This adds test coverage for this case.

Makes createResponseFile() look more like the versions in the ELF and
MachO ports too.
2024-12-17 11:30:13 -05:00
Jacek Caban
7168de5ca7 Revert "[LLD][COFF] Introduce hybrid symbol table for EC input files on ARM64X (#119294)"
This reverts commit a8206e7b37929f4754806667680ffba0206eef95 due to sanitizer failures.
2024-12-15 22:31:28 +01:00
Jacek Caban
a8206e7b37
[LLD][COFF] Introduce hybrid symbol table for EC input files on ARM64X (#119294)
On hybrid ARM64X targets, ARM64 and ARM64EC input files operate in
separate namespaces and cannot reference each other. This change
introduces separate `SymbolTable` instances and associates each
`InputFile` with the appropriate table to reflect this behavior.
2024-12-15 18:49:32 +01:00
Jacek Caban
d3c4857179
[LLD][COFF] Store machine type in SymbolTable (NFC) (#119298)
This change prepares for hybrid ARM64X support, which requires two
`SymbolTable` instances: one for native symbols and one for EC symbols.
In such cases, `config.machine` will remain ARM64X, while the
`SymbolTable` instances will store ARM64 and ARM64EC machine types.
2024-12-15 18:43:09 +01:00
Jacek Caban
0a9810d325
[LLD][COFF] Factor out LinkerDriver::setMachine (NFC) (#119297) 2024-12-15 18:41:26 +01:00
Jacek Caban
6b493baec1
[LLD][COFF] Store reference to SymbolTable instead of COFFLinkerContext in InputFile (NFC) (#119296)
This change prepares for the introduction of separate hybrid namespaces.
Hybrid images will require two `SymbolTable` instances, making it
necessary to associate `InputFile` objects with the relevant one.
2024-12-15 12:45:34 +01:00
Fangrui Song
983f88c1ec [lld-link] Use COFFSyncStream
Add a operator<< overload for Symbol *.
2024-12-05 20:41:37 -08:00
Fangrui Song
8b844de3c9 [lld-link] Replace fatal(...) with Fatal 2024-12-05 20:18:01 -08:00
Fangrui Song
8d225f10ef [lld-link] Replace error(...) with Err 2024-12-05 19:44:26 -08:00
Fangrui Song
4639a9a063 [lld-link] Replace log(...) with Log 2024-12-04 09:04:40 -08:00
Fangrui Song
1534f45694 [lld-link] Replace warn(...) with Warn(ctx) 2024-12-03 22:19:30 -08:00
Fangrui Song
bc66e9aaa6 [lld-link] Replace message(...) with Msg(ctx)
to avoid the global ctx.
2024-12-03 21:29:02 -08:00
Fangrui Song
982575fd06
[lld-link] Add context-aware diagnostic functions (#118430)
Similar to #112319 for ELF. While there is some initial boilerplate, it
can simplify some call sites that use Twine, especially when a printed
element uses `ctx` or toString.
2024-12-03 20:51:50 -08:00
Fangrui Song
1bfe55aff5 [lld-link] Replace errorHandler() with ctx.e
errorHandler() uses the global state, which should be avoided in
lld/COFF code.
2024-11-16 18:31:58 -08:00