Link 4. Relocation. Young W. Lim Wed. Young W. Lim Link 4. Relocation Wed 1 / 22

Similar documents
Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 26

Link 4. Relocation. Young W. Lim Sat. Young W. Lim Link 4. Relocation Sat 1 / 33

Link 4. Relocation. Young W. Lim Mon. Young W. Lim Link 4. Relocation Mon 1 / 35

Link 4. Relocation. Young W. Lim Tue. Young W. Lim Link 4. Relocation Tue 1 / 38

Link 4. Relocation. Young W. Lim Thr. Young W. Lim Link 4. Relocation Thr 1 / 48

Link 2. Object Files

Link 2. Object Files

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Link 7. Static Linking

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Link Edits and Relocatable Code

Link 7. Dynamic Linking

Link 7.A Static Linking

ELF (1A) Young Won Lim 10/22/14

Link 8.A Dynamic Linking

Linking. Today. Next time. Static linking Object files Static & dynamically linked libraries. Exceptional control flows

CSE 333 Lecture 2 Memory

E = 2 e lines per set. S = 2 s sets tag. valid bit B = 2 b bytes per cache block (the data) CSE351 Inaugural EdiNon Spring

Example C program. 11: Linking. Why linkers? Modularity! Static linking. Why linkers? Efficiency! What do linkers do? 10/28/2013

CS , Fall 2001 Exam 1

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

Compiler Drivers = GCC

Example C Program The course that gives CMU its Zip! Linking March 2, Static Linking. Why Linkers? Page # Topics

Binghamton University. CS-220 Spring Loading Code. Computer Systems Chapter 7.5, 7.8, 7.9

GAS Tutorial - 4. Sections & Relocation

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

Intro x86 Part 3: Linux Tools & Analysis

CS , Fall 2004 Exam 1

Linking February 24, 2005

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Lecture 16: Linking Computer Architecture and Systems Programming ( )

Linking. Explain what ELF format is. Explain what an executable is and how it got that way. With huge thanks to Steve Chong for his notes from CS61.

CSC 405 Computer Security Stack Canaries & ASLR

Linking and Loading. CS61, Lecture 16. Prof. Stephen Chong October 25, 2011

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

GAS Tutorial - 6. Expression

A SimplisHc Program TranslaHon Scheme. TranslaHng the Example Program. Example C Program. Why Linkers? - Modularity. Linking

CS , Spring 2004 Exam 1

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

Outline. 1 Background. 2 ELF Linking. 3 Static Linking. 4 Dynamic Linking. 5 Summary. Linker. Various Stages. 1 Linking can be done at compile.

