Pilation
Pilation
cpp
Steps in gcc
hello.c hello.i
cpp
Steps in gcc
hello.c hello.i
cpp cc1
Steps in gcc
hello.c hello.i hello.s
cpp cc1
Steps in gcc
hello.c hello.i hello.s
cpp cc1
as
Steps in gcc
hello.c hello.i hello.s
cpp cc1
as
hello.o
Steps in gcc
hello.c hello.i hello.s
cpp cc1
Libraries as
(eg. Math)
hello.o
ld
Steps in gcc
hello.c hello.i hello.s
cpp cc1
Libraries as
(eg. Math)
a.out hello.o
ld
Steps in gcc
DATA
DATA
a b c
INSTRUCTIONS
INSTRUCTIONS
Libraries
(eg. Math)
hello.s
as a.out
ld
hello.o
Program to Memory Layout
Object File Format (hello.o)
The Assembler
The Assembler
The Assembler
Encodes
Encodeseasily!
easily!
The Assembler
Encodes
Encodeseasily!
easily!
The Assembler
The Assembler
Assembler Directives
RO
ROdata
data(constant)
(constant)begins
beginshere
here
Align at doubleword boundary
Align at doubleword boundary
The Assembler
Assembler Directives
RO
ROdata
data(constant)
(constant)begins
beginshere
here
Align at doubleword boundary
Align at doubleword boundary
Create
Createaastring.
string.Others
Othersare
are.byte,
.byte,
.half,
.half,.short,
.short,.word,
.word,.dword
.dword
The Assembler
Assembler Directives
RO
ROdata
data(constant)
(constant)begins
beginshere
here
Align at doubleword boundary
Align at doubleword boundary
Create
Createaastring.
string.Others
Othersare
are.byte,
.byte,
.half,
.half,.short,
.short,.word,
.word,.dword
.dword
Text
Text(code)
(code)section
sectionbegins
beginshere
here
Align
Alignatatbyte
byteboundary
boundary
main
mainisisaaglobal
global“label”.
“label”.Can
Can
be
bereferred
referredfrom
fromother
othermodules.
modules.
The Assembler
The Assembler
Labels
Labels
The Assembler
Labels
Labels
Assembler Actions
● How to encode?
● What information is required?
Assembler Actions
● How to encode?
● What information is required?
– Where to branch? Where is “.L2”?
– Where is “.LC0”?
Assembler Actions
● Labels – main, .LC0, .L2, .L3,
printf are names for memory locations.
Assembler Actions
● Labels – main, .LC0, .L2, .L3,
printf are names for memory locations.
– main: first instruction in the text segment
– .L3: 8th instruction in the text segment
– .LC0: starts from the first byte in the data
segment (.rodata)
– printf: undefined
Assembler Actions
● Labels – main, .LC0, .L2, .L3,
printf are names for memory locations.
– main: first instruction in the text segment
– .L3: 8th instruction in the text segment
– .LC0: starts from the first byte in the data
segment (.rodata)
main
main 00
– printf: undefined .L3
.L3 28
28
.L2
.L2 56
56
.LC0
.LC0 0x8000
0x80000000
0000
printf
printf ??
??
Assembler Actions
● Labels – main, .LC0, .L2, .L3, printf
are names for memory locations.
– main: first instruction in the text segment
– .L3: 8th instruction in the text segment
– .LC0: starts from the first byte in the data segment
(.rodata)
main
main 00
– printf: undefined .L3
.L3 28
28
.L2 56
● Symbol Table .L2 56
0x8000
.LC0
.LC0 0x80000000
0000
printf
printf ??
??
The Assembler
Library
LibraryFunction
FunctionCall
Call
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
● Will exist in the symbol table entries of those
obj files
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
● Will exist in the symbol table entries of those
obj files
● Make a list of unresolved references in the
present object file
– Relocation information
Unresolved references
● Defined in other object files
– Library functions, external calls to other modules
● Will exist in the symbol table entries of those
obj files
● Make a list of unresolved references in the
present object file
– Relocation information
64
64 call
call printf
printf ??
??
Object File Format (hello.o)
Object File Format (hello.o)
800
800 Machine
Machinecodecodesize
size
28
28 Initialized
Initializeddata
datasize
size
00 Size
Sizeof
ofuninitialized
uninitializeddata
datasize
size
36
36 Size
Sizeof
ofthe
theSymbol
SymbolTable
Table
16
16 Size
Sizeof
ofthe
theRelocation
RelocationInfo
Info
Object File Format (hello.o)
800
800 Machine
Machinecodecodesize
size
28
28 Initialized
Initializeddata
datasize
size
00 Size
Sizeof
ofuninitialized
uninitializeddata
datasize
size
36
36 Size
Sizeof
ofthe
theSymbol
SymbolTable
Table
16
16 Size
Sizeof
ofthe
theRelocation
RelocationInfo
Info
Object File Format (hello.o)
Object File Format (hello.o)
main
main 00
.L3
.L3 36
36
.L2
.L2 68
68
.LC0
.LC0 800
800
printf
printf ??
??
Object File Format (hello.o)
main
main 00
.L3
.L3 36
36
.L2
.L2 68
68
.LC0
.LC0 800
800
printf
printf ??
??
64
64 call
call printf
printf ??
??
Assembler
Assembler
● Pass 1: Finds instructions with labels. Records
their memory locations and labels so that the
relationship between symbolic names and
addresses is known when instructions are
translated.
Assembler
● Pass 1: Finds instructions with labels. Records
their memory locations and labels so that the
relationship between symbolic names and
addresses is known when instructions are
translated.
● Pass 2: Translate each assembly statement by
combining the numeric equivalents of
opcodes, register specifiers, and labels into a
legal instruction.