Commit Graph

908 Commits

Author SHA1 Message Date
Arthur Eubanks
dca43a1c82 [lld/Macho][test] Mark objc-category-merging-minimal.s as unsupported on Windows (#113209)
With #112981, the test uses awk, which gnuwin32 doesn't seem to have.
2024-10-21 12:45:51 -07:00
Ellis Hoag
ed5072ee28 [NFC][lld-macho] Generate test bodies for icf-safe-thunk tests (#111927)
Autogenerate `.ll` code from cpp code in some `-icf-safe-thunk` tests
using `update_test_body.py`

```
PATH=build/bin:$PATH llvm/utils/update_test_body.py lld/test/MachO/icf-safe-thunks.ll lld/test/MachO/icf-safe-thunks-dwarf.ll
```
https://llvm.org/docs/TestingGuide.html#elaborated-tests

I recently became aware of this tool and I wanted to practice using it.
This also allows to remove the custom instructions to generate the `.ll`
code.
2024-10-21 10:46:21 -07:00
alx32
b35b583809 [lld-macho] Fix category merging sed issue - Try nr.2 (#112981)
We replace sed with awk as I couldn't find a syntax that works
consistently on Linux/Mac for sed.
Repro'ed original issue on Mac and confirmed working now on Mac/Linux.
2024-10-18 15:45:00 -07:00
alx32
caa9e41814 [lld-macho] Fix category merging sed issue (#112955)
Fix 'sed' spacing to ensure compatibility with all platforms. 

Original failure:
https://lab.llvm.org/buildbot/#/builders/190/builds/7903
```
RUN: at line 33: sed -E '/^__OBJC_\$_CATEGORY_MyBaseClass_\$_Category01:/ { n; s/^[ \t]*\.quad[ \t]+l_OBJC_CLASS_NAME_$/\t.quad\tL_OBJC_IMAGE_INFO+3/ }' merge_cat_minimal.s > merge_cat_minimal_bad_name.s
+ sed -E '/^__OBJC_\$_CATEGORY_MyBaseClass_\$_Category01:/ { n; s/^[ \t]*\.quad[ \t]+l_OBJC_CLASS_NAME_$/\t.quad\tL_OBJC_IMAGE_INFO+3/ }' merge_cat_minimal.s
sed: 1: "/^__OBJC_\$_CATEGORY_My ...": bad flag in substitute command: '}'
```
2024-10-18 11:51:21 -07:00
alx32
f9d3e98207 [lld-macho] Improve robustness of ObjC category merging (#112618)
This patch enhances the robustness of lld's Objective-C category
merging. Currently, the category merger assumes it can fully parse and
understand the format of all categories in the input, triggering an
assert if any invalid category data is encountered.

This will end up causing asserts in certain rare corner cases that are
difficult to reproduce in small test cases. The proposed changes modify
the behavior so that if invalid category data is detected, category
merging is skipped for that specific class and all other categories
sharing the same base class. This approach allows the linker to continue
processing other categories without failing entirely due to a single
problematic input.

We also add a LIT test to where we corrupt category data and check that
category merging for that class was skipped but the link was successful.
2024-10-18 11:03:16 -07:00
alx32
97a4324224 [lld-macho] Fix ICF differentiation of safe_thunks relocs (#111811)
In `--icf=safe_thunks` mode, the linker differentiates `keepUnique`
functions by creating thunks during a post-processing step after
Identical Code Folding (ICF). While this ensures that `keepUnique`
functions themselves are not incorrectly merged, it overlooks functions
that reference these `keepUnique` symbols.

If two functions are identical except for references to different
`keepUnique` functions, the current ICF algorithm incorrectly considers
them identical because it doesn't account for the future differentiation
introduced by thunks. This leads to incorrect deduplication of functions
that should remain distinct.

To address this issue, we modify the ICF comparison to explicitly check
for references to `keepUnique` functions during deduplication. By doing
so, functions that reference different `keepUnique` symbols are
correctly identified as distinct, preventing erroneous merging and
ensuring the correctness of the linked output.
2024-10-10 08:22:48 -07:00
Nuri Amari
2edd897a42 Make WriteIndexesThinBackend multi threaded (#109847)
We've noticed that for large builds executing thin-link can take on the
order of 10s of minutes. We are only using a single thread to write the
sharded indices and import files for each input bitcode file. While we
need to ensure the index file produced lists modules in a deterministic
order, that doesn't prevent us from executing the rest of the work in
parallel.

In this change we use a thread pool to execute as much of the backend's
work as possible in parallel. In local testing on a machine with 80
cores, this change makes a thin-link for ~100,000 input files run in ~2
minutes. Without this change it takes upwards of 10 minutes.

---------

Co-authored-by: Nuri Amari <nuriamari@fb.com>
2024-10-07 08:16:46 -07:00
alx32
9e862ae321 [lld-macho] Fix invalid DWARF with --icf=safe_thunks (#111097)
There is a bug in the current implementation of `--icf=safe_thunks`
where a STABS entry is emitted for generated thunks. This is problematic
as we end up generating invalid DWARF as dsymutil will think the entire
function body is at the thunk location, when in actuality there will
only be a single branch present. This will end up causing overlapping
DWARF entries.

To fix this we never generate STABS entries for such thunks. 
The existing `--icf=safe_thunks` test is updated to also generate debug
info and we add a check that no corrupt DWARF is generated.

As a future TODO we need to make `--keep-icf-stabs` compatible with
`--icf=safe_thunks`.
2024-10-04 21:48:09 -07:00
Kyungwoo Lee
f4763b3d24 Reland [CGData] LLD for MachO #90166 (#108733)
It reads raw CG data encoded in the custom section (__llvm_outline) in
object files and merges them into the indexed codegen data file
specified by -codegen-data-generate-path={path}.

This depends on https://github.com/llvm/llvm-project/pull/90074.
This is a patch for
https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
2024-09-15 06:39:06 -07:00
Kyungwoo Lee
9de260364b Revert "[CGData] LLD for MachO (#90166)"
This reverts commit 00c0b1ae20.
2024-09-14 21:15:53 -07:00
Kyungwoo Lee
00c0b1ae20 [CGData] LLD for MachO (#90166)
It reads raw CG data encoded in the custom section (__llvm_outline) in
object files and merges them into the indexed codegen data file
specified by `-codegen-data-generate-path={path}`.

This depends on https://github.com/llvm/llvm-project/pull/90074.
This is a patch for
https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753.
2024-09-14 20:38:14 -07:00
Ellis Hoag
ce91e2153f [lld][InstrProf] Sort startup functions for compression (#107348) 2024-09-06 09:22:03 -07:00
alx32
d1756165a9 [lld-macho][arm64] Enhance safe ICF with thunk-based deduplication (#106573)
Currently, our `safe` ICF mode only merges non-address-significant code,
leaving duplicate address-significant functions in the output. This
patch introduces `safe_thunks` ICF mode, which keeps a single master
copy of each function and replaces address-significant duplicates with
thunks that branch to the master copy.
Currently `--icf=safe_thunks` is only supported for `arm64`
architectures.

**Perf stats for a large binary:**
| ICF Option | Total Size | __text Size | __unwind_info | % total |

|-------------------|------------|-------------|---------------------|---------------------------|
| `--icf=none` | 91.738 MB | 55.220 MB | 1.424 MB | 0% |
| `--icf=safe` | 85.042 MB | 49.572 MB | 1.168 MB | 7.30% |
| `--icf=safe_thunks` | 84.650 MB | 49.219 MB | 1.143 MB | 7.72% |
| `--icf=all` | 82.060 MB | 48.726 MB | 1.111 MB | 10.55% |

So overall we can expect a `~0.45%` binary size reduction for a typical
large binary compared to the `--icf=safe` option.

**Runtime:**
Linking the above binary took ~10 seconds. Comparing the link
performance of --icf=safe_thunks vs --icf=safe, a ~2% slowdown was
observed.
2024-09-05 16:36:21 -07:00
Nico Weber
62e6c1ead7 [lld/mac] Allow -segprot having stricter initprot than maxprot on mac (#107269)
...including for catalyst.

The usecase for this is to put certain security-critical variables into
a special segment/section that's mapped as read-only most of the time,
and that temporary gets remapped as writeable when these variables are
written to be the program. This protects against them being written to
by heap spraying attacks. This special section should be mapped as
read-only at program start, so using

`-segprot MY_PROTECTED_MEMORY_THINGER rw r`

to mark that segment as rw maxprot and r initprot is exactly what we
want.

lld has so far rejected mismatching initprot and maxprot.

ld64 doesn't reject this, but silently writes initprot into both fields
(!) It looks like this might not be fully intentional, see
https://crbug.com/41495919#comment5 and
http://crbug.com/41495919#comment8.

In any case, when postprocessing ld64's output to have different values
for initprot and maxprot, the dynamic loader seems to do the right thing
(see also the previous two links).

The same technique also works on Windows, using both link.exe and
lld-link.exe using `/SECTION:myprotsect,R`.

So, since this is useful, allow it when targeting macOS, and make it do
what you'd expect.

Since loader support for this on iOS is less clear, keep disallowing it
there for now.

See the PR for the program I used to check that this seems to work. (I
only checked on arm64 macOS 14.5 so far; will run this on many more
systems on bots once this is merged and rolled in.)
2024-09-05 12:29:46 -04:00
Eisuke Kawashima
4514c381f3 [lld] Fix invalid Python escape sequences (#94033) 2024-08-31 16:37:49 +02:00
Jan Voung
fa4fbaefde Reapply: Use an abbrev to reduce size of VALUE_GUID records in ThinLTO summaries (#106165)
This retries #90692 which was reverted previously due to issues with
lld-available being set, even if the copy of lld is not built from
source.

This does not change any code compared to #90692 to address the
lld-available issue.
The main change w.r.t, lld-available is xfailing tests in PR #99056
(until a longer term fix is available).
2024-08-27 13:53:25 -04:00
Peter Rong
89a1f144f3 [LLD, MachO] Default objc_relative_method_lists on MacOS10.16+/iOS14+ (#104519)
This patch makes `-objc_relative_method_lists` default on MacOS
10.16+/iOS 14+. Manual override still work if command line argument is
provided.

To test this change, many explict arguments are removed from the test
files. Some explict `-objc_no_objc_relative_method_lists` are also added
for tests that don't support this yet.

This commit tries to revive #101360, which exposes a bug that breaks CI.
#104081 has fixed that bug.
2024-08-16 11:00:11 -07:00
alx32
0df91893ef [lld-macho] Fix crash: ObjC category merge + relative method lists (#104081)
A crash was happening when both ObjC Category Merging and Relative
method lists were enabled.

ObjC Category Merging creates new data sections and adds them by calling
`addInputSection`. `addInputSection` uses the symbols within the added
section to determine which container to actually add the section to.

The issue is that ObjC Category merging is calling `addInputSection`
before actually adding the relevant symbols the the added section. This
causes `addInputSection` to add the `InputSection` to the wrong
container, eventually resulting in a crash.

To fix this, we ensure that ObjC Category Merging calls
`addInputSection` only after the symbols have been added to the
`InputSection`.
2024-08-14 19:30:41 -07:00
Daniel Bertalan
51ed383d38 [lld-macho] Make relative method lists work on x86-64 (#103905)
Local data is referenced in Objective-C metadata via section + offset
relocations on x86-64 rather than via symbols. Without this change, we
would crash on incorrect casts of the referents to `Defined`.

A basic test based on the existing `objc-relative-method-lists-simple.s`
adopted to x86-64 is added.
2024-08-14 20:42:07 +02:00
Peter Rong
dd251b0504 Revert PR #101360 (#102420) 2024-08-07 21:33:04 -07:00
Peter Rong
6a3604ef85 [LLD, MachO] Default objc_relative_method_lists on MacOS11+/iOS14+ (#101360)
This patch makes `objc_relative_method_lists` default on MacOS 11+ / iOS
14+. Manual override still work if command line argument is provided.

To test this change, many explicit arguments are removed from the test
files. Some explicit `no_objc_relative...` are also added for tests that
don't support this yet.

Signed-off-by: Peter Rong <PeterRong@meta.com>
2024-08-07 13:27:49 -07:00
Harini0924
19f379420b [llvm-lit] Fix LLD Test when using lit internal shell (#102063)
Resolved the issue in MachO/lto-object-path.ll test where
'ZERO_AR_DATE=0' was not recognized as a command. Changed the test
command to set the environment variable correctly using 'env'. This
allows that the environment variable is set properly, allowing the test
to pass.
2024-08-05 22:50:23 -07:00
Ellis Hoag
f95bd62cf9 [lld][InstrProf] Add "Separate" irpgo-profile-sort option (#101084)
Add the "Separate" option `--irpgo-profile-sort <profile` instead of
just the "Joined" option `--irpgo-profile-sort=<profile>`. This is
useful if the path has a `,` for some reason which would break when
trying to use `-Wl,--irpgo-profile-sort=<profile-with-comma>`.

While I'm here, use `static_cast<>` instead of the C style cast
introduced in https://github.com/llvm/llvm-project/pull/100627
2024-08-01 11:20:01 -07:00
Fangrui Song
364ee4ee36 [lld-macho,test] Avoid writing to CWD
Don't write to `filelist` in the current working directory, whigh might
be read-only in some testing environmens.
2024-07-25 15:12:39 -07:00
David Blaikie
74e14605d7 Fix test to write to %T instead of the current working directory (which may not be writeable) 2024-07-24 22:00:59 +00:00
Sam Elliott
ea222be0d9 [MC] Honour alignment directive fill value for non-intel (#100136)
As reported in https://llvm.org/PR30955, `.balign` with a fill-value of 0 did
not actually align using zeroes, on non-x86 targets.

This is because the check of whether to use the code alignment routines
or whether to just use the fill value was checking whether the fill
value was equal to `TextAlignFillValue`, which has not been changed from
its default of 0 on most targets (it has been changed for x86). However,
most targets do not set the fill value because it doesn't entirely make
sense -- i.e. on AArch64 there's no reasonable byte value to use for
alignment, as instructions are word-sized and have to be well-aligned.

I think the check at the end `AsmParser::parseDirectiveAlign` is
suspicious even on x86 - if you use `.balign <align>, 0x90` in a code
section, you don't end up with a block of `0x90` repeated, you end up
with a block of NOPs of various widths. This functionality is never
tested.

The fix here is to modify the check to ignore the default text align
fill value when choosing to do code alignment or not.

Fixes #30303
2024-07-24 18:24:39 +01:00
Ellis Hoag
e3b30bc553 [lld][InstrProf] Profile guided function order (#96268)
Add the lld flags `--irpgo-profile-sort=<profile>` and
`--compression-sort={function,data,both}` to order functions to improve
startup time, and functions or data to improve compressed size,
respectively.

We use Balanced Partitioning to determine the best section order using
traces from IRPGO profiles (see
https://discourse.llvm.org/t/rfc-temporal-profiling-extension-for-irpgo/68068
for details) to improve startup time and using hashes of section
contents to improve compressed size.

In our recent LLVM talk (https://www.youtube.com/watch?v=yd4pbSTjwuA),
we showed that this can reduce page faults during startup by 40% on a
large iOS app and we can reduce compressed size by 0.8-3%.

More details can be found in https://dl.acm.org/doi/10.1145/3660635

---------

Co-authored-by: Vincent Lee <thevinster@users.noreply.github.com>
2024-07-23 08:34:40 -07:00
Leonard Grey
58f3c5e696 [lld-macho] Fix thunks for non-__text TEXT sections (#99052)
This supersedes https://github.com/llvm/llvm-project/pull/87818 and
fixes https://github.com/llvm/llvm-project/issues/52767

When calculating arm64 thunks, we make a few assumptions that may not
hold when considering code sections outside of `__text`:

1. That a section needs thunks only if its size is larger than the
branch range.
2. That any calls into `__stubs` are necessarily forward jumps (that is,
the section with the jump is ordered before `__stubs`)

Sections like this exist in the wild, most prominently the
`__lcxx_overrides` section introduced in
https://github.com/llvm/llvm-project/pull/69498

This change:
- Ensures that if one section in `__TEXT` gets thunks, all of them do.
- Makes all code sections in `__TEXT` contiguous (and guaranteed to be
placed before `__stubs`)
2024-07-23 11:02:55 -04:00
Daniel Bertalan
9a8b0407fc Reapply "[lld] enable fixup chains by default (#79894)" (#99255)
This reverts commit f55b79f59a.

The known issues with chained fixups have been addressed by #98913,
#98305, #97156 and #95171.

Compared to the original commit, support for xrOS (which postdates
chained fixups' introduction) was added and an unnecessary test change
was removed.

----------
Original commit message:

Enable chained fixups in lld when all platform and version criteria are
met. This is an attempt at simplifying the logic used in ld 907:


93d74eafc3/src/ld/Options.cpp (L5458-L5549)

Some changes were made to simplify the logic:
- only enable chained fixups for macOS from 13.0 to avoid the arch check
- only enable chained fixups for iphonesimulator from 16.0 to avoid the
arch check
- don't enable chained fixups for not specifically listed platforms
- don't enable chained fixups for arm64_32
2024-07-22 22:03:32 +02:00
alx32
37be437b12 [lld-macho] Fix erasing category names for ObjC categories (#99400)
We were already not deleting category names for Swift classes as those
names can be reused by other parts. However, I have come across a corner
case where this also happens for ObjC categories - so we can't delete
category names for them either. TODO remains to optimize this behavior
for both ObjC and Swift.
2024-07-19 23:17:59 -07:00
Fangrui Song
2d756d9d4c [lld-macho,test] Adjust reproduce-thin-archive-objc.s
When `cd %t` is used, it's conventional to move it above and omit `-o /dev/null`.

We don't check the string before `warning:` since (a) the string is not
very useful and (b) downstream might customize `ctx->e.logName`
(argv[0]).

count 0 is better than `--allow-empty`. In addition, without `2>&1` the
previous test was effective.
2024-07-19 20:12:14 -07:00
Daniel Bertalan
47b63cd508 [lld-macho] Save all thin archive members in repro tarball (#97169)
Previously, we only saved those members of thin archives into a repro
file that were actually used during linking. However, -ObjC handling
requires us to inspect all members, even those that don't end up being
loaded.

We weren't handling missing members correctly and crashed with an
"unhandled `Error`" failure in LLVM_ENABLE_ABI_BREAKING_CHECKS builds.

To fix this, we now eagerly load all object files and warn when
encountering missing members (in the instances where it wasn't a hard
error before). To avoid having to patch out the checks when dealing
with older repro files, the `--no-warn-thin-archive-missing-members`
flag is added as an escape hatch.
2024-07-18 16:26:32 +02:00
Daniel Bertalan
b1864a8d6a [lld-macho] Ignore duplicate -rpath entries (#99289)
Starting with Xcode 16 (dyld-1122), Apple's binary utilities, e.g.
`dyld_info` (but not dyld itself), will refuse to load binaries built
against the macOS 15 SDK or newer that contain the same `LC_RPATH`
entry multiple times:

https://github.com/apple-oss-distributions/dyld/blob/rel/dyld-1122/mach_o/Policy.cpp#L246-L249

`ld-prime` deduplicates entries (regardless of the deployment target),
we now do the same. We also match `ld-prime`'s and `ld64`'s behavior by
warning on duplicate `-rpath` arguments. This can be disabled by the
LLD-specific `--no-warn-duplicate-rpath` flag.
2024-07-18 10:49:19 +02:00
Daniel Bertalan
c7b08ac01f [lld-macho][test] Require "shell" feature for usage of ln -s (#99355)
The use of `ln -s` is not guaranteed to be supported on Windows.
2024-07-17 19:07:47 +02:00
Daniel Bertalan
6ad2987a72 [lld-macho] Omit __llvm_addrsig metadata from the output (#98913)
This section contains metadata that's only relevant for Identical Code
Folding at link time, we should not include it in the output.

We still treat it like a regular section during input file parsing (e.g.
create a `ConcatInputSection` for it), as we want its relocations to be
parsed. But it should not be passed to `addInputSection`, as that's what
assigns it to an `OutputSection` and adds it to the `inputSections`
vector which specifies the inputs to dead-stripping and relocation
scanning.

This fixes a "__DATA,__llvm_addrsig, offset 0: fixups overlap" error
when using `--icf=safe` alongside `-fixup_chains`. This occurs because
all `__llvm_addrsig` sections are 8 bytes large, and the relocations
which signify functions whose addresses are taken are all at offset 0.

This makes the fix in 5fa24ac2 ("Category Merger: add support for
addrsig references") obsolete, as we no longer try to resolve symbols
referenced in `__llvm_addrsig` when writing the output file. When we do
iterate its relocations in `markAddrSigSymbols`, we do not try to
resolve their addresses.
2024-07-17 00:41:36 +02:00
alx32
5cb7e49e73 [lld-macho] Fix -no_objc_category_merging flag (#98238)
`-no_objc_category_merging` flag was behaving like
`-objc_category_merging` - i.e. acting to enable category merging.
This is because we were using `hasArg` instead of `hasFlag` to test for
it. Fix this and add test to ensure it behaves as expected.
2024-07-10 14:37:05 -07:00
alx32
e97bd0300b [lld-macho] Category Merging: Detect correct class language for classes with aliased symbols (#97454)
Currently we rely on detecting the source language of a class by the
name of the symbol pointing to it. This generally works, but there are
scenarios where Swift generates both an ObjC name and a Swift name for
the symbol - by aliasing the ObjC name to the Swift name, as follows:

```
.globl	_OBJC_CLASS_$__TtC11MyTestClass11MyTestClass
.private_extern _OBJC_CLASS_$__TtC11MyTestClass11MyTestClass
.set _OBJC_CLASS_$__TtC11MyTestClass11MyTestClass, _$s11MyTestClassAACN
```

So to correctly detect the source language in all cases, we need to
handle scenarios where both an ObjC symbol and a Swift symbol is defined
for a class - in this case the symbol is always a Swift class.
2024-07-08 14:10:50 -07:00
Fangrui Song
ce863dd5a7 [lld-macho,test] Fix REQUIRES after #97639 2024-07-08 12:07:54 -07:00
Daniel Rodríguez Troitiño
840e507544 [lld] Do not implicitly link non "public" libraries (#97639)
The LC_SUB_CLIENT Mach-O command and the `allowable-clients` TBD entry
specify that the given framework (or library?) can only be linked
directly from the specified names, even if it is sitting in `/usr/lib`
or `/System/Library/Frameworks`.

Add a check for those conditions before checking if a library should be
implicitly linked, and link against their umbrella if they have
allowable clients. The code needs to be in both the binary libraries and
the interface libraries.

Add a test that reproduces the scenario in which a framework reexports a
private framework that sits in `/System/Library/Frameworks`, and check
for the symbols of the reexported framework to be associated with the
public framework, and not the private one.
2024-07-08 09:26:16 -07:00
Daniel Bertalan
d64efe42eb [lld-macho] Remove symbols to __mod_init_func with -init_offsets (#97156)
When `-fixup_chains`/`-init_offsets` is used, a different section,
`__init_offsets` is synthesized from `__mod_init_func`. If there are any
symbols defined inside `__mod_init_func`, they are added to the symbol
table unconditionally while processing the input files. Later, when
querying these symbols' addresses (when constructing the symtab or
exports trie), we crash with a null deref, as there is no output section
assigned to them.

Just making the symbols point to `__init_offsets` is a bad idea, as the
new section stores 32-bit integers instead of 64-bit pointers; accessing
the symbols would not do what the programmer intended. We should
entirely omit them from the output. This is what ld64 and ld-prime do.

This patch uses the same mechanism as dead-stripping to mark these
symbols as not needed in the output. There might be nicer fixes than the
workaround, this is discussed in #97155.

Fixes https://github.com/llvm/llvm-project/pull/79894#issuecomment-1944092892
Fixes #94716
2024-07-06 15:41:40 +02:00
alx32
9fa7f401b2 [lld-macho] Category merger: handle addends when getting symbol at offset (#91238)
Currently the `tryFindDefinedOnIsec` takes in an `InputSection` and an
`offset` and is supposed to return the target symbol that is referenced
on that `InputSection` at the given offset.

However, it does not deal with the reloc `addend` and might return the
incorrect symbol.
Here we add support for handling the reloc's `addend`.
2024-07-02 14:11:13 -07:00
Fangrui Song
5cf0b4d94e [lld-macho] Stabilize iteration order of config->exportedSymbols.literals
Otherwise lto-explicit-exports.ll might fail due to #96282.
2024-06-28 16:47:59 -07:00
Fangrui Song
ceeea91937 [lld-macho] Stabilize symbol order from InterfaceFile
llvm/include/llvm/TextAPI/InterfaceFile.h SymbolsSet uses a
llvm::DenseMap<SymbolsMapKey, Symbol *>, which has a non-deterministic
order. For now, stabilize the order in lld since it is much simpler
than refactoring InterfaceFile.h.
2024-06-28 16:36:29 -07:00
alx32
93318a8b73 [lld-macho] Add swift support to ObjC category merger (#95124)
Currently ObjC category merger only supports categories defined in ObjC.
But swift supports generating ObjC categories also. This change adds
supports for the later categories.
2024-06-13 08:06:25 -07:00
Daniel Bertalan
b5c850f718 [lld-macho] Mark local personality functions as INDIRECT_SYMBOL_LOCAL (#95171)
This expands on the fix in 4e572db. The issue is pretty similar: we
might put symbols in the GOT which don't need run-time binding, locally
defined personality symbols in this case. We should set their indirect
symbol table entries to `INDIRECT_SYMBOL_LOCAL` to help `strip` remove
these local names from the symbol table.

Checking if the symbol is private-extern doesn't cover all cases; it can
also be a non-weak extern function too, for instance; use the
`needsBinding()` helper to determine it. This was the case for the
personality function in statically linked Rust executables.

The extra non-`LOCAL` symbols triggered a bug in Apple's `strip`
implementation. As the indirect value for the personality function was
not set to the flag, but the symbol didn't require binding, it tried to
make the symbol local, overwriting the GOT entry with the function's
address in the process. This normally wouldn't be a problem, but if
chained fixups are used, the fixup also encodes the offset to the next
fixup, and it effectively zeroed this offset out, causing the remaining
relocations on the page to not be performed by dyld.

This caused the crash in https://issues.chromium.org/issues/325410295

The change in tests is a bit ugly, as a lot of symbol information is now
removed by turning more symbols `LOCAL`.
2024-06-13 15:16:55 +02:00
Daniel Bertalan
6afbda7130 [lld-macho] Fix duplicate GOT entries for personality functions (#95054)
As stated in `UnwindInfoSectionImpl::prepareRelocations`'s comments, the
unwind info uses section+addend relocations for personality functions
defined in the same file as the function itself. As personality
functions are always accessed via the GOT, we need to resolve those to a
symbol. Previously, we did this by keeping a map which resolves these to
symbols, creating a synthetic symbol if we didn't find it in the map.

This approach has an issue: if we process the object file containing the
personality function before any external uses, the entry in the map
remains unpopulated, so we create a synthetic symbol and a corresponding
GOT entry. If we encounter a relocation to it in a later file which
requires GOT (such as in `__eh_frame`), we add that symbol to the GOT,
too, effectively creating two entries which point to the same piece of
code.

This commit fixes that by searching the personality function's section
for a symbol at that offset which already has a GOT entry, and only
creating a synthetic symbol if there is none. As all non-unwind sections
are already processed by this point, it ensures no duplication.

This should only really affect our tests (and make them clearer), as
personality functions are usually defined in platform runtime libraries.
Or even if they are local, they are likely not in the first object file
to be linked.
2024-06-11 21:51:28 +02:00
Nuri Amari
1697030d9d [MachO LLD] Respect -all_load with --start-lib --end-lib style archives (#93993)
The -all_load flag is intended to force the linker to load all lazy members, but doesn't do so if the archive is specified with --start-lib, --end-lib flags. The `-all_load` flag is global, that is it can be placed anywhere in the linker invocation, and it affects the load behavior of all conventional archives listed. Unlike ELF's --whole-archive, the user need not necessarily have access to the entire linker invocation to reasonably make use of the flag. The user can supply `-all_load` to a build system without inspecting the rest of the linker invocation.

To make the behavior of `--start-lib` style archives consistent with regular archives, this patch makes it so that -all_load also applies in this case.
2024-05-31 18:17:51 -07:00
alx32
b963931eb8 [lld-macho][ObjC] Implement category merging into base class (#92448)
Currently category merging only supports merging multiple categories
into one. With this commit we add the ability to fully merge categories
into the base class, if the base class is included in the current
module. This is the optimal approach for defined classes.
2024-05-28 10:21:22 -07:00
alx32
5eea4f4425 [lld-macho] Add flag --keep-icf-stabs to LLD for MachO (#93137)
This change adds the `--keep-icf-stabs` which, when specified, preserves
symbols that were folded by ICF in the binary's stabs entries.
This allows `dsymutil` to process debug information for the folded
symbols.
2024-05-27 19:07:39 -07:00
klensy
f97f039e0b [lld,test] Fix few FileCheck annotation typos (#92238) 2024-05-15 15:11:14 -07:00