Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSU0027 - System Programming, NTNU CSIE1 / 99 Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin."— Presentation transcript:

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

2 CSU0027 - System Programming, NTNU CSIE2 / 99 Agenda Introduction Static-Linking Libraries: Build & Usage Dynamic-Linking and ELF Dynamic-Linking Libraries: Build & Usage Windows Platform Approach

3 CSU0027 - System Programming, NTNU CSIE3 / 99 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.

4 CSU0027 - System Programming, NTNU CSIE4 / 99 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.

5 CSU0027 - System Programming, NTNU CSIE5 / 99 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.

6 CSU0027 - System Programming, NTNU CSIE6 / 99 Linkers Module Find the library routines and determine the addresses at runtime.

7 CSU0027 - System Programming, NTNU CSIE7 / 99 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

8 CSU0027 - System Programming, NTNU CSIE8 / 99 Categories of Libraries (by linking time) Static linking libraries Dynamic linking libraries –Run-Time Environment libraries –Programming Language libraries

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

10 CSU0027 - System Programming, NTNU CSIE10 / 99 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.

11 CSU0027 - System Programming, NTNU CSIE11 / 99 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.

12 CSU0027 - System Programming, NTNU CSIE12 / 99 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

13 CSU0027 - System Programming, NTNU CSIE13 / 99 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++.

14 CSU0027 - System Programming, NTNU CSIE14 / 99 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.

15 CSU0027 - System Programming, NTNU CSIE15 / 99 End of Introduction

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

17 CSU0027 - System Programming, NTNU CSIE17 / 99 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:

18 CSU0027 - System Programming, NTNU CSIE18 / 99 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 ˇ ˇ

19 CSU0027 - System Programming, NTNU CSIE19 / 99 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)

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

21 CSU0027 - System Programming, NTNU CSIE21 / 99 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

22 CSU0027 - System Programming, NTNU CSIE22 / 99 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

23 CSU0027 - System Programming, NTNU CSIE23 / 99 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

24 CSU0027 - System Programming, NTNU CSIE24 / 99 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

25 CSU0027 - System Programming, NTNU CSIE25 / 99 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.

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

27 CSU0027 - System Programming, NTNU CSIE27 / 99 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.

28 CSU0027 - System Programming, NTNU CSIE28 / 99 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.

29 CSU0027 - System Programming, NTNU CSIE29 / 99 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)

30 CSU0027 - System Programming, NTNU CSIE30 / 99 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/

31 CSU0027 - System Programming, NTNU CSIE31 / 99 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

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

33 CSU0027 - System Programming, NTNU CSIE33 / 99 References Wikipedia Linkers and Loaders Google


Download ppt "CSU0027 - System Programming, NTNU CSIE1 / 99 Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin."

Similar presentations


Ads by Google