Professional Documents
Culture Documents
Lec03 Linker Loader
Lec03 Linker Loader
Lec03 Linker Loader
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
(B) Elf32_Ehdr (Y) ELF Section Header (2) Stores roadmap of ELF
(C) Elf32_Shdr (Z) ELF Program Header (3) Information for Linker
Today’s Class
● Revisiting Executable File Format (ELF)
● Dissecting the sections of an ELF
o Symbol resolution
● Working of loader
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
.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
● 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
Today’s Class
● Revisiting Executable File Format (ELF)
● Dissecting the sections of an ELF
o Symbol resolution
● Working of loader
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
Fix: rename
“main” to _start”
● 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
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