Program Translation and Execution I:Linking Sept. 29, 1998
Total Page:16
File Type:pdf, Size:1020Kb
15-213 Introduction to Computer Systems Program Translation and Execution I: Linking Sept. 29, 1998 Topics • object files • linkers class11.ppt A simplistic program translation scheme p.c source file Translators executable object file p (memory image on disk) Loader executing program p (memory image) Problems: • efficiency: small change requires complete recompilation • modularity: hard to share common functionality (e.g. printf) class11.ppt – 2 – CS 213 F’98 Separate compilation p1.c p2.c libc.c Translator Translator Translator relocatable p1.o p2.o libc.o object files Static Linker executable object file p (contains code and data for all functions Loader defined in libc.c) Improves modularity and efficiency, but still hard to package common functions class11.ppt – 3 – CS 213 F’98 Static libraries p1.c p2.c Translator Translator static library p1.o p2.o libc.a of relocatable object files Static Linker p executable object file (only contains code and data for libc functions that are called Loader from p1.c and p2.c) Further improves modularity and efficiency, but duplicates common functions in different processes class11.ppt – 4 – CS 213 F’98 Shared libraries p1.c p2.c Translator Translator p1.o p2.o Static Linker shared library of dynamically p libc.so relocatable object files Loader/Dynamic Linker libc functions called by p1.c and p2.c are loaded at run- time and shared among processes. class11.ppt – 5 – CS 213 F’98 The complete picture p1.c p2.c Translator Translator p1.o p2.o libwhatever.a Static Linker p libc.so libm.so Loader/Dynamic Linker running program class11.ppt – 6 – CS 213 F’98 Object files C Program Object file char c[100]; bss sections double d; uninitialized static variables Block Started by Symbol int n=20; Better Save Space main() { int i; data sections Increasing initialized static variables addresses i=5; c[i] = ‘z’; } text sections: read-only code and data Note: local variables don’t go in object files. They are created at Headers runtime on the stack. Relocation info Symbol table 0 class11.ppt – 7 – CS 213 F’98 Example C program main.c: zip.c: code.c: main() { extern int x; int x=0xfe; zip(); } int *p = &x; code(int *p) { srand(*p); zip() { } code(p); } class11.ppt – 8 – CS 213 F’98 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 class11.ppt – 9 – CS 213 F’98 Linker functions 1. Resolving external references 2. Relocating symbol definitions and references main crt0.o crt 0 x *p Data main.o main() 0 code() *p zip() zip.o zip() Text 0 main() x crt code.o code() 0 0 class11.ppt – 10 – CS 213 F’98 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.c: zip.c: code.c: main() { extern int x; int x=0xfe; zip(); } int *p = &x; code(int *p) { srand(*p); zip() { Def of } code(p); local Ref to } symbol x external symbol zip Ref to external Def of Ref to Def of local symbol srand local external symbol zip (defined in libc.so) symbol p symbol x Ref to local symbol p class11.ppt – 11 – CS 213 F’98 Alpha ECOFF object files File and optional headers • magic number, size and number of sections, flags – byte ordering, executable or relocatable?, shared?, calls shared library functions? Symbol table • entry point, gp value, start and sizes of Section relocation info text, data, and bss segments Section data Section headers Section headers Optional header • for each section in “Section data”: address, size, number of relocation entries File header 0 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 class11.ppt – 12 – CS 213 F’98 Section data (relocatable object files) (Runtime memory image) (File format) bss segment .bss uninitialized data uninitialized variables .sbss small (4-byte) bss (block started by symbol) (better save space!) .pdata/.xdata support for exceptions .lit4 4-byte literal pool .lit8 8-byte literal pool data segment .lita initialized variables 8-byte literal address pool .rconst read only constants .rdata read only data .sdata small data .data large data text segment .init/.fini machine code process init/deinit code .text machine instructions class11.ppt – 13 – CS 213 F’98 .text section (main.o) start size main() { .text 0x00 (0) 0x30 (48) zip(); } .lita 0x50 (80) 0x10 (16) main: Assembler assumes 0x0: lda sp, -16(sp) that gp=32,832=(64K+128)/2 0x4: ldah gp, 1(t12) 0x8: lda gp, -32704(gp) (Note that t12 = pc-4) 0xc: stq ra, 0(sp) 0x10: ldq t12, -32752(gp) t12 = address stored at .lita+0 0x14: jsr ra, (t12), zip 0x18: ldah gp, 1(ra) resets gp to 32,832 0x1c: ldq ra, 0(sp) (Note: ra = pc) 0x20: bis zero, zero, v0 0x24: lda gp, -32728(gp) 0x28: lda sp, 16(sp) 0x2c: ret zero, (ra), 1 Question: Why use gp-relative referencing of data and functions? class11.ppt – 14 – CS 213 F’98 Symbol table (main.o) main() { start size .text 0x00 (0) 0x30 (48) zip(); } .lita 0x50 (80) 0x10 (16) [Index] Name Value Sclass Symtype [0] main 0x0 Text Proc [1] zip 0x0 Undefined Proc class11.ppt – 15 – CS 213 F’98 Relocation entries (main.o) start size main() { .text 0x00 (0) 0x30 (48) zip(); } .lita 0x50 (80) 0x10 (16) Vaddr Symndx Type Extern Name GPDISP : instruction pair that sets gp value .text: 0x04 4 GPDISP local LITERAL: gp-relative 0x10 13 LITERAL local .lita reference to literal in .lita 0x14 3 LITUSE local R_LU_JSR LITUSE: identifies instance of 0x14 1 HINT extern zip a literal address previously 0x18 12 GPDISP local loaded into a register .lita: HINT: 14 bit jsr hint reference 0x50 1 REFQUAD extern zip REFQUAD: 64-bit reference to the symbol’s virtual address class11.ppt – 16 – CS 213 F’98 Relocations (main.o) start size main() { .text 0x00 (0) 0x30 (48) zip(); } .lita 0x50 (80) 0x10 (16) main: Vaddr Symndx Type Extern Name 0x0: lda sp, -16(sp) .text: 0x4: ldah gp, 1(t12) 0x04 4 GPDISP local 0x8: lda gp, -32704(gp) 0x10 13 LITERAL local .lita 0xc: stq ra, 0(sp) 0x14 3 LITUSE local R_LU_JSR 0x10: ldq t12, -32752(gp) 0x14 1 HINT extern zip 0x14: jsr ra, (t12), zip 0x18 12 GPDISP local 0x18: ldah gp, 1(ra) 0x1c: ldq ra, 0(sp) .lita: 0x20: bis zero, zero, v0 0x50 1 REFQUAD extern zip 0x24: lda gp, -32728(gp) 0x28: lda sp, 16(sp) Notes: 0x2c: ret zero, (ra), 1 1. LITUSE at 0x14 allows jsr to be replaced by bsr class11.ppt – 17 – CS 213 F’98 .text section (zip.o) extern int x; int *p = &x; start size .text 0x00 (0) 0x40 (64) zip() { .lita 0x60 (96) 0x10 (16) code(p); .sdata 0x70 (112) 0x10 (16) } 0x0: lda sp, -16(sp) 0x4: ldah gp, 1(t12) Assembler assumes 0x8: lda gp, -32688(gp) that gp=32,848 0xc: stq ra, 0(sp) 0x10: ldq t12, -32744(gp) t12 = addr(code) = cont(.lita+8) 0x14: ldq a0, -32752(gp) a0 = addr(p) = cont(.lita+0) 0x18: ldq a0, 0(a0) a0 = p 0x1c: jsr ra, (t12), code 0x20: ldah gp, 1(ra) 0x24: ldq ra, 0(sp) 0x28: lda gp, -32720(gp) 0x2c: lda sp, 16(sp) 0x30: ret zero, (ra), 1 class11.ppt – 18 – CS 213 F’98 Symbol table (zip.o) extern int x; int *p = &x; start size .text 0x00 (0) 0x40 (64) zip() { .lita 0x60 (96) 0x10 (16) code(p); .sdata 0x70 (112) 0x10 (16) } [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 class11.ppt – 19 – CS 213 F’98 Relocation entries (zip.o) extern int x; int *p = &x; start size .text 0x00 (0) 0x40 (64) zip() { .lita 0x60 (96) 0x10 (16) code(p); .sdata 0x70 (112) 0x10 (16) } 0x0: lda sp, -16(sp) Vaddr Symndx Type Extern Name 0x4: ldah gp, 1(t12) .text: 0x8: lda gp, -32688(gp) 0x04 4 GPDISP local 0xc: stq ra, 0(sp) 0x14 13 LITERAL local .lita 0x10: ldq t12, -32744(gp) 0x18 1 LITUSE local R_LU_BASE 0x14: ldq a0, -32752(gp) 0x10 13 LITERAL local .lita 0x18: ldq a0, 0(a0) 0x1c 3 LITUSE local R_LU_JSR 0x1c: jsr ra, (t12), code 0x1c 3 HINT extern code 0x20: ldah gp, 1(ra) 0x20 8 GPDISP local 0x24: ldq ra, 0(sp) .lita: 0x28: lda gp, -32720(gp) 0x60 0 REFQUAD extern p 0x2c: lda sp, 16(sp) 0x68 3 REFQUAD extern code 0x30: ret zero, (ra), 1 .sdata: 0x70 1 REFQUAD extern x class11.ppt – 20 – CS 213 F’98 .text section (code.o) start size int x=0xfe; .text 0x00 (0) 0x40 (64) code(int *p) { .lita 0x60 (96) 0x10 (16) srand(*p); .sdata 0x70 (112) 0x10 (16) } 0x0: lda sp, -64(sp) 0x4: ldah gp, 1(t12) Assembler assumes 0x8: lda gp, -32688(gp) that gp=32,848 0xc: stq ra, 0(sp) 0x10: stq a0, 16(sp) 0x14: ldq t12, -32752(gp) t12 = addr(srand) = cont(.lita+0) 0x18: bis a0, a0, t0 0x1c: ldl a0, 0(t0) 0x20: jsr ra, (t12), srand srand is unresolved 0x24: ldah gp, 1(ra) 0x28: ldq ra, 0(sp) 0x2c: lda gp, -32724(gp) 0x30: lda sp, 64(sp) 0x34: ret zero, (ra), 1 class11.ppt – 21 – CS 213 F’98 Symbol table (code.o) start size int x=0xfe; .text 0x00 (0) 0x40 (64) code(int *p) { .lita 0x60 (96) 0x10 (16) srand(*p); .sdata 0x70 (112) 0x10 (16) } [Index] Name Value Sclass Symtype [0] x 0x70 SData Global [1] code 0x00 Text Proc [2] srand 0x00 Undefined Proc class11.ppt – 22 – CS 213 F’98 Relocation entries (code.o) start size int x=0xfe; .text 0x00 (0) 0x40 (64) code(int *p) { .lita 0x60 (96) 0x10 (16) srand(*p); .sdata 0x70 (112) 0x10 (16) } 0x0: lda sp, -64(sp) Vaddr Symndx Type Extern Name 0x4: ldah gp, 1(t12) 0x8: lda gp, -32688(gp) .text: 0xc: stq ra, 0(sp)