



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




2
3
CIT 595
.ORIG^ x x^
x^
x^
TRAP^ x x^
x^
x3005^ TEST^
BRz^ OUTPUT x^
x^
x^
BRnp^ GETCHAR x300A^
x300B^ GETCHAR
x300C^
x300D^
BRnzp^ TEST x300E^ OUTPUT
x300F^
x^
TRAP^ x x^
TRAP^ x x3012^ ASCII
x x3013^ PTR^
.FILL^ x4000.END
4
.ORIG^ x x^
x^
x^
TRAP^ x x^
x^
x3005^ TEST^
BRz^ OUTPUT x^
x^
x^
x^
BRnp^ GETCHAR x300A^
x300B^ GETCHAR
x300C^
x300D^
BRnzp^ TEST x300E^ OUTPUT
x300F^
x^
TRAP^ x x^
TRAP^ x x3012^ ASCII
x x3013^ PTR^
.FILL^ x4000.END
000010001 1111 0000 00100011..
5
CIT 595
Object File
^ The partial machine code (a.k.a object code) producedby the compiler/assembler is outputted to a file^ ^ e.g. gcc –c filename.c produces file called filename
.o
^ Each file or modules gets it own object codei.e. proc1.c ->proc1.o and proc2.c ->proc.o Format allows other programs Linkers and Loader toextract information Depending on the Operating System, object formatsmay vary ^ E.g. Unix uses a file format called ELF (Executable and LinkingFormat)
6
CIT 595
Object File Contents ^ Header: Overall Information about the file and itscontents ^ Object code and data ^ Relocation information^ ^ Information to help fix up the object code during linking^ ^ Which addresses are to be relocated is provided in this section ^ Symbol Table^ ^ Information about symbols defined in the module (file) andsymbols to be imported from other modules ^ Debugging Information (optional) 7
Linker Function 1: Fixing Addresses Address in an object file are usually relative to start ofthe code or data segment in that file Because compiler seldom knows where an object will reside, itoften assumes a fixed base location (for example, zero).^ ¾ CIT 595 Known as relocatable code When different object files are combined: Similar segments (e.g. text section) from different object filesget merged Address have to be fixed up to account for this merging ¾ Esp. absolute jumps, loads and stores instructions The relocation information in the object file helps linker in thefixing of addresses
8
CIT 595
Fixing Addresses Example
13
CIT 595
Loader
^ Loading is the process of copying an executable imageinto memory i.e. at the actual physical location Steps: ^ Determine how much address space is need from theobject file header (for e.g. text and data section) ^ Allocate the address space ^ Read the program segments in the address space ^ Create a stack segment ^ Tell the OS the value of PC (program counter) to startexecuting program (OS puts this info in PCB)
14
CIT 595
Loader (contd..)
15
CIT 595
Binding ^ Regardless of relocatable or absolute code^ ^ Program’s instruction and data are bound to physical address^ ^ This known as address
binding^ a process ^ Binding can be done at:^ ^ Compile-Time
: executable already indicates where exactly it should be loaded i.e. absolute code Loader learns whether the object file is absolute or relocatabledepending on the system:^ ¾^ Have distinct file extension e.g. .COM (absolute) vs. .EXEin MS-DOS^ ¾^ Encode some magic number (some code)
16
CIT 595
Binding ^ Load-Time^ ^ The executable output by the linker needs another relocationpass when it is finally loaded into memory^ ¾^ Adds starting address to each reference in the objectmodule^ ^ The loaded executable a.k.a
process image
cannot be moved
during execution as starting address must remain same (codenow is absolute) Run-time or execution time Binding Relocation step is omitted on hardware offering virtual memory Every program is put into its own address space, so there is noconflict even if all programs start at the same base address Allows for process image to be moved
17
CIT 595
Dynamic Linking ^ Dynamic Linking
defers much of the linking process till either load or runtime At load time, program and it’s unlinked modules arelinked and loaded by the loader^ ^ Causes start up delays The unlinked module can be linked upon invocation ofthat module while the program that requires it isrunning (run-time)^ ^ Static linker links each external reference in the program to adummy module whose sole purpose is to call the
dynamic
linker^ or^ linking loader
& pass the external symbol to it ^ The dummy module code to call the dynamic linker is knownas^ stub
18
Dynamic Linker or Linking Loader Loads the unlinked module in memory May just call the loader program to do this The unlinked module has some relocation and symboltable information that will help the dynamic linker toperform the linking process i.e. relocates any references in executable to symbols definedby unlinked modules The application/program is given back control after thelinking process is completed CIT 595
19
CIT 595
Advantage of Dynamic Linking Many programs can
share^ the same object module (library code), hence only one copy of the moduleneeds to be present in memory^ ^ With prior linking (static linking) there will be n copies of sameroutine as n programs might require it Dynamically linked shared libraries are easier to update^ than static linked shared libraries^ ^ Change is visible to all programs that use it^ ^ Static libraries would have to be re-linked
20
CIT 595
Shared Libraries in Unix ^ .so^ stands for shared library ^ To create shared library and setting up dynamic linking^ ^ Create:
gcc –shared –fPIC –o lib
name.so^ f1.o f2.o ¾^ Tells gcc to create shared library with PositionIndependent Code(PIC) ¾^ PIC allows library code to be loaded and executed at anyaddress Usage: assume f3.c actually uses dynamic library gcc f3.c ./libname.so Here a.out does not contain the object modules f1.o and f2.o ¾^ Still a partial executable ¾^ Contains some relocation and symbol table informationthat allow references to code and data in
libname.so^
to be
resolved at run time
25
CIT 595
GCC programs at Run-time ^ 1. loader (
ldd ) input is an executable file output is a process image on disk result is a PCB (process control block) placed in the “ready torun” queue of the operating system’s scheduler ldd is also called the “dynamic linker” or the “linking loader” ^ 2. scheduler (part of OS)^ ^ all populated parts of the process image exist (either in mainmemory or on disk)^ ^ PCB is either 1) running, 2) in “ready to run” queue, 3) in“blocked” queue ^ 3. dynamic linker (accessed via “stub” functions)
26
Disadvantage of Dynamic Linking No direct control over the contents of the dynamiclinked library routine Execution of the program is vulnerable If the library is moved, renamed, deleted or if an incompatibleversion of the DLL is copied to a place that is earlier in thesearch Basically program will fail CIT 595