Practical Session 8 Computer Architecture and Assembly Language.

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.
David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
Introduction to X86 assembly by Istvan Haller
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.
CS2422 Assembly Language & System Programming September 26, 2006.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
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.
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, _,
Lecture-1 Compilation process
Introduction to Information Security מרצים : Dr. Eran Tromer: Prof. Avishai Wool: מתרגלים : Itamar Gilad
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.
ELF binary # readelf -a foo.out ELF Header:
CNIT 127: Exploit Development Ch 1: Before you begin.
Practical Session 4 Computer Architecture and Assembly Language.
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.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
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.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Practical Session 6 Computer Architecture and Assembly Language.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Practical Session 3.
Section 5: Procedures & Stacks
Computer Architecture and Assembly Language
Practical Session 5.
Assemblers, linkers, loaders
Computer Architecture and Assembly Language
Introduction to Information Security
Assembly language.
Problem Identification
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
asum.ys A Y86 Programming Example
Computer Architecture and Assembly Language
Assembly Language Programming I: Introduction
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
Multi-modules programming
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 Computer Architecture and Assembly Language

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

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

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 addresses, and thus execute relative jumps.

No direct usage of labels An absolute addresses of STR1 and STR2 would be resolved based on the addresses of STR1 and STR2 in.rodata section (which may be changed if the strings would be moved in their section) Labels are resolved at compile time to absolute address If the code is moved, the absolute address that was calculated before the moving would not be correct any more

Only relative jumps  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 ’ If all the code changes its position, relative jumps are still valid, because the address difference is preserved.

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.

section.text name: db " Hello ",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 labels – PIC example

section.text name: db " Hello ",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 labels – PIC example

section.text name: db " Hello ",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 labels – PIC example

section.text name: db " Hello ",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx; edx gets address of ‘next_i’ 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 labels – PIC example

section.text name: db " Hello ",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 labels – PIC example

section.text name: db " Hello ",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 labels – PIC example the address difference between “next_i” and “name” is constant even if the code changes it’s position

section.text name: db " Hello ",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 labels – PIC example why we may use ‘nameLen’ label directly ?

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