24
Linking and Loading Linking and Loading Fred Prussack Fred Prussack CS 518 CS 518

Linking and Loading

  • Upload
    libba

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Linking and Loading. Fred Prussack CS 518. L&L: Overview. Wake-up Questions Terms and Definitions / General Information Loading Linking Static vs. Dynamic ELF Other Linking Information/Processing References. L&L: Topics Not Covered. Windows Alternate binary file formats - PowerPoint PPT Presentation

Citation preview

Page 1: Linking and Loading

Linking and LoadingLinking and Loading

Fred PrussackFred Prussack

CS 518CS 518

Page 2: Linking and Loading

L&L: OverviewL&L: Overview

Wake-up QuestionsWake-up QuestionsTerms and Definitions / General Terms and Definitions / General InformationInformationLoadingLoadingLinkingLinking– Static vs. DynamicStatic vs. Dynamic– ELFELF– Other Linking Information/ProcessingOther Linking Information/Processing

ReferencesReferences

Page 3: Linking and Loading

L&L: Topics Not CoveredL&L: Topics Not Covered

WindowsWindows

Alternate binary file formatsAlternate binary file formats

Different versions of glibcDifferent versions of glibc

Different versions of the kernel (from 2.4)Different versions of the kernel (from 2.4)

Page 4: Linking and Loading

L&L: Questions for the SleepyL&L: Questions for the Sleepy

What is the name of the compile time What is the name of the compile time linker in Linux?linker in Linux?– ldld

What is the name of the run-time linker in What is the name of the run-time linker in Linux?Linux?– ld.sold.so

Where is the loader located in Linux?Where is the loader located in Linux?– Part kernel / part ld.soPart kernel / part ld.so

Page 5: Linking and Loading

L&L: TermsL&L: Terms

LinkingLinking– Taking object files and creating loadable Taking object files and creating loadable

modules with correct references to required modules with correct references to required libraries, data, and procedureslibraries, data, and procedures

LoadingLoading– Placing a program image into main memory Placing a program image into main memory

for executionfor execution

Page 6: Linking and Loading

L&L: General InformationL&L: General Information

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

Page 7: Linking and Loading

L&L: Static vs. DynamicL&L: Static vs. Dynamic

Fully statically compiled executables Fully statically compiled executables – Provide for faster load->execution time due to Provide for faster load->execution time due to

no run-time linking requirementno run-time linking requirement– Generate larger executables requiring more Generate larger executables requiring more

disk spacedisk space

Executables with dynamic dependenciesExecutables with dynamic dependencies– Require run-time linking and thus potential Require run-time linking and thus potential

time implicationstime implications– Allows for easier and better code re-useAllows for easier and better code re-use

Page 8: Linking and Loading

L&L: LoadingL&L: Loading

do_execve()

search_binary_handler Searches all the registered binary handlers

load_elf_binary Loads current binary and elf interpreter

start_threadsets up correct registers

Question: What does the instruction pointer have in it now?

Answer: Entry point of the ELF interpreter

Page 9: Linking and Loading

L&L: Loading/LinkingL&L: Loading/Linking

At this point ld.so now has controlAt this point ld.so now has control

Determine what libraries need to be Determine what libraries need to be loaded for this binaryloaded for this binary

Determine dependencies for these Determine dependencies for these librarieslibraries

In what order are these loaded and what In what order are these loaded and what type of list is produced from this type of list is produced from this dependency list?dependency list?

Page 10: Linking and Loading

L&L: LinkingL&L: Linking

Basic job to clean up unresolved symbolsBasic job to clean up unresolved symbols

At compile time this can be accomplished At compile time this can be accomplished by executing ld with object files to produce by executing ld with object files to produce an executablean executable

At run-time this is accomplished by loading At run-time this is accomplished by loading all required shared libraries (.so’s) and all required shared libraries (.so’s) and fixing unresolved symbols found in the fixing unresolved symbols found in the librarieslibraries

Page 11: Linking and Loading

L&L: Dynamic LinkingL&L: Dynamic Linking

Load Time Dynamic Linking Done By ld.soLoad Time Dynamic Linking Done By ld.so– Most likely on your system it is ld-linux.so which links Most likely on your system it is ld-linux.so which links

to ld-2.3.2.soto ld-2.3.2.so– All possibly resolved symbols are resolved during All possibly resolved symbols are resolved during

compilation/first link (run of ld). Remaining unresolved compilation/first link (run of ld). Remaining unresolved symbols are done at time of loadsymbols are done at time of load

