Commit Graph

597 Commits

Author SHA1 Message Date
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
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
chrulski-intel
a9fe23cde3 [LLD] [COFF] Port -lto-sample-profile to COFF version of LLD (#85701)
Following the commit of #83972 which added COFF support for SPGO, this
patch ports the support of the option -lto-sample-profile that was only
available in the ELF variant of LLD to the COFF variant to enable
running the SPGO passes in the LTO/thinLTO pipelines.
2024-03-20 23:02:43 +08:00
Jacek Caban
46122082a6 [Object][COFF][NFC] Make writeImportLibrary NativeExports argument optional. (#81600)
It's not interesting for majority of downstream users.
2024-02-13 15:17:28 +01:00
Jacek Caban
a38152e215 [llvm-lib] Add support for -defArm64Native argument. (#81426)
This can be used to create import libraries that contain both ARM64EC
and native exports. The implementation follows observed MSVC lib.exe
behaviour. It's ignored on targets other than ARM64EC.
2024-02-13 01:49:45 +01:00
Martin Storsjö
0df8aed6c3 [LLD] [COFF] Pick timestamps from the SOURCE_DATE_EPOCH variable (#81326)
The SOURCE_DATE_EPOCH environment variable can be set in order to get
reproducible build.

When linking PE/COFF modules with LLD, the timestamp field is set to the
current time, unless either the /timestamp: or /Brepro option is set. If
neither of them is set, check the SOURCE_DATE_EPOCH variable, before
resorting to using the actual current date and time.

See https://reproducible-builds.org/docs/source-date-epoch/ for reference
on the use of this variable.
2024-02-10 23:57:12 +02:00
Kazu Hirata
21730eb49b [lld] Use SmallString::operator std::string (NFC) 2024-01-22 00:13:23 -08:00
Martin Storsjö
92126ca62e [LLD] [COFF] Prefer paths specified with -libpath: over toolchain paths (#78039)
The main reason for adding the toolchain paths early was to prefer
libraries from the toolchain over ones from MSVC (primarily for
compiler-rt builtins). But if the user specifies a directory explicitly
with the -libpath: option, that should be preferred over the built-in
default paths.

This fixes an issue raised at
https://discourse.llvm.org/t/lld-prefers-system-llvm-libraries-to-user-provided-ones-on-windows/76148.
2024-01-15 22:52:57 +02:00
Jacek Caban
76243adc46 [LLD][COFF] Merge .wowthk section to .text. (#76254)
.wowthk section is used by the compiler for ARM64EC entry thunks.
2023-12-25 20:31:08 +01:00
Martin Storsjö
e36535d4be [LLD] [COFF] Add /debug: options nodwarf and nosymtab (#75180)
These allow tweaking what gets implied by /debug and /debug:dwarf.
2023-12-15 20:10:41 +02:00
Martin Storsjö
efe017f8f0 [LLD] [COFF] Parse all /debug: options, like /opt: (#75178)
Most option handling is like it was before; the last /debug: option
takes effect.

However, the options /debug:dwarf or /debug:symtab don't reset all flags
into the specific behaviour they chose before - e.g. if an earlier
option enables writing a PDB, a later /debug:dwarf or /debug:symtab
doesn't disable that. This allows combining these options with options
for controlling PDB writing, for finetuning what is done.
2023-12-15 20:09:24 +02:00
Martin Storsjö
e6e615cade [LLD] [COFF] Rewrite handling of the /debug: option. NFC. (#75175)
Don't treat the options as unique enum items, but more as flags that can
be composed, like the /opt: options.

This still only processes the last option on the command line though, so
the behaviour should still remain exactly as it was, in all corner
cases.
2023-12-15 20:07:22 +02:00
Martin Storsjö
23e6e88187 [LLD] [COFF] Rewrite the config flags for dwarf debug info or symtab. NFC. (#75172)
This shouldn't have any user visible effect, but makes the logic within
the linker implementation more explicit.

Note how DWARF debug info sections were retained even if enabling a link
with PDB info only; that behaviour is preserved.
2023-12-15 20:01:13 +02:00
Zequan Wu
47b4bbfe52 [LLD][COFF] add __buildid symbol. (#74652)
After #71433, lld-link is able to always generate build id even when PDB
is not generated.

This adds the `__buildid` symbol to points to the start of 16 bytes guid
(which is after `RSDS`) and allows profile runtime to access it and dump
it to raw profile.
2023-12-14 17:43:10 -05:00
Jacek Caban
f78024c855 [lld][COFF] Merge .00cfg section into .rdata. (#75207)
.00cfg section is used by crt for load config and is merged by MS
link.exe into .rdata.
2023-12-13 11:43:38 +01:00
Zequan Wu
aaf3a8ded4 [LLD][COFF] Add -build-id flag to generate .buildid section. (#71433)
[RFC](https://discourse.llvm.org/t/rfc-add-build-id-flag-to-lld-link/74661)

Before, lld-link only generate the debug directory containing guid when
generating PDB with the hash of PDB content.

With this change, lld-link can generate the debug directory when only
`/build-id` is given:
1. If generating PDB, `/build-id` is ignored. Same behaviour as before.
2. Not generating PDB, using hash of the binary.
   - Not under MinGW, the debug directory is still in `.rdata` section.
   - Under MinGW, place the debug directory into new `.buildid` section.
2023-12-05 14:57:45 -05:00
Jacek Caban
72c6ca6943 [lld][COFF] Support .pdata section on ARM64EC targets. (#72521)
ARM64EC needs to handle both ARM and x86_64 exception tables. This is
achieved by separating their chunks and sorting them separately.
EXCEPTION_TABLE directory references x86_64 variant, while ARM variant
is exposed using CHPE metadata, which references
__arm64x_extra_rfe_table and __arm64x_extra_rfe_table_size symbols.
2023-12-05 11:59:43 +01:00
Adrian Prantl
2c07181424 [LEB128] Don't initialize error on success
This change removes an unnecessary branch from a hot path. It's also
questionable API to override any previous error unconditonally.
2023-11-29 12:47:27 -08:00
Adrian Prantl
69b0cb9c56 Revert "[LEB128] Don't initialize error on success"
This reverts commit 545c8e009e.
2023-11-29 12:40:37 -08:00
Adrian Prantl
545c8e009e [LEB128] Don't initialize error on success
This change removes an unnecessary branch from a hot path. It's also
questionable API to override any previous error unconditonally.
2023-11-29 12:16:32 -08:00
Jacek Caban
fe2bd12396 [lld] Add support for EC code map. (#69101) 2023-11-15 12:35:45 +01:00
Aleksei Nurmukhametov
76947e0405 [LLD][COFF] Support /DEPENDENTLOADFLAG[:flags] (#71537)
This should fix https://github.com/llvm/llvm-project/issues/43935
2023-11-08 15:21:05 -05:00
Martin Storsjö
303370e871 [LLD] [COFF] Mark the symbol _tls_used as a GC root (#71336)
This is necessary if the object file containing _tls_used is built with
-fdata-sections; if this symbol is present, lld is going to include
references to it in the PE header, in the TLS_TABLE data directory.

Therefore, treat the symbol as a GC root, i.e. retain it (and whatever
other symbols the section chunk references) when doing doing GC
(/opt:ref), if the symbol is present.
2023-11-07 11:49:05 +02:00
Martin Storsjö
241c290ad7 Reland [LLD] [COFF] Don't try to detect MSVC installations in mingw mode
In mingw mode, all linker paths are passed explicitly to the linker
by the compiler driver. Don't try to implicitly add linker paths
from the LIB environment variable or by detecting an MSVC
installation directory.

If the /winsysroot command line parameter is explicitly passed to
lld-link while /lldmingw is specified, it could be considered reasonable
to actually include those paths. However, modifying the code to
handle only the /winsysroot case but not the other ones, when the
mingw mode has been enabled, seems like much more code complexity
for a mostly hypothetical case.

Add a test for this when case when using LIB. (The code paths for
trying to detect an MSVC installation aren't really regression tested.)

Also fix an issue in the existing test for "Check that when /winsysroot
is specified, %LIB% is ignored.", where the LIB variable pointed
to a nonexistent directory, so the test would pass even if /winsysroot
wouldn't be specified.

Reland this after https://github.com/llvm/llvm-project/pull/68077 and
https://github.com/llvm/llvm-project/pull/69781 - the compiler-rt test
that used -lldmingw in MSVC environments has been updated to use a more
specific option.

Differential Revision: https://reviews.llvm.org/D144084
2023-10-21 22:36:11 +03:00
Martin Storsjö
a67ae8c0fd [LLD] [COFF] Add a separate option for allowing duplicate weak symbols (#68077)
The MinGW mode (enabled with the flag -lldmingw) does allow duplicate
weak symbols. A test in
compiler-rt/test/profile/Windows/coverage-weak-lld.cpp does currently
enable the -lldmingw flag in an MSVC context, in order to deal with
duplicate weak symbols.

Add a new, separate, lld specific flag for enabling this. In MinGW mode,
this is enabled by default, otherwise it is disabled.

This allows making the MinGW mode more restrictive in adding libpaths
from the surrounding environment; in MinGW mode, all libpaths are passed
explicitly by the compiler driver to the linker, which is attempted in
https://reviews.llvm.org/D144084.
2023-10-20 23:44:44 +03:00
Kazu Hirata
4a0ccfa865 Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class as opposed to an
enum. This patch replaces support::{big,little,native} with
llvm::endianness::{big,little,native}.
2023-10-12 21:21:45 -07:00
Jacek Caban
85d0fbeaae [lld] Don't allow -dynamicbase:no on ARM64EC. 2023-10-11 18:09:00 +02:00
Alexandre Ganea
356139bd02 [LLD][COFF] Add support for --time-trace (#68236)
This adds support for generating Chrome-tracing .json profile traces in
the LLD COFF driver.

Also add the necessary time scopes, so that the profile trace shows in
great detail which tasks are executed.

As an example, this is what we see when linking a Unreal Engine
executable:

![image](https://github.com/llvm/llvm-project/assets/37383324/b2e26eb4-9d37-4cf9-b002-48b604e7dcb7)
2023-10-05 22:33:58 -04:00
Matheus Izvekov
d12b99a431 [lld][COFF][LTO] Implement /lldemit:asm option (#67079)
With this new option, assembly code will be emited instead of object
code.
This is analogous to the `--lto-emit-asm` option in the ELF linker.
2023-10-04 01:24:54 +02:00
Matheus Izvekov
3923e61b96 [lld][COFF][LTO] Implement /lldemit:llvm option (#66964)
With this new option, bitcode will be emited instead of object code.
This is analogous to the `--plugin-opt=emit-llvm` option in the ELF
linker.
2023-10-02 22:54:43 +02:00
Martin Storsjö
f906fd53b5 [LLD] [COFF] Restore the current dir as the first entry in the search path (#67857)
Before af744f0b84, the first entry
among the search paths was the empty string, indicating searching
in (or starting from) the current directory. After
af744f0b84, the toolchain/clang
specific lib directories were added at the head of the search path.

This would cause lookups of literal file names or relative paths
to match paths in the toolchain, if there are coincidental files
with similar names there, even if they would be find in the current
directory as well.

Change addClangLibSearchPaths to append to the list like all other
operations on searchPaths - but move the invocation of the
function to the right place in the sequence.

This fixes #67779.
2023-10-02 13:47:30 +03:00
Martin Storsjö
7d7d9e462a [LLD] [COFF] Clarify -print-search-path for the empty string element (#67856)
Also switch the test case to use -NEXT to strictly match all lines.
2023-10-02 13:43:56 +03:00
Matheus Izvekov
a5e280bc6b [NFC] clang-format lld/COFF/Driver.cpp and lld/Common/Filesystem.cpp
In order to reduce noise for a MR.
2023-09-21 13:19:04 +02:00
Tobias Hieta
af744f0b84 [LLD][COFF] Add LLVM toolchain library paths by default.
We want lld-link to automatically find compiler-rt's and
libc++ when it's in the same directory as the rest of the
toolchain. This is because on Windows linking isn't done
via the clang driver - but instead invoked directly.

This prepends: <llvm>/lib <llvm>/lib/clang/XX/lib and
<llvm>/lib/clang/XX/lib/windows automatically to the library
search paths.

Related to #63827

Differential Revision: https://reviews.llvm.org/D151188
2023-07-14 14:37:24 +02:00
Tobias Hieta
b6c2f100c2 [lld][COFF] Find libraries with relative paths.
This patch is spun out of https://reviews.llvm.org/D151188
and makes it possible for lld-link to find libraries with
relative paths. This will be used later to implement the
changes to autolinking runtimes explained in #63827

Differential Revision: https://reviews.llvm.org/D155268
2023-07-14 12:09:26 +02:00
Tobias Hieta
33f93425dd [lld][COFF] Add -print-search-paths for debugging.
While working on adding more implicit search paths to the
lld COFF driver, it was helpful to have a way to print all
the search paths, both for debugging and for testing without
having to create very complicated test cases.

This is a simple arg that just prints the search paths and exits.

Related to the efforts in #63827

Differential Revision: https://reviews.llvm.org/D155047
2023-07-13 10:52:57 +02:00
Haohai Wen
2a631a8fed [lld/COFF] Add /dwodir to enable DWARF fission with LTO
This patch added /dwodir to lld/COFF which is equivalent to lld/ELF
option -plugin-opt=dwo_dir=. This option tells LTO backend to create
dwo directory and files and all dwo files will be in it. Otherwise all
dwarf sections will be embeded into image even if -gsplit-dwarf is
specified when using LTO.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D154070
2023-07-01 08:59:12 +08:00
Alexandre Ganea
6f2e92c10c Re-land [LLD] Allow usage of LLD as a library
This reverts commit aa495214b3.

As discussed in https://github.com/llvm/llvm-project/issues/53475 this patch
allows for using LLD-as-a-lib. It also lets clients link only the drivers that
they want (see unit tests).

This also adds the unit test infra as in the other LLVM projects. Among the
test coverage, I've added the original issue from @krzysz00, see:
https://github.com/ROCmSoftwarePlatform/D108850-lld-bug-reproduction

Important note: this doesn't allow (yet) linking in parallel. This will come a
bit later hopefully, in subsequent patches, for COFF at least.

Differential revision: https://reviews.llvm.org/D119049
2023-06-19 07:35:11 -04:00
Leonard Chan
aa495214b3 Revert "[LLD] Allow usage of LLD as a library"
This reverts commit 2700da5fe2.

Reverting since this causes some test failures on our builders: https://ci.chromium.org/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8778372807208184913/overview
2023-06-14 20:36:27 +00:00
Alexandre Ganea
2700da5fe2 [LLD] Allow usage of LLD as a library
As discussed in https://github.com/llvm/llvm-project/issues/53475 this patch allows using LLD-as-a-lib. It also lets clients link only the drivers that they want (see unit tests).

This also adds the unit test infra as in the other LLVM projects. Among the test coverage, I've added the original issue from @krzysz00, see: https://github.com/ROCmSoftwarePlatform/D108850-lld-bug-reproduction

Important note: this doesn't allow (yet) linking in parallel. This will come a bit later, in subsequent patches, for COFF at last.

Differential revision: https://reviews.llvm.org/D119049
2023-06-13 16:22:59 -04:00
Alexandre Ganea
adcdc9cc37 [LLD][COFF] Allow overwriting directives exports with cmd-line exports
MSVC link.exe allows overriding exports on the cmd-line with exports seen in OBJ directives. The typical case is what is described in #62329.

Before this patch, trying to override an export with `/export` or `/def` would generate a duplicate warning. This patches tries to replicate the MSVC behavior. A second override on the cmd-line would still generate the warning.

There's still a case which we don't cover: MSVC link.exe is able to demangle an exported OBJ directive function, and match it with a unmangled export function in a .def file. In the meanwhile, one can use the mangled export in the .def to cover that case.

This fixes #62329

Differential revision: https://reviews.llvm.org/D149611
2023-06-13 15:29:46 -04:00
Arthur Eubanks
5f8f310a1d [NFC][lld][COFF] Rename findFile* methods
findFile returns null if we've already seen the file, make that clearer.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D152292
2023-06-06 13:15:09 -07:00
Arthur Eubanks
ec1998cd48 [lld][COFF] Don't handle an input file multiple times when retrying
Follow up to D151815.

Or else we properly handle the first instance of a file, then error out on the second instance of the same file.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D152198
2023-06-06 11:07:00 -07:00
Fangrui Song
8d85c96e0e [lld] StringRef::{starts,ends}with => {starts,ends}_with. NFC
The latter form is now preferred to be similar to C++20 starts_with.
This replacement also removes one function call when startswith is not inlined.
2023-06-05 14:36:19 -07:00
Arthur Eubanks
c8ea66937b [lld][COFF] Retry failed paths to take advantage of winsysroot search paths
With /winsysroot and without /machine, we don't know which paths to add to the search paths.

We do autodetect machine type and add winsysroot search paths in SymbolTable::addFile(), but that happens after all input files are opened. So in the loop where we read files, if we fail to open a file we can retry with the winsysroot search path potentially added by reading a previous file. This will fail if we try to open something in the winsysroot before reading a file that can give us the architecture, but shrug.

Fixes #54409

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D151815
2023-06-01 15:37:02 -07:00
Kazu Hirata
ed1539c6ad Migrate {starts,ends}with_insensitive to {starts,ends}_with_insensitive (NFC)
This patch migrates uses of StringRef::{starts,ends}with_insensitive
to StringRef::{starts,ends}_with_insensitive so that we can use names
similar to those used in std::string_view.

Note that the llvm/ directory has migrated in commit
6c3ea866e9.

I'll post a separate patch to deprecate
StringRef::{starts,ends}with_insensitive.

Differential Revision: https://reviews.llvm.org/D150506
2023-05-16 10:12:42 -07:00
Alvin Wong
8fa0cfeb61 [LLD][COFF] Add /inferasanlibs to lld-link as ignored flag
MSVC link.exe added this flag and MS STL started using this flag in
.drectve [1] when compiling with Clang with asan enabled, as reported
on https://github.com/llvm/llvm-project/issues/56300. This causes issues
with lld-link because it rejects any unknown flags in .drective sections.

As dc07867dc9 noted that, when using Clang
as the driver it explicitly passes the proper asan libraries. Therefore
it should be acceptable to ignore this flag in lld-link to at least
unbreak building with clang-cl and linking with lld-link.

[1]: faaf094ee1/stl/inc/__msvc_sanitizer_annotate_container.hpp (L35)

Differential Revision: https://reviews.llvm.org/D149023
2023-04-24 22:06:34 +08:00
Alvin Wong
9b15e9840f [LLD][COFF] Print object file name for unsupported directives
This is a small QoL improvement suggested by FrancescElies in
https://github.com/llvm/llvm-project/issues/56300#issuecomment-1172104966.

Differential Revision: https://reviews.llvm.org/D149022
2023-04-24 22:06:34 +08:00