Linking
ELF Overview
- Review of 15-elf-intro
- ELF Executable Layout
Ehdr
e_shoff
e_shstrndx
- HW 5 Part 1:
gen-exe
Phdr
table
- Segment 0 contains
Ehdr
& Phdr
table
- A segment contains one or more sections
readelf -a ./main
(in code/13) shows .data
&
.bss
sections in the same segment
.symtab
& .strtab
st_name
st_value
st_shndx
Shdr
table & .shstrtab
sh_name
`
sh_size
sh_offset
Linking an Assembler-Generated Object File
hello
program (code/16/hello)
- code walkthrough
- ld-generated executable:
readelf -a hello
- cld-generated executable:
readelf -a a.out
- compare final cld linker to ld: pretty much the same
- compare final cld linker to part 2 cld: missing strings & symbols
- Alignment
- Segments start addresses must be aligned to page boundary
- cld aligns
.symtab
start offset to page boundary for simplicity
Linking a gcc-Generated Object File
my_libc
library
- Linking GNU C stdlib (glibc) is very complex (regardless of static vs.
dynamic linking)
- We provide our own minimal C library that contains:
- Startup routine that calls
main()
write()
& exit()
syscalls built on top of our own syscall()
wrapper
echo1
program walkthrough (code/16/echo1)
Single Object File Relocation
readelf -a echo.o
- relocation entries
Elf64_Rela
objdump -d echo.o
- Inspect placeholder call instruction to
my_strlen()
- Calculate PC-relative encoding for operand to call instruction
- Verify relocation result by comparing with
objdump -d echo
to see
completed relocation
Linking Multiple Object Files
Each object file will have a mixture of symbol definitions and external symbol
references in its symbol table. External symbol references refer to symbols
define in some other file and appear as undefined symbols in the referencer’s
symbol table. The linker’s job is to merge the input .text
sections and
symbols into a single executable. Here’s an outline of the steps to be
performed:
- Concatenate the input
.text
sections into a single output .text
section
- Collect all symbols from multiple input symbol tables and generate an output
symbol table, the union of the input symbol tables
- Create a mapping from input symbols to output symbols to use for relocation
later. Multiple references to the same symbol should map to the same output
symbol.
- Linker emits error if there are multiple definitions of the same symbol
- Linker emits error if there are undefined symbols after processing all
input object files
- Generate a new output string table for the output symbol table
- Adjust the following fields in the output symbol:
st_name
: Refer to the newly generated string table
st_value
: Refer to the memory address of the symbol in the output
.text
section, which is the concatenation of all the input .text
sections
Last updated: 2024-04-03