SlideShare a Scribd company logo
1 of 38
Download to read offline
The ELF File Format and the Linux Loader
Divino C´esar S. Lucas
Universidade de Campinas - Instituto de Computac¸ ˜ao
Laborat´orio de Sistemas de Computac¸ ˜ao
(www.lsc.ic.unicamp.br)
XIV F´orum Internacional de Software Livre
05 de Julho de 2013
The ELF Format
The Linux ELF Loader
Questions
Agenda
1 The ELF (Executable and Linkable Format) Format
2 The Linux ELF Loader
3 Questions
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
The Pieces of a Program
gcc -c -O3 bob.c -o bob.o // relocatable files
gcc -c -O3 main.c -o main.o
gcc bob.o main.o -o hello // executable file
gcc -shared -fPIC bob.c -o libhello.so // shared library
gcc main.c -lhello -L. -o hello // dynamically linked executable
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
What the file looks inside?
The Matrix?
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
What the file looks inside?
The Matrix?
Not. An ELF!
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
The Executable and Linkable Format (ELF)
Specification for Executable and Linkable Files.
Successor of a.out and COFF formats.
Besides Linux, it’s also used in: FreeBSD, OpenBSD,
Solaris, HP-UX, etc.
Used in many devices: PlayStation 3, Dreamcast, Some
Nokia Cellphones.
Support for modern programming languages and code
reutilization.
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
The ELF Header
The ELF Header
typedef struct elf32 hdr {
unsigned char e ident [ EI NIDENT ] ;
Elf32 Half e type ;
Elf32 Half e machine ;
Elf32 Word e version ;
Elf32 Addr e entry ;
Elf32 Off e phoff ;
Elf32 Off e shoff ;
Elf32 Word e flags ;
Elf32 Half e ehsize ;
Elf32 Half e phentsize ;
Elf32 Half e phnum ;
Elf32 Half e shentsize ;
Elf32 Half e shnum ;
Elf32 Half e shstrndx ;
} Elf32 Ehdr ;
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
The ELF Header - Example
The ELF Header
$ readelf -h executavel
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2’s complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x400410
Start of program headers: 64 (bytes into file)
Start of section headers: 4440 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 30
Section header string table index: 27
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
Program Representation
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
ELF Sections
Mainly used during static linking process.
Used to group together program portions with are
sematically related (eg: data / code / constructors /
destructors).
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
ELF Sections - Example
ELF Header
Sections Table
. . .
.text
.data
.bss
. . .
Allice.o
ELF Header
Sections Table
Segments Table
. . .
.text
.text
.data
.data
.bss
.bss
. . .
Executable
ELF Header
Sections Table
. . .
.text
.data
.bss
. . .
Bob.o
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
ELF Sections - Example
Section Table
$ readelf -WS executavel
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 1] .interp PROGBITS 0000000000400238 000238 00001c 00 A 0 0 1
[11] .init PROGBITS 00000000004003c8 0003c8 000018 00 AX 0 0 4
[12] .plt PROGBITS 00000000004003e0 0003e0 000030 10 AX 0 0 16
[13] .text PROGBITS 0000000000400410 000410 000228 00 AX 0 0 16
[15] .rodata PROGBITS 0000000000400648 000648 000031 00 A 0 0 4
[22] .got PROGBITS 0000000000600fe0 000fe0 000008 08 WA 0 0 8
[24] .data PROGBITS 0000000000601010 001010 000020 00 WA 0 0 8
[25] .bss NOBITS 0000000000601030 001030 000010 00 WA 0 0 8
[28] .symtab SYMTAB 0000000000000000 0018d8 000690 18 29 48 8
[29] .strtab STRTAB 0000000000000000 001f68 00022b 00 0 0 1
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
ELF Segments
Used mainly during process creation (by the dynamic
loader).
Used to group together sections with the same flags (all
text, all data.
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
ELF Segments - Example
Program Headers
$ readelf -Wl executavel
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000040 0x0000000000400040 0x0000000000400040 0x0001f8 0x0001f8 R E 0x8
INTERP 0x000238 0x0000000000400238 0x0000000000400238 0x00001c 0x00001c R 0x1
LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x00079c 0x00079c R E 0x200000
LOAD 0x000e28 0x0000000000600e28 0x0000000000600e28 0x000208 0x000218 RW 0x200000
DYNAMIC 0x000e50 0x0000000000600e50 0x0000000000600e50 0x000190 0x000190 RW 0x8
NOTE 0x000254 0x0000000000400254 0x0000000000400254 0x000044 0x000044 R 0x4
GNU_EH_FRAME 0x00067c 0x000000000040067c 0x000000000040067c 0x00003c 0x00003c R 0x4
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x8
GNU_RELRO 0x000e28 0x0000000000600e28 0x0000000000600e28 0x0001d8 0x0001d8 R 0x1
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Relocatable, Shared Libraries and Executable Files
What / Why ELFs?
The ELF Header
Sections
Segments
ELF Segments - Sections to Segments
Mapping from Sections to Segments
$ readelf -Wl executavel
00
01 .interp
02 .interp .gnu.hash .dynsym .dynstr .rela.dyn .rela.plt .init .plt .text .fini .rodata
03 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss
04 .dynamic
05 .note.ABI-tag .note.gnu.build-id
06 .eh_frame_hdr
07
08 .ctors .dtors .jcr .dynamic .got
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
How a Process Start?
Function File Comment
Shell/GUI user application
Executes fork() to create a news process
and execve() to replace the child.
execve fs/exec.c
Do some preprocessing stuff and calls
search binary handler.
search binary handler fs/exec.c
Identify which type is the binary
and calls the appropriate handler.
load elf binary fs/binfmt elf.c
Validate the binary file and
do some preprocessing.
start thread arch/x86/kernel/process.c Start a new thread of execution.
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Loader - workflow
1 Load the main binary.
2 Load the main binary dependencies (shared libraries).
3 Create a symbol resolution map.
4 Apply data relocations (fill the GOT table).
5 Apply function relocations (fill the GOT.PLT table).
6 Call libraries initializers, registry finalizers and start the
program.
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Loading the main binary
Loadable Segments
$ readelf -Wl executavel
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000040 0x0000000000400040 0x0000000000400040 0x0001f8 0x0001f8 R E 0x8
INTERP 0x000238 0x0000000000400238 0x0000000000400238 0x00001c 0x00001c R 0x1
**** LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x00079c 0x00079c R E 0x200000
**** LOAD 0x000e28 0x0000000000600e28 0x0000000000600e28 0x000208 0x000218 RW 0x200000
**** DYNAMIC 0x000e50 0x0000000000600e50 0x0000000000600e50 0x000190 0x000190 RW 0x8
NOTE 0x000254 0x0000000000400254 0x0000000000400254 0x000044 0x000044 R 0x4
GNU_EH_FRAME 0x00067c 0x000000000040067c 0x000000000040067c 0x00003c 0x00003c R 0x4
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x8
GNU_RELRO 0x000e28 0x0000000000600e28 0x0000000000600e28 0x0001d8 0x0001d8 R 0x1
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
The Dynamic Section/Segment
Dynamic Information Block
$ readelf -dW executavel
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000c (INIT) 0x4003c8
0x000000000000000d (FINI) 0x400638
0x000000006ffffef5 (GNU_HASH) 0x400298
0x0000000000000005 (STRTAB) 0x400318
0x0000000000000006 (SYMTAB) 0x4002b8
0x000000000000000a (STRSZ) 63 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000003 (PLTGOT) 0x600fe8
0x0000000000000002 (PLTRELSZ) 48 (bytes)
0x0000000000000014 (PLTREL) RELA
0x0000000000000017 (JMPREL) 0x400398
0x0000000000000007 (RELA) 0x400380
0x0000000000000008 (RELASZ) 24 (bytes)
0x0000000000000009 (RELAENT) 24 (bytes)
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Dependences from my dependences...
Executable
libz.so
liby.so
...
libc.so
libb.so
liba.so
libb.so
libe.so
libd.so
libc.so
liby.so
libg.so
libf.so
libe.so
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Shared Libraries: ldd and ldconfig
ldconfig to find dynamic dependencies
$ ldconfig -p
...
libBrokenLocale.so.1 (libc6, OS ABI: Linux 2.6.24) => /lib/i386-linux-gnu/libBrokenLocale.so.1
ld-linux-x86-64.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
libQtXml.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtXml.so.4
libQtWebKit.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4
libQtSvg.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtSvg.so.4
libQtSql.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtSql.so.4
...
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Process Address Space
Executable (.text)
Executable (.data)
Executable ( .bss)
HEAP
Library-A (.text)
Library-A (.data)
Library-A ( .bss)
Library-B (.text)
Library-B (.data)
Library-B ( .bss)
STACK
Lower Addresses
Higher Addresses
Process Address Space
Fixed distance
Fixed distance
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Symbol resolution scope and ordering
Executable
libz.so
liby.so
...
libc.so
libb.so
liba.so
libb.so
libe.so
libd.so
libc.so
liby.so
libg.so
libf.so
libe.so
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Relocations
Relocation is the process of connecting symbolic
references with symbolic definitions.
There are data relocations and function relocations.
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Relocations - Why do we need relocations (Part 1)?
gcc -c -O3 bob.c -o bob.o // relocatable files
gcc -c -O3 main.c -o main.o
gcc bob.o main.o -o hello // executable file
gcc -shared -fPIC bob.c -o libhello.so // shared library
gcc main.c -lhello -L. -o hello // dynamically linked executable
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Relocations - Why do we need relocations (Part 1)?
gcc -c -O3 bob.c -o bob.o // relocatable files
gcc -c -O3 main.c -o main.o
gcc bob.o main.o -o hello // executable file
gcc -shared -fPIC bob.c -o libhello.so // shared library
gcc main.c -lhello -L. -o hello // dynamically linked executable
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Relocations - Why do we need relocations (Part 2)?
Executable (.text)
Executable (.data)
Executable ( .bss)
HEAP
Library-A (.text)
Library-A (.data)
Library-A ( .bss)
Library-B (.text)
Library-B (.data)
Library-B ( .bss)
STACK
Process A
. . .
. . .
Library-A (.text)
Library-A (.data)
Library-A ( .bss)
Library-A (.data)
Library-A ( .bss)
Library-B (.text)
Library-B (.data)
Library-B ( .bss)
Library-C (.text)
Library-C (.data)
Library-C ( .bss)
. . .
. . .
Physical Memory
Executable (.text)
Executable (.data)
Executable ( .bss)
HEAP
Library-A (.text)
Library-A (.data)
Library-A ( .bss)
Library-C (.text)
Library-C (.data)
Library-C ( .bss)
STACK
Process B
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Relocations - Where are them?
Data and function relocations
$ readelf -rW executavel
Relocation section ’.rela.dyn’ at offset 0x380 contains 1 entries:
Offset Info Type Symbol’s Value Symbol’s Name + Addend
0000000000600fe0 0000000300000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
Relocation section ’.rela.plt’ at offset 0x398 contains 2 entries:
Offset Info Type Symbol’s Value Symbol’s Name + Addend
0000000000601000 0000000100000007 R_X86_64_JUMP_SLOT 0000000000000000 printf + 0
0000000000601008 0000000200000007 R_X86_64_JUMP_SLOT 0000000000000000 __libc_start_main + 0
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Dynamic Symbol Table (dynsym)
$ readelf -Ws libtest.so
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000498 0 SECTION LOCAL DEFAULT 9
2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__
3: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND foo
4: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
5: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5 (2)
6: 0000000000201018 0 NOTYPE GLOBAL DEFAULT ABS _edata
7: 0000000000201028 0 NOTYPE GLOBAL DEFAULT ABS _end
8: 0000000000201018 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
9: 00000000000005ac 11 FUNC GLOBAL DEFAULT 11 function
10: 0000000000000498 0 FUNC GLOBAL DEFAULT 9 _init
11: 00000000000005f8 0 FUNC GLOBAL DEFAULT 12 _fini
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Dynamic String Table (dynstr)
$ readelf -p .dynstr executavel
String dump of section ’.dynstr’:
[ 1] libhello.so
[ d] __gmon_start__
[ 1c] _Jv_RegisterClasses
[ 30] bob_speak
[ 3a] _init
[ 40] _fini
[ 46] libc.so.6
[ 50] __libc_start_main
[ 62] _edata
[ 69] __bss_start
[ 75] _end
[ 7a] GLIBC_2.2.5
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Global Offset Table
gcc -shared -fPIC hello.c -o libhello.so
#include <stdio . h>
int valor1 ;
int fun1 ( void ) {
valor1 = 123;
p r i n t f ( ” Valor de valor1 = %dn ” , valor1 ) ;
return 0;
}
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Global Offset Table
objdump -d libhello.so
5f0 : mov 0x2009d9(% r i p ),% rax # 200fd0 < DYNAMIC+0x188>
5f7 : movl $0x7b ,(% rax )
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Global Offset Table
readelf -WS libhello.so
[20] .got PROGBITS 0000000000200 fc8 000fc8 000020 08 WA 0 0 8
[21] . g o t . p l t PROGBITS 0000000000200fe8 000fe8 000028 08 WA 0 0 8
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Global Offset Table
readelf -Wr libhello.so
Relocation section ’.rela.dyn’ at offset 0x428 contains 5 entries:
Offset Info Type Symbol’s Value Symbol’s Name + Addend
...
0000000000200fd0 0000000c00000006 R_X86_64_GLOB_DAT 0000000000201028 valor1 + 0
...
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Procedure Linkage Table
objdump -d exec
4005 fd : c a l l q 400500 <fun1@plt>
400500 <fun1@plt >:
400500: jmpq ∗0x200b02(% r i p ) # 601008 < GLOBAL OFFSET TABLE +0x20>
400506: pushq $0x1
40050b : jmpq 4004e0 < i n i t +0x20>
4004e0 < l i b c s t a r t m a i n @ p l t −0x10 >:
4004e0 : pushq 0x200b0a(% r i p ) # 600 f f 0 < GLOBAL OFFSET TABLE +0x8>
4004e6 : jmpq ∗0x200b0c(% r i p ) # 600 f f 8 < GLOBAL OFFSET TABLE +0x10>
4004ec : nopl 0x0(%rax )
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Procedure Linkage Table
readelf -Wr exec
Offset Info Type Symbol’s Value Symbol’s Name + Addend
0000000000601000 0000000100000007 R_X86_64_JUMP_SLOT 0000000000000000 __libc_start_main + 0
0000000000601008 0000000300000007 R_X86_64_JUMP_SLOT 0000000000000000 fun1 + 0
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
1) Load the main binary
2) Load the main binary dependencies
3) Create a symbol resolution map
4 and 5) Apply data and function relocations
6) Initialize libraries and jump to program start.
Program Initialization Flow
Divino C´esar S. Lucas The ELF File Format and the Linux Loader
The ELF Format
The Linux ELF Loader
Questions
Thank you!
Questions?
divcesar [at] gmail [dot] com
http://johntortugo.wordpress.com
Divino C´esar S. Lucas The ELF File Format and the Linux Loader

