cs 61c: great ideas in computer architecture running a

83
CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading) Instructors: Krste Asanović & Randy H. Katz http://inst.eecs.Berkeley.edu/~cs61c/fa17 9/19/17 Fall 2017 - Lecture #8 1

Upload: others

Post on 15-Oct-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 61C: Great Ideas in Computer Architecture Running a

CS61C:GreatIdeasinComputerArchitecture

RunningaProgram - CALL(Compiling,Assembling,Linking,andLoading)

Instructors:Krste Asanović &RandyH.Katz

http://inst.eecs.Berkeley.edu/~cs61c/fa17

9/19/17 Fall2017 - Lecture#8 1

Page 2: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

9/19/17 Fall2017 - Lecture#8 2

Page 3: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

9/19/17 Fall2017 - Lecture#8 3

Page 4: CS 61C: Great Ideas in Computer Architecture Running a

Review:ComponentsofaComputer

4Fall2017 - Lecture#89/19/17

ProcessorControl

Datapath

PC

RegistersArithmetic&LogicUnit

(ALU)

Memory Input

Output

Bytes

Enable?Read/Write

Address

WriteData

ReadData

Processor-MemoryInterface I/O-MemoryInterfaces

Program

Data

Instructions

Page 5: CS 61C: Great Ideas in Computer Architecture Running a

Review:RISC-VInstructionFormats

5Fall2017 - Lecture#89/19/17

rd =rs1OPrs2

rd =rs1OPImmediate;Loadrd fromMemory(rs1+Immediate);JALR(rd =PC+4,PC=rs1+Immediate)

Storers2toMemory(rs1+Immediate)

Branchif(rs1conditionrs2)istruetoMemory(PC+Immediate*2),i.e.,PC=PC+Immediate*2

Upper“Long”Immediate(AUIPC,LUI):PCorrd ={imm, 12b’0}

JALtoMemory(PC+Immediate*2),i.e.,rd =PC+4;PC=PC+immediate*2

Page 6: CS 61C: Great Ideas in Computer Architecture Running a

J-FormatforJumpInstructions(JAL)

• JALsavesPC+4inregisterrd (thereturnaddress)– Assembler“j”jumpispseudo-instruction,usesJALbutsetsrd=x0todiscardreturnaddress

• SetPC=PC+offset(PC-relativejump:offset=signedimmediate*2)• Targetsomewherewithin±219 locations,2bytesapart– ±218 32-bitinstructions,±220 bytes

• Immediateencodedoptimizedsimilarlytobranchinstructiontoreducehardwarecost

6Fall2017 - Lecture#89/19/17

Page 7: CS 61C: Great Ideas in Computer Architecture Running a

UsesofJAL

# j pseudo-instructionj Label = jal x0, Label # Discard return address

# Call function within 218 instructions of PCjal ra, FuncName

7Fall2017 - Lecture#89/19/17

Page 8: CS 61C: Great Ideas in Computer Architecture Running a

JALRInstruction(I-Format)

• JALR rd, rs, immediate–WritesPC+4 tord (returnaddress)– SetsPC =rs +immediate (12bit,signextended)– Usessameimmediates asarithmeticandloads• Unlike branches,nomultiplicationbytwobeforeaddingtors toformthenewPC• ByteoffsetNOT halfword offsetasinbranchesandJAL

8Fall2017 - Lecture#89/19/17

Page 9: CS 61C: Great Ideas in Computer Architecture Running a

UsesofJALR# ret and jr pseudo-instructionsret = jr ra = jalr x0, ra, 0

# Call function at any 32-bit absolute addresslui x1, <hi20bits>jalr ra, x1, <lo12bits>

# Jump PC-relative with 32-bit offsetauipc x1, <hi20bits>jalr x0, x1, <lo12bits>

9Fall2017 - Lecture#89/19/17

Page 10: CS 61C: Great Ideas in Computer Architecture Running a

PseudoInstructions

9/19/17 Fall2017 - Lecture#8 10

Validinassemblylanguagebutnotinmachinelanguage(i.e.,mapstomultiplemachinelanguageinstructions)

Page 11: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

9/19/17 Fall2017 - Lecture#8 11

Page 12: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(1/3)• Paperandpencilexample(unsigned):

Multiplicand 1000 8Multiplier x 1001 9

72• m bitsx n bits=m +n bitproduct

129/19/17 Fall2017 - Lecture#8

Page 13: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(1/3)• Paperandpencilexample(unsigned):

