<<

Loaders

Computer Science and Engineering  College of Engineering  The Ohio State University

Lecture 24 Again?

Computer Science and Engineering  The Ohio State University  Haven’t we done this already?  We built one in lab 1!  Our programming model then was simplistic:

Memory Source Object Program File

Compiler Loader Recall: “Big” Picture

Computer Science and Engineering  The Ohio State University

Assembly EG1 .ORIG x3400 Assembler ST R1,x34A2 File ...

HEG1 34000040 Object Loader T340032A2 File ...

Emulator Problems With This Model

Computer Science and Engineering  The Ohio State University  Programmer is responsible for putting absolute addresses in code  Error prone calculation  Brittle: Change in program entails change of addresses Assembly EG1 .ORIG x3400 Assembler ST R1,x34A2 File ...

HEG1 34000040 Object Loader T340032A2 File ... Problems (II)

Computer Science and Engineering  The Ohio State University  Programmer decides where in memory to load the  Better to give OS control  Dynamic scheduling, concurrent jobs

Assembly EG1 .ORIG x3400 Assembler ST R1,x34A2 File ...

HEG1 34000040 Object Loader T340032A2 File ... Problems (III)

Computer Science and Engineering  The Ohio State University  Program must be self-contained  No guarantees on what is in memory outside of loaded segment  Entire program is contained in a single  Better: Allow separate assembly  One program is split between multiple files  Each source file can be assembled independently from the others  Loader is responsible for stitching the multiple object files together Separate Assembly: Advantages

Computer Science and Engineering  The Ohio State University  Modularity: Make one change in source, do not need to recompile entire program  Libraries: Support external libraries for common bits of code; e.g., strcpy(), sqrt(), sin()…  Language neutrality: Write different parts in different source languages  Some languages are better suited to certain tasks than others  functions can be written in a single language (rather than rewriting for every possible source language) More General View of Compilation and Loading

Computer Science and Engineering  The Ohio State University

Source Object Library Memory Files Files Files C .c .o Program Compiler .a

Fortran Fortran .f .o Compiler Program Loader

Assembly .sAssembler .o Program

10 General Loaders

Computer Science and Engineering  The Ohio State University This approach requires standardizing the format of the object file Each source language translator then follows this same standard

11 Steps Before

Computer Science and Engineering  The Ohio State University 1. Translation  Produce object file from source code 2. Allocation  Select area in memory for program 3.  Adjust address references in object file 4. Linking  Combine multiple object files 5. Loading  Initialize memory with segment contents Types of Loaders

Computer Science and Engineering  The Ohio State University  There are various strategies for accomplishing these five steps  Distinguish different types of loaders based on strategy used  Compile-and-go  Absolute  Relocating  Linking   Dynamic linking Types of Loaders

Computer Science and Engineering  The Ohio State University  There are various strategies for accomplishing these five steps  Distinguish different types of loaders based on strategy used  Compile-and-go  Absolute  Relocating  Linking  Dynamic loading  Dynamic linking Compile-And-Go

Computer Science and Engineering  The Ohio State University  Observation: A translator is, itself, a program  Loaded in memory, executing  Lab 4: Write lab 2 assembler in assembly  Reserve memory at the end of the translator’s segment as destination of compiled  As source is compiled, load machine code directly into this reserved memory  Combines loader with the compiler  Example: WATFOR compiler Translate and Load

Computer Science and Engineering  The Ohio State University

Memory

Source Translator Program

16 Compile-And-Go: Responsibility

Computer Science and Engineering  The Ohio State University 1. Translation  2. Allocation  3. Relocation  4. Linking  5. Loading  Tradeoffs

Computer Science and Engineering  The Ohio State University  Advantages:  Speed (vs compiled): no intermediate file (I/O is always slow)  Speed (vs interpreted): runs compiler- optimized machine code  Low start-up cost: Translator remains resident in memory  Disadvantages:  Program must be recompiled to run every time  Libraries must be source language-based  Memory must be conservatively reserved Types of Loaders