More Related Content

What's hot

Implementing generic JNI hardware control for Kotlin based app on AOSP
Implementing generic JNI hardware control for Kotlin based app on AOSPImplementing generic JNI hardware control for Kotlin based app on AOSP
Implementing generic JNI hardware control for Kotlin based app on AOSPCheng Wig
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesChris Simmonds
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧Will Huang
 
Introduction to GitHub Actions
Introduction to GitHub ActionsIntroduction to GitHub Actions
Introduction to GitHub ActionsKnoldus Inc.
 
LCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEELCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEELinaro
 
LCU13: An Introduction to ARM Trusted Firmware
LCU13: An Introduction to ARM Trusted FirmwareLCU13: An Introduction to ARM Trusted Firmware
LCU13: An Introduction to ARM Trusted FirmwareLinaro
 
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEEBKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEELinaro
 
Secure storage updates - SFO17-309
Secure storage updates - SFO17-309Secure storage updates - SFO17-309
Secure storage updates - SFO17-309Linaro
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/CoreShay Cohen
 
Eclipse Iceoryx Overview
Eclipse Iceoryx OverviewEclipse Iceoryx Overview
Eclipse Iceoryx OverviewTomoya Fujita
 
Runtime Symbol Resolution
Runtime Symbol ResolutionRuntime Symbol Resolution
Runtime Symbol ResolutionKen Kawamoto
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationWei-Ren Chen
 
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...Kuniyasu Suzaki
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsBrendan Gregg
 
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !Pierre-jean Texier
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1LiviaLiaoFontech
 
