flat assembler
Message board for the users of flat assembler.
Index
> Linux > gotpc and shared libraries |
Author |
|
Tomasz Grysztar 28 Feb 2023, 12:45
The GOT-related relocation types were not implemented in fasm's ELF64 formatter.
It is easier to add them with fasmg, where the fasm-compatible formatter is just a set of headers. Even though these macros were not written with ease of adding new relocation types in mind, it is still possible without modifying the base headers: Code: ; Assemble with fasmg, with "format" and "cpu" headers sets either in the same directory or in the path provided by INCLUDE variable: include 'format/format.inc' format ELF64 extrn fputs extrn stdout ; The "extrn" macro has defined symbols PLT.fputs and PLT.stdout, ; as variable terms with metadata of form PLT+index, ; where PLT is another special variable term and the constant terms is index into the symbol table. ; Let's define a similarly structured GOT.stdout: element GOT element GOT.stdout : 1 metadataof PLT.stdout - PLT + GOT ; replace PLT term with GOT in the metadata ; Of course we may also alter the "extrn" macro to always define such symbol ; Now override the "dword" instruction that is called by x86 encoders to generate displacement/immediate fields: calminstruction dword? value check 1 elementof (1 metadataof (value + ELF.SECTION_BASE)) eq GOT jyes r_gotpcrel ; If the argument does not contain our special symbol, just call the previous implementation of "dword": call dword?, value exit r_gotpcrel: compute offset, $% emit 4 check $% > offset jno done compute offset, offset - ELF.SECTION_OFFSET compute addend, value + ELF.SECTION_BASE + offset - 1 elementof value compute info, R_X86_64_GOTPCREL + (0 scaleof (1 metadataof value)) shl 32 add_relocation: local Rela compute Rela, ELF.RELOCATION_INDEX * sizeof Elf64_Rela asm store offset at ELF.relocations : Rela + Elf64_Rela.r_offset asm store addend at ELF.relocations : Rela + Elf64_Rela.r_addend asm store info at ELF.relocations : Rela + Elf64_Rela.r_info compute ELF.RELOCATION_INDEX, ELF.RELOCATION_INDEX + 1 done: end calminstruction section '.text' writeable executable public f f: mov rax, [GOT.stdout] lea rdi, [message] mov rsi, qword [rax] jmp PLT.fputs section '.data' message db "test", 10, 0 Code: iterate instruction, call,jmp calminstruction instruction? operand* match =PLT? operand, operand jyes legacy_PLT call instruction, operand exit legacy_PLT: arrange operand, =instruction =PLT.operand assemble operand end calminstruction end iterate I may add this kind of GOT relocations into fasmg's distributed headers - please let me know if that would work for you. I cannot promise the same for fasm 1. |
|||
28 Feb 2023, 12:45 |
|
part time student 02 Mar 2023, 17:15
Thank you for your help. I've downloaded fasmg (the download page calls it "new" and I genuinely didn't realize it's almost 20 years since the initial release ), exported the INCLUDE path and now everything assembles and runs correctly.
Quote:
That would be very nice. |
|||
02 Mar 2023, 17:15 |
|
Tomasz Grysztar 02 Mar 2023, 18:51
part time student wrote: the download page calls it "new" and I genuinely didn't realize it's almost 20 years since the initial release part time student wrote:
|
|||
02 Mar 2023, 18:51 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.