Multiplicand 1000 8Multiplier x 1001 9

1000

72• m bitsx n bits=m +n bitproduct

139/19/17 Fall2017 - Lecture#8

Page 14: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(1/3)• Paperandpencilexample(unsigned):

Multiplicand 1000 8Multiplier x 1001 9

10000000

72• m bitsx n bits=m +n bitproduct

149/19/17 Fall2017 - Lecture#8

Page 15: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(1/3)• Paperandpencilexample(unsigned):

Multiplicand 1000 8Multiplier x 1001 9

10000000

0000

72• m bitsx n bits=m +n bitproduct

159/19/17 Fall2017 - Lecture#8

Page 16: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(1/3)• Paperandpencilexample(unsigned):

Multiplicand 1000 8Multiplier x 1001 9

10000000

0000+ 1000

72• m bitsx n bits=m +n bitproduct

169/19/17 Fall2017 - Lecture#8

Page 17: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(1/3)• Paperandpencilexample(unsigned):

Multiplicand 1000 8Multiplier x 1001 9

10000000

0000+ 100001001000 72

• m bitsx n bits=m +n bitproduct

179/19/17 Fall2017 - Lecture#8

Page 18: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(2/3)• InRISC-V,wemultiplyregisters,so:– 32-bitvaluex 32-bitvalue=64-bitvalue

• SyntaxofMultiplication(signed):– MUL performsan32-bit×32-bitmultiplicationandplacesthelower32bitsinthedestinationregister

– MULH performsthesamemultiplicationbutreturnstheupper32bitsofthefull2×32-bitproduct

– If64-bitproductisrequired,thentherecommendedcodesequenceis:MULH rdh, rs1, rs2MUL rdl, rs1, rs2 (sourceregisterspecifiersmustbeinsameorderandrdh cannotbethesameasrs1 orrs2)

189/19/17 Fall2017 - Lecture#8

Page 19: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(3/3)• Example:– inC: a = b * c; # a should be declared long long– inRISC-V:

• Letb bet2;letc bet3;andleta bet0 andt1 (sinceitmaybeupto64bits)# upper half of# product into $s0# lower half of# product into $s1

199/19/17 Fall2017 - Lecture#8

Page 20: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(3/3)• Example:– inC: a = b * c; # a should be declared long long– inRISC-V:

• Letb bet2;letc bet3;andleta bet0 andt1 (sinceitmaybeupto64bits)mulh t0,t2,t3 # upper half of

# product into $s0# lower half of# product into $s1

209/19/17 Fall2017 - Lecture#8

Page 21: CS 61C: Great Ideas in Computer Architecture Running a

IntegerMultiplication(3/3)• Example:– inC: a = b * c; # a should be declared long long– inRISC-V:

• Letb bet2;letc bet3;andleta bet0 andt1 (sinceitmaybeupto64bits)mulh t0,t2,t3 # upper half of

# product into $s0mul t1,t2,t3 # lower half of

# product into $s1

219/19/17 Fall2017 - Lecture#8

Page 22: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

22

• Paperandpencilexample(unsigned):Quotient

Divisor 1000|1001010 Dividend

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 23: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

23

• Paperandpencilexample(unsigned):1 Quotient

Divisor 1000|1001010 Dividend-1000

1

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 24: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

24

• Paperandpencilexample(unsigned):10 Quotient

Divisor 1000|1001010 Dividend-1000

10

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 25: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

25

• Paperandpencilexample(unsigned):100 Quotient

Divisor 1000|1001010 Dividend-1000

10101

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 26: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

26

• Paperandpencilexample(unsigned):1001 Quotient

Divisor 1000|1001010 Dividend-1000

101011010

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 27: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

27

• Paperandpencilexample(unsigned):1001 Quotient

Divisor 1000|1001010 Dividend-1000

101011010-1000

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 28: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(1/2)

28

• Paperandpencilexample(unsigned):1001 Quotient

Divisor 1000|1001010 Dividend-1000

101011010-1000

10 Remainder(or Modulo result)

• Dividend=Quotientx Divisor+Remainder9/19/17 Fall2017 - Lecture#8

Page 29: CS 61C: Great Ideas in Computer Architecture Running a

IntegerDivision(2/2)• SyntaxofDivision(signed):

– DIV performssignedintegerdivisionof32bitsby32bits.REMprovidestheremainderofthecorrespondingdivisionoperation

– Ifthequotientandremainderarerequiredfromthesamedivision,therecommendedcodesequenceis:DIV rdq, rs1, rs2REM rdr, rs1, rs2

