Binary Loader What is done by binary loader? ● Read executable from the filesystem ● Parse the binary header ● Copy all segments into addresses specified.

Slides:



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

Xeno Kovah – 2012 xkovah at gmail
Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
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
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.
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++
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Compilation (Semester A, 2013/14) Lecture 13: Assembler, Linker & Loader Noam Rinetzky Slides credit: Eli Bendersky, Mooly Sagiv & Sanjeev Setia.
Linking and Loading Fred Prussack CS 518. L&L: Overview Wake-up Questions Terms and Definitions / General Information LoadingLinking –Static vs. Dynamic.
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.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
UNIX ELF File Format. Elf File Format The a.out format served the Unix community well for over 10 years. However, to better support cross-compilation,
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.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 8: Main Memory.
MIPS coding. SPIM Some links can be found such as:
CSU System Programming, NTNU CSIE1 / 99 Linkers and Libraries Advisor: Dr. Gwan-Hwan Hwang Lecturer: Che-Sheng Lin.
Topic 2d High-Level languages and Systems Software
CSE451 Linking and Loading Autumn 2002 Gary Kimura Lecture #21 December 9, 2002.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
1 Chapter 4 Processes R. C. Chang. 2 Linux Processes n Each process is represented by a task_struct data structure (task and process are terms that Linux.
Linking Ⅱ.
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.
Chapter 13 : Symbol Management in Linking
Different Types of Libraries
Week 4 - Friday.  What did we talk about last time?  Some extra systems programming stuff  Scope.
CSc 453 Linking and Loading
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
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.
Program Translation and Execution I: Linking Sept. 29, 1998 Topics object files linkers class11.ppt Introduction to Computer Systems.
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.
Language Translation Compilation vs. interpretation Compilation diagram Step 1: compile Step 2: run program Compiled program compiler input output Compiled.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
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
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
Chapter 8 Main Memory.
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
Segmentation Lecture November 2018.
A Case Study on UNIX a.out File Format
Memory Allocation CS 217.
Computer Organization and Design Assembly & Compilation
Linking & Loading CS-502 Operating Systems
Computer Architecture
Program Execution in Linux
Linking & Loading CS-502 Operating Systems
Program Assembly.
CSE 542: Operating Systems
Reverse Engineering for CTFs
Process Address Spaces and Binary Formats
Presentation transcript:

Binary Loader

What is done by binary loader? ● Read executable from the filesystem ● Parse the binary header ● Copy all segments into addresses specified in the binary header – text,data,bss ● Call binary “interpreter” to initialize the binary ● Jump to the entry point of dynamic linker, not executable.

Executable ● a.out – The old and classic unix object format. – It contains text,data and bss sections plus one symbol table and one string table. ● COFF – The SVR3 object format. – The header now comprises a section table ● ELF – The successor to COFF – Make the support of shared library easier.

What’s in the executable file ● Headers – Architecture, version, entry point, index table ● Object Code – Data or instructures ● Relocation – Position Independent code(PIC) ● Symbols – Index to the data inside object code. ● Debug information

Microsoft.COM format ● 0-0xff: PSP ● 0x100-xxxx – The whole.COM executable will be loaded here. ● No headers, symbol table and debug information.

A.out ● Contains – a.out header – Text section – Data section – Other sections ● The instruction(text) and data(data) section are seperated. – Multiple process can share the same text

Relocation ● Mainly used by MMU-less system and some DLLs. ● An relocation entry(fixups) contains – An address relative to the beginning of the section – Length of fixups – Index with different meaning according to ● Extern: 1 if it is a external symbols ● Pcrel: It is relative to the PC. ● Others.

Symbol and string table ● Each entry in the symbol table represent either a function or variable in the program. ● Each symbol entry hold a index to the string table.

ELF(Executable and Linkable Format) ● A ELF header ● zero or more program tables ● zero or more section tables ● support dlopen,dlsym ● Support real dynamic libraries ● References –

ELF:header ● ELF magic ● Type, machine,version ● entry:start point of program ● ehsize: the size of header(sizeof(struct elfhdr)) ● shnum: The number of sectionss. ● shoff: The starting point of the section table ● shentsize: The size of each section ● phoff,shoff,flags ● phentsize,phnum

largo% readelf -S hello.o There are 11 section headers, starting at offset 1b8: name type VM addr off size flag [0] NULL / [1].textPROGBITS / [2].rel.textREL / [3].dataPROGBITS / [4].bssNOBITS / [5].noteNOTE / [6].rodataPROGBITS d 00 / [7].commentPROGBITS / [8].shstrtabSTRTAB d 00 / [9].symtabSYMTAB d4 000c0 10 / 0 a a 4 [a].strtabSTRTAB /

Type of sections ● PROGBITS: Program contents. ● NOBITS: BSS ● SYMTAB and DYNSYM: Symbol tables ● STRTAB: A string table ● REL and RELA: Relocation information. REL entries add the relocation value to the base value stored in the code or data, while RELA entries include the base value for relocation in the relocation entries themselves. ● DYNAMIC and HASH: Dynamic linking information and the runtime symbol hash table.

Typical sections ●.interp: The dynamic linker ●.hash,.dynsym,.dynstr: tables used by DLL ●.plt:jump tables to functions in libraries(RO) – items are point to the DLL – lazy binding(LD_BIND_NOW) ●.got: The global offset table(RW) – the DLL will change the value of this section ●.text,.data,.bss

ELF:program headers largo% readelf -l hello Elf file is Executable Entry point 0x There are 5 program headers, starting at offset 34: PHDR 0x x x000a0 0x000a0 R E Interp 0x000d4 0x080000d4 0x x00017 R Requesting program interpreter [/lib/elf/ld-linux.so.1] Load 0x x x x00515 R E Load 0x x x000cc 0x000d4 RW Dynamic 0x0054c 0x c 0x x00098 RW Shared library: [libc.so.4] 1

PLT and GOT ● Procedure Linkage Table(PLT) – Function jump table ● Global Offset Table(GOT) – Data jump table

XIP(eXecute In Place) ● Save memory(Especially for NOMMU system) ● Fast startup time(less memory copy) ● Requirements – no writable data in text segment

XIP Example ● Eamples- uCLinux fs/binfmt_flat.c – Allocate the memory for ● data segmenet ● bss segment ● stack ● relocation entries ● Shared Library headers extra = MAX(bss_len + stack_len, relocs * sizeof(unsigned long)); down_write(¤t->mm->mmap_sem); realdatastart = do_mmap(0, 0, data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long), PROT_READ|PROT_WRITE|PROT_EXEC, 0, 0); up_write(¤t->mm->mmap_sem);

Relocation Information ● Global Offset Table(GOT) ● Contains pointers to all global data and codes ● We need to recalculate all addresses if (flags & FLAT_FLAG_GOTPIC) { for (rp = (unsigned long *)datapos; *rp != 0xffffffff;rp++) { unsigned long addr; if (*rp) { addr = calc_reloc(*rp, libinfo, id, 0); if (addr == RELOC_FAILED) return -ENOEXEC; *rp = addr; }

Header of FLAT binary MAGIC version entry data start data end bss end stack size reloc start reloc count flags reserved FLAT_FLAG_RAM FLAT_FLAG_GOTPIC FLAT_FLAG_GZIP Text DATA Relocs BSS Stack

Relocation Information(Cont) ● relocation table ● This is created by elf2flt ● The gcc will assume the following binary striucture – text segment – data segment – bss segment ● The link script must implement this order

XIP relocation ● Two memory segment – text segment: point to filesystem directly. – data,bss segment ● The filesystem must put the entire binary in contiguous blocks. – Otherwise, do_mmap will copy all blocks into contiguous in the RAM. ● The mmnommu/filemap.c: generic_file_mmap