Commit Graph

1685 Commits

Author SHA1 Message Date
Justin Bogner
dcb6d212fd Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
This reverts commit 4e3b89483a, with
fixes for places I'd missed updating in lld and lldb. I've also
renamed OptionVisibility::Default to "DefaultVis" to avoid ambiguity
since the undecorated name has to be available anywhere Options.inc is
included.

Original message follows:

This splits OptTable's "Flags" field into "Flags" and "Visibility",
updates the places where we instantiate Option tables, and adds
variants of the OptTable APIs that use Visibility mask instead of
Include/Exclude flags.

We need to do this to clean up a bunch of complexity in the clang
driver's option handling - there's a whole slew of flags like
CoreOption, NoDriverOption, and FlangOnlyOption there today to try to
handle all of the permutations of flags that the various drivers need,
but it really doesn't scale well, as can be seen by things like the
somewhat recently introduced CLDXCOption.

Instead, we'll provide an additive model for visibility that's
separate from the other flags. For things like "HelpHidden", which is
used as a "subtractive" modifier for option visibility, we leave that
in "Flags" and handle it as a special case.

Note that we don't actually update the users of the Include/Exclude
APIs here or change the flags that exist in clang at all - that will
come in a follow up that refactors clang's Options.td to use the
increased flexibility this change allows.

Differential Revision: https://reviews.llvm.org/D157149
2023-08-15 01:16:58 -07:00
namazso
e335c78ec2
[lld][COFF] Remove incorrect flag from EHcont table
Fixes EHCont implementation in LLD. Closes #64570

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D157623
2023-08-10 16:17:38 -04:00
Jan Svoboda
3f092f37b7 [llvm] Extract common OptTable bits into macros
All command-line tools using `llvm::opt` create an enum of option IDs and a table of `OptTable::Info` object. Most of the tools use the same ID (`OPT_##ID`), kind (`Option::KIND##Class`), group ID (`OPT_##GROUP`) and alias ID (`OPT_##ALIAS`). This patch extracts that common code into canonical macros. This results in fewer changes when tweaking the `OPTION` macros emitted by the TableGen backend.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D157028
2023-08-04 13:57:13 -07:00
Jacek Caban
ca348125ba [lld] [NFC] Use llvm::COFF::is64Bit in is64.
Reviewed By: mstorsjo
Differential Revision: https://reviews.llvm.org/D156450
2023-07-28 14:20:14 +02:00
Fangrui Song
1d1f245270 [COFF] Switch to xxh3_64bits
Similar to recent changes to ELF (e.g., commit
f4b4bc2f18) and Mach-O to improve hashing
performance.
2023-07-19 14:08:14 -07: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
Martin Storsjö
6daa4b90e2 [LLD] [COFF] Warn about pseudo relocations that are too narrow
In 64 bit mode, any references to symbols that might end up autoimported
must be made via full 64 bit pointers (usually in .refptr stubs
generated by the compiler).

If referenced via e.g. a 32 bit rip relative offset, it might work
as long as DLLs are loaded close together in the 64 bit address
space, but will fail surprisingly later if they happen to be loaded
further apart. Any cases of that happening is usually a toolchain
error, and the sooner we can warn about it, the easier it is to diagnose.

Differential Revision: https://reviews.llvm.org/D154777
2023-07-11 23:43:34 +03: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
Elliot Goodrich
b0abd4893f [llvm] Add missing StringExtras.h includes
In preparation for removing the `#include "llvm/ADT/StringExtras.h"`
from the header to source file of `llvm/Support/Error.h`, first add in
all the missing includes that were previously included transitively
through this header.
2023-06-25 15:42:22 +01: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
Kazu Hirata
da12f05101 [lld] Remove unused forward declarations for DefinedRelative
The corresponding class definition was removed by:

  commit 502d4ce2e4
  Author: Reid Kleckner <rnk@google.com>
  Date:   Mon Jun 26 15:39:52 2017 +0000
