1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.

Slides:



Advertisements
Similar presentations
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Advertisements

Fabián E. Bustamante, Spring 2007 Linking Today Static linking Object files Static & dynamically linked libraries Next time Exceptional control flows.
Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
Linker and Loader. Program building into four stages (C Program) Preprocessing (Preprocessor) It processes include files, conditional compilation instructions.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES Credit: Mostly Bryant & O’Hallaron.
Winter 2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University Linking.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
“The course that gives CMU its Zip!”
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
CS 330 What software engineers need to know about linking and a few things about execution.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Linking Topics Static linking Object files Static libraries Loading Dynamic linking of shared libraries CS213.
Carnegie Mellon 1 Linking Lecture, Apr. 11, 2013 These slides are from website which accompanies the book “Computer Systems: A.
CS 3204 Operating Systems Godmar Back Lecture 11.
Lecture-1 Compilation process
CS 367 Linking Topics Static linking Object files Static libraries
Linking February 20, 2001 Topics static linking object files static libraries loading dynamic linking of shared libraries class16.ppt “The course.
Computer System Chapter 7. Linking Lynn Choi Korea University.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Linking Topics Static linking Object files Static libraries Loading.
Linking Topics Static linking Dynamic linking Case study: Library interpositioning.
Lec 4Systems Architecture1 Systems Architecture Lecture 4: Compilers, Assemblers, Linkers & Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
Linking Ⅱ.
Linking October 5, 2002 Topics static linking object files static libraries loading dynamic linking of shared libraries Reading: Chapter 7 Problems: 7.8.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Linking Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Chapter 13 : Symbol Management in Linking
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7.
CSc 453 Linking and Loading
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
Computer System Chapter 7. Linking Lynn Choi Korea University.
Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Linking Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Lecture 3 Translation.
Instruction Set Architecture
Assemblers, linkers, loaders
Slides adapted from Bryant and O’Hallaron
Overview of today’s lecture
Linking Topics Static linking Object files Static libraries Loading
Linking & Loading.
143A: Principles of Operating Systems Lecture 8: Basic Architecture of a Program Anton Burtsev October, 2017.
Homework In-line Assembly Code Machine Language
ICS143A: Principles of Operating Systems Lecture 21: Program linking and loading Anton Burtsev March, 2017.
Program Execution in Linux
CS 5204 Operating Systems Linking and Loading Godmar Back.
CS-3013 Operating Systems C-term 2008
Machine-Level Programming 1 Introduction
Machine-Level Programming 4 Procedures
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
“The course that gives CMU its Zip!”
Assembly Language Programming II: C Compiler Calling Sequences
“The course that gives CMU its Zip!”
Machine-Level Programming: Introduction
Linking & Loading CS-502 Operating Systems
Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
Instructors: Majd Sakr and Khaled Harras
“The course that gives CMU its Zip!”
Presentation transcript:

1 Linking

2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5

3 Monolithic source file Problems: efficiency: small change requires complete recompilation modularity: hard to share common functions (e.g. printf) Translator main.c p ASCII source file binary executable object file (memory image on disk)

4 main.cswap.c 1./*main.c */ 2.void swap() ; 3. 4.int buf[2] = {1, 2}; 5. 6.Int main() 7.{ 8. swap() ; 9. return 0 ; 10.} 1./*swap.c */ 2.extern int buf[]; 3. 4.int *bufp0 = &buf[0] 5.int *bufp1 ; 6. 7.void swap() 8.{ 9. int temp ; int bufp1 = &buf[1]; 12. temp = *bufp0 ; 13. *bufp0 = *bufp1 ; 14. *bufp1 = temp ; 15.}

5 Linker (ld) Translators main.c main.o Translators swap.c swap.o p separately compiled relocatable object files executable object file (contains code and data for all functions defined in main.c and swap.c) Separate Compilation

6 What is linker Linking is the process of –collecting and combining various pieces of code and data into a single executable file Executable file –Can be loaded (copied) into memory and executed.

7 What is linker Linking can be performed –at compile time, when the source code is translated into machine code by the linker –at load time, when the program is loaded into memory and executed by the loader –at run time, by application programs.

