Page 1 of 1

Position Independent Flat Binary (gcc+ld)

Posted: Fri Aug 01, 2008 8:12 am
by thepowersgang
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

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;
}

Re: Position Independent Flat Binary (gcc+ld)

Posted: Fri Aug 01, 2008 10:02 pm
by elderK
So, let me get this straight. You want to create a PIC executable. Okay, sure. You want all the references to be position independent, thus you want the GOT.

But ... You don't want the relocation information, so that when you actually load it at a random place, you can update the GOT? :P Do you see the problem here?

The GOT is not filled in to the correct values at Linktime. That's the job of the Loader/Linker. It cannot correct the GOT entries without the relocation information.
That relocation information it cannot find without DYNAMIC.

If I got you wrong, I appologize. If not, I suggest you read the ELF Specification in detail. Let it sink in, all will become clear.
~elderK

Re: Position Independent Flat Binary (gcc+ld)

Posted: Sun Aug 03, 2008 1:05 am
by thepowersgang
What I want to do is embed a GOT into a flat binary. My executable loader then adjusts the GOT entries to point to the correct values. The problem is that the GOT is not included in the final binary and gcc reports that the code is already position independent when -fpic is used, even though it uses absolute addresses in some places.

Re: Position Independent Flat Binary (gcc+ld)

Posted: Sun Aug 03, 2008 3:11 am
by JamesM
thepowersgang wrote:What I want to do is embed a GOT into a flat binary. My executable loader then adjusts the GOT entries to point to the correct values. The problem is that the GOT is not included in the final binary and gcc reports that the code is already position independent when -fpic is used, even though it uses absolute addresses in some places.
The Global Offset Table is an ELF concept. It therefore makes sense that the linker has absolutely no idea how to embed one into a flat binary!How would it inform you of where it is? it's size? This is usually done via ELF sections.

Re: Position Independent Flat Binary (gcc+ld)

Posted: Sun Aug 03, 2008 4:31 am
by thepowersgang
To ld the executable is a flat binary but to the OS it is a custom format that has a header defined in an assembler file. I will probebly got for either hacking my normal ELF code to load modules or create a ELF to AMOD (Acess Module) converter.