2023-06-14 22:04:41 -07: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
Nick Desaulniers
8abbc17ff3 reland: [Demangle] make llvm::demangle take std::string_view rather than const std::string&
As suggested by @erichkeane in
https://reviews.llvm.org/D141451#inline-1429549

There's potential for a lot more cleanups around these APIs. This is
just a start.

Callers need to be more careful about sub-expressions producing strings
that don't outlast the expression using `llvm::demangle`. Add a
release note.

Differential Revision: https://reviews.llvm.org/D149104
2023-06-06 10:18:06 -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
Jacek Caban
cb227c944f [lld] Allow input files from compatible architectures on EC targets.
Differential Revision: https://reviews.llvm.org/D149087
2023-05-29 19:43:21 +02:00
Jacek Caban
482ee33a63 [lld] Use correct machine type in ARM64EC COFF headers.
This adds very minimal support for ARM64EC/ARM64X targets,
just enough for interesting test cases. Next patches in the
series extend llvm-objdump and llvm-readobj to provide
better tests. Those will also be useful for testing further
ARM64EC LLD support.

Differential Revision: https://reviews.llvm.org/D149086
2023-05-29 19:42:24 +02:00
Haohai Wen
c384fcd3ea [lld] Partially revert "Always emit symbol table when dwarf section exists in COFF"
This reverts part of commit 44363f2ff2.

Fixup for NO symbol table test has been reserved.

Reviewed By: wxiao3

Differential Revision: https://reviews.llvm.org/D151417
2023-05-29 09:23:51 +08: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
Phoebe Wang
360d0cd0a2 [LLD] Do not assume /guard:cf always set together with /guard:ehcont
MS link accepts *.obj with ehcont bit set only. LLD should match this
behavoir too.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D150508
2023-05-16 23:12:03 +08:00
Nick Desaulniers
3e3c6f24ff Revert "[Demangle] make llvm::demangle take std::string_view rather than const std::string&"
This reverts commit c117c2c8ba.

itaniumDemangle calls std::strlen with the results of
std::string_view::data() which may not be NUL-terminated. This causes
lld/test/wasm/why-extract.s  to fail when "expensive checks" are enabled
via -DLLVM_ENABLE_EXPENSIVE_CHECKS=ON. See D149675 for further
discussion. Back this out until the individual demanglers are converted
to use std::string_view.
2023-05-02 15:54:09 -07:00
Nick Desaulniers
c117c2c8ba [Demangle] make llvm::demangle take std::string_view rather than const std::string&
As suggested by @erichkeane in
https://reviews.llvm.org/D141451#inline-1429549

There's potential for a lot more cleanups around these APIs. This is
just a start.

Callers need to be more careful about sub-expressions producing strings
that don't outlast the expression using ``llvm::demangle``. Add a
release note.

Reviewed By: MaskRay, #lld-macho

Differential Revision: https://reviews.llvm.org/D149104
2023-05-02 11:20:15 -07:00
Ulrich Weigand
fb8557813a Fix PDB relocation on big-endian hosts
When running the LLD test suite on a big-endian host, the
COFF/pdb-framedata.yaml test case currently fails.

As it turns out, this is because code in DebugSHandler::finish
intended to relocate RvaStart entries of FDO records does not
work correctly when compiled for a big-endian host.

Fixed by always reading file data in little-endian mode.

Reviewed By: aganea

Differential Revision: https://reviews.llvm.org/D149268
2023-04-27 17:48:27 +02:00
Fangrui Song
71cb689661 [LTO] Change getThinLTOOutputFile to take StringRef 2023-04-26 20:43:11 -07:00
Haohai Wen
44363f2ff2 Always emit symbol table when dwarf section exists in COFF
This also fixes check prefix NO which is pointless in symtab.test

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D149235
2023-04-27 09:42:47 +08: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
Ivan Tadeu Ferreira Antunes Filho
73fd9d310f [lld] Support separate native object file path in --thinlto-prefix-replace
Currently, the --thinlto-prefix-replace="oldpath;newpath" option is used during
distributed ThinLTO thin links to specify the mapping of the input bitcode object
files' directory tree (oldpath) to the directory tree (newpath) used for both:

