CSE 451: Operating Systems Winter 2009 Module 16 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura 1.

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Linking & Loading CS-502 Operating Systems
Lecture 10: Linking and loading. Lecture 10 / Page 2AE4B33OSS 2011 Contents Linker vs. loader Linking the executable Libraries Loading executable ELF.
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.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
Memory Management 2010.
Chapter 91 Memory Management Chapter 9   Review of process from source to executable (linking, loading, addressing)   General discussion of memory.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
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.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
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
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.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
CSE 451: Operating Systems Winter 2011 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura.
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
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Main Memory CSSE 332 Operating Systems Rose-Hulman Institute of Technology.
Lecture 3 Translation.
Basic Paging (1) logical address space of a process can be made noncontiguous; process is allocated physical memory whenever the latter is available. Divide.
Assemblers, linkers, loaders
Protecting Memory What is there to protect in memory?
CSE 120 Principles of Operating
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
System Programming and administration
Linking & Loading.
Operating Systems: A Modern Perspective, Chapter 6
Swapping Segmented paging allows us to have non-contiguous allocations
Software Development with uMPS
CS-3013 Operating Systems C-term 2008
Main Memory Management
Memory Management 11/17/2018 A. Berrached:CS4315:UHD.
Loaders and Linkers: Features
Machine Independent Features
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.
CS399 New Beginnings Jonathan Walpole.
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Chapter 8: Memory management
CSE 451: Operating Systems Winter 2011 Processes
CSE 451: Operating Systems Winter 2010 Module 4 Processes
CSE 451: Operating Systems Winter 2010 Module 16 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura 1.
CSE 451: Operating Systems Autumn 2005 Memory Management
The Assembly Language Level
Linking & Loading CS-502 Operating Systems
Operating System Chapter 7. Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Memory management Explain how memory is managed in a typical modern computer system (virtual memory, paging and segmentation should be described.
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Machine Independent Assembler Features
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Operating Systems: A Modern Perspective, Chapter 6
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Memory Management CSE451 Andrew Whitaker.
CSC 497/583 Advanced Topics in Computer Security
Linking & Loading CS-502 Operating Systems
Machine Independent Assembler Features
COMP755 Advanced Operating Systems
Lecture Topics: 11/20 HW 7 What happens on a memory reference Traps
CSE 542: Operating Systems
Presentation transcript:

CSE 451: Operating Systems Winter 2009 Module 16 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura 1

So you want to run a program.. How was it created? Someone wrote some C/C#/C++/etc Compile/fix-errors/compile again Get object files What’s in the .o or .obj files? Code and data and fixups Code and data are easy Fixups describe relationships Targets of jumps/calls Data references What do you do about references to other (extern) code/data? Fixups too! 4/19/2019

More of what’s in .o / .obj files Old style format (reflecting stream view of files) Stream of records <tag><data> where <tag> was DATA: <data section> was constant data BSS: <data section> was just the size of the BSS reserved CODE: just like DATA FIXUP: applies to previous record, may list a name (external) or an offset into some other prior record and describe a width (8, 16, 32, 64) and an operation (ADD, IMM, SELF-REL) Modern format (take advantage of memory mapping) Header on file describes sections suitable for mmap() 4/19/2019

What’s a section? Section is a piece of contiguous memory Named Protected: read only, read/write, execute, read/execute, etc. Location in file Location in memory Some names are important DATA CODE BSS DEBUG FIXUP 4/19/2019

Putting .o/.obj files together The “linker” take a collection of object files and produce an executable image Gathers and appends like named/protected sections Evaluates fixups and establishes addressing (linkages) between sections Emits special sections DEBUG IMPORT EXPORT All into a file with the same general format as .o/.obj files A few new sections But it’s header says it’s executable Called the image file 4/19/2019

Executing the image file What does exec() or CreateProcess() do? Easy stuff: Allocate PCB Create address space Harder stuff Create first thread Copy handle environment from parent The meat: Opens image file Memory maps header (reading section table) For each section: Memory map the appropriate portion of the file Into the correct address space location With correct memory protection 4/19/2019

Is that all? Once upon a time, yes Not nearly useful enough All code was in one file Included all special stuff for calling the OS Not nearly useful enough What if system call #’s changed? What about sharing common code between apps? What about 3rd party code? What about extensibility? 4/19/2019

Dynamic libraries Goal: break down single images into multiple pieces Independently distributable Breakdown based on functionality / extensibility Implications on image format Need a way to reference between image files Add IMPORT and EXPORT sections IMPORT lists all functions required by the image file (executable or library) EXPORT lists all functions offered by the image file Big implications on process creation 4/19/2019

Process Creation with libraries Easy/Harder stuff still the same Hardest stuff: No longer loading just a single file, loading multiple modules Walking each IMPORT table, finding references to modules not yet loaded and loading them Big graph traversal How are linkages established between modules? 4/19/2019

Module Linkage Naïve approach is to use something similar to fixups Modify the sections to establish linkage Modifies the memory mapped pages Don’t want to modify the original file Copy-on-write Bigger page file More dirty pages in memory Work with compiler Observe that inter-module references are always direct (never self-relative). Call or pointer reference Keywords in language (or header files) that change direct calls into indirect calls and direct addressing into indirect addressing. 4/19/2019

Efficient Linkage Foo( args ) turns into (*import_Foo)( args ) Gather all import_X addresses into a single section Called IAT (import address table) Usually only a single page in size, not inefficient to dirty Still have to do some big work Can we do better? 4/19/2019

Binding Floating modules Bind modules to specific locations No known address IAT required to handling differing locations based on other modules’ locations Bind modules to specific locations Section table describes location, mapping is trivial IAT can be pre-built with locations already in mind Zero program-startup fixups What’s the issue? 4/19/2019

Binding What address do you assign? What if there’s a collision? 32 bit address space seems large enough XP has >1200 modules. What if there’s a collision? New release of module grows in size (bug fixes, functionality) Modules produced by two independent companies Loader needs to be robust in the face of this Choose another location Fix up IAT (small number of pages) 4/19/2019

A few cheats Compiler needs to generate self-relative instructions Otherwise relocation of module would require fixups Works well on x86… Most of XP’s DLL’s can be broken into disjoint groups and addresses assigned to each 4/19/2019

Vista cool feature “dynamic rebasing” Buffer overflow attacks At install time, all modules are rebased randomly in memory Just edit the IATs, still have speedy program start What problem would this solve? Buffer overflow attacks Operate by overflowing a stack buffer and overwriting a return address Knowing where special code might be would allow attacker to hijack return to code in a module not directly referenced Not if the module moves… 4/19/2019

Windows CreateProcess Different from fork/exec. Fork/exec are in kernel mode and embody the entire process creation experience Windows Kernel has NtCreateProcess – creates a new process address space. BUT NO THREAD NtCreateThread – creates a new thread in a given process NtSetThreadInformation – sets execution context for thread (notably stack and PC) 4/19/2019

Windows CreateProcess CreateProcess is user code in kernel32 module Creates process (NtCreateProcess) Maps in kernel call DLL (ntdll) Maps in image (but no libraries) Creates initial thread Sets thread to initialization routine in ntdll (LdrpInitializeProcess) Go! LdrpInitializeProcess does all the memory mapping work Executing in the new image’s context Walking module lists is just memory access Makes NtCreateSection calls 4/19/2019

Why not do what unix did? Extensibility Simpler loader code Differing loader policies (OS/2, DOS) New loader implementations Smaller kernel Simpler loader code 4/19/2019