Prevent Instruction Fixup from using relocations

Hi:

I’m trying to build a toy backend that produces code without relocation for my toy ELF loader. For now, my InstructionSelection works as intended and for MCInstLowering, I just lower MO_GlobalAddress operands with the following code:

MCOperand::createExpr(MCSymbolRefExpr::create(getSymbol(MO.getGlobal())))

However the backend still tries to use relocation even when I’ve set RelocationModel to Reloc::Static. Further debugging shows that in MCAssembler::evaluateFixup, which in turn calls MCExpr::evaluateAsRelocatableImpl, the evaluation failed due to the MCSymbol is not variable.

That’s where I’m lost with what does variable MCSymbol means, where does it come from and what should I do to fix it. Any help would be appreciated.

Zhang

The MC layer is not meant to produce ELF executables, but to produce ELF
relocatable object files. You are still supposed to run a linker on
them. As such, the MC layer will emit relocations for things like symbol
references (e.g. the name of a function) as it is the job of the linker
to resolve those. There are some complications like turning symbol
references into section references, but the same idea applies to those.

Joerg

Hi:
Thanks for the reply. I’m aware of this however I’d assume it’s still possible to emit internal references if two functions are in the same translation unit?

Zhang