Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt 15-213 Introduction to Computer Systems.

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.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
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
Chapter 3 Loaders and Linkers
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
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++
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Object Code.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
CS 330 What software engineers need to know about linking and a few things about execution.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Spring 2004 © UCB Roy Wang inst.eecs.berkeley.edu/~cs61c-tf inst.eecs.berkeley.edu/~cs61c.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Linking Topics Static linking Object files Static libraries Loading Dynamic linking of shared libraries CS213.
MIPS coding. SPIM Some links can be found such as:
Topic 2d High-Level languages and Systems Software
C OMPUTER A RCHITECTURE & O RGANIZATION Assemblers and Compilers Engr. Umbreen Sabir Computer Engineering Department, University of Engg. & Technology.
CS 367 Linking Topics Static linking Object files Static libraries
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Linking February 20, 2001 Topics static linking object files static libraries loading dynamic linking of shared libraries class16.ppt “The course.
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 Ⅱ.
Static Shared Library. Non-shared v.s. Shared Library A library is a collection of pre-written function calls. Using existing libraries can save a programmer.
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.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Chapter 13 : Symbol Management in Linking
Programs and Processes Jeff Chase Duke University.
Different Types of Libraries
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.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
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
System Programming and administration
The University of Adelaide, School of Computer Science
Linking & Loading.
Program Execution in Linux
CS-3013 Operating Systems C-term 2008
Topic 2e High-Level languages and Systems Software
Generating Programs and Linking
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
“The course that gives CMU its Zip!”
“The course that gives CMU its Zip!”
Linking.
Linking & Loading CS-502 Operating Systems
Computer Architecture
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
Program Assembly.
“The course that gives CMU its Zip!”
Presentation transcript:

Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems

CS 213 F’98– 2 – class11.ppt A simplistic program translation scheme Problems: efficiency: small change requires complete recompilation modularity: hard to share common functionality (e.g. printf) Translators p.c p Loader p source file executable object file (memory image on disk) executing program (memory image)

CS 213 F’98– 3 – class11.ppt Separate compilation Static Linker Translator p1.c p1.o Translator p2.c p2.o p Loader Improves modularity and efficiency, but still hard to package common functions Translator libc.c libc.o relocatable object files executable object file (contains code and data for all functions defined in libc.c)

CS 213 F’98– 4 – class11.ppt Static libraries Translator p1.c p1.o Translator p2.c p2.olibc.a static library of relocatable object files executable object file (only contains code and data for libc functions that are called from p1.c and p2.c) Further improves modularity and efficiency, but duplicates common functions in different processes Static Linker p Loader

CS 213 F’98– 5 – class11.ppt Shared libraries libc functions called by p1.c and p2.c are loaded at run- time and shared among processes. shared library of dynamically relocatable object files Translator p1.c p1.o Translator p2.c p2.o libc.so Static Linker p Loader/Dynamic Linker

CS 213 F’98– 6 – class11.ppt The complete picture Translator p1.c p1.o Translator p2.c p2.o libc.so Static Linker p Loader/Dynamic Linker libwhatever.a running program libm.so

CS 213 F’98– 7 – class11.ppt Object files char c[100]; double d; int n=20; main() { int i; i=5; c[i] = ‘z’; } Headers Relocation info Symbol table text sections: read-only code and data data sections initialized static variables 0 bss sections uninitialized static variables Block Started by Symbol Better Save Space C ProgramObject file Note: local variables don’t go in object files. They are created at runtime on the stack. Increasing addresses

CS 213 F’98– 8 – class11.ppt Example C program main() { zip(); } int x=0xfe; code(int *p) { srand(*p); } extern int x; int *p = &x; zip() { code(p); } main.c:zip.c:code.c:

CS 213 F’98– 9 – class11.ppt Compiling and linking the example program % cc -v -c main.c cfe main.c > /tmp/ctmfaacgja ugen -O1 /tmp/ctmfaacgja -o /tmp/ctmcaacgja as1 -O1 /tmp/ctmcaacgja -o main.o % cc -c zip.c % cc -c code.c % cc -v -Wl,-O0 -o main main.o zip.o code.o ld -o main -O0 -call_shared /usr/lib/cmplrs/cc/crt0.o main.o zip.o code.o -lc Unix linker C runtime system

