[llvm-jitlink] Add -alias option, shorten "-define-abs" option to "-abs".

The -alias option can be used to define aliases within a JITDylib. The
immediate motivation is to simplify testing of ORC runtime functions using
existing testcases (e.g. by aliasing dlfcn functions to their ORC-runtime
counterparts, like -alias dlopen=__orc_rt_macho_dlopen). The option is likely
to be useful for testing in general.

The -define-abs option is shortened to -abs for consistency with -alias.
This commit is contained in:
Lang Hames 2022-02-03 17:46:49 +11:00
parent f3a66ec0bd
commit 517a4844bf
16 changed files with 77 additions and 23 deletions

View File

@ -1,6 +1,6 @@
# RUN: rm -rf %t && mkdir -p %t
# RUN: llvm-mc -triple=arm64-apple-darwin19 -filetype=obj -o %t/macho_reloc.o %s
# RUN: llvm-jitlink -noexec -define-abs external_data=0xdeadbeef -define-abs external_func=0xcafef00d -check=%s %t/macho_reloc.o
# RUN: llvm-jitlink -noexec -abs external_data=0xdeadbeef -abs external_func=0xcafef00d -check=%s %t/macho_reloc.o
.section __TEXT,__text,regular,pure_instructions

View File

@ -5,11 +5,11 @@
# RUN: -o %t/elf_riscv32_non_pc_indirect_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0x1ff00000 -slab-page-size 4096 \
# RUN: -define-abs external_data=0x1ff10000 \
# RUN: -abs external_data=0x1ff10000 \
# RUN: -check %s %t/elf_riscv64_non_pc_indirect_reloc.o
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0x1ff00000 -slab-page-size 4096 \
# RUN: -define-abs external_data=0x1ff10000 \
# RUN: -abs external_data=0x1ff10000 \
# RUN: -check %s %t/elf_riscv32_non_pc_indirect_reloc.o
#

View File

@ -5,11 +5,11 @@
# RUN: -o %t/elf_riscv32_branch.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_func=0xfe \
# RUN: -abs external_func=0xfe \
# RUN: -check %s %t/elf_riscv64_branch.o
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_func=0xfe \
# RUN: -abs external_func=0xfe \
# RUN: -check %s %t/elf_riscv32_branch.o
#

View File

@ -5,11 +5,11 @@
# RUN: -o %t/elf_riscv32_sm_pic_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0x1ff00000 -slab-page-size 4096 \
# RUN: -define-abs external_func=0x1 -define-abs external_data=0x2 \
# RUN: -abs external_func=0x1 -abs external_data=0x2 \
# RUN: -check %s %t/elf_riscv64_sm_pic_reloc.o
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0x1ff00000 -slab-page-size 4096 \
# RUN: -define-abs external_func=0x1 -define-abs external_data=0x2 \
# RUN: -abs external_func=0x1 -abs external_data=0x2 \
# RUN: -check %s %t/elf_riscv32_sm_pic_reloc.o
#
# Test ELF small/PIC relocations

View File

@ -3,7 +3,7 @@
# RUN: -o %t/elf_riscv32_got_plt_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_func=0x1 -define-abs external_data=0x2 \
# RUN: -abs external_func=0x1 -abs external_data=0x2 \
# RUN: -check %s %t/elf_riscv32_got_plt_reloc.o
.text

View File

@ -3,7 +3,7 @@
# RUN: -o %t/elf_riscv64_got_plt_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_func=0x1 -define-abs external_data=0x2 \
# RUN: -abs external_func=0x1 -abs external_data=0x2 \
# RUN: -check %s %t/elf_riscv64_got_plt_reloc.o

View File

@ -2,8 +2,8 @@
# UNSUPPORTED: system-windows
# RUN: llvm-mc -triple=x86_64-unknown-linux -position-independent \
# RUN: -filetype=obj -o %t %s
# RUN: llvm-jitlink -debug-only=jitlink -define-abs bar=0x01 \
# RUN: -define-abs _ZTIi=0x02 -noexec %t 2>&1 | FileCheck %s
# RUN: llvm-jitlink -debug-only=jitlink -abs bar=0x01 \
# RUN: -abs _ZTIi=0x02 -noexec %t 2>&1 | FileCheck %s
#
# FIXME: This test should run on windows. Investigate spurious
# 'note: command had no output on stdout or stderr' errors, then re-enable.

View File

@ -3,7 +3,7 @@
# RUN: -filetype=obj -o %t/elf_sm_pic_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs extern_in_range32=0xffe00000 \
# RUN: -abs extern_in_range32=0xffe00000 \
# RUN: -check %s %t/elf_sm_pic_reloc.o
#

View File

@ -3,8 +3,8 @@
# RUN: -filetype=obj -o %t/elf_sm_pic_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_data=0x1 \
# RUN: -define-abs extern_out_of_range32=0x7fff00000000 \
# RUN: -abs external_data=0x1 \
# RUN: -abs extern_out_of_range32=0x7fff00000000 \
# RUN: -check %s %t/elf_sm_pic_reloc.o
#
# Test ELF small/PIC relocations.

View File

@ -3,8 +3,8 @@
# RUN: -filetype=obj -o %t/elf_abs_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_data_low=0x1 \
# RUN: -define-abs external_data_high=0xffffffff80000000 \
# RUN: -abs external_data_low=0x1 \
# RUN: -abs external_data_high=0xffffffff80000000 \
# RUN: -check %s %t/elf_abs_reloc.o
#
# Test ELF absolute relocations.

View File