Lazy Binding (LD_BIND_NOW)Lazy Binding (LD_BIND_NOW)

Run Time Dynamic (inline) LinkingRun Time Dynamic (inline) Linking– Allows applications to, during run time, open shared Allows applications to, during run time, open shared

object files and execute their functionsobject files and execute their functions<dlfcn.h><dlfcn.h>

Page 12: Linking and Loading

L&L: L&L: [more] [more] Questions for the SleepyQuestions for the Sleepy

In what package is ld.so distributed and In what package is ld.so distributed and built from?built from?– GlibcGlibc

Can gcc be made to not link files Can gcc be made to not link files automatically?automatically?– Yes, of course! Use the –c option.Yes, of course! Use the –c option.

Page 13: Linking and Loading

L&L: ELF File FormatL&L: ELF File Format

Currently the standard binary format for Linux Currently the standard binary format for Linux since the late 90’s. Created in late 80’s.since the late 90’s. Created in late 80’s.Three types of object filesThree types of object files– Shared Object Files (.so; shared object file)Shared Object Files (.so; shared object file)– Relocatable Object Files (.o; object file)Relocatable Object Files (.o; object file)– Executable Object Files (executable binary file)Executable Object Files (executable binary file)

First 4 characters of this type of file is First 4 characters of this type of file is [backspace (ascii 127)]ELF[backspace (ascii 127)]ELF

Page 14: Linking and Loading

L&L: ELF File FormatL&L: ELF File Format

First the ELF Header – 52 bytes in length on a First the ELF Header – 52 bytes in length on a 32 bit system32 bit systemSections and Segments for libraries and binariesSections and Segments for libraries and binariesVarious ELF segmentsVarious ELF segments– text: program instructionstext: program instructions– data: initialized datadata: initialized data– plt: procedure linkage tableplt: procedure linkage table– got: global offset tablegot: global offset table

Checking for NEEDED entries in the dynamic Checking for NEEDED entries in the dynamic segment will let ld know what it needs to loadsegment will let ld know what it needs to load

Page 15: Linking and Loading

L&L: ld.so & Library LocationL&L: ld.so & Library Location

ld.so must be able to correctly locate the ld.so must be able to correctly locate the identified libraries in the executable. It does this identified libraries in the executable. It does this by looking for them in the following order:by looking for them in the following order:– DT_RPATH (-rpath-link option)DT_RPATH (-rpath-link option)

Section in ELF fileSection in ELF file

– LD_LIBRARY_PATHLD_LIBRARY_PATHEnvironment VariableEnvironment Variable

– /etc/ld.so.cache/etc/ld.so.cacheCompiled list of files to loadCompiled list of files to load

– /lib; /usr/lib/lib; /usr/lib– /etc/ld.so.conf/etc/ld.so.conf

Page 16: Linking and Loading

L&L: ld.so processingL&L: ld.so processing

Loop all the program headers to find Loop all the program headers to find necessary infonecessary info– PHDR (program header): where the program PHDR (program header): where the program

headers start; This must be found first.headers start; This must be found first.– DYNAMIC: indicates where to find the DYNAMIC: indicates where to find the

dynamic segment (what must be loaded)dynamic segment (what must be loaded)NEEDED: Name of file neededNEEDED: Name of file needed

– INTERP: used to find the interpreter – which INTERP: used to find the interpreter – which generally turns out to be ld.sogenerally turns out to be ld.so

Page 17: Linking and Loading

L&L: ld.so processingL&L: ld.so processing

Load all required libraries found in Load all required libraries found in NEEDED portions of the DYNAMIC NEEDED portions of the DYNAMIC segmentsegment

Get all necessary information from Get all necessary information from librarylibrary– Dynamic header; phdr; load headerDynamic header; phdr; load header

Page 18: Linking and Loading

L&L: ld.so infoL&L: ld.so info

Read-Write

Read-Only

Page 19: Linking and Loading

L&L: ld.so processingL&L: ld.so processing

What about when we actually call a What about when we actually call a function that hasn’t been loaded?function that hasn’t been loaded?– First need to resolve addressing issuesFirst need to resolve addressing issues– Probably best to permanently fix themProbably best to permanently fix them– Then we need to call the actual procedureThen we need to call the actual procedure

Page 20: Linking and Loading

L&L: ld.so processingL&L: ld.so 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

Page 21: Linking and Loading

