From 517a4844bf26da21b0c7454a9388500b62cd6106 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Thu, 3 Feb 2022 17:46:49 +1100 Subject: [PATCH] [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. --- .../JITLink/AArch64/MachO_arm64_relocations.s | 2 +- .../ExecutionEngine/JITLink/RISCV/ELF_abs_reloc.s | 4 +-- .../ExecutionEngine/JITLink/RISCV/ELF_branch.s | 4 +-- .../JITLink/RISCV/ELF_pc_indirect.s | 4 +-- .../JITLink/RISCV/ELF_riscv32_got_plt_reloc.s | 2 +- .../JITLink/RISCV/ELF_riscv64_got_plt_reloc.s | 2 +- .../JITLink/X86/ELF_ehframe_basic.s | 4 +-- .../JITLink/X86/ELF_x86-64_got_plt_optimizations.s | 2 +- .../JITLink/X86/ELF_x86-64_small_pic_relocations.s | 4 +-- .../JITLink/X86/ELF_x86_64_absolute_relocations.s | 4 +-- .../JITLink/X86/LocalDependencyPropagation.s | 2 +- .../JITLink/X86/MachO_llvm_jitlink_alias_option.s | 20 +++++++++++ .../JITLink/X86/MachO_weak_references.s | 2 +- .../JITLink/X86/MachO_x86-64_ehframe.test | 2 +- .../JITLink/X86/MachO_x86-64_relocations.s | 2 +- llvm/tools/llvm-jitlink/llvm-jitlink.cpp | 40 ++++++++++++++++++++-- 16 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 llvm/test/ExecutionEngine/JITLink/X86/MachO_llvm_jitlink_alias_option.s diff --git a/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_relocations.s b/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_relocations.s index 5d4539d..1015864 100644 --- a/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_relocations.s +++ b/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_relocations.s @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_abs_reloc.s b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_abs_reloc.s index d4d9ff3..b70f8f8 100644 --- a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_abs_reloc.s +++ b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_abs_reloc.s @@ -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 # diff --git a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s index 5bafcc4..0e7a83e 100644 --- a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s +++ b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_branch.s @@ -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 # diff --git a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_pc_indirect.s b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_pc_indirect.s index 32897e3..a5381b3 100644 --- a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_pc_indirect.s +++ b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_pc_indirect.s @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv32_got_plt_reloc.s b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv32_got_plt_reloc.s index c8df485..0990df3 100644 --- a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv32_got_plt_reloc.s +++ b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv32_got_plt_reloc.s @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv64_got_plt_reloc.s b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv64_got_plt_reloc.s index e5b789d..8e01b08 100644 --- a/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv64_got_plt_reloc.s +++ b/llvm/test/ExecutionEngine/JITLink/RISCV/ELF_riscv64_got_plt_reloc.s @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/X86/ELF_ehframe_basic.s b/llvm/test/ExecutionEngine/JITLink/X86/ELF_ehframe_basic.s index 170843b..1b3ff16 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/ELF_ehframe_basic.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/ELF_ehframe_basic.s @@ -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. diff --git a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_got_plt_optimizations.s b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_got_plt_optimizations.s index 6090cc9..7ec72cc 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_got_plt_optimizations.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_got_plt_optimizations.s @@ -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 # diff --git a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_small_pic_relocations.s b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_small_pic_relocations.s index 59981f1..83e7f7c 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_small_pic_relocations.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86-64_small_pic_relocations.s @@ -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. diff --git a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s index caaaeee..f7ce00a 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/ELF_x86_64_absolute_relocations.s @@ -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. diff --git a/llvm/test/ExecutionEngine/JITLink/X86/LocalDependencyPropagation.s b/llvm/test/ExecutionEngine/JITLink/X86/LocalDependencyPropagation.s index 68b1aa9..d70cdfa 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/LocalDependencyPropagation.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/LocalDependencyPropagation.s @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_llvm_jitlink_alias_option.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_llvm_jitlink_alias_option.s new file mode 100644 index 0000000..fc35294 --- /dev/null +++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_llvm_jitlink_alias_option.s @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_references.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_references.s index 20fa553..1395bb5 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_references.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_references.s @@ -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. diff --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_ehframe.test b/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_ehframe.test index 08c616f..b6ebddb 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_ehframe.test +++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_ehframe.test @@ -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 diff --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_relocations.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_relocations.s index 7efef9c..fc59f66 100644 --- a/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_relocations.s +++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_x86-64_relocations.s @@ -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 diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp index aacac72..3dc1767 100644 --- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp @@ -136,10 +136,14 @@ static cl::opt cl::init(false), cl::cat(JITLinkCategory)); static cl::list AbsoluteDefs( - "define-abs", + "abs", cl::desc("Inject absolute symbol definitions (syntax: =)"), cl::ZeroOrMore, cl::cat(JITLinkCategory)); +static cl::list + Aliases("alias", cl::desc("Inject symbol aliases (syntax: =)"), + cl::ZeroOrMore, cl::cat(JITLinkCategory)); + static cl::list 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("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 &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("Invalid alias definition \"" + AliasStmt + + "\". Syntax: =", + 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; -- 2.7.4