Computer Architecture and System Programming Laboratory

Slides:



Advertisements
Similar presentations
Hello World!. PC / MS-DOS code segment para assume cs:code,ds:code org 0100h start: mov dx,offset message ;point to message mov ah,09h ;func# to printstring.
Advertisements

C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Practical session 7 review. Little – Endian What’s in memory? Section.rodata a: DB ‘hello’, 0x20, ’world’, 10, 0 b: DW ‘hello’, 0x20, ’world’, 10, 0 c:
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
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.
Position Independent Code self sufficiency of combining program.
Practical Session 8 Computer Architecture and Assembly Language.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Lecture-1 Compilation process
Computer Architecture and Assembly Language. Byte structure : a byte has 8 bits MSB (Most Significant Bit) LSB (Least Significant Bit) Data Representation.
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.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
ELF binary # readelf -a foo.out ELF Header:
CNIT 127: Exploit Development Ch 1: Before you begin.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Architecture and Assembly Language
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.
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.
Paradyn Project Paradyn / Dyninst Week Madison, Wisconsin April 12-14, 2010 Paradyn Project Safe and Efficient Instrumentation Andrew Bernat.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Practical Session 6 Computer Architecture and Assembly Language.
Practical Session 3.
Computer Architecture and Assembly Language
Practical Session 5.
Computer Architecture and Assembly Language
Assembly language.
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Additional Assembly Programming Concepts
Computer Architecture and Assembly Language
Exploiting & Defense Day 2 Recap
High-Level Language Interface
Practical Session 7.
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 9.
Computer Architecture and Assembly Language
Practical Session 4.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Machine-Level Programming: Introduction
Multi-modules programming
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
ICS51 Introductory Computer Organization
Computer Architecture and Assembly Language
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
Computer Architecture and System Programming Laboratory
Presentation transcript:

Computer Architecture and System Programming Laboratory TA Session 9 PIC – Position Independent Code

any memory address without modification Position Independent Code (PIC) can be executed at any memory address without modification

Would the process run correctly ? RAM RAM section .data var: dd 0x10 str: db "The result is %d",10,0 extern printf global main section .text main: call func push dword [var] push str call printf add esp, 8 mov eax,1 int 0x80 func: push ebp mov ebp, esp mov ecx, 3 calcLoop: add dword [var],2 loop calcLoop pop ebp ret Executable binary file (partially): .data start address content .text .text “confused” loader loader .text What if a loader is “confused” and place the sections in wrong position ? Would the process run correctly ? .data If the program is written in PIC manner, it would run despite of the wrong position. Otherwise, it would not. .data

0x10 … ‘h’ ‘T’ .data 0xfffffebb=-0x145 str = 0x0804a020 0xfffffebb=-0x145 Offset of printf function in section .text 0x10 var = 0x0804a01c

No direct usage of labels No library functions Indirect usage of labels Variables address detection in run time  only system calls  (their code is not necessarily PIC and thus not safe to use)  jump, call, loop commands are PIC  (note that there are several types of jump and call commands, not all of them are safe)  singe section code

stack section .data var: dd 0x10 section .text global main main: add dword [var],2 mov eax,1 int 0x80 section .data var: dd 0x10 get_my_loc: call next_i next_i: pop ecx ret global main main: call get_my_loc sub ecx, next_i – var add dword [ecx],2 mov eax,1 int 0x80 stack ESP

address of ‘sub ecx, next_i-name’ section .data var: dd 0x10 section .text global main main: add dword [var],2 mov eax,1 int 0x80 section .data var: dd 0x10 get_my_loc: call next_i next_i: pop ecx ret global main main: call get_my_loc sub ecx, next_i – var add dword [ecx],2 mov eax,1 int 0x80 stack address of ‘sub ecx, next_i-name’ ESP

address of ‘sub ecx, next_i-name’ section .data var: dd 0x10 section .text global main main: add dword [var],2 mov eax,1 int 0x80 section .data var: dd 0x10 get_my_loc: call next_i next_i: pop ecx ret global main main: call get_my_loc sub ecx, next_i – var add dword [ecx],2 mov eax,1 int 0x80 stack address of ‘sub ecx, next_i-name’ address of ‘next_i’ ESP

address of ‘sub ecx, next_i-name’ section .data var: dd 0x10 section .text global main main: add dword [var],2 mov eax,1 int 0x80 section .data var: dd 0x10 get_my_loc: call next_i next_i: pop ecx ret global main main: call get_my_loc sub ecx, next_i – var add dword [ecx],2 mov eax,1 int 0x80 stack address of ‘sub ecx, next_i-name’ ESP ; ecx gets address of ‘next_i’

stack section .data var: dd 0x10 section .text global main main: add dword [var],2 mov eax,1 int 0x80 section .data var: dd 0x10 get_my_loc: call next_i next_i: pop ecx ret global main main: call get_my_loc sub ecx, next_i – var add dword [ecx],2 mov eax,1 int 0x80 stack ESP

section .data var: dd 0x10 section .text global main main: add dword [var],2 mov eax,1 int 0x80 section .data var: dd 0x10 get_my_loc: call next_i next_i: pop ecx ret global main main: call get_my_loc sub ecx, next_i – var add dword [ecx],2 mov eax,1 int 0x80 stack ESP ; ecx = ‘next_i’ address – (‘next_i’ address – ‘var’ address) the address difference between “next_i” and “var” is constant even if the code changes it’s position

Does this code is PIC ? section .text name: db "Hello",10 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop ecx ret _start: call get_my_loc sub ecx, next_i – name mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 Does this code is PIC ?

Does this code is PIC ? may we use ‘nameLen’ label directly ? section .text name: db "Hello",10 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop ecx ret _start: call get_my_loc sub ecx, next_i – name mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 Does this code is PIC ? may we use ‘nameLen’ label directly ?