Unformatted text preview:

Systems I Linking II Topics Relocation Static libraries Loading Dynamic linking of shared libraries Relocating Symbols and Resolving External References Symbols are lexical entities that name functions and variables Each symbol has a value typically a memory address Code consists of symbol definitions and references References can be either local or external m c Def of local symbol e int e 7 int main int r a exit 0 Def of local symbol Ref to external symbol exit Ref to external ep defined in symbol a libc so a c extern int e int ep e int x 15 int y int a return ep x y Ref to external symbol e Defs of local symbols x and y Def of Refs of local local symbols ep x y symbol a 2 m o Relocation Info m c int e 7 Disassembly of section text int main int r a exit 0 00000000 main 00000000 main 0 55 pushl ebp 1 89 e5 movl esp ebp 3 e8 fc ff ff ff call 4 main 0x4 4 R 386 PC32 a 8 6a 00 pushl 0x0 a e8 fc ff ff ff call b main 0xb b R 386 PC32 exit f 90 nop Disassembly of section data 00000000 e 0 07 00 00 00 source objdump 3 a o Relocation Info text a c extern int e Disassembly of section text int ep e int x 15 int y 00000000 a 0 55 1 8b 15 00 00 00 6 00 int a return ep x y 7 c e 10 12 17 18 19 a1 00 00 00 00 89 03 89 03 00 5d c3 e5 02 ec 05 00 00 00 pushl movl ebp 0x0 edx 3 R 386 32 ep movl 0x0 eax 8 R 386 32 x movl esp ebp addl edx eax movl ebp esp addl 0x0 eax 14 R 386 32 popl ebp ret y 4 a o Relocation Info data a c extern int e int ep e int x 15 int y int a return ep x y Disassembly of section data 00000000 ep 0 00 00 00 00 0 R 386 32 e 00000004 x 4 0f 00 00 00 5 Executable After Relocation and External Reference Resolution text 08048530 main 8048530 55 8048531 89 8048533 e8 8048538 6a 804853a e8 804853f 90 08048540 a 8048540 8048541 8048546 8048547 804854c 804854e 8048550 8048552 8048557 8048558 8048559 55 8b 08 a1 89 03 89 03 08 5d c3 pushl movl call pushl call nop ebp esp ebp 8048540 a 0x0 8048474 init 0x94 15 1c a0 04 pushl movl ebp 0x804a01c edx 20 a0 04 08 e5 02 ec 05 d0 a3 04 movl movl addl movl addl 0x804a020 eax esp ebp edx eax ebp esp 0x804a3d0 eax popl ret ebp e5 08 00 00 00 00 35 ff ff ff 6 Executable After Relocation and External Reference Resolution data m c int e 7 int main int r a exit 0 a c extern int e Disassembly of section data 0804a018 e 804a018 07 00 00 00 0804a01c ep 804a01c 18 a0 04 08 0804a020 x 804a020 0f 00 00 00 int ep e int x 15 int y int a return ep x y 7 Strong and Weak Symbols Program symbols are either strong or weak strong procedures and initialized globals weak uninitialized globals p1 c p2 c strong int foo 5 int foo strong p1 p2 weak strong 8 Linker s Symbol Rules Rule 1 A strong symbol can only appear once Rule 2 A weak symbol can be overridden by a strong symbol of the same name references to the weak symbol resolve to the strong symbol Rule 3 If there are multiple weak symbols the linker can pick an arbitrary one 9 Linker Puzzles int x p1 p1 int x p1 int x p2 int x int y p1 double x p2 int x 7 int y 5 p1 double x p2 int x 7 p1 int x p2 10 Linker Puzzles int x p1 p1 int x p1 int x p2 References to x will refer to the same uninitialized int Is this what you really want int x int y p1 double x p2 Writes to x in p2 might overwrite y Evil int x 7 int y 5 p1 double x p2 int x 7 p1 int x p2 Link time error two strong symbols p1 Writes to x in p2 will overwrite y Nasty References to x will refer to the same initialized variable Nightmare scenario two identical weak structs compiled by different compilers with different alignment rules 11 Packaging Commonly Used Functions How to package functions commonly used by programmers Math I O memory management string manipulation etc Awkward given the linker framework so far Option 1 Put all functions in a single source file Programmers link big object file into their programs Space and time inefficient Option 2 Put each function in a separate source file Programmers explicitly link appropriate binaries into their programs More efficient but burdensome on the programmer Solution static libraries a archive files Concatenate related relocatable object files into a single file with an index called an archive Enhance linker so that it tries to resolve unresolved external references by looking for the symbols in one or more archives If an archive member file resolves reference link into executable 12 Static Libraries archives p1 c p2 c Translator Translator p1 o p2 o libc a static library archive of relocatable object files concatenated into one file Linker ld p 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 by packaging commonly used functions e g C standard library libc math library libm Linker selects only the o files in the archive that are actually needed by the program 13 Creating Static Libraries atoi c printf c Translator Translator atoi o printf o random c random o Archiver ar libc a Translator ar rs libc a atoi o printf o random o C standard library Archiver allows incremental updates Recompile function that changes and replace o file in archive 14 Commonly Used Libraries libc a the C standard library 8 MB archive of 900 object files I O memory allocation signal handling string handling data and time random numbers integer math libm a the C math library 1 MB archive of 226 object files floating point math sin cos tan log exp sqrt ar t usr lib libc a sort fork o fprintf o fpu control o fputc o freopen o fscanf o fseek o fstab o ar t usr lib libm a sort e acos o e acosf o e acosh o e acoshf o e acoshl o e acosl o e asin o e asinf o e asinl o 15 Using Static Libraries Linker s algorithm for resolving external references Scan o files and a files in the command line order During the scan keep a list of the current unresolved references As each new o or a file obj is encountered try to resolve each unresolved reference …


View Full Document

UT CS 429H - Lecture Notes

Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?