Lec03 Linker Loader

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Lecture 03: Linking and Loading

of an ELF File
Vivek Kumar
Computer Science and Engineering
IIIT Delhi
vivekk@iiitd.ac.in
CSE231: Operating Systems
Lecture 02: Linking and Loading of an ELF file

Last Lecture

● Compilation phases
● Static and dynamic linking
● Object files
o Relocatable and executable files
CSE231: Operating Systems © Vivek Kumar 1
Lecture 02: Linking and Loading of an ELF file

Today’s Class
● Revisiting Executable File Format (ELF)
● Dissecting the sections of an ELF
o Symbol resolution
● Working of loader

CSE231: Operating Systems © Vivek Kumar 2


Lecture 02: Linking and Loading of an ELF file

Class Quiz (1/5)


● Match the following structures with their respective names
and responsibilities

(A) Elf32_Phdr (X) ELF header (1) Information for Loader

(B) Elf32_Ehdr (Y) ELF Section Header (2) Stores roadmap of ELF

(C) Elf32_Shdr (Z) ELF Program Header (3) Information for Linker

CSE231: Operating Systems © Vivek Kumar 3


Lecture 02: Linking and Loading of an ELF file

Class Quiz (2/5)


● What is the meaning of “entrypoint” address?
1. Offset of SHDR table from the beginning of ELF file
2. Offset of PHDR table from the beginning of ELF file
3. Address of “main” method
4. Address of “_start” method

● Where it is stored inside the ELF binaries?


1. Program header table
2. Section header table
3. ELF header
4. Segments
5. Sections

CSE231: Operating Systems © Vivek Kumar 4


Lecture 02: Linking and Loading of an ELF file

Class Quiz (3/5)


● Which of these are required at the compile time and
runtime, respectively?
1. ELF header
2. PHDR
3. SHDR
4. Segments
5. Sections

CSE231: Operating Systems © Vivek Kumar 5


Lecture 02: Linking and Loading of an ELF file

Class Quiz (4/5)


● Which of these sections appear in “executable” file and
which ones in “relocatable” file?
1. .bss
2. .dynsym
3. .shstrtab
4. .data
5. .text
6. .dynstr
7. .symtab
8. .strtab

CSE231: Operating Systems © Vivek Kumar 6


Lecture 02: Linking and Loading of an ELF file

Class Quiz (5/5)


● Match the location shown with their respective section
name inside the ELF file
1. .bss
A B
2. .strtab
C D
3. .rodata
Symbol
E
name
only in
4. .text
this case
F 5. .data

CSE231: Operating Systems © Vivek Kumar 7


Lecture 02: Linking and Loading of an ELF file

Today’s Class
● Revisiting Executable File Format (ELF)
● Dissecting the sections of an ELF
o Symbol resolution
● Working of loader

CSE231: Operating Systems © Vivek Kumar 8


Lecture 02: Linking and Loading of an ELF file

Portability with ELF


● Recall, one of the challenges of OS is to support portability
● Machine code is architecture dependent
● Having an architecture independent layout of object files
helps in achieving portability
o The interpreter (loader + dynamic linker) becomes portable, and
could have same set of steps to load executables across different
architectures (portability)
§ Easier to change and test the tools used for manipulating object files

CSE231: Operating Systems © Vivek Kumar 9


Lecture 02: Linking and Loading of an ELF file

Example: Reading ”.shstrtab” in Plain C


1. Open the a.out in O_RDONLY
mode
2. Read the value of the following
member in the Elf32_Ehdr
a. e_shoff
b. e_shentsize (fixed)
Relocatable file fib.o

c. e_shstrndx
3. Move to the offset e_shoff +
e_shstrndx* e_shentsize
a. Starting address of Elf32_Shdr for
“.shstrtab”
4. Read the value of the following
.shstrtab members in the Elf32_Shdr
a. sh_offset
b. sh_size
5. Move to the offset sh_offset from
the start of the file a.out
a. Content of the “.shstrtab” starts
from this address
b. Total content size “sh_size” bytes

CSE231: Operating Systems © Vivek Kumar 10


Lecture 02: Linking and Loading of an ELF file

Example: Reading Section Names in Plain C


6. Iterate through all
entries in the SHDR
one by one
o SHDR array starts at
e_shoff
Relocatable file fib.o

7. Find the value of