LCA14: LCA14-418: Testing a secure framework
LCA14: LCA14-418: Testing a secure frameworkLCA14: LCA14-418: Testing a secure framework
LCA14: LCA14-418: Testing a secure frameworkLinaro
 

What's hot (20)

Implementing generic JNI hardware control for Kotlin based app on AOSP
Implementing generic JNI hardware control for Kotlin based app on AOSPImplementing generic JNI hardware control for Kotlin based app on AOSP
Implementing generic JNI hardware control for Kotlin based app on AOSP
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot images
 
你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧你一定不能不知道的 Markdown 寫作技巧
你一定不能不知道的 Markdown 寫作技巧
 
Introduction to GitHub Actions
Introduction to GitHub ActionsIntroduction to GitHub Actions
Introduction to GitHub Actions
 
LCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEELCU14-103: How to create and run Trusted Applications on OP-TEE
LCU14-103: How to create and run Trusted Applications on OP-TEE
 
LCU13: An Introduction to ARM Trusted Firmware
LCU13: An Introduction to ARM Trusted FirmwareLCU13: An Introduction to ARM Trusted Firmware
LCU13: An Introduction to ARM Trusted Firmware
 
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEEBKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
BKK16-110 A Gentle Introduction to Trusted Execution and OP-TEE
 
