Compilation, Disassembly, and Profiling (in Linux)

Similar documents
Compila(on, Disassembly, and Profiling

Instruction Set Architectures

Instruction Set Architectures

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

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

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

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

+ Machine Level Programming: x86-64 History

Systems I. Machine-Level Programming I: Introduction

Today: Machine Programming I: Basics. Machine Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

M2 Instruction Set Architecture

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

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

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

MACHINE-LEVEL PROGRAMMING I: BASICS

Today: Machine Programming I: Basics

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

Chapter 3 Machine-Level Programming I: Basics. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Machine-Level Programming I: Basics

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

Machine Level Programming: Basics

Giving credit where credit is due

Machine Level Programming I: Basics

Machine Level Programming: Basics

Last Time: Floating Point. Intel x86 Processors. Lecture 4: Machine Basics Computer Architecture and Systems Programming ( )

Machine Level Programming I: Basics. Credits to Randy Bryant & Dave O Hallaron

Department of Computer Science and Engineering Yonghong Yan

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

Today: Machine Programming I: Basics. Machine-Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

Machine-level Representation of Programs

Machine-Level Programming Introduction

Systems Programming. Fatih Kesgin &Yusuf Yaslan Istanbul Technical University Computer Engineering Department 18/10/2005

LC-3 Assembly Language

CS429: Computer Organization and Architecture

CS 270 Systems Programming. Debugging Tools. CS 270: Systems Programming. Instructor: Raphael Finkel

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Today: Machine Programming I: Basics. Machine-Level Programming I: Basics. Intel x86 Processors. Intel x86 Evolution: Milestones

Machine-Level Programming I: Basics

Machine Language, Assemblers and Linkers"

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Computer Systems Organization

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

Machine Programming I: Basics

CS429: Computer Organization and Architecture

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Machine Programming 1: Introduction

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Machine-level Representation of Programs


The Hardware/Software Interface CSE351 Spring 2015

The Hardware/Software Interface CSE351 Spring 2015

18-600: Recitation #3

Machine-Level Programming Introduction

Machine-Level Programming I: Basics

Machine- level Programming

CS2141 Software Development using C/C++ Libraries

Lecture 3: Instruction Set Architecture

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

U Reverse Engineering

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

CS 107 Lecture 18: GCC and Make

Introduction to Computer Systems

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

CS241 Computer Organization Spring Data Alignment

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Machine Programming. CSE 351 Autumn Instructor: Justin Hsia

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CSE 351: Week 4. Tom Bergan, TA

Chris Riesbeck, Fall Introduction to Computer Systems

Introduction to Linux (Part II) BUPT/QMUL 2018/03/21

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

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

CS429: Computer Organization and Architecture

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

mp2 Warmup Instructions (Updated 1/25/2016 by Ron Cheung for using VMs)

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

ECE 3210 Laboratory 1: Develop an Assembly Program

Executables and Linking. CS449 Fall 2017

CS631 - Advanced Programming in the UNIX Environment. UNIX development tools

Software. Hardware. x86 basics. ISA View. a brief history of x86 10/6/15. Program, Application. Programming Language. Compiler/Interpreter

Introduction Presentation A

Topics of this Slideset. CS429: Computer Organization and Architecture. x86 Evolution: Programmer s View. Intel x86 Processors

Introduction to Computer Systems

Introduction to Computer Systems

Intro x86 Part 3: Linux Tools & Analysis

Compiler (1A) Young Won Lim 6/8/14

Assembly Language Programming Debugging programs

Credits and Disclaimers

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

Object Files. An Overview of. And Linking. Harry H. Porter III. Goals of this Paper. Portland State University cs.pdx.edu/~harry.

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

PROGRAM COMPILATION MAKEFILES. Problem Solving with Computers-I

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

Machine-Level Programming I Introduction

X86 Assembly -Data II:1

Transcription:

Compilation, Disassembly, and Profiling (in Linux) CS 485: Systems Programming Spring 2016 Instructor: Neil Moore 1

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc O1 p1.c p2.c -o p Use basic optimizations (-O1) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm program (p1.s p2.s) binary binary Assembler (gcc or as) Object program (p1.o p2.o) Linker (gcc or ld) Executable program (p) Static libraries (.a) 2

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc O1 p1.c p2.c -o p Use basic optimizations (-O1) Put resulting binary in file p Can stop the compilation process at any stage. text C program (p1.c p2.c) Compiler (gcc -S) text Asm program (p1.s p2.s) binary binary Assembler (gcc or as) Object program (p1.o p2.o) Linker (gcc or ld) Executable program (p) Static libraries (.a) 3

Invoking the compiler We will use the Gnu command line compilers gcc compiles C programs g++ - compiles C++ programs (and C programs) Useful command line options -o filename Defines the output filename Example: gcc o hello hello.c will create an executable file named hello -E Preprocess only (the same as running the cpp program) Example: gcc E hello.c > hello.i Will run the preprocessor and process header files to create hello.i -c Create an object file (.o) i.e. Compile/Assemble, but do not link Example: gcc c hello.c Will create an object file called hello.o -S Create an assembly language file (.s) i.e., Compile, but do not assemble Example: gcc S hello.c Will create an assembly language file called hello.s 4

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc O1 p1.c p2.c -o p Use basic optimizations (-O1) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) Information is lost at each step of the compilation Process. text Asm program (p1.s p2.s) binary binary Assembler (gcc or as) Object program (p1.o p2.o) Linker (gcc or ld) Executable program (p) Static libraries (.a) 5

Compiling Into Assembly C Code int triplesum(int x, int y) { int t = x+y; return 3*t; } Also some directives like.cfi_startproc (used for debugging), and labels like.lfb0: Obtain with command gcc O1 -S tsum.c Produces file tsum.s CS 485 Systems Programming For example: Variable names are lost Parameters are lost Generated IA32 Assembly triplesum: addl %edi, %esi leal (%rsi,%rsi,2), %eax ret Upon entry: x stored in %edi register y stored in %esi register Then: t replaces y in %esi (%rsi is 64-bit %esi) return value goes to %eax 6

Object Code Code for sum 0000 <triplesum>: 0x01 0xfe 0x8d 0x04 0x76 0xc3 Total of 6 bytes Each instruction here is 1, 2, or 3 bytes (but can be much longer) Placeholder address 0x0000; actual address assigned by linker. Just binary bytes. Most assembly language is lost CS 485 Systems Programming Assembler Translates.s into.o Binary encoding of each instruction Nearly-complete image of executable code Missing linkages between code in different files Linker Resolves references between files Combines with static run-time libraries E.g., code for malloc, printf Some libraries are dynamically linked Linking occurs when program begins execution 7

Invoking the compiler We will use the Gnu command line compilers gcc compiles C programs g++ - compiles C++ programs (and C programs) Useful command line options -o filename Defines the output filename Example: gcc o hello hello.c will create an executable file named hello -E Preprocess only (the same as running the cpp program) Example: gcc E hello.c > hello.i Will run the preprocessor and process header files to create hello.i -c Create an object file (.o) i.e. Compile/Assemble, but do not link Example: gcc c hello.c Will create an object file called hello.o -S Create an assembly language file (.s) i.e., Compile, but do not assemble Example: gcc S hello.c Will create an assembly language file called hello.s -g Add symbol information to the file Useful when debugging and disassembling programs -pg Add profiling information to the file Useful when profiling performance -Olevel Optimize the code using the specified level s optimizations Level 0 is the fewest optimizations, Level 3 is the most optimizations Level g (-Og) optimizes as much as possible without hurting debugging. Example: gcc O3 hello.c 8

Profiling Code Compile with the pg option to gcc gprof commonly installed and used profiling tool for unix-based systems Valgrind more advanced tool that also comes with graphical user interfaces to visualize a program s performance and call graph 9

Gprof concepts Step 1: Add profiling information to the program gcc pg o myprog myprog.c Step 2: Run the program to create gmon.out (profile info)./myprog Step 3: Analyze the performance information View time spent in each procedure gprof p./myprog View call graph gprof q./myprog 10

Disassembling Code There are a variety of tools that can be used to look at compiled code. Some are useful for seeing the assembly language code/instructions objdump -d gdb using the disassemble command Some provide information about the data/variables nm Some are basics tools that can give hints about what is in the file strings od Some are graphical front ends dissy 11

Disassembling Object Code Disassembled 000000000040055d <triplesum>: 40055d: 01 fe add %edi,%esi 40055f: 8d 04 76 lea (%rsi,%rsi,2),%eax 400562: c3 ret Disassembler objdump d filename Useful tool for examining object code Analyzes bit pattern of series of instructions Produces approximate rendition of assembly code Can be run on either a.out (complete executable) or.o file 12

Alternate Disassembly Object 0x40055d: 0x01 0xfe 0x8d 0x04 0x76 0xc3 Disassembled Dump of assembler code for function triplesum: 0x..40055d <+0>: add %edi,%esi 0x..40055f <+2>: lea (%rsi,%rsi,2),%eax 0x..400562 <+5>: ret Within gdb Debugger First run gdb filename Then inside gdb type disassemble sum Disassemble procedure x/6xb sum Examine the 6 bytes starting at sum, as hexadecimal 13