(rdq cannotbethesameasrs1orrs2)

• ImplementsCdivision(/)andmodulo(%)

• ExampleinC: a = c / d; b = c % d;

• InRISC-V: a↔t0; b↔t1; c↔t2; d↔t3div t0,t2,t3 # a=c/d

rem t1,t2,t3 # b=c%d299/19/17 Fall2017 - Lecture#8

Page 30: CS 61C: Great Ideas in Computer Architecture Running a

PeerQuestion

WhichofthefollowingplacestheaddressofLOOPint1?1) la t2, LOOP

lw t1, 0(t2)

2) jal LOOPLOOP: add t1, ra, x0

3) la t1, LOOP

1 2 3T, T, TT, T, FF, T, T

9/19/17 Fall2017 - Lecture#8 30

Page 31: CS 61C: Great Ideas in Computer Architecture Running a

PeerQuestion

WhichofthefollowingplacestheaddressofLOOPint1?1) la t2, LOOP

lw t1, 0(t2)

2) jal LOOPLOOP: add t1, ra, x0

3) la t1, LOOP

1 2 3T, T, TT, T, FF, T, T

9/19/17 Fall2017 - Lecture#8 31

Page 32: CS 61C: Great Ideas in Computer Architecture Running a

Break!

9/19/17 Fall2017 - Lecture#8 32

Page 33: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

9/19/17 Fall2017 - Lecture#8 33

Page 34: CS 61C: Great Ideas in Computer Architecture Running a

LevelsofRepresentation/Interpretation

lw $t0,0($2)lw $t1,4($2)sw $t1,0($2)sw $t0,4($2)

High Level LanguageProgram (e.g., C)

Assembly Language Program (e.g., RISC-V)

Machine Language Program (RISC-V)

Hardware Architecture Description(e.g., block diagrams)

Compiler

Assembler

Machine Interpretation

temp = v[k];v[k] = v[k+1];v[k+1] = temp;

0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111

Architecture Implementation

Anything can be representedas a number,

i.e., data or instructions

Logic Circuit Description(Circuit Schematic Diagrams)

+ How to take a program and run it

9/19/17 Fall2017 - Lecture#8 34

Page 35: CS 61C: Great Ideas in Computer Architecture Running a

LanguageExecutionContinuum

• Interpreterisaprogramthatexecutesotherprograms

• Languagetranslationgivesusanotheroption• Ingeneral,weinterpretahigh-levellanguagewhenefficiencyisnotcriticalandtranslatetoalower-levellanguagetoincreaseperformance

EasytoprogramInefficienttointerpret

DifficulttoprogramEfficienttointerpret

PythonJavaC++ C Assembly MachinecodeJavabytecode

9/19/17 Fall2017 - Lecture#8 35

Page 36: CS 61C: Great Ideas in Computer Architecture Running a

InterpretationvsTranslation

• Howdowerunaprogramwritteninasourcelanguage?– Interpreter:Directlyexecutesaprograminthesourcelanguage– Translator:Convertsaprogramfromthesourcelanguagetoanequivalentprograminanotherlanguage

9/19/17 Fall2017 - Lecture#8 36

Page 37: CS 61C: Great Ideas in Computer Architecture Running a

Interpretation

• Forexample,consideraPythonprogramfoo.py

• Pythoninterpreterisjustaprogramthatreadsapythonprogramandperformsthefunctionsofthatpythonprogram

9/19/17 Fall2017 - Lecture#8 37

Page 38: CS 61C: Great Ideas in Computer Architecture Running a

Interpretation