CS 213 F’98– 10 – class11.ppt Linker functions main() 1. Resolving external references 2. Relocating symbol definitions and references main.o *p zip() zip.o x code() code.o x crt main() zip() crt crt0.o 0 code() *p main Data Text

CS 213 F’98– 11 – class11.ppt Symbols Lexical entities that correspond to functions and global variables Each symbol has a value (address) Code consists of symbol definitions and references References can be either local or external main() { zip(); } int x=0xfe; code(int *p) { srand(*p); } extern int x; int *p = &x; zip() { code(p); } main.c:zip.c:code.c: Ref to external symbol zip Def of local symbol zip Def of local symbol p Ref to external symbol x Def of local symbol x Ref to external symbol srand (defined in libc.so) Ref to local symbol p

CS 213 F’98– 12 – class11.ppt Alpha ECOFF object files File header Optional header Section headers Section data Section relocation info Symbol table 0 File and optional headers magic number, size and number of sections, flags –byte ordering, executable or relocatable?, shared?, calls shared library functions? entry point, gp value, start and sizes of text, data, and bss segments Section headers for each section in “Section data”: address, size, number of relocation entries Section data program code and data Section relocation info which instructions and data items will need to be relocated? Symbol table type and value of relevant symbols

CS 213 F’98– 13 – class11.ppt Section data (relocatable object files).bss.sbss.sdata.lit4.lit8.lita.data.init/.fini.text.pdata/.xdata.rdata.rconst bss segment uninitialized variables (block started by symbol) (better save space!) data segment initialized variables text segment machine code uninitialized data small (4-byte) bss small data 4-byte literal pool 8-byte literal pool 8-byte literal address pool read only data read only constants large data support for exceptions machine instructions process init/deinit code (Runtime memory image)(File format)

CS 213 F’98– 14 – class11.ppt.text section (main.o) main: 0x0: ldasp, -16(sp) 0x4: ldahgp, 1(t12) 0x8: ldagp, (gp) 0xc: stqra, 0(sp) 0x10: ldqt12, (gp) 0x14: jsrra, (t12), zip 0x18: ldahgp, 1(ra) 0x1c: ldqra, 0(sp) 0x20: biszero, zero, v0 0x24: ldagp, (gp) 0x28: ldasp, 16(sp) 0x2c: retzero, (ra), 1 main() { zip(); }.text.lita 0x00 (0) 0x50 (80) start 0x30 (48) 0x10 (16) size Assembler assumes that gp=32,832=(64K+128)/2 (Note that t12 = pc-4) t12 = address stored at.lita+0 resets gp to 32,832 (Note: ra = pc) Question: Why use gp-relative referencing of data and functions?

CS 213 F’98– 15 – class11.ppt Symbol table (main.o) [Index] Name Value Sclass Symtype [0] main 0x0 Text Proc [1] zip 0x0 Undefined Proc main() { zip(); }.text.lita 0x00 (0) 0x50 (80) start 0x30 (48) 0x10 (16) size

CS 213 F’98– 16 – class11.ppt Relocation entries (main.o) Vaddr SymndxType Extern Name.text: 0x04 4 GPDISP local 0x10 13 LITERAL local.lita 0x14 3 LITUSE local R_LU_JSR 0x14 1 HINT extern zip 0x18 12 GPDISP local.lita: 0x50 1 REFQUAD extern zip main() { zip(); }.text.lita 0x00 (0) 0x50 (80) start 0x30 (48) 0x10 (16) size GPDISP : instruction pair that sets gp value LITERAL: gp-relative reference to literal in.lita LITUSE: identifies instance of a literal address previously loaded into a register HINT: 14 bit jsr hint reference REFQUAD: 64-bit reference to the symbol’s virtual address

