Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.

Slides:



Advertisements
Similar presentations
Smashing the Stack for Fun and Profit
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Position Independent Code self sufficiency of combining program.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
September 22, 2014 Pengju (Jimmy) Jin Section E
Practical Session 8 Computer Architecture and Assembly Language.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Hello ASM World: A Painless and Contextual Introduction to x86 Assembly rogueclown DerbyCon 3.0 September 28, 2013.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 4.
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
CNIT 127: Exploit Development Ch 1: Before you begin.
Practical Session 4 Computer Architecture and Assembly Language.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
Practical Session 5 Computer Architecture and Assembly Language.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Architecture and Assembly Language
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
Practical Session 6 Computer Architecture and Assembly Language.
Practical Session 3.
Computer Architecture and Assembly Language
Practical Session 5.
Assemblers, linkers, loaders
Computer Architecture and Assembly Language
Introduction to Information Security
Assembly language.
Data Transfers, Addressing, and Arithmetic
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Homework Reading Machine Projects Labs PAL, pp ,
Computer Architecture and Assembly Language
Exploiting & Defense Day 2 Recap
Computer Architecture and Assembly Language
Assembly Language Programming I: Introduction
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
EECE.3170 Microprocessor Systems Design I
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Practical Session 8

Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs internally PIC can be placed somewhere in memory, executes properly regardless of its absolute address PIC can be added to any other program, without fear that it might not work

No direct usage of labels Only relative jumps (“call”) One section only No library functions only system calls Position Independent Code- requirements

No direct usage of labels Labels are resolved by the assembler and linker at compile time to an absolute address. If the code is moved (in an appropriate section), the absolute address that was calculated before the moving wont be correct any more.

No direct usage of labels An absolute address of STR1 and STR2 would be resolved based on the relative address of STR1 and STR2 in.rodata section (which may change if all the code would be moved)

We can use only relative jumps because the code we wish to jump to may change its position. If all the code changes its position, relative jumps are still valid, because the address difference is preserved. We can use “call” instruction because it makes a relative jump to the function (which means that the new IP (after “call”) will be the old IP (before “call”) plus a number (may be negative) with was the address difference of the two places calculated by the assembler). Only relative jumps

Only relative jumps - example The first column (from the left) is simply the line number in the listing and is otherwise meaningless The second column is the relative address, in hex, of where the code will be placed in memory The third column is the actual compiled code For the normal type of call in 32- bit mode (relative near call), the binary code for ‘CALL myFunc’ is the opcode E8 followed by a 4-byte value that specifies the target address, relative to the next instruction after the call.  Address of myFunc label = 0x1F  Address of the next instruction after the call (i.e. ‘mov [answer], eax’) is 0xA  0x1F-0xA=0x15, and we get exactly the binary code written here ‘E ’

One section only We put all the code in a single section –.text (read- only) or.data (read-write). Both.text and.data sections may contain any valid assembly instruction. Usage of a single section gives us a possibility to calculate a difference between each pair of code instructions, and thus execute relative jumps.

No library functions only system calls We don’t know if and where the library functions are.Thus there are no “printf” “gets” and so on… To perform I/O operation we have to use the Linux system calls because INT 0x80 isn’t a regular procedure - it is called via the interrupt table which is static.

Since ‘call’ instruction executes a relative jump, we may ‘call’ functions that are defined in PIC ‘call’ instruction pushes the return address at run-time Thus we may calculate a run-time address of any label relatively to ‘call’ return address Finding a code address at run-time

get_my_loc function gets the address of ‘next_i’ label at run-time get_my_loc: call next_i next_i: pop edx ret

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h ESP stack Using strings – PIC example

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h ESP address of ‘sub edx, next_i-name’ stack Using strings – PIC example

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h ESP address of ‘sub edx, next_i-name’ address of ‘next_i’ stack Using strings – PIC example

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx; edx gets ‘next_i’ address ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h ESP address of ‘sub edx, next_i-name’ stack Using strings – PIC example

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret ; EIP gets address of ‘sub edx, next_i-name’ _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h ESP stack Using strings – PIC example

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name ; edx = ‘next_i’ address – (‘next_i’ address – ‘name’ address) mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h Using strings – PIC example the address difference between “next_i” and “name” is constant even if the code changes it’s position

section.text name: db "Moshe",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 int 80h Using strings – PIC example why we may use ‘nameLen’ label directly ?

Using strings – PIC example 0x0C = ‘next_i’ – ‘name’ >nasm -f elf sample.s -l sample.lst