Problem Description
I want to hardcode function addresses into my LLVM module
source_filename = "generated_code"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@"RuntimeFunctions::doIt" = internal alias void (), inttoptr (i64 94434395959904 to void ()*)
define void @_1_generatedFunction() {
body_0:
call void @"RuntimeFunctions::doIt"()
ret void
}
and then compile this module using clang codegen_1.ll -shared
. However, clang
reports unexpected linker errors:
ld.lld: error: generated_code:(function _1_generatedFunction: .text+0x2): relocation R_X86_64_PLT32 out of range: 94434395954218 is not in [-2147483648, 2147483647]
Why is clang inserting a relocation for my alias here?
I thought an “alias” was exactly that: an alias, i.e. identical to when I would inline the pointer at into the call
instruction? Or am I misunderstanding the meaning of LLVM aliases?
Debugging so far
-
Everything works as expected when using ORC to compile & link in-process (
llvm::orc::JITDylib
,llvm::orc::RTDyldObjectLinkingLayer
, etc.) -
Everything works as expected if I inline the alias, i.e. if I use
source_filename = "generated_code"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define void @_1_generatedFunction() {
body_0:
call void inttoptr (i64 94434395959904 to void ()*)()
ret void
}
Background
In my use case, I am using LLVM as JIT compiler and load the compiled modules directly into my own process. As such, I know the exact function addresses and can hardcode them into the generated module. I then want to call clang
to compile the module to a .so
which I load using dlopen
into my own process. I need to take the dlopen
detour to ensure compatibility with debugging tools which hook dlopen
.