C03c: Linkers and Loaders

Similar documents
CS 550 Operating Systems Spring Process I

2012 LLVM Euro - Michael Spencer. lld. Friday, April 13, The LLVM Linker

COS 318: Operating Systems

CISC 7310X. C03: Process. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 2/15/2018 CUNY Brooklyn College

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

EEE 435 Principles of Operating Systems

CHAPTER 2: SYSTEM STRUCTURES. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

CS420: Operating Systems. OS Services & System Calls

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edit9on

COS 318: Operating Systems. Overview. Andy Bavier Computer Science Department Princeton University

Executables and Linking. CS449 Spring 2016

The Slide does not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams.

Kernel Types Simple OS Examples System Calls. Operating Systems. Autumn CS4023

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

An Introduction to Python (TEJ3M & TEJ4M)

Chapter 2: Operating-System Structures

Compiler Drivers = GCC

Operating Systems CS3502 Spring 2018

Chapter 2: Operating-System Structures. Operating System Concepts Essentials 8 th Edition

C18: Network Fundamentals and Reliable Sockets

Executables and Linking. CS449 Fall 2017

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

(Extract from the slides by Terrance E. Boult

Chapter 2: Operating-System

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edition

Lec 13: Linking and Memory. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Generating Programs and Linking. Professor Rick Han Department of Computer Science University of Colorado at Boulder

Chapter 8: Memory Management. Operating System Concepts with Java 8 th Edition

CSE 4/521 Introduction to Operating Systems. Lecture 12 Main Memory I (Background, Swapping) Summer 2018

Chapter 8: Memory Management. Background Swapping Contiguous Allocation Paging Segmentation Segmentation with Paging

Software Concepts. It is a translator that converts high level language to machine level language.

CS420: Operating Systems

Memory Management (1) Memory Management

Memory Management (1) Memory Management. CPU vs. memory. No.8. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Lecture 2 - Fundamental Concepts

OPERATING SYSTEMS. Prescribed Text Book Operating System Principles, Seventh Edition By Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

CSE 4/521 Introduction to Operating Systems

Lecture 2 Operating System Structures (chapter 2)

C19: User Datagram and Multicast

Compiler, Assembler, and Linker

Computer Systems Organization

Chapter 2: System Structures. Operating System Concepts 9 th Edition

Chapter 2: System Structures. Operating System Concepts 9 th Edition

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

Chapter 2. Operating-System Structures

Link 2. Object Files

Department of Computer Science and Engineering Yonghong Yan

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Embedded Systems Programming

Chapter 2: System Structures

Chapter 2: Operating-System Structures

CS2141 Software Development using C/C++ Libraries

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

CS370 Operating Systems

Operating Systems CMPSC 473. Process Management January 29, Lecture 4 Instructor: Trent Jaeger

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edit9on

Chapter 2: Operating-System Structures

C06: Memory Management

Objectives. Chapter 2: Operating-System Structures. 2.1 Operating System Services

Operating System Services. User Services. System Operation Services. User Operating System Interface - CLI. A View of Operating System Services

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

Chapter 2 Operating-System Structures

COS 318: Operating Systems. Overview. Jaswinder Pal Singh Computer Science Department Princeton University

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

What we saw. Desarrollo de Aplicaciones en Red. 1. OS Design. 2. Service description. 1.1 Operating System Service (1)

OPERATING SYSTEM. Chapter 4: Threads

CS631 - Advanced Programming in the UNIX Environment

Introduction to Java Programming

Cross Compiling. Real Time Operating Systems and Middleware. Luca Abeni

Link 2. Object Files

Four Components of a Computer System

Chapter 9 Memory Management Main Memory Operating system concepts. Sixth Edition. Silberschatz, Galvin, and Gagne 8.1

ECE 598 Advanced Operating Systems Lecture 10

Chapter 2: Operating-System Structures

Chapter 2: Operating-System Structures

Computer Basics 1/24/13. Computer Organization. Computer systems consist of hardware and software.

Chapter 4: Multi-Threaded Programming

Metasm. a ruby (dis)assembler. Yoann Guillot. 20 october 2007

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

Chapter 2: System Structures

CS307: Operating Systems

Notes of the course - Advanced Programming. Barbara Russo

Operating System: Chap2 OS Structure. National Tsing-Hua University 2016, Fall Semester

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

CS240: Programming in C

Often, more information is required when designing system call Information varies according to OS and types of system call

Background. Contiguous Memory Allocation

Chapter 2: Operating-System Structures. Chapter 2: Operating-System Structures. Objectives. Operating System Services

The C Programming Language

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

What does an Operating System do?

9/19/18. COS 318: Operating Systems. Overview. Important Times. Hardware of A Typical Computer. Today CPU. I/O bus. Network

Executing Legacy Applications on a Java Operating System

Memory Management 9th Week

Process Environment. Pradipta De

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

CS 300 Leftovers. CS460 Pacific University 1

Full file at

12: Memory Management

Transcription:

CISC 3320 MW3 C03c: Linkers and Loaders Hui Chen Department of Computer & Information Science CUNY Brooklyn College 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 1

Outline Linkers and linking Loaders and loading Object and executable files 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 2

Authoring and Running a Program A programmer writes a program in a programming language (the source code) The program resides on disk as a binary executable file translated from the source code of the program e.g., a.out or prog.exe To run the program on a CPU the program must be brought into memory, and create a process for it A multi-step process 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 3

2/4/2019 CUNY Brooklyn College: CISC 3320 OS 4

Compilation Source files are compiled into object files The object files are designed to be loaded into any physical memory location, a format known as an relocatable object file. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 5

Relocatable Object File Object code: formatted machine code, but typically non-executable Many formats The Executable and Linkable Format (ELF) The Common Object File Format (COFF) 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 6

Examining an Object File In Linux/UNIX, $ file main.o main.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped $ nm main.o Also use objdump, readelf, elfutils, hexedit You may need # apt-get install hexedit # apt-get install elfutils 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 7

Linking During the linking phase, other object files or libraries may be included as well Example: $ g++ -o main -lm main.o sumsine.o A program consists of one or more object files. Linker combines relocatable object files into a single binary executable file. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 8

Linking: Example 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 9

Loader A loader is used to load the binary executable file into memory, where it is eligible to run on a CPU core. An activity associated with linking and loading is relocation It assigns final addresses to the program parts and adjusts code and data in the program to match those addresses So that, for example, the code can call library functions and access its variables as it executes. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 10

Loading: Example 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 11

Running Loader to Load a Program The loader may be invoked differently in different operating systems or operating systems shells Examples: Enter the name of the execute file on the command line and hit the ENTER key. Double-clicking on the icon associated with the executable file invokes the loader using a similar mechanism In Linux/UNIX, The shell first creates a new process to run the program using the fork() system call. The shell then invokes the loader with the exec() system call, passing exec() the name of the executable file. The loader then loads the specified program into memory using the address space of the newly created process. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 12

Library and Dynamically Link Library Library: a collection object code/files combined in a single formatted file Static library Object code in the library are linked into the executable file during the linking process, becomes a part of the executable, and loaded into memory from the executable Dynamic link library 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 13

Dynamic Link Library Object code in the library are linked during the loading process, and the object code is not a part of the executable. Examples Windows: DLLs (dynamically linked libraries) Linux/UNIX: SOs (dynamically linked shared object libraries) Benefit: the library is conditionally linked and is loaded as required during program run time, which avoids linking and loading libraries that may end up not being used into an executable file. For example, the math library is a Linux dynamically linked shared object library, as such is not linked into the executable file main. In this case, the linker inserts relocation information that allows it to be dynamically linked and loaded as the program is loaded. It is possible for multiple processes to share dynamically linked libraries, resulting in a significant savings in memory use. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 14

Object File and Executable File Executable files, like object files, typically have standard formats Executable files and object files include the compiled machine code, and a symbol table containing metadata about functions and variables that are referenced in the program. Example formats Linux/UNIX: the ELF format (for Executable and Linkable Format). There are separate ELF formats for relocatable and executable files. The ELF file for executable files has the program's entry point, the address of the first instruction to be executed when the program runs. Windows: the Portable Executable (PE) format Mac OS X: the Mach-O format. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 15

Questions? 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 16

Applications are System Specific Why? Each operating system provides a unique set of system calls. System calls are part of the set of services provided by operating systems for use by applications. But, we do run some applications cross platforms Chrome, Firefox, etc 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 17

Portable Applications How? Via interpreted program languages Via a virtual machine Via a standard language or API and compilation 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 18

Interpreted Programming Languages Via interpreted programming languages The application can be written in an interpreted language (such as Python or Ruby) that has an interpreter available for multiple operating systems. The interpreter reads each line of the source program, executes equivalent instructions on the native instruction set, and calls native operating system calls. Performance suffers relative to that for native applications, and the interpreter provides only a subset of each operating system's features, possibly limiting the feature sets of the associated applications. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 19

Virtual Machine The application can be written in a language that includes a virtual machine containing the running application. The virtual machine is part of the language's full Run-Time Environment (RTE). Example: Java and JRE Java has an RTE that includes a loader, byte-code verifier, and other components that load the Java application into the Java virtual machine. This RTE has been ported, or developed, for many operating systems, from mainframes to smartphones, and in theory any Java app can run within the RTE wherever it is available. Systems of this kind have disadvantages similar to those of interpreters, discussed above. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 20

Standard API and Compilation The application developer can use a standard language or API in which the compiler generates binaries in a machine- and operating-system-specific language. The application must be ported to each operating system on which it will run. This porting can be quite time consuming and must be done for each new version of the application, with subsequent testing and debugging. Example of a Standard API: the POSIX API and its set of standards for maintaining source-code compatibility between different variants of UNIX-like operating systems. 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 21

Questions? Applications are often system specific Approaches to write portable applications 2/4/2019 CUNY Brooklyn College: CISC 3320 OS 22