1 / 33

Linkers and Libraries

Linkers and Libraries. Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin. Agenda. Introduction Static-Linking Libraries: Build & Usage Dynamic-Linking and ELF Dynamic-Linking Libraries: Build & Usage Windows Platform Approach. Libraries.

ion
Download Presentation

Linkers and Libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin CSU0027 - System Programming, NTNU CSIE

  2. Agenda • Introduction • Static-Linking Libraries: Build & Usage • Dynamic-Linking and ELF • Dynamic-Linking Libraries: Build & Usage • Windows Platform Approach CSU0027 - System Programming, NTNU CSIE

  3. Libraries • A library is a collection of subprograms used to develop software. • Allows code and data to be reused, shared and changed in a modular fashion. • Linking: A linker resolves the references between executables and libraries. CSU0027 - System Programming, NTNU CSIE

  4. Benefits of Using Libraries • Software Engineering Perspective: • Increasing the reusability of common routines. • Easy to upgrade by changing the libraries only. • System Utilization Perspective: • The code segment can be sharing at runtime; decrease the consume of memory and disk space. CSU0027 - System Programming, NTNU CSIE

  5. Naming and Binding • Name Binding: The association of values with identifiers. • An identifier bound to a value is said to reference that value. • Reference Resolving: Retrieving the value/address by a reference. • Note: Different to the reference resolving, memory-mapping is retrieving the memory address by another memory address. CSU0027 - System Programming, NTNU CSIE

  6. Linkers • Module • Find the library routines and determine the addresses at runtime. CSU0027 - System Programming, NTNU CSIE

  7. Print Shared Library Dependencies • UNIX Platform > ldd /bin/bash linux-gate.so.1 => (0xffffe000) libncurses.so.5 => /lib/libncurses.so.5 (0xb7f1d000) libdl.so.2 => /lib/libdl.so.2 (0xb7f19000) libc.so.6 => /lib/libc.so.6 (0xb7dfb000) /lib/ld-linux.so.2 (0xb7f61000) • Windows Platform • PE Viewer or PE Explorer • PE: Portable Executable CSU0027 - System Programming, NTNU CSIE

  8. Categories of Libraries(by linking time) • Static linking libraries • Dynamic linking libraries • Run-Time Environment libraries • Programming Language libraries CSU0027 - System Programming, NTNU CSIE

  9. From Source to Execution Source Program Assembly Code Assembler as / gcc -c Preprocessor cpp / gcc -E Compiler cc1 / gcc -S dynamic-linking static-linking Object Program Loader Linker ld / gcc -l Executables CSU0027 - System Programming, NTNU CSIE

  10. Static Linking Libraries • The code segments will be copy to each executables. • Pros: • Easy to use; no dependency problem after compilation. • Cons: • The executable size will be larger. • Require re-linking when libraries changed. CSU0027 - System Programming, NTNU CSIE

  11. Dynamic Linking Libraries (1/2) • Allow multiple processes to share the same code segment. • Pros: • Greater flexibility • Possible support for plugins. • Cons: • Slow application at start time. • Dependent on the libraries when execution. CSU0027 - System Programming, NTNU CSIE

  12. Dynamic Linking Libraries (2/2) • The references can be resolved either at: • Load-time • Run-time • UNIX Platform • “shared-object”: *.so • Windows Platform • “dynamic-linking library”: *.dll CSU0027 - System Programming, NTNU CSIE

  13. Location of Libraries • UNIX Platform • /lib: runtime environment libraries • /usr/lib: for program development • Windows Platform • C:\WINDOWS\system32\ • The libraries for program development will be accompanies with compiler, like: Visual C++. CSU0027 - System Programming, NTNU CSIE

  14. Linking with C Runtime Libraries • Static Linking • gcc –static –o hello-s helloworld.c /usr/lib/libc.a • hello-s: 521,238 bytes • Dynamic Linking • gcc –o hello-d helloworld.c • hello-d: 6,020 bytes • The CRT libraries consume 515,218 bytes. CSU0027 - System Programming, NTNU CSIE

  15. End of Introduction CSU0027 - System Programming, NTNU CSIE

  16. Static-Linking Libraries • Build • gcc –c mystaticlib.c • ar –r mystaticlib.a mystaticlib.o • Usage • (same as using C runtime libraries) CSU0027 - System Programming, NTNU CSIE

  17. GNU Binary Utilities • strings: display all printable characters. • ar: create static-linking libraries. • nm: list symbols from object files. • size: list section sizes and total sizes. • readelf: • objdump: CSU0027 - System Programming, NTNU CSIE

  18. Unix a.out Format • Hardware MMU is required. • No relocation at load time. • Separate address space for code (I space) and data (D space), 64KB of each, in the later model of PDP-11 line. • Since a program couldn’t change its own I space, multiple copies of a single program could share a single copy of a program’s code. • Sharing at the runtime. a.out header text section ˇ data section ˇ text relocation data relocation symbol table string table CSU0027 - System Programming, NTNU CSIE

  19. ELF: Executable and Linking Format • To support cross-compilation, dynamic linking and other modern system features. • ELF has an associated debugging format called DWARF. • Three slightly different flavors: relocatable, executable, and shared object. • Dual natures: • A set of logical sections described by a section table. (for compilers, assemblers, and linkers) • A set of segments described by a program header table. (for loaders) CSU0027 - System Programming, NTNU CSIE

  20. Two Views of an ELF File Linkable Sections Executable Segments ELF Header (describes segments) Program Header Table (optional, ignored) .plt read only .text segments .got .data read/write sections .bss … … Section Header Table (optional, ignored) (describes sections) CSU0027 - System Programming, NTNU CSIE

  21. ELF Sections • .text: Equivalent to the a.out text segment. • .data: Equivalent to the a.out data segment. • .bss: Takes no space in the file but is allocated at runtime. • .init and .finit: Code to be executed when the program starts up or terminates, respectively. • Useful for C++ which has global data with initializers and finalizers. • .symtab CSU0027 - System Programming, NTNU CSIE

  22. ELF Sections There are 32 section headers, starting at offset 0xabc: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .interp PROGBITS 08048134 000134 000013 00 A 0 0 1 [ 2] .note.ABI-tag NOTE 08048148 000148 000020 00 A 0 0 4 [ 3] .hash HASH 08048168 000168 00002c 04 A 4 0 4 [ 4] .dynsym DYNSYM 08048194 000194 000060 10 A 5 1 4 [ 5] .dynstr STRTAB 080481f4 0001f4 00005e 00 A 0 0 1 [ 8] .rel.dyn REL 08048280 000280 000008 08 A 4 0 4 [ 9] .rel.plt REL 08048288 000288 000018 08 A 4 11 4 [10] .init PROGBITS 080482a0 0002a0 000017 00 AX 0 0 4 [11] .plt PROGBITS 080482b8 0002b8 000040 04 AX 0 0 4 [12] .text PROGBITS 08048300 000300 000184 00 AX 0 0 16 [13] .fini PROGBITS 08048484 000484 00001c 00 AX 0 0 4 [14] .rodata PROGBITS 080484a0 0004a0 000015 00 A 0 0 4 [15] .eh_frame PROGBITS 080484b8 0004b8 000004 00 A 0 0 4 [16] .ctors PROGBITS 080494bc 0004bc 000008 00 WA 0 0 4 [17] .dtors PROGBITS 080494c4 0004c4 000008 00 WA 0 0 4 [18] .jcr PROGBITS 080494cc 0004cc 000004 00 WA 0 0 4 [19] .dynamic DYNAMIC 080494d0 0004d0 0000c8 08 WA 5 0 4 [20] .got PROGBITS 08049598 000598 000004 04 WA 0 0 4 [21] .got.plt PROGBITS 0804959c 00059c 000018 04 WA 0 0 4 [22] .data PROGBITS 080495b4 0005b4 00000c 00 WA 0 0 4 [23] .bss NOBITS 080495c0 0005c0 000004 00 WA 0 0 4 [29] .shstrtab STRTAB 00000000 0009ae 00010c 00 0 0 1 [30] .symtab SYMTAB 00000000 000fbc 0004e0 10 31 55 4 [31] .strtab STRTAB 00000000 00149c 0002e8 00 0 0 1 CSU0027 - System Programming, NTNU CSIE

  23. Dynamic Linking • Static Shared Libraries • Dynamic Shared Libraries • (Propose the problems when using dynamic linking.) • Shared code in the runtime • Virtual Addressing Space • Position-Independent Code • cf: relocatable CSU0027 - System Programming, NTNU CSIE

  24. Dynamic Linking Library:File Header (/lib/libc.so.6) ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: DYN (Shared object file) Machine: Intel 80386 Version: 0x1 Entry point address: 0x159c0 Start of program headers: 52 (bytes into file) Start of section headers: 1194540 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 32 (bytes) Number of program headers: 11 Size of section headers: 40 (bytes) Number of section headers: 66 Section header string table index: 65 CSU0027 - System Programming, NTNU CSIE

  25. Position-Independent Code • The text pages of the file need not be relocated and thus can be shared among multiple process. • ELF linkers supports PIC with a global offset table (GOT) in each shared library. • GOT contains pointer to all the static data reference in the program. CSU0027 - System Programming, NTNU CSIE

  26. Position-Independent Code 0x08048000 0x08048000 main: ... call printf main: ... call printf program program printf: call GOT[5] 0x08048f44 PLT (r/o code) printf: ... ret libc ... [5]: &printf ... GOT (r/w data) Static Libraries 0x40001234 printf: ... ret libc Dynamic Shared Libraries CSU0027 - System Programming, NTNU CSIE

  27. Global Offset Table • To be an indirection table and accessed when PIC calls functions and operates on data variables. • It always consumes no space in executables and allocate memory when loading. • The values in GOT will be evaluated at runtime by linker. CSU0027 - System Programming, NTNU CSIE

  28. Procedure Linkage Table • For procedure only. • To support dynamic linking, each ELF shared libraries or executables that uses shared libraries has a PLT. • Adds a level of indirection for function calls analogous to that provided by the GOT for data. CSU0027 - System Programming, NTNU CSIE

  29. Dynamic Linking Library:Section Headers (/lib/libc.so.6) > readelf –S /lib/libc.so.6 There are 66 section headers, starting at offset 0x123a2c: Section Headers: [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [ 0] NULL 00000000 000000 000000 00 0 0 0 [ 1] .note.ABI-tag NOTE 00000194 000194 000020 00 A 0 0 4 [ 2] .hash HASH 000001b4 0001b4 0032d8 04 A 3 0 4 [ 3] .dynsym DYNSYM 0000348c 00348c 008b50 10 A 4 19 4 [ 4] .dynstr STRTAB 0000bfdc 00bfdc 0054e8 00 A 0 0 1 [ 8] .rel.dyn REL 00012904 012904 002c98 08 A 3 0 4 [ 9] .rel.plt REL 0001559c 01559c 000040 08 A 3 10 4 [10] .plt PROGBITS 000155dc 0155dc 000090 04 AX 0 0 4 [11] .text PROGBITS 00015670 015670 0de440 00 AX 0 0 16 [27] .data.rel.ro PROGBITS 00118060 117060 001cfc 00 WA 0 0 32 [28] .dynamic DYNAMIC 00119d5c 118d5c 0000f0 08 WA 4 0 4 [29] .got PROGBITS 00119e4c 118e4c 0001a8 04 WA 0 0 4 [30] .got.plt PROGBITS 00119ff4 118ff4 00002c 04 WA 0 0 4 [31] .data PROGBITS 0011a020 119020 00095c 00 WA 0 0 32 [32] .bss NOBITS 0011a980 11997c 002bfc 00 WA 0 0 32 Key to Flags: A (alloc), X (execute), W (write) CSU0027 - System Programming, NTNU CSIE

  30. Dynamic Linking Library • Build • gcc –fPIC –c –o myprnt.c myprnt.c • gcc –shared –o libmyprnt.so myprnt.o • Usage • gcc –c main.c • gcc –o main.out main.o –L. –lmyprnt • Runtime Environment Variable • export LD_LIBRARY_PATH = $LD_LIBRARY_PATH : /path/ CSU0027 - System Programming, NTNU CSIE

  31. GCC – GNU Compiler Collection • cpp: preprocess macros • cc1: perform semantic routines and translate into assembly language • as: assemble to relocatable object files • ld: linking • To view the commands executed to run the stages of compilation. • gcc -v CSU0027 - System Programming, NTNU CSIE

  32. From Source to Execution Source Program Assembly Code Assembler as / gcc -c Preprocessor cpp / gcc -E Compiler cc1 / gcc -S Object Program Loader Executables Linker ld / gcc -l CSU0027 - System Programming, NTNU CSIE

  33. References • Wikipedia • Linkers and Loaders • Google CSU0027 - System Programming, NTNU CSIE

More Related