Problem Loading Elf Executables
Posted: Thu Jan 01, 2009 2:24 pm
Hi, I'm trying to load an Elf file but I'm having problems.
So far I've writen this.
The main problem is that no program headers are present in my .o file and the 9 section headers that are found have all address zero:
Am I compiling the c incorrectly? I'm using cc -nostdlib -nostdinc -fno-builtin -fno-stack-protector -c -o test.o test.c
Thanks,
Gonzalo
So far I've writen this.
Code: Select all
s_node_t *fsnode = finddir_fs(fs_root, "test.o");
Elf32_Ehdr n;
read_fs(fsnode, 0, sizeof(Elf32_Ehdr), &n);
ASSERT(n.e_ident[0] == 0x7f);
ASSERT(n.e_ident[1] == 'E');
ASSERT(n.e_ident[2] == 'L');
ASSERT(n.e_ident[3] == 'F');
ASSERT(n.e_type == 1);
int i;
for(i=0;i<n.e_shnum;i++){
Elf32_Shdr r;
read_fs(fsnode, n.e_shoff + i * n.e_phentsize, sizeof(Elf32_Shdr), &r);
if(r.sh_addr != 0){
printf("Section %d to be loaded from %d to %d (%d bytes)\n", i, r.sh_offset, r.sh_addr, r.sh_size);
}
}
Code: Select all
readelf test.o -S
There are 9 section headers, starting at offset 0xd4:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000034 000031 00 AX 0 0 4
[ 2] .data PROGBITS 00000000 000068 000000 00 WA 0 0 4
[ 3] .bss NOBITS 00000000 000068 000000 00 WA 0 0 4
[ 4] .comment PROGBITS 00000000 000068 000025 00 0 0 1
[ 5] .note.GNU-stack PROGBITS 00000000 00008d 000000 00 0 0 1
[ 6] .shstrtab STRTAB 00000000 00008d 000045 00 0 0 1
[ 7] .symtab SYMTAB 00000000 00023c 000080 10 8 7 4
[ 8] .strtab STRTAB 00000000 0002bc 00000d 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
-----------
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Intel 80386
Version: 0x1
[b] Entry point address: 0x0[/b]
Start of program headers: 0 (bytes into file)
Start of section headers: 212 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 40 (bytes)
Number of section headers: 9
Section header string table index: 6
Am I compiling the c incorrectly? I'm using cc -nostdlib -nostdinc -fno-builtin -fno-stack-protector -c -o test.o test.c
Thanks,
Gonzalo