Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer runapp05.tex V1 - 09/04/2008 6:13pm Page 1262

Appendix E: The ELF Binary Format


Since they are very similar to their 32-bit counterparts, they need no extra description.

RelocationTypes


The ELF standard defines a large number of relocation types, and there is a separate set for each sup-
ported architecture. Most of the types are used when dynamic libraries or location-independent code are
generated. On some platforms — particularly on IA-32 platforms — it is also necessary to compensate for
many design errors or historical ballast. Fortunately, the kernel, which is interested only in the relocation
of modules, makes do with just the following two relocation types:

❑ PC-relative relocation
❑ Absolute relocation

PC-relative relocation generates relocation entries that point to addresses in memory that are defined
relative to theprogram counter(PC).^9 These are needed primarily for subroutine calls. The alternative
form of relocation generates absolute addresses, as the name clearly suggests. Typically, these are used
to refer to data in memory that is already known at compilation time — for instance, string constants.

On IA-32 systems the two relocation types are represented by theR_386_PC32constant (PC-relative
relocation) and theR_386_32constant (absolute relocation). The relocation result is computed as follows:

R_386_32:Result=S+A
R_386_PC32:Result=S−P+A

Astands for the addend value that, on IA-32 architecture, is supplied implicitly by the memory contents
of the relocation position.Sis the value of the symbol held in the symbol table, andPstands for the relo-
cation position offset — in other words, the position in the binary file to which the computed data are
to be written. If the addend value is 0, absolute relocations simply insert the value of the symbol in the
symbol table at the relocation position. In PC-relative relocations, however, the difference between sym-
bol position and relocation position is computed — in other words, a computation is made to determine
how many bytes the symbol is from the relocation position.

In both cases, the addend value is added and therefore produces a linear displacement of the result.

ExampleofRelativeDisplacements


The test filetest.oincludes the followingcallstatement:

45: e8 fc ff ff ff call 46 <main+0x2c>

e8istheopcodeofthecallstatement and 0xffffffffc (little endian notation!) is the value passed to the
statement as a parameter. Because IA-32 uses normal relocations instead of add relocations, this value
is the addend value. Therefore, 0xfffffffc is not the final address, but must first run through the relo-
cation process. In decimal terms, 0xfffffffc corresponds to the value−4, but it should be noted that 2’s
complement notation is used to represent signed integers.

(^9) Reminder: The program counter is a special processor register that defines the position of the processor in the machine code during
program execution.

Free download pdf