Position Independent Flat Binary (gcc+ld)
Posted: Fri Aug 01, 2008 8:12 am
I have been designing a kernel module interface for my OS and I planned to use position independent code for it to allow the modules to run in the kernel address space.
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
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;
}