@ -1,6 +1,6 @@
# REQUIRES: asserts
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj -o %t %s
# RUN: llvm-jitlink -debug-only=orc -noexec -define-abs _external_func=0x1 \
# RUN: llvm-jitlink -debug-only=orc -noexec -abs _external_func=0x1 \
# RUN: -entry=_foo %t 2>&1 | FileCheck %s
#
# Verify that symbol dependencies are correctly propagated through local

View File

@ -0,0 +1,20 @@
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj -o %t.o %s
# RUN: llvm-jitlink -noexec -alias x=y %t.o
#
# Check that the -alias option works.
.section __TEXT,__text,regular,pure_instructions
.globl _main
.p2align 4, 0x90
_main:
movq x@GOTPCREL(%rip), %rax
movl (%rax), %eax
retq
.section __DATA,__data
.globl y
.p2align 2
y:
.long 42
.subsections_via_symbols

View File

@ -1,6 +1,6 @@
# RUN: rm -rf %t && mkdir -p %t
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj -o %t/macho_weak_refs.o %s
# RUN: llvm-jitlink -noexec -check-name=jitlink-check-bar-present -define-abs bar=0x1 -check=%s %t/macho_weak_refs.o
# RUN: llvm-jitlink -noexec -check-name=jitlink-check-bar-present -abs bar=0x1 -check=%s %t/macho_weak_refs.o
# RUN: llvm-jitlink -noexec -check-name=jitlink-check-bar-absent -check=%s %t/macho_weak_refs.o
# Test weak reference handling by linking with and without a definition of 'bar' available.

View File

@ -1,4 +1,4 @@
# RUN: llvm-jitlink -noexec -define-abs __ZTIi=0x1 -define-abs ___gxx_personality_v0=0x2 %S/Inputs/MachO_x86-64_ehframe.o
# RUN: llvm-jitlink -noexec -abs __ZTIi=0x1 -abs ___gxx_personality_v0=0x2 %S/Inputs/MachO_x86-64_ehframe.o
#
# Perform a no-exec link of MachO_x86-64_ehframe and verify that it does not
# generate any errors despite the last FDE referring to the first CIE (rather

View File

@ -2,7 +2,7 @@
# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj -o %t/macho_reloc.o %s
# RUN: llvm-jitlink -noexec \
# RUN: -slab-allocate 100Kb -slab-address 0xfff00000 -slab-page-size 4096 \
# RUN: -define-abs external_data=0x1 -define-abs external_func=0x2 \
# RUN: -abs external_data=0x1 -abs external_func=0x2 \
# RUN: -check=%s %t/macho_reloc.o
#
# Test standard MachO relocations. Simulates slab allocation in the top 1Mb of

View File

@ -136,10 +136,14 @@ static cl::opt<bool>
cl::init(false), cl::cat(JITLinkCategory));
static cl::list<std::string> AbsoluteDefs(
"define-abs",
"abs",
cl::desc("Inject absolute symbol definitions (syntax: <name>=<addr>)"),
cl::ZeroOrMore, cl::cat(JITLinkCategory));
static cl::list<std::string>
Aliases("alias", cl::desc("Inject symbol aliases (syntax: <name>=<addr>)"),
cl::ZeroOrMore, cl::cat(JITLinkCategory));
static cl::list<std::string> TestHarnesses("harness", cl::Positional,
cl::desc("Test harness files"),
cl::ZeroOrMore,
@ -1374,8 +1378,8 @@ static Error addAbsoluteSymbols(Session &S,
uint64_t Addr;
if (AddrStr.getAsInteger(0, Addr))
return make_error<StringError>("Invalid address expression \"" + AddrStr +
"\" in absolute define \"" + AbsDefStmt +
"\"",
"\" in absolute symbol definition \"" +
AbsDefStmt + "\"",
inconvertibleErrorCode());
JITEvaluatedSymbol AbsDef(Addr, JITSymbolFlags::Exported);
if (auto Err = JD.define(absoluteSymbols({{S.ES.intern(Name), AbsDef}})))
@ -1388,6 +1392,33 @@ static Error addAbsoluteSymbols(Session &S,
return Error::success();
}
static Error addAliases(Session &S,
const std::map<unsigned, JITDylib *> &IdxToJD) {
// Define absolute symbols.
LLVM_DEBUG(dbgs() << "Defining aliases...\n");
for (auto AliasItr = Aliases.begin(), AliasEnd = Aliases.end();
AliasItr != AliasEnd; ++AliasItr) {
unsigned AliasArgIdx = Aliases.getPosition(AliasItr - Aliases.begin());
auto &JD = *std::prev(IdxToJD.lower_bound(AliasArgIdx))->second;
StringRef AliasStmt = *AliasItr;
size_t EqIdx = AliasStmt.find_first_of('=');
if (EqIdx == StringRef::npos)
return make_error<StringError>("Invalid alias definition \"" + AliasStmt +
"\". Syntax: <name>=<addr>",
inconvertibleErrorCode());
StringRef Alias = AliasStmt.substr(0, EqIdx).trim();
StringRef Aliasee = AliasStmt.substr(EqIdx + 1).trim();
SymbolAliasMap SAM;
SAM[S.ES.intern(Alias)] = {S.ES.intern(Aliasee), JITSymbolFlags::Exported};
if (auto Err = JD.define(symbolAliases(std::move(SAM))))
return Err;
}
return Error::success();
}
static Error addTestHarnesses(Session &S) {
LLVM_DEBUG(dbgs() << "Adding test harness objects...\n");
for (auto HarnessFile : TestHarnesses) {
@ -1711,6 +1742,9 @@ static Error addSessionInputs(Session &S) {
if (auto Err = addAbsoluteSymbols(S, IdxToJD))
return Err;
if (auto Err = addAliases(S, IdxToJD))
return Err;
if (!TestHarnesses.empty())
if (auto Err = addTestHarnesses(S))
return Err;