8 main.cswap.c 1./*main.c */ 2.void swap() ; 3. 4.int buf[2] = {1, 2}; 5. 6.Int main() 7.{ 8. swap() ; 9. return 0 ; 10.} 1./*swap.c */ 2.extern int buf[]; 3. 4.int *bufp0 = &buf[0] 5.int *bufp1 ; 6. 7.void swap() 8.{ 9. int temp ; int bufp1 = &buf[1]; 12. temp = *bufp0 ; 13. *bufp0 = *bufp1 ; 14. *bufp1 = temp ; 15.}

9 Example Two functions –main() and swap() Three global variables –buf, bufp0 which are initialized explicitly – bufp1 implicitly initialized to 0

10 Compiler Drivers Coordinates all steps in the translation and linking process –Typically included with each compilation system (e.g., gcc) –Invokes preprocessor (cpp) compiler (cc1) assembler (as), linker (ld). –Passes command line args to appropriate phases

11 unix> gcc -O2 -g -o p main.c swap.c cpp [args] main.c /tmp/main.i cc1 /tmp/main.i main.c -O2 [args] -o /tmp/main.s as [args] -o /tmp/main.o /tmp/main.s ld -o p [system obj files] /tmp/main.o /tmp/swap.o unix> Example

12 Static linking Input –A relocatable object files and command line arguments Output –Fully linked executable object file that can be loaded and run

13 Static linking Object file –Various code and data sections –Instructions are in one section –Initialized global variables are in one section –Uninitialized global variables are in one section

14 Static linking Symbol resolution –resolves external references. external reference: reference to a symbol defined in another object file

15 Code Examples C code int accum = 0; int sum(int x, int y) { int t = x+y; accum += t; return t; } _sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax addl %eax, accum movl %ebp,%esp popl %ebp ret Obtain with command gcc –O2 -S code.c Assembly file code.s

16 Code Examples e5 8b 45 0c ec 5d c3 Obtain with command gcc –O2 -c code.c Relocatable object file code.o

17 Code Examples Obtain with command objdump -d code.o Disassembly output 0x80483b4 : 0x80483b4 55 0x80483b5 89 e5 0x80483b7 8b 45 0c 0x80483ba x80483bd x80483c3 89 ec 0x80483c5 5d 0x80483c6 c3 push %ebp mov %esp,%ebp mov 0xc(%ebp),%eax add 0x8(%ebp),%eax add %eax, 0x0 mov %ebp,%esp pop %ebp ret

18 Static linking Relocation –relocates symbols from their relative locations in the.o files to new absolute positions in the executable. –updates all references to these symbols to reflect their new positions. references can be in either code or data –code: swap(); /* ref to symbol swap */ –data: int *bufp0=&buf[0]; /* ref to symbol buf */

b4 : b4: 55push %ebp b5: 89 e5mov %esp, %ebp b7: 83 ec 08sub $0x8, %esp ba: e call 80483c bf: 31 c0xor %eax, %eax c1: 89 ecmov %ebp, %esp c3: 5dpop %ebp c4: c3ret c5: 90nop c6: 90nop c7: 90nop Relocation

c8 : c8:55 push%ebp c9: 8b 15 5c mov0x804945c, %edx get *bufp cf: a mov0x , %edx get buf[1] d4:89 e5 mov%esp, %ebp d6:c movl $0x , 0x de: bufp1 = &buf[1] e0:89 ec mov%ebp, %esp e2:8b 0a mov (%edx), %ecx e4:80 02 mov%eax, (%edx) e6:a mov0x , %eax eb:89 08 mov%ecx, (%eax) ed:5d pop%ebp ee:c3 ret Relocation

: : c : c: Relocation

22 Object files Relocatable object file –Contain binary code and data in a form that can be combined with other relocatable object files to create an executable file Executable object file –Contains binary code and data in a form that can be copied directly into memory and executed

23 Object files Shared object file –A special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run time

24 Executable and Linkable Format (ELF) Standard binary format for object files Derives from AT&T System V Unix –later adopted by BSD Unix variants and Linux One unified format for relocatable object files (.o), executable object files, and shared object files (.so) –generic name: ELF binaries Better support for shared libraries than old a.out formats.

25 ELF header Program header table (required for executables).text section.data section.bss section.symtab.rel.txt.rel.data.debug Section header table (required for relocatables).line.strtab ELF object file format

26 EFI object file format Elf header –magic number, type (.o, exec,.so), machine, byte ordering, etc. Section header table

