Insert constant value into code segment with a relocation

Viewed 26

I'm in a process of making a native compiled language using LLVM as backend.

For a couple of special features I need to be able to do two things via LLVM API:

  • Provide custom relocations into both data and code segments to LLVM
  • Ability to insert a constant value (specifically arrays, but it doesn't really matter) into code segment, in specific places in between functions and create a relocation of it for LLVM-defined objects (assume functions, but it doesn't matter).

It looks like if I insert non-zero initialized global variables, they are going in the segment in the order of their declaration in LLVM IR module, I would like to to the same in the code segment, but it is read-only at the runtime, so let it be constants as in rdata segment.

For example:

@myConst1 = const [2 x i32 (i32)*] {MyProc1, MyProc2} // how do I put this into code segment before first instruction of MyProc1?
define i32 @MyProc1() !dbg !524 {
  ret i32 5
}

@myConst2 = const [16 x i8] zeroinitializer // ideally would like to do this, and create relocations manually into this array for two pointers to both MyProc1 and MyProc2
define i32 @MyProc2(i32 %0) !dbg !524 {
  ret i32 %0
}

Is this even possible to do with LLVM and it's API? If yes, I need help to understand how, as after reading a ton of documentation I'm unable to figure out how.

Thank you.

0 Answers
Related