Secure storage updates - SFO17-309
Secure storage updates - SFO17-309Secure storage updates - SFO17-309
Secure storage updates - SFO17-309
 
Introduction to Makefile
Introduction to MakefileIntroduction to Makefile
Introduction to Makefile
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
 
Eclipse Iceoryx Overview
Eclipse Iceoryx OverviewEclipse Iceoryx Overview
Eclipse Iceoryx Overview
 
Runtime Symbol Resolution
Runtime Symbol ResolutionRuntime Symbol Resolution
Runtime Symbol Resolution
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
Hardware-assisted Isolated Execution Environment to run trusted OS and applic...
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
Diving into SWUpdate: adding new platform support in 30minutes with Yocto/OE !
 
2021.laravelconf.tw.slides1
2021.laravelconf.tw.slides12021.laravelconf.tw.slides1
2021.laravelconf.tw.slides1
 
LCA14: LCA14-418: Testing a secure framework
LCA14: LCA14-418: Testing a secure frameworkLCA14: LCA14-418: Testing a secure framework
LCA14: LCA14-418: Testing a secure framework
 

Viewers also liked

LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723Iftach Ian Amit
 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Varun Mahajan
 
Kernel Recipes 2016 - Kernel documentation: what we have and where it’s going
Kernel Recipes 2016 - Kernel documentation: what we have and where it’s goingKernel Recipes 2016 - Kernel documentation: what we have and where it’s going
Kernel Recipes 2016 - Kernel documentation: what we have and where it’s goingAnne Nicolas
 
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...Anne Nicolas
 
Introduction to spartakus and how it can help fight linux kernel ABI breakages
Introduction to spartakus and how it can help fight linux kernel ABI breakagesIntroduction to spartakus and how it can help fight linux kernel ABI breakages
Introduction to spartakus and how it can help fight linux kernel ABI breakagesSamikshan Bairagya
 
Abi capabilities
Abi capabilitiesAbi capabilities
Abi capabilitiesABI
 
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHackito Ergo Sum
 
Symbolic Debugging with DWARF
Symbolic Debugging with DWARFSymbolic Debugging with DWARF
Symbolic Debugging with DWARFSamy Bahra
 
Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADDharmalingam Ganesan
 
