Problem Loading Elf Executables

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
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Problem Loading Elf Executables

Post by gzaloprgm »

Hi, I'm trying to load an Elf file but I'm having problems.

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

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
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: Problem Loading Elf Executables

Post by neonek »

You're compiling code to object file but you aren't linking it, creating executable. Try to remove -c switch.
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Problem Loading Elf Executables

Post by gzaloprgm »

Thanks!
That was exactly the problem.

Cheers,
Gonzalo
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
Post Reply