Computer Science and Engineering  The Ohio State University  There are various strategies for accomplishing these five steps  Distinguish different types of loaders based on strategy used  Compile-and-go  Absolute  Relocating  Linking  Dynamic loading  Dynamic linking Absolute Loaders

Computer Science and Engineering  The Ohio State University  Familiar loader from Lab 1  Consider responsibility for each task:  Translate:  Allocate:  Determine length of segment:  Determine load location:  Relocate:  Link:  Load: Tradeoffs

Computer Science and Engineering  The Ohio State University  Advantages  Simple, small, fast  Affords high degree of control to programmer  Disadvantages  Lack of flexibility in load location means wasted memory  Small change in source requires recompiling everything  Generally: Too error-prone and wasteful for most cases Special Case

Computer Science and Engineering  The Ohio State University  Observation: a loader is a program  Resides in memory, executes  Q: How did it get there? Ie what loads the loader?  A: The OS. Wait, then what loads the OS?  From an idle machine, we need a way to start things up  Solution: Bootstrap Loader

Computer Science and Engineering  The Ohio State University  Store a special program in ROM  This program is automatically executed at power-up  This program is an absolute loader  Reads records from an input device  Puts them in a predetermined (absolute) memory location  Control is then transferred to loaded program (which can load other things, etc) Types of Loaders

Computer Science and Engineering  The Ohio State University  There are various strategies for accomplishing these five steps  Distinguish different types of loaders based on strategy used  Compile-and-go  Absolute  Relocating  Linking  Dynamic loading  Dynamic linking Relocating Loader

Computer Science and Engineering  The Ohio State University  The assembler does 2 things to support relocation:  Produces size-of-segment information  Flags relative value (modification records)  Loader works with OS to determine load location at load/run time  Loader patches (appropriate) text records by adding loading location  Q. How many passes (over the object file) are required?  A. Tradeoffs

Computer Science and Engineering  The Ohio State University  Advantages  Dynamic load location means more efficient memory allocation  Disadvantages  Program must be self-contained (no external subroutines or libraries)

 Time for a slight detour, as a motivational aside for remaining loader types… Subroutine Linkage

Computer Science and Engineering  The Ohio State University  Example: Implementing Connect Four  Game board .BLKW #7*#6 ;0/1/2 empty/red/yellow  Problem: Given column for next move, calculate row in which piece would end up Idea 1: Who Needs Branching?

Computer Science and Engineering  The Ohio State University  Embed the code for this calculation right in “main”  Advantages:  Branching can be slow (ha ha)  Disadvantages:  No code reuse if this calculation is needed in multiple places  Even if the calculation is needed in just one place, monolithic programs are hard to read and debug Idea 2: A Function/Method

Computer Science and Engineering  The Ohio State University  Problem: There are no functions or methods in  Write code, add a label  From “main”, need to jump to this code Cnct4 .ORIG main . . . TRAP x25

CRow LD R0,=#0 ADD . . . etc etc

.END main Branching

Computer Science and Engineering  The Ohio State University  Want to branch two times:  First, to CRow  Later, back to caller  Why does this not work? main … BRNZP CRow Cllr ;etc… TRAP x25 CRow LD R0, =#0 ;etc… BRNZP Cllr .END main Solution: Jump-To-Subroutine

Computer Science and Engineering  The Ohio State University  JSR pgoffset9  Example: JSR CRow ;x3F0C: 481B  Loads the PC into R7  Branches to location CRow  Q: What is the value of R7 after JSR?  A:  Q: What restriction exists on target of branch?  A: Solution: Jump-To-Subroutine

Computer Science and Engineering  The Ohio State University  JSRR BR,index6  Example: LEA R3,CRow ;x3F0C: E61B JSRR R3,#0 ;x3F0D: C8C0  Loads R3 with value of symbol CRow  Loads the PC into R7  Branches to location R3  Q: Does this approach remove the restriction on target of branch?  A: Summary

Computer Science and Engineering  The Ohio State University  Different loading strategies  Compile-and-go: Combine translation and loading  Absolute: Programmer control, useful for bootstrapping  Relocating: Dynamic load location for efficient memory use  Subroutine linkage  JSR/JSRR affords linking to reusable subroutines  Working towards: Separate compilation