1) the output files of the thin link itself (the .thinlto.bc index files and the
optional .imports files)
2) the specified object file paths written to the response file given in the
--thinlto-index-only=${response} option, which is used by the final native
link and must match the paths of the native object files that will be
produced by ThinLTO backend compiles.
This patch expands the --thinlto-prefix-replace option to allow a separate directory
tree mapping to be specified for the object file paths written to the response file
(number 2 above). This is important to support builds and build systems where the
same output directory may not be written by multiple build actions (e.g. the thin link
and the ThinLTO backend compiles).

The new format is: --thinlto-prefix-replace="origpath;outpath[;objpath]"

This replaces the origpath directory tree of the thin link input files with
outpath when writing the thin link index and imports outputs (number 1
above). If objpath is specified it replaces origpath of the input files with
objpath when writing the response file (number 2 above), otherwise it
falls back to the old behavior of using outpath for this as well.

Reviewed By: tejohnson, MaskRay

Differential Revision: https://reviews.llvm.org/D144596
2023-04-04 11:24:51 -07:00
Jacek Caban
a5988034a4 [lld] Fill .text section gaps with INT3 only on x86 targets.
It doesn't make sense on ARM and using default 0 fill is compatible
with MSVC.

(It's more noticeable ARM64EC targets, where additional padding mixed
with alignment is used for entry thunk association, so there are more
gaps).

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D145962
2023-03-23 13:43:21 +02:00
Saleem Abdulrasool
af5f468228 MSVC: support version preference with search
Extend the logic for the WinSDK and UCRT handling to prefer a user
specified version of the VisualC++ tools and Windows SDK.  This allows
us to now perform the regular search for the installation but select the
exact version of the SDK or VC++ tools to override the latest version.
Similar to the other flags controlling this behaviour, if the user
specifies a value, we will not perform validation on the input and will
attempt to prefer that, particularly in the case of VisualC++ tools
where no fallback occurs.

Reviewed by: hans
Differential Revision: https://reviews.llvm.org/D145517
2023-03-13 09:25:31 -07:00
Markus Mützel
0ea8229bf8 Reapply [lld][flang] Add exceptions for Flang runtime libraries on MinGW.
When linking a shared library with Flang on MinGW, the functions from the
Flang runtime are exported from the shared library. When trying to link an
executable to that library using Flang, the linker errors out because the
functions from the runtime conflict with the functions exported from the
shared library.

Add the Flang runtime libraries to the list of libraries for which no
symbols are exported.

Reapplying the patch with the git author name corrected.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D145389
2023-03-09 13:19:22 +02:00
Martin Storsjö
0dd98e1251 Revert "[lld][flang] Add exceptions for Flang runtime libraries on MinGW."
This reverts commit 04fc67bf08.

The patch was applied with the wrong git author name (forgot to
amend in the right user name after applying the patch); I'll
reapply it with the right author name.
2023-03-09 13:19:22 +02:00
Martin Storsjö
04fc67bf08 [lld][flang] Add exceptions for Flang runtime libraries on MinGW.
When linking a shared library with Flang on MinGW, the functions from the
Flang runtime are exported from the shared library. When trying to link an
executable to that library using Flang, the linker errors out because the
functions from the runtime conflict with the functions exported from the
shared library.

Add the Flang runtime libraries to the list of libraries for which no
symbols are exported.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D145389
2023-03-09 13:16:12 +02:00
Hans Wennborg
724b58f766 [PDB] Error on too large stream directories
We hit this in Chromium builds where the PDB file was just under 4GB,
but the stream directory was actually too large to be correctly
represented.

