Message board for the users of flat assembler.
> Compiler Internals > Relocations problem
I am still working on a EFI64 (PE64) program.
I don't quite understand the way FASM treats a label as "relocatable".
Suppose TheSymbol is defined in a .data section, and there is a .reloc section with fixups.
In the .code section, it seems to me that:
mov rax, TheSymbol
lea rax, [TheSymbol]
make TheSymbol relocatable, while
does not. When exactly is a label reloacatable? How can I make TheSymbol relocatable in the latter example?
Thanks in advance
|24 May 2018, 14:24||
The difference between PE fixups and "classic" relocations in object format is that in case of PE the program image moves as a whole within memory, so relative offsets between any code or data cannot change, only the base address of entire image.
Therefore any instructions that use RIP-relative addressing, where the relative offset between the instruction and target object is used in the opcode, is not going to generate fixup. In this case it is "LEA RAX,[TheSymbol]" that automatically uses RIP-relative addressing and thus needs no relocating.
However both "MOV RAX,TheSymbol" and "PUSH TheSymbol" use the absolute address as an immediate and this (as opposed to offsets within image) changes when the image is moved (that is when the base address is changed from the default). Therefore both these instructions are going to generate fixup entries and they need to be relocated by the loader.
If the address that ends up on the stack after that PUSH is not a valid one, it might be some other issue in your code, because fasm certainly generates a fixup for such PUSH when the ".reloc" section is defined.
|24 May 2018, 20:04||
;From my tests on FASM 1.73.02, MOV generates relocation information, PUSH and CMP do not.
format pe64 efi
section '.text' code executable readable
and spl, 11110000b
sub rsp, 4 * 8
mov rcx, [rdx + 64] ;EFI_SYSTEM_TABLE.ConOut
mov rdx, msg
cmp rdx, msg
call qword [rcx + 8] ;SIMPLE_TEXT_OUTPUT_INTERFACE.OutputString
section '.data' data readable writeable
msg dq 4300420041h
section '.reloc' fixups data discardable
|25 May 2018, 14:24||
It is easy to test which instruction generates fixups by counting words in the automatically generated contents of the fixups table:
format pe64 efi section '.text' code executable readable mov rdx, msg ; comment out instructions or add new ones cmp rdx, msg ; to test how many fixups are generated push msg lea rdx,