Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.

Similar presentations

Presentation on theme: "Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic."— Presentation transcript:

1 Linking and Loading Fred Prussack CS 518

2 L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic –ELF –Other Linking Information/Processing References

3 L&L: Topics Not Covered Windows Alternate binary file formats Different versions of glibc Different versions of the kernel (from 2.4)

4 L&L: Questions for the Sleepy What is the name of the compile time linker in Linux? –ld What is the name of the run-time linker in Linux? – Where is the loader located in Linux? –Part kernel / part

5 L&L: Terms Linking –Taking object files and creating loadable modules with correct references to required libraries, data, and procedures Loading –Placing a program image into main memory for execution

6 L&L: General Information Static libraries generally named xxx.a (archives) Dynamic libraries generally named (shared objects) Object files generally named xxx.o ELF: Executable and Linking Format. Currently the most common object file format on Linux systems. Other formats: a.out, COFF, etc…

7 L&L: Static vs. Dynamic Fully statically compiled executables –Provide for faster load->execution time due to no run-time linking requirement –Generate larger executables requiring more disk space Executables with dynamic dependencies –Require run-time linking and thus potential time implications –Allows for easier and better code re-use

8 L&L: Loading do_execve() search_binary_handler Searches all the registered binary handlers load_elf_binary Loads current binary and elf interpreter start_thread sets up correct registers Question: What does the instruction pointer have in it now? Answer: Entry point of the ELF interpreter

9 L&L: Loading/Linking At this point now has control Determine what libraries need to be loaded for this binary Determine dependencies for these libraries In what order are these loaded and what type of list is produced from this dependency list?

10 L&L: Linking Basic job to clean up unresolved symbols At compile time this can be accomplished by executing ld with object files to produce an executable At run-time this is accomplished by loading all required shared libraries (.so’s) and fixing unresolved symbols found in the libraries

11 L&L: Dynamic Linking Load Time Dynamic Linking Done By –Most likely on your system it is which links to –All possibly resolved symbols are resolved during compilation/first link (run of ld). Remaining unresolved symbols are done at time of load Lazy Binding (LD_BIND_NOW) Run Time Dynamic (inline) Linking –Allows applications to, during run time, open shared object files and execute their functions

12 L&L: [more] Questions for the Sleepy In what package is distributed and built from? –Glibc Can gcc be made to not link files automatically? –Yes, of course! Use the –c option.

13 L&L: ELF File Format Currently the standard binary format for Linux since the late 90’s. Created in late 80’s. Three types of object files –Shared Object Files (.so; shared object file) –Relocatable Object Files (.o; object file) –Executable Object Files (executable binary file) First 4 characters of this type of file is [backspace (ascii 127)]ELF

14 L&L: ELF File Format First the ELF Header – 52 bytes in length on a 32 bit system Sections and Segments for libraries and binaries Various ELF segments –text: program instructions –data: initialized data –plt: procedure linkage table –got: global offset table Checking for NEEDED entries in the dynamic segment will let ld know what it needs to load

15 L&L: & Library Location must be able to correctly locate the identified libraries in the executable. It does this by looking for them in the following order: –DT_RPATH (-rpath-link option) Section in ELF file –LD_LIBRARY_PATH Environment Variable –/etc/ Compiled list of files to load –/lib; /usr/lib –/etc/

16 L&L: processing Loop all the program headers to find necessary info –PHDR (program header): where the program headers start; This must be found first. –DYNAMIC: indicates where to find the dynamic segment (what must be loaded) NEEDED: Name of file needed –INTERP: used to find the interpreter – which generally turns out to be

17 L&L: processing Load all required libraries found in NEEDED portions of the DYNAMIC segment Get all necessary information from library –Dynamic header; phdr; load header

18 L&L: info Read-Write Read-Only

19 L&L: processing What about when we actually call a function that hasn’t been loaded? –First need to resolve addressing issues –Probably best to permanently fix them –Then we need to call the actual procedure

20 L&L: processing PLT0: pushl GOT + 4 jmp *GOT + 8 PLTN: jmp *GOT+n push #reloc_offset jmp PLT0 Procedure Start Loc reloc_offset library Next Procedure Run Routine to fix GOT then jump to procedure after locating correct symbol Stack Question: What is the name of the fix routine? Answer: fixup

21 L&L: misc. info You can run from the command line with an executable –This provides a great ability to test out new’s if necessary –/lib/ [executable [args…]]

22 L&L: linking helper tools ldd – list the dynamic dependancies readelf – displays information from ELF files objdump – show information from object files nm – show symbol information from object files strip – removes symbols from object files LD_DEBUG/LD_DEBUG_OUTPUT – shows debug output from

23 L&L: References Stallings, William. Operating Systems Internals and Design Principles, 4 th Edition. Upper Saddle River, NJ: Prentice-Hall, 2001 003-25-link.html 1/books/linux-c-programming/GCC-HOWTO/x796.html 1/books/linux-c-programming/GCC-HOWTO/x575.html 1/books/linux-c-programming/GCC-HOWTO/x575.html

24 L&L: References (cont.),,sid9_gci212493,00.html 20code HOWTO.html dll.html?dwzone=linux

Download ppt "Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic."

Similar presentations

Ads by Google