CS 213 F’98– 17 – class11.ppt Relocations (main.o) main: 0x0: ldasp, -16(sp) 0x4: ldahgp, 1(t12) 0x8: ldagp, (gp) 0xc: stqra, 0(sp) 0x10: ldqt12, (gp) 0x14: jsrra, (t12), zip 0x18: ldahgp, 1(ra) 0x1c: ldqra, 0(sp) 0x20: biszero, zero, v0 0x24: ldagp, (gp) 0x28: ldasp, 16(sp) 0x2c: retzero, (ra), 1 main() { zip(); }.text.lita 0x00 (0) 0x50 (80) start 0x30 (48) 0x10 (16) size Vaddr Symndx Type Extern Name.text: 0x04 4 GPDISP local 0x10 13 LITERAL local.lita 0x14 3 LITUSE local R_LU_JSR 0x14 1 HINT extern zip 0x18 12 GPDISP local.lita: 0x50 1 REFQUAD extern zip Notes: 1. LITUSE at 0x14 allows jsr to be replaced by bsr

CS 213 F’98– 18 – class11.ppt.text section (zip.o) extern int x; int *p = &x; zip() { code(p); }.text.lita 0x00 (0) 0x60 (96) start 0x40 (64) 0x10 (16) size.sdata0x70 (112)0x10 (16) 0x0:ldasp, -16(sp) 0x4:ldahgp, 1(t12) 0x8:ldagp, (gp) 0xc:stqra, 0(sp) 0x10:ldqt12, (gp) 0x14:ldqa0, (gp) 0x18:ldqa0, 0(a0) 0x1c:jsrra, (t12), code 0x20:ldahgp, 1(ra) 0x24:ldqra, 0(sp) 0x28:ldagp, (gp) 0x2c:ldasp, 16(sp) 0x30:retzero, (ra), 1 Assembler assumes that gp=32,848 t12 = addr(code) = cont(.lita+8) a0 = addr(p) = cont(.lita+0) a0 = p

CS 213 F’98– 19 – class11.ppt Symbol table (zip.o) [Index] Name Value Sclass Symtype [0] p 0x70 SData Global [1] x 0x04 SUndefined Global [2] zip 0x00 Text Proc [3] code 0x00 Undefined Proc extern int x; int *p = &x; zip() { code(p); }.text.lita 0x00 (0) 0x60 (96) start 0x40 (64) 0x10 (16) size.sdata0x70 (112)0x10 (16)

CS 213 F’98– 20 – class11.ppt Relocation entries (zip.o) extern int x; int *p = &x; zip() { code(p); }.text.lita 0x00 (0) 0x60 (96) start 0x40 (64) 0x10 (16) size.sdata0x70 (112)0x10 (16) Vaddr Symndx Type Extern Name.text: 0x04 4 GPDISP local 0x14 13 LITERAL local.lita 0x18 1 LITUSE local R_LU_BASE 0x10 13 LITERAL local.lita 0x1c 3 LITUSE local R_LU_JSR 0x1c 3 HINT extern code 0x20 8 GPDISP local.lita: 0x60 0 REFQUAD extern p 0x68 3 REFQUAD extern code.sdata: 0x70 1 REFQUAD extern x 0x0:ldasp, -16(sp) 0x4:ldahgp, 1(t12) 0x8:ldagp, (gp) 0xc:stqra, 0(sp) 0x10:ldqt12, (gp) 0x14:ldqa0, (gp) 0x18:ldqa0, 0(a0) 0x1c:jsrra, (t12), code 0x20:ldahgp, 1(ra) 0x24:ldqra, 0(sp) 0x28:ldagp, (gp) 0x2c:ldasp, 16(sp) 0x30:retzero, (ra), 1

CS 213 F’98– 21 – class11.ppt.text section (code.o) int x=0xfe; code(int *p) { srand(*p); }.text.lita 0x00 (0) 0x60 (96) start 0x40 (64) 0x10 (16) size.sdata0x70 (112)0x10 (16) Assembler assumes that gp=32,848 t12 = addr(srand) = cont(.lita+0) srand is unresolved 0x0: ldasp, -64(sp) 0x4: ldahgp, 1(t12) 0x8: ldagp, (gp) 0xc: stqra, 0(sp) 0x10: stqa0, 16(sp) 0x14: ldqt12, (gp) 0x18: bisa0, a0, t0 0x1c: ldla0, 0(t0) 0x20: jsrra, (t12), srand 0x24: ldahgp, 1(ra) 0x28: ldqra, 0(sp) 0x2c: ldagp, (gp) 0x30: ldasp, 64(sp) 0x34: retzero, (ra), 1

