Position Independent Flat Binary (gcc+ld)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Position Independent Flat Binary (gcc+ld)

Post 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;
}
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
elderK
Member
Member
Posts: 190
Joined: Mon Dec 11, 2006 10:54 am
Libera.chat IRC: elderK
Location: Christchurch, New Zealand
Contact:

Re: Position Independent Flat Binary (gcc+ld)

Post 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
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Position Independent Flat Binary (gcc+ld)

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Position Independent Flat Binary (gcc+ld)

Post 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.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Position Independent Flat Binary (gcc+ld)

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
Post Reply