Download Understanding Linking in C Programming: Correlating Assembly and Source Code and more Slides Computer System Design and Architecture in PDF only on Docsity!
Linking
x86-64 Assembly
Brief overview last time
Lecture notes available on the course web page
and x86-64 assembly overview in the textbook
You will not write any x86-64 assembly
Need to be able to recognize/understand code
fragments
Need to be able to correlate assembly code to
source code
More assembly examples today
Example Program (2 .c files)
/* main.c */ void swap(void); int buf[2] = {1, 2};
int main(void) { swap(); return (0); }
/* swap.c */ extern int buf[];
int *bufp0 = &buf[0]; int *bufp1;
void swap(void) { int temp;
bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; }
An Analogy for Linking
Compilation
main.c swap.c
ld (collect2)
p
ELF Format Files
Linking step
main.o
as
swap.o
as
Relocatable
object code
Executable
C source code
cc
main.s
cc
swap.s Assembly code
UNIX% gcc -O – g -o p main.c swap.c
Compilation
UNIX% gcc -v -O -g -o p main.c swap.c
cc1 -quiet -v main.c -quiet -dumpbase main.c -mtune=generic
-auxbase main -g -O -version -o /tmp/cchnheja.s
as -V -Qy -o /tmp/ccmNFRZd.o /tmp/cchnheja.s
cc1 -quiet -v swap.c -quiet -dumpbase swap.c -mtune=generic
-auxbase swap -g -O -version -o /tmp/cchnheja.s
as -V -Qy -o /tmp/ccx8FECg.o /tmp/ccheheja.s
collect2 --eh-frame-hdr – m elf_x86_64 --hash-style=gnu -dynamic-
linker /lib64/ld-linux-x86-64.so.2 -o p crt1.o crti.o
crtbegin.o – L <..snip..> /tmp/ccmNFRZd.o /tmp/ccx8FECg.o – lgcc
--as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed
-lgcc_s --no-as-needed crtend.o crtn.o
Assembler: .s assembly code to .o relocatable object code Compiler: .c C source code to .s assembly code
Linker: .o to executable
ELF Header
Basic description of file
contents:
File format identifier
Endianness
Alignment requirement
for other sections
Location of other
sections
Code’s starting address
ELF header
Program header table .text
.data .bss .symtab .rel.text .rel.data .debug
Section header table
.rodata
Program and Section Headers
Info about other sections
necessary for loading
Required for
executables & libraries
Info about other sections
necessary for linking
Required for
relocatables
ELF header
Program header table .text
.data .bss .symtab .rel.text .rel.data .debug
Section header table
.rodata
Data Sections
Static data
initialized, read-only
initialized, read/write
uninitialized, read/write
(BSS = “Block Started by
Symbol” pseudo-op for
IBM 704)
Initialized
Initial values in ELF file
Uninitialized
Only total size in ELF file
Writable distinction enforced
at run-time
Why? Protection; sharing
How? Virtual memory
ELF header
Program header table .text
.data .bss .symtab .rel.text .rel.data .debug
Section header table
.rodata
Relocation Information
Describes where and how
symbols are used
A list of locations in the
.text section that will
need to be modified
when the linker
combines this object file
with others
Relocation information
for any global variables
that are referenced or
defined by the module
Allows object files to be
easily relocated
ELF header
Program header table .text
.data .bss .symtab .rel.text .rel.data .debug
Section header table
.rodata
Other Sections
Other kinds of sections
also supported, including:
Other debugging info
Version control info
Dynamic linking info
C++ initializing &
finalizing code
ELF header
Program header table .text
.data .bss .symtab .rel.text .rel.data .debug
Section header table
.rodata
Symbol Table
Describes where global
variables and functions
are defined
Present in all relocatable
ELF files
ELF header
Program header table .text
.data .bss .symtab .rel.text .rel.data .debug
Section header table
.rodata
/* main.c */ void swap(void); int buf[2] = {1, 2};
int main(void) { swap(); return (0); }
Linker Symbols
/* main.c */ void swap(void); int buf[2] = {1, 2};
int main(void) { swap(); return (0); }
/* swap.c */ extern int buf[];
int *bufp0 = &buf[0]; int *bufp1;
void swap(void) { int temp;
bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; }
Definition of global symbols buf and main
Reference to external symbol swap
Definition of global symbol swap
Definition of global symbols bufp0 and bufp (even though not used outside file)
Reference to external symbol buf Linker knows nothing about local variables
Linker Symbols
What’s missing?
swap – where is it?
/* main.c */ void swap(void); int buf[2] = {1, 2};
int main(void) { swap(); return (0); }
UNIX% gcc -O -c main.c UNIX% readelf -s main.o
Symbol table '.symtab' contains 11 entries: Num: Value Size Type Bind Vis Ndx Name … 8: 0000000000000000 19 FUNC GLOBAL DEFAULT 1 main 9: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND swap 10: 0000000000000000 8 OBJECT GLOBAL DEFAULT 3 buf
main is a 19-byte function located at
offset 0 of section 1 (.text)
swap is referenced in this file, but is
undefined (UND)
buf is an 8-byte object located at offset 0
of section 3 (.data)
use readelf – s to see sections