c4-object-files An introduction of unix object files 20011029 We will tear down a "complete" compile suich as: % gcc -o prop prog.c -lm into the substeps that are preformed. C language source file prog.c C language header file(s) stdio.h math.h Assembly language source file prog.s unlinked Object file prog.o executable file prog shared object library file(s) libc.so.1 libm.so.1 A few "words of wisdom": 1) A Compier "compiles"! (J.M. 1970) 2) An Assembler "assembles"! (J.M. 1970) 3) A Linker "links"! (J.M. 1970) 4) Only exec() "executes" (J.K. 2001) Action Command input output --------- --------------- ---------- -------- compile gcc -s prog.c prog.c, prog.s stdio.h math.h assemble gcc -c prog.s prog.s prog.o as -c prog.s link gcc -o prog prog.o -lm prog.o, prog ld -o prog prog.o -lc -lm libc.so.1, libm.so.1 execute % prog prog a new process (in the execute, the shell makes a fork() call and the child shell then effectivly makes an execlp("prog", "prog", (char *)NULL) call while the parent shell waits for the child to terminate.) ------------------------------------------------------------------ Generaic Object/Executable FIle Format +-------------------------+ | Header | | source of file | | date | | offsets to parts below | |-------------------------| | Text Section | | prog machine code, | | some must be modified | | when placed into | | memory to execute | |-------------------------| | Data Section | | prog initialized data, | | some must be modified | | when placed into | | memory to execute | |-------------------------| | Text Relocation Section | | details of what to | | modify in Text Section | |-------------------------| | Data Relocation Section | | details of what to | | modify in data Section | |-------------------------| | Symbol Table | | globals undefined here | | globals defined here | | location of strings | |-------------------------| | String Table | | strings | +-------------------------+ The exec(2) calls read in such "executable" object files and use them to overwrite the calling process's text and data segments. Thsi effectively has the calling process "run" the compiled program. dump(1) may be used to "dump" selected parts of an object files (including executable objects). ------------------------------------------------------------------ Static linking All library functions are taken out of the libraries at link time an included in the executable. After linking the libraries could be "removed", the executable would still work. Dynamic Linking Library functions are checked for in the dynamic libraries at link time, but are not included in the executable. Only an indications of which dynamic library file each needed function is in is included.S When the executable is run, the needed library functions are taken out of the dynamic libraries and the exec() function operates. If after linking the dynamic libraries are "removed", the executable will not work. Shared Objects Two processes that need the same library function can share a copy in memory, since the text segment can be read only. ------------------------------------------------------------------ UNIX: (man gcc(1) for linking options) static library files libc.a libm.a shared object library files libc.so.1 libm.so.1 All modern Operating Systems support Shared Objects and Dynamic Linking, which is usually the default. Consider the two cases (static vs dyanmic/shared oboject) with respect to: speed: compile, link, executte space: disk: object files executable files memory: running processes List Advantages of static linking List Advantages of dynamic/shared object linking