05 March 2017, 14:07 | #1 |
Registered User
Join Date: Jan 2017
Location: London, UK
Posts: 433
|
ELF Vs Hunk
I'm quite familiar with the Hunk format. It's nice an straight forward to parse from a file into memory segments:
1. Check for a hunk signature 1011. 2. Scan the hunk table and allocate required memory. 3. Identify hunk type (only care about CODE, DATA or BSS). 4. If hunk type is BSS, skip to step 7. 5. Copy hunk data into the pre allocated memory. 6. Check for RELOC information at the end of the hunk: If present, write the correct segment memory address to the stated offset. 7. advance to the next hunk and jump back to step 3, until no more hunks. It's a nice executable file format. Now I am exploring the ELF file format. And it seem to be well structured, with plenty of extra information in the header for supporting different systems. But I have a few questions which I can't find decent answers to: 1. There appear to be two distinct types of file... an executable object and a relocatable object... It is my understanding that the "Executable Objects", assume the code will be loaded to fixed address, which is common on UNIX machines with virtual memory and that Relocatable objects are far more like amiga Hunk files, where the code is supplied with relocations information. If the above assumption is true then the "Executable Object" is of no interest to me, and I need to spend my time learning how the ELF stores its relocation information. 2. Are there any good resources for learning about how ELF relocation is done? I'm currently reading: http://wiki.osdev.org/ELF_Tutorial But any other resource would be good! Thanks |
05 March 2017, 15:42 | #2 | |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 54
Posts: 4,501
|
Quote:
5b. fill up with 0 remaining allocated memory I'm making a offload system loader and facing the problem of extra space in CODE or DATA section No help for the ELF, sorry. |
|
05 March 2017, 16:07 | #3 | ||
Registered User
Join Date: Jan 2017
Location: London, UK
Posts: 433
|
Quote:
Quote:
|
||
05 March 2017, 16:33 | #4 |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 54
Posts: 4,501
|
|
05 March 2017, 20:14 | #5 | |||
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,553
|
Quote:
Quote:
Quote:
|
|||
05 March 2017, 22:33 | #6 | |
Registered User
Join Date: Mar 2016
Location: Australia
Posts: 882
|
Quote:
https://github.com/alpine9000/BitMac...h-pages/elf.js |
|
05 March 2017, 22:57 | #7 | |||
Registered User
Join Date: Jan 2017
Location: London, UK
Posts: 433
|
Quote:
Quote:
Quote:
Last edited by bloodline; 05 March 2017 at 23:46. |
|||
05 March 2017, 23:01 | #8 | |
Registered User
Join Date: Jan 2017
Location: London, UK
Posts: 433
|
Quote:
|
|
07 March 2017, 15:41 | #9 |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,553
|
The sections in ELF executables are grouped as segments, which are usually page-aligned for the MMU to support memory protection (1st segment is usually code and read-only data, which is write-protected), altough this is probably irrelevant for you.
Segments also have file size and memory size in their PHDR, which allows techniques like stripping uninitialized data from the end of your code or data sections. This is not possible in ELF objects. Also, when you want to use a standard GNU-ld linker, you have no possibility to detect unresolved symbol references, when generating another object file as output (-r option), as they are allowed in objects. |
22 March 2017, 11:42 | #10 |
Registered User
Join Date: Jan 2017
Location: London, UK
Posts: 433
|
So, after some playing with compiler settings, I have found that (as phx said) the segments are to be found in the Program header. My ELFs seem to always have two segments:
1. A text (code) and rodata (constants) segment. 2. A data and bss segment. I can allocate memory and load these as I would with a hunk executable... But still I was unable to workout how to relocate the code/data to point to the correct memory... After much playing around with gcc settings, I found that I needed to pass --emit-relocs to the linker (via the -Wl option) to get it to generate relocation information. This still wasn't very useful as the code was still using absolute references... finally passing the -fPIC option to gcc made forced gcc to generate position independent code (yes I appreciate that seems obvious now...) with a table of pointers at the end of the text section to point to the variables in the data segment... finally I'm getting somewhere! This also adds a Global Offset Table... which I'm not sure what to do with yet... positive side effect, I'm becoming increasingly familiar with ARM assembler |
22 March 2017, 23:51 | #11 | |||
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,553
|
The combination of ELF and ARM makes this increasingly off-topic...
Quote:
Quote:
Quote:
Coming back to your relocation problem. With ELF there are two types of relocation entries: with and without addends. Reloc tables named ".secname.rela" have the addend included. ".secname.rel" have not. In the last case the addend must be extracted from the location in the section, before the relocation can take place. This is the same method as with Amiga hunk format. Easier for you (and common for PPC, but not for x86, not sure about ARM) would be when the addend is included. Then you just have to take the new load address of the target section, add the addend and overwrite whatever there was in the location to relocate. Otherwise you have to read the location first and subtract the relocation section's absolute base address, then add the real address again. |
|||
23 March 2017, 16:32 | #12 | ||||
Registered User
Join Date: Jan 2017
Location: London, UK
Posts: 433
|
Indeed, I would be happy to move the topic if anyone is annoyed, but there is no other forum where I have to opportunity to converse with coders like yourself, who are happy to indulge my naive understanding of C compilers/linkers
I would be happy to simply use the HUNK format, but ld only wants to produce ELF files... and since I want to support both ARM and x86 with my code, ELF seems like a good choice. Quote:
Quote:
Quote:
Quote:
|
||||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Elf (OCEAN) problem | Bolch | support.Games | 20 | 11 December 2014 12:25 |
Elf - Ocean's Elf - could do with an update. | MethodGit | project.WHDLoad | 9 | 28 August 2013 18:50 |
Elf (Ocean) - US version | MethodGit | request.Old Rare Games | 10 | 21 September 2012 17:22 |
Elf Mania CDTV | macce2 | Retrogaming General Discussion | 15 | 03 December 2006 03:07 |
Can anybone help me with Elf? | robbert | support.Games | 1 | 21 April 2006 21:04 |
|
|