elf linker specification - Hiroshi123/bin_tools GitHub Wiki

Division of fixed size sections and non-fixed size sections.

If you compile a c program with gcc, it should emit the minimal amount of program headers which are required. That means the sections which are added by linker may join the sections which are supplied from input object files. It is good for loading computational performance, while requires some efforts on linking process. This is because linker needs to emit sections whose size are dependent on relocation step, These are, for instance, .plt, .plt.got, .rela.plt, .rela.dyn, .dynamic, .dynstr, .dynsym. If none of the object files are resolved a symbol that you have defined on an object file, you just need to check if the symbol will be resolved by external shared libraries. If it is, then, you need to add an entry on a section which might alter the virtual address of proceeding section. If these sections are put before the section whose address will be resolved, you cannot change their size. Default ld may insert sections which are non-size fixed before relocation step between sections which needs to be relocated if section type and section flags are matched.

Program header type Program header flags Section name Section type Section flags
PT_LOAD READ .rodata PROGBITS ALLOC
PT_LOAD READ & WRITE .data,.bss PROGBITS(NOTBITS) ALLOC & WRITE
PT_LOAD READ & Execute .text PROGBITS ALLOC & EXECUTE
PT_LOAD READ & WRITE & EXECUTE .plt,.plt.got,.got,.init,.fini PROGBITS ALLOC & WRITE & EXECUTE
PT_LOAD READ .dynsym, .dynstr,.rela.dyn,.rela.plt, ALLOC
PT_LOAD READ & WRITE .init_array,.fini_array,.dynamic ALLOC & WRITE
PT_DYNAMIC READ & WRITE .dynamic
PT_NOTE READ
PT_GNU_EH_FRAME READ
PT_GNU_STACK READ & WRITE
PT_GNU_RELRO READ

Section Comparison

Comparison and Difference between SectionHeader(ELF) and ImageSectionHeader(PE).
The biggest and most notable one is the PE shdr is mandate to be loaded, otherwise shdr on elf is not necessarily. This is because program header on elf which is loaded by dynamic loader on elf points directly data or code on section, while, ImageOptionalHeader on PE will point to offset of section header. To put it simple, the one that elf does gets loading process faster, but it requires to add some information on program header as the field of section header does not effect on dynamic loading process.

||elf|coff|

elf(shdr) elf(phdr) PE(coff) operation
sh_name Name
sh_type p_type Characteristics only fixed type should be program header
sh_flags p_flags Characteristics sections which has same flags should be aggregated
sh_addr p_vaddr(p_paddr) VirtualAddress set before relocation
sh_offset p_offset PointerToRawData converted on emit output file from original pointer
sh_size p_filesz SizeOfRawData Needs to be summed up
p_memsz VirtualSize Loader will allocate this size
sh_link
sh_info PointerToRelocations
sh_addralign p_align Characteristics
sh_entsize

Loader Specification

Symbol table specification

elf coff
st_name
st_info
st_other
st_shndx
st_value
st_size

relocation comparison

elf coff
static linking gathered on .rela.text section each section may contain relocation information

Static linking

elf coff role
r_offset VirtualAddress Offset from section head
r_info SymbolTableIndex Relocation needs a name to be resolved. This symbol table entry provides this
r_info Type
r_addend

ObjectChain -> SectionChain