[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법
[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법
[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법GangSeok Lee
 
DWARF Data Representation
DWARF Data RepresentationDWARF Data Representation
DWARF Data RepresentationWang Hsiangkai
 
06 - ELF format, knowing your friend
06 - ELF format, knowing your friend06 - ELF format, knowing your friend
06 - ELF format, knowing your friendAlexandre Moneger
 
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...peknap
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic LinkingWang Hsiangkai
 
Linux binary analysis and exploitation
Linux binary analysis and exploitationLinux binary analysis and exploitation
Linux binary analysis and exploitationDharmalingam Ganesan
 
Compilation and Execution
Compilation and ExecutionCompilation and Execution
Compilation and ExecutionChong-Kuan Chen
 

Viewers also liked (20)

LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723
 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)
 
Ch 4 linker loader
Ch 4 linker loaderCh 4 linker loader
Ch 4 linker loader
 
Kernel Recipes 2016 - Kernel documentation: what we have and where it’s going
Kernel Recipes 2016 - Kernel documentation: what we have and where it’s goingKernel Recipes 2016 - Kernel documentation: what we have and where it’s going
Kernel Recipes 2016 - Kernel documentation: what we have and where it’s going
 
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
Kernel Recipes 2016 - Would an ABI changes visualization tool be useful to Li...
 
Introduction to spartakus and how it can help fight linux kernel ABI breakages
Introduction to spartakus and how it can help fight linux kernel ABI breakagesIntroduction to spartakus and how it can help fight linux kernel ABI breakages
Introduction to spartakus and how it can help fight linux kernel ABI breakages
 
Abi capabilities
Abi capabilitiesAbi capabilities
Abi capabilities
 
ELF 101
ELF 101ELF 101
ELF 101
 
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARFHES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
HES2011 - James Oakley and Sergey bratus-Exploiting-the-Hard-Working-DWARF
 
Intro reverse engineering
Intro reverse engineeringIntro reverse engineering
Intro reverse engineering
 
Symbolic Debugging with DWARF
Symbolic Debugging with DWARFSymbolic Debugging with DWARF
Symbolic Debugging with DWARF
 
Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOAD
 
[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법
[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법
[2008 CodeEngn Conference 02] graylynx - 정적 링크된 Stripped ELF 바이너리 상에서의 함수 탐지 기법
 
DWARF Data Representation
DWARF Data RepresentationDWARF Data Representation
DWARF Data Representation
 
06 - ELF format, knowing your friend
06 - ELF format, knowing your friend06 - ELF format, knowing your friend
06 - ELF format, knowing your friend
 
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
 
Linux binary analysis and exploitation
Linux binary analysis and exploitationLinux binary analysis and exploitation
Linux binary analysis and exploitation
 
Compilation and Execution
Compilation and ExecutionCompilation and Execution
Compilation and Execution
 
Sp chap2
Sp chap2Sp chap2
Sp chap2
 

Similar to FISL XIV - The ELF File Format and the Linux Loader

ELF(executable and linkable format)
ELF(executable and linkable format)ELF(executable and linkable format)
ELF(executable and linkable format)Seungha Son
 
C++ shared libraries and loading
C++ shared libraries and loadingC++ shared libraries and loading
C++ shared libraries and loadingRahul Jamwal
 
101 2.3 manage shared libraries
101 2.3 manage shared libraries101 2.3 manage shared libraries
101 2.3 manage shared librariesAcácio Oliveira
 
2.3 manage shared libraries
2.3 manage shared libraries2.3 manage shared libraries
2.3 manage shared librariesAcácio Oliveira
 
Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)HU-man
 
ApacheCon NA 2011 report
ApacheCon NA 2011 reportApacheCon NA 2011 report
ApacheCon NA 2011 reportKoji Kawamura
 
Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008guestd9065
 
Devfest kyoto2018 Lisp-Koans
Devfest kyoto2018 Lisp-KoansDevfest kyoto2018 Lisp-Koans
Devfest kyoto2018 Lisp-KoansTomoki Aburatani
 
Unix_commands_theory
Unix_commands_theoryUnix_commands_theory
Unix_commands_theoryNiti Patel
 
OS Lab Manual.pdf
OS Lab Manual.pdfOS Lab Manual.pdf
OS Lab Manual.pdfQucHunh15
 
(Very u seful) different file format
(Very u seful) different file format(Very u seful) different file format
(Very u seful) different file formatJitendra Chinchore
 
Shell tutorial
Shell tutorialShell tutorial
Shell tutorialVu Duy Tu
 
seL4 on RISC-V/lowRISC - ORCONF'15
seL4 on RISC-V/lowRISC - ORCONF'15seL4 on RISC-V/lowRISC - ORCONF'15
seL4 on RISC-V/lowRISC - ORCONF'15Hesham Almatary
 
linux-file-system01.ppt
linux-file-system01.pptlinux-file-system01.ppt
linux-file-system01.pptMeesanRaza
 
Low Level Exploits
Low Level ExploitsLow Level Exploits
Low Level Exploitshughpearse
 

Similar to FISL XIV - The ELF File Format and the Linux Loader (20)

ELF(executable and linkable format)
ELF(executable and linkable format)ELF(executable and linkable format)
ELF(executable and linkable format)
 
C++ shared libraries and loading
C++ shared libraries and loadingC++ shared libraries and loading
C++ shared libraries and loading
 
Linux
LinuxLinux
Linux
 
101 2.3 manage shared libraries
101 2.3 manage shared libraries101 2.3 manage shared libraries
101 2.3 manage shared libraries
 
2.3 manage shared libraries
2.3 manage shared libraries2.3 manage shared libraries
2.3 manage shared libraries
 
Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)Den-long-men of void-jar(hardware and Software)
Den-long-men of void-jar(hardware and Software)
 
ApacheCon NA 2011 report
ApacheCon NA 2011 reportApacheCon NA 2011 report
ApacheCon NA 2011 report
 
ELF
ELFELF
ELF
 
Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008Purdue CS354 Operating Systems 2008
Purdue CS354 Operating Systems 2008
 
Devfest kyoto2018 Lisp-Koans
Devfest kyoto2018 Lisp-KoansDevfest kyoto2018 Lisp-Koans
Devfest kyoto2018 Lisp-Koans
 
Unix_commands_theory
Unix_commands_theoryUnix_commands_theory
Unix_commands_theory
 
Edubooktraining
EdubooktrainingEdubooktraining
Edubooktraining
 
OS Lab Manual.pdf
OS Lab Manual.pdfOS Lab Manual.pdf
OS Lab Manual.pdf
 
(Very u seful) different file format
(Very u seful) different file format(Very u seful) different file format
(Very u seful) different file format
 
Operating system lab manual
Operating system lab manualOperating system lab manual
Operating system lab manual
 
Shell tutorial
Shell tutorialShell tutorial
Shell tutorial
 
Basic Make
Basic MakeBasic Make
Basic Make
 
seL4 on RISC-V/lowRISC - ORCONF'15
seL4 on RISC-V/lowRISC - ORCONF'15seL4 on RISC-V/lowRISC - ORCONF'15
seL4 on RISC-V/lowRISC - ORCONF'15
 
linux-file-system01.ppt
linux-file-system01.pptlinux-file-system01.ppt
linux-file-system01.ppt
 
Low Level Exploits
Low Level ExploitsLow Level Exploits
Low Level Exploits
 

Recently uploaded

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 

Recently uploaded (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

FISL XIV - The ELF File Format and the Linux Loader

  • 1. The ELF File Format and the Linux Loader Divino C´esar S. Lucas Universidade de Campinas - Instituto de Computac¸ ˜ao Laborat´orio de Sistemas de Computac¸ ˜ao (www.lsc.ic.unicamp.br) XIV F´orum Internacional de Software Livre 05 de Julho de 2013
  • 2. The ELF Format The Linux ELF Loader Questions Agenda 1 The ELF (Executable and Linkable Format) Format 2 The Linux ELF Loader 3 Questions Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 3. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments The Pieces of a Program gcc -c -O3 bob.c -o bob.o // relocatable files gcc -c -O3 main.c -o main.o gcc bob.o main.o -o hello // executable file gcc -shared -fPIC bob.c -o libhello.so // shared library gcc main.c -lhello -L. -o hello // dynamically linked executable Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 4. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments What the file looks inside? The Matrix? Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 5. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments What the file looks inside? The Matrix? Not. An ELF! Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 6. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments The Executable and Linkable Format (ELF) Specification for Executable and Linkable Files. Successor of a.out and COFF formats. Besides Linux, it’s also used in: FreeBSD, OpenBSD, Solaris, HP-UX, etc. Used in many devices: PlayStation 3, Dreamcast, Some Nokia Cellphones. Support for modern programming languages and code reutilization. Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 7. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments The ELF Header The ELF Header typedef struct elf32 hdr { unsigned char e ident [ EI NIDENT ] ; Elf32 Half e type ; Elf32 Half e machine ; Elf32 Word e version ; Elf32 Addr e entry ; Elf32 Off e phoff ; Elf32 Off e shoff ; Elf32 Word e flags ; Elf32 Half e ehsize ; Elf32 Half e phentsize ; Elf32 Half e phnum ; Elf32 Half e shentsize ; Elf32 Half e shnum ; Elf32 Half e shstrndx ; } Elf32 Ehdr ; Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 8. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments The ELF Header - Example The ELF Header $ readelf -h executavel ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2’s complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x400410 Start of program headers: 64 (bytes into file) Start of section headers: 4440 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 9 Size of section headers: 64 (bytes) Number of section headers: 30 Section header string table index: 27 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 9. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments Program Representation Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 10. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments ELF Sections Mainly used during static linking process. Used to group together program portions with are sematically related (eg: data / code / constructors / destructors). Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 11. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments ELF Sections - Example ELF Header Sections Table . . . .text .data .bss . . . Allice.o ELF Header Sections Table Segments Table . . . .text .text .data .data .bss .bss . . . Executable ELF Header Sections Table . . . .text .data .bss . . . Bob.o Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 12. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments ELF Sections - Example Section Table $ readelf -WS executavel [Nr] Name Type Address Off Size ES Flg Lk Inf Al [ 1] .interp PROGBITS 0000000000400238 000238 00001c 00 A 0 0 1 [11] .init PROGBITS 00000000004003c8 0003c8 000018 00 AX 0 0 4 [12] .plt PROGBITS 00000000004003e0 0003e0 000030 10 AX 0 0 16 [13] .text PROGBITS 0000000000400410 000410 000228 00 AX 0 0 16 [15] .rodata PROGBITS 0000000000400648 000648 000031 00 A 0 0 4 [22] .got PROGBITS 0000000000600fe0 000fe0 000008 08 WA 0 0 8 [24] .data PROGBITS 0000000000601010 001010 000020 00 WA 0 0 8 [25] .bss NOBITS 0000000000601030 001030 000010 00 WA 0 0 8 [28] .symtab SYMTAB 0000000000000000 0018d8 000690 18 29 48 8 [29] .strtab STRTAB 0000000000000000 001f68 00022b 00 0 0 1 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 13. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments ELF Segments Used mainly during process creation (by the dynamic loader). Used to group together sections with the same flags (all text, all data. Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 14. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments ELF Segments - Example Program Headers $ readelf -Wl executavel Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align PHDR 0x000040 0x0000000000400040 0x0000000000400040 0x0001f8 0x0001f8 R E 0x8 INTERP 0x000238 0x0000000000400238 0x0000000000400238 0x00001c 0x00001c R 0x1 LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x00079c 0x00079c R E 0x200000 LOAD 0x000e28 0x0000000000600e28 0x0000000000600e28 0x000208 0x000218 RW 0x200000 DYNAMIC 0x000e50 0x0000000000600e50 0x0000000000600e50 0x000190 0x000190 RW 0x8 NOTE 0x000254 0x0000000000400254 0x0000000000400254 0x000044 0x000044 R 0x4 GNU_EH_FRAME 0x00067c 0x000000000040067c 0x000000000040067c 0x00003c 0x00003c R 0x4 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x8 GNU_RELRO 0x000e28 0x0000000000600e28 0x0000000000600e28 0x0001d8 0x0001d8 R 0x1 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 15. The ELF Format The Linux ELF Loader Questions Relocatable, Shared Libraries and Executable Files What / Why ELFs? The ELF Header Sections Segments ELF Segments - Sections to Segments Mapping from Sections to Segments $ readelf -Wl executavel 00 01 .interp 02 .interp .gnu.hash .dynsym .dynstr .rela.dyn .rela.plt .init .plt .text .fini .rodata 03 .ctors .dtors .jcr .dynamic .got .got.plt .data .bss 04 .dynamic 05 .note.ABI-tag .note.gnu.build-id 06 .eh_frame_hdr 07 08 .ctors .dtors .jcr .dynamic .got Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 16. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. How a Process Start? Function File Comment Shell/GUI user application Executes fork() to create a news process and execve() to replace the child. execve fs/exec.c Do some preprocessing stuff and calls search binary handler. search binary handler fs/exec.c Identify which type is the binary and calls the appropriate handler. load elf binary fs/binfmt elf.c Validate the binary file and do some preprocessing. start thread arch/x86/kernel/process.c Start a new thread of execution. Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 17. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Loader - workflow 1 Load the main binary. 2 Load the main binary dependencies (shared libraries). 3 Create a symbol resolution map. 4 Apply data relocations (fill the GOT table). 5 Apply function relocations (fill the GOT.PLT table). 6 Call libraries initializers, registry finalizers and start the program. Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 18. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Loading the main binary Loadable Segments $ readelf -Wl executavel Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align PHDR 0x000040 0x0000000000400040 0x0000000000400040 0x0001f8 0x0001f8 R E 0x8 INTERP 0x000238 0x0000000000400238 0x0000000000400238 0x00001c 0x00001c R 0x1 **** LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x00079c 0x00079c R E 0x200000 **** LOAD 0x000e28 0x0000000000600e28 0x0000000000600e28 0x000208 0x000218 RW 0x200000 **** DYNAMIC 0x000e50 0x0000000000600e50 0x0000000000600e50 0x000190 0x000190 RW 0x8 NOTE 0x000254 0x0000000000400254 0x0000000000400254 0x000044 0x000044 R 0x4 GNU_EH_FRAME 0x00067c 0x000000000040067c 0x000000000040067c 0x00003c 0x00003c R 0x4 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x8 GNU_RELRO 0x000e28 0x0000000000600e28 0x0000000000600e28 0x0001d8 0x0001d8 R 0x1 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 19. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. The Dynamic Section/Segment Dynamic Information Block $ readelf -dW executavel Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] 0x000000000000000c (INIT) 0x4003c8 0x000000000000000d (FINI) 0x400638 0x000000006ffffef5 (GNU_HASH) 0x400298 0x0000000000000005 (STRTAB) 0x400318 0x0000000000000006 (SYMTAB) 0x4002b8 0x000000000000000a (STRSZ) 63 (bytes) 0x000000000000000b (SYMENT) 24 (bytes) 0x0000000000000003 (PLTGOT) 0x600fe8 0x0000000000000002 (PLTRELSZ) 48 (bytes) 0x0000000000000014 (PLTREL) RELA 0x0000000000000017 (JMPREL) 0x400398 0x0000000000000007 (RELA) 0x400380 0x0000000000000008 (RELASZ) 24 (bytes) 0x0000000000000009 (RELAENT) 24 (bytes) Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 20. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Dependences from my dependences... Executable libz.so liby.so ... libc.so libb.so liba.so libb.so libe.so libd.so libc.so liby.so libg.so libf.so libe.so Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 21. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Shared Libraries: ldd and ldconfig ldconfig to find dynamic dependencies $ ldconfig -p ... libBrokenLocale.so.1 (libc6, OS ABI: Linux 2.6.24) => /lib/i386-linux-gnu/libBrokenLocale.so.1 ld-linux-x86-64.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 libQtXml.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtXml.so.4 libQtWebKit.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtWebKit.so.4 libQtSvg.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtSvg.so.4 libQtSql.so.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libQtSql.so.4 ... Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 22. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Process Address Space Executable (.text) Executable (.data) Executable ( .bss) HEAP Library-A (.text) Library-A (.data) Library-A ( .bss) Library-B (.text) Library-B (.data) Library-B ( .bss) STACK Lower Addresses Higher Addresses Process Address Space Fixed distance Fixed distance Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 23. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Symbol resolution scope and ordering Executable libz.so liby.so ... libc.so libb.so liba.so libb.so libe.so libd.so libc.so liby.so libg.so libf.so libe.so Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 24. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Relocations Relocation is the process of connecting symbolic references with symbolic definitions. There are data relocations and function relocations. Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 25. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Relocations - Why do we need relocations (Part 1)? gcc -c -O3 bob.c -o bob.o // relocatable files gcc -c -O3 main.c -o main.o gcc bob.o main.o -o hello // executable file gcc -shared -fPIC bob.c -o libhello.so // shared library gcc main.c -lhello -L. -o hello // dynamically linked executable Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 26. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Relocations - Why do we need relocations (Part 1)? gcc -c -O3 bob.c -o bob.o // relocatable files gcc -c -O3 main.c -o main.o gcc bob.o main.o -o hello // executable file gcc -shared -fPIC bob.c -o libhello.so // shared library gcc main.c -lhello -L. -o hello // dynamically linked executable Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 27. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Relocations - Why do we need relocations (Part 2)? Executable (.text) Executable (.data) Executable ( .bss) HEAP Library-A (.text) Library-A (.data) Library-A ( .bss) Library-B (.text) Library-B (.data) Library-B ( .bss) STACK Process A . . . . . . Library-A (.text) Library-A (.data) Library-A ( .bss) Library-A (.data) Library-A ( .bss) Library-B (.text) Library-B (.data) Library-B ( .bss) Library-C (.text) Library-C (.data) Library-C ( .bss) . . . . . . Physical Memory Executable (.text) Executable (.data) Executable ( .bss) HEAP Library-A (.text) Library-A (.data) Library-A ( .bss) Library-C (.text) Library-C (.data) Library-C ( .bss) STACK Process B Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 28. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Relocations - Where are them? Data and function relocations $ readelf -rW executavel Relocation section ’.rela.dyn’ at offset 0x380 contains 1 entries: Offset Info Type Symbol’s Value Symbol’s Name + Addend 0000000000600fe0 0000000300000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0 Relocation section ’.rela.plt’ at offset 0x398 contains 2 entries: Offset Info Type Symbol’s Value Symbol’s Name + Addend 0000000000601000 0000000100000007 R_X86_64_JUMP_SLOT 0000000000000000 printf + 0 0000000000601008 0000000200000007 R_X86_64_JUMP_SLOT 0000000000000000 __libc_start_main + 0 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 29. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Dynamic Symbol Table (dynsym) $ readelf -Ws libtest.so Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000498 0 SECTION LOCAL DEFAULT 9 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__ 3: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND foo 4: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses 5: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5 (2) 6: 0000000000201018 0 NOTYPE GLOBAL DEFAULT ABS _edata 7: 0000000000201028 0 NOTYPE GLOBAL DEFAULT ABS _end 8: 0000000000201018 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 9: 00000000000005ac 11 FUNC GLOBAL DEFAULT 11 function 10: 0000000000000498 0 FUNC GLOBAL DEFAULT 9 _init 11: 00000000000005f8 0 FUNC GLOBAL DEFAULT 12 _fini Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 30. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Dynamic String Table (dynstr) $ readelf -p .dynstr executavel String dump of section ’.dynstr’: [ 1] libhello.so [ d] __gmon_start__ [ 1c] _Jv_RegisterClasses [ 30] bob_speak [ 3a] _init [ 40] _fini [ 46] libc.so.6 [ 50] __libc_start_main [ 62] _edata [ 69] __bss_start [ 75] _end [ 7a] GLIBC_2.2.5 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 31. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Global Offset Table gcc -shared -fPIC hello.c -o libhello.so #include <stdio . h> int valor1 ; int fun1 ( void ) { valor1 = 123; p r i n t f ( ” Valor de valor1 = %dn ” , valor1 ) ; return 0; } Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 32. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Global Offset Table objdump -d libhello.so 5f0 : mov 0x2009d9(% r i p ),% rax # 200fd0 < DYNAMIC+0x188> 5f7 : movl $0x7b ,(% rax ) Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 33. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Global Offset Table readelf -WS libhello.so [20] .got PROGBITS 0000000000200 fc8 000fc8 000020 08 WA 0 0 8 [21] . g o t . p l t PROGBITS 0000000000200fe8 000fe8 000028 08 WA 0 0 8 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 34. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Global Offset Table readelf -Wr libhello.so Relocation section ’.rela.dyn’ at offset 0x428 contains 5 entries: Offset Info Type Symbol’s Value Symbol’s Name + Addend ... 0000000000200fd0 0000000c00000006 R_X86_64_GLOB_DAT 0000000000201028 valor1 + 0 ... Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 35. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Procedure Linkage Table objdump -d exec 4005 fd : c a l l q 400500 <fun1@plt> 400500 <fun1@plt >: 400500: jmpq ∗0x200b02(% r i p ) # 601008 < GLOBAL OFFSET TABLE +0x20> 400506: pushq $0x1 40050b : jmpq 4004e0 < i n i t +0x20> 4004e0 < l i b c s t a r t m a i n @ p l t −0x10 >: 4004e0 : pushq 0x200b0a(% r i p ) # 600 f f 0 < GLOBAL OFFSET TABLE +0x8> 4004e6 : jmpq ∗0x200b0c(% r i p ) # 600 f f 8 < GLOBAL OFFSET TABLE +0x10> 4004ec : nopl 0x0(%rax ) Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 36. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Procedure Linkage Table readelf -Wr exec Offset Info Type Symbol’s Value Symbol’s Name + Addend 0000000000601000 0000000100000007 R_X86_64_JUMP_SLOT 0000000000000000 __libc_start_main + 0 0000000000601008 0000000300000007 R_X86_64_JUMP_SLOT 0000000000000000 fun1 + 0 Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 37. The ELF Format The Linux ELF Loader Questions 1) Load the main binary 2) Load the main binary dependencies 3) Create a symbol resolution map 4 and 5) Apply data and function relocations 6) Initialize libraries and jump to program start. Program Initialization Flow Divino C´esar S. Lucas The ELF File Format and the Linux Loader
  • 38. The ELF Format The Linux ELF Loader Questions Thank you! Questions? divcesar [at] gmail [dot] com http://johntortugo.wordpress.com Divino C´esar S. Lucas The ELF File Format and the Linux Loader