sh_name in the SHDR
.text entry
.bss 8. Name of this section
.shstrtab will be inside the
content of the section
”.shstrtab and starting
at the location
sh_name
o printf (%s) at that
location prints the
name
CSE231: Operating Systems © Vivek Kumar 11
Lecture 02: Linking and Loading of an ELF file

Linker: Merges the Sections


A.o
.text
.text ● Relocation is the
.data process of merging the
sections and resolving
.bss .data
the symbol addresses
.rodata

.bss
.text

.data

.bss .rodata

.rodata
C.out
B.o
CSE231: Operating Systems © Vivek Kumar 12
Lecture 02: Linking and Loading of an ELF file

Linker: Relocation Resolve symbols by linking


fib.o fib.o and call-fib.o together
SYM Name Type Location
get_number External NULL

fib Internal Addr_A


a.out
SYM Name Location
compute Internal Addr_B
get_number Addr_D
Compilation
call-fib.o fib Addr_E

SYM Name Type Location compute Addr_F

compute External NULL

get_number Internal Addr_C

● Relocatable files contains section headers of type SHT_REL indicating the presence of relocation sections
● Relocation section contains an array of relocation entries, where each entry corresponds to a relocatable symbol
in the object file
● Each relocation entry contains information about the name of the symbol (index into the string table section),
type of relocation (internal or external), and position (address) of the symbol in the current file

CSE231: Operating Systems © Vivek Kumar 13


Lecture 02: Linking and Loading of an ELF file

Today’s Class
● Revisiting Executable File Format (ELF)
● Dissecting the sections of an ELF
o Symbol resolution
● Working of loader

CSE231: Operating Systems © Vivek Kumar 14


Lecture 02: Linking and Loading of an ELF file

Loader
● When an executable file is run, the operating system loads
it into memory and start running it
● Loader uses the information stored inside the Program
Header Table (PHT)
o It doesn’t require information stored in the Section Header Table
(SHDR)
o Linker combines multiple sections of similar type to create one
segment
o There are multiple segments in an executable file, and for each
segment, there is one entry in the PHDR
o Several sections can be removed from the executable without
affecting program execution
CSE231: Operating Systems © Vivek Kumar 15
Lecture 02: Linking and Loading of an ELF file

Example: Loading Executable in Plain C


Why ??

Fix: rename
“main” to _start”

● For simplicity, a) assume there are no APIs from the GNU C


library, b) 32-bit compilation, and c) no use of global variables
● The loader has to simply iterate through the PHDR and identify
the segment of PT_LOAD type
● Load this segment in memory and jump to [where?] for starting
the execution e_entrypoint
address in EHDR
CSE231: Operating Systems © Vivek Kumar 16
Lecture 02: Linking and Loading of an ELF file

Example: Loading Executable in Plain C

● Array of PHDR with total ”e_phnum” number of elements starts at ”e_phoff” bytes from the start of
the file a.out
● Load the first segment whose ”p_type” is “PT_LOAD” in memory. This segment starts at “p_offset”
● Move to the virtual address ”e_entrypoint” inside this segment
● Typecast it to the ”_start” function pointer type and simply execute!
CSE231: Operating Systems © Vivek Kumar 17
Lecture 02: Linking and Loading of an ELF file

Loader in OS: Complete Steps


● When an executable file is run, the operating system loads it
into memory and start it running as follows
1. OS kernel reads the PHT and loads the parts specified in the
segments of type “INTERP” and “LOAD”
2. Control is transferred to the interpreter (/lib/ld-library.so)
3. Dynamic Linker (DL) is invoked
4. DL load the shared libraries needed by the executable and carry out
the relocations
§ This information is available in “.dynamic” section
§ Connects symbolic references with their corresponding symbolic
definitions (e.g., connecting the call to printf with the actual
implementation of the printf in GNU C library)
5. DL transfer the control to the address given by the symbol “_start”
(and eventually to user main)

CSE231: Operating Systems © Vivek Kumar 18


Lecture 02: Linking and Loading of an ELF file

CSE231: Operating Systems © Vivek Kumar 19


Lecture 02: Linking and Loading of an ELF file

Reference Materials
● Executable and Linkable Format
o https://man7.org/linux/man-pages/man5/elf.5.html
o https://www.sco.com/developers/devspecs/gabi41.pdf
o https://wiki.osdev.org/ELF_Tutorial

CSE231: Operating Systems © Vivek Kumar 20

You might also like