Linking Ⅱ.

Slides:



Advertisements
Similar presentations
Hand-Held Devices and Embedded Systems Course Student: Tomás Sánchez López Student ID:
Advertisements

Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
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.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Linking & Loading CS-502 Operating Systems
Linker and Loader. Program building into four stages (C Program) Preprocessing (Preprocessor) It processes include files, conditional compilation instructions.
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
Lecture 10: Linking and loading. Lecture 10 / Page 2AE4B33OSS 2011 Contents Linker vs. loader Linking the executable Libraries Loading executable ELF.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
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!”
Position Independent Code self sufficiency of combining program.
CS 330 What software engineers need to know about linking and a few things about execution.
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.
A Case Study on UNIX a.out File Format. a.out Object File Format A.out is an object/executable file format used on UNIX machines. –Think about why the.
Topic 2d High-Level languages and Systems Software
CS 367 Linking Topics Static linking Object files Static libraries
CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002.
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.
Processes and Threads-I Static and dynamic linking, Loading, Anatomy of a Process.
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
Different Types of Libraries
1 Linking. 2 Outline Symbol Resolution Relocation Suggested reading: 7.6~7.7.
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
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.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Lecture 3 Translation.
Assemblers, linkers, loaders
Computer Architecture & Operations I
Slides adapted from Bryant and O’Hallaron
Overview of today’s lecture
Linking Topics Static linking Object files Static libraries Loading
The University of Adelaide, School of Computer Science
Linking & Loading.
ICS143A: Principles of Operating Systems Lecture 21: Program linking and loading Anton Burtsev March, 2017.
Program Execution in Linux
CS-3013 Operating Systems C-term 2008
“The course that gives CMU its Zip!”
“The course that gives CMU its Zip!”
Linking.
Linking & Loading CS-502 Operating Systems
Program Execution in Linux
Linking & Loading CS-502 Operating Systems
Instructors: Majd Sakr and Khaled Harras
“The course that gives CMU its Zip!”
Presentation transcript:

Linking Ⅱ

Outline Static linking Symbols & Symbol Table Relocation Executable Object Files Loading Suggested reading: 7.3~7.5, 7.7~7.9

Figure 7.1 P541 main.c swap.c /*main.c */ void swap() ; int buf[2] = {1, 2}; Int main() { swap() ; return 0 ; } /*swap.c */ extern int buf[]; int *bufp0 = &buf[0] int *bufp1 ; void swap() int temp ; bufp1 = &buf[1]; temp = *bufp0 ; *bufp0 = *bufp1 ; *bufp1 = temp ; Figure 7.1 P541

Example P542 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 <similar process for swap.c> ld -o p [system obj files] /tmp/main.o /tmp/swap.o unix>

Object file 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

(required for executables) (required for relocatables) ELF object file format 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 Figure 7.3 P544

7.3 Object Files

Relocatable object file 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

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

7.2 Static Linking

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

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

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: a(); /* ref to symbol a */ data: int *xp=&x; /* ref to symbol x */

7.4 Relocatable Object Files

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.

(required for executables) (required for relocatables) EFI object file format 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 Figure 7.3 P544

EFI object file format Elf header Program header table magic number, type (.o, exec, .so), machine, byte ordering, etc. Program header table page size, virtual addresses for memory segments (sections), segment sizes.

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

EFI object file format .symtab section .rel.text 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 instructions for modifying.

EFI object file format .rel.data section .debug 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)

EFI object file format .line: .strtab: 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.

7.5 Symbols and Symbol Tables

Symbols Three kinds of symbols 1) 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 2) Referenced global symbols Referenced by module m but defined by some other module C functions and variables that are defined in other modules 3) Local symbols Defined and referenced exclusively by module m. C functions and global variables with static attribute

Symbols Three kinds of symbols 1) 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

Symbols Three kinds of symbols 2) Referenced global symbols Referenced by module m but defined by some other module C functions and variables that are defined in other modules 3) Local symbols Defined and referenced exclusively by module m. C functions and global variables with static attribute

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

Local nonstatic program variables Local static procedure variables 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

(required for executables) (required for relocatables) ELF object file format 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 Figure 7.3 P544

Examples int f() { static int x=1 ; return x; } int g() x.1 and x.2 are allocated in .data

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

main.c swap.c /*main.c */ void swap() ; int buf[2] = {1, 2}; Int main() { swap() ; return 0 ; } /*swap.c */ extern int buf[]; int *bufp0 = &buf[0] int *bufp1 ; void swap() int temp ; bufp1 = &buf[1]; temp = *bufp0 ; *bufp0 = *bufp1 ; *bufp1 = temp ;

ELF Symbol Tables P547 ABS, UNDEF, COMMON typedef struct { int name ; /* string table offset */ int value ; /* section offset, or VM address */ int size ; /* object size in bytes */ char type:4 , /* data, func, section, or src file name */ binding:4 ; /* local or global */ char reserved ;/* unused */ char section ; /* section header index, ABS, UNDEF, */ /* or COMMON */ } ABS, UNDEF, COMMON

ELF Symbol Tables P547 Num: Value Size Type Bind Ot Ndx Name 8: 0 8 OBJECT GLOBAL 0 3 buf 9: 0 17 FUNC GLOBAL 0 1 main 10: 0 0 NOTYPE GLOBAL 0 UND swap 8: 0 4 OBJECT GLOBAL 0 3 bufp0 9: 0 0 NOTYPE GLOBAL 0 UND buf 10: 0 39 FUNC GLOBAL 0 1 swap 11: 4 4 OBJECT GLOBAL 0 COM bufp1 alignment

7.6 Symbol Resolution