• WHYinterpretmachinelanguageinsoftware?• VENUS(Lab#3),Project#1:RISC-Vsimulatorusefulforlearning/debugging

• E.g.,AppleMacintoshconversion– SwitchedfromMotorola680x0instructionarchitecturetoPowerPC(beforex86)

– Couldrequireallprogramstobere-translatedfromhighlevellanguage– Instead,letexecutablescontainoldand/ornewmachinecode,interpretoldcodeinsoftwareifnecessary(emulation)

9/19/17 Fall2017 - Lecture#8 38

Page 39: CS 61C: Great Ideas in Computer Architecture Running a

Interpretationvs.Translation?(1/2)

• Generallyeasiertowriteinterpreter• Interpreterclosertohigh-level,socangivebettererrormessages(e.g.,VENUS,Project#1)– Translatorreaction:addextrainformationtohelpdebugging(linenumbers,names)

• Interpreterslower(10x?),codesmaller(2x?)• Interpreterprovidesinstructionsetindependence:runonanymachine

9/19/17 Fall2017 - Lecture#8 39

Page 40: CS 61C: Great Ideas in Computer Architecture Running a

Interpretationvs.Translation?(2/2)

• Translated/compiledcodealmostalwaysmoreefficientandthereforehigherperformance:– Importantformanyapplications,particularlyoperatingsystems

• Translation/compilationhelps“hide”theprogram“source”fromtheusers:– Onemodelforcreatingvalueinthemarketplace(e.g.,Microsoftkeepsalltheirsourcecodesecret)

– Alternativemodel,“opensource”,createsvaluebypublishingthesourcecodeandfosteringacommunityofdevelopers.

9/19/17 Fall2017 - Lecture#8 40

Page 41: CS 61C: Great Ideas in Computer Architecture Running a

StepsinCompilingaCProgram

9/19/17 Fall2017 - Lecture#8 41

gcc -O2 -S -c foo.c

Page 42: CS 61C: Great Ideas in Computer Architecture Running a

Compiler

• Input:High-LevelLanguageCode(e.g.,foo.c)• Output:AssemblyLanguageCode(e.g.,foo.s forRISC-V)

• Note:Outputmay containpseudo-instructions• Pseudo-instructions:instructionsthatassemblerunderstandsbutnotinmachineForexample(movet2tot1):– mv t1,t2Þ addi t1,t2,0

429/19/17 Fall2017 - Lecture#8

Page 43: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

439/19/17 Fall2017 - Lecture#8

Page 44: CS 61C: Great Ideas in Computer Architecture Running a

WhereAreWeNow?

44

CS164

9/19/17 Fall2017 - Lecture#8

Page 45: CS 61C: Great Ideas in Computer Architecture Running a

Assembler

• Input:AssemblyLanguageCode(includespseudoops)(e.g.,foo.s forRISC-V)

• Output:ObjectCode,informationtables(trueassemblyonly)(e.g.,foo.o forRISC-V)

• ReadsandUsesDirectives• ReplacePseudo-instructions• ProduceMachineLanguage• CreatesObjectFile

459/19/17 Fall2017 - Lecture#8

Page 46: CS 61C: Great Ideas in Computer Architecture Running a

AssemblerDirectives(SeeRISCVReader,Chapter3)

• Givedirectionstoassembler,butdonotproducemachineinstructions.text: Subsequentitemsputinusertextsegment(machine

code).data: Subsequentitemsputinuserdatasegment(binaryrepof

datainsourcefile).globl sym: declaressym globalandcanbereferencedfromother

files.string str: Storethestringstr inmemoryandnull-terminateit.word w1…wn: Storethen 32-bitquantitiesinsuccessivememory

words

469/19/17 Fall2017 - Lecture#8

Page 47: CS 61C: Great Ideas in Computer Architecture Running a

Pseudo-instructionReplacement• AssemblertreatsconvenientvariationsofmachinelanguageinstructionsasifrealinstructionsPseudo: Real:mv t0, t1

9/19/17 Fall2017 - Lecture#8 47

Page 48: CS 61C: Great Ideas in Computer Architecture Running a

Pseudo-instructionReplacement• AssemblertreatsconvenientvariationsofmachinelanguageinstructionsasifrealinstructionsPseudo: Real:mv t0, t1 addi t0,t1,0 neg t0, t1 sub t0, zero, t1 li t0, imm addi t0, zero, immnot t0, t1 xori t0, t1, -1beqz t0, loop beq t0, zero, loopla t0, str lui t0, str[31:12]

addi t0, t0, str[11:0] ORauipc t0, str[31:12]addi t0, t0, str[11:0]

9/19/17 Fall2017 - Lecture#8 48

DON’T FORGET: sign extended immediates

+Branch immediates

count halfwords)

STATICAddressing

PC-RelativeAddressing

Page 49: CS 61C: Great Ideas in Computer Architecture Running a

PeerInstructionWhichofthefollowingisacorrectassemblylanguagesequencesequenceforthepseudoinstruction:lat1,FOO?*A:ori t1, 0xABCD0addi t1, 0x124

B:ori t1, 0x124lui t1, 0xABCD0

C:lui t1, 0xD0124ori t1, 0xABC

:lui t1, 0xABCD0addi t1, 0x124

*AssumetheaddressofFOOis0xABCD0124

9/19/17 Fall2017 - Lecture#8 49

Page 50: CS 61C: Great Ideas in Computer Architecture Running a

PeerInstructionWhichofthefollowingisacorrectassemblylanguagesequencesequenceforthepseudoinstruction:lat1,FOO?*A:ori t1, 0xABCD0addi t1, 0x124

B:ori t1, 0x124lui t1, 0xABCD0

C:lui t1, 0xD0124ori t1, 0xABC

:lui t1, 0xABCD0addi t1, 0x124

*AssumetheaddressofFOOis0xABCD0124

9/19/17 Fall2017 - Lecture#8 50

Page 51: CS 61C: Great Ideas in Computer Architecture Running a

ProducingMachineLanguage(1/3)

• SimpleCase– Arithmetic,Logical,Shifts,andsoon– Allnecessaryinfoiswithintheinstructionalready

• WhataboutBranchesandJumps?– PC-Relative(e.g.,beq/bne andjal)– Sooncepseudo-instructionsarereplacedbyrealones,weknowbyhowmanyinstructionstobranch/jumpover

• Sothesecanbehandled

9/19/17 Fall2017 - Lecture#8 51

Page 52: CS 61C: Great Ideas in Computer Architecture Running a

ProducingMachineLanguage(2/3)• “ForwardReference”problem– Branchinstructionscanrefertolabelsthatare“forward”intheprogram:

– Solvedbytakingtwopassesovertheprogram• Firstpassrememberspositionoflabels• Secondpassuseslabelpositionstogeneratecode

52

addi t2, zero, 9 # t2 = 9L1: slt t1, zero, t2 # 0 < t2? Set t1

beq t1, zero, L2 # NO! t2 <= 0; Go to L2addi t2, t2, -1 # YES! t2 > 0; t2--j L1 # Go to L1

L2:

9/19/17 Fall2017 - Lecture#8

3wordsback(6halfwords)

3wordsforward(6halfwords)

addi t2, zero, 9 # t2 = 9L1: bge zero, t2, L2 # 0 >= t2? Exit!

addi t2, t2, -1 # 0 < t2; t2--j L1 # Go to L1

L2:

2 wordsback(4halfwords)

2 wordsforward(4halfwords)

Page 53: CS 61C: Great Ideas in Computer Architecture Running a

ProducingMachineLanguage(3/3)

• WhataboutPC-relativejumps(jal)andbranches(beq,bne)?– j offset pseudoinstructionexpandstoJAL zero, offset– Justcountthenumberofinstructionhalfwords betweentargetandjumptodeterminetheoffset:position-independentcode(PIC)

• Whataboutreferencestostaticdata?– la getsbrokenupintolui andaddi (use auipc/addi forPIC)– Theserequirethefull32-bitaddressofthedata

• Thesecan’tbedeterminedyet,sowecreatetwotables…

539/19/17 Fall2017 - Lecture#8

Page 54: CS 61C: Great Ideas in Computer Architecture Running a

SymbolTable

• Listof“items”inthisfilethatmaybeusedbyotherfiles• Whatarethey?– Labels:functioncalling– Data:anythinginthe.data section;variableswhichmaybeaccessedacrossfiles

9/19/17 Fall2017 - Lecture#8 54

Page 55: CS 61C: Great Ideas in Computer Architecture Running a

RelocationTable

• Listof“items”whoseaddressthisfileneedsWhatarethey?– Anyabsolutelabeljumpedto:jal, jalr• Internal• External(includinglibfiles)• Suchasthela instructionE.g.,forjalr baseregister

– Anypieceofdatainstaticsection• Suchasthela instructionE.g.,forlw/sw baseregister

559/19/17 Fall2017 - Lecture#8

Page 56: CS 61C: Great Ideas in Computer Architecture Running a

ObjectFileFormat

• objectfileheader:sizeandpositionoftheotherpiecesoftheobjectfile• textsegment:themachinecode• datasegment:binaryrepresentationofthestaticdatainthesourcefile• relocationinformation:identifieslinesofcodethatneedtobefixeduplater

• symboltable:listofthisfile’slabelsandstaticdatathatcanbereferenced

• debugginginformation• AstandardformatisELF(exceptMS)

http://www.skyfree.org/linux/references/ELF_Format.pdf

569/19/17 Fall2017 - Lecture#8

Page 57: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

9/19/17 Fall2017 - Lecture#8 57

Page 58: CS 61C: Great Ideas in Computer Architecture Running a

WhereAreWeNow?

9/19/17 Fall2017 - Lecture#8 58

Page 59: CS 61C: Great Ideas in Computer Architecture Running a

Linker(1/3)

• Input:Objectcodefiles,informationtables(e.g.,foo.o,libc.o forRISC-V)

• Output:Executablecode(e.g.,a.out forRISC-V)• Combinesseveralobject(.o)filesintoasingleexecutable(“linking”)• Enableseparatecompilationoffiles– Changestoonefiledonotrequirerecompilationofthewholeprogram

• Linuxsource>20Mlinesofcode!

– Oldname“LinkEditor”fromeditingthe“links”injumpandlinkinstructions

599/19/17 Fall2017 - Lecture#8

Page 60: CS 61C: Great Ideas in Computer Architecture Running a

.o file 1text 1data 1info 1

.o file 2text 2data 2info 2

Linker

a.outRelocated text 1Relocated text 2Relocated data 1Relocated data 2

Linker(2/3)

9/19/17 Fall2017 - Lecture#8 60

Page 61: CS 61C: Great Ideas in Computer Architecture Running a

Linker(3/3)

• Step1:Taketextsegmentfromeach.o fileandputthemtogether

• Step2:Takedatasegmentfromeach.o file,putthemtogether,andconcatenatethisontoendoftextsegments

• Step3:Resolvereferences– GothroughRelocationTable;handleeachentry– I.e.,fillinallabsoluteaddresses

9/19/17 Fall2017 - Lecture#8 61

Page 62: CS 61C: Great Ideas in Computer Architecture Running a

FourTypesofAddresses

• PC-RelativeAddressing(beq,bne,jal; auipc/addi)– Neverneedtorelocate(PIC:positionindependentcode)

• AbsoluteFunctionAddress(auipc/jalr)– Alwaysrelocate

• ExternalFunctionReference(auipc/jalr)– Alwaysrelocate

• StaticDataReference(oftenlui/addi)– Alwaysrelocate

629/19/17 Fall2017 - Lecture#8

Page 63: CS 61C: Great Ideas in Computer Architecture Running a

AbsoluteAddressesinRISC-V• Whichinstructionsneedrelocationediting?– J-format:jump/jumpandlink

– I-,S- Format:Loadsandstorestovariablesinstaticarea,relativetoglobalpointer

– Whataboutconditionalbranches?

– PC-relativeaddressingpreservedevenifcodemoves63

jalxxxxx

lwgp rdxxx

beqbners1 rs2 x

9/19/17 Fall2017 - Lecture#8

rd

swrs1 gpxx

xx

x

Page 64: CS 61C: Great Ideas in Computer Architecture Running a

ResolvingReferences(1/2)

• Linkerassumesfirstwordoffirsttextsegmentisataddress0x10000 forRV32.– (Morelaterwhenwestudy“virtualmemory”)

• Linkerknows:– Lengthofeachtextanddatasegment– Orderingoftextanddatasegments

• Linkercalculates:– Absoluteaddressofeachlabeltobejumpedto(internalorexternal)andeachpieceofdatabeingreferenced

649/19/17 Fall2017 - Lecture#8

Page 65: CS 61C: Great Ideas in Computer Architecture Running a

ResolvingReferences(2/2)

• Toresolvereferences:– Searchforreference(dataorlabel)inall“user”symboltables– Ifnotfound,searchlibraryfiles(e.g.,forprintf)– Onceabsoluteaddressisdetermined,fillinthemachinecodeappropriately

• Outputoflinker:executablefilecontainingtextanddata(plusheader)

659/19/17 Fall2017 - Lecture#8

Page 66: CS 61C: Great Ideas in Computer Architecture Running a

Administrivia

• Midterm#1in1week:September26!– INCLASS!8-9:30AMWheelerAuditorium+fiveSpillRoomsbasedonclasslogin

– NumberRepresentations,C(pointers,arrays,strings),mappingCtoRISC-VAssembly(andviceversa),NOMACHINELANGUAGEONTHISEXAM

– Singledoublesided8.5”x11”cheatsheet+wegiveyoutheRISC-VGreenCard

– ReviewsessionSaturday,September23:TimeandplaceTBD– DSPstudents:pleasemakesureweknowaboutyourspecialaccommodations(contactTAStevenHo)

669/19/17 Fall2017 - Lecture#8

Page 67: CS 61C: Great Ideas in Computer Architecture Running a

Break!

679/19/17 Fall2017 - Lecture#8

Page 68: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

689/19/17 Fall2017 - Lecture#8

Page 69: CS 61C: Great Ideas in Computer Architecture Running a

WhereAreWeNow?

699/19/17 Fall2017 - Lecture#8

Page 70: CS 61C: Great Ideas in Computer Architecture Running a

LoaderBasics

• Input:ExecutableCode(e.g.,a.out forRISC-V)• Output:(programisrun)• Executablefilesarestoredondisk• Whenoneisrun,loader’sjobistoloaditintomemoryandstartitrunning

• Inreality,loaderistheoperatingsystem(OS)– LoadingisoneoftheOStasks

709/19/17 Fall2017 - Lecture#8

Page 71: CS 61C: Great Ideas in Computer Architecture Running a

Loader…WhatDoesItDo?

• Readsexecutablefile’sheadertodeterminesizeoftextanddatasegments• Createsnewaddressspaceforprogramlargeenoughtoholdtextanddatasegments,alongwithastacksegment

• Copiesinstructions+datafromexecutablefileintothenewaddressspace• Copiesargumentspassedtotheprogramontothestack• Initializesmachineregisters– Mostregisterscleared,butstackpointerassignedaddressof1stfreestacklocation

• Jumpstostart-uproutinethatcopiesprogram’sargumentsfromstacktoregisters&setsthePC– Ifmainroutinereturns,start-uproutineterminatesprogramwiththeexitsystemcall

719/19/17 Fall2017 - Lecture#8

Page 72: CS 61C: Great Ideas in Computer Architecture Running a

PeerInstructionAtwhatpointinprocessareallthemachinecodebitsdeterminedforthefollowingassemblyinstructions:1)add x6, x7, x82)jal x1, fprintf

