An experiment on object files.
Basically on static linking.
- Go to WSL environment.
- Move the terminal panel to the right.
- Compile the code:
make main
. - experiments:
-
check disassembled:
objdump -d main
objdump -d main.o
objdump -d sum.o
-
check the symbols:
readelf -SWs main
readelf -SWs main.o
readelf -SWs sum.o
-
check symbol table:
readelf -r main
readelf -r main.o
readelf -r sum.o
-
check hex:
hexdump -C main
hexdump -C main.o
hexdump -C sum.o
-
- cases
objdump -d main.o
-
8: be 02 00 00 00 mov $0x2,%esi d: bf 00 00 00 00 mov $0x0,%edi 12: e8 00 00 00 00 callq 17 <main+0x17>
02 00 00 00
-> 2- little-endian
- left is low address.
int
has 4 bytes- hex has 4 bits.
- line
8
: load argument2
. - line
d
: load argumentarray
.00 00 00 00
- unknown address to
array
.
- unknown address to
- line
12
: call functionsum
.00 00 00 00
- unknown address to
sum
.
- unknown address to
-
objdump -d main
-
In
<main>
:40049f: be 02 00 00 00 mov $0x2,%esi 4004a4: bf 30 10 60 00 mov $0x601030,%edi 4004a9: e8 0a 00 00 00 callq 4004b8 <sum>
- those addresses are resolved.
-
readelf -r main.o
-
Relocation section '.rela.text' at offset 0x498 contains 2 entries: Offset Info Type Sym. Value Sym. Name + Addend 00000000000e 000d0000000a R_X86_64_32 0000000000000000 array + 0 000000000013 000f00000002 R_X86_64_PC32 0000000000000000 sum - 4
R_X86_64_32
:= replace the0
s to a absolute addressR_X86_64_PC32
:= replace the0
s to a relative address to PC.
-
objdump -dx main.o
-
8: be 02 00 00 00 mov $0x2,%esi d: bf 00 00 00 00 mov $0x0,%edi e: R_X86_64_32 array 12: e8 00 00 00 00 callq 17 <main+0x17> 13: R_X86_64_PC32 sum-0x4 17: 89 45 fc mov %eax,-0x4(%rbp)
- it shows the relocation records.
e: R_X86_64_32 array
13: R_X86_64_PC32 sum-0x4
e:
: ate
offset from the beginning ofmain
, there has a reference to a memory location.- memory location: global data structure array.
R_X86_64_32 array
- the target address, linker overwrites the
00 00 00 00
to which, is the absolute address ofarray
. R_X86_64_32
32
: 32 bits -> 4 bytes -> overwrite the following four bytes -> overwrite00 00 00 00
.
- the target address, linker overwrites the
R_X86_64_PC32 sum-0x4
-0x4
:addend
.<the exact address of sum> - 4 = <address of the 00 00 00 00> + <address that is overwrited to 00 00 00 00>
.- PC alway pointers to the next instruction while CPU is working on the previous instruction.
- PC is pointing
17
while the instruction at12
is executing.
- PC is pointing
- it shows the relocation records.
-
objdump -d main
-
In
<main>
:40049f: be 02 00 00 00 mov $0x2,%esi 4004a4: bf 30 10 60 00 mov $0x601030,%edi 4004a9: e8 0a 00 00 00 callq 4004b8 <sum> 4004ae: 89 45 fc mov %eax,-0x4(%rbp)
0a 00 00 00
=4004b8
-4004ae
.- PC alway pointers to the next instruction while CPU is working on the previous instruction.
- PC is pointing
4004ae
while the instruction at4004a9
is executing.
- PC is pointing
-
objdump -dx main
-
SYMBOL TABLE: ... 0000000000601030 g O .data 0000000000000010 array ... 00000000004004b8 g F .text 0000000000000045 sum
- linker at static linking
- linker combine the relocatable object files.
- linker looks for the relocation entries in
.rel.data
and.rel.text
inmain.o
andsum.o
.- eg. linker found
array
inmain.o
.
- eg. linker found
- linker finds exact address of the
array
in the combined file. - linker replaces the 4 bytes
00 00 00 00
started from addresse
to the new address.
.data
:array
is put at.data
section.
- linker at static linking
-
- compile
make main.2
- cases
objdump -dx main.2.o
-
8: be 02 00 00 00 mov $0x2,%esi d: bf 00 00 00 00 mov $0x0,%edi e: R_X86_64_32 array+0xc 12: e8 00 00 00 00 callq 17 <main+0x17> 13: R_X86_64_PC32 sum-0x4
array+0xc
+0xc
addend
.- the array offset.
-
Ndx
:= index of the section where it has been.UND
:= unknown. From other relocatable object file.1
:=.text
3
:=.data
.rela
:= relocation entries.- only relocatable objects have.
- executable objects do not have.
- those entries are resolved by linking.
typedef struct {
long offset; /* Offset of the reference to relocate */
long type:32, /* Relocation type */
symbol:32; /* Symbol table index */
long addend; /* Constant part of relocation expression */
} Elf64_Rela;
offset
- offset in
.text
/.data
section.
- offset in
objdump -dx main.o
:
8: be 02 00 00 00 mov $0x2,%esi
d: bf 00 00 00 00 mov $0x0,%edi
e: R_X86_64_32 array
12: e8 00 00 00 00 callq 17 <main+0x17>
13: R_X86_64_PC32 sum-0x4
17: 89 45 fc mov %eax,-0x4(%rbp)
array
offset = e
type = R_X86_64_32
symbol
not shown here.addend = 0
sum
offset = 13
type = R_X86_64_PC32
symbol
not shown here.addend = -0x4
no-pie
- That flag is telling gcc not to make a position independent executable (PIE). PIE is a precodition to enable address space layout randomization (ASLR). ASLR is a security feature where the kernel loads the binary and dependencies into a random location of virtual memory each time it's run.
-
GNU
binutils
ar
. Creates static libraries, and inserts, deletes, lists, and extracts members.strings
. Lists all of the printable strings contained in an object file.strip
. Deletes symbol table information from an object file.nm
. Lists the symbols defined in the symbol table of an object file.size
. Lists the names and sizes of the sections in an object file.readelf
. Displays the complete structure of an object file, including all of the information encoded in the ELF header. Subsumes the functionality ofsize
andnm
.objdump
. The mother of all binary tools. Can display all of the information in an object file. Its most useful function is disassembling the binary instructions in the.text
section.
-
Linux systems
ldd
: Lists the shared libraries that an executable needs at run time.
- https://youtu.be/E804eTETaQs
- csapp/Chapter 7: Linking.