Download presentation
Presentation is loading. Please wait.
1
Program Execution in Linux
David Ferry, Chris Gill, Brian Kocoloski CSE 422S - Operating Systems Organization Washington University in St. Louis St. Louis, MO 63143
2
Creating an Executable File
//Source code #include <stdio.h> int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Compiler Relocatable Object file: D foo T main U puts Linker Executable file Two stages: Compilation Linking The compiler translates source code to machine code. The linker connects binary files to libraries to create an executable. CSE 422S – Operating Systems Organization
3
Static vs. Dynamic Linking
Static linking – required code and data is copied into executable at compile time Dynamic linking – required code and data is linked to executable at runtime my_program.o Static: Dynamic: Program code my_program.o libc.so Program code Program Data Library Code/Data Program Data Library Code/Data CSE 422S – Operating Systems Organization
4
CSE 422S – Operating Systems Organization
Static Linking With multiple programs, what happens to the size of the executables? Program code Program Data Library Code CSE 422S – Operating Systems Organization
5
CSE 422S – Operating Systems Organization
Static Linking With multiple programs, what happens to the size of the executables? Program code Program code Program code Program code Program Data Program Data Program Data Program Data Library Code Library Code Library Code Library Code Program code Program code Program code Program Data Program Data Program Data Library Code Library Code Library Code CSE 422S – Operating Systems Organization
6
CSE 422S – Operating Systems Organization
Dynamic Linking With multiple programs, what happens to the size of the executables? Program code Program code Program code Program code Program Data Program Data Program Data Program Data Library Code Program code Program code Program code Program Data Program Data Program Data CSE 422S – Operating Systems Organization
7
Running a Statically Linked Program
All functions and data needed by the process space are linked as the last step of the compiler Only that code/data needed by the program are loaded into virtual memory Stack Static library A code/data Static library B code/data Heap .bss .data .text CSE 422S – Operating Systems Organization
8
Running a Statically Linked Program
A statically linked program is entirely self-contained User forks() an existing process to get a new process space execve() reads program into memory Starts executing at _start() in the C runtime, which sets up environment C runtime eventually calls main() After main returns, C runtime does some cleanup CSE 422S – Operating Systems Organization
9
Running a Dynamically Linked Program
Some functions and data do not exist in process space at runtime The dynamic linker (called ld) maps these into the memory map segment on-demand Stack Memory Map Segment (s) (added at runtime by linker) Heap .bss .data .text CSE 422S – Operating Systems Organization
10
CSE 422S – Operating Systems Organization
Linking at Runtime At compile time: The linker (ld) is embedded in program Addresses of dynamic functions are replaced with calls to the linker At runtime the linker does lazy-binding: Program runs as normal until it encounters an unresolved function Program jumps to linker Linker maps shared library into address space and replaces the unresolved address with the resolved address CSE 422S – Operating Systems Organization
11
CSE 422S – Operating Systems Organization
Linking at Runtime Procedure Linkage Table (PLT) Used by dynamic linker to resolve locations of library functions All function calls to shared libraries are replaced by stub functions that query the PLT at runtime for the address of the function Global Offset Table (GOT) Used by dynamic linker to resolve locations of library data All references to variables in shared libraries are replaced with references to the GOT CSE 422S – Operating Systems Organization
12
Runtime Linker Implementation
Uses a procedure link table (PLT) to do lazy binding Stack //Source code #include <stdio.h> int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Heap .bss Procedure Link Table (PLT) .data linker_stub() .text CSE 422S – Operating Systems Organization
13
Runtime Linker Implementation
Uses a procedure link table (PLT) to do lazy binding Stack //Source code #include <stdio.h> int foo = 20; int main( int argc, char* argv[]){ printf(“Hello, world!\n”); return 0; } Library with printf() function Heap .bss Procedure Link Table (PLT) .data library printf() .text CSE 422S – Operating Systems Organization
14
CSE 422S – Operating Systems Organization
Example Uses the following tools: objdump nm strace gdb You aren’t responsible for following all of the details of this example – it’s just to reinforce the concepts behind the PLT and GOT CSE 422S – Operating Systems Organization
15
Static vs. Dynamic Linking Tradeoffs
Does not need to look up libraries at runtime Does not need extra PLT indirection Consumes more memory with copies of each library in every program Dynamic: Less disk space/memory (7K vs 571K for hello world) Shared libraries already in memory and in hot cache Incurs lookup and indirection overheads CSE 422S – Operating Systems Organization
16
CSE 422S – Operating Systems Organization
Binary File Utilities nm – prints symbol table objdump – prints all binary data readelf – prints ELF data pmap – prints memory map of a running process ldd – prints dynamic library dependencies of a binary strip – strips symbol data from a binary CSE 422S – Operating Systems Organization
17
Executable File Format
The current binary file format is called ELF - Executable and Linking Format First part of file is the ELF Header, which defines contents of the rest of the file Segments contain data & code needed at runtime Sections contain linking & relocation data Adds additional segments past .text, .data, etc.: .rodata – read-only data .debug – debugging symbol table and more… GCC adds it’s own sections… CSE 422S – Operating Systems Organization
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.