A:1)&2)AftercompilationB:1)Aftercompilation,2)AfterassemblyC:1)Afterassembly,2)Afterlinking:1)Afterassembly,2)Afterloading

9/19/17 Fall2017 - Lecture#8 72

Page 73: CS 61C: Great Ideas in Computer Architecture Running a

PeerInstructionAtwhatpointinprocessareallthemachinecodebitsdeterminedforthefollowingassemblyinstructions:1)add x6, x7, x82)jal x1, fprintf

A:1)&2)AftercompilationB:1)Aftercompilation,2)AfterassemblyC:1)Afterassembly,2)Afterlinking:1)Afterassembly,2)Afterloading

9/19/17 Fall2017 - Lecture#8 73

Page 74: CS 61C: Great Ideas in Computer Architecture Running a

ExampleCProgram:Hello.c

#include <stdio.h> int main() { printf("Hello, %s\n", "world"); return 0;

}

749/19/17 Fall2017 - Lecture#8 74

Page 75: CS 61C: Great Ideas in Computer Architecture Running a

CompiledHello.c:Hello.s.text

.align 2

.globl mainmain:

addi sp,sp,-16sw ra,12(sp)lui a0,%hi(string1)addi a0,a0,%lo(string1)lui a1,%hi(string2)addi a1,a1,%lo(string2)call printflw ra,12(sp)addi sp,sp,16li a0,0ret.section .rodata.balign 4

