362 Commits

Author SHA1 Message Date
Aiden Grossman
6ba25c1a56
[llvm] Remove uses of %T in tests (#151621)
This patch removes all uses of %T from within LLVM tests. %T has been
deprecated for about seven years and use is not advised given it is not
unique per test and can thus lead to races. The goal of this is to
eventually remove support for %T from lit.
2025-08-01 08:24:56 -07:00
Maksim Panchenko
0c33799e37
[JITLink] Include target addend in out-of-range error (#145423)
When JITLink reports an out-of-range error, the underlying reason could
be hidden from the user if it's due to an excessively large target
addend. Add non-zero target addend to the message for clarity.
2025-06-23 22:46:15 -07:00
Lang Hames
d6a486c221
[ORC] Apply MachO::CPU_SUBTYPE_MASK to comparison in getDylibInterfac… (#145154)
…eFromDylib.

When comparing CPU subtypes from slices in a MachO universal binary we
need to apply the MachO::CPU_SUBTYPE_MASK to mask out any flags in the
high bits, otherwise we might fail to correctly match a slice and return
a spurious "does not contain slice" error.

rdar://153913779
2025-06-21 19:31:51 +10:00
Martin Storsjö
902d6894f5 [JITLink] [test] Add a trailing newline to a test file. NFC.
Otherwise, this shows up as an unrelated diff after editing the
file in some editors (like vim).
2025-06-10 12:08:24 +03:00
Martin Storsjö
f715ff4a9d
[JITLink] [test] Generalize UNSUPPORTED markings for Windows/arm64 (#142377)
Don't needlessly specify the vendor field as "pc"; in MinGW
configurations, the vendor field is often "w64".
2025-06-10 12:06:29 +03:00
Martin Storsjö
9b282afb8a
[JITLink] [test] Extend preexisting MinGW XFAILs to new tests (#142375)
This extends the preexisting XFAILs from
4c642b62b99fa128c180f28278637b32be5e5576 to these new tests from
6fa8657a622173c177d863b763550de4d388f73c.
2025-06-10 12:05:16 +03:00
Sam Elliott
65077c7ac7
[RISCV] Remove -riscv-asm-relax-branches flag (#142855)
This flag has been superseded by `.option exact`, as the test updates
show.

Given the flag was always hidden, it makes sense to me to remove it, and
move tests that required it to use `.option exact`.
2025-06-06 15:23:31 -07:00
Lang Hames
fe40f97afe [JITLink] Disable the all-load- tests on Windows/arm64.
We don't have a JITLink Windows/arm64 backend yet, so disable these tests
there.
2025-05-30 09:33:45 +10:00
Lang Hames
e2d646226c Re-apply "[ORC] Add optional context string to duplicate symbol definition..."
This reapplies b0979b8c65d, which was reverted in 370aecb9572 due to bot
failures. The failures were caused by typos in the testcase that are fixed in
this patch.
2025-05-28 10:38:18 +10:00
Lang Hames
6fa8657a62
[ORC] Refactor visit-members in StaticLibraryDefinitionGenerator. (#141546)
This refactor was motivated by two bugs identified in out-of-tree
builds:

1. Some implementations of the VisitMembersFunction type (often used to	
implement special loading semantics, e.g. -all_load or -ObjC) were assuming
that buffers for archive members were null-terminated, which they are not in
general. This was triggering occasional assertions.

2. Archives may include multiple members with the same file name, e.g.
when constructed by appending files with the same name:
  % llvm-ar crs libfoo.a foo.o
  % llvm-ar q libfoo.a foo.o
  % llvm-ar t libfoo.a foo.o
  foo.o

   While confusing, these members may be safe to link (provided that they're
   individually valid and don't define duplicate symbols). In ORC however, the
   archive member name may be used to construct an ORC initializer symbol,
   which must also be unique. In that case the duplicate member names lead to a
   duplicate definition error even if the members define unrelated symbols.

In addition to these bugs, StaticLibraryDefinitionGenerator had grown a
collection of all member buffers (ObjectFilesMap), a BumpPtrAllocator
that was redundantly storing synthesized archive member names (these are
copied into the MemoryBuffers created for each Object, but were never
freed in the allocator), and a set of COFF-specific import files.

To fix the bugs above and simplify StaticLibraryDefinitionGenerator this
patch makes the following changes:

1. StaticLibraryDefinitionGenerator::VisitMembersFunction is generalized
   to take a reference to the containing archive, and the index of the
   member within the archive. It now returns an Expected<bool> indicating
   whether the member visited should be treated as loadable, not loadable,
   or as invalidating the entire archive.
2. A static StaticLibraryDefinitionGenerator::createMemberBuffer method
   is added which creates MemoryBuffers with unique names of the form
   `<archive-name>[<index>](<member-name>)`. This defers construction of
   member names until they're loaded, allowing the BumpPtrAllocator (with
   its redundant name storage) to be removed.
3. The ObjectFilesMap (symbol name -> memory-buffer-ref) is replaced
   with a SymbolToMemberIndexMap (symbol name -> index) which should be
   smaller and faster to construct.
4. The 'loadability' result from VisitMemberFunctions is now taken into
   consideration when building the SymbolToMemberIndexMap so that members
   that have already been loaded / filtered out can be skipped, and do not
   take up any ongoing space.
5. The COFF ImportedDynamicLibraries member is moved out into the
   COFFImportFileScanner utility, which can be used as a
   VisitMemberFunction.

This fixes the bugs described above; and should lower memory consumption
slightly, especially for archives with many files and / or symbol where
most files are eventually loaded.
2025-05-27 20:58:53 +10:00
Michael Kruse
ddf1249842
[Test] Disable Linux perf test under WSL (#137822)
Linux perf_events is not implemented in WSL1, skip the test that
requires it.

There is just a single test that requires perf_events. It fails under
WSL1 with:
```sh
env JITDUMPDIR=/home/meinersbur/build/llvm-project/release/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp /home/meinersbur/build/llvm-project/release/bin/llvm-jitlink -perf-support /home/meinersbur/build/llvm-project/release/test/ExecutionEngine/JITLink/x86-64/Output/ELF_perf.s.tmp/ELF_x86-64_perf.o
llvm-jitlink error: PerfState not initialized
```

WSL environment detection logic follows
https://github.com/scivision/detect-windows-subsystem-for-linux/blob/main/is_wsl.py

Also see WSL issue: https://github.com/microsoft/WSL/issues/4595
2025-05-13 13:35:08 +02:00
JP Lehr
370aecb957 Revert "[ORC] Add optional context string to duplicate symbol definition errors."
Broke buildbots: https://lab.llvm.org/buildbot/#/builders/10/builds/5025

This reverts commit b0979b8c65d76cc1897e97b9ad091d8d99abdd18.
2025-05-09 04:18:02 -05:00
Lang Hames
b0979b8c65 [ORC] Add optional context string to duplicate symbol definition errors.
The context string can be added to indicate the source of the duplicate
definition. E.g. if the context is set to "foo2.o", then:

"Duplicate definition of symbol 'foo'"

becomes

"In foo2.o, duplicate definition of symbol 'foo'".

The JITDylib::defineImpl method is updated to use the name of the
MaterializationUnit being added as the context string for duplicate definition
errors. The JITDylib::defineMaterializing method is updated to use
"defineMaterializing operation" as the conext string.
2025-05-09 17:30:42 +10:00
Lang Hames
b972164f38 [JITLink] Rename 'i386' namespace and files to 'x86'.
When building on i386, both clang and gcc define a builtin 'i386' macro (see
discussion in https://github.com/llvm/llvm-project/pull/137063). This causes
build errors in the JITLink/i386 backend when attempting to build LLVM on i386.

This commit renames the 'i386' backend (namespaces, APIs and files) to 'x86' to
avoid this issue.
2025-05-08 11:35:14 +10:00
Lang Hames
32f514c68d [JITLink][i386] Support R_386_GOT32X using existing non-relaxable edge kind.
R_386_GOT32 was already handled by lowering to
EdgeKind_i386::RequestGOTAndTransformToDelta32FromGOT. R_386_GOT32X is just an
optionally relaxable version of R_386_GOT32, so we can lower to the same edge
kind.

I've left a TODO to add a relaxable edge kind and update the i386 relaxation
optimization in the future, though I'll probably leave this as an exercise for
any i386 aficionados out there. ;)
2025-05-08 10:07:44 +10:00
Henry Jiang
7d3dfc862d
[JITLink][XCOFF] Setup initial build support for XCOFF (#127266)
This patch starts the initial implementation of JITLink for XCOFF (Object format for AIX).
2025-04-03 17:01:18 -04:00
Lang Hames
191e0622e8 [JITLink][MachO][arm64] Don't lower ptrauth edges in noalloc-lifetime sections.
Ptrauth relocations can only be fixed up in the executing process, but noalloc
sections do not have any memory in the executing process. Failure to skip
ptrauth edges results in signing instructions that operate on invalid addresses,
leading to segfaults or data corruption.

Ignoring noalloc sections for ptrauth lowering purposes allows the ptrauth
edges to persist until they reach the applyFixup method, at which point they
raise a useful error and cleanly terminate linking.
2025-04-01 22:18:00 +11:00
Lang Hames
14c36db16f [ORC] Generalize GetDylibInterface to support MachO Universal Binaries.
Also adds a testcase for dylib handling in llvm-jitlink`s -weak-lx and
-weak_library options.
2025-03-28 13:28:04 +11:00
Lang Hames
8493467490 [JITLink][AArch64] Ensure that nulls remain null during ptrauth signing.
Signing a null pointer value can, and usually will, result in some high bits
being set, causing null checks to fail. E.g. in

extern void __attribute__((weak_import)) f(void);
void (*p) = &f;

if f is undefined then p should be null (left unsigned).

This patch updates lowerPointer64AuthEdgesToSigningFunction to check for
Pointer64Authenticated edges to null targets. Where found, these edges are
turned into plain Pointer64 edges (which we know from context will write a null
value to the fixup location), and signing instructions for these locations are
omitted from the signing function.
2025-02-28 15:19:41 +11:00
Lang Hames
253e11695b [ORC][llvm-jitlink] Add support for emulating ld64 -weak-lx / -weak_library.
Linking libraries in ld64 with -weak-lx / -weak_library causes all references
to symbols in those libraries to be made weak, allowing the librarie to be
missing at runtime.

This patch extends EPCDynamicLibrarySearchGenerator with support for emulating
this behavior: If an instance is constructed with an Allow predicate but no
dylib handle then all symbols matching the predicate are immediately resolved
to null.

The llvm-jitlink tool is updated with -weak-lx / -weak_library options for
testing. Unlike their ld64 counterparts these options take a TBD file as input,
and always resolve all exports in the TBD file to null.
2025-02-25 13:54:17 +11:00
Daniel Paoliello
b3458fdec5
[llvm] Win x64 Unwind V2 1/n: Mark beginning and end of epilogs (#110024)
Windows x64 Unwind V2 adds epilog information to unwind data:
specifically, the length of the epilog and the offset of each epilog.

The first step to do this is to add markers to the beginning and end of
each epilog when generating Windows x64 code. I've modelled this after
how LLVM was marking ARM and AArch64 epilogs in Windows (and unified the
code between the three).
2025-01-30 13:51:30 -08:00
Lang Hames
bd8578bf2e [JITLink] Add 'REQUIRES: asserts' to MachO_ptrauth_dependencies.s.
This test depends on debugging output.
2025-01-28 15:33:24 +11:00
Lang Hames
79685b59b7 [JITLink][aarch64] Fix dependence tracking for Pointer64Authenticated edges.
Transform Pointer64Authenticated edges into KeepAlive edges, rather than
removing them, in order to preserve symbol dependence information.

The lowerPointer64AuthEdgesToSigningFunction pass is responsible for
transforming Pointer64Authenticated edges to a signing function that can be run
in the executing process to initialize global PAC pointers. Removing the edges
entirely in this pass results in loss of dependence tracking, which can in turn
cause ORC to report PAC pointers as ready before the pointers targets have
completed materialization (resulting in a use-before-finalize error, often
manifesting as access to uninitialized / unprotected memory).

This commit addresses the issue by leaving the edges in the graph and simply
changing their kind to KeepAlive, a no-op for fixup purposes but followed for
dependence tracking purposes.
2025-01-28 14:57:49 +11:00
ZhaoQi
f6253f8fe9
[JITLink][LoongArch] Add label addition and subtraction relocations (#122262) 2025-01-24 15:11:39 +08:00
ZhaoQi
9dd5aed4ed
[JITLink][LoongArch] Support R_LARCH_ALIGN relaxation (#122259)
Linker relaxation is not implemented for jitlink now. But if
relaxation is enabled by clang, R_LARCH_RELAX and
R_LARCH_ALIGN relocations will be emitted.

This commit adapts lld's algorithm to jitlink. Currently, only
relaxing R_LARCH_ALIGN is implemented. Other relaxable
relocs can be implemented in the future.

Without this, interpreting C++ code using clang-repl or running
ir using lli when relaxation is enabled will occur error: `JIT
session error: Unsupported loongarch relocation:102: R_LARCH_ALIGN`.

Similar to 310473c536 but only implement align.
2025-01-24 14:24:01 +08:00
Lang Hames
bfb0a518e7 [llvm-jitlink] Use -num-threads=0 for regression tests relying on debug output.
ORC and JITLink debugging output write the dbgs() raw_ostream, which isn't
thread-safe. Use -num-threads=0 to force single-threaded linking for tests that
produce debugging output.

The llvm-jitlink tool is updated to suggest -num-threads=0 when debugging
output is enabled.
2025-01-08 14:01:33 +11:00
Haojian Wu
e576c5bed7 Fix an incorrect -show-graph command-line flag in COFF_comdat_weak_plus_strong.s test
The flag -show-graph has been renamed to -show-graphs in 01bdd8cffcaf97636b5fb6ee4933e62c872528d3
2025-01-03 15:17:50 +01:00
Lang Hames
ad192f9f20 [ORC] Restrict check-dwarf-filename test to Darwin for now.
This test is failing on Windows (see e.g.
https://lab.llvm.org/buildbot/#/builders/146/builds/1983), probably due to
incomplete debugger support there (the test registers debug info in-process, so
non-Darwin builds shouldn't be expected to have the right symbols).
2025-01-03 20:46:55 +11:00
Lang Hames
93a68a5188 [ORC] Testcase requires asserts as it depends on debugging output. 2025-01-03 17:30:19 +11:00
Lang Hames
b6c06d1a8d [ORC] Fix bug in source file name finding in DebuggerSupportPlugin.
The debug section map was using MachO section names (with the "__" prefix), but
DWARFContext expects section names with the object format prefix stripped off.
This was preventing DWARFContext from accessing the debug_str section,
resulting in bogus source name strings.
2025-01-03 15:45:43 +11:00
ZhaoQi
8e404509cc
[JITLink][RISCV] Add feature relax for addsub tests. NFC (#121204)
R_RISCV_{ADD*/SUB*} relocations are kept only when feature relax
enabled. So it is better to add relax to the test, so that relocs can be
reserved for processing by the jitlink. That's what this test really
wants to test.
2025-01-03 09:07:44 +08:00
ZhaoQi
4c5c5e2f41
[JITLink][LoongArch] Add R_LARCH_{B16,B21} relocations support (#121096) 2025-01-02 18:09:06 +08:00
Lang Hames
096551537b "Reapply "[llvm-jitlink] Use concurrent linking by default." with more fixes.
This reapplies edca1d9bad2 which was reverted in 7ec139ad4bc due to bot
failures.

LocalDependencyPropagation.s is updated to use -num-threads=0 in order to
avoid interleaving debugging output.

ELFNixPlatform.h is updated to protect the deferred runtime function calls
map during bootstrap.
2025-01-02 15:08:21 +11:00
Lang Hames
f2d18a4d00 Reapply "[ORC] Introduce LazyReexportsManager, ... (#118923)" with fixes.
This re-applies 570ecdcf8b4, which was reverted in 74e8a37ff32 due to bot
failures. This commit renames sysv_resolve.cpp to resolve.cpp, which was the
cause of the config errors.
2024-12-09 03:22:51 +00:00
Lang Hames
74e8a37ff3 Revert "Reapply "[ORC] Introduce LazyReexportsManager, … (#118923)" with fixes"
This reverts commit 41652c6c92958a87b8505b9b1e6f008856e392ac while I investigate
more bot failures.
2024-12-09 12:38:59 +11:00
Lang Hames
41652c6c92 Reapply "[ORC] Introduce LazyReexportsManager, … (#118923)" with fixes
This reapplies 570ecdcf8b4, which was reverted in 6073dd923b8 due to bot
failures.

The test failures on Linux were fixed by:
1. Removing an overly restrictive assertion (query dependence on a symbol no
longer implies a MaterializingInfo for that symbol)
2. Adding reentry and resolver files to the ORC runtime CMakeLists.txt for
Linux.
3. Adding the __orc_rt_reentry -> __orc_rt_sysv_reentry alias to ELFNixPlatform.
2024-12-09 12:08:07 +11:00
Lang Hames
6073dd923b Revert "[ORC] Introduce LazyReexportsManager, JITLinkTrampolines, … (#118923)"
This reverts commit 570ecdcf8b44aec853ce381a5f6b77222b041afa while I investigate
bot failures, e.g. https://lab.llvm.org/buildbot/#/builders/17/builds/4446.
2024-12-07 22:15:27 +11:00
Lang Hames
570ecdcf8b
[ORC] Introduce LazyReexportsManager, JITLinkTrampolines, ORC-RT base… (#118923)
…d reentry.

These utilities provide new, more generic and easier to use support for
lazy compilation in ORC.

LazyReexportsManager is an alternative to LazyCallThroughManager. It
takes requests for lazy re-entry points in the form of an alias map:
lazy-reexports = {
  ( <entry point symbol #1>, <implementation symbol #1> ),
  ( <entry point symbol #2>, <implementation symbol #2> ),
  ...
  ( <entry point symbol #n>, <implementation symbol #n> )
}

LazyReexportsManager then:
1. binds the entry points to the implementation names in an internal
table.
2. creates a JIT re-entry trampoline for each entry point.
3. creates a redirectable symbol for each of the entry point name and
binds redirectable symbol to the corresponding reentry trampoline.

When an entry point symbol is first called at runtime (which may be on
any thread of the JIT'd program) it will re-enter the JIT via the
trampoline and trigger a lookup for the implementation symbol stored in
LazyReexportsManager's internal table. When the lookup completes the
entry point symbol will be updated (via the RedirectableSymbolManager)
to point at the implementation symbol, and execution will proceed to the
implementation symbol.

Actual construction of the re-entry trampolines and redirectable symbols
is delegated to an EmitTrampolines functor and the
RedirectableSymbolsManager respectively.

JITLinkReentryTrampolines.h provides a JITLink-based implementation of
the EmitTrampolines functor. (AArch64 only in this patch, but other
architectures will be added in the near future).

Register state save and reentry functionality is added to the ORC
runtime in the __orc_rt_sysv_resolve and __orc_rt_resolve_implementation
functions (the latter is generic, the former will need custom
implementations for each ABI and architecture to be supported, however
this should be much less effort than the existing OrcABISupport
approach, since the ORC runtime allows this code to be written as native
assembly).

The resulting system:
1. Works equally well for in-process and out-of-process JIT'd code.
2. Requires less boilerplate to set up.

Given an ObjectLinkingLayer and PlatformJD (JITDylib containing the ORC
runtime), setup is just:

```c++
auto RSMgr = JITLinkRedirectableSymbolManager::Create(OLL);
if (!RSMgr)
  return RSMgr.takeError();

auto LRMgr = createJITLinkLazyReexportsManager(OLL, **RSMgr, PlatformJD);
if (!LRMgr)
  return LRMgr.takeError();
```

after which lazy reexports can be introduced with:

```c++
JD.define(lazyReexports(LRMgr, <alias map>));
```

LazyObectLinkingLayer is updated to use this new method, but the LLVM-IR
level CompileOnDemandLayer will continue to use LazyCallThroughManager
and OrcABISupport until the new system supports a wider range of
architectures and ABIs.

The llvm-jitlink utility's -lazy option now uses the new scheme. Since
it depends on the ORC runtime, the lazy-link.ll testcase and associated
helpers are moved to the ORC runtime.
2024-12-07 21:08:39 +11:00
wanglei
ca9f358b14
[JITLink][LoongArch] Add support for R_LARCH_CALL36 relocation
This relocation is used for function calls with medium code model.

Reviewed By: lhames, SixWeining

Pull Request: https://github.com/llvm/llvm-project/pull/117127
2024-11-22 09:50:23 +08:00
Lang Hames
3f540e1a21 [JITLink] Restrict execution of MachO_ptrauth-globals.s test to arm64 hosts.
Should fix the bot failures on Darwin x86-64 machines.
2024-11-22 11:44:13 +11:00
Lang Hames
a432f11a52 [JITLink][arm64] Support arm64e JIT'd code (initially enabled for MachO only).
Adds two new JITLink passes to create and populate a pointer-signing function
that can be called via an allocation-action attached to the LinkGraph:

* createEmptyPointerSigningFunction creates a pointer signing function in a
custome section, reserving sufficient space for the signing code. It should
be run as a post-prune pass (to ensure that memory is reserved prior to
allocation).
* lowerPointer64AuthEdgesToSigningFunction pass populates the signing function
by walking the graph, decoding the ptrauth info (encoded in the edge addend) and
writing an instruction sequence to sign all ptrauth fixup locations.

rdar://61956998
2024-11-21 14:59:52 +11:00
Lang Hames
61d1b7c5ed [ORC] Tweak lazy-link testcase's UNSUPPORTED condition for armv8a some more.
The change in c0cbcb4efe8 was insufficient: The armv8a subarch is a property of
the compiled testcase, not the test target triple. Having double-checked the
EPCIndirectionUtils::Create method we want to disable this test for all arm.*
prefixes except arm64 (we want the test to continue working on Darwin).
2024-11-18 13:54:53 +11:00
Lang Hames
c0cbcb4efe [ORC] Tweak lazy-link testcase's UNSUPPORTED condition for armv8a.
The previous attempt in 6349c1c28195 didn't seem to work: the test is still
failing as of https://lab.llvm.org/buildbot/#/builders/154/builds/7609.
2024-11-18 13:19:19 +11:00
Lang Hames
6349c1c281 [ORC] Disable lazy-linking test on armv8 and powerpc.
These architectures are not supported yet.
2024-11-18 12:59:25 +11:00
Lang Hames
224290d448
[ORC] Add LazyObjectLinkingLayer, lazy-linking support to llvm-jitlink (#116002)
LazyObjectLinkingLayer can be used to add object files that will not be linked
into the executor unless some function that they define is called at runtime.
(References to data members defined by these objects will still trigger
immediate linking)

To implement lazy linking, LazyObjectLinkingLayer uses the lazyReexports
utility to construct stubs for each function in a given object file, and an
ObjectLinkingLayer::Plugin to rename the function bodies at link-time. (Data
symbols are not renamed)

The llvm-jitlink utility is extended with a -lazy option that can be
passed before input files or archives to add them using the lazy linking
layer rather than the base ObjectLinkingLayer.
2024-11-18 11:17:36 +11:00
Douglas
d822c099ee
[JITLink] Use rsplit on -sectcreate argument in llvm-jitlink (#115511)
This accounts for cases where the file path may contain an `@` symbol.
In such cases, the split occurs too early causing argument parsing to
fail.
2024-11-10 19:19:02 +11:00
Lang Hames
841227a5d9 Re-apply "Revert "[JITLink] Use MapVector to stabilize iteration.."" with fixes.
This re-applies 244ea406259, which was reverted in 0019d061854 while I
investigated a bot failure. The fix for the failure will be committed as a
follow-up.
2024-11-01 10:35:49 -07:00
Lang Hames
0019d06185 Revert "Revert "[JITLink] Use MapVector to stabilize iteration order""
This reverts commit 244ea4062590b4fbda56bbae6cd3700159db19bf while I test a fix
for a build failure: https://lab.llvm.org/buildbot/#/builders/174/builds/7685.
2024-10-31 19:18:34 -07:00
Lang Hames
fe5215281d [JITLink][COFF] Delete trailing whitespace in testcase. NFC. 2024-11-01 04:50:24 +11:00
Lang Hames
244ea40625 Revert "[JITLink] Use MapVector to stabilize iteration order"
This reverts commit f8f4235612b9668bbcbb6a58634fcb756794045e and replaces the
MapVector with a sorted vector in the debug dump: We only need to sort the
sections for debug dumping, and don't want LinkGraph API clients assuming
anything about the section iteration order.
2024-11-01 04:50:24 +11:00