SlideShare a Scribd company logo
1 of 48
Download to read offline
Compiling Fundamentals
15-213/15-513/14-513: Introduction to Computer Systems
Questions that will be answered today
ā— What does it mean to compile code?
ā— What does compiling code look like?
ā— How can code be compiled?
ā— What are Makeļ¬les?
ā— It is important to understand how programs are compiled to have a better understanding of
how different parts of a computer interact with each other.
ā— Fundamental aspect of how computers run code.
Why is this important?
Levels of Abstraction
ā— C [and other high level languages]
are easy for programmers to
understand, but computers
require lots of software to process
them
ā— Machine code is just the opposite:
easy for the computer to process,
humans need lots of help to
understand it
ā— Assembly language is a
compromise between the two:
readable by humans (barely), close
correspondence to machine code
What does it mean to compile code?
ā— The computer only understands
machine code directly
ā— All other languages must be either
ā—‹ interpreted: executed by software
ā—‹ compiled: translated to machine
code by software
What does it mean to compile code?
ā— Computer follows steps to translate your
code into something the computer can
understand
ā— This is the process of compiling code [a
compiler completes these actions]
ā— Four steps for C: preprocessing, compiling,
assembling, linking
ā—‹ Most other compiled languages donā€™t
have the preprocessing step, but do
have the other three
Stepping through the stages
ā— Pre-Processor
ā—‹ $ gcc -E [ļ¬‚ags] [ļ¬lenames]
ā— Compiler
ā—‹ $ gcc -S [ļ¬‚ags] [ļ¬lenames]
ā— Assembler
ā—‹ $ gcc -c [ļ¬‚ags] [ļ¬lenames]
ā—‹ $ objdump -d [ļ¬lenames]
ā— Linker
ā—‹ $ gcc -o [exename] [ļ¬‚ags] [ļ¬lenames]
C Code to Machine Code
Pre-Processor
ā— Peculiar to the C family; other languages
donā€™t have this
ā— Processes #include, #deļ¬ne, #if, macros
ā—‹ Combines main source ļ¬le with
headers (textually)
ā—‹ Deļ¬nes and expands macros
(token-based shorthand)
ā—‹ Conditionally removes parts of the
code (e.g. specialize for Linux, Mac, ā€¦)
ā— Removes all comments
ā— Output looks like C still
Before and after preprocessing
#include <limits.h>
#include <stdio.h>
int main(void) {
// Report the range of `char` on this system
printf("CHAR_MIN = %dn"
"CHAR_MAX = %dn",
CHAR_MIN, CHAR_MAX);
return 0;
}
# 1 "test.c"
# 1 "/usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h" 1 3 4
...
# 1 "/usr/include/stdio.h" 1 3 4
...
extern int fprintf (FILE *__restrict __stream,
const char *__restrict __format, ...);
extern int printf (const char *__restrict __format, ...);
...
# 874 "/usr/include/stdio.h" 3 4
# 3 "test.c" 2
int main(void) {
printf("CHAR_MIN = %dn"
"CHAR_MAX = %dn",
# 6 "test.c" 3 4
(-0x7f - 1)
# 6 "test.c"
, 0x7f);
return 0;
}
ā— Contents of header ļ¬les inserted inline
ā— Comments removed
ā— Macros expanded
ā— ā€œDirectiveā€ lines (beginning with #)
communicate things like original line numbers
Compiler
ā— The compiler translates the preprocessed
code into assembly code
ā—‹ This changes the format and structure
of the code but preserves the
semantics (what it does)
ā—‹ Can change lots of details for
optimization, as long as the overall
effect is the same
Before and after compilation
extern int printf (const char *__restrict
__format, ...);
int main(void) {
printf("CHAR_MIN = %dn"
"CHAR_MAX = %dn",
(-0x7f - 1), 0x7f);
return 0;
}
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "CHAR_MIN = %dnCHAR_MAX = %dn"
.text
.globl main
main:
subq $8, %rsp
movl $127, %edx
movl $-128, %esi
leaq .LC0(%rip), %rdi
xorl %eax, %eax
call printf@PLT
xorl %eax, %eax
addq $8, %rsp
ret
.size main, .-main
ā— C source code converted to assembly language
ā— Textual, but 1:1 correspondence to machine
language
ā— String out-of-line, referred to by label (.LC0)
ā— printf just referred to, not declared
Assembler
ā— Parses assembly code and mainly
translates into bits
ā—‹ There is some ļ¬‚exibility to
generate the most efļ¬cient
version of machine code
Before and after assembling
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "CHAR_MIN = %dnCHAR_MAX = %dn"
.text
.globl main
main:
subq $8, %rsp
movl $127, %edx
movl $-128, %esi
leaq .LC0(%rip), %rdi
xorl %eax, %eax
call printf@PLT
xorl %eax, %eax
addq $8, %rsp
ret
.size main, .-main
$ objdump -s -r test.o
test.o: file format elf64-x86-64
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
0000000000000011 R_X86_64_PC32 .LC0-0x0000000000000004
0000000000000018 R_X86_64_PLT32 printf-0x0000000000000004
Contents of section .rodata.str1.1:
0000 43484152 5f4d494e 203d2025 640a4348 CHAR_MIN = %d.CH
0010 41525f4d 4158203d 2025640a 00 AR_MAX = %d..
Contents of section .text:
0000 4883ec08 ba7f0000 00be80ff ffff488d H.............H.
0010 3d000000 0031c0e8 00000000 31c04883 =....1......1.H.
0020 c408c3 ...
ā— Everything is now binary
ā— ā€œRelocationsā€ for addresses not yet known
Before and after assembling
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "CHAR_MIN = %dnCHAR_MAX = %dn"
.text
.globl main
main:
subq $8, %rsp
movl $127, %edx
movl $-128, %esi
leaq .LC0(%rip), %rdi
xorl %eax, %eax
call printf@PLT
xorl %eax, %eax
addq $8, %rsp
ret
.size main, .-main
$ objdump -d -r test.o
test.o: file format elf64-x86-64
Disassembly of section .text.startup:
0000000000000000 <main>:
0: 48 83 ec 08 sub $0x8,%rsp
4: ba 7f 00 00 00 mov $0x7f,%edx
9: be 80 ff ff ff mov $0xffffff80,%esi
e: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi
11: R_X86_64_PC32 .LC0-0x4
15: 31 c0 xor %eax,%eax
17: e8 00 00 00 00 call 1c <main+0x1c>
18: R_X86_64_PLT32 printf-0x4
1c: 31 c0 xor %eax,%eax
1e: 48 83 c4 08 add $0x8,%rsp
22: c3 ret
ā— Just to emphasize that 1:1 correspondence
between assembly and machine instructions
ā— Aggregates multiple independently
compiled ļ¬les containing machine code
ā— Fills in those unknown addresses
ā— The goal is to create 1 ļ¬le with all of the
needed code to run the program
ā—‹ This is the ļ¬le you run to check your
code!
Linker Static Library
Files (.lib, .a)
How to Use The Compiler (gcc)
GCC - What is it?
ā— GNU Compiler Collection
ā—‹ GCC is a set of compilers for various languages. It provides all of the infrastructure for
building software in those languages from source code to assembly.
ā— The compiler can handle compiling everything on its own, but you can use various ļ¬‚ags to
breakdown the compilation steps
ā— Example:
gcc [flags] [infile(s)]
Common GCC Flags
-o [EXECUTABLE NAME] : names executable ļ¬le
-Ox : Code optimization
-O0 : Compile as fast as possible, donā€™t optimize [this is the default]
-O1, -O2, -O3: Optimize for reduced execution time [higher numbers are more optimized]
-Os : Optimize for code size instead of execution time.
-Og : Optimize for execution time, but try to avoid making interactive debugging harder.
-g : produce ā€œdebug infoā€: annotate assembly so gdb can ļ¬nd variables and source code
-Wall : enable many ā€œwarningā€ messages that should be on by default, but arenā€™t
- Does not turn on all of the warning messages GCC can produce.
- See https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Warning-Options.html for many more
-Werror : turns all warnings into errors
-std=c99 : use the 1999 version of the C standard and disable some (not all!) extensions
Makeļ¬les
What is a makeļ¬le?
ā— Automates the process of creating ļ¬les (using a compiler)
ā— For example, create bomb from bomb.c, phases.c, and util.c
ā— Running make bomb will update bomb
ā—‹ Only if any of the source ļ¬les have changed;
avoids unnecessary work
ā—‹ Remembers complicated compiler commands for you
ā— Can also store recipes for automating development tasks
ā—‹ make format to reformat source ļ¬les
Makeļ¬les are lists of rules
ā— There are two kinds of rules: normal and phony
ā—‹ Normal rules create ļ¬les
ā—‹ Phony rules donā€™t directly create ļ¬les
ā— Each rule has a target.
ā—‹ For normal rules, the target is the name of the ļ¬le that the rule will create
ā—‹ For phony rules, the target is an arbitrary name for what the rule does
ā— Rules may have prerequisites (also known as dependencies)
ā—‹ Prerequisites are the ļ¬les that are needed to create the target
ā—‹ If any of the prerequisites doesnā€™t exist, it must be created ļ¬rst
ā—‹ If any of the prerequisites is newer than the target, the target is ā€œout of dateā€ and must be re-created
ā— Rules may have commands.
ā—‹ One or more shell commands that create the target from its prerequisites
ā—‹ For phony rules, just some commands to be run
Normal rule example
bomb: bomb.o phases.o util.o
$(CC) -o bomb bomb.o phases.o util.o
Normal rule example
bomb: bomb.o phases.o util.o
$(CC) -o bomb bomb.o phases.o util.o
If this ļ¬le
doesnā€™t existā€¦ ā€¦ or if it is older than any of these ļ¬lesā€¦
ā€¦ then run this command.
Normal rule example
bomb: bomb.o phases.o util.o
$(CC) -o bomb bomb.o phases.o util.o
If this ļ¬le
doesnā€™t existā€¦ ā€¦ or if it is older than any of these ļ¬lesā€¦
ā€¦ then run this command.
This refers to the value of a
variable, named CC, that holds
the name of a C compiler.
Normal rule without prerequisites
output_dir:
mkdir output_dir
ā— Run mkdir output_dir if output_dir does not exist
ā— If it does exist, no action
Normal rule without commands
bomb.o: bomb.c support.h phases.h
ā— Re-create bomb.o if any of bomb.c, support.h, phases.h is newer
ā— The commands to do this are given somewhere else
ā—‹ A pattern rule elsewhere in the Makeļ¬le
ā—‹ An implicit rule built into Make
Pattern and implicit rules
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
ā— To create an .o ļ¬le from a .c ļ¬le with the same base name, use this command
ā— Special variables $@ and $< give the name of the .o and .c ļ¬les respectively
ā— Variables CC and CFLAGS can be set to customize behavior
ā— This rule is implicit ā€” built into Make ā€” you donā€™t have to write it yourself
Phony rule example
all: bomb bomb-solve
.PHONY: all
ā— When asked to create ā€œallā€, create bomb and bomb-solve
ā— Does not create a ļ¬le named ā€œallā€
ā— The .PHONY annotation can be anywhere in the makeļ¬le
Phony rule example 2
clean:
rm -f bomb bomb-solve *.o
.PHONY: clean
ā— When asked to create ā€œcleanā€, run this command
ā—‹ Which deletes bomb, bomb-solve, and all object ļ¬les
ā— Does not create a ļ¬le named ā€œcleanā€
The make command
ā— Running make in the shell will cause the shell to look for a Makeļ¬le in the current
directory. If it ļ¬nds one, it will attempt to create the ļ¬rst target listed in the Makeļ¬le.
ā— You can also run make <target_name> to indicate exactly which target you want to
create.
ā— By convention, the ļ¬rst target is a phony target named all
ā—‹ so make and make all do the same thing
ā—‹ as the name implies, this is to create everything that the makeļ¬le knows how to create
ā— Phony rules serve as entry points into the Makeļ¬le
ā—‹ make all creates everything, make clean deletes all generated ļ¬les, make check runs
tests, ā€¦
ā—‹ But you can also make bomb.o if thatā€™s the only thing you want
A complete Makeļ¬le
CC = gcc
CFLAGS = -std=c99 -g -O2 -Wall -Werror
all: bomb bomb-solve
bomb: bomb.o phases.o util.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
bomb-solve: bomb.o phases-solve.o util.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
bomb.o: bomb.c phases.h support.h
phases.o: phases.c phases.h support.h
phases-solve.o: phases-solve.c phases.h support.h
util.o: util.c support.h
clean:
rm -f bomb bomb-solve *.o
.PHONY: all clean
ā— OK to use undeļ¬ned variables
ā—‹ LDFLAGS, LIBS
ā—‹ Found in environment or treated as empty
ā— Donā€™t need to give commands to create object
ļ¬les from C source
ā—‹ But do need to list header ļ¬le dependencies
for each object ļ¬le
ā— Do need to give commands to create
executables (missing feature)
ā— all rule at the top, clean rule at the bottom
ā— One .PHONY annotation for all phony rules
Rules form a graph
CC = gcc
CFLAGS = -std=c99 -g -O2 -Wall -Werror
all: bomb bomb-solve
bomb: bomb.o phases.o util.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
bomb-solve: bomb.o phases-solve.o util.o
$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
bomb.o: bomb.c phases.h support.h
phases.o: phases.c phases.h support.h
phases-solve.o: phases-solve.c phases.h support.h
util.o: util.c support.h
clean:
rm -f bomb bomb-solve *.o
.PHONY: all clean
all
bomb bomb-solve
bomb.o phases.o phases-solve.o util.o
bomb.c phases.c phases-solve.c util.c
clean
ā— Make avoids unnecessary work
ā—‹ If bomb.c changes, make all will
re-create bomb.o, bomb, bomb-solve
ā—‹ If phases.c changes, make all will
only re-create phases.o and bomb
ā— Make can see through missing
targets
ā—‹ If bomb.o does not exist, make bomb
creates it from bomb.c
Practice!
https:/
/www.cs.cmu.edu/~213/bootc
amps/lab3_handout.pdf
Feedback:
https:/
/tinyurl.com/213bootcam
p2
Appendix
Linking Files
Why are we learning about linking ļ¬les?
ā— Linker is a computer system program
that object ļ¬les (generated by a
compiler or an assembler) and
combines them into a single executable
ļ¬le, library ļ¬le, or another object ļ¬le.
ā— Programs are translated and linked
using a compiler driver:
ā—‹ linux> gcc -Og -o prog main.c sum.c
ā—‹ linux> ./prog
ā— More in future lecture!
What does a linker do?
ā— Symbol resolution
ā—‹ Programs deļ¬ne and reference symbols (global variables and functions)
ā—‹ Linker associates each symbol reference with exactly 1 symbol deļ¬nition
ā— Relocation
ā—‹ Merges separate code and data sections into single sections
ā—‹ Relocates symbols from relative locations in .o ļ¬les to ļ¬nal memory locations
ā—‹ Updates all references to symbols to reļ¬‚ect new positions
Linker symbols
ā— Global symbols
ā—‹ Symbols deļ¬ned by module m that can be referenced by other modules.
ā–  e.g., non-static C functions and non-static global variables.
ā— External symbols
ā—‹ Global symbols that are referenced by module m but deļ¬ned by some other module.
ā— Local symbols
ā—‹ Symbols that are deļ¬ned and referenced exclusively by module m.
ā–  e.g., C functions and global variables deļ¬ned with the static attribute.
ā—‹ Local linker symbols are not local program variables
Symbols
Why do you need linkers?
ā— Modularity
ā—‹ Program can be written as a collection of smaller source ļ¬les, rather than one
monolithic mass.
ā— Efļ¬ciency
ā—‹ Time: Separate compilation
ā–  Change one source ļ¬le, compile, and then relink. No need to recompile other
source ļ¬les.
ā—‹ Space: Libraries
ā–  Common functions can be aggregated into a single ļ¬leā€¦
Static vs Dynamic Linking
ā— Static Linking
ā—‹ Executable ļ¬les and running memory images contain only the library code they
actually use
ā— Dynamic linking
ā—‹ Executable ļ¬les contain no library code
ā—‹ During execution, single copy of library code can be shared across all executing
processes
Types of object ļ¬les
ā— Relocatable object ļ¬le (.o ļ¬le)
ā—‹ Code and data that can be combined with other relocatable object ļ¬les to form executable
object ļ¬le
ā–  Each .o ļ¬le is produced from exactly one source (.c) ļ¬le
ā— Executable object ļ¬le (a.out ļ¬le)
ā—‹ Code and data that can be copied directly into memory and then executed
ā— Shared object ļ¬le (.so ļ¬le)
ā—‹ Special type of relocatable object ļ¬le that can be loaded into memory and linked
dynamically, at either load time or run-time
How Linker resolves duplicate symbol deļ¬nitions
ā— Program symbols are either strong or weak
ā—‹ Strong: procedures and initialized globals
ā—‹ Weak: uninitialized globals
ā–  Or one's declared with speciļ¬er extern
Symbol rules
1. Multiple strong symbols are not allowed
ā—‹ Each item can be deļ¬ned only once
2. Given a strong symbol and multiple weak symbols, choose the strong
symbol
ā—‹ References to the weak symbol resolve to the strong symbol
3. If there are multiple weak symbols, pick an arbitrary one
ā— If you are using dynamic libraries, you need to tell the compiler where to look for the
library!
ā— It is easiest to use dynamic libraries with makeļ¬les, just include this line:
LD_LIBRARY_PATH = ā€œ~/my/pathā€
ā— If you are interested in creating a dynamic library, follow the steps here:
ā—‹ Shared Libraries: https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html
ā—‹ Dynamic Libraries: https://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html
LD_LIBRARY_PATH
Resources
https://missing.csail.mit.edu/2020/metaprogramming/
https://www.cs.cmu.edu/~15131/f17/topics/makeļ¬les/
https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
https://makeļ¬letutorial.com/
https://www.oreilly.com/library/view/programming-embedded-systems/0596009836/ch04.html
https://gcc.gnu.org/onlinedocs/gcc/
https://daveparillo.github.io/cisc187-reader/build-tools/make.html

More Related Content

Similar to Compiler design notes phases of compiler

C tutorials
C tutorialsC tutorials
C tutorialsAmit Kapoor
Ā 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdfKhaledIbrahim10923
Ā 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdfRajb54
Ā 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docMayurWagh46
Ā 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docxtarifarmarie
Ā 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
Ā 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdfDurga Padma
Ā 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptxDanielle780357
Ā 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
Ā 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
Ā 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
Ā 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterHossam Hassan
Ā 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programmingMuhammed Thanveer M
Ā 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
Ā 

Similar to Compiler design notes phases of compiler (20)

Lecture 1
Lecture 1Lecture 1
Lecture 1
Ā 
C tutorials
C tutorialsC tutorials
C tutorials
Ā 
Presentation 2.ppt
Presentation 2.pptPresentation 2.ppt
Presentation 2.ppt
Ā 
0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf0100_Embeded_C_CompilationProcess.pdf
0100_Embeded_C_CompilationProcess.pdf
Ā 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
Ā 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Ā 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
Ā 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
Ā 
1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx1 CMPS 12M Introduction to Data Structures Lab La.docx
1 CMPS 12M Introduction to Data Structures Lab La.docx
Ā 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
Ā 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
Ā 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
Ā 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
Ā 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
Ā 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Ā 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals master
Ā 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programming
Ā 
C Language
C LanguageC Language
C Language
Ā 
Learn C Language
Learn C LanguageLearn C Language
Learn C Language
Ā 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
Ā 

Recently uploaded

Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
Ā 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
Ā 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelDrAjayKumarYadav4
Ā 
šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...
šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...
šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...manju garg
Ā 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
Ā 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
Ā 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)ChandrakantDivate1
Ā 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
Ā 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
Ā 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
Ā 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
Ā 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information SystemsAnge Felix NSANZIYERA
Ā 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsmeharikiros2
Ā 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
Ā 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
Ā 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
Ā 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
Ā 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
Ā 