string1:.string "Hello, %s!\n"

string2:.string "world"

# Directive: enter text section# Directive: align code to 2^2 bytes# Directive: declare global symbol main# label for start of main# allocate stack frame# save return address# compute address of# string1# compute address of# string2# call function printf# restore return address# deallocate stack frame# load return value 0# return# Directive: enter read-only data section# Directive: align data section to 4 bytes# label for first string# Directive: null-terminated string# label for second string# Directive: null-terminated string

759/19/17 Fall2017 - Lecture#8 75

Page 76: CS 61C: Great Ideas in Computer Architecture Running a

AssembledHello.s:LinkableHello.o00000000 <main>: 0: ff010113 addi sp,sp,-16 4: 00112623 sw ra,12(sp) 8: 00000537 lui a0,0x0 # addr placeholderc: 00050513 addi a0,a0,0 # addr placeholder10: 000005b7 lui a1,0x0 # addr placeholder14: 00058593 addi a1,a1,0 # addr placeholder18: 00000097 auipc ra,0x0 # addr placeholder1c: 000080e7 jalr ra # addr placeholder20: 00c12083 lw ra,12(sp) 24: 01010113 addi sp,sp,16 28: 00000513 addi a0,a0,0 2c: 00008067 jalr ra

769/19/17 Fall2017 - Lecture#8 76