CS 213 F’98– 22 – class11.ppt Symbol table (code.o) [Index] Name Value Sclass Symtype [0] x 0x70 SData Global [1] code 0x00 Text Proc [2] srand 0x00 Undefined Proc int x=0xfe; code(int *p) { srand(*p); }.text.lita 0x00 (0) 0x60 (96) start 0x40 (64) 0x10 (16) size.sdata0x70 (112)0x10 (16)

CS 213 F’98– 23 – class11.ppt Relocation entries (code.o) int x=0xfe; code(int *p) { srand(*p); }.text.lita 0x00 (0) 0x60 (96) start 0x40 (64) 0x10 (16) size.sdata0x70 (112)0x10 (16) 0x0: ldasp, -64(sp) 0x4: ldahgp, 1(t12) 0x8: ldagp, (gp) 0xc: stqra, 0(sp) 0x10: stqa0, 16(sp) 0x14: ldqt12, (gp) 0x18: bisa0, a0, t0 0x1c: ldla0, 0(t0) 0x20: jsrra, (t12), srand 0x24: ldahgp, 1(ra) 0x28: ldqra, 0(sp) 0x2c: ldagp, (gp) 0x30: ldasp, 64(sp) 0x34: retzero, (ra), 1 Vaddr Symndx Type Extern Name.text: 0x04 4 GPDISP local 0x14 13 LITERAL local.lita 0x20 3 LITUSE local R_LU_JSR 0x20 2 HINT extern srand 0x24 8 GPDISP local.lita: 0x60 2 REFQUAD extern srand Why no relocation entry for x?

CS 213 F’98– 24 – class11.ppt Executable object file (main).text.data 0x (4416) 0x (0).rconst0x (4224).lit80x a0 (160).sdata0x140000b0 (176) main: 0x : ldasp, -16(sp) 0x : ldahgp, 8192(t12) 0x : ldagp, 28064(gp) 0x c: stqra, 0(sp) 0x : ldqt12, (gp) 0x : ldaha0, -1(gp) 0x : ldqa0, 32752(a0) 0x c: bsrra, code 0x : ldahgp, 8192(ra) 0x : ldqra, 0(sp) 0x : ldagp, 28032(gp) 0x c: ldasp, 16(sp) 0x : retzero, (ra), 1 Linker sets gp at.data x60 (96) 0x450 (1104) 0x10 (16) 0x20 (32).got0x140000d0 (208)0x120 (288).sbss0x f0 (496)0x40 (64) addr(code) at.data+288 changed by linker!

CS 213 F’98– 25 – class11.ppt Symbol table (main) [0].rconst 0x scRConst Local [1].pdata 0x e0 PData Local [2].text 0x Text Local [3].init 0x Init Local [4].fini 0x d0 Fini Local [5].data 0x Data Local [6].xdata 0x XData Local [7].rdata 0x RData Local [8].lit8 0x b0 SData Local [9].lit4 0x b0 SData Local [10].sdata 0x b0 SData Local [11.sbss 0x f0 SBss Local [12].bss 0x Bss Local

CS 213 F’98– 26 – class11.ppt Symbol table (main) [13] __start 0x a0 Text Proc [24] x 0x c0 SData Global [25] __environ 0x Common Global [26] errno 0x Common Global [29] exit 0x Undefined Proc [31] main 0x f0 Text Proc [32] zip 0x Text Proc [33] code 0x Text Proc [42] p 0x b0 SData Global [43] __Argc 0x f0 SBss Global [44] __Argv 0x f8 SBss Global [56] srand 0x Undefined Proc

CS 213 F’98– 27 – class11.ppt Strong and weak symbols Program symbols are either strong or weak strong: procedures and ininitialized globals weak: uninitialized globals int foo=5; p1() { } int foo; p2() { } p1.c:p2.c: strong weak strong

CS 213 F’98– 28 – class11.ppt Linker’s symbol rules 1. A strong symbol can only appear once. 2. A weak symbol can be overridden by a strong symbol of the same name. 3. If multiple weak symbols, the linker can pick either one.

CS 213 F’98– 29 – class11.ppt Linker puzzles int foo; p1() {} int foo; p2() {} int foo; p1() {} double foo; p2() {} int foo=7; p1() {} double foo; p2() {} int foo=7; p1() {} int foo; p2() {} int foo; p1() {}