Commit Graph

1038 Commits

Author SHA1 Message Date
Kyungwoo Lee
77e204c7b0
[lld-macho][arm64] implement -objc_stubs_small (#78665)
This patch implements `-objc_stubs_small` targeting arm64, aiming to
align with ld64's behavior.
1. `-objc_stubs_fast`: As previously implemented, this always uses the
Global Offset Table (GOT) to invoke `objc_msgSend`. The alignment of the
objc stub is 32 bytes.
2. `-objc_stubs_small`: This behavior depends on whether `objc_msgSend`
is defined. If it is, it directly jumps to `objc_msgSend`. If not, it
creates another stub to indirectly jump to `objc_msgSend`, minimizing
the size. The alignment of the objc stub in this case is 4 bytes.
2024-01-23 07:31:34 -08:00
Kazu Hirata
21730eb49b [lld] Use SmallString::operator std::string (NFC) 2024-01-22 00:13:23 -08:00
OldWorldOrdr
46a9135d61
[lld-macho] Find objects in library search path (#78628)
Find object files in library search path just like Apple's linker, this
makes building with some older MacOS SDKs easier since clang runs with
`-lcrt1.10.6.o`
2024-01-20 13:53:55 -08:00
kyulee-com
5de1d007dd
[lld-macho] Fix for objc_msgSend stubs (#78557)
This commit corrects the address computation for objc_msgSend stubs.
Previously, the address computation was incidentally correct due to
objc_msgSend often being the first entry in the got section, resulting
in a 0 index. This commit ensures accurate address computation
regardless of the objc_msgSend stub's position in the got section.
2024-01-18 10:46:30 -08:00
Kazu Hirata
51fb76ff1d [lld] Use StringRef::consume_front_insensitive (NFC) 2024-01-12 22:08:26 -08:00
Nico Weber
d782f198a6 lld/MachO: Fix two typos to cycle bots 2023-12-22 12:06:38 -05:00
Kazu Hirata
cc4ecfd68b
[ADT] Rename SmallString::{starts,ends}with to {starts,ends}_with (#74916)
This patch renames {starts,ends}with to {starts,ends}_with for
consistency with std::{string,string_view}::{starts,ends}_with in
C++20.  Since there are only a handful of occurrences, this patch
skips the deprecation phase and simply renames them.
2023-12-09 14:28:45 -08:00
Daniel Thornburgh
71de61259a
[lld][MachO] Prevent doubled N_SO when comp_dir and name absolute (#71608)
When forming MachO STABS, this change detects if the DW_AT_name of the
compile unit is already absolute (as allowed by DWARF), and if so, does
not prepend DW_AT_comp_dir.

Fixes #70995
2023-11-08 15:55:22 -08: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
Matheus Izvekov
8ff77a8f04
[NFC][LLD] Refactor some copy-paste into the Common library (#67598) 2023-09-28 00:06:48 +02:00
kyulee-com
e04bf91111
[lld-macho][NFC] Remove redundant checks (#67450)
`ignoreAutoLinkOptions` checks run both in `parseLCLinkerOptions` and
`resolveLCLinkerOptions`. Convert the latter check to an assert.
2023-09-26 14:59:18 -07:00
Nico Weber
210e8984fe
[lld/mac] Resolve defined symbols before undefined symbols in bitcode (#67445)
Ports https://reviews.llvm.org/D106293 to bitcode, or
https://github.com/llvm/llvm-project/commit/bd448f01a6 from ELF to
MachO.

See also #59162 for some vaguely related discussion.
2023-09-26 15:31:51 -04:00
Arthur Eubanks
0a1aa6cda2
[NFC][CodeGen] Change CodeGenOpt::Level/CodeGenFileType into enum classes (#66295)
This will make it easy for callers to see issues with and fix up calls
to createTargetMachine after a future change to the params of
TargetMachine.

This matches other nearby enums.

For downstream users, this should be a fairly straightforward
replacement,
e.g. s/CodeGenOpt::Aggressive/CodeGenOptLevel::Aggressive
or s/CGFT_/CodeGenFileType::
2023-09-14 14:10:14 -07:00
Vy Nguyen
595cd45a66 [lld-macho][nfc]Add bounds on sections and subsections check before attempting to dereferencing iterators.
Runnign some tests with asan built of LLD would throw errors similar to the following:

AddressSanitizer:DEADLYSIGNAL
    #0 0x55d8e6da5df7 in operator() /mnt/ssd/repo/lld/llvm-project/lld/MachO/Arch/ARM64.cpp:612
    #1 0x55d8e6daa514 in operator() /mnt/ssd/repo/lld/llvm-project/lld/MachO/Arch/ARM64.cpp:650

Differential Revision: https://reviews.llvm.org/D157027
2023-09-11 14:19:25 -04:00
Ellis Hoag
30e688e6d0
[lld][MachO] Add option to suppress mismatch profile errors (#65551)
Both ELF and COFF support `--no-lto-pgo-warn-mismatch` in
https://reviews.llvm.org/D104431 to suppress warnings due to mismatching
profile hashes. As profiles go stale, it becomes likely that some
function's CFGs will change so that their profiles can no longer be
used. This commit adds the linker option `--no-pgo-warn-mismatch` to
suppress these warnings.

Note that we do have the LLVM backend flag `no-pgo-warn-mismatch`
3df1a64eba/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (L210)

but that is set to true by default during LTO
3df1a64eba/llvm/include/llvm/LTO/Config.h (L76-L77)
2023-09-11 09:13:55 -07:00
Vy Nguyen
84a2155921 [lld-macho]Limit "cannot-export-hidden-symbol" warnings to only 3 to avoid crowding logs.
Details:
We often use wildcard symbols in the exported_symbols list, and sometimes they match autohide symbols, which triggers these "cannot export hidden symbols" warnings that can be a bit noisy.
It'd be more user-friendly if LLD could truncate these.

Differential Revision: https://reviews.llvm.org/D159095
2023-09-07 13:50:55 -04:00
Takuya Shimizu
01b88dd66d [NFC] Remove unused variables declared in conditions
D152495 makes clang warn on unused variables that are declared in conditions like `if (int var = init) {}`
This patch is an NFC fix to suppress the new warning in llvm,clang,lld builds to pass CI in the above patch.

Differential Revision: https://reviews.llvm.org/D158016
2023-08-30 10:05:06 +09:00
Kyungwoo Lee
a033184abb [lld-macho] Stricter Bitcode Symbol Resolution
LLD resolves symbols before performing LTO compilation, assuming that the symbols in question are resolved by the resulting object files from LTO. However, there is a scenario where the prevailing symbols might be resolved incorrectly due to specific assembly symbols not appearing in the symbol table of the bitcode. This patch deals with such a scenario by generating an error instead of silently allowing a mis-linkage.
If a prevailing symbol is resolved through post-loaded archives via LC linker options, a warning will now be issued.

Reviewed By: #lld-macho, thevinster

Differential Revision: https://reviews.llvm.org/D158003
2023-08-22 12:03:17 -07:00
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
Justin Bogner
4e3b89483a Revert "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
this is failing on bots, reverting to investigate.

This reverts commit a16104e6da.
2023-08-14 13:31:02 -07:00
Justin Bogner
a16104e6da [Option] Add "Visibility" field and clone the OptTable APIs to use it
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-14 13:24:54 -07:00
Kyungwoo Lee
484c961ccd [lld-macho] Postprocess LC Linker Option
LLD resolves symbols regardless of LTO modes early when reading and parsing input files in order. The object files built from LTO passes are appended later.
Because LLD eagerly resolves the LC linker options while parsing a new object file (and its chain of dependent libraries), the prior decision on pending prevailing symbols (belonging to some bitcode files) can change to ones in those native libraries that are just loaded.

This patch delays processing LC linker options until all the native object files are added after LTO is done, similar to LD64. This way we preserve the decision on prevailing symbols LLD made, regardless of LTO modes.
 - When parsing a new object file in `parseLinkerOptions()`, it just parses LC linker options in the header, and saves those contents to `unprocessedLCLinkerOptions`.
 - After LTO is finished, `resolveLCLinkerOptions()` is called to recursively load dependent libraries, starting with initial linker options collected in `unprocessedLCLinkerOptions` (which also updates during recursions)

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D157716
2023-08-13 13:39:04 -07:00
Vy Nguyen
2d873d5aa4 [lld-macho]Rework error-checking in peeking at first-member in archive to avoid segfault.
Details:
calling getMemoryBufferRef() on an empty archive can trigger segfault so the code should check before calling this.
this seems like a bug in the Archive API but that can be fixed separately.

P.S: follow up to D156468

Differential Revision: https://reviews.llvm.org/D157300
2023-08-09 09:54:32 -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
Vy Nguyen
5ba906327b [lld-macho] Fixed crashes when linking with incompatible-arch archives/
Two changes:
 - Avoid crashing in predicate functions.
   Querying the property of the Symbols via these is*() functions shouldn't crash the program - the answer should just be "false".
   Currently, having them throw UNREACHABLE already (incorrectly) crash certain code paths involving macho::validateSymbolRelocation() .

 - Simply ignore input archives with incompatible arch (changes from PRESIDENT810@)

Differential Revision: https://reviews.llvm.org/D156468
2023-08-04 09:25:59 -04:00
Fangrui Song
fb2a971c01 [Support] Change MapVector's default template parameter to SmallVector<*, 0>
SmallVector<*, 0> is often a better replacement for std::vector :
both the object size and the code size are smaller.
(SmallMapVector uses SmallVector as well, but it is not common.)

clang size decreases by 0.0226%.
instructions:u decreases 0.037% when compiling a sqlite3 amalgram.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D156016
2023-07-24 22:04:03 -07:00
Fangrui Song
359f170f5f [lld-macho] Use fixed chunk size for UUID
Chunk size decided by the thread count makes the UUID less deterministic
(e.g. across machines with different core counts.)
Follow ELF and just use a fixed chunksize.

Fixes: https://github.com/llvm/llvm-project/issues/63961

Reviewed By: #lld-macho, keith

Differential Revision: https://reviews.llvm.org/D155761
2023-07-19 17:24:36 -07:00
Keith Smiley
f317ce218e
[lld-macho] Implement -no_uuid
Since UUID generation in lld is fast this is rarely used but it can be
helpful to avoid temporary issues like https://github.com/llvm/llvm-project/issues/63961

Differential Revision: https://reviews.llvm.org/D155735
2023-07-19 16:39:31 -07:00
Vy Nguyen
642ffbbf38 [lld-macho]Use install_name as Identifier for code-sign, if available.
Detail:
LD64 uses the name provided via  -[dylib]install_name as "Identifier", when available.
For compatiblity, LLD should do that too.

Differential Revision: https://reviews.llvm.org/D155508
2023-07-19 14:19:15 -04:00
Fangrui Song
2090d66b23 [lld-macho] Switch to xxh3_64bits
xxh3 is substantially faster than xxh64.
For lld/ELF, there is substantial speedup in `.debug_str` duplicate
elimination (D154813). Use xxh3 for lld-macho as well.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D155677
2023-07-19 09:58:44 -07: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
Keith Smiley
806f5b3019
[lld-macho] Switch to new tool ID
As of Xcode 15 there is now a tool ID for LLD, likely driven by Apple's
tests with using LLD for their CAS work in clang. This updates LLD to
use the correct ID, and updates the object library so that llvm-objdump
prints it correctly.

Differential Revision: https://reviews.llvm.org/D152929
2023-06-15 09:40:02 -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
Vy Nguyen
e60b30d5e3 Reland "D144999 [MC][MachO]Only emits compact-unwind format for "canonical" personality symbols. For the rest, use DWARFs."
Reasons for rolling forward:
    - the crash reported from Chromium was fixed in D151824 (not related to this patch at all)
    - since D152824 was committed, it should now be safe to roll this forward.

New change:
    - add an additional _ in name check

This reverts commit 4980eead4d.
2023-06-07 10:03:50 -04: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
Vy Nguyen
7e5f4ed556 [lld-macho]Ensure canonicalization happen even for "skipped" referent sections.
Details:

See bug report:  https://github.com/llvm/llvm-project/issues/63039

Differential Revision: https://reviews.llvm.org/D151824
2023-06-06 12:53:03 -04: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
Ellis Hoag
85af42df5d [lld] add context-sensitive PGO options for MachO
Enable support for CSPGO for lld MachO targets.

Since lld MachO does not support `-plugin-opt=`, we need to create the `--cs-profile-generate` and `--cs-profile-path=` options and propagate them in `Darwin.cpp`. These flags are not supported by ld64.

Also outline code into `getLastCSProfileGenerateArg()` to share between `CommonArgs.cpp` and `Darwin.cpp`.

CSPGO is already implemented for ELF (https://reviews.llvm.org/D56675) and COFF (https://reviews.llvm.org/D98763).

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D151589
2023-05-31 17:53:46 -07:00
Ellis Hoag
f92410429a [lld] Add --lto-debug-pass-manager option
Add support for printing the passes run for LTO.

Both ELF and COFF have `--lto-debug-pass-manager` (`-ltodebugpassmanager`) to print the compiler passes run during LTO. This is useful to check that a certain compiler pass is run in a test, e.g., https://reviews.llvm.org/D151589

Reviewed By: #lld-macho, MaskRay, int3

Differential Revision: https://reviews.llvm.org/D151746
2023-05-31 14:14:19 -07:00
Keith Smiley
48e5f704c5
[lld-macho] Remove linking bitcode support
Apple deprecated bitcode in the deployment process in Xcode 14.0. Last
month Apple started requiring Xcode 14.1+ to submit apps to the App
Store. Since there isn't a use for bundling bitcode outside of
submitting to the App Store we should be safe to delete this handling
entirely from LLD.

Differential Revision: https://reviews.llvm.org/D150697
2023-05-30 14:47:11 -07:00
Keith Smiley
f38f23bc9e
[lld-macho] Add support for .so file discovery
While not the recommended extension on macOS .so is supported by ld64.
This mirrors that behavior.

Related report: https://github.com/bazelbuild/bazel/issues/18464

Differential Revision: https://reviews.llvm.org/D151147
2023-05-23 13:50:14 -07:00
Vincent Lee
ed59b8a11c [lld-macho] Remove partially supported 32-bit ARM arch
We never really supported 32-bit ARM arch entirely, and partial support was added for
very specific features. Regardless, it fails to even link the most basic applications that at
this point, it might be better to move this arch as unsupported. Given that Apple will be
moving towards arm64 long term, I don't see any reason for anyone to invest time in
supporting this either, and for those who still need it should use apple's ld64 linker.

Fixes #62691

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D150544
2023-05-20 13:06:03 -07:00
Nico Weber
4980eead4d Revert "[RFC][MC][MachO]Only emits compact-unwind format for "canonical" personality symbols. For the rest, use DWARFs."
This reverts commit 09aaf53a05.
Causes toolchain asserts building libc++ for x86_64,
see https://reviews.llvm.org/D144999#4356215
2023-05-19 09:40:54 -04:00
Vy Nguyen
09aaf53a05 [RFC][MC][MachO]Only emits compact-unwind format for "canonical" personality symbols. For the rest, use DWARFs.
Details: https://github.com/rust-lang/rust/issues/102754

The MachO format uses 2 bits to encode these personality funtions, with 0 reserved for "no-personality".
This means we can only have up to 3 personality. There are already three popular personalities:  __gxx_personality_v0, __gcc_personality_v0, and __objc_personality_v0.
As a result, any system that needs custom-personality will run into a problem.

This patch implemented jyknight's proposal to simply force DWARFs for all non-canonical personality functions.

Differential Revision: https://reviews.llvm.org/D144999
2023-05-18 13:27:47 -04: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
Fangrui Song
71cb689661 [LTO] Change getThinLTOOutputFile to take StringRef 2023-04-26 20:43:11 -07:00
Jez Ng
9df7daa5ef [lld-macho] Tweak the names we give to archive members
In particular, make it `foo.a(foo.o)$ARCHIVE_OFFSET`. The goal is to
make it more similar to both ld64 implementation, which uses the
`foo.a(foo.o)$MODULE_ID` format. We dump some of these names in LTO
code, so matching ld64's format is helpful. This format is also more
similar to LLD-ELF's, which is `foo.a(foo.o at $ARCHIVE_OFFSET)`.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D148828
2023-04-20 17:24:06 -04:00
Keith Smiley
1167d67610
[lld-macho] Add new -reproducible flag
As of Xcode 14.3 it passes -reproducible by default to ld64. It seems
this flag was added in ld64 with Xcode 14.0, but it is not documented.
Through my testing the only thing I have seen it do is the same as
ZERO_AR_DATE, but it's possible it does more, or will do more in the
future. Since we already default to this option, this is more about
handling the command line flag to maintain Xcode compatibility than
anything else.

Differential Revision: https://reviews.llvm.org/D147663
2023-04-06 09:59:53 -07:00