Separate Assembly allows a program to be built from modules rather than a single source file.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Linkage Editors Difference between a linkage editor and a linking loader: Linking loader performs all linking and relocation operations, including automatic.
Linking & Loading CS-502 Operating Systems
3. Loaders & Linkers1 Chapter III: Loaders and Linkers Chapter goal: r To realize how a source program be loaded into memory m Loading m Relocation m Linking.
Chapter 3 Loaders and Linkers
Chapter 3 Loaders and Linkers. Purpose and Function Places object program in memory Linking – Combines 2 or more obj programs Relocation – Allows loading.
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
Mr. D. J. Patel, AITS, Rajkot 1 Operating Systems, by Dhananjay Dhamdhere1 Static and Dynamic Memory Allocation Memory allocation is an aspect of a more.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
Machine Independent Assembler Features
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.
Programming Types of Testing.
1 Machine-Independent Features Automatic Library Search automatically incorporate routines from a subprogram library Loading Options.
Loader- Machine Independent Loader Features
An introduction to systems programming
A. Frank - P. Weisberg Operating Systems Real Memory Management.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
UNIT 13 Separate Compilation.
CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Static Shared Library. Non-shared v.s. Shared Library A library is a collection of pre-written function calls. Using existing libraries can save a programmer.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Chapter 13 : Symbol Management in Linking
Different Types of Libraries
Loader and Linker.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
CSc 453 Linking and Loading
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 – Loaders.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 –Linkers.
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Lecture 3 Translation.
Assemblers, linkers, loaders
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
System Programming and administration
The University of Adelaide, School of Computer Science
Linking & Loading.
Chapter 8 Main Memory.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
Program Execution in Linux
CS-3013 Operating Systems C-term 2008
CS1010 Programming Methodology
CS1010 Programming Methodology
Assembler Design Options
Loaders and Linkers: Features
Machine Independent Features
Loaders and Linkers.
Linking & Loading CS-502 Operating Systems
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
An introduction to systems programming
Chapter 3 Loaders and Linkers
Presentation transcript:

Separate Assembly allows a program to be built from modules rather than a single source file

Advantages of separate assembly separate source files provide a nice structure for dividing a project between several team members separate source files allow separate testing (and thus better isolation and detection of bugs) separate source files provide for easy reuse separate source files minimize the work of reassembly and relinking needed whenever a change occurs in only one source file prewritten libraries provide machine extensions (higher-level functions like square root, if not available as a machine instruction)

Programming testing Example programs linked from web pages illustrate subroutines exercised by test drivers individual testing of modules like this is often not done (e.g., we are lazy or we foolishly think we will save time by skipping testing) or testing is done poorly (e.g., we neglect important test cases) "write all the code, bolt it all together, and hope for the best"? - bad idea; it is easier to detect errors and debug when we individually test than when we combine previously tested and debugged modules, the remaining errors will probably stem from misunderstandings about the interface specifications (i.e., the number, ordering, and data types of the actual parameters in the subroutine calls)

Programming testing bottom-up development: –write and test the lowest-level (leaf) subroutines first, then write the modules or program that uses them –requires extra effort of writing test drivers top-down development: –write the highest-level modules/program first and test the logic; –requires extra effort of writing "stub" routines that are simply place holders for lower-level subroutines that will be developed later; these stubs can return fixed values if necessary and are often useful just to print that the call occurred (and optionally print what parameters were passed)

Programming testing Programmers often mix the two approaches –top-down typically offers the ability to quickly and easily prototype a program and obtain feedback from the end user –see this essay by Paul Graham on "Programming Bottom-Up" in which he argues that bottom-up programs are usually smaller and easier to read:

Linking objects compiler or assembler produces object file (.o) -- using -c flag for armc linker (unfortunately named ld since ln already used as a file system command) yields an executable file (default name is a.out)

Linking objects in file p1.s.global main.global x x:.word 55 y:.word 66 main: push {lr} prt_addr: ldr r1, =x ldr r0, =fmt1 bl printf prt_value: ldr r0, =x ldr r1,[r0] ldr r0, =fmt2 bl printf return: pop {pc}.section ".rodata" fmt1:.asciz "the address of x is %p\n" fmt2:.asciz "the value of x is %d\n"

Linking objects in file p2.s.global x.section ".data" x:.word 55 y:.word 66 [03:08:42] [84] armc p1.s /tmp/ccSiPZZk.o: In function `return': (.text+0x24): undefined reference to `x' collect2: ld returned 1 exit status

