Upload
camron-salvage
View
225
Download
0
Tags:
Embed Size (px)
Citation preview
TodayToday’’s Topics Topic
Breakdown of GCC scriptBreakdown of GCC script Description of how different system Description of how different system
programs work together to build (and programs work together to build (and run) a C program.run) a C program.
System Software (System Software (for todayfor today))
AssemblerAssembler CompilerCompiler LinkerLinker LoaderLoader Operating systemOperating system PreprocessorPreprocessor
TodayToday’’s Objectivess Objectives
Within context of Within context of ““compilingcompiling”” a C program a C program Define actions of linker, compiler, assembler, Define actions of linker, compiler, assembler,
OS, macro preprocessor, loaderOS, macro preprocessor, loader Draw block diagram showing order of linker, Draw block diagram showing order of linker,
compiler, assembler, macro preprocessor, compiler, assembler, macro preprocessor, loaderloader
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor Compiler
Assembler
LinkerLoader
Executable
a.out
Executable
Program
In Memory
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor Compiler
Assembler
LinkerLoader
Executable
a.out
Executable
Program
In Memory
C Preprocessor (cpp)C Preprocessor (cpp)
Pass over sourcePass over source Insert included filesInsert included files Perform macro substitutionsPerform macro substitutions
Define macrosDefine macros #define NUM 100#define NUM 100 #define xx(v,name,op,metrics) \ #define xx(v,name,op,metrics) \
v=xxinit(op,name,IR->metrics)v=xxinit(op,name,IR->metrics)
gcc –E example.c sends preprocessor gcc –E example.c sends preprocessor output to stdoutoutput to stdout
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor Compiler
Assembler
LinkerLoader
Executable
a.out
Executable
Program
In Memory
CompilerCompiler
gcc actually name of a scriptgcc actually name of a script Compiler translates one language to Compiler translates one language to
another (or the same???)another (or the same???) gcc compiler translates C to assemblergcc compiler translates C to assembler gcc –S example.c gcc –S example.c ““savessaves”” example.s example.s Compiler consists ofCompiler consists of
ParserParser Code generationCode generation MysticismMysticism
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor Compiler
Assembler
LinkerLoader
Executable
a.out
Executable
Program
In Memory
AssemblerAssembler
Another translator ??? (Another translator ??? (asas example.s)example.s)
Assembler to (binary) objectAssembler to (binary) object Why not compile straight to binary?Why not compile straight to binary? gcc –c example.c to gcc –c example.c to ““savesave”” object object NM to look at object (nm example.o)NM to look at object (nm example.o)
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor Compiler
Assembler
LinkerLoader
Executable
a.out
Executable
Program
In Memory
LinkerLinker
Combines objects, both user .o files Combines objects, both user .o files and libraries; makes an executable and libraries; makes an executable file file
gcc *.o –lm yields a.outgcc *.o –lm yields a.out gcc –o myExec *.o –lm gcc –o myExec *.o –lm Use nm to look at executableUse nm to look at executable
Breaking Down CCBreaking Down CC
source
Preprocessed
SourceASM
O
B
J
E
C
T
.s
.o
Preprocessor Compiler
Assembler
LinkerLoader
Executable
a.out
Executable
Program
In Memory
LoaderLoader
Runs when you type ./a.outRuns when you type ./a.out
Gets an address to place program Gets an address to place program (from the operating system)(from the operating system)
Changes necessary addresses (if Changes necessary addresses (if any)any)
Places code into memory Places code into memory
Operating SystemOperating System
OS is ALWAYS runningOS is ALWAYS running Oversees whole compilation processOversees whole compilation process
““RecognizesRecognizes”” gcc example.c command gcc example.c command Parses flags and argumentsParses flags and arguments Invokes gcc scriptInvokes gcc script Performs memory management (malloc)Performs memory management (malloc) Chooses Chooses ““addressaddress”” to place program to place program
Compiler OptimizationCompiler Optimization
What does optimization mean?What does optimization mean? What does compiler optimization mean?What does compiler optimization mean? ““Optimization” takes more time to Optimization” takes more time to
compile in hopes of generating faster compile in hopes of generating faster executableexecutable
-O# is the flag to ask for optimization-O# is the flag to ask for optimization gcc –O3 tsp.c gives “highest” level gcc –O3 tsp.c gives “highest” level