Compiling C Code Philipp Koehn 28 October 2019 Philipp Koehn - - PowerPoint PPT Presentation

compiling c code
SMART_READER_LITE
LIVE PREVIEW

Compiling C Code Philipp Koehn 28 October 2019 Philipp Koehn - - PowerPoint PPT Presentation

Compiling C Code Philipp Koehn 28 October 2019 Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019 C Code 1 Source Code #include <stdlib.h> #include <stdio.h> int main(void) { printf("Hello


slide-1
SLIDE 1

Compiling C Code

Philipp Koehn 28 October 2019

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-2
SLIDE 2

1

C Code

  • Source Code

#include <stdlib.h> #include <stdio.h> int main(void) { printf("Hello world!\n"); return EXIT_SUCCESS; }

  • Compile

linux> gcc -Og hello-world.c

  • Execute

linux> ./a.out Hello world!

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-3
SLIDE 3

2

Compilation Steps

cpp cc1 as ld

.i .c .s .o exe

preprocessor compiler assembler linker

  • C code first gets compiled into assembly code
  • Assembly code is then converted into machine code

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-4
SLIDE 4

3

Even Simpler Program

  • A simple C program:

return47.c #define FOURTYSEVEN 47 int main(void) { return FOURTYSEVEN; }

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-5
SLIDE 5

4

Preprocessor

cpp cc1 as ld

.i .c .s .o exe

preprocessor compiler assembler linker

  • Resolves constants (#define)
  • Adds additional source code (#include)
  • Handles other directives like #ifdef / #endif
  • Example

linux> gcc -Og -E return47.c [...] int main(void) { return 47; }

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-6
SLIDE 6

5

Compiler

cpp cc1 as ld

.i .c .s .o exe

preprocessor compiler assembler linker

  • Compilation into assembly code
  • Example

linux> gcc -Og -S return47.c linux> cat return47.s [...] main: movl $47, %eax ret

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-7
SLIDE 7

6

Assembler

cpp cc1 as ld

.i .c .s .o exe

preprocessor compiler assembler linker

  • Conversion into machine code
  • Example

linux> gcc -Og -c return47.c linux> objdump -d return47.o [...] 0000000000000000 <main>: 0: b8 2f 00 00 00 mov $0x2f,%eax 5: c3 retq

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-8
SLIDE 8

7

Linker

cpp cc1 as ld

.i .c .s .o exe

preprocessor compiler assembler linker

  • Adds start-up code
  • May combine multiple object files
  • Example

linux> gcc -Og return47.c linux> ./a.out linux> echo $? 47 call main main: …

startup main

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-9
SLIDE 9

8

loops

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-10
SLIDE 10

9

Simple Program with For Loop

int main(void) { int sum = 0; for(int i=0;i<100;i++) { sum += i; } return 0; }

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-11
SLIDE 11

10

Assembly Code

main: movl $0, %eax jmp .L2 .L3: addl $1, %eax .L2: cmpl $99, %eax jle .L3 movl $0, %eax ret

  • Wait!
  • -- where is the sum computed?
  • Removed by optimizations in compiler (sum is never used)
  • Compiling with -O9 would also remove loop

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-12
SLIDE 12

11

Use Sum as Return Value

int main(void) { int sum = 0; for(int i=0;i<100;i++) { sum += i; } return sum; }

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-13
SLIDE 13

12

Assembly Code

main: .LFB0: movl $0, %edx movl $0, %eax jmp .L2 .L3: addl %edx, %eax addl $1, %edx .L2: cmpl $99, %edx jle .L3 rep ret

  • Now sum is computed in register %eax (return value)

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-14
SLIDE 14

13

hello world

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-15
SLIDE 15

14

Source Code

#include <stdlib.h> #include <stdio.h> int main(void) { printf("Hello world!\n"); return EXIT_SUCCESS; }

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-16
SLIDE 16

15

Assembly Code

  • Compiled into:

.LC0: .string "Hello world!" .text .globl main .type main, @function main: subq $8, %rsp movl $.LC0, %edi call puts movl $0, %eax addq $8, %rsp ret

  • Calls the function "puts"

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019

slide-17
SLIDE 17

16

Machine Code (Disassembled)

  • Object code

linux> objdump -t hello-world.o [...] 0000000000000000 g F .text 0000000000000018 main 0000000000000000 *UND* 0000000000000000 puts

  • Function "puts" is labeled as undefined (*UND*)
  • Linker resolves this

Philipp Koehn Computer Systems Fundamentals: Compiling C Code 28 October 2019