Linking objects [03:13:18] [89] armc -c p1.s [03:13:28] [90] nm p1.o nm - prints symbols in object file or t $a executable file t $dr = read only data (addresses are r fmt1 relative to data section) r fmt2T = text (addresses are relative to text T main section U printf U = undefined t prt_addrt = text (lower case type code => t prt_value private, upper case type code => t return global U x U = undefined

Linking objects [03:25:39] [95] armc -c p2.s [03:25:47] [96] nm p2.o D x D = data (lower case type code => private d y lower case type code => public

Linking objects [03:52:49] [107] arm-linux-gnueabi-gcc p1.o p2.o nm a.out // could use ld p1.o p2.o t $a 00008e90 t $a T main.. U undefined since t prt_addr default is dynamic t prt_value linking to shared t return object for printf D x c d y [04:06:40] the address of x is 0x69078 the value of x is 55

Linking objects the linker resolves external references between.o (simple object files),.a (libraries/archives), and.so (shared objects) the linker also performs storage management to assign regions within the executable file to each program section; the linker resolves any external references and also performs relocation, that is, it fixes addresses within the program sections relative to each other

Linking objects simple object files and libraries/archives are combined using static linking to make a self-contained executable (i.e., all parts needed for execution are contained in the executable); however, for common library routines such as printf, this requires too much disk space for every executable that uses the common routine to have to store its own copy

Linking objects shared objects use dynamic linking (at run time) to save disk space (since the program doesn't need to keep a copy of the shared object inside its executable file) and memory space (since many programs can share a single memory-resident copy of the shared object); e.g., using static linking (arm-gnueabi-gcc --static) on p1.s and p2.s resulted in an executable file size of bytes, which was reduced to 7962 bytes when dynamic linking was used (arm-gnueabi-gcc)

Linking objects (dynamic link libraries (DLLs) in Windows systems are similar to shared objects, however, some early Windows systems would link DLL files into a complete executable memory image at load time rather than run time)

Linking objects dynamic linking Originally all programs were linked statically ₋ All external references fully resolved ₋ Each program complete Since late 1980's most systems have supported shared libraries and dynamic linking: ₋ For common library packages, only keep a single copy in memory, shared by all processes. ₋ Don't know where library is loaded until runtime; must resolve references dynamically, when program runs.

Linking objects dynamic linking One way of implementing dynamic linking: jump table. ₋ Jump table: an array in which each entry is a single machine instruction containing an unconditional branch (jump). ₋ For each function in a shared library used by the program, there is one entry in the jump table that will jump to the beginning of that function. ₋ If one of the files being linked is a shared library, the linker doesn't actually include the shared library code in the final program. Instead it creates a jump table with slots for all of the functions that are used from that library.

Linking objects dynamic linking One way of implementing dynamic linking: jump table. ₋ For relocation records referring to functions in the shared library, the linker substitutes the address of the jump table entry: when the function is invoked, the caller will "call" the jump table entry, which redirects the call to the real function. ₋ When the program starts up, the shared library is loaded into memory and the jump table addresses are adjusted to reflect the load location.

Linking objects static linking advantages executable is self-contained no run-time overhead dynamic linking advantages reduced disk space for executable only one copy of shared routine needs to be in memory, thus reduced memory space across several currently executing programs will get latest version of shared object

Linking example.global main, sub1, y main: push {lr} ldr r0, =x bl sub1 ldr r2, =y ldr r1, [r2] ldr r3, =x ldr r2, [r3] ldr r0, =fmt bl printf mov r0, #0 pop {pc}.section ".data" x:.word 1 y:.word 5.section ".rodata" fmt:.asciz "\nx = %d y = %d\n\n"

Linking example /** sub1 **/ sub1: push {lr} add r0,r0, #4 bl sub2 pop {pc} /** sub2 ***/.global sub2,y sub2: push {lr} ldr r1, =y ldr r2, [r1] add r1, r2, #1 str r1,[r0] pop {pc} ~

Loading running a program == actual _loading_ into memory and then _branching_ to the entry point address loader usually performs address relocation as words containing absolute addresses are loaded into memory; relocation is required in both the linker and the loader so that the program will run correctly (with the correct addresses)

Summary PC-relative offset * caller and subroutine in same source file bound-- * caller and subroutine in different files --bound-- absolute address * definitions and use in same source file boundmay need relocation * definition and use in different files --boundmay need relocation AssemblerLinkerLoading