CSE 451: Operating Systems Winter 2011 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura.

Slides:



Advertisements
Similar presentations
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Advertisements

Part IV: Memory Management
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
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.
Machine Independent Assembler Features
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.
Chapter 101 Virtual Memory Chapter 10 Sections and plus (Skip:10.3.2, 10.7, rest of 10.8)
CSE 451: Operating Systems Winter 2009 Module 4 Processes Mark Zbikowski Gary Kimura.
Memory Management 2010.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Chapter 6 Implementing Processes, Threads, and Resources.
03/05/2008CSCI 315 Operating Systems Design1 Memory Management Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Software Development and Software Loading in Embedded Systems.
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.
CSE 451: Operating Systems Winter 2012 Processes Mark Zbikowski Gary Kimura.
Dynamically Linked Libraries. 2 What’s the goal? Each program you build consists of –Code you wrote –Pre-existing libraries your code accesses In early.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 8: Main Memory.
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.
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.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Paging (continued) & Caching CS-3013 A-term Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Processes and Virtual Memory
Lecture 10 Page 1 CS 111 Summer 2013 File Systems Control Structures A file is a named collection of information Primary roles of file system: – To store.
CS6502 Operating Systems - Dr. J. Garrido Memory Management – Part 1 Class Will Start Momentarily… Lecture 8b CS6502 Operating Systems Dr. Jose M. Garrido.
NETW3005 Memory Management. Reading For this lecture, you should have read Chapter 8 (Sections 1-6). NETW3005 (Operating Systems) Lecture 07 – Memory.
Windows workshop 2010 Understanding Software Dependencies in Windows Roland Yap School of Computing National University of Singapore Singapore
Win32 Programming Lesson 19: Introduction to DLLs.
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
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.
Memory Management Chapter 5 Advanced Operating System.
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.
Assemblers, linkers, loaders
System Programming and administration
Linking & Loading.
Operating Systems: A Modern Perspective, Chapter 6
CS-3013 Operating Systems C-term 2008
Loaders and Linkers: 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.
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.
The Assembly Language Level
Linking & Loading CS-502 Operating Systems
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Winter 2009 Module 16 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura 1.
Linking & Loading CS-502 Operating Systems
COMP755 Advanced Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CSE 451: Operating Systems Winter 2011 Linking, Loading and Process Startup Mark Zbikowski Gary Kimura

12/8/20152 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!

12/8/20153 More of what’s in.o /.obj files Old style format (reflecting stream view of files) –Stream of records where was DATA: was constant data BSS: was just the size of the BSS reserved CODE: just like DATA FIXUP: applies to previous CODE/DATA 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()

12/8/20154 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

12/8/20155 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

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

12/8/20157 Is that all? Once upon a time, yes –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 3 rd party code? –What about extensibility?

12/8/20158 Dynamic (aka Shared) 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

12/8/20159 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 (remember “transitive closure”?) –How are linkages established between modules?

12/8/ 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.

12/8/ 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?

12/8/ Binding Floating modules –No known address –IAT required to handle 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?

12/8/ Binding What address do you assign? –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)

12/8/ 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

12/8/ Vista cool feature “dynamic rebasing” –At install time, all modules are rebased to random addresses –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…

12/8/ 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)

12/8/ 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

12/8/ Why not do what Unix did? Extensibility –Differing loader policies (OS/2, DOS) –New loader implementations –Smaller kernel Simpler kernel loader code