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

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

Credits and Disclaimers

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

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

GCC: the GNU Compiler Collection

2 Compiling a C program

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Stream Model of I/O. Basic I/O in C

PROGRAMMAZIONE I A.A. 2017/2018

Compilation, Disassembly, and Profiling (in Linux)

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

CSE 333 Lecture 7 - final C details

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

GCC: the GNU Compiler Collection

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

Motivation was to facilitate development of systems software, especially OS development.

PROGRAMMAZIONE I A.A. 2017/2018

Topic 6: A Quick Intro To C

Advanced Programming & C++ Language

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

Motivation was to facilitate development of systems software, especially OS development.

Compila(on, Disassembly, and Profiling

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

Machine Program: Procedure. Zhaoguo Wang

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer variable of type FILE.

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

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

Credits and Disclaimers

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

The Compilation Process

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

Compiler Drivers = GCC

Lectures 5-6: Introduction to C

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Design IIIT Kalyani, West Bengal 1. Introduction. Goutam Biswas. Lect 1

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

Exercise Session 2 Simon Gerber

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

TDDE18 & 726G77. Functions

C++ Tutorial AM 225. Dan Fortunato

C - Basics, Bitwise Operator. Zhaoguo Wang

CSE 374 Programming Concepts & Tools

Memory layout. int foo() { int b; } int main() { int a; foo(); Program. Stack. Heap. foo: int b main: int a

System calls and assembler

Errors During Compilation and Execution Background Information

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CSE 333 Lecture 2 Memory

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Obtained the source code to gcc, one can just follow the instructions given in the INSTALL file for GCC.

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

CSE 333 Lecture 6 - data structures

Function Calls and Stack

CSE 333 Autumn 2014 Midterm Key

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CPS104 Recitation: Assembly Programming

CS 470 Spring Mike Lam, Professor. OpenMP

1 You seek performance 3

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

Instruction Set Architectures

Introduction Presentation A

CSE 333 Autumn 2013 Midterm

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Programming. Projects with Multiple Files

The C Preprocessor (and more)!

CIS 341 Midterm March 2, Name (printed): Pennkey (login id): Do not begin the exam until you are told to do so.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

Scientific Computing WS 2018/2019. Lecture 2. Jürgen Fuhrmann Lecture 2 Slide 1

C and Programming Basics

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

The New C Standard (Excerpted material)

The Structure of a C++ Program

CS240: Programming in C

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Brief Assembly Refresher

Memory Allocation in C

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Final CSE 131B Spring 2004

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

Credits and Disclaimers

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Intermediate Programming, Spring 2017*

EL6483: Brief Overview of C Programming Language

Exercise Session 2 Systems Programming and Computer Architecture

Chapter 11 Introduction to Programming in C

CPSC 261 Midterm 1 Tuesday February 9th, 2016

Chapter 11 Introduction to Programming in C

The Compilation Process

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Transcription:

Program Translation Compilation & Linking 1 text C program (p1.c) Compiler (gcc -S) text Asm code (p1.s) binary binary Assembler (gcc or as) Object code (p1.o) Linker (gcc or ld) Executable program (p1) Sta=c libraries (.a)

C Source to Assembly Code Compilation & Linking 2 C program (p1.c) gcc S O0 -Wall m64 p1.c Asm code(p1.s) // p1.c int main() { int x, y, t; x = 5; y = 16; t = x + y; return 0; main:.file "p1.c".text.globl main.type main, @function pushq movq subq addl leave ret %rbp %rsp, %rbp $16, %rsp $5, -4(%rbp) $16, -8(%rbp) -8(%rbp), %eax -4(%rbp), %edx %edx, %eax %eax, -12(%rbp) $0, %eax

Assembly Code to Object Code Compilation & Linking 3 Asm code (p1.s) as 64 o p1.o p1.s Object code (p1.o) main:.file "p1.c".text.globl main.type main, @function pushq movq subq addl leave ret %rbp %rsp, %rbp $16, %rsp $5, -4(%rbp) $16, -8(%rbp) -8(%rbp), %eax -4(%rbp), %edx %edx, %eax %eax, -12(%rbp) $0, %eax 55 89 e5 83 ec 10 c7 45 fc 05 00 00 00 c7 45 f8 10 00 00 00 8b 45 f8 8b 55 fc 8d 04 02 89 45 f4 b8 00 00 00 00 c9 c3

Object Code to Executable Compilation & Linking 4 x86 object (p1.o) 55 89 e5 83 ec 10 c7 45 fc 05 00 00 00 c7 45 f8 10 00 00 00 8b 45 f8 8b 55 fc 8d 04 02 89 45 f4 b8 00 00 00 00 c9 c3 gcc -o p1 m64 p1.o Executable program (p1) 55 89 e5 53 83 ec 04 e8 00 00 00 00 5b 81 c3 74 1d 00 00 8b 93 fc ff ff ff 85 d2 74 05 e8 1e 00 00 00 e8 d5 00 00 00 e8 90 01 00 00 58 5b c9 c3

The Preprocessor Compilation & Linking 5 When a C compiler is invoked, the first thing that happens is that the code is parsed and modified by a preprocessor. The preprocessor handles a collection of commands (commonly called directives), which are denoted by the character '#'. #include directives specify an external file (for now a C library file); the preprocessor essentially copies the contents of the specified file in place of the directive. We will see more interesting preprocessor directives later. #include <stdio.h> int main() { printf("hello, world!\n"); Contents of file stdio.h are copied here. return 0;

Typical C Program Organization Compilation & Linking 6 // single file For very small programs, code is often organized in a single source file; the most common convention uses the extension c for C source files. For more interesting C programs, the code is typically organized into a collection of header files (extension h) and source files. In most cases, the header files contain only type declarations and function prototypes, while the c files contain the corresponding implementations. // a.h // b.h // z.h // main.c // a.c // b.c // z.c #include directives are used within both h and c files to import declarations as necessary.

Using Inclusion Compilation & Linking 7 #include directives are used within both h and cpp files to import declarations as necessary: // main.c #include "A.h" #include "B.h" // A.h uint64_t A(); // B.h int32_t B(int32_t x); A(); // call int32_t x = B(42); // A.c #include A.h uint64_t A() { // do stuff // B.c #include "B.h" int32_t B(int32_t x) { // do stuff

Controlling Access Compilation & Linking 8 Often, a.c file includes some functions (and other things) that are needed only in that file: // B.c #include "B.h" static void Helper(); int32_t B(int32_t x) { // do stuff Helper(); Here, B() calls Helper(), but no other functions outside of the file B.c do so So, we put the declaration of Helper() inside B.c, before any calls to it and make it static and we do NOT declare Helper() in B.h: void Helper() {... // B.h int32_t B(int32_t x);

The Multiple Inclusion Problem Compilation & Linking 9 It is possible to create some unfortunate situations: // main.c #include "A.h" #include "B.h" Here, the preprocessor will copy the text of B.h into main.c twice, once due to the inclusion in A.h and once due to the explicit inclusion in main.c. // A.h #include "B.h" This scenario is difficult to avoid in large projects involving many individuals or teams. However, there is a simple fix using preprocessor directives: // B.h void foo(); // B.h #ifndef B_H #define B_H void foo(); #endif

Example: Fibonacci Compilation & Linking 10 Here's a short program with a test driver, a Fibonacci function in a second file, and a header file containing the declarations needed for the Fibonacci function: #include <stdio.h> #include <inttypes.h> #include "rfibonacci.h" int main() { #ifndef RFIBONACCI_H #define RFIBONACCI_H uint64_t rfibonacci(uint64_t n); #endif for (int i = 0; i < 50; i++) { printf("fibo(%d) = %"PRIu64"\n", i, rfibonacci(i)); return 0; #include "rfibonacci.h" uint64_t rfibonacci(uint64_t n) { if ( n < 2 ) return 1; return rfibonacci(n - 1) + rfibonacci(n - 2);

Compilation Compilation & Linking 11 We can compile either c file without the other (but not produce an executable): #include <stdio.h> #include <inttypes.h> #include "rfibonacci.h" int main() { for (int i = 0; i < 50; i++) { OK: the function call matches this declaration; compiler doesn't need to see function definition. The compiler notes that this call "binds" to this declaration. printf("fibo(%d) = %"PRIu64"\n", i, rfibonacci(i)); return 0;

Compilation Compilation & Linking 12 -c switch tells gcc to compile and exit If compilation is successful, an object file is created; this is a partial translation of the C source file into machine code.

Linking Compilation & Linking 13 The final step in producing an executable is to invoke the linker. The linker resolves the "notes" left by the compiler for external references (like the function name noted earlier), and writes the final executable file. With gcc, the simplest way to do this is to just invoke gcc on the object files

Linking Compilation & Linking 14 -o switch tells gcc what to call executable can invoke gcc with object files to link them and produce executable

Compilation Errors Compilation & Linking 15 The compiler will generate error messages if it cannot resolve the references it encounters: #include <stdio.h> #include <inttypes.h> //#include "rfibonacci.h" int main() { Omit this include directive and the compiler cannot find a declaration that matches the call to rfibonacci(). for (int i = 0; i < 50; i++) { printf("fibo(%d) = %"PRIu64"\n", i, rfibonacci(i));

Compilation Errors Compilation & Linking 16 The compiler will generate error messages if it cannot resolve the references it encounters: #ifndef RFIBONACCI_H #define RFIBONACCI_H Or, if the declaration doesn't match the call, the compiler will generate an error message. uint64_t Fibonacci(uint64_t n); #endif

Linker Errors Compilation & Linking 17 The linker may also produce error messages: #include <stdio.h> #include "rfibonacci.h" int main() { for (int i = 0; i < 50; i++) { #ifndef RFIBONACCI_H #define RFIBONACCI_H uint64_t rfibonacci(uint64_t n); #endif OK printf("fibo(%d) = %lld\n", i, rfibonacci(i)); return 0; #include "rfibonacci.h" uint64_t Fibonacci(uint64_t n) { if ( n < 2 ) return 1; return rfibonacci(n - 1) + rfibonacci(n - 2); Now, linker cannot find a definition (implementation) that matches the function declaration the compiler matched the call to that's an error.

Linker Errors Compilation & Linking 18 Unfortunately, linker error messages are less direct than compiler error messages: The message does not refer to a specific line of source code. But, it does name a specific function and it implies there's a difference between the declaration of that function and the definition (implementation) of that function.

Caesar Cypher Redux Compilation & Linking 19 The Caesar cypher program we saw earlier can be improved by separating the code that manages the input/output from the code that actually performs the translation: // cypher.c #include "caesar.h" uint32_t processfile(int shiftamt, const char* const filename); int main(int argc, char** argv) { uint32_t processfile(int shiftamt, const char* const filename) { // caesar.h #ifndef CAESAR_H #define CAESAR_H char mapchar(char Original, uint8_t shiftamt); #endif // caesar.c #include "caesar.h" char mapchar(char Original, uint8_t shiftamt) {