CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Linking & Loading CS-502 Operating Systems
Chapter 3 Loaders and Linkers
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
Lecture 10: Linking and loading. Lecture 10 / Page 2AE4B33OSS 2011 Contents Linker vs. loader Linking the executable Libraries Loading executable ELF.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
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++
Loaders and Linkers Object Program contains the information:
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions.
Memory Management Chapter 7.
Assembly Process. Machine Code Generation Assembling a program entails translating the assembly language into binary machine code This requires more than.
Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.
1 Machine-Independent Features Automatic Library Search automatically incorporate routines from a subprogram library Loading Options.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
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.
OBJECT MODULE FORMATS. The object module format we have employed as an educational device is called OMF (relocatable object format). It’s one of the earliest.
Separate Assembly allows a program to be built from modules rather than a single source file.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
MIPS coding. SPIM Some links can be found such as:
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Topic 2d High-Level languages and Systems Software
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
© 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.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
CSE 451: Operating Systems Winter 2011 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura.
Chapter 13 : Symbol Management in Linking
Different Types of Libraries
The Assembly Language Level Part C – Linking and Loading.
LINKERS Execution of a program written in a language L involves the following steps: 1.Translation of the program: Performed by the translator for language.
The Assembly Process Computer Organization and Assembly Language: Module 10.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
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.
Hello world !!! ASCII representation of hello.c.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
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)
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
System Programming and administration
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
Topic 2e High-Level languages and Systems Software
Background Program must be brought into memory and placed within a process for it to be run. Input queue – collection of processes on the disk that are.
Memory Management Tasks
CSE 451: Operating Systems Winter 2010 Module 16 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura 1.
The Assembly Language Level
Linking & Loading CS-502 Operating Systems
Program Execution in Linux
CMSC 491/691 Malware Analysis
CSE 451: Operating Systems Winter 2009 Module 16 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura 1.
CSC 497/583 Advanced Topics in Computer Security
Linking & Loading CS-502 Operating Systems
CSC 497/583 Advanced Topics in Computer Security
Program Assembly.
Presentation transcript:

CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002

Today’s Topic How do programs actually get loaded into memory The Windows executable image format

From source to execution –A programmer writes a Source file (helloworld.c file) –A compiler then translates it into an Object module (helloworld.obj file) –The linker combines various object modules it an Executable image (helloworld.exe file) –The loader does the final work in getting the image executing on the system But what does a “.obj” or a “.exe” file really contain?

First, a little theory then the real stuff Three ways a program can get loaded –Absolute loading – Load program at the same address (virtual and/or physical) every time –Relocatable loading – Load program at different addresses based on what is available –Dynamic run-time loading – Load and reload the program at different addresses while the program is running Address Binding –Where a symbolic label/name is translated (bound) to an actual address –The actual binding can be specified in the program, or resolved at compile time, link time, load time, or run time.

COFF and PE Files Common Object File Format (COFF) Portable Executable (PE) File Format † We are going to concentrate on the PE File format for executable images. Roughly the same format is used for object modules and dynamic link libraries. The PE file closely resembles what is needed in memory to run the program. The PE file itself is divided into various sections representing code, data, etc. † There are many more formats, such as ELF, etc.

Overall PE File Mapping Copyright © 2001 Microsoft Corporation, One Microsoft Way, Redmond, Washington U.S.A. All rights reserved.

Relative Virtual Addresses Some important things to note: –The address where the program runs is not equal to the file offset where code is stored in the PE file. –The address where the program runs may not be known at link time. –So any addresses stored in the code by the linker need to all be relative. A Relative Virtual Addresses (RVA) is an offset in memory relative to where the PE file is loaded We’ll see a example of this later with base relocation fixups

The PE File The PE File starts with a DOS Header –Signature (“MZ”) –Offset to the PE Header Followed by a PE Header –Machine type –Number of sections –Timestamp –Data Directory (table of where in the image is stored the export, import, resource, exception, security, base relocation, debug, etc.) Followed by a Section Table –Named list of the sections in the PE file (.text,.data,.rdata,.idata,.edata,.rsrc,.reloc, etc.) Followed by the sections

The usual suspects.text –Executable code.data –Read/write initialized data.rdata –Read only data Note that the linker combines text and data from various object modules to form the executable image. Compilers can append “$…” to the end of the names to dictate the ordering within a section. For example “.text$X” is before “.text$Y” in the.text section

Exporting names and ordinals To run an image that requires calling a dll the loader needs to be able to find the entry points into the dll Conceptually associated with a dll is a list of addresses (RVAs) that other modules can call Each exported entry point is assigned a unique ordinal value The module that then wants to call an entry point only needs to know the dll’s name and the ordinal value. However we as programmers really know the name and not the ordinal value that gets assigned by the linker. The export table saves us by specifying ordinal values and translating names to their ordinal value

The.edata section (what I export) Copyright © 2001 Microsoft Corporation, One Microsoft Way, Redmond, Washington U.S.A. All rights reserved.

Kernel 32 Exports exports table: Name: KERNEL32.dll Characteristics: TimeDateStamp: 3B7DDFD8 -> Fri Aug 17 23:24: Version: 0.00 Ordinal base: # of functions: A0 # of Names: A0 Entry Pt Ordn Name 00012ADA 1 ActivateActCtx C2 2 AddAtomA remainder of exports omitted Copyright © 2001 Microsoft Corporation, One Microsoft Way, Redmond, Washington U.S.A. All rights reserved.

Function calls Consider these three ways to call the function AddAtomA 1.call AddAtomA 2.call PTR [0x1234] 3.call 0x x67890: call PTR [0x1234] (where 0x1234 contains the address of AddAtomA) But compilers usually output call 0x And expect the linker to put in the correct address for the function imported or not. So the linker is stuck using method #3.

Importing functions The PE contains a table of imported modules (identified by the imported dll name) Each table entry identifies the module and lists the functions that need to be imported There a three ways of naming the imported function –Virtual address (nice if the dll never moves and the linker knows this address) –Ordinal value (nice if the linker knows the ordinal value) –Function name (refer back to how exports works) This information is stored in two tables –Import Address Table (IAT) –Import Name Table (INT)

The IAT and INT The IAT and INT are simply array of dwords (4 bytes) Each dword is either the function address (see earlier discussion on function calls), ordinal value, or a pointer to the function name. The loader changes the IAT values at load time to function addresses. For faster image startup images can be bound. Binding an image means resolving and overwriting the IAT table in the actual PE file. However if the imported dll changes the binding needs to be redone. The INT is used for this purpose.

The.idata section (what I import) Copyright © 2001 Microsoft Corporation, One Microsoft Way, Redmond, Washington U.S.A. All rights reserved.

Base Relocation Each module has a preferred load address. However the loader may not be able to always honor the request. If the module is relocated then the loader must fixup the addresses. The.reloc section specifies each location that needs to be fixed if the modules is moved. Don’t do this too often because it is a big performance hit

Other sections. resrc –Resources for the image such as icons, bitmaps, etc. –Organized like a file system.debug… –Debug information –Was “coff” up to NT 4.0 and has moved onto “pdb” in Window XP

Debugging Speaking of debugging…

Things to come Wednesday we’ll wrap everything up Final is on Tuesday December 17 th at 2:30