Page 77: CS 61C: Great Ideas in Computer Architecture Running a

LinkedHello.o:a.out000101b0 <main>:

101b0: ff010113 addi sp,sp,-16 101b4: 00112623 sw ra,12(sp) 101b8: 00021537 lui a0,0x21 101bc: a1050513 addi a0,a0,-1520 # 20a10 <string1> 101c0: 000215b7 lui a1,0x21 101c4: a1c58593 addi a1,a1,-1508 # 20a1c <string2> 101c8: 288000ef jal ra,10450 # <printf> 101cc: 00c12083 lw ra,12(sp) 101d0: 01010113 addi sp,sp,16 101d4: 00000513 addi a0,0,0101d8: 00008067 jalr ra

779/19/17 Fall2017 - Lecture#8

Page 78: CS 61C: Great Ideas in Computer Architecture Running a

LUI/ADDIAddressCalculationinRISC-VTargetaddressof<string1>is0x00020 A10InstructionsequenceLUI 0x00020,ADDI 0xA10 doesnotquiteworkbecauseimmediates inRISC-Varesignextended(and0xA10 hasa1inthehighorderbit)!

0x00020 000 + 0xFFFFF A10 = 0x0001F A10 (Offby0x00001000)Sowegettherightaddressifwecalculateitasfollows:

