Converting label to PC relative address for passing strings to functions

Hi, I’m working on a custom backend, and am looking to pass strings as arguments to a function. I currently have assembly being output which calls a PC-relative load function to place the location of the string into a register to pass. However, at the moment, the output assembly has a label for the location. I was wondering at which stage in the lowering I should look into converting this label into an actual address?

It has been a while since I worked in that area, but my memory is that AsmPrinter connects to an MCStreamer, which can be either one that produces assembler text or one that produces the object file. The one that produces the object file would be where the label would end up being encoded as an address (or a relocation). This is the same path that llvm-mc uses after parsing an assembler text file, so it might be easier to debug what you’re doing by giving llvm-mc the assembler text version of your output.

You say “custom backend”, do you mean custom ISA or are you talking some known architecture target and calling conventions? For something like x86, the PC-relative (rip-rel) would be a 32-bit displacement which might not be sufficient if the code and data end up being far part (the x86 abi has those small, medium, and large memory models for such things). For large distances, you would have to go through the GOT. Either way, you would have to use a linker relocation to get the address of the label (unless it happens to be in the same section as the code).