Docsity

Programming Tools and Software Development: Compilers, Assemblers, Linkers, and Loaders - , Study notes of Computer Science

Toggle sidebar
bg1
1
Programming Tools
CIT 595
Spring 2008
2
CIT 595
System Software: Programming Tools
Programming tools carry out the mechanics of software
creation within the confines of the operating system and
hardware environment
These include:
Compiler & Assembler
¾Translate source program to machine language
¾Still not suitable for execution because of unresolved
references to external and library routines
Linker
¾Bring together the binaries of separately compiled modules
¾Search libraries and resolve eternal references
Loader
¾Bring an object program into memory for execution by
allocating memory to program (now becomes a process)
¾Might have to fix up some address
3
CIT 595
.ORIG x3000
x3000 AND R2,R2,#0
x3001 LD R3,PTR
x3002 TRAP x23
x3003 LDR R1,R3,#0
x3004 ADD R4,R1,#-4
x3005 TEST BRz OUTPUT
x3006 NOT R1,R1
X3007 ADD R1,R1,#1
x3008 ADD R1,R1,R0
x3009 BRnp GETCHAR
x300A ADD R2,R2,#1
x300B GETCHAR ADD R3,R3,#1
x300C LDR R1,R3,#0
x300D BRnzp TEST
x300E OUTPUT LD R0,ASCII
x300F ADD R0,R0,R2
x3010 TRAP x21
x3011 TRAP x25
x3012 ASCII .FILL x0030
x3013 PTR .FILL x4000
.END
x3013PTR
x3012ASCII
x300EOUTPUT
x300BGETCHAR
x3005TEST
AddressSymbol
Assembly Process Example: First Pass
4
CIT 595
Assembly Process Example: Second Pass
.ORIG x3000
x3000 AND R2,R2,#0
x3001 LD R3,PTR
x3002 TRAP x23
x3003 LDR R1,R3,#0
x3004 ADD R4,R1,#-4
x3005 TEST BRz OUTPUT
x3006 NOT R1,R1
x3007 ADD R1,R1,#1
x3008 ADD R1,R1,R0
x3009 BRnp GETCHAR
x300A ADD R2,R2,#1
x300B GETCHAR ADD R3,R3,#1
x300C LDR R1,R3,#0
x300D BRnzp TEST
x300E OUTPUT LD R0,ASCII
x300F ADD R0,R0,R2
x3010 TRAP x21
x3011 TRAP x25
x3012 ASCII .FILL x0030
x3013 PTR .FILL x4000
.END x3013PTR
x3012ASCII
x300EOUTPUT
x300BGETCHAR
x3005TEST
AddressSymbol
0101 010 010 1 00000
0010 011 000010001
1111 0000 00100011
.
.
pf3
pf4
pf5

Partial preview of the text

Download Programming Tools and Software Development: Compilers, Assemblers, Linkers, and Loaders - and more Study notes Computer Science in PDF only on Docsity!

Programming Tools

CIT 595Spring 2008

2

System Software: Programming Tools „ Programming tools carry out the mechanics of softwarecreation within the confines of the operating system andhardware environment „ These include: „ Compiler & Assembler^ ¾^ CIT 595

Translate source program to machine language ¾ Still not suitable for execution because of unresolvedreferences to external and library routines „ Linker ¾ Bring together the binaries of separately compiled modules ¾ Search libraries and resolve eternal references „ Loader ¾ Bring an object program into memory for execution byallocating memory to program (now becomes a process) ¾ Might have to fix up some address

3

CIT 595

.ORIG^ x x^

AND^ R2,R2,#

x^

LD^ R3,PTR

x^

TRAP^ x x^

LDR^ R1,R3,#

x^

ADD^ R4,R1,#-

x3005^ TEST^

BRz^ OUTPUT x^

NOT^ R1,R

X^

ADD^ R1,R1,#

x^

ADD^ R1,R1,R

x^

BRnp^ GETCHAR x300A^

ADD^ R2,R2,#

x300B^ GETCHAR

ADD^ R3,R3,#

x300C^

LDR^ R1,R3,#

x300D^

BRnzp^ TEST x300E^ OUTPUT

LD^ R0,ASCII

x300F^

ADD^ R0,R0,R

x^

TRAP^ x x^

TRAP^ x x3012^ ASCII

.FILL^

x x3013^ PTR^

.FILL^ x4000.END

x

PTR

x

ASCII

x300E

OUTPUT

x300B

GETCHAR

x

TEST

Address

Symbol

Assembly Process Example: First Pass

4

Assembly Process Example: Second PassCIT 595

.ORIG^ x x^

AND^ R2,R2,#

x^

LD^ R3,PTR

x^

TRAP^ x x^

LDR^ R1,R3,#

x^

ADD^ R4,R1,#-

x3005^ TEST^

BRz^ OUTPUT x^

NOT^ R1,R

x^

ADD^ R1,R1,#

x^

ADD^ R1,R1,R

x^

BRnp^ GETCHAR x300A^

ADD^ R2,R2,#

x300B^ GETCHAR

ADD^ R3,R3,#

x300C^

LDR^ R1,R3,#

x300D^

BRnzp^ TEST x300E^ OUTPUT

LD^ R0,ASCII

x300F^

ADD^ R0,R0,R

x^

TRAP^ x x^

TRAP^ x x3012^ ASCII

.FILL^

x x3013^ PTR^

.FILL^ x4000.END

x

PTR

x

ASCII

x300E

OUTPUT

x300B

GETCHAR

x

TEST

Address

0101 010 010 1 000000010 011^ Symbol

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