How to set the start address of the text segment?

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
andrew_w
Posts: 19
Joined: Wed May 07, 2008 5:06 am

How to set the start address of the text segment?

Post by andrew_w »

I am trying to build dummy executables to test my loader. I can get 32-bit executables to link with the specified text segment start address, but 64-bit ones always seem to be linked with a text segment start address of 0, with the specified address only affecting the start of the text section within the segment rather than the start of the segment itself (i.e. there is a large amount of padding between the ELF header and the start of the text section). How do I prevent the text segment from being padded to start at 0 (and why does this only happen to 64-bit executables)?

Here is the linker script that I am using (the only difference between 32- and 64-bit versions are the OUTPUT_FORMAT and OUTPUT_ARCH lines). It is based on the libpayload linker script from coreboot. I am building my executables with GNU ld on Linux.

Code: Select all

BASE_ADDRESS = 0x100000;

OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)

ENTRY(_start)

HEAP_SIZE = 16384;
STACK_SIZE = 16384;

SECTIONS
{
	. = BASE_ADDRESS;

	.text : {
		*(.text._entry)
		*(.text)
		*(.text.*)
	}
	.rodata : {
		*(.rodata)
		*(.rodata.*)
	}
	. = ALIGN(0x1000);
    _etext = .;

	.data : {
		*(.data)
		*(.data.*)
	}

	_edata = .;

	.bss : {
		*(.sbss)
		*(.sbss.*)
		*(.bss)
		*(.bss.*)
		*(COMMON)

		/* Stack and heap */

		. = ALIGN(16);
		_heap = .;
		. += HEAP_SIZE;
		. = ALIGN(16);
		_eheap = .;

		_estack = .;
		. += STACK_SIZE;
		. = ALIGN(16);
		_stack = .;
	}

	_end = .;

	/DISCARD/ : { 
		*(.comment)
		*(.note.gnu.build-id)
	}
}
Developer of UX/RT, a QNX/Plan 9-like OS
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: How to set the start address of the text segment?

Post by egos »

Maybe linker generates 64-bit position independent executables.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply