Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

Similar documents
Buffer Overflow Attack

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

x86 assembly CS449 Fall 2017

x86 assembly CS449 Spring 2016

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

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

CSE 351: Week 4. Tom Bergan, TA

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Lab 10: Introduction to x86 Assembly

CPS104 Recitation: Assembly Programming

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

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

CPEG421/621 Tutorial

Assembly Language Programming - III

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

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

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

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

Assembly Language Programming Debugging programs

CSCI 192 Engineering Programming 2. Assembly Language

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

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

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

W4118: PC Hardware and x86. Junfeng Yang

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

buffer overflow exploitation

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

CS241 Computer Organization Spring 2015 IA

X86 Stack Calling Function POV

System calls and assembler

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

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

GCC and Assembly language. GCC and Assembly language. Consider an example (dangeous) foo.s

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

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

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Comp 524 Spring 2009 Exercise 1 Solutions Due in class (on paper) at 3:30 PM, January 29, 2009.

Machine Programming 1: Introduction

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

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

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Stack Discipline Jan. 19, 2018

x86 Assembly Tutorial COS 318: Fall 2017

Ausgewählte Betriebssysteme. Anatomy of a system call

Intro x86 Part 3: Linux Tools & Analysis

Practical Malware Analysis

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

COMP 210 Example Question Exam 2 (Solutions at the bottom)

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Credits and Disclaimers

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

O S E. Operating-System Engineering. Thread Abstraction Layer Tal

why we shouldn t use assembly compilers generate pre8y fast, efficient code tedious, easy to screw up not portable

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

Function Call Convention

Compila(on, Disassembly, and Profiling

Credits and Disclaimers

Follow us on Twitter for important news and Compiling Programs

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Program Exploitation Intro

Machine Programming 3: Procedures

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Introduction to 8086 Assembly

x86 architecture et similia

Representation of Information

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Process Layout, Function Calls, and the Heap

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ROP It Like It s Hot!

Machine and Assembly Language Principles

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

What You Need to Know for Project Three. Dave Eckhardt Steve Muckle

War Industries Presents: An Introduction to Programming for Hackers Part V - Functions. By Lovepump, Visit:

Assembly III: Procedures. Jo, Heeseung

Chapter 11. Addressing Modes

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Compiler Construction D7011E

Compilation, Disassembly, and Profiling (in Linux)

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

x86 Assembly Crash Course Don Porter

Exercise Session 6 Computer Architecture and Systems Programming

Lecture 04 Control Flow II. Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Based on Michael Bailey s ECE 422

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

U23 - Binary Exploitation

CS 314 Principles of Programming Languages

Computer Architecture and Assembly Language. Practical Session 3

Process Layout and Function Calls

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Transcription:

Simple C Program Helloworld.c Programming and Debugging Assembly under Linux slides by Alexandre Denault int main(int argc, char *argv[]) { } printf("hello World"); Programming and Debugging Assembly under Linux Page 1 Programming and Debugging Assembly under Linux Page 2 Using GCC to produce Assembly Assembly Ouput Assembly produced by GCC is easy to recognize: From the prompt, type : $ gcc -S HelloWorld.c This will produce a file named HelloWorld.s You can produce cleaner assembly code using the on flag, N being the optimization level $ gcc -S -o3 HelloWorld.c Programming and Debugging Assembly under Linux Page 3.file "HelloWorld.c".section.rodata.LC0:.string "Hello World".text.globl main.type main,@function main: pushl %ebp movl %esp, %ebp subl $8, %esp pushl $.LC0 call printf addl $16, %esp leave ret.lfe1:.size main,.lfe1-main.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2 Programming and Debugging Assembly under Linux Page 4

Mixing C and Assembly Seperate File Compilation Sometimes, only a particular function needs to be written in assembly. GCC allows several way to mix C code and assembly code. Seperate files for C code and assembly code Inlining assembly code directly in the C code Adding assembly code to your C code makes your project less portable. Unless special precaution are taken, you can only compile on one architecute. C code should be stored in.c files. Assembly code should be stored in.s files. (this is case sensitive,.s are different types of files) The C code should have the function prototype of any function called from assembly. Functions in the assembly code that will called from C should be declared global. Programming and Debugging Assembly under Linux Page 5 Programming and Debugging Assembly under Linux Page 6 C program that uses assembly Assembly function called from C cprogram.c add.s.text int add(int x, int y); int main(void) { int i,j,k; } i = 2; j = 3; k = add(i, j); return k;.globl add.type add, @function add: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax popl %ebp ret Programming and Debugging Assembly under Linux Page 7 Programming and Debugging Assembly under Linux Page 8

Compiling.s and.c files Inlining assembly in C code Once both your.c and.s files are ready, you need to compile them togheter. This can be done by specifying both filenames to the gcc command. gcc cprogram.c add.s Don't forget the -g flag if you want to debug your new executable. If you only want to add a few lines of assembly in your C code, it might be preferable to inlining the assembly code. This can be done using the asm function. GCC will have a harder time optimizing your code if you use the asm directive. You can use the volatile keyword to prevent GCC from optimizing out your code. Programming and Debugging Assembly under Linux Page 9 Programming and Debugging Assembly under Linux Page 10 Using the asm function Using GDB to trace program execution add.c int add(int x, int y) { } asm volatile ("movl 12(%ebp), %eax"); asm volatile ("addl 8(%ebp), %eax"); It is possible to have more than one assembly instruction in the asm function. More information asm is available on the Internet. First, you must compile the executable with the g flag $ gcc HelloWorld.s -g -o HelloWorld Please note that there is a difference between s and capital S in the extension As taken from the man page: file.s : Assembly code. file.s :Assembly code which must be preprocessed. Programming and Debugging Assembly under Linux Page 11 Programming and Debugging Assembly under Linux Page 12

Using GDB on Solaris 8 (Sparc) When working on the Sparc architecture, please try to use the gcc and as command found in the /usr/local/pkgs if you are planning to use the gdb debugger. First, you must compile the assembler with gas to produce an object file with debugging information. The as normally found on Solaris does not produce debugging information we can use with gdb. $ /usr/local/pkgs/binutils-2.14/bin/as -gstabs addition.s -o addition.o Second, you must use gcc to generate an executable from that object file. You should also add any C files (if needed) that are needed to produce an executable. $ /usr/local/pkgs/gcc3.2.2/bin/gcc cprogram.c addition.o -o addition Programming and Debugging Assembly under Linux Page 13 Starting GDB Any executable compiled with the g flag can be used with gdb. $ gdb HelloWorld GNU gdb Red Hat Linux (5.3post-0.20021129.18rh) Copyright 2003 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"... (gdb) Programming and Debugging Assembly under Linux Page 14 Getting Help Breakpoints When using GDB, you can always type in the help command to get a list of availible commands. (gdb) help You can also get help on a specific command by typing help and the name of that command. (gdb) help breakpoints Making program stop at certain points. First step is set a breakpoint so we can slowly trace the execution of the application. This can be done using the break command. (gdb) break 1 Breakpoint 1 at 0x8048328: file HelloWorld.s, line 1. Programming and Debugging Assembly under Linux Page 15 Programming and Debugging Assembly under Linux Page 16

Running step-by-step Content of registers We next to start the application. This is done using the run command. (gdb) run Starting program: /home/adenau/comp573/helloworld Breakpoint 1, main () at HelloWorld.s:10 10 pushl %ebp Current language: auto; currently asm We can advance to the next instruction using the nexti command. (gdb) nexti 11 movl %esp, %ebp The print command can be used to print out values of registers and variables. (gdb) print $ebp $1 = (void *) 0xbffff6b8 The x command allows you to examine a block of memory. It has several options. (gdb) x /8xw 0xbffff6b8 0xbffff6b8: 0xbffff6d8 0x42015574 0xbffff6c8: 0xbffff70c 0x4001582c Programming and Debugging Assembly under Linux Page 17 Programming and Debugging Assembly under Linux Page 18 Complete Register Information The info register command gives us a quick overview of the content of every register. (gdb) info registers eax 0x1 1 ecx 0x42015554 1107383636 edx 0x40016bc8 1073834952 ebx 0x42130a14 1108544020 esp 0xbffff6b8 0xbffff6b8 ebp 0xbffff6d8 0xbffff6d8 esi 0x40015360 1073828704 edi 0x8048370 134513520 eip 0x8048329 0x8048329 eflags 0x346 838 cs 0x23 35 ss 0x2b 43 ds 0x2b 43 es 0x2b 43 fs 0x0 0 gs 0x33 51 Programming and Debugging Assembly under Linux Page 19 Continual tracing information Sometimes, it s more convenient to have trace information printed out after every instruction. This can be done using the display command. (gdb) display $ebp 1: $ebp = (void *) 0xbffff6d8 (gdb) display $esp 2: $esp = (void *) 0xbffff6b8 (gdb) nexti main () at HelloWorld.s:12 12 subl $8, %esp 2: $esp = (void *) 0xbffff6b8 1: $ebp = (void *) 0xbffff6b8 The command undisplay cancels a display request. Programming and Debugging Assembly under Linux Page 20

For more information on GDB More information about GDB is available on the Internet (tutorials, examples, etc). Many other commands allow you to fine tune the debugging process. Programming and Debugging Assembly under Linux Page 21 ABI Compliance Certain architecture allow a great deal of freedom on how registers and memory are used. ( ex: Sparc, PowerPC). The System V Application Binary Specification (ABI) are use to establish standards on how system resources are used. The ABI usually defines conventions on register usage, stack format, etc. For x86, we can avoid having to look at the ABI specification because we have the The Floppy Textbook. Programming and Debugging Assembly under Linux Page 22 References The Floppy Textbook ( 1999 edition ) Using the GNU Assembler (gas) http://www.gnu.org/manual/gas/html_chapter/as_toc.html Man Pages : Gcc and Gdb GDB Tutorial http://www.cs.princeton.edu/%7ebenjasik/gdb/gdbtut.html Programming and Debugging Assembly under Linux Page 23