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

Similar documents
Assembly Language Programming - III

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

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

Buffer Overflow Attack

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

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

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

Lab 10: Introduction to x86 Assembly

You may work with a partner on this quiz; both of you must submit your answers.

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

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

CPEG421/621 Tutorial

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

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

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

CPS104 Recitation: Assembly Programming

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

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

Assembly Language: Function Calls

Compila(on, Disassembly, and Profiling

Assembly Language: Function Calls" Goals of this Lecture"

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

Assembly Language: Function Calls" Goals of this Lecture"

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

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

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

Assembly I: Basic Operations. Jo, Heeseung

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

CS 201 Winter 2014 (Karavanic) Final Exam

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Intel assembly language using gcc

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

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

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

Processes (Intro) Yannis Smaragdakis, U. Athens

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

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

Advanced School in High Performance and GRID Computing November 2008

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

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES. Credit: Mostly Bryant & O Hallaron

CPSC W Term 2 Problem Set #3 - Solution

How Compiling and Compilers Work

Machine Programming 1: Introduction

Instruction Set Architectures

Self-assessment Exercise

BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

CS 537 Lecture 2 - Processes

Systems I. Machine-Level Programming I: Introduction

CIT Week13 Lecture

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

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

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Process Layout, Function Calls, and the Heap

x86 assembly CS449 Fall 2017

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

Code Optimization. What is code optimization?

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

CS241 Computer Organization Spring 2015 IA

THEORY OF COMPILATION

Instruction Set Architectures

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

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

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

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

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

W4118: PC Hardware and x86. Junfeng Yang

Program Building. Goals for this Lecture. Help you learn about: Program structure. Detecting and reporting failure

CMPSC 497 Buffer Overflow Vulnerabilities

Final Exam. Fall Semester 2015 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS241 Computer Organization Spring Loops & Arrays

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

CSE 351: Week 4. Tom Bergan, TA

CS , Fall 2001 Exam 1

Practical Malware Analysis

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

Intro to Threads. Two approaches to concurrency. Threaded multifinger: - Asynchronous I/O (lab) - Threads (today s lecture)

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Princeton University COS 217: Introduction to Programming Systems Spring 2007 Final Exam Answers

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

An Elegant Weapon for a More Civilized Age

238P: Operating Systems. Lecture 7: Basic Architecture of a Program. Anton Burtsev January, 2018

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Stack Debugging. Young W. Lim Mon. Young W. Lim Stack Debugging Mon 1 / 63

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

CS , Fall 2002 Exam 1

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

Linking Oct. 26, 2009"

Stack Discipline Jan. 19, 2018

Machine-Level Programming Introduction

Follow us on Twitter for important news and Compiling Programs

CS , Spring 2002 Exam 2

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Transcription:

HrwCC A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner Universität Salzburg VP Compiler Construction June 26, 2007

Overview 1 Introduction Basic properties Features 2 Selected details Translation process Compilation sample 3 Live demos Features source file Self-compilation

Basic properties HrwCC HrwCC is a self-compiling compiler for a subset of C whose output is linked with hrwcc and executed in the VM hrwvm or assembled with GNU assembler and executed natively.

Features I Some features of hrwcc: Single-pass compiler Strong featured preprocessor (i.e. #define, #ifdef, macros) Use abstract list implemented with macros. Separate compiling (using Makefiles, hrwcc supports -g, -c, -O, -D, -E) Simple optimizations available (-O) output of hrwcc itself reduced from 103.000 to 81.000 loc Calling libc-functions like malloc, open, read, printf

Features II Some selected features of the language: Arrays of primitive types and struct-instances Full pointer support Assign arrays to pointers, incrementing pointers,... C-structs with assigning/passing struct-instances Lazy-evaluation of logical and and or Type safe assignments

Translation process I HrwCC creates GNU assembler code as output. Two possibilites: Linking with hrwcc and executing with own VM hrwvm Assembling and linking with gcc and executing under, lets say, Linux. c c hrwcc -c hrwcc -c s s gnu asm elf Operating System hrwcc exe Virtual Machine c hrwcc -c. s Figure: The build process with hrwcc.

Translation process II GNU assembler output has several advantages: Strong output language Strict separation of compiler and VM Very good testing capabilities Fast output when assembling with gcc Solving the library-function problem: Heap-memory, printf, etc. Compiler was self-compiling at 28. April!

A short compilation sample In the following a short example will be compiled and the output will be shown. #include "../../include/hrwcccomp.h" int main(int argc, char** argv) { printf("%d arguments:\n", argc); return 2; }

The compiler output.section.text #debug: func-def: ( int argc, char * * argv ).globl main.type main, @function main: pushl %ebp movl %esp, %ebp subl $0, %esp #debug: func-call: printf ( "%d arguments:\n", argc ) subl $8, %esp movl $symtab+0, 0(%esp) movl %ebp, %eax addl $8, %eax movl (%eax), %eax movl %eax, 4(%esp) call printf addl $8, %esp #debug: return: return 2 ; movl $2, %eax jmp main_ret main_ret: movl popl ret %ebp, %esp %ebp # The symbol table.section.data symtab:.string "%d arguments:\n"

The optimized compiler output.section.text.globl main.type main,@function main: pushl %ebp movl %esp,%ebp subl $0,%esp subl movl movl movl call addl movl jmp main_ret: movl popl ret.section symtab:.string $8,%esp $symtab+0,0(%esp) 8(%ebp),%eax %eax,4(%esp) printf $8,%esp $2,%eax main_ret %ebp,%esp %ebp.data "%d arguments:\n"

The linker output.section.text call 3 pushl %eax call exit #.globl main [@3] #.type main,@function # main : [@3] pushl %ebp movl %esp,%ebp subl $0,%esp subl $8,%esp movl $17,0(%esp) movl 8(%ebp),%eax movl %eax,4(%esp) call printf addl $8,%esp movl $2,%eax jmp 14 # main_ret : [@14] movl %ebp,%esp popl %ebp ret.section.data # symtab : [@17].string "%d arguments:\n"

Features source file Live: The features.cpp file

Self-compilation Live: make -f Makefile.hrwcc

Bibliography I Stefan Huber, Christian Rathgeb, Stefan Walkner. Hrwcc website. http://www.cs.uni-salzburg.at/~ck/wiki/index.php? n=cc-summer-2007.hrwcc, 2007.

Thanks......for your attention.