Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.

Slides:



Advertisements
Similar presentations
Hand-Held Devices and Embedded Systems Course Student: Tomás Sánchez López Student ID:
Advertisements

Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
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
Chapter 3 Loaders and Linkers. Purpose and Function Places object program in memory Linking – Combines 2 or more obj programs Relocation – Allows loading.
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++
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Object Code.
1 Machine-Independent Features Automatic Library Search automatically incorporate routines from a subprogram library Loading Options.
Generating Programs and Linking Professor Rick Han Department of Computer Science University of Colorado at Boulder.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Loader- Machine Independent Loader Features
Protected Mode. Protected Mode (1 of 2) 4 GB addressable RAM –( to FFFFFFFFh) Each program assigned a memory partition which is protected from.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
F13 Forensic tool analysis Dr. John P. Abraham Professor UTPA.
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.
SUSE Linux Enterprise Server Administration (Course 3037) Chapter 4 Manage Software for SUSE Linux Enterprise Server.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
A genda for Today What is memory management Source code to execution Address binding Logical and physical address spaces Dynamic loading, dynamic linking,
MIPS coding. SPIM Some links can be found such as:
Chapter 10 - Memory Management –Memory management can have a large influence on the performance of a program. –Operating system handles allocation of memory.
CSU System Programming, NTNU CSIE1 / 99 Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin.
Linking and Loading Linker collects procedures and links them together object modules into one executable program. Why isn't everything written as just.
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.
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.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
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.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Different Types of Libraries
CSc 453 Linking and Loading
Linking I Topics Assembly and symbol resolution Static linking Systems I.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
The World Leader in High Performance Signal Processing Solutions Toolchain Basics.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Program Execution in Linux David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3 Translation.
Assemblers, linkers, loaders
Computer Architecture & Operations I
Slides adapted from Bryant and O’Hallaron
The University of Adelaide, School of Computer Science
Linking & Loading.
Linux Userspace Process Memory Layout
Debugging with gdb gdb is the GNU debugger on our CS machines.
Chapter 8 Main Memory.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
ICS143A: Principles of Operating Systems Lecture 21: Program linking and loading Anton Burtsev March, 2017.
Program Execution in Linux
Software Development with uMPS
CS-3013 Operating Systems C-term 2008
Topic 2e High-Level languages and Systems Software
Linking & Loading CS-502 Operating Systems
Program Execution in Linux
10/6: Lecture Topics C Brainteaser More on Procedure Call
Linking & Loading CS-502 Operating Systems
Program Assembly.
Chapter 3 Loaders and Linkers
Reverse Engineering for CTFs
Presentation transcript:

Linking and Loading Fred Prussack CS 518

L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic –ELF –Other Linking Information/Processing References

L&L: Topics Not Covered Windows Alternate binary file formats Different versions of glibc Different versions of the kernel (from 2.4)

L&L: Questions for the Sleepy What is the name of the compile time linker in Linux? –ld What is the name of the run-time linker in Linux? –ld.so Where is the loader located in Linux? –Part kernel / part ld.so

L&L: Terms Linking –Taking object files and creating loadable modules with correct references to required libraries, data, and procedures Loading –Placing a program image into main memory for execution

L&L: General Information Static libraries generally named xxx.a (archives) Dynamic libraries generally named xxx.so (shared objects) Object files generally named xxx.o ELF: Executable and Linking Format. Currently the most common object file format on Linux systems. Other formats: a.out, COFF, etc…

L&L: Static vs. Dynamic Fully statically compiled executables –Provide for faster load->execution time due to no run-time linking requirement –Generate larger executables requiring more disk space Executables with dynamic dependencies –Require run-time linking and thus potential time implications –Allows for easier and better code re-use

L&L: Loading do_execve() search_binary_handler Searches all the registered binary handlers load_elf_binary Loads current binary and elf interpreter start_thread sets up correct registers Question: What does the instruction pointer have in it now? Answer: Entry point of the ELF interpreter