llvm-pdbutil would error about this in llvm::msf::validateSuperBlock,
but lld should not write such PDB files in the first place.

Differential revision: https://reviews.llvm.org/D144385
2023-02-24 15:07:11 +01:00
Martin Storsjö
eb14186771 Revert "[LLD] [COFF] Don't try to detect MSVC installations in mingw mode"
This reverts commit 389bfbd66d.

This commit broke the compiler-rt/test/profile/Windows/coverage-weak-lld.cpp
testcase. This testcase builds and links in an MSVC environment,
but explicitly passes -lldmingw to the linker, to opt in to
certain mingw-style behaviours regarding weak symbols, since
effb87dfa8. This patch broke that
test, which now errored out with these errors:

    lld-link: error: could not open 'libuuid.a': no such file or directory
    lld-link: error: could not open 'libLIBCMT.a': no such file or directory
    lld-link: error: could not open 'libOLDNAMES.a': no such file or directory
2023-02-17 11:27:24 +02:00
Martin Storsjö
389bfbd66d [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.

Differential Revision: https://reviews.llvm.org/D144084
2023-02-17 10:56:43 +02:00
Scott Linder
45ee0a9afc [LLD] Add --lto-CGO[0-3] option
Allow controlling the CodeGenOpt::Level independent of the LTO
optimization level in LLD via new options for the COFF, ELF, MachO, and
wasm frontends to lld. Most are spelled as --lto-CGO[0-3], but COFF is
spelled as -opt:lldltocgo=[0-3].

See D57422 for discussion surrounding the issue of how to set the CG opt
level. The ultimate goal is to let each function control its CG opt
level, but until then the current default means it is impossible to
specify a CG opt level lower than 2 while using LTO. This option gives
the user a means to control it for as long as it is not handled on a
per-function basis.

Reviewed By: MaskRay, #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D141970
2023-02-15 17:34:35 +00:00
Archibald Elliott
62c7f035b4 [NFC][TargetParser] Remove llvm/ADT/Triple.h
I also ran `git clang-format` to get the headers in the right order for
the new location, which has changed the order of other headers in two
files.
2023-02-07 12:39:46 +00:00
Fangrui Song
053479118f lld/COFF: simplify startswith+substr 2023-01-30 15:28:10 -08:00
Kazu Hirata
f6b8f05bb3 Use llvm::byteswap instead of ByteSwap_{16,32,64} (NFC) 2023-01-28 15:22:37 -08:00
Jez Ng
3df4c5a92f [NFC] Optimize vector usage in lld
By using emplace_back, as well as converting some loops to for-each, we can do more efficient vectorization.

Make copy constructor for TemporaryFile noexcept.

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D139552
2023-01-26 20:31:42 -05:00
Arthur Eubanks
ae5efe9761 [lld] Remove transitional legacy pass manager flags
Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D142571
2023-01-26 11:12:21 -08:00
Joe Loser
a288d7f937 [llvm][ADT] Replace uses of makeMutableArrayRef with deduction guides
Similar to how `makeArrayRef` is deprecated in favor of deduction guides, do the
same for `makeMutableArrayRef`.

Once all of the places in-tree are using the deduction guides for
`MutableArrayRef`, we can mark `makeMutableArrayRef` as deprecated.

Differential Revision: https://reviews.llvm.org/D141814
2023-01-16 14:49:37 -07:00
serge-sans-paille
c512eda38e
[lld][COFF] Provide unwinding information for Chunk injected by /delayloaded
For each symbol in a /delayloaded library, lld injects a small piece of
code to handle the symbol lazy loading. This code doesn't have unwind
information, which may be troublesome.

Provide these information for AMD64.

Thanks to Yannis Juglaret <yjuglaret@mozilla.com> for contributing the
unwinding info and for his support while crafting this patch.

Fix #59639

Differential Revision: https://reviews.llvm.org/D141691
2023-01-16 18:39:21 +01:00