DOC PREVIEW
CMU CS 15410 - Lecture

This preview shows page 1-2-3-23-24-25-26-46-47-48 out of 48 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

15-410, S’041LinkingFebruary 13, 2004Some slides taken from 15-213 S’03 (Goldstein, Maggs).Some slides taken from 15-213 S’03 (Goldstein, Maggs).Original slides authored by Randy Bryant and Dave O’Hallaron.Original slides authored by Randy Bryant and Dave O’Hallaron.15-410“Nobody else reads these quotes anyway…”Dave EckhardtDave EckhardtBruce MaggsBruce Maggs15-410, S’042SynchronizationUpcoming eventsUpcoming events2/18 (tonight) – Project 2 due2/20 (Friday) – Project 3 out2/25 (Wednesday) – Homework 1 due2/25 (Wednesday) – Midterm exam (evening)March 3 – Project 3 checkpoint 1March 5 – Mid-semester/spring breakSpring breakSpring breakWe do not plan for you to work on Project 3It can be an excellent time for some “light reading”15-410, S’043Pop QuizQ1. What does this program do?[bmm@bmm bmm]$ cat pf.c#include <stdio.h>int main(){ printf("%d\n",printf);}Q2. What does the Unix “ld” program do?Q2. What does the Unix “ld” program do?15-410, S’044Pop QuizQ1. What does this program do?[bmm@bmm bmm]$ cat pf.c#include <stdio.h>int main(){ printf("%d\n",printf);}[bmm@bmm bmm]$ gcc pf.c –o pf[bmm@bmm bmm]$ pf13451341615-410, S’045OutlineWhat What isis printf()? printf()?Where addresses come fromWhere addresses come fromExecutable files vs. Memory ImagesExecutable files vs. Memory ImagesConversion by “ program loader”You will write one for exec() in Project 3Object file linking (answer to Q2)Object file linking (answer to Q2)Loader bugs make programs execute half-rightYou will need to characterize what's broken1. (Not: “ every time I call printf() I get a triple fault” )You will need to how the parts should fit together15-410, S’046Where do addresses come from?Program linking, program loadingProgram linking, program loading... means getting bits in memory at the right addressesWho Who usesuses those addresses? those addresses?(Where did that “ wild access” come from?)Code addresses: program counter (%cs:%eip)Code addresses: program counter (%cs:%eip)Straight-line codeLoops, conditionalsProcedure callsStack area: stack pointer (%ss:%esp, %ss:%ebp)Stack area: stack pointer (%ss:%esp, %ss:%ebp)Data regions (data/bss/heap)Data regions (data/bss/heap)Most pointers in general purpose registers (%ds:%ebx)15-410, S’047How are they initialized?Program counterProgram counterSet to “ entry point” by OS program loaderStack pointerStack pointerSet to “ top of stack” by OS program loaderRegistersRegistersHow does my code know the address of thread_table[]?Some pointers are stored in the instruction streamfor (tp = thread_table, tp < &thread_table[n_threads], ++tp)Some pointers are stored in the data segment struct thread *thr_base = &thread_table[0];How do these all point to the right places?15-410, S’048Where does an int live?int k = 3;int foo(void) { return (k);}int a = 0;int b = 12;int bar (void) { return (a + b);}...retleavemovl _k,%eaxcode0b = 12k = 3data4096a = 0bss819215-410, S’049Loader: Image File ⇒ Memory Image...retleavemovl _k,%eaxcode012 3data40960bss8192...retleavemovl _k,%eaxcode012 3data4096headerImage file has header (tells loader what to do)Memory image has bss segment!15-410, S’0410Programs are Multi-partModularityModularityProgram can be written as a collection of smaller source files, rather than one monolithic mass.Can build libraries of common functions (more on this later)e.g., Math library, standard C libraryEfficiency (time)Efficiency (time)Change one source file, compile, and then relink.No need to recompile other source files.“ Link editor” combines objects into one image file“ Link editor” combines objects into one image fileUnix “ link editor” called “ ld”15-410, S’0411Combining Objects: Link EditorLinker (ld)Translatorsm.cm.oTranslatorsa.ca.opSeparately compiled relocatable object filesExecutable object file (contains code and data for all functions defined in m.cand a.c)15-410, S’0412Linker Todo ListMerge object filesMerge object filesMerges multiple relocatable (.o) object files into a single executable object file that can loaded and executed by the loader.Resolve external referencesResolve external referencesAs part of the merging process, resolves external references. External reference: reference to a symbol defined in another object file.Relocate symbolsRelocate symbolsRelocates symbols from their relative locations in the .o files to new absolute positions in the executable.Updates all references to these symbols to reflect their new positions.What does this mean??15-410, S’0413Every .o uses same address spacecodedatabsscodedatabss15-410, S’0414Combining .o's Changes Addressescodedatabsscodedatabss15-410, S’0415Linker uses relocation informationFieldField address, bit field sizeField typeField typerelative, absoluteField referenceField referencesymbol nameExampleExample“ Bytes 1024..1027 of foo.o refer to absolute address of _main”15-410, S’0416Example C Programint e=7; int main() { int r = a(); exit(0); } m.c a.cextern int e; int *ep=&e;int x=15; int y; int a() { return *ep+x+y; }15-410, S’0417Merging Relocatable Object Files into an Executable Object Filemain()m.oint *ep = &ea()a.oint e = 7headersmain()a()0system codeint *ep = &eint e = 7system datamore system codeint x = 15int ysystem dataint x = 15Relocatable Object FilesExecutable Object File.text.text.data.text.data.text.data.bss .symtab.debug.datauninitialized data.bsssystem code15-410, S’0418Relocating Symbols and Resolving External ReferencesSymbols are lexical entities that name functions and variables.Each symbol has a value (typically a memory address).Code consists of symbol definitions and references.References can be either local or external.int e=7; int main() { int r = a(); exit(0); } m.c a.cextern int e; int *ep=&e;int x=15; int y; int a() { return *ep+x+y; } Def of local symbol eRef to external symbol exit(defined in libc.so)Ref toexternalsymbol eDef oflocal symbol ep Defs of local symbols x and yRefs of local symbols ep,x,yDef oflocal symbol a Ref to external symbol a15-410, S’0419m.o Relocation InfoDisassembly of section .text: 00000000 <main>: 00000000 <main>: 0: 55 pushl %ebp 1: 89 e5 movl %esp,%ebp 3: e8 fc ff ff ff call 4 <main+0x4> 4: R_386_PC32 a 8: 6a 00


View Full Document

CMU CS 15410 - Lecture

Download Lecture
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 Lecture 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 Lecture 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?