L&L: Loading/Linking At this point ld.so now has control Determine what libraries need to be loaded for this binary Determine dependencies for these libraries In what order are these loaded and what type of list is produced from this dependency list?

L&L: Linking Basic job to clean up unresolved symbols At compile time this can be accomplished by executing ld with object files to produce an executable At run-time this is accomplished by loading all required shared libraries (.so’s) and fixing unresolved symbols found in the libraries

L&L: Dynamic Linking Load Time Dynamic Linking Done By ld.so –Most likely on your system it is ld-linux.so which links to ld so –All possibly resolved symbols are resolved during compilation/first link (run of ld). Remaining unresolved symbols are done at time of load Lazy Binding (LD_BIND_NOW) Run Time Dynamic (inline) Linking –Allows applications to, during run time, open shared object files and execute their functions <dlfcn.h>

L&L: [more] Questions for the Sleepy In what package is ld.so distributed and built from? –Glibc Can gcc be made to not link files automatically? –Yes, of course! Use the –c option.

L&L: ELF File Format Currently the standard binary format for Linux since the late 90’s. Created in late 80’s. Three types of object files –Shared Object Files (.so; shared object file) –Relocatable Object Files (.o; object file) –Executable Object Files (executable binary file) First 4 characters of this type of file is [backspace (ascii 127)]ELF

L&L: ELF File Format First the ELF Header – 52 bytes in length on a 32 bit system Sections and Segments for libraries and binaries Various ELF segments –text: program instructions –data: initialized data –plt: procedure linkage table –got: global offset table Checking for NEEDED entries in the dynamic segment will let ld know what it needs to load

L&L: ld.so & Library Location ld.so must be able to correctly locate the identified libraries in the executable. It does this by looking for them in the following order: –DT_RPATH (-rpath-link option) Section in ELF file –LD_LIBRARY_PATH Environment Variable –/etc/ld.so.cache Compiled list of files to load –/lib; /usr/lib –/etc/ld.so.conf

L&L: ld.so processing Loop all the program headers to find necessary info –PHDR (program header): where the program headers start; This must be found first. –DYNAMIC: indicates where to find the dynamic segment (what must be loaded) NEEDED: Name of file needed –INTERP: used to find the interpreter – which generally turns out to be ld.so

L&L: ld.so processing Load all required libraries found in NEEDED portions of the DYNAMIC segment Get all necessary information from library –Dynamic header; phdr; load header

L&L: ld.so info Read-Write Read-Only

L&L: ld.so processing What about when we actually call a function that hasn’t been loaded? –First need to resolve addressing issues –Probably best to permanently fix them –Then we need to call the actual procedure

L&L: ld.so processing PLT0: pushl GOT + 4 jmp *GOT + 8 PLTN: jmp *GOT+n push #reloc_offset jmp PLT0 Procedure Start Loc reloc_offset library Next Procedure Run Routine to fix GOT then jump to procedure after locating correct symbol Stack Question: What is the name of the fix routine? Answer: fixup

L&L: ld.so misc. info You can run ld.so from the command line with an executable –This provides a great ability to test out new ld.so’s if necessary –/lib/ld-linux.so [executable [args…]]

L&L: linking helper tools ldd – list the dynamic dependancies readelf – displays information from ELF files objdump – show information from object files nm – show symbol information from object files strip – removes symbols from object files LD_DEBUG/LD_DEBUG_OUTPUT – shows debug output from ld.so

L&L: References Stallings, William. Operating Systems Internals and Design Principles, 4 th Edition. Upper Saddle River, NJ: Prentice-Hall, link.html 1/books/linux-c-programming/GCC-HOWTO/x796.html 1/books/linux-c-programming/GCC-HOWTO/x575.html 1/books/linux-c-programming/GCC-HOWTO/x575.html

L&L: References (cont.) code HOWTO.html dll.html?dwzone=linux