27 EFI object file format.text section –code.data section –initialized (static) data.bss section –uninitialized (static) data –“Block Started by Symbol” –“Better Save Space” –has section header but occupies no space

28 EFI object file format.symtab section –symbol table –procedure and static variable names –section names and locations.rel.text section –relocation info for.text section –addresses of instructions that will need to be modified in the executable

29 EFI object file format.rel.data section –relocation info for.data section –addresses of pointer data that will need to be modified in the merged executable.debug section –debugging symbol table, local variables and typedefs, global variables, original C source file (gcc -g)

30 EFI object file format.line: –Mapping between line numbers in the original C source program and machine code instructions in the.text section..strtab: –A string table for the symbol tables and for the section names.

31 ELF header Program header table (required for executables).text section.data section.bss section.symtab.rel.txt.rel.data.debug Section header table (required for relocatables).line.strtab EFI object file format

32 Symbols Three kinds of symbols –Defined global symbols –Referenced global symbols –Local symbols

33 Symbols Defined global symbols –Defined by module m and can be referenced by other modules Nonstatic C functions Global variables that are defined without the C static attribute

34 Symbols Referenced global symbols –Referenced by module m but defined by some other module C functions and variables that are defined in other modules Local symbols –Defined and referenced exclusively by module m. C functions and global variables with static attribute

35 Symbol Tables Each relocatable object module has a symbol table A symbol table contains information about the symbols that are defined and referenced by the module

36 Symbol Tables Local nonstatic program variables –does not contain in the symbol table in.symbol Local static procedure variables –Are not managed on the stack –Be allocated in.data or.bss

37 Examples 1. int f() 2. { 3. static int x=1 ; 4. return x; 5. } int g() 8. { 9. static int x = 1; 10. return x ; 11. } x.1 and x.2 are allocated in.data

38 Symbol Tables Compiler exports symbols in.s file Assembler builds symbol tables using exported symbols An ELF symbol table is contained in.symtab section Symbol table contains an array of entries

39 extern int buf[]; int *bufp0 = &buf[0]; static int *bufp1; static void incr() { static int count=0; count++; } Example (Symbol Table)

40.file"symbol.c".globl _bufp0.data.align 4 _bufp0:.long_buf.lcomm _count.1181,16 Example (Symbol Table)

41.text.def_incr;.scl3;.type32;.endef _incr: pushl%ebp movl%esp, %ebp movl_count.1181, %eax addl$1, %eax movl%eax, _count.1181 popl%ebp ret.lcomm _bufp1,16 Example (Symbol Table)

42 void swap() { int temp; incr(); bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; } Example (Symbol Table)

43 the module that defines the symbol –swap.o –main.o the symbol type –local –global –extern the section –.text –.data –.bss Example (Symbol Table)

44 main.cswap.c 1./*main.c */ 2.void swap() ; 3. 4.int buf[2] = {1, 2}; 5. 6.Int main() 7.{ 8. swap() ; 9. return 0 ; 10.} 1./*swap.c */ 2.extern int buf[]; 3. 4.int *bufp0 = &buf[0] 5.int *bufp1 ; 6. 7.void swap() 8.{ 9. int temp ; bufp1 = &buf[1]; 12. temp = *bufp0 ; 13. *bufp0 = *bufp1 ; 14. *bufp1 = temp ; 15.}

45 ELF Symbol Tables 1. typedef struct { 2. int name ;/* string table offset */ 3. int value ;/* section offset, or VM address */ 4. int size ;/* object size in bytes */ 5. char type:4, /* data, func, section, or src file name */ 6. binding:4 ;/* local or global */ 7. char reserved ;/* unused */ 8. char section ;/* section header index, ABS, UNDEF, */ 9. /* or COMMON */ 10. } ABS, UNDEF, COMMON

46 Num:ValueSizeType BindOtNdxName 8: 0 8 OBJECT GLOBAL03buf 9: 0 17 FUNC GLOBAL01main 10: 0 0 NOTYPE GLOBAL0UNDswap Num:ValueSizeType BindOtNdxName 8: 0 4 OBJECT GLOBAL03bufp0 9: 0 0 NOTYPE GLOBAL0UNDbuf 10: 0 39 FUNC GLOBAL01swap 11: 4 4 OBJECT GLOBAL0COMbufp1 alignment ELF Symbol Tables