Page 1 of 1

Fixing up an ELF executable

Posted: Tue Jun 19, 2007 7:30 am
by AJ
Hi,

My kernel exe loader currently deals with PE and DJGPP-COFF files, but I am just in the process of adding ELF support.

Reading through the ELF docs, I think it all looks fairly similar to what I already do for COFF, but I'm a little confused over Program Headers and Section Headers.

Am I correct in thinking that for a statically linked executable with no residual dependancies, I only need to look at the Program Headers and fix-up based on that data?

Now, supposing as a debug tool, I want to display a table of sections (as I do for COFF files), I would need to obtain section names ".text", ".data" etc... However, the Elf32_Phdr structure does not appear to have a pointer in to the string table. Do I somehow need to extract these names from the section header table rather than the program header table?

I'm also a little confused over
An object file segment contains one or more sections. Program headers are meaningful only for executable and shared object files.
This seems a bit fractal :shock: . A program header points to a segment, which can contain one or more sections - but I have a separate section header table, so why do I need the program header table at all?

I hope someone can shed some light!

Cheers,
Adam[/quote]

Posted: Tue Jun 19, 2007 7:58 am
by jnc100
Yeah, its a bit confusing. Basically, segments are defined in the program headers, sections in the section table. A segment can contain both initialised and uninitialised data (referenced by the p_filesz and p_memsz sections respectively) which I should imagine correspond to the .data and .bss sections respectively. You would then likely have another segment, marked read-only, execute, which corresponds to the .text section.

In general, to run a (statically-linked) program, you just need all the segments marked PT_LOAD. As to why there are two separate tables in the ELF file, I have no idea. I guess the designers first wanted a file type that could represent all information regarding an executable/library/object file which could be placed in the section headers, then wanted to add extra information regarding read/write/execute access and everything in the program headers.

Regards,
John

Posted: Tue Jun 19, 2007 8:09 am
by AJ
Thanks for the explanation - I'm just writing the implementation now.