Recently uploaded (20)

Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
Ā 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
Ā 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
Ā 
šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...
šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...
šŸ‘‰ Yavatmal Call Girls Service Just Call šŸ‘šŸ‘„6378878445 šŸ‘šŸ‘„ Top Class Call Girl S...
Ā 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
Ā 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
Ā 
Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)Introduction to Artificial Intelligence ( AI)
Introduction to Artificial Intelligence ( AI)
Ā 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Ā 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
Ā 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Ā 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
Ā 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
Ā 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Ā 
Introduction to Geographic Information Systems
Introduction to Geographic Information SystemsIntroduction to Geographic Information Systems
Introduction to Geographic Information Systems
Ā 
Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
Ā 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
Ā 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
Ā 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
Ā 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
Ā 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Ā 

Compiler design notes phases of compiler

  • 2. Questions that will be answered today ā— What does it mean to compile code? ā— What does compiling code look like? ā— How can code be compiled? ā— What are Makeļ¬les?
  • 3. ā— It is important to understand how programs are compiled to have a better understanding of how different parts of a computer interact with each other. ā— Fundamental aspect of how computers run code. Why is this important?
  • 4. Levels of Abstraction ā— C [and other high level languages] are easy for programmers to understand, but computers require lots of software to process them ā— Machine code is just the opposite: easy for the computer to process, humans need lots of help to understand it ā— Assembly language is a compromise between the two: readable by humans (barely), close correspondence to machine code
  • 5. What does it mean to compile code? ā— The computer only understands machine code directly ā— All other languages must be either ā—‹ interpreted: executed by software ā—‹ compiled: translated to machine code by software
  • 6. What does it mean to compile code? ā— Computer follows steps to translate your code into something the computer can understand ā— This is the process of compiling code [a compiler completes these actions] ā— Four steps for C: preprocessing, compiling, assembling, linking ā—‹ Most other compiled languages donā€™t have the preprocessing step, but do have the other three
  • 7. Stepping through the stages ā— Pre-Processor ā—‹ $ gcc -E [ļ¬‚ags] [ļ¬lenames] ā— Compiler ā—‹ $ gcc -S [ļ¬‚ags] [ļ¬lenames] ā— Assembler ā—‹ $ gcc -c [ļ¬‚ags] [ļ¬lenames] ā—‹ $ objdump -d [ļ¬lenames] ā— Linker ā—‹ $ gcc -o [exename] [ļ¬‚ags] [ļ¬lenames]
  • 8. C Code to Machine Code
  • 9. Pre-Processor ā— Peculiar to the C family; other languages donā€™t have this ā— Processes #include, #deļ¬ne, #if, macros ā—‹ Combines main source ļ¬le with headers (textually) ā—‹ Deļ¬nes and expands macros (token-based shorthand) ā—‹ Conditionally removes parts of the code (e.g. specialize for Linux, Mac, ā€¦) ā— Removes all comments ā— Output looks like C still
  • 10. Before and after preprocessing #include <limits.h> #include <stdio.h> int main(void) { // Report the range of `char` on this system printf("CHAR_MIN = %dn" "CHAR_MAX = %dn", CHAR_MIN, CHAR_MAX); return 0; } # 1 "test.c" # 1 "/usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h" 1 3 4 ... # 1 "/usr/include/stdio.h" 1 3 4 ... extern int fprintf (FILE *__restrict __stream, const char *__restrict __format, ...); extern int printf (const char *__restrict __format, ...); ... # 874 "/usr/include/stdio.h" 3 4 # 3 "test.c" 2 int main(void) { printf("CHAR_MIN = %dn" "CHAR_MAX = %dn", # 6 "test.c" 3 4 (-0x7f - 1) # 6 "test.c" , 0x7f); return 0; } ā— Contents of header ļ¬les inserted inline ā— Comments removed ā— Macros expanded ā— ā€œDirectiveā€ lines (beginning with #) communicate things like original line numbers
  • 11. Compiler ā— The compiler translates the preprocessed code into assembly code ā—‹ This changes the format and structure of the code but preserves the semantics (what it does) ā—‹ Can change lots of details for optimization, as long as the overall effect is the same
  • 12. Before and after compilation extern int printf (const char *__restrict __format, ...); int main(void) { printf("CHAR_MIN = %dn" "CHAR_MAX = %dn", (-0x7f - 1), 0x7f); return 0; } .file "test.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "CHAR_MIN = %dnCHAR_MAX = %dn" .text .globl main main: subq $8, %rsp movl $127, %edx movl $-128, %esi leaq .LC0(%rip), %rdi xorl %eax, %eax call printf@PLT xorl %eax, %eax addq $8, %rsp ret .size main, .-main ā— C source code converted to assembly language ā— Textual, but 1:1 correspondence to machine language ā— String out-of-line, referred to by label (.LC0) ā— printf just referred to, not declared
  • 13. Assembler ā— Parses assembly code and mainly translates into bits ā—‹ There is some ļ¬‚exibility to generate the most efļ¬cient version of machine code
  • 14. Before and after assembling .file "test.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "CHAR_MIN = %dnCHAR_MAX = %dn" .text .globl main main: subq $8, %rsp movl $127, %edx movl $-128, %esi leaq .LC0(%rip), %rdi xorl %eax, %eax call printf@PLT xorl %eax, %eax addq $8, %rsp ret .size main, .-main $ objdump -s -r test.o test.o: file format elf64-x86-64 RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 0000000000000011 R_X86_64_PC32 .LC0-0x0000000000000004 0000000000000018 R_X86_64_PLT32 printf-0x0000000000000004 Contents of section .rodata.str1.1: 0000 43484152 5f4d494e 203d2025 640a4348 CHAR_MIN = %d.CH 0010 41525f4d 4158203d 2025640a 00 AR_MAX = %d.. Contents of section .text: 0000 4883ec08 ba7f0000 00be80ff ffff488d H.............H. 0010 3d000000 0031c0e8 00000000 31c04883 =....1......1.H. 0020 c408c3 ... ā— Everything is now binary ā— ā€œRelocationsā€ for addresses not yet known
  • 15. Before and after assembling .file "test.c" .section .rodata.str1.1,"aMS",@progbits,1 .LC0: .string "CHAR_MIN = %dnCHAR_MAX = %dn" .text .globl main main: subq $8, %rsp movl $127, %edx movl $-128, %esi leaq .LC0(%rip), %rdi xorl %eax, %eax call printf@PLT xorl %eax, %eax addq $8, %rsp ret .size main, .-main $ objdump -d -r test.o test.o: file format elf64-x86-64 Disassembly of section .text.startup: 0000000000000000 <main>: 0: 48 83 ec 08 sub $0x8,%rsp 4: ba 7f 00 00 00 mov $0x7f,%edx 9: be 80 ff ff ff mov $0xffffff80,%esi e: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi 11: R_X86_64_PC32 .LC0-0x4 15: 31 c0 xor %eax,%eax 17: e8 00 00 00 00 call 1c <main+0x1c> 18: R_X86_64_PLT32 printf-0x4 1c: 31 c0 xor %eax,%eax 1e: 48 83 c4 08 add $0x8,%rsp 22: c3 ret ā— Just to emphasize that 1:1 correspondence between assembly and machine instructions
  • 16. ā— Aggregates multiple independently compiled ļ¬les containing machine code ā— Fills in those unknown addresses ā— The goal is to create 1 ļ¬le with all of the needed code to run the program ā—‹ This is the ļ¬le you run to check your code! Linker Static Library Files (.lib, .a)
  • 17. How to Use The Compiler (gcc)
  • 18. GCC - What is it? ā— GNU Compiler Collection ā—‹ GCC is a set of compilers for various languages. It provides all of the infrastructure for building software in those languages from source code to assembly. ā— The compiler can handle compiling everything on its own, but you can use various ļ¬‚ags to breakdown the compilation steps ā— Example: gcc [flags] [infile(s)]
  • 19. Common GCC Flags -o [EXECUTABLE NAME] : names executable ļ¬le -Ox : Code optimization -O0 : Compile as fast as possible, donā€™t optimize [this is the default] -O1, -O2, -O3: Optimize for reduced execution time [higher numbers are more optimized] -Os : Optimize for code size instead of execution time. -Og : Optimize for execution time, but try to avoid making interactive debugging harder. -g : produce ā€œdebug infoā€: annotate assembly so gdb can ļ¬nd variables and source code -Wall : enable many ā€œwarningā€ messages that should be on by default, but arenā€™t - Does not turn on all of the warning messages GCC can produce. - See https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Warning-Options.html for many more -Werror : turns all warnings into errors -std=c99 : use the 1999 version of the C standard and disable some (not all!) extensions
  • 21. What is a makeļ¬le? ā— Automates the process of creating ļ¬les (using a compiler) ā— For example, create bomb from bomb.c, phases.c, and util.c ā— Running make bomb will update bomb ā—‹ Only if any of the source ļ¬les have changed; avoids unnecessary work ā—‹ Remembers complicated compiler commands for you ā— Can also store recipes for automating development tasks ā—‹ make format to reformat source ļ¬les
  • 22. Makeļ¬les are lists of rules ā— There are two kinds of rules: normal and phony ā—‹ Normal rules create ļ¬les ā—‹ Phony rules donā€™t directly create ļ¬les ā— Each rule has a target. ā—‹ For normal rules, the target is the name of the ļ¬le that the rule will create ā—‹ For phony rules, the target is an arbitrary name for what the rule does ā— Rules may have prerequisites (also known as dependencies) ā—‹ Prerequisites are the ļ¬les that are needed to create the target ā—‹ If any of the prerequisites doesnā€™t exist, it must be created ļ¬rst ā—‹ If any of the prerequisites is newer than the target, the target is ā€œout of dateā€ and must be re-created ā— Rules may have commands. ā—‹ One or more shell commands that create the target from its prerequisites ā—‹ For phony rules, just some commands to be run
  • 23. Normal rule example bomb: bomb.o phases.o util.o $(CC) -o bomb bomb.o phases.o util.o
  • 24. Normal rule example bomb: bomb.o phases.o util.o $(CC) -o bomb bomb.o phases.o util.o If this ļ¬le doesnā€™t existā€¦ ā€¦ or if it is older than any of these ļ¬lesā€¦ ā€¦ then run this command.
  • 25. Normal rule example bomb: bomb.o phases.o util.o $(CC) -o bomb bomb.o phases.o util.o If this ļ¬le doesnā€™t existā€¦ ā€¦ or if it is older than any of these ļ¬lesā€¦ ā€¦ then run this command. This refers to the value of a variable, named CC, that holds the name of a C compiler.
  • 26. Normal rule without prerequisites output_dir: mkdir output_dir ā— Run mkdir output_dir if output_dir does not exist ā— If it does exist, no action
  • 27. Normal rule without commands bomb.o: bomb.c support.h phases.h ā— Re-create bomb.o if any of bomb.c, support.h, phases.h is newer ā— The commands to do this are given somewhere else ā—‹ A pattern rule elsewhere in the Makeļ¬le ā—‹ An implicit rule built into Make
  • 28. Pattern and implicit rules %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< ā— To create an .o ļ¬le from a .c ļ¬le with the same base name, use this command ā— Special variables $@ and $< give the name of the .o and .c ļ¬les respectively ā— Variables CC and CFLAGS can be set to customize behavior ā— This rule is implicit ā€” built into Make ā€” you donā€™t have to write it yourself
  • 29. Phony rule example all: bomb bomb-solve .PHONY: all ā— When asked to create ā€œallā€, create bomb and bomb-solve ā— Does not create a ļ¬le named ā€œallā€ ā— The .PHONY annotation can be anywhere in the makeļ¬le
  • 30. Phony rule example 2 clean: rm -f bomb bomb-solve *.o .PHONY: clean ā— When asked to create ā€œcleanā€, run this command ā—‹ Which deletes bomb, bomb-solve, and all object ļ¬les ā— Does not create a ļ¬le named ā€œcleanā€
  • 31. The make command ā— Running make in the shell will cause the shell to look for a Makeļ¬le in the current directory. If it ļ¬nds one, it will attempt to create the ļ¬rst target listed in the Makeļ¬le. ā— You can also run make <target_name> to indicate exactly which target you want to create. ā— By convention, the ļ¬rst target is a phony target named all ā—‹ so make and make all do the same thing ā—‹ as the name implies, this is to create everything that the makeļ¬le knows how to create ā— Phony rules serve as entry points into the Makeļ¬le ā—‹ make all creates everything, make clean deletes all generated ļ¬les, make check runs tests, ā€¦ ā—‹ But you can also make bomb.o if thatā€™s the only thing you want
  • 32. A complete Makeļ¬le CC = gcc CFLAGS = -std=c99 -g -O2 -Wall -Werror all: bomb bomb-solve bomb: bomb.o phases.o util.o $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) bomb-solve: bomb.o phases-solve.o util.o $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) bomb.o: bomb.c phases.h support.h phases.o: phases.c phases.h support.h phases-solve.o: phases-solve.c phases.h support.h util.o: util.c support.h clean: rm -f bomb bomb-solve *.o .PHONY: all clean ā— OK to use undeļ¬ned variables ā—‹ LDFLAGS, LIBS ā—‹ Found in environment or treated as empty ā— Donā€™t need to give commands to create object ļ¬les from C source ā—‹ But do need to list header ļ¬le dependencies for each object ļ¬le ā— Do need to give commands to create executables (missing feature) ā— all rule at the top, clean rule at the bottom ā— One .PHONY annotation for all phony rules
  • 33. Rules form a graph CC = gcc CFLAGS = -std=c99 -g -O2 -Wall -Werror all: bomb bomb-solve bomb: bomb.o phases.o util.o $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) bomb-solve: bomb.o phases-solve.o util.o $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) bomb.o: bomb.c phases.h support.h phases.o: phases.c phases.h support.h phases-solve.o: phases-solve.c phases.h support.h util.o: util.c support.h clean: rm -f bomb bomb-solve *.o .PHONY: all clean all bomb bomb-solve bomb.o phases.o phases-solve.o util.o bomb.c phases.c phases-solve.c util.c clean ā— Make avoids unnecessary work ā—‹ If bomb.c changes, make all will re-create bomb.o, bomb, bomb-solve ā—‹ If phases.c changes, make all will only re-create phases.o and bomb ā— Make can see through missing targets ā—‹ If bomb.o does not exist, make bomb creates it from bomb.c
  • 38. Why are we learning about linking ļ¬les? ā— Linker is a computer system program that object ļ¬les (generated by a compiler or an assembler) and combines them into a single executable ļ¬le, library ļ¬le, or another object ļ¬le. ā— Programs are translated and linked using a compiler driver: ā—‹ linux> gcc -Og -o prog main.c sum.c ā—‹ linux> ./prog ā— More in future lecture!
  • 39. What does a linker do? ā— Symbol resolution ā—‹ Programs deļ¬ne and reference symbols (global variables and functions) ā—‹ Linker associates each symbol reference with exactly 1 symbol deļ¬nition ā— Relocation ā—‹ Merges separate code and data sections into single sections ā—‹ Relocates symbols from relative locations in .o ļ¬les to ļ¬nal memory locations ā—‹ Updates all references to symbols to reļ¬‚ect new positions
  • 40. Linker symbols ā— Global symbols ā—‹ Symbols deļ¬ned by module m that can be referenced by other modules. ā–  e.g., non-static C functions and non-static global variables. ā— External symbols ā—‹ Global symbols that are referenced by module m but deļ¬ned by some other module. ā— Local symbols ā—‹ Symbols that are deļ¬ned and referenced exclusively by module m. ā–  e.g., C functions and global variables deļ¬ned with the static attribute. ā—‹ Local linker symbols are not local program variables
  • 42. Why do you need linkers? ā— Modularity ā—‹ Program can be written as a collection of smaller source ļ¬les, rather than one monolithic mass. ā— Efļ¬ciency ā—‹ Time: Separate compilation ā–  Change one source ļ¬le, compile, and then relink. No need to recompile other source ļ¬les. ā—‹ Space: Libraries ā–  Common functions can be aggregated into a single ļ¬leā€¦
  • 43. Static vs Dynamic Linking ā— Static Linking ā—‹ Executable ļ¬les and running memory images contain only the library code they actually use ā— Dynamic linking ā—‹ Executable ļ¬les contain no library code ā—‹ During execution, single copy of library code can be shared across all executing processes
  • 44. Types of object ļ¬les ā— Relocatable object ļ¬le (.o ļ¬le) ā—‹ Code and data that can be combined with other relocatable object ļ¬les to form executable object ļ¬le ā–  Each .o ļ¬le is produced from exactly one source (.c) ļ¬le ā— Executable object ļ¬le (a.out ļ¬le) ā—‹ Code and data that can be copied directly into memory and then executed ā— Shared object ļ¬le (.so ļ¬le) ā—‹ Special type of relocatable object ļ¬le that can be loaded into memory and linked dynamically, at either load time or run-time
  • 45. How Linker resolves duplicate symbol deļ¬nitions ā— Program symbols are either strong or weak ā—‹ Strong: procedures and initialized globals ā—‹ Weak: uninitialized globals ā–  Or one's declared with speciļ¬er extern
  • 46. Symbol rules 1. Multiple strong symbols are not allowed ā—‹ Each item can be deļ¬ned only once 2. Given a strong symbol and multiple weak symbols, choose the strong symbol ā—‹ References to the weak symbol resolve to the strong symbol 3. If there are multiple weak symbols, pick an arbitrary one
  • 47. ā— If you are using dynamic libraries, you need to tell the compiler where to look for the library! ā— It is easiest to use dynamic libraries with makeļ¬les, just include this line: LD_LIBRARY_PATH = ā€œ~/my/pathā€ ā— If you are interested in creating a dynamic library, follow the steps here: ā—‹ Shared Libraries: https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html ā—‹ Dynamic Libraries: https://tldp.org/HOWTO/Program-Library-HOWTO/dl-libraries.html LD_LIBRARY_PATH