(0x00020 000 + 0x00001 000) + 0xFFFFF A10 = 0x00020 A10Whatis0xFFFFF A10?

Twoscomplementof0xFFFFF A10 =0x00000 5EF + 1 =0x00000 5F0 =1520tenSo0xFFFFF A10 =-1520ten

InstructionsequenceLUI 0x00021,ADDI -1520 calculates0x00020 A10

789/19/17 Fall2017 - Lecture#8

Page 79: CS 61C: Great Ideas in Computer Architecture Running a

Staticvs.DynamicallyLinkedLibraries

• Whatwe’vedescribedisthetraditionalway:statically-linkedapproach– Libraryisnowpartoftheexecutable,soifthelibraryupdates,wedon’tgetthefix(havetorecompileifwehavesource)

– Includestheentire libraryevenifnotallofitwillbeused– Executableisself-contained

• Alternativeisdynamicallylinkedlibraries(DLL),commononWindows&UNIXplatforms

9/19/17 Fall2017 - Lecture#8 79

Page 80: CS 61C: Great Ideas in Computer Architecture Running a

DynamicallyLinkedLibraries

• Space/timeissues+Storingaprogramrequireslessdiskspace+Sendingaprogramrequireslesstime+Executingtwoprogramsrequireslessmemory(iftheysharealibrary)– Atruntime,there’stimeoverheadtodolink

• Upgrades+Replacingonefile(libXYZ.so)upgradeseveryprogramthatuseslibrary“XYZ”– Havingtheexecutableisn’tenoughanymore

Overall, dynamic linking adds quite a bit of complexity to the compiler, linker, and operating system. However, it provides many benefits that often outweigh these

en.wikipedia.org/wiki/Dynamic_linking

9/19/17 Fall2017 - Lecture#8 80

Page 81: CS 61C: Great Ideas in Computer Architecture Running a

DynamicallyLinkedLibraries

• Prevailingapproachtodynamiclinkingusesmachinecodeasthe“lowestcommondenominator”– Linkerdoesnotuseinformationabouthowtheprogramorlibrarywascompiled(i.e.,whatcompilerorlanguage)

– Canbedescribedas“linkingatthemachinecodelevel”– Thisisn’ttheonlywaytodoit...

9/19/17 Fall2017 - Lecture#8 81

Page 82: CS 61C: Great Ideas in Computer Architecture Running a

Outline

• ReviewInstructionFormats+J/JAL• MultiplyandDivide• Interpretationvs.Translation• Assembler• Linker• Loader• AndinConclusion…

829/19/17 Fall2017 - Lecture#8

Page 83: CS 61C: Great Ideas in Computer Architecture Running a

AndInConclusion,…§ CompilerconvertsasingleHLLfileintoasingle

assemblylanguagefile§ Assemblerremovespseudo-instructions,

convertswhatitcantomachinelanguage,andcreatesachecklistforthelinker(relocationtable):A.s filebecomesa.o fileú Does2passestoresolveaddresses,handlinginternal

forwardreferences

§ Linkercombinesseveral.o filesandresolvesabsoluteaddressesú Enablesseparatecompilation,librariesthatneednot

becompiled,andresolvesremainingaddresses

§ Loaderloadsexecutableintomemoryandbeginsexecution9/19/17 Fall2017 - Lecture#8 83