How to define a linker symbol in llvm?

Viewed 171

I would like to add a linker symbol in an llvm pass that I can access later in a linker script.

For example, to show what I'd like to see in the resulting object file, here is an assembly file that creates a linker symbol with a given value.

$ cat sym.s
    asdf = 0x1234
    .global asdf

$ clang -c sym.s && readelf --symbols sym.o

Symbol table '.symtab' contains 2 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000001234     0 NOTYPE  GLOBAL DEFAULT  ABS asdf

I could not see how to do this in the llvm library (c++ api), as I do not see how to interact with the symbol table given a handle to a Module when my pass is called via runOnModule(Module &M).

I'm not even sure the correct way to do this from an llvm source file.
Here is my best attempt exploring in that direction

$ cat attempt.ll
target triple = "x86_64-pc-linux-gnu"

@asdf = global i8 1234, !absolute_symbol !0

!0 = !{ i64 1234, i64 1234 }

$ clang -c attempt.ll  && readelf --symbols attempt.o

Symbol table '.symtab' contains 3 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS attempt.ll
     2: 0000000000000000     1 OBJECT  GLOBAL DEFAULT    3 asdf

The !absolute_symbol does not actually create an absolute symbol in the object file. It instead still creates a variable (of size 1 in this case), and apparently the variable (not the symbol) has an associated value. So this way of approaching it may be a dead end.

So getting back on track:

  • How to I add a symbol given a handle to an llvm::Module?
0 Answers
Related