CMSC 313 Lecture 12 [draft] How C functions pass parameters

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES. Credit: Mostly Bryant & O Hallaron

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Example C Program. Linking CS Instructor: Sanjeev Se(a. int buf[2] = {1, 2}; extern int buf[]; int main() { swap(); return 0; }

Lecture 2 Assembly Language

Full Name: CISC 360, Fall 2008 Example of Exam

Day21 A. Young W. Lim Wed. Young W. Lim Day21 A Wed 1 / 13

Machine Programming 1: Introduction

Buffer Overflow Attack

CS241 Computer Organization Spring Buffer Overflow

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

CS , Fall 2002 Exam 1

Overview REWARDS TIE HOWARD Summary CS 6V Data Structure Reverse Engineering. Zhiqiang Lin

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Machine-Level Programming I: Introduction Jan. 30, 2001

Accessibility (1A) Young Won Lim 8/22/13

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

Sungkyunkwan University

ELF (1A) Young Won Lim 3/24/16

Systems I. Machine-Level Programming I: Introduction

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

Introduction Presentation A

CS 3214 Spring # Problem Points Min Max Average Median SD Grader. 1 Memory Layout and Locality Bill

Revealing Internals of Linkers. Zhiqiang Lin

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

CS 3214 Computer Systems. Do not start the test until instructed to do so! printed

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

Exercise Session 7 Computer Architecture and Systems Programming

CSC 405 Computer Security Reverse Engineering Part 1

Link 8. Dynamic Linking

CS , Spring 2002 Exam 2

CSE 2421: Systems I Low-Level Programming and Computer Organization. Linking. Presentation N. Introduction to Linkers

15-213/18-243, Spring 2011 Exam 1

Machine Language, Assemblers and Linkers"

LINKING. Jo, Heeseung

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Machine-Level Programming Introduction

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Machine Programming 4: Structured Data

CS 33. Linkers. CS33 Intro to Computer Systems XXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Labeling Library Functions in Stripped Binaries

Sungkyunkwan University

Instruction Set Architectures

Transcription:

Link 4. Relocation Young W. Lim 2017-09-13 Wed Young W. Lim Link 4. Relocation 2017-09-13 Wed 1 / 22

Outline 1 Linking - 4. Relocation Based on Relocation Relocation Entries Relocating Symbol Reference Reloation Examples Young W. Lim Link 4. Relocation 2017-09-13 Wed 2 / 22

Based on "Self-service Linux: Mastering the Art of Problem Determination", Mark Wilding "Computer Architecture: A Programmer s Perspective", Bryant & O Hallaron I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Link 4. Relocation 2017-09-13 Wed 3 / 22

Relocation after symbol resolution each symbol reference in the code has exactly only one symbol definition a symbol table entry in one of its input object modules the linker knows the exact sizes of the code sections the exact sizes of the data sections the relocation merges the input modules and assigns run-time address to each symbol Young W. Lim Link 4. Relocation 2017-09-13 Wed 4 / 22

Relocating sections and symbol definitions the linker merges all sections of the same type into a new aggregate section of the same type.data sections from the input modules are all merged into a new.data section for the output executable object file the linker assigns run-time memory addresses to the new aggregated sections to each section defined by the input modules to each symbol defined by the input modules finally, every instruction and global variable has a unique run-time memory addresses Young W. Lim Link 4. Relocation 2017-09-13 Wed 5 / 22

Relocating symbol references within sections the linker modifies every symbol reference in the bodies of the code and data sections so that they point to the correct run-time addresses the linker relies on data structures in the relocatable object modules known as relocation entries Young W. Lim Link 4. Relocation 2017-09-13 Wed 6 / 22

Relocation Entry Whenever the assembler encounters a reference to an object whose ultimate address is not known, the assembler generates a relocation entry that tells the linker how to modify the reference when the linker merges the object files into an executable.relo.text : relocation entries for code.relo.data : relocation entries for initialized data Young W. Lim Link 4. Relocation 2017-09-13 Wed 7 / 22

ELF Relocation Entry offset : the section offset of the reference which will be modified symbol : the symbol that the modified reference should point to type : tells the linker how to modify the new reference 11 different relocation types in ELF R_386_PC32 R_386_32 typedef struct { int offset; int symbol:24, type:8; } Elf32_Rel; // offset of the reference to relocate symbol // the reference should point to // relocation type Young W. Lim Link 4. Relocation 2017-09-13 Wed 8 / 22

Basic ELF Relocation Types : R_386_PC32 relocating a reference that uses a 32-bit PC-relative address a PC-relative address : an offset from the current run-time value of the (PC) the program coutner (PC) : the address of the next instruction to be executed the effective address : the target of the call instruction adding 32-bit value encoded in the instruction to the current value of the PC Young W. Lim Link 4. Relocation 2017-09-13 Wed 9 / 22

Basic ELF Relocation Types : R_386_32 relocating a reference that uses a 32-bit absolute address the effective address : the 32-bit value encoded in the instruction Young W. Lim Link 4. Relocation 2017-09-13 Wed 10 / 22

Relocation Algorithm in pseudocode foreach section s { foreach relocation entry r { refp = s + r.offset; // p to references to be relocated // relocate a PC-relative references if (r.type == R_386_PC32) { refaddr = ADDR(s) + r.offset; *refp = (unsigned) (ADDR(r.symbol) + *refp - refaddr; } // relocate an absolute reference if (r.type == R_386_3) { *refp = (unsigned) (ADDR(r.symbol) + *refp); } } } Young W. Lim Link 4. Relocation 2017-09-13 Wed 11 / 22

Relocation Algorithm (1) s and r s : one of sections r : an entry of the relocation table in a section each entry for a reference (instance) of a symbol each section s is an array of bytes each relocation entry r is a struct of type Elf32_Rel assume that when the algorithm runs, the linker has already chosen run-time addresses for each section (ADDR(s)) run-time addresses for each symbol (ADDR(r.symbol) Young W. Lim Link 4. Relocation 2017-09-13 Wed 12 / 22

Relocation Algorithm (2) r.offset, r.type, r.symbol r.offset : the section offset of the reference to be modified r.type : how to modify for a new reference r.symbol : the symbol that the modified reference should point to the buf symbol in swap.o 4 instances in swap.o each instance is called as a reference offset address of each instance of a symbol typedef struct { int offset; int symbol:24, type:8; } Elf32_Rel; // offset of the reference to relocate symbol // the reference should point to // relocation type Young W. Lim Link 4. Relocation 2017-09-13 Wed 13 / 22

Relocation Algorithm (3) a reference : an instance of a symbol in a object file refp : pointer to the reference to be relocated refaddr : the run-time address of the reference refp = s + r.offset refaddr = ADDR(s) + r.offset *refp = (unsigned) (ADDR(r.symbol) + *refp - refaddr) Young W. Lim Link 4. Relocation 2017-09-13 Wed 14 / 22

Example Program // swap.c --------------------- // main.c --------------------- void swap(); int buf[2] = {1, 2}; int main() { int tmp; swap(); return 0; } extern int buf[]; int *p0 = &buf[0]; int *p1; void swap() { int tmp; p1 = &buf[1]; tmp = *p0; *p0 = *p1; *p1 = tmp; } Young W. Lim Link 4. Relocation 2017-09-13 Wed 15 / 22

objdump -d main.o (disassemble) main.o: formato del fichero elf32-i386 Desensamblado de la sección.text.startup: 00000000 <main>: 0: 8d 4c 24 04 lea 0x4(%esp),%ecx 4: 83 e4 f0 and $0xfffffff0,%esp 7: ff 71 fc pushl -0x4(%ecx) a: 55 push %ebp b: 89 e5 mov %esp,%ebp d: 51 push %ecx e: 83 ec 04 sub $0x4,%esp 11: e8 fc ff ff ff call 12 <main+0x12> 16: 83 c4 04 add $0x4,%esp 19: 31 c0 xor %eax,%eax 1b: 59 pop %ecx 1c: 5d pop %ebp 1d: 8d 61 fc lea -0x4(%ecx),%esp 20: c3 ret Young W. Lim Link 4. Relocation 2017-09-13 Wed 16 / 22

objdump -t main.o (symbol table) main, swap, buf main.o: formato del fichero elf32-i386 SYMBOL TABLE: 00000000 l df *ABS* 00000000 main.i 00000000 l d.text 00000000.text 00000000 l d.data 00000000.data 00000000 l d.bss 00000000.bss 00000000 l d.text.unlikely 00000000.text.unlikely 00000000 l d.text.startup 00000000.text.startup 00000000 l d.note.gnu-stack 00000000.note.GNU-stack 00000000 l d.eh_frame 00000000.eh_frame 00000000 l d.comment 00000000.comment 00000000 g F.text.startup 00000021 main 00000000 *UND* 00000000 swap 00000000 g O.data 00000008 buf Young W. Lim Link 4. Relocation 2017-09-13 Wed 17 / 22

objdump -r main.o (relocation entry) swap, R_386_PC32, 00000012 main.o: formato del fichero elf32-i386 RELOCATION RECORDS FOR [.text.startup]: OFFSET TYPE VALUE 00000012 R_386_PC32 swap RELOCATION RECORDS FOR [.eh_frame]: OFFSET TYPE VALUE 00000020 R_386_PC32.text.startup Young W. Lim Link 4. Relocation 2017-09-13 Wed 18 / 22

objdump -d swap.o (disassemble) swap.o: formato del fichero elf32-i386 Desensamblado de la sección.text: 00000000 <swap>: 0: a1 00 00 00 00 mov 0x0,%eax 5: 8b 0d 04 00 00 00 mov 0x4,%ecx b: c7 05 00 00 00 00 04 movl $0x4,0x0 12: 00 00 00 15: 8b 10 mov (%eax),%edx 17: 89 08 mov %ecx,(%eax) 19: 89 15 04 00 00 00 mov %edx,0x4 1f: c3 ret Young W. Lim Link 4. Relocation 2017-09-13 Wed 19 / 22

objdump -t swap.o (symbol table) swap, p0, buf, p1 SYMBOL TABLE: 00000000 l df *ABS* 00000000 swap.i 00000000 l d.text 00000000.text 00000000 l d.data 00000000.data 00000000 l d.bss 00000000.bss 00000000 l d.text.unlikely 00000000.text.unlikely 00000000 l d.note.gnu-stack 00000000.note.GNU-stack 00000000 l d.eh_frame 00000000.eh_frame 00000000 l d.comment 00000000.comment 00000000 g F.text 00000020 swap 00000000 g O.data 00000004 p0 00000000 *UND* 00000000 buf 00000004 O *COM* 00000004 p1 Young W. Lim Link 4. Relocation 2017-09-13 Wed 20 / 22

objdump -r swap.o (relocation entry) p0, p1, buf(3 references in.text and 1 in.data) swap.o: formato del fichero elf32-i386 RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 00000001 R_386_32 p0 00000007 R_386_32 buf 0000000d R_386_32 p1 00000011 R_386_32 buf 0000001b R_386_32 buf RELOCATION RECORDS FOR [.data]: OFFSET TYPE VALUE 00000000 R_386_32 buf RELOCATION RECORDS FOR [.eh_frame]: OFFSET TYPE VALUE 00000020 R_386_PC32.text Young W. Lim Link 4. Relocation 2017-09-13 Wed 21 / 22

Relocating PC-Relative References main in the.text of main.o calls swap in swap.o Young W. Lim Link 4. Relocation 2017-09-13 Wed 22 / 22