Symbol Resolution P549 void foo(void) int main() { foo() ; return 0 ; } Unix> gcc –Wall –O2 –o linkerror linkerror.c /tmp/ccSz5uti.o: In function ‘main’: /tmp/ccSz5uti.o (.text+0x7): undefined reference to ‘foo’ collect2: ld return 1 exit status

7.6.1 How Linkers Resolve Multiply Defined Global Symbols

7.6.2 Linking with Static Libraries

7.6.3 How Linkers Use Static Libraries to Resolve References

7.7 Relocation

Relocation Relocation Two steps Merge the input modules Assign runtime address to each symbol Two steps Relocating sections and symbol definitions Relocating symbol references within sections

For each reference to an object with unknown location Relocation For each reference to an object with unknown location Assembler generates a relocation entry Relocation entries for code are placed in .rel.text Relocation entries for data are placed in .rel.data

Relocation Relocation Entry Figure 7.8 P558 typedef struct { int offset ; int symbol:24, type:8 ; } Elf32_Rel ; Figure 7.8 P558

Relocation P559 1) Recolating PC-relative References e8 fc ff ff ff call 7<main+0x7> swap(); There is a relocation entry in rel.txt offset symbol type 7 swap R_386_PC32

Relocation 2) Relocating Absolute References int *bufp0 = &buf[0] ; 00000000 <bufp0>: 0: 00 00 00 00 There is a relocation entry in rel.data offset symbol type 0 buf R_386_32

For each reference to an object with unknown location Relocation For each reference to an object with unknown location Assembler generates a relocation entry Relocation entries for code are placed in .rel.text Relocation entries for data are placed in .rel.data

Relocation P559 1) Relocating PC-relative References e8 fc ff ff ff call 7<main+0x7> swap(); 7: R_386_PC32 swap relocation entry r.offest = 0x7 r.symbol = swap r.type = R_386_PC32 ADDR(main)=ADDR(.text) = 0x80483b4 ADDR(swap)=0x80483c8 refaddr = ADDR(main)+r.offset = 0x80483bb ADDR(r.symbol)=ADDR(swap)=0x80483c8 *refptr = (unsigned) (ADDR(r.symbol) + *refptr – refaddr = (unsigned) (0x80483c8 + (-4) – 0x80483bb) = (unsigned) 0x9

Relocation int *bufp0 = &buf[0] ; 00000000 <bufp0>: 0: 00 00 00 00 int *bufp0 = &buf[0]; 0: R_386_32 buf relocation entry ADDR(r.symbol) = ADDR(buf) = 0x8049454 *refptr = (unsigned) (ADDR(r.symbol)+ *refptr) = (unsigned) (0x8049454) 0804945c <bufp0>: 0804945c: 54 94 04 08

Relocation Figure 7.9 P559 foreach section s { foreach relocation entry r { refptr = s + r.offset ; /* ptr to reference to be relocated */ /* relocate a PC-relative reference */ if (r.type == R_386_PC32) { refaddr = ADDR(s) + r.offset ; /* ref’s runtime address */ *refptr = (unsigned) (ADDR(r.symbol) + *refptr –refaddr) ; } /* relocate an absolute reference */ if ( r.type == R_386_32 ) *refptr = (unsigned) (ADDR(r.symbol) + *refptr) ; Figure 7.9 P559

Relocation 080483b4<main>: 08483b4: 55 push %ebp 08483b5: 89 e5 mov %esp, %ebp 08483b7: 83 ec 08 sub $0x8, %esp 08483ba: e8 09 00 00 00 call 80483c8 <swap> 08483bf: 31 c0 xor %eax, %eax 08483c1: 89 ec mov %ebp, %esp 08483c3: 5d pop %ebp 08483c4: c3 ret 08483c5: 90 nop 08483c6: 90 nop 08483c7: 90 nop

Relocation Figure 7.10 (a) P562 080483c8<swap>: 80483c8: 55 push %ebp 80483c9: 8b 15 5c 94 04 08 mov 0x804945c, %edx get *bufp0 80483cf: a1 58 94 04 08 mov 0x8049458, %edx get buf[1] 80483d4: 89 e5 mov %esp, %ebp 80483d6: c7 05 48 85 04 08 58 movl $0x8049458, 0x8049548 80483dd: 94 04 08 bufp1 = &buf[1] 80483e0: 89 ec mov %ebp, %esp 80483e2: 8b 0a mov (%edx), %ecx 80483e4: 80 02 mov %eax, (%edx) 80483e6: a1 48 95 04 08 mov 0x8049548, %eax 80483eb: 89 08 mov %ecx, (%eax) 80483ed: 5d pop %ebp 80483ee: c3 ret Figure 7.10 (a) P562

Relocation Figure 7.10 (b) P562 08049454 <buf>: 8049454: 01 00 00 00 02 00 00 00 084945c<bufp0>: 84945c: 54 94 04 08 Figure 7.10 (b) P562

7.8 Executable Object Files

EFI object file format Figure 7.11 P563 ELF header Segment header table .init section .data section .bss section .symtab .debug Section header table .line .strtab .rodata section .text section Figure 7.11 P563

Executable Object Files ELF header Overal information Entry point .init section A small function _init Initialization Segment header table

7.9 Loading Executable Object Files

Figure 7.13 P565

Loading Unix> ./p Loader Memory-resident operating system code Invoked by call the execve function Copy the code and data in the executable object file from disk into memory Jump to the entry point Run the program

Loading P565 Startup code At the _start address defined in the crt1.o Same for all C program 0x080480c0<_start>: call _libc_init_first call _init call atexit call main call _exit

7.10 Dynamic Linking with Shared Libraries 7.11 Loading and Linking Shared Libraries from Applications 7.12 *Position-Independent Code (PIC) 7.13 Tools for Manipulating Object Files