L&L: ld.so misc. infoL&L: ld.so misc. info

You can run ld.so from the command You can run ld.so from the command line with an executableline with an executable– This provides a great ability to test out new This provides a great ability to test out new

ld.so’s if necessaryld.so’s if necessary– /lib/ld-linux.so [executable [args…]]/lib/ld-linux.so [executable [args…]]

Page 22: Linking and Loading

L&L: linking helper toolsL&L: linking helper tools

ldd – list the dynamic dependanciesldd – list the dynamic dependancies

readelf – displays information from ELF filesreadelf – displays information from ELF files

objdump – show information from object filesobjdump – show information from object files

nm – show symbol information from object filesnm – show symbol information from object files

strip – removes symbols from object filesstrip – removes symbols from object files

LD_DEBUG/LD_DEBUG_OUTPUT – shows LD_DEBUG/LD_DEBUG_OUTPUT – shows debug output from ld.sodebug output from ld.so

Page 23: Linking and Loading

L&L: ReferencesL&L: ReferencesStallings, William. Operating Systems Internals and Design Principles, 4Stallings, William. Operating Systems Internals and Design Principles, 4 thth Edition. Upper Saddle Edition. Upper Saddle River, NJ: Prentice-Hall, 2001River, NJ: Prentice-Hall, 2001

http://efrw01.frascati.enea.it/Software/Unix/IstrFTU/cern-cnl-2001-http://efrw01.frascati.enea.it/Software/Unix/IstrFTU/cern-cnl-2001-003-25-link.html003-25-link.htmlhttp://www.iecc.com/linker/linker10.htmlhttp://www.iecc.com/linker/linker10.htmlhttp://www.ibiblio.org/oswg/oswg-nightly/oswg/en_GB.ISO_8859-1/http://www.ibiblio.org/oswg/oswg-nightly/oswg/en_GB.ISO_8859-1/books/linux-c-programming/GCC-HOWTO/x796.htmlbooks/linux-c-programming/GCC-HOWTO/x796.htmlhttp://linux.about.com/library/cmd/blcmdl2_execve.htmhttp://linux.about.com/library/cmd/blcmdl2_execve.htmhttp://www.iecc.com/linker/http://www.iecc.com/linker/http://www.suse.de/~bastian/Export/linking.txthttp://www.suse.de/~bastian/Export/linking.txthttp://linux.about.com/library/cmd/blcmdl8_ld.so.htmhttp://linux.about.com/library/cmd/blcmdl8_ld.so.htmhttp://www.linuxjournal.com/node/6463http://www.linuxjournal.com/node/6463http://www.ibiblio.org/oswg/oswg-nightly/oswg/en_GB.ISO_8859-1/bhttp://www.ibiblio.org/oswg/oswg-nightly/oswg/en_GB.ISO_8859-1/books/linux-c-programming/GCC-HOWTO/x575.htmlooks/linux-c-programming/GCC-HOWTO/x575.html

Page 24: Linking and Loading

L&L: References (cont.)L&L: References (cont.)

http://www.moses.uklinux.net/patches/lki-single.htmlhttp://www.moses.uklinux.net/patches/lki-single.htmlhttp://whatis.techtarget.com/definition/0,,sid9_gci212493,00.htmlhttp://whatis.techtarget.com/definition/0,,sid9_gci212493,00.htmlhttp://encyclopedia.thefreedictionary.com/position%20independenthttp://encyclopedia.thefreedictionary.com/position%20independent%20code%20codehttp://www.faqs.org/docs/Linux-HOWTO/Program-Library-http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.htmlHOWTO.htmlhttp://sources.redhat.com/autobook/autobook/autobook_71.htmlhttp://sources.redhat.com/autobook/autobook/autobook_71.htmlhttp://www.educ.umu.se/~bjorn/linux/howto/ELF-HOWTO-1.htmlhttp://www.educ.umu.se/~bjorn/linux/howto/ELF-HOWTO-1.htmlhttp://www.tcfs.it/docs/manpages/BSD/gcc-howto-6.htmlhttp://www.tcfs.it/docs/manpages/BSD/gcc-howto-6.htmlhttp://www.cs.ucdavis.edu/~haungs/paper/http://www.cs.ucdavis.edu/~haungs/paper/http://www-106.ibm.com/developerworks/linux/library/l-dll.html?http://www-106.ibm.com/developerworks/linux/library/l-dll.html?dwzone=linuxdwzone=linux