Unformatted text preview:

Linking ITopicsTopics Assembly and symbol resolution Static linkingSystems I2A Simplistic Program TranslationSchemeProblems:• Efficiency: small change requires complete recompilation• Modularity: hard to share common functions (e.g. printf)Solution:• Static linker (or linker)Translatorm.cpASCII source fileBinary executable object file(memory image on disk)Translatorm.sCompilerAssembler3A Better Scheme Using a LinkerLinker (ld)Translatorsm.cm.oTranslatorsa.ca.opSeparately compiledrelocatable object filesExecutable object file (contains codeand data for all functions defined in m.cand a.c)Translators Translatorsm.s a.sAssemblerCompiler4Translating the Example ProgramCompiler driverCompiler driver coordinates all steps in the translation coordinates all steps in the translationand linking process.and linking process. Typically included with each compilation system (e.g., gcc) Invokes preprocessor (cpp), compiler (cc1), assembler (as),and linker (ld). Passes command line arguments to appropriate phasesExample: create executable Example: create executable pp from from m.cm.c and and a.ca.c::bass> gcc -O2 -v -o p m.c a.c cpp [args] m.c /tmp/cca07630.i cc1 /tmp/cca07630.i m.c -O2 [args] -o /tmp/cca07630.s as [args] -o /tmp/cca076301.o /tmp/cca07630.s <similar process for a.c>ld -o p [system obj files] /tmp/cca076301.o /tmp/cca076302.o bass>5Compiling/AssemblingC CodeC Codedouble sum_loop(int val) { int sum = 0; double pi = 3.14; int i; for(i=3; i<=val; i++) { sum = sum + i; } return sum+pi;}Generated Assemblysum_loop: pushl %ebp movl %esp, %ebp movl 8(%ebp), %ecx movl $0, %edx cmpl $2, %ecx jle .L4 movl $0, %edx movl $3, %eax.L5: addl %eax, %edx addl $1, %eax cmpl %eax, %ecx jge .L5.L4: pushl %edx fildl (%esp) leal 4(%esp), %esp faddl .LC0 popl %ebp ret.LC0: .long 1374389535 .long 1074339512Obtain with commandgcc -O -S sum_loop.cProduces file code.s6Role of the AssemblerTranslateTranslate assembly codeassembly code into machine codeinto machine code Compiled or hand-generatedTranslate data into binary codes (using directives)Translate data into binary codes (using directives)Resolve symbolsResolve symbols Translate into relocatable offsetsError checkError check Syntax checking Ensure that constants are not too large for fields7Where did the labels go?Disassembled Object Code08048334 <sum_loop>: 8048334: 55 push %ebp 8048335: 89 e5 mov %esp,%ebp 8048337: 8b 4d 08 mov 0x8(%ebp),%ecx 804833a: ba 00 00 00 00 mov $0x0,%edx 804833f: 83 f9 02 cmp $0x2,%ecx 8048342: 7e 13 jle 8048357 <sum_loop+0x23> 8048344: ba 00 00 00 00 mov $0x0,%edx 8048349: b8 03 00 00 00 mov $0x3,%eax 804834e: 01 c2 add %eax,%edx 8048350: 83 c0 01 add $0x1,%eax 8048353: 39 c1 cmp %eax,%ecx 8048355: 7d f7 jge 804834e <sum_loop+0x1a> 8048357: 52 push %edx 8048358: db 04 24 fildl (%esp) 804835b: 8d 64 24 04 lea 0x4(%esp),%esp 804835f: dc 05 50 84 04 08 faddl 0x8048450 8048365: 5d pop %ebp 8048366: c3 ret8Label ResolutionDisassembled Object Code8048342: 7e 13 jle 8048357 <sum_loop+0x23>…8048355: 7d f7 jge 804834e <sum_loop+0x1a>…804835f: dc 05 50 84 04 08 faddl 0x8048450Byte relative offsets for Byte relative offsets for jle jle and and jgejge jle: 13 bytes forward jge: 9 bytes bytes backward (twoʼs comp. of xf7)Relocatable Relocatable absolute addressabsolute address faddl x80484509How does the assembler workOne passOne pass Record label definitions When use is found, compute offsetTwo passTwo pass Pass 1: scan for label instantiations - creates symbol table Pass 2: compute offsets from label use/def Can detect if computed offset is too large for assemblyinstruction10Symbol Table00000000 g F .text 00000033 sum_loopTracks location of symbols in object fileTracks location of symbols in object file Symbols that can be resolved need not be included Symbols that may be needed during linking must beincludedsymbol type(global)segmentoffset fromsegment startsymbolname11What Does a Linker Do?Merges object filesMerges object files Merges multiple relocatable (.o) object files into a single executableobject file that can loaded and executed by the loader.Resolves external referencesResolves external references As part of the merging process, resolves external references. External reference: reference to a symbol defined in another object file.Relocates symbolsRelocates symbols Relocates symbols from their relative locations in the .o files tonew absolute positions in the executable. Updates all references to these symbols to reflect their newpositions. References can be in either code or data» code: a(); /* reference to symbol a */» data: int *xp=&x; /* reference to symbol x */12Why Linkers?ModularityModularity Program can be written as a collection of smaller sourcefiles, rather than one monolithic mass. Can build libraries of common functions (more on this later) e.g., Math library, standard C libraryEfficiencyEfficiency Time: Change one source file, compile, and then relink. No need to recompile other source files. Space: Libraries of common functions can be aggregated into a singlefile... Yet executable files and running memory images contain onlycode for the functions they actually use.13Executable and Linkable Format(ELF)Standard binary format for object filesStandard binary format for object filesDerives from AT&T System V UnixDerives from AT&T System V Unix Later adopted by BSD Unix variants and LinuxOne unified format forOne unified format for Relocatable object files (.o), Executable object files Shared object files (.so)Generic name: ELF binariesGeneric name: ELF binariesBetter support for shared libraries than old Better support for shared libraries than old a.outa.out formats. formats.14ELF


View Full Document

UT CS 429H - Linking I

Download Linking I
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Linking I and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Linking I 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?