1 / 26

Library

제 12 강 : Library. Library. Grouping *.o files. Why we need library. Binary functions are contained in *.o files System maintains many *.o files. “find binary for printf()”  Search all *.o files in system? Search time (disk) is t oo slow (must be < 1 sec!)

magdalenok
Download Presentation

Library

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. 제12강 : Library Library Grouping *.o files

  2. Why we need library • Binary functions are contained in *.o files • System maintains many *.o files. “find binary for printf()”  Searchall *.o files in system? • Search time (disk) is too slow (must be < 1 sec!) • smaller search space  “Grouping” C functions Object modules: printf() Looking for printf()? search only one library io-1.o scanf() io-2.o I/O library io-3.o Library sin() Graphics library trig.o cosin() geom.o tan() Math library poly.o

  3. Symbol table • {symbol, address, type, …} • used for linking & symbolic debugging • symbol table related commands: • strip removes symbol table (cc -s) • nm examine symbol table • try gcc –c my.c; nm my.o; (name value type) • ar merge *.o files into single archive (lib) file saves a lot of disk space also to transmit many files across machine (tar archives entire subtree, save network time ….) ex cd linux-practice/make_ex  add.c sub.c mul.c ….files cat add.c add.h main.c ex man strip nm add.o

  4. Static library Don’t have to be *.o file • Suffix .a for library (archive) • ar rcs libname.a a.o b.o c.o r include this (replace if exist) c silently (if not exist) s maintain table (symbol : file) x extract t print content of archive ex cc –c *.c ar rcs libx.a add.o sub.o ar t libx.a rm sub.o ar x libx.a sub.o ls /* now you see sub.o*/

  5. Convention - Static library • Library name • libhello.a, libc.a, libm.a, lib .a • Usually libraries are found under • /lib, /usr/lib, … • Linking • cc my.c -lx… (libc.a is default for cc) then cc searches /lib/libx.a for linking • full pathname, if not in system library directory • copy entire module not individual function

  6. Programming: my.c his.o his.h (his.c) my.c my.o #include his.h printf(); (use) a.out just before gcc his.h: my.o his.o extern int printf(); extern char y; (hint gcc) <lib/> entire module pre compiled stored in lib his.c int printf(); { ….} his.o (define)

  7. ls /lib | more ls /usr/include /usr/include/sys Separate Compile BUILD my.c coding #include he.h main() {printf(), EOF my.o gcc -lc object module a.out ld or cc -lc /include/he.h executable binary man printf #include he.h printf( ) #extern printf() #define EOF -1 /lib/libc.a he.o he.c ar r libc.a gcc -c object module printf() { ….. } <library function>

  8. /usr/include #extern x.h printf() cpp ld libx.a my.c use define /lib • Standard library header files • <…> /usr/include • <sys/…> /usr/include/sys • linear library (v.s. random library) • gcc looks for a function(3) – search method • /* has */ a.o: a( ) b.o: b( ) c.o: c( ) • /* calls */ a( ) => b( ) => c( ) • linear search library • User must arrange modules carefully (call-, order- sequence) • so that system may scan libraries only once. • Fast, User burden. • randomly search library • convenient, search overhead • Microsoft library

  9. tar • Functions < object modules (*.o) < library file (*.a) • Save entire subtree (ar – cannot save the subtree) • useful for • network transfer a subtree of files • backup • Can compress (-z option) • or use separate command (compress, gzip) .z compress .gz: gzip .tar.gz: tar compression for DOS .taz: compress for DOS

  10. tar command • tar cvf test.tar * c : create a new archive v : verbosely list files processed f : use archive file t : list the contents of an archive x : extract the files from an archive z : filter the archive through gzip

  11. tar exercise cd tar cvf test.tar * • Create a new archive ls –l • Check the archive file size gzip test.tar • Compress the archive file • “tar cvf test.tar *” and “gzip test.tar” are equivalent to “tar zcvf test.tar.gz *” ls –l • Check the compressed archive file size tar ztvf test.tar.gz • List the contents of the archive file

  12. Two Types of Libraries • Static library • functions are copied into a.out during gcc time • binding: compile time, linking time • suffix: .a • Shared library (DLL: Dynamically Linking Library) • functions are not copied during gcc time. Just map. • Function is linked when program starts running • in-core pages are shared among processes • binding: run time • suffix .so • default linking (eg /lib/libc.so) • Window allows any program to write DLL! null

  13. : a.out If printf() is *.a library : *.o (printf()) printf() Memory my code my code printf() printf() library libc.a his code printf() printf() his code user code Each a.out has a copy Binding at cc ld time printf() printf()

  14. : a.out DLL library : *.o (printf()) printf() Memory my code my code his code share single copy printf() user code loaded on demand his code a.out does not have copy Binding at run time DLL library printf()

  15. : a.out Update in *.a library : *.o (printf()) printf() Memory my code my code printf() printf() Relink & rebuild all a.out’s his code static library printf() printf() his code Updated? user code printf() If Million a.out have printf()? printf()

  16. : a.out Update in *.a library : *.o (printf()) printf() Memory my code my code printf() printf() Relink & rebuild all a.out’s printf() printf() his code static library printf() printf() printf() printf() his code Updated? user code printf() printf() If Million a.out have printf()? printf() printf()

  17. : a.out Update in DLL library : *.o (printf()) printf() Memory my code my code his code share single copy a.out does not have copy Binding at run time printf() user code load recent copy his code DLL library printf() printf()

  18. Comparison • Advantage of shared library • library function update is automated • Many processes share one lib function in memory • eg many a.out’s calls printf() --- one copy shared • less memory space, fewer page faults • Disadvantage of shared library • Cannot give a.out to others who do not have same lib! • same shared library, same version? (dependency) • Slower -- page faults during run time (for lib calls) • Not for small, short-lived programs • Overhead for building up *.so runtime environment

  19. Backward Compatibility • If an a.out was built for .so library (version N), .so library version is also important for this a.out • A library is “backward compatible” if • a.out built for older version of library • library -> new version • old a.out still works with newer version of library? ’99 printf()  ’03 printf() worked works? a.out

  20. Name of Shared library • Suffix .so • so: “shared object” but plus some more • “soname”: • eg libc.so.5 • part 1 name of library: libc.so • part 2 version number: 5 (update if interface is changed) -Even longer names: • libc.so. 5.m.r -- minor version, release number

  21. a.out format magic number text size data size bss size symbol table size entry point relo. info. “Magic number” first few bytes of executable file Script If first 2 bytes is # ! – pathname of interpreter (from BSD - to Linux) Executable Binary a.out format: COFF  ELF(support dynamic libraries)

  22. Building Shared Library • Compile gcc -fPIC option • Insert into library: • gcc -shared -Wl.-soname,soname-o lib_nameobj_fileslibs gcc do not use ld Wl pass options to ld libs other libraries that provide symbols accessed here (such as C library -lc) gcc -shared -Wl,-soname,-libmy.so.1-o libmy.so.1.0.1a.o b.o-lc • Installthe library. [soname]

  23. Upgrading Shared library • To upgrade shared library • from (libc.so.5.2.13) to (libc.so.5.3.11) • put new library into /lib directory • run ldconfig command, which • make symbolic link from soname to latest library (5.3.11) • old library (5.2.13) is removed • Later, “gcc my.c” looks for soname “/lib/libc.so” • this is linked to latest library (5.3.11) thru symbolic link

  24. gcc my.c preprocessing cpp or cc -E .c .i compile c0 c1 or cc -S .s assembling as or cc -c .o ex cd linux-practice cd make_ex gcc –E add.c ls gcc –S add.c cat add.s linking ld or cc a.out

  25. Keywords • object module *.o • executable binary a.out • extern • define/declaration • #include • preprocessing

  26. Study UNIX kernel first(option)

More Related