To do this I tried to compile the C files with the -fpic flag and use a linker script to place the GOT (global offset table) in the final flat binary.
I'm using a simple executable format that just has an identifier, code size, GOT offset and entrypoint in the first 16 bytes of the file.
My problem is that no GOT is produced and the global variables in C are linked to point to ~0x1000 by ld.
Is there any way to tell ld to produce a GOT and link a position independent flat binary?
link.ld
Code: Select all
ENTRY(start)
OUTPUT_FORMAT(binary)
SECTIONS {
loadTo = 0;
entrypoint = start;
. = loadTo;
.text : AT(ADDR(.text)) {
code = .;
*(.text)
*(.rodata*)
}
.data ALIGN (0x1000) : AT(ADDR(.data)) {
data = .;
*(.data)
}
.bss : AT(ADDR(.bss)) {
_sbss = .;
*(COMMON)
*(.bss)
_ebss = .;
bss = .;
}
.got : {
ld_GOT = .;
*(.got)
}
end = .;
codeLength = end - loadTo;
magic = ld_GOT + entrypoint + codeLength;
}


