Commit Graph

15656 Commits

Author SHA1 Message Date
Saleem Abdulrasool 8f45b5a7a9 RISCV: permit unaligned nop-slide padding emission
We may be requested to emit an unaligned nop sequence (e.g. 7-bytes or
3-bytes).  These should be 0-filled even though that is not a valid
instruction.  This matches the behaviour on other architectures like
ARM, X86, and MIPS.  When a custom section is emitted, it may be
classified as text even though it may be a data section or we may be
emitting data into a text segment (e.g. a literal pool).  In such cases,
we should be resilient to the emission request.

This was originally identified by the Linux kernel build and reported on
D131270 by Nathan Chancellor.

Differential Revision: https://reviews.llvm.org/D132482
Reviewed By: luismarques
Tested By: Nathan Chancellor
2022-08-24 20:26:48 +00:00
Fangrui Song 3b4d800911 [ELF] Parallelize writes of different OutputSections
We currently process one OutputSection at a time and for each OutputSection
write contained input sections in parallel. This strategy does not leverage
multi-threading well. Instead, parallelize writes of different OutputSections.

The default TaskSize for parallelFor often leads to inferior sharding. We
prepare the task in the caller instead.

* Move llvm::parallel::detail::TaskGroup to llvm::parallel::TaskGroup
* Add llvm::parallel::TaskGroup::execute.
* Change writeSections to declare TaskGroup and pass it to writeTo.

Speed-up with --threads=8:

* clang -DCMAKE_BUILD_TYPE=Release: 1.11x as fast
* clang -DCMAKE_BUILD_TYPE=Debug: 1.10x as fast
* chrome -DCMAKE_BUILD_TYPE=Release: 1.04x as fast
* scylladb build/release: 1.09x as fast

On M1, many benchmarks are a small fraction of a percentage faster. Mozilla showed the largest difference with the patch being about 1.03x as fast.

Differential Revision: https://reviews.llvm.org/D131247
2022-08-24 09:40:03 -07:00
John Ericson ad8c34bc30 [CMake] Avoid `LLVM_BINARY_DIR` when other more specific variable are better-suited
A simple sed doing these substitutions:

- `${LLVM_BINARY_DIR}/(\$\{CMAKE_CFG_INTDIR}/)?lib(${LLVM_LIBDIR_SUFFIX})?\>` -> `${LLVM_LIBRARY_DIR}`
- `${LLVM_BINARY_DIR}/(\$\{CMAKE_CFG_INTDIR}/)?bin\>` -> `${LLVM_TOOLS_BINARY_DIR}`

where `\>` means "word boundary".

The only manual modifications were reverting changes in

- `compiler-rt/cmake/Modules/CompilerRTUtils.cmake
- `runtimes/CMakeLists.txt`

because these were "entry points" where we wanted to tread carefully not not introduce a "loop" which would end with an undefined variable being expanded to nothing.

This hopefully increases readability overall, and also decreases the usages of `LLVM_LIBDIR_SUFFIX`, preparing us for D130586.

Reviewed By: sebastian-ne

Differential Revision: https://reviews.llvm.org/D132316
2022-08-24 10:14:05 -04:00
Shoaib Meenai 491a5c9570 [MachO] Fix formatting. NFC
The style guide says that all arms of an if-else should have braces if
any arm does [1].

[1] https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements
2022-08-22 23:00:07 +03:00
Kazu Hirata 8b1b0d1d81 Revert "Use std::is_same_v instead of std::is_same (NFC)"
This reverts commit c5da37e42d.

This patch seems to break builds with some versions of MSVC.
2022-08-20 23:00:39 -07:00
Kazu Hirata c5da37e42d Use std::is_same_v instead of std::is_same (NFC) 2022-08-20 22:36:26 -07:00
Kazu Hirata 8e494b85a5 Use llvm::drop_begin (NFC) 2022-08-20 21:18:30 -07:00
Kazu Hirata 258531b7ac Remove redundant initialization of Optional (NFC) 2022-08-20 21:18:28 -07:00
Kazu Hirata 9e296584ce Fix unused variable warnings
These warnings came up with gcc-11.3.0.
2022-08-20 00:12:35 -07:00
Fangrui Song e0f64b319e [docs] Remove lld/ELF release note about --package-metadata= which is in release/15.x 2022-08-19 19:08:29 -07:00
Sam Clegg 2cd4cd9a32 [lld][ELF] Rename SymbolTable::symbols() to SymbolTable::getSymbols(). NFC
This change renames this method match its original name and the name
used in the wasm linker.

Back in d8f8abbd4a the ELF SymbolTable
method `getSymbols()` was replaced with `forEachSymbol`.

Then in a2fc964417 `forEachSymbol` was
replaced with a `llvm::iterator_range`.

Then in e9262edf0d we came full circle
and the `llvm::iterator_range` was replaced with a `symbols()` accessor
that was identical the original `getSymbols()`.

`getSymbols` also matches the name used elsewhere in the ELF linker as
well as in both COFF and wasm backend (e.g. `InputFiles.h` and
`SyntheticSections.h`)

Differential Revision: https://reviews.llvm.org/D130787
2022-08-19 14:56:08 -07:00
John Ericson e941b031d3 Revert "[cmake] Use `CMAKE_INSTALL_LIBDIR` too"
This reverts commit f7a33090a9.

Unfortunately this causes a number of failures that didn't show up in my
local build.
2022-08-18 22:46:32 -04:00
John Ericson f7a33090a9 [cmake] Use `CMAKE_INSTALL_LIBDIR` too
We held off on this before as `LLVM_LIBDIR_SUFFIX` conflicted with it.
Now we return this.

`LLVM_LIBDIR_SUFFIX` is kept as a deprecated way to set
`CMAKE_INSTALL_LIBDIR`. The other `*_LIBDIR_SUFFIX` are just removed
entirely.

I imagine this is too potentially-breaking to make LLVM 15. That's fine.
I have a more minimal version of this in the disto (NixOS) patches for
LLVM 15 (like previous versions). This more expansive version I will
test harder after the release is cut.

Reviewed By: sebastian-ne, ldionne, #libc, #libc_abi

Differential Revision: https://reviews.llvm.org/D130586
2022-08-18 15:33:35 -04:00
Fangrui Song 4c244b2833 [ELF] Fix .plt.got comments. NFC 2022-08-16 23:29:01 -07:00
Daniel Bertalan 1b67ce79e3 [lld-macho] Honor weak and thread-local flags for TAPI symbols
Differential Revision: https://reviews.llvm.org/D131995
2022-08-17 07:03:24 +02:00
Nico Weber 330abe8d43 fix comment typo to cycle bots 2022-08-15 18:49:02 -04:00
Kazu Hirata f5a68feab3 Use llvm::none_of (NFC) 2022-08-14 16:25:39 -07:00
Nico Weber b696500b9a [lld/mac] Stop accepting arbitrary suffixes on --(no-)warn-dylib-install-name
The flag accidentally used Joined<> instead of Flag<>.

Previously, `--warn-dylib-install-namefoobarbaz` would be accepted and
had the same effect as `-warn-dylib-install-name`. Now the flag only
works if no suffix is attached to it, as originally intended.

Also fix a typo in the flag's help text.

Differential Revision: https://reviews.llvm.org/D131781
2022-08-12 15:27:02 -04:00
Leonard Grey 6c62795058 [lld-macho] Ensure cached objects are affected by `-object_path_lto`
This refactors LTO compile to look more like COFF, where cache hits and misses are all funneled through the same code path.

Previously, cache hits were *not* being saved to -object_path_lto, which led to them sometimes falling out of the cache before dsymutil could process them. As a side effect of the refactor, cached objects are now saved with -save-temps as well, which seems desirable.

(Deleted lld/test/MachO/lto-cache-dsymutil.ll and rolled it into lld/test/MachO/lto-object-path.ll, since the cache-only, non object path approach is unreliable anyway).

Differential Revision: https://reviews.llvm.org/D131624
2022-08-12 10:56:58 -04:00
Nico Weber 39a093471a [lld/mac] Add missing `_eq` suffix on two (internal) option names
No behavior change.
2022-08-12 08:18:52 -04:00
Martin Storsjö e6db064394 [doc] Remove release notes from the main branch for changes that were backported to 15.x 2022-08-12 11:21:51 +03:00
Martin Storsjö 5d513ef6cf [LLD] [COFF] Add support for a new, mingw specific embedded directive -exclude-symbols:
This is an entirely new embedded directive - extending the GNU ld
command line option --exclude-symbols to be usable in embedded
directives too.

(GNU ld.bfd also got support for the same new directive, currently in
the latest git version, after the 2.39 branch.)

This works as an inverse to the regular embedded dllexport directives,
for cases when autoexport of all eligible symbols is performed.

Differential Revision: https://reviews.llvm.org/D130120
2022-08-11 11:59:48 +03:00
Martin Storsjö d1da6469f9 [LLD] [MinGW] Implement the --exclude-symbols option
This adds support for the existing GNU ld command line option, which
allows excluding individual symbols from autoexport (when linking a
DLL and no symbols are marked explicitly as dllexported).

Differential Revision: https://reviews.llvm.org/D130118
2022-08-11 11:59:47 +03:00
Keith Smiley 3c24fae398
[lld-macho] Add support for objc_msgSend stubs
Apple Clang in Xcode 14 introduced a new feature for reducing the
overhead of objc_msgSend calls by deduplicating the setup calls for each
individual selector. This works by clang adding undefined symbols for
each selector called in a translation unit, such as `_objc_msgSend$foo`
for calling the `foo` method on any `NSObject`. There are 2
different modes for this behavior, the default directly does the setup
for `_objc_msgSend` and calls it, and the smaller option does the
selector setup, and then calls the standard `_objc_msgSend` stub
function.

The general overview of how this works is:

- Undefined symbols with the given prefix are collected
- The suffix of each matching undefined symbol is added as a string to
  `__objc_methname`
- A pointer is added for every method name in the `__objc_selrefs`
  section
- A `got` entry is emitted for `_objc_msgSend`
- Stubs are emitting pointing to the synthesized locations

Notes:

- Both `__objc_methname` and `__objc_selrefs` can also exist from object
  files, so their contents are merged with our synthesized contents
- The compiler emits method names for defined methods, but not for
  undefined symbols you call, but stubs are used for both
- This only implements the default "fast" mode currently just to reduce
  the diff, I also doubt many folks will care to swap modes
- This only implements this for arm64 and x86_64, we don't need to
  implement this for 32 bit iOS archs, but we should implement it for
  watchOS archs in a later diff

Differential Revision: https://reviews.llvm.org/D128108
2022-08-10 17:17:17 -07:00
Nico Weber 87248ba5b1 [lld/elf] Use C++17 nested namespace syntax in most places
Like D131405, but for ELF.

No behavior change.

Differential Revision: https://reviews.llvm.org/D131612
2022-08-10 16:47:30 -04:00
Fangrui Song e3fcf2e06f [ELF] Simplify llvm::enumerate with structured binding. NFC 2022-08-09 21:52:08 -07:00
Fangrui Song 596f490ab9 [ELF] De-template BitcodeFile::parse. NFC 2022-08-09 21:46:28 -07:00
Fangrui Song dc55ab3840 [ELF] De-template createBitcodeSymbol. NFC 2022-08-09 21:43:36 -07:00
Fangrui Song dfe2a3f3eb [COFF] Change a llvm::StringSet<> to llvm::DenseSet<StringRef>. NFC
The former stores strings and is therefore more expensive.
2022-08-09 17:50:42 -07:00
Pengxuan Zheng c951edb7b2 [LLD][COFF] Identify /GL object files which are inside libraries
With D26647, we can already identify input object files compiled by cl.exe with
/GL. It seems to be helpful to do the same and print an error message for those
object files compiled with /GL but are inside libraries/archives too.

Reviewed By: rnk, thieta

Differential Revision: https://reviews.llvm.org/D131458
2022-08-09 12:32:35 -07:00
Keith Smiley ee27bca2e2
[lld-macho][test] Rename dtrace test (NFC)
This matches the convention used elsewhere

Differential Revision: https://reviews.llvm.org/D131456
2022-08-09 08:40:08 -07:00
Alex Brachet dbd04b853b [ELF] Support --package-metadata
This was recently introduced in GNU linkers and it makes sense for
ld.lld to have the same support. This implementation omits checking if
the input string is valid json to reduce size bloat.

Differential Revision: https://reviews.llvm.org/D131439
2022-08-08 21:31:58 +00:00
Nico Weber 09db7f5331 [lld/mac] Remove unusual "Fallthrough" comments
Normally we'd use LLVM_FALLTHROUGH, or now, [[fallthrough]].
But for case labels followed directly by other case labels, we
use neither.

No behavior change.
2022-08-08 14:18:44 -04:00
Nico Weber aa1abd7684 [lld/win] Use C++17 structured bindings
No behavior change.

Differential Revision: https://reviews.llvm.org/D131403
2022-08-08 11:51:38 -04:00
Nico Weber 7c26641d9d [lld/win] Use C++17 nested namespace syntax in most places
Like D131354, but for COFF.

No behavior change.

Differential Revision: https://reviews.llvm.org/D131405
2022-08-08 11:48:12 -04:00
Nico Weber 2f3256a02e [lld/mac] Use C++17 structured bindings in two more places
No behavior change.
2022-08-08 11:09:44 -04:00
Tobias Hieta 576375a2d6
[LLD][COFF] Ignore DEBUG_S_XFGHASH_TYPE/VIRTUAL
These are new debug types that ships with the latest
Windows SDK and would warn and finally fail lld-link.

The symbols seems to be related to Microsoft's XFG
which is their version of CFG. We can't handle any of
this yet, so for now we can just ignore these types
so that lld doesn't fail with a new version of Windows
SDK.

Fixes: #56285

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D129378
2022-08-08 15:53:52 +02:00
Nico Weber b99da9d255 [lld/mac] Use C++17 structured bindings
No behavior change.

Differential Revision: https://reviews.llvm.org/D131355
2022-08-08 07:21:28 -04:00
Nico Weber bf20d43f82 [lld/mac] Use C++17 nested namespace syntax in most places
Some header files used

    namespace lld {
    namespace macho {
    // ...
    } // namespace macho
    std::string toString(const Type &t);
    } // namespace lld

In those files, I didn't use a nested namespace since it's not a big win there.

No behavior change.

Differential Revision: https://reviews.llvm.org/D131354
2022-08-08 07:11:17 -04:00
Fangrui Song ec04e45c03 [lld] LLVM_FALLTHROUGH => [[fallthrough]]. NFC
With C++17 there is no Clang pedantic warning or MSVC C5051.
2022-08-07 00:02:35 +00:00
Fangrui Song bccdf9197b Revert "[lld-macho] Work around odr-use of const non-inline static data member to fix -O0 build after D128298"
This reverts commit 20b2d3260d.
The workaround is no longer needed for C++17.
2022-08-06 16:44:14 -07:00
Tom Stellard 91f3f0bf31 lld/cmake: Drop use of llvm-config for LLVM install discovery
This has been deprecated since D116492 earlier in 2022.

That seems recent, but with the recent cut of LLVM 15 that is still two releases (14 and 15). Meanwhile Clang has deprecated `llvm-config` for a lot longer, and since it is likely that LLD users are also Clang users, this serves as an extra "heads up" that `llvm-config` is on its way out.

Remove it in favor of using CMake's find_package() function.

Reviewed By: MaskRay, mgorny

Differential Revision: https://reviews.llvm.org/D131144
2022-08-06 16:24:04 -04:00
Fangrui Song e45a5696bb [ELF] toString(const InputFile *): synchronize toStringCache
The function may be called currently for diagnostics.
2022-08-06 01:00:06 -07:00
Tobias Hieta b1356504e6
[LLVM] Update C++ standard to 17
Also make the soft toolchain requirements hard. This allows
us to use C++17 features in LLVM now.

If we find patterns with C++17 that improve readability
it should be recommended in the coding standards.

Reviewed By: jhenderson, cor3ntin, MaskRay

Differential Revision: https://reviews.llvm.org/D130689
2022-08-06 09:42:10 +02:00
Fangrui Song d7cbfcf36a [ELF][AArch64] Fix potentially corrupted section content for PAC
D74537 introduced a bug: if `(config->andFeatures & GNU_PROPERTY_AARCH64_FEATURE_1_PAC) != 0`
with -z pac-plt unspecified, we incorrectly use AArch64BtiPac, whose writePlt will make
out-of-bounds write after the .plt section. This is often benign because the
output section after .plt will usually overwrite the content.

This is very difficult to test without D131247 (Parallelize writes of different OutputSections).
2022-08-05 18:24:54 -07:00
Fangrui Song e89d6d2ac5 [ELF] Keep only getTarget() call. NFC
The place from D61712 seems unneeded now. We can just use the place added by
D62609 (support AArch64 BTI/PAC).
2022-08-05 18:20:23 -07:00
Fangrui Song abd9807590 [ELF] mergeCmp: work around irreflexivity bug
Some tests (e.g. aarch64-feature-pac.s) segfault in libstdc++ _GLIBCXX_DEBUG
builds (enabled by LLVM_ENABLE_EXPENSIVE_CHECKS).

dyn_cast<ThunkSection> is incorrectly true for any SyntheticSection. std::merge
transitively calls mergeCmp(x, x) (due to __glibcxx_requires_irreflexive_pred)
and will segfault in `ta->getTargetInputSection()`. The dyn_cast<ThunkSection>
issue should be eventually fixed properly, bug `a != b` is robust enough for now.
2022-08-05 17:08:37 -07:00
Fangrui Song 28d05d6723 [ELF][PPC64] Fix potentially corrupted section content with empty .got
D91426 makes .got possibly empty while needed. If .got and .data have the same
address, and .got's content is written after .data, the first word of .data will
be corrupted.

The bug is not testable without D131247.
2022-08-05 15:22:57 -07:00
Fangrui Song 2515cb80cd [ELF] Parallelize input section initialization
This implements the last step of
https://discourse.llvm.org/t/parallel-input-file-parsing/60164 for the ELF port.

For an ELF object file, we previously did: parse, (parallel) initializeLocalSymbols, (parallel) postParseObjectFile.
Now we do: parse, (parallel) initSectionsAndLocalSyms, (parallel) postParseObjectFile.

initSectionsAndLocalSyms does most of input section initialization.
The sequential `parse` does SHT_ARM_ATTRIBUTES/SHT_RISCV_ATTRIBUTES/SHT_GROUP initialization for now.

Performance linking some programs with --threads=8 (glibc 2.33 malloc and mimalloc):

* clang: 1.05x as fast with glibc malloc, 1.03x as fast with mimalloc
* chrome: 1.04x as fast with glibc malloc, 1.03x as fast with mimalloc
* internal search program: 1.08x as fast with glibc malloc, 1.05x as fast with mimalloc

Reviewed By: peter.smith

Differential Revision: https://reviews.llvm.org/D130810
2022-08-04 11:47:52 -07:00
Fangrui Song f6bd0a8f2b [ELF] Add makeThreadLocal/makeThreadLocalN and remove InputFile::localSymStorage
makeThreadLocal/makeThreadLocalN are moved from D130810 ([ELF] Parallelize input
section initialization) here to make D130810 more focused on the refactor:

* COFF has some needs for multiple linker contexts. D108850 partially removed
  global states from lldCommon but left the global variable `lctx`.
* To the best of my knowledge, all multiple-linker-context feature requests to
  ELF are more from user convenience, with no very strong argument.
* In practice, ELF port is very difficult to remove global states without
  introducing significant performance regression/hurting code readability.
* Per-thread allocators from D122922/D123879 are too expensive and will not
  really benefit ELF.

This patch adds a simple thread_local based makeThreadLocal to
lld/Common/Memory.h. It will enable further optimization in ELF.
2022-08-04 11:09:40 -07:00
Fangrui Song 077b16aa6c [ELF] Remove unneeded make<InputSection>. NFC 2022-08-03 21:51:39 -07:00
Fangrui Song e2a932dd8a [ELF] Move updateARMVFPArgs/updateARMVFPArgs. NFC
To reduce diff for D130810.
2022-08-03 21:49:17 -07:00
Martin Storsjö 59c6f418fa [LLD] [MachO] Fix GCC build warnings
This fixes the following warnings produced by GCC 9:

    ../tools/lld/MachO/Arch/ARM64.cpp: In member function ‘void {anonymous}::OptimizationHintContext::applyAdrpLdr(const lld::macho::OptimizationHint&)’:
    ../tools/lld/MachO/Arch/ARM64.cpp:448:18: warning: comparison of integer expressions of different signedness: ‘int64_t’ {aka ‘long int’} and ‘uint64_t’ {aka ‘long unsigned int’} [-Wsign-compare]
      448 |   if (ldr.offset != (rel1->referentVA & 0xfff))
          |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ../tools/lld/MachO/UnwindInfoSection.cpp: In function ‘bool canFoldEncoding(compact_unwind_encoding_t)’:
    ../tools/lld/MachO/UnwindInfoSection.cpp:404:44: warning: comparison between ‘enum<unnamed>’ and ‘enum<unnamed>’ [-Wenum-compare]
      404 |   static_assert(UNWIND_X86_64_MODE_MASK == UNWIND_X86_MODE_MASK, "");
          |                                            ^~~~~~~~~~~~~~~~~~~~
    ../tools/lld/MachO/UnwindInfoSection.cpp:405:49: warning: comparison between ‘enum<unnamed>’ and ‘enum<unnamed>’ [-Wenum-compare]
      405 |   static_assert(UNWIND_X86_64_MODE_STACK_IND == UNWIND_X86_MODE_STACK_IND, "");
          |                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~

Differential Revision: https://reviews.llvm.org/D130970
2022-08-03 00:14:39 +03:00
Gabriel Ravier 5dbd8faad5 [lld] Fixed a number of typos
I went over the output of the following mess of a command:

`(ulimit -m 2000000; ulimit -v 2000000; git ls-files -z | parallel --xargs -0 cat | aspell list --mode=none --ignore-case | grep -E '^[A-Za-z][a-z]*$' | sort | uniq -c | sort -n | grep -vE '.{25}' | aspell pipe -W3 | grep : | cut -d' ' -f2 | less)`

and proceeded to spend a few days looking at it to find probable typos
and fixed a few hundred of them in all of the llvm project (note, the
ones I found are not anywhere near all of them, but it seems like a
good start).

Differential Revision: https://reviews.llvm.org/D130982
2022-08-02 09:52:31 -04:00
Fangrui Song dde41c6c56 [ELF] --reproduce: strip directories for --print-archive-stats= and --why-extract=
Similar to -o and -Map.
2022-08-01 22:06:46 -07:00
Fangrui Song d0cf7b2015 [ELF] EhInputSection::getParentOffset: fix out-of-bounds access for symbols relative to a non-empty .eh_frame
This has unclear semantics and can be considered invalid. Return an arbitrary value.
2022-08-01 01:10:51 -07:00
Fangrui Song b2559f2f5c [ELF] .eh_frame: remove config->wordsize padding
Linux Standard Base Core Specification says that CIE/FDE is padded to an
addressing unit size boundary, but in practice GNU assembler/LLVM integrated
assembler pad FDE/CIE to 4 and the last FDE to 8 on 64-bit systems.

In addition, GNU ld doesn't pad to 8, so let's drop excess padding, too.
If the assembler provides aligned pieces, the output will be aligned.

Noticed .eh_frame size reduction for 3 executables: 0.3% (chrome), 4.7% (clang),
7.6% (an internal program).
2022-07-31 23:35:44 -07:00
Fangrui Song 81ed005c4c [ELF] Remove EhFrameSection::addSection. NFC 2022-07-31 19:55:05 -07:00
Fangrui Song b3452f8f13 [ELF] redirectSymbols: skip versioned symbol combine if config->versionDefinitions.size() == 2 2022-07-31 17:47:09 -07:00
Jez Ng 6c9f681252 [lld-macho] Support EH frame pointer encodings that use sdata4
Previously we only supporting using the system pointer size (aka the
`absptr` encoding) because `llvm-mc`'s CFI directives always generate EH
frames with that encoding. But libffi uses 4-byte-encoded, hand-rolled
EH frames, so this patch adds support for it.

Fixes #56576.

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D130804
2022-07-31 20:16:33 -04:00
Fangrui Song af1328ef45 [ELF] Simplify EhInputSection::split. NFC
* Inline getReloc
* Fold the UINT32_MAX length check into the section size check.
  This transformation is valid because we don't support .eh_frame input sections
  larger than 32-bit (unrealistic even for large code models).
2022-07-31 16:59:57 -07:00
Fangrui Song 3e9adff456 [ELF] Split EhInputSection::pieces into cies and fdes
This simplifies code, removes a read32 (for id==0 check), and makes it feasible
to combine some operations in EhInputSection::split and EhFrameSection::addRecords.

Mostly NFC, but fixes "Relocation not in any piece" assertion failure in an
erroneous case when a relocation offset precedes all CIE/FDE pices.
2022-07-31 16:16:10 -07:00
Fangrui Song 7c158b9ed1 [ELF][test] Merge .eh_frame tests 2022-07-31 14:52:20 -07:00
Fangrui Song a9c5d09c5e [ELF][test] Test identical CIE content with different personality
If we change
CieRecord *&rec = cieMap[{cie.data(), personality}];
to
CieRecord *&rec = cieMap[{cie.data(), nullptr}];

The new test can catch the failure.
2022-07-31 13:43:07 -07:00
Fangrui Song c09d323599 [ELF] Move EhInputSection out of inputSections. NFC
inputSections temporarily contains EhInputSection objects mainly for
combineEhSections. Place EhInputSection objects into a new vector
ehInputSections instead of inputSections.
2022-07-31 11:58:08 -07:00
Fangrui Song 4b2b68d5ab [lld] Change vector to SmallVector. NFC
My lld executable is 1.6KiB smaller and some functions are now more efficient.
2022-07-30 18:11:21 -07:00
Fangrui Song a465e79f19 [ELF] Move SyntheticSections to InputSection.h. NFC
Keep the main SectionBase hierarchy in InputSection.h.
And inline MergeInputSection::getParent.
2022-07-30 17:42:08 -07:00
Fangrui Song 0a28cfdff5 [ELF] Simplify getRankProximity. NFC 2022-07-30 16:32:42 -07:00
Fangrui Song 3046e7d8e7 [ELF] Support missing relocations in RISCV::getImplicitAddend() 2022-07-30 10:55:59 -07:00
Fangrui Song 97b767eb3b [ELF] Support missing relocations in PPC64::getImplicitAddend() 2022-07-30 10:46:58 -07:00
Kazu Hirata 729a76433a [lld] Remove unused forward declarations (NFC) 2022-07-30 10:35:46 -07:00
Fangrui Song efb7a71a13 [ELF] Support missing relocations in AArch64::getImplicitAddend() 2022-07-30 01:24:55 -07:00
Fangrui Song c4c68b676e [ELF] Write R_AARCH64_IRELATIVE addends with -z rel 2022-07-30 01:19:59 -07:00
Kazu Hirata 873888c179 Use is_sorted (NFC) 2022-07-29 21:18:42 -07:00
Fangrui Song d6d4f631ac [ELF] Move addDependentLibrary/handleSectionGroup. NFC
To reduce diff for my upcoming parallel input section initialization patch.
2022-07-29 17:07:09 -07:00
Sam Clegg b0f18af30b [lld][WebAssemby] Demote LazySymbols back to undefined symbols if they are not loaded
A LazySymbol is one that lives in `.a` archive and gets pulled in by a
strong reference.  However, weak references to such symbols do not
result in them be loaded from the archive.  In this case we want to
treat such symbols at undefined rather then lazy, once symbols
resolution is complete.

This fixes a crash bug in the linker when weakly referenced symbol that
lives in an archive file is live at the end of the link.  In the case of
dynamic linking this is expected to turn into an import with (in the
case of a function symbol) a function index.

Differential Revision: https://reviews.llvm.org/D130736
2022-07-29 13:53:54 -07:00
Sam Clegg 48129cf0ed [lld][WebAsssembly] Convert more tests from IR to assembly. NFC
Differential Revision: https://reviews.llvm.org/D130738
2022-07-29 13:52:52 -07:00
Nico Weber 2681c9e065 [lld/mac] Comment changes requested on https://reviews.llvm.org/D130725
No behavior change.
2022-07-29 12:55:48 -04:00
Alex Brachet 7baf1db392 [ELF] Strip directories for -Map when emitting reproducer rsp
Similarly to -o output directories will not be created so -Map being
copied verbatim will likely cause ld.lld @response.txt to fail.

Differential Revision: https://reviews.llvm.org/D130681
2022-07-29 16:17:33 +00:00
Fangrui Song a041ce3eb1 [ELF] CallGraphSort: replace vector<int> with unique_ptr<int[]>. NFC
We can't use C++20 make_unique_for_overwrite yet.
2022-07-29 00:59:48 -07:00
Fangrui Song 2e2d5304f0 [ELF] Move combineEhSections from Writer to SyntheticSections. NFC
This not only places the function in the right place, but also allows inlining addSection.
2022-07-29 00:47:30 -07:00
Fangrui Song c72973608d [ELF] Combine EhInputSection removal and MergeInputSection removal. NFC 2022-07-29 00:39:57 -07:00
Fangrui Song 8d4b11b4f1 [ELF] Remove redundant isa<InputSection>(sec). NFC
combineEhSections has been called to remove EhInputSection.
2022-07-29 00:30:52 -07:00
Fangrui Song 4db7c7dbb4 [ELF] Remove one inputSections loop. NFC 2022-07-29 00:03:50 -07:00
Nico Weber 241f0e8b76 [lld/mac] Add support for $ld$previous symbols with explicit symbol name
A symbol `$ld$previous$/Another$1.2.3$1$3.0$14.0$_xxx$` means
"pretend symbol `_xxx` is in dylib `/Another` with version `1.2.3`
if the deployment target is between `3.0` and `14.0` and we're
targeting platform `1` (ie macOS)".

This means dylibs can now inject synthetic dylibs into the link, so
DylibFile needs to grow a 3rd constructor.

The only other interesting thing is that such an injected dylib
counts as a use of the original dylib. This patch gets this mostly
right (if _only_ `$ld$previous` symbols are used from a dylib,
we don't add a dep on the dylib itself, matching ld64), but one case
where we don't match ld64 yet is that ld64 even omits the original
dylib when linking it with `-needed-l`. Lld currently still adds a load
command for the original dylib in that case. (That's for a future
patch.)

Fixes #56074.

Differential Revision: https://reviews.llvm.org/D130725
2022-07-28 20:35:48 -04:00
Vincent Lee f030132c72 [lld-macho] Allow linking with ABI compatible architectures
Linking fails when targeting `x86_64-apple-darwin` for runtimes. The issue
is that LLD strictly assumes the target architecture be present in the tbd
files (which isn't always true). For example, when targeting `x86_64h`, it should
work with `x86_64` because they are ABI compatible. This is also inline with what
ld64 does.

An environment variable (which ld64 also supports) is also added to preserve the
existing behavior of strict architecture matching.

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D130683
2022-07-28 17:16:32 -07:00
John Ericson cc56a5022c [clang][lld][cmake] Simplify header dirs
We don't need to recompute the list LLVMConfig.cmake provides us.

When LLVM is being built, the list is two elements long: generated headers and headers from source.

When LLVM is already built,the list is one element long: the installed header directory containing both generated and hand-written sources.

Reviewed By: sebastian-ne

Differential Revision: https://reviews.llvm.org/D130553
2022-07-28 19:36:40 -04:00
Jez Ng 31760e8189 [lld-macho] `-exported_symbols` should hide symbols before LTO runs
We were previously doing it after LTO, which did have the desired effect
of having the un-exported symbols marked as private extern in the final
output binary, but doing it before LTO creates more optimization
opportunities.

One observable difference is that LTO can now elide un-exported symbols
entirely, so they may not even be present as private externs in the
output.

This is also what ld64 implements.

Reviewed By: #lld-macho, thevinster

Differential Revision: https://reviews.llvm.org/D130429
2022-07-28 17:55:49 -04:00
Daniel Bertalan d1e40f4d58 [lld-macho] Add LOH_ARM64_ADRP_ADD_LDR optimization hint support
This hint instructs the linker to optimize an adrp+add+ldr sequence used
for loading from a local symbol's address by loading directly if it's
close enough, or with an adrp(p)+ldr sequence if it's not.

This transformation is the same as what's done for ADRP_LDR_GOT_LDR when
the symbol is local. The logic for acting on this hint is therefore
moved to a new function which will be called from the existing
applyAdrpLdrGotLdr() function.

Differential Revision: https://reviews.llvm.org/D130505
2022-07-28 10:45:28 +02:00
Daniel Bertalan f2c7f75f61 [lld-macho] Support creating N_SO stab for DWARF5 compile units
In DWARF5, the `DW_AT_name` and `DW_AT_comp_dir` attributes are encoded
using the `strx*` forms, which specify an index into `__debug_str_offs`.
This commit adds that section to DwarfObject, so the debug info parser
can resolve these references.

The test case was manually adapted from stabs-icf.s.

Fixes #51668

Differential Revision: https://reviews.llvm.org/D130559
2022-07-28 09:58:26 +02:00
Fangrui Song 876af563e7 [ELF][test] Fix compressed-debug-level.test with zlib-ng
The test is brittle but it seems unnecessary to remove size check now.

Close #56222
2022-07-27 11:18:20 -07:00
Tom Stellard 809855b56f Bump the trunk major version to 16 2022-07-26 21:34:45 -07:00
Keith Smiley e3857791a7
[lld-macho] Fix -bitcode_process_mode arg type
This is still undocumented and unsupported, but if someone passed it
before you would end up with a missing file error since this takes an
argument that wouldn't be handled.

Differential Revision: https://reviews.llvm.org/D130606
2022-07-26 20:58:17 -07:00
Fangrui Song b3270888cb [ELF] addDependentLibrary: fix a use-after-free bug in archiveName 2022-07-26 14:52:07 -07:00
Fangrui Song 6d0b42745e [ELF] addLibrary: fix a use-after-free bug in archiveName
It manifests as an incorrect name in --print-archive-stats=.
2022-07-26 14:36:57 -07:00
Fangrui Song e27a029ed0 [ELF][test] Clean up print-archive-stats.s 2022-07-26 14:36:09 -07:00
Arthur Eubanks 2eade1dba4 [WPD] Use new llvm.public.type.test intrinsic for potentially publicly visible classes
Turning on opaque pointers has uncovered an issue with WPD where we currently pattern match away `assume(type.test)` in WPD so that a later LTT doesn't resolve the type test to undef and introduce an `assume(false)`. The pattern matching can fail in cases where we transform two `assume(type.test)`s into `assume(phi(type.test.1, type.test.2))`.

Currently we create `assume(type.test)` for all virtual calls that might be devirtualized. This is to support `-Wl,--lto-whole-program-visibility`.

To prevent this, all virtual calls that may not be in the same LTO module instead use a new `llvm.public.type.test` intrinsic in place of the `llvm.type.test`. Then when we know if `-Wl,--lto-whole-program-visibility` is passed or not, we can either replace all `llvm.public.type.test` with `llvm.type.test`, or replace all `llvm.public.type.test` with `true`. This prevents WPD from trying to pattern match away `assume(type.test)` for public virtual calls when failing the pattern matching will result in miscompiles.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D128955
2022-07-26 08:01:08 -07:00
Sebastian Neubauer ae222dae56 [CMake] Fix add_subdirectory llvm builds
Fixes a regression from D117973, that used CMAKE_BINARY_DIR instead of
LLVM_BINARY_DIR in some places.

Differential Revision: https://reviews.llvm.org/D130555
2022-07-26 11:24:23 +02:00
Simon Tatham 2b38f58930 [llvm-objdump,ARM] Add PrettyPrinters for Arm and AArch64.
Most Arm disassemblers, including GNU objdump and Arm's own `fromelf`,
emit an instruction's raw encoding as a 32-bit words or (for Thumb)
one or two 16-bit halfwords, in logical order rather than according to
their storage endianness. This is generally easier to read: it matches
the encoding diagrams in the architecture spec, it matches the value
you'd write in a `.inst` directive, and it means that fields within
the instruction encoding that span more than one byte (such as branch
offsets or `SVC` immediates) can be read directly in the encoding
without having to mentally reverse the bytes.

llvm-objdump already has a system of PrettyPrinter subclasses which
makes it easy for a target to drop in its own preferred formatting.
This patch adds pretty-printers for all the Arm targets, so that
llvm-objdump will display Arm instruction encodings in their preferred
layout instead of little-endian and bytewise.

Reviewed By: DavidSpickett

Differential Revision: https://reviews.llvm.org/D130358
2022-07-26 09:35:30 +01:00
Daniel Bertalan b2c53a9217 [lld-macho] Implement -hidden-l
Similarly to -load_hidden, this flag instructs the linker to not export
symbols from the specified archive. While that flag takes a path,
-hidden-l looks for the specified library name in the search path.

The test changes are needed because -hidden-lfoo resolves to libfoo.a,
not foo.a.

Differential Revision: https://reviews.llvm.org/D130529
2022-07-26 01:41:51 +02:00
John Ericson ac0d1d5c7b [cmake] Support custom package install paths
Firstly, we we make an additional GNUInstallDirs-style variable. With
NixOS, for example, this is crucial as we want those to go in
`${dev}/lib/cmake` not `${out}/lib/cmake` as that would a cmake subdir
of the "regular" libdir, which is installed even when no one needs to do
any development.

Secondly, we make *Config.cmake robust to absolute package install
paths. We for NixOS will in fact be passing them absolute paths to make
the `${dev}` vs `${out}` distinction mentioned above, and the
GNUInstallDirs-style variables are suposed to support absolute paths in
general so it's good practice besides the NixOS use-case.

Thirdly, we make `${project}_INSTALL_PACKAGE_DIR` CACHE PATHs like other
install dirs are.

Reviewed By: sebastian-ne

Differential Revision: https://reviews.llvm.org/D117973
2022-07-25 21:02:53 +00:00
Daniel Bertalan 595fc59f74 Reland "[lld-macho] Implement -load_hidden"
This flag was introduced in ld64-609. It instructs the linker to link to
a static library while treating its symbols as if they had hidden
visibility. This is useful when building a dylib that links to static
libraries but we don't want the symbols from those to be exported.

Closes #51505

This reland adds bitcode file handling, so we won't get any compile
errors due to BitcodeFile::forceHidden being unused.

Differential Revision: https://reviews.llvm.org/D130473
2022-07-25 22:51:24 +02:00
Daniel Bertalan 9bf1c6dabf Revert "[lld-macho] Implement -load_hidden"
This reverts commit 4c79e1a3f4.

Broke this bot: https://lab.llvm.org/buildbot/#builders/57/builds/20319
2022-07-25 21:11:19 +02:00
Daniel Bertalan 4c79e1a3f4 [lld-macho] Implement -load_hidden
This flag was introduced in ld64-609. It instructs the linker to link to
a static library while treating its symbols as if they had hidden
visibility. This is useful when building a dylib that links to static
libraries but we don't want the symbols from those to be exported.

Closes #51505

Differential Revision: https://reviews.llvm.org/D130473
2022-07-25 20:59:33 +02:00
Daniel Bertalan fd30414206 [lld-macho] Demangle location name in undefined symbol diagnostics
If the `-demangle` flag is passed to lld, symbol names will now be
demangled in the "referenced by:" message in addition to the referenced
symbol's name, which was already demangled before this change.

Differential Revision: https://reviews.llvm.org/D130490
2022-07-25 18:42:16 +02:00
Fangrui Song b89407153d [ELF] --reproduce: support --export-dynamic-symbol-list 2022-07-25 01:20:32 -07:00
Fangrui Song cbcdb5248d [ELF] Simplify --build-id/--color-diagnostics with AliasArgs. NFC 2022-07-25 01:14:53 -07:00
Kazu Hirata 95a932fb15 Remove redundaunt override specifiers (NFC)
Identified with modernize-use-override.
2022-07-24 22:28:11 -07:00
Fangrui Song 85cfd91723 [ELF] Optimize some non-constant alignTo with alignToPowerOf2. NFC
My x86-64 lld executable is 2KiB smaller. .eh_frame writing gets faster as there
were lots of divisions.
2022-07-24 11:20:49 -07:00
Kazu Hirata c730f9a164 Convert for_each to range-based for loops (NFC) 2022-07-23 12:17:27 -07:00
Jez Ng ee61dc5f6c [lld-macho][nfc] Reduce nesting of code added in D130125 2022-07-23 13:16:00 -04:00
Kazu Hirata 1cc7f5bede Use static_assert instead of assert (NFC)
Identified with misc-static-assert.
2022-07-23 09:22:27 -07:00
Jez Ng d23da0ec6c [lld-macho] Fold __objc_imageinfo sections
Previously, we treated it as a regular ConcatInputSection. However, ld64
actually parses its contents and uses that to synthesize a single image
info struct, generating one 8-byte section instead of `8 * number of
object files with ObjC code`.

I'm not entirely sure what impact this section has on the runtime, so I
just tried to follow ld64's semantics as closely as possible in this
diff. My main motivation though was to reduce binary size.

No significant perf change on chromium_framework on my 16-core Mac Pro:

             base           diff           difference (95% CI)
  sys_time   1.764 ± 0.062  1.748 ± 0.032  [  -2.4% ..   +0.5%]
  user_time  5.112 ± 0.104  5.106 ± 0.046  [  -0.9% ..   +0.7%]
  wall_time  6.111 ± 0.184  6.085 ± 0.076  [  -1.6% ..   +0.8%]
  samples    30             32

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D130125
2022-07-23 12:12:01 -04:00
Jez Ng b35e0d0cf3 [lld-macho] Fix segfault when handling LTO + object file weak defs
which occurs when there are EH frames present in the object file's weak
def.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D130409
2022-07-23 11:48:45 -04:00
Dmitri Gribenko aba43035bd Use llvm::sort instead of std::sort where possible
llvm::sort is beneficial even when we use the iterator-based overload,
since it can optionally shuffle the elements (to detect
non-determinism). However llvm::sort is not usable everywhere, for
example, in compiler-rt.

Reviewed By: nhaehnle

Differential Revision: https://reviews.llvm.org/D130406
2022-07-23 15:19:05 +02:00
Fangrui Song 50f5f37b07 [ELF] Internalize isBitcode. NFC 2022-07-22 11:56:46 -07:00
Fangrui Song e8fd49f24b [ELF] --fortran-common: remove unneeded identify_magic call. NFC 2022-07-22 11:54:27 -07:00
Fangrui Song 242316bc27 [ELF] Simplify createObjectFile/createLazyFile. NFC
And avoid redundant identify_magic test.
2022-07-22 01:26:12 -07:00
Kazu Hirata 70257fab68 Use any_of (NFC) 2022-07-22 01:05:17 -07:00
John Ericson 07b749800c [cmake] Don't export `LLVM_TOOLS_INSTALL_DIR` anymore
First of all, `LLVM_TOOLS_INSTALL_DIR` put there breaks our NixOS
builds, because `LLVM_TOOLS_INSTALL_DIR` defined the same as
`CMAKE_INSTALL_BINDIR` becomes an *absolute* path, and then when
downstream projects try to install there too this breaks because our
builds always install to fresh directories for isolation's sake.

Second of all, note that `LLVM_TOOLS_INSTALL_DIR` stands out against the
other specially crafted `LLVM_CONFIG_*` variables substituted in
`llvm/cmake/modules/LLVMConfig.cmake.in`.

@beanz added it in d0e1c2a550 to fix a
dangling reference in `AddLLVM`, but I am suspicious of how this
variable doesn't follow the pattern.

Those other ones are carefully made to be build-time vs install-time
variables depending on which `LLVMConfig.cmake` is being generated, are
carefully made relative as appropriate, etc. etc. For my NixOS use-case
they are also fine because they are never used as downstream install
variables, only for reading not writing.

To avoid the problems I face, and restore symmetry, I deleted the
exported and arranged to have many `${project}_TOOLS_INSTALL_DIR`s.
`AddLLVM` now instead expects each project to define its own, and they
do so based on `CMAKE_INSTALL_BINDIR`. `LLVMConfig` still exports
`LLVM_TOOLS_BINARY_DIR` which is the location for the tools defined in
the usual way, matching the other remaining exported variables.

For the `AddLLVM` changes, I tried to copy the existing pattern of
internal vs non-internal or for LLVM vs for downstream function/macro
names, but it would good to confirm I did that correctly.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D117977
2022-07-21 19:04:00 +00:00
Jez Ng ec315a5fa1 [lld-macho] Fix LOH parsing segfault
`advanceSubsection()` didn't account for the possibility that a section
could have no subsections.

Reviewed By: #lld-macho, thakis, BertalanD

Differential Revision: https://reviews.llvm.org/D130288
2022-07-21 13:59:39 -04:00
Jez Ng 241f62d8d3 [lld-macho] Fix assertion when two symbols at same addr have unwind info
If there are multiple symbols at the same address, our unwind info
implementation assumes that we always register unwind entries to a
single canonical symbol.

This assumption was violated by the `registerEhFrame` code.

Fixes #56570.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D130208
2022-07-21 09:44:49 -04:00
Daniel Bertalan 888d0a5ef2 [lld-macho][NFC] Remove redundant StringRef construction
It's only used in one branch, so we were unnecessarily calculating the
length of many symbol names.

Tiny speedup when linking chromium_framework on my M1 Mac mini:

x before.txt
+ after.txt

    N           Min           Max        Median           Avg        Stddev
x  10     3.9917109        4.0418     4.0318099     4.0203902   0.021459873
+  10      3.944725      4.053988     3.9708955     3.9825602   0.037257609
Difference at 95.0% confidence
	-0.03783 +/- 0.0285663
	-0.940953% +/- 0.710536%
	(Student's t, pooled s = 0.0304028)

Differential Revision: https://reviews.llvm.org/D130234
2022-07-21 15:36:56 +02:00
Daniel Bertalan 54e18b2397 [lld-macho] Optimize rebase opcode generation
This commit reduces the size of the emitted rebase sections by
generating the REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB and
REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB opcodes.

With this change, chromium_framework's rebase section is a 40% smaller
197 kilobytes, down from the previous 320 kB. That is 6 kB smaller than
what ld64 produces for the same input.

Performance figures from my M1 Mac mini:

x before
+ after

    N           Min           Max        Median           Avg        Stddev
x  10     4.2269349     4.3300061     4.2689675     4.2690016   0.031151669
+  10      4.219331     4.2914009     4.2398136     4.2448277   0.023817308
No difference proven at 95.0% confidence

Differential Revision: https://reviews.llvm.org/D130180
2022-07-21 10:00:39 +02:00
Keith Smiley 15f685eaa8
[lld-macho] Fold cfstrings with --deduplicate-literals
Similar to cstrings ld64 always deduplicates cfstrings. This was already
being done when enabling ICF, but for debug builds you may want to flip
this on if you cannot eliminate your instances of this, so this change
makes --deduplicate-literals also apply to cfstrings.

Differential Revision: https://reviews.llvm.org/D130134
2022-07-20 11:11:09 -07:00
Kazu Hirata 360c1111e3 Use llvm::is_contained (NFC) 2022-07-20 09:09:19 -07:00
Martin Storsjö 801971e5b4 [LLD] [COFF] Improve the error message for too many exported symbols
Print the actual number of symbols that would have been exported
too, which helps assessing the situation.

Differential Revision: https://reviews.llvm.org/D130117
2022-07-20 16:58:29 +03:00
Jez Ng 87ce7b41d8 [lld-macho] Simplify archive loading logic
This is a follow-on to {D129556}. I've refactored the code such that
`addFile()` no longer needs to take an extra parameter. Additionally,
the "do we force-load or not" policy logic is now fully contained within
addFile, instead of being split between `addFile` and
`parseLCLinkerOptions`. This also allows us to move the `ForceLoad` (now
`LoadType`) enum out of the header file.

Additionally, we can now correctly report loads induced by
`LC_LINKER_OPTION` in our `-why_load` output.

I've also added another test to check that CLI library non-force-loads
take precedence over `LC_LINKER_OPTION` + `-force_load_swift_libs`. (The
existing logic is correct, just untested.)

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D130137
2022-07-19 21:56:24 -04:00
Jez Ng 2d889a87fb [lld-macho] Read in new addrsig format
The new format uses symbol relocations, as described in {D127637}.

Reviewed By: #lld-macho, alx32

Differential Revision: https://reviews.llvm.org/D128938
2022-07-19 21:22:27 -04:00
Kaining Zhong dd5635541c [lld-macho] Fix loading same libraries from both LC_LINKER_OPTION and command line
This fixes https://github.com/llvm/llvm-project/issues/56059 and
https://github.com/llvm/llvm-project/issues/56440. This is inspired by
tapthaker's patch (https://reviews.llvm.org/D127941), and has reused his
test cases. This patch adds an bool "isCommandLineLoad" to indicate
where archives are from. If lld tries to load the same library loaded
previously by LC_LINKER_OPTION from CLI, it will use this
isCommandLineLoad to determine if it should be affected by -all_load &
-ObjC flags. This also prevents -force_load from affecting archives
loaded previously from CLI without such flag, whereas tapthaker's patch
will fail such test case (introduced by
https://reviews.llvm.org/D128025).

Reviewed By: int3, #lld-macho

Differential Revision: https://reviews.llvm.org/D129556
2022-07-19 17:46:14 -04:00
Keith Smiley 0bc100986c
[lld-macho] Add support for -alias
This creates a symbol alias similar to --defsym in the elf linker. This
is used by swiftpm for all executables, so it's useful to support. This
doesn't implement -alias_list but that could be done pretty easily as
needed.

Differential Revision: https://reviews.llvm.org/D129938
2022-07-19 13:55:56 -07:00
Arthur Eubanks 5bce73ba75 [test] Convert some tests to use opaque pointers 2022-07-19 13:11:08 -07:00
Jez Ng f6017abb60 [lld-macho] Support folding of functions with identical LSDAs
To do this, we need to slice away the LSDA pointer, just like we are
slicing away the functionAddress pointer.

No observable difference in perf on chromium_framework:

             base           diff           difference (95% CI)
  sys_time   1.769 ± 0.068  1.761 ± 0.065  [  -2.7% ..   +1.8%]
  user_time  9.517 ± 0.110  9.528 ± 0.116  [  -0.6% ..   +0.8%]
  wall_time  8.291 ± 0.174  8.307 ± 0.183  [  -1.1% ..   +1.5%]
  samples    21             25

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D129830
2022-07-19 13:29:52 -04:00
Daniel Bertalan 1fb9466c6a [lld-macho] Devirtualize TargetInfo::getRelocAttrs
This method is called on each relocation when parsing input files, so
the overhead of using virtual functions ends up being quite large.  We
now have a single non-virtual method, which reads from the appropriate
array of relocation attributes set in the TargetInfo constructor.

This change results in a modest 2.3% reduction in link time for
chromium_framework measured on an x86-64 VPS, and 0.7% on an arm64 Mac.

    N           Min           Max        Median           Avg        Stddev
x  10     11.869417     12.032609     11.935041     11.938268   0.045802324
+  10     11.581526     11.785265     11.649885     11.659507   0.054634834
Difference at 95.0% confidence
	-0.278761 +/- 0.0473673
	-2.33502% +/- 0.396768%
	(Student's t, pooled s = 0.0504124)

Differential Revision: https://reviews.llvm.org/D130000
2022-07-18 19:32:58 +02:00
Nico Weber 7b3146dcd3 fix comment typo to cycle bots 2022-07-17 09:10:05 -04:00
Daniel Bertalan 2b2e858e9c [lld-macho] Handle filename being passed in -lto_object_path
Clang passes a filename rather than a directory in -lto_object_path when
using FullLTO. Previously, it was always treated it as a directory, so
lld would crash when it attempted to create temporary files inside it.

Fixes #54805

Differential Revision: https://reviews.llvm.org/D129705
2022-07-16 21:46:47 +02:00
Jez Ng fe47cfb324 [lld-macho][nfc] Add more tests + comments around ICF + unwind info interaction
While working on {D129830}, I realized that our handling of ICF +
eh_frame combined was untested. Additionally I realized that the comment
explaining why we were safely slicing away the functionAddress reloc
from our compact unwind entries was... insufficient and slightly
misleading. I've tried to clarify it.

Reviewed By: #lld-macho, thevinster

Differential Revision: https://reviews.llvm.org/D129894
2022-07-16 00:52:47 -04:00
Kazu Hirata 5cff5142a8 Use value instead of getValue (NFC) 2022-07-15 20:03:13 -07:00
Jez Ng dbbdc3d6fb [lld-macho][nfc] Fix numeric substitutions in icf.s test
We were re-defining the various numeric variables when we actually
intended to check already-defined variables against the value on the
current CHECK line.

Reviewed By: #lld-macho, thakis

Differential Revision: https://reviews.llvm.org/D129831
2022-07-15 15:28:27 -04:00
Fangrui Song f77b77e8db [ELF][RISCV] Relax local-exec TLS model
In -mrelax mode, GCC/Clang may generate a local-exec TLS code sequence like:
```
# R_RISCV_TPREL_HI20, R_RISCV_RELAX
lui rd, %tprel_hi(x)
# R_RISCV_TPREL_ADD, R_RISCV_RELAX
add rd, rd, tp, %tprel_add(x)
# (R_RISCV_TPREL_LO12_I || R_RISCV_TPREL_LO12_S), R_RISCV_RELAX
addi rd, rd, %tprel_lo(x) || sw rs, %tprel(x)(rd)
```

Note: st_value(x) for TLS should be in the range [0,p_memsz(PT_TLS)).
When st_value(x) < 2048 (i.e. hi20(x) == 0), the linker can relax the code
sequence to:
```
addi rd, tp, st_value(x) || sw rs, st_value(x)(rd)
```

Differential Revision: https://reviews.llvm.org/D129425
2022-07-15 10:08:08 -07:00
Fangrui Song 51b9e099d5 [ELF] Reword --no-allow-shlib-undefined diagnostic
Use a format more similar to unresolved references from regular object
files. It's probably easier to read for people who are less familiar
with the linker diagnostics.

Reviewed By: ikudrin

Differential Revision: https://reviews.llvm.org/D129790
2022-07-15 01:29:58 -07:00
Alexandre Ganea 17a4427e82 [LLD][COFF] On Windows, fix the date formatting in the 'incremental' test.
On my system the date formatting is a bit different from what the test used to
support. I'm using:

  Windows 11 version 21H2, build 22000.795 using the English(Canada) region.
  ls from BusyBox 1.36
  VS 2022 17.2.5
  WinSDK 10.0.22000
2022-07-14 17:10:09 -04:00
Fangrui Song 889c6f3996 [ELF][test] Fix a typo in aarch64-ifunc-bti.s to actually test what was intended
Thanks to Alex Brachet for spotting it in D110217.
2022-07-14 13:46:38 -07:00
Jez Ng 403d61aedd [lld-macho] Enable EH frame relocation / pruning
This just removes the code that gates the logic. The main issue here is
perf impact: without {D122258}, LLD takes a significant perf hit because
it now has to do a lot more work in the input parsing phase. But with
that change to eliminate unnecessary EH frames from input object files,
the perf overhead here is minimal. Concretely, here are the numbers for
some builds as measured on my 16-core Mac Pro:

**chromium_framework**

This is without the use of `-femit-dwarf-unwind=no-compact-unwind`:

             base           diff           difference (95% CI)
  sys_time   1.826 ± 0.019  1.962 ± 0.034  [  +6.5% ..   +8.4%]
  user_time  9.306 ± 0.054  9.926 ± 0.082  [  +6.2% ..   +7.1%]
  wall_time  8.225 ± 0.068  8.947 ± 0.128  [  +8.0% ..   +9.6%]
  samples    15             22

With that flag enabled, the regression mostly disappears, as hoped:

             base           diff           difference (95% CI)
  sys_time   1.839 ± 0.062  1.866 ± 0.068  [  -0.9% ..   +3.8%]
  user_time  9.452 ± 0.068  9.490 ± 0.067  [  -0.1% ..   +0.9%]
  wall_time  8.383 ± 0.127  8.452 ± 0.114  [  -0.1% ..   +1.8%]
  samples    17             21

**Unnamed internal app**

Without `-femit-dwarf-unwind`, this is the perf hit:

             base           diff           difference (95% CI)
  sys_time   1.372 ± 0.029  1.317 ± 0.024  [  -4.6% ..   -3.5%]
  user_time  2.835 ± 0.028  2.980 ± 0.027  [  +4.8% ..   +5.4%]
  wall_time  3.205 ± 0.079  3.383 ± 0.066  [  +4.9% ..   +6.2%]
  samples    102            83

With `-femit-dwarf-unwind`, the perf hit almost disappears:

             base           diff           difference (95% CI)
  sys_time   1.274 ± 0.026  1.270 ± 0.025  [  -0.9% ..   +0.3%]
  user_time  2.812 ± 0.023  2.822 ± 0.035  [  +0.1% ..   +0.7%]
  wall_time  3.166 ± 0.047  3.174 ± 0.059  [  -0.2% ..   +0.7%]
  samples    95             97

Just for fun, I measured the impact of `-femit-dwarf-unwind` on ld64
(`base` has the extra DWARF unwind info in the input object files,
`diff` doesn't):

             base           diff           difference (95% CI)
  sys_time   1.128 ± 0.010  1.124 ± 0.023  [  -1.3% ..   +0.6%]
  user_time  7.176 ± 0.030  7.106 ± 0.094  [  -1.5% ..   -0.4%]
  wall_time  7.874 ± 0.041  7.795 ± 0.121  [  -1.7% ..   -0.3%]
  samples    16             25

And for LLD:

             base           diff           difference (95% CI)
  sys_time   1.315 ± 0.019  1.280 ± 0.019  [  -3.2% ..   -2.0%]
  user_time  2.980 ± 0.022  2.822 ± 0.016  [  -5.5% ..   -5.0%]
  wall_time  3.369 ± 0.038  3.175 ± 0.033  [  -6.2% ..   -5.3%]
  samples    47             47

So parsing the extra EH frames is a lot more expensive for us than for
ld64. But given that we are quite a lot faster than ld64 to begin with,
I guess this isn't entirely unexpected...

Reviewed By: #lld-macho, oontvoo

Differential Revision: https://reviews.llvm.org/D129540
2022-07-13 21:14:05 -04:00
Stefan Pintilie c1f3cffee1 [PowerPC][LLD] Change PPC64R2SaveStub to only use non-PC-relative code
Currently the PPC64R2SaveStub thunk will produce Power 10 code by default.
This produced an issue when linking older code that made use of the st_other=1
bit but was never meant to be linked or run on Power 10.

This patch makes it so that only the R_PPC64_REL24_NOTOC relocation can produce
Power 10 code.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D129580
2022-07-13 19:34:33 -05:00
Fangrui Song e690137dde [Support] Change compression::zlib::{compress,uncompress} to use uint8_t *
It's more natural to use uint8_t * (std::byte needs C++17 and llvm has
too much uint8_t *) and most callers use uint8_t * instead of char *.
The functions are recently moved into `llvm::compression::zlib::`, so
downstream projects need to make adaption anyway.
2022-07-13 16:26:54 -07:00
Daniel Bertalan 94e0f8e001 [lld-macho] Accept dylibs with LC_DYLD_EXPORTS_TRIE
This load command specifies the offset and size of the exports trie.
This information used to be a field in LC_DYLD_INFO, but in newer
libraries, it has a dedicated load command: LC_DYLD_EXPORTS_TRIE.

The format of the trie is the same for both load commands, so the code
for parsing it can be shared.

LLD does not generate this yet; it is mainly useful when chained fixups
are in use, as the other members of LC_DYLD_INFO are unused then, so the
smaller LC_DYLD_EXPORTS_TRIE can be output instead.

LLDB gained support for this in D107673.

Fixes #54550

Differential Revision: https://reviews.llvm.org/D129430
2022-07-13 22:34:11 +02:00
Daniel Bertalan ecb14fd872 [lld-macho] Add LOH_ARM64_ADRP_LDR_GOT_LDR optimization hint support
This hint instructs the linker to relax a GOT-indirect load.
If the referenced symbol is external and its GOT entry is within +/- 1
MiB, the GOT entry can be loaded with a single literal ldr instruction.
If the referenced symbol is local, its address may be loaded directly if
it's close enough, or with an adr(p) + ldr pair if it's not.

This type accounts for more than half of all LOHs in chromium_framework.

This commit moves the eligibility checks into helper functions to
improve the readability of the LOH processing code. Ho functional
changes are intended to the previously implemented LOH types.

Differential Revision: https://reviews.llvm.org/D129427
2022-07-13 12:20:14 +02:00
Kazu Hirata e5f568a49f Use has_value instead of hasValue (NFC) 2022-07-13 01:58:03 -07:00
Fangrui Song 9ea5b34f05 [ELF][RISCV] Use unshifted value for overflow check
The unshifted value indicates an displacement in bytes which is more meaningful.
2022-07-13 00:28:29 -07:00