ELF Tutorial: Difference between revisions

Jump to navigation Jump to search
m
→‎ELF Sections: Fix more typing errors
[unchecked revision][unchecked revision]
m (Fix typing errors)
m (→‎ELF Sections: Fix more typing errors)
Line 366:
===The String Table===
 
The string table conceptually is quite simple: it's just a number of consecutive zero-terminated strings. String literals used in the program are stored in one of the tables. There are a number of different string tables that may be present in an ELF object such as .strtab (the default string table), .shstrtab (the section string table) and .dynstr (string table for dynamic linking). AnytimeAny time the loading process needs access to a string, it uses an offset into one of the string tables. The offset may point to the beginning of a zero-terminated string or somewhere in the middle or even to the zero terminator itself, depending on usage and scenario. The size of the string table itself is specified by '''sh_size''' in the corresponding section header entry.
 
The simplest program loader may copy all string tables into memory, but a more complete solution would omit any that are not necessary during runtime such, notably those not flagged with '''SHF_ALLOC''' in their respective section header (such as .shstrtab, since section names aren't used in program runtime).
Line 409:
===Relocation Sections===
 
Relocatable ELF files have many uses in kernel programming, especially as modules and drivers that can be loaded at startup, and are especially useful because they are position independantindependent, thus can easily be placed after the kernel or starting at some convinientconvenient address, and don't require their own address space to function. The process of relocation itself is conceptually simple, but may get more difficult with the introduction of complex relocation types.
 
Relocation starts with a table of relocation entries, which can be located using the relevant section header. There are actually two different kinds of relocation structures; one with an explicit added (section type '''SHT_RELA'''), one without (section type '''SHT_REL'''). Relocation entires in the table are continuous and the number of entries in a given table can be found by dividing the size of the table (given by '''sh_size''' in the section header) by the size of each entry (given by '''sh_entsize'''). Each relocation table is specific to a single section, so a single file may have multiple relocation tables (but all entries within a given table will be the same relocation structure type).
Line 426:
</source>
 
The above are the defintionsdefinitions for the different structure types for relocations. Of note if the value stored in '''r_info''', as the upper byte designates the entry in the symbol table to which the relocation applies, whereas the lower byte stores the type of relocation that should be applied. Note that an ELF file may have multiple symbol tables, thus the index of the section header table that refers to the symbol table to which these relocation apply can be found in the '''sh_link''' field on this relocation table's section header. The value in '''r_offset''' gives the relative position of the symbol that is being relocated, within its section.
 
<source lang="cpp">
Line 439:
</source>
 
As previously mentioned, the '''r_info''' field in '''Elf32_Rel'''('''a''') refers to 2 seperateseparate values, thus the set of macro functions above can be used to attain the individual values; '''ELF32_R_SYM'''() provides access to the symbol index and '''ELF32_R_TYPE'''() provides access to the relocation type. The enumeration '''RtT_Types''' defines the relocation typs this tutorial will encompass.
 
====Relocation Example====
Line 478:
====Relocating a Symbol====
 
As the following function is fairly complex, it's been broken up into smaller managablemanageable chumkschunks and explained in detail. Note that the code shown below assumes that the file being relocated is a relocatable ELF file (ELF executables and shared objects may also contain relocation entries, but are processed somewhat differently). Also note that '''sh_info''' for section headers of type '''SHT_REL''' and '''SHT_RELA''' stores the section header to which the relocation applies.
 
<source lang="cpp">
Line 491:
</source>
 
The above code defines the macro functions that are used to complete relocation calculations. It also retreivesretrieves the section header for the section wherein the symbol exists and computes a reference to the symbol. The variable '''addr''' denotes the start of the symbol's section, and '''ref''' is created by adding the offset to the symbol from the relocation entry.
 
<source lang="cpp">
Line 502:
</source>
 
Next the value of the symbol being relocated is accessed. If the symbol table index stored in '''r_info''' is undefined, then the value defaults to 0. The code also references a function called '''elf_get_symval'''(), which was implemented previously. If the value returned by the function is equal to '''ELF_RELOC_ERR''', relocation is stopedstopped and said error code is returned.
 
<source lang="cpp">
Line 527:
</source>
 
Finally, this segment of code details the actual relocation process, performing the necessary calculating the relocated symbol and returning it's value on success. If the relocation type is unsupported an error message is displayed, relocation is stopedstopped and the function returns an error code. Assuming no errors have occuredoccurred, relocation is now complete.
 
==The ELF Program Header==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu