19
CS61C L13 CALL I (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #13 – Compiling, Assembly, Linking, Loader I 2008-7-14 CS61C L13 CALL I (2) Chae, Summer 2008 © UCB Review Disassembly is simple and starts by decoding opcode field. Be creative, efficient when authoring C Assembler expands real instruction set (TAL) with pseudoinstructions (MAL) Only TAL can be converted to raw binary Assemblers job to do conversion Assembler uses reserved register $at MAL makes it much easier to write MIPS

inst.eecs.berkeley.edu/~cs61c CS61C : Machine …cs61c/su08/lectures/13/L13-ac-call-2up.… · •Disassembly is simple and starts by decoding opcode field. •Be creative, efficient

  • Upload
    lyhanh

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

CS61C L13 CALL I (1) Chae, Summer 2008 © UCB

Albert Chae, Instructor

inst.eecs.berkeley.edu/~cs61cCS61C : Machine Structures

Lecture #13 – Compiling, Assembly, Linking,Loader I

2008-7-14

CS61C L13 CALL I (2) Chae, Summer 2008 © UCB

Review

•Disassembly is simple and starts bydecoding opcode field.

• Be creative, efficient when authoring C

•Assembler expands real instruction set(TAL) with pseudoinstructions (MAL)

• Only TAL can be converted to raw binary• Assembler’s job to do conversion• Assembler uses reserved register $at• MAL makes it much easier to write MIPS

CS61C L13 CALL I (3) Chae, Summer 2008 © UCB

Overview

• Interpretation vs Translation•Translating C Programs

• Compiler• Assembler• Linker• Loader

•Example

CS61C L13 CALL I (4) Chae, Summer 2008 © UCB

Language Continuum

• In general, we interpret a high levellanguage if efficiency is not critical ortranslated to a lower level language toimprove performance

Easy to programInefficient to interpret

EfficientDifficult to program

SchemeJavaC++ C Assembly machine language

Java bytecode

CS61C L13 CALL I (5) Chae, Summer 2008 © UCB

Interpretation vs Translation

•How do we run a program written in asource language?• Interpreter: Directly executes aprogram in the source language•Translator: Converts a program fromthe source language to an equivalentprogram in another language•For example, consider a Schemeprogram foo.scm

CS61C L13 CALL I (6) Chae, Summer 2008 © UCB

Interpretation

• Scheme Interpreter is just a programthat reads a scheme program andperforms the functions of thatscheme program.

CS61C L13 CALL I (7) Chae, Summer 2008 © UCB

Translation

• Scheme Compiler is a translatorfrom Scheme to machine language.

• The processor is a hardwareinterpeter of machine language.

CS61C L13 CALL I (8) Chae, Summer 2008 © UCB

Interpretation

•Any good reason to interpret machinelanguage in software?•MARS – useful for learning/debugging•What if you want to run compiledprograms (executables) from anotherISA?•Examples

• VirtualPC let Windows (compiled to x86)run on old Macs (680x0 or PowerPC)

• Run old video games on newer consoles

CS61C L13 CALL I (9) Chae, Summer 2008 © UCB

Machine Code Interpretation

• Apple’s Two Conversions• In the last 2 years, switched to Intel’s x86 from

IBM’s PowerPC• Could require all programs to be re-translated

from high level language• Did so with minimal disruption to programmer,

and especially the user- Rosetta allows old PowerPC programs to run on the

new x86 systems by runtime translation- Universal Binaries contain the machine code for

both platforms, so both systems can run at nativespeeds

• Did a similar thing 13 years ago when theyswitched from Motorola 680x0 instructionarchitecture to PowerPC

CS61C L13 CALL I (10) Chae, Summer 2008 © UCB

Interpretation vs. Translation? (1/2)

• Generally easier to write interpreter• Interpreter closer to high-level, so

can give better error messages (e.g.,SPIM)• Translator reaction: add extra

information to help debugging (linenumbers, names)

• Interpreter slower (10x?), codesmaller (2X?)

• Interpreter provides instruction setindependence: run on any machine

CS61C L13 CALL I (11) Chae, Summer 2008 © UCB

Interpretation vs. Translation? (2/2)

• Translated/compiled code almostalways more efficient and thereforehigher performance:• Important for many applications,

particularly operating systems.

• Translation/compilation helps “hide”the program “source” from the users:• One model for creating value in the

marketplace (eg. Microsoft keeps all theirsource code secret)

• Alternative model, “open source”, createsvalue by publishing the source code andfostering a community of developers.

CS61C L13 CALL I (12) Chae, Summer 2008 © UCB

Steps to Starting a Program (translation)

CS61C L13 CALL I (13) Chae, Summer 2008 © UCB

Compiler

• Input: High-Level Language Code(e.g., C, Java such as foo.c)•Output: Assembly Language Code(e.g., foo.s for MIPS)•Note: Output may containpseudoinstructions•Pseudoinstructions: instructions thatassembler understands but not inmachine. E.g.,• mov $s1,$s2 ⇒ or $s1,$s2,$zero

CS61C L13 CALL I (14) Chae, Summer 2008 © UCB

Where Are We Now?

CS164

CS61C L13 CALL I (15) Chae, Summer 2008 © UCB

Assembler

• Input: Assembly Language Code(e.g., foo.s for MIPS)•Output: Object Code, information tables(e.g., foo.o for MIPS)•Reads and Uses Directives•Replace Pseudoinstructions•Produce Machine Language•Creates Object File

CS61C L13 CALL I (16) Chae, Summer 2008 © UCB

Assembler Directives (p. A-51 to A-53)

•Give directions to assembler, but do notproduce machine instructions .text: Subsequent items put in user textsegment (machine code) .data: Subsequent items put in user datasegment (binary rep of data in source file) .globl sym: declares sym global and canbe referenced from other files .asciiz str: Store the string str inmemory and null-terminate it.word w1…wn: Store the n 32-bit quantitiesin successive memory words

CS61C L13 CALL I (17) Chae, Summer 2008 © UCB

Pseudoinstruction Replacement•Asm. treats convenient variations ofmachine language instructions as if realinstructionsPseudo: Real: subu $sp,$sp,32 addiu $sp,$sp,-32

sd $a0, 32($sp) sw $a0, 32($sp)sw $a1, 36($sp)

mul $t7,$t6,$t5 mul $t6,$t5mflo $t7

addu $t0,$t6,1 addiu $t0,$t6,1

ble $t0,100,loop slti $at,$t0,101bne $at,$0,loop

la $a0, str lui $at,left(str) ori $a0,$at,right(str)

CS61C L13 CALL I (18) Chae, Summer 2008 © UCB

Producing Machine Language (1/3)

•Simple Case• Arithmetic, Logical, Shifts, and so on.• All necessary info is within theinstruction already.

•What about Branches?• PC-Relative• So once pseudoinstructions arereplaced by real ones, we know by howmany instructions to branch.

•So these can be handled easily.

CS61C L13 CALL I (19) Chae, Summer 2008 © UCB

Producing Machine Language (2/3)

• “Forward Reference” problem• Branch instructions can refer to labels

that are “forward” in the program:

• Solved by taking 2 passes over theprogram.- First pass remembers position of labels- Second pass uses label positions to

generate code

or $v0, $0, $0L1:slt $t0, $0, $a1beq $t0, $0, L2addi $a1, $a1, -1j L1L2: add $t1, $a0, $a1

CS61C L13 CALL I (20) Chae, Summer 2008 © UCB

Producing Machine Language (3/3)

•What about jumps (j and jal)?• Jumps require absolute address.• So, forward or not, still can’t generatemachine instruction without knowing theposition of instructions in memory.

•What about references to data?•la gets broken up into lui and ori• These will require the full 32-bit addressof the data.

•These can’t be determined yet, so wecreate two tables…

CS61C L13 CALL I (21) Chae, Summer 2008 © UCB

Symbol Table•List of “items” in this file that may beused by other files.•What are they?

• Labels: function calling• Data: anything in the .data section;variables which may be accessed acrossfiles

CS61C L13 CALL I (22) Chae, Summer 2008 © UCB

Relocation Table

•List of “items” for which this fileneeds the address.•What are they?

• Any label jumped to: j or jal- internal- external (including lib files)

• Any piece of data- such as the la instruction

CS61C L13 CALL I (23) Chae, Summer 2008 © UCB

Object File Format•object file header: size and position ofthe other pieces of the object file• text segment: the machine code•data segment: binary representation ofthe data in the source file• relocation information: identifies lines ofcode that need to be “handled”•symbol table: list of this file’s labels anddata that can be referenced•debugging information•A standard format is ELF (except MS)http://www.skyfree.org/linux/references/ELF_Format.pdf

CS61C L13 CALL I (24) Chae, Summer 2008 © UCB

Administrivia•Assignments

• Proj1 extended to 7/13 @ 11:59pm• Quiz 5/6 due 7/14 @ 11:59pm• HW3 due 7/16 @ 11:59pm• Proj2 due 7/18 @ 11:59pm

•My OH today are canceled•Go to TA’s OH

CS61C L13 CALL I (25) Chae, Summer 2008 © UCB

Administrivia…Midterm

•Midterm Mon 2008-07-21@7-10pm, 155Dwinelle

• Conflicts/DSP? Email me• You can bring green sheet and onehandwritten double sided note sheet

• faux midterm: 7/16 @ 6-9pm 10 Evans• review session: 7/17 in lecture (will goover faux exam)

CS61C L13 CALL I (26) Chae, Summer 2008 © UCB

Peer Instruction

1. Assembler knows where a module’s data &instructions are in relation to other modules.

2. Assembler will ignore the instructionLoop:nop because it does nothing.

3. Java designers used an interpreter (ratherthan a translater) mainly because of (at leastone of): ease of writing, better error msgs,smaller object code.

ABC1: FFF2: FFT3: FTF4: FTT5: TFF6: TFT7: TTF8: TTT

CS61C L13 CALL I (27) Chae, Summer 2008 © UCB

Peer Instruction Answer

1. Assembler knows where a module’s data &instructions are in relation to other modules.

2. Assembler will ignore the instructionLoop:nop because it does nothing.

3. Java designers used an interpreter (ratherthan a translater) mainly because of (at leastone of): ease of writing, better error msgs,smaller object code.

ABC1: FFF2: FFT3: FTF4: FTT5: TFF6: TFT7: TTF8: TTT

1. Assembler only sees one compiled program at a time,that’s why it has to make a symbol & relocation table.It’s the job of the linker to link them all together…F!

2. Assembler keeps track of all labels in symbol table…F!3. Java designers used

an interpreter mainlybecause of code portability…F!

CS61C L13 CALL I (28) Chae, Summer 2008 © UCB

Where Are We Now?

CS61C L13 CALL I (29) Chae, Summer 2008 © UCB

Link Editor/Linker (1/3)• Input: Object Code, information tables(e.g., foo.o for MIPS)•Output: Executable Code(e.g., a.out for MIPS)•Combines several object (.o) files intoa single executable (“linking”)•Enable Separate Compilation of files

• Changes to one file do not requirerecompilation of whole program

- Windows NT source is >40 M lines of code!• Link Editor name from editing the “links”in jump and link instructions

CS61C L13 CALL I (30) Chae, Summer 2008 © UCB

Link Editor/Linker (2/3).o file 1text 1data 1info 1

.o file 2text 2data 2info 2

Linker

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

CS61C L13 CALL I (31) Chae, Summer 2008 © UCB

Link Editor/Linker (3/3)

•Step 1: Take text segment from each.o file and put them together.•Step 2: Take data segment from each.o file, put them together, andconcatenate this onto end of textsegments.•Step 3: Resolve References

• Go through Relocation Table and handleeach entry

• That is, fill in all absolute addresses

CS61C L13 CALL I (32) Chae, Summer 2008 © UCB

• PC-Relative Addressing (beq, bne)• never relocate

• Absolute Address (j, jal)• always relocate

• External Reference (usually jal)• always relocate

• Data Reference (often lui and ori)• always relocate

Four Types of Addresses we’ll discuss

CS61C L13 CALL I (33) Chae, Summer 2008 © UCB

Absolute Addresses in MIPS•Which instructions need relocationediting?•J-format: jump, jump and linkj/jal xxxxx

•Loads and stores to variables in staticarea, relative to global pointerlw/sw $gp $x address

•What about conditional branches?beq/bne $rs $rt address•PC-relative addressing preservedeven if code moves

CS61C L13 CALL I (34) Chae, Summer 2008 © UCB

Resolving References (1/2)

•Linker assumes first word of first textsegment is at address 0x00000000.•Linker knows:

• length of each text and data segment• ordering of text and data segments

•Linker calculates:• absolute address of each label to bejumped to (internal or external) and eachpiece of data being referenced

CS61C L13 CALL I (35) Chae, Summer 2008 © UCB

Resolving References (2/2)

•To resolve references:• search for reference (data or label) in allsymbol tables

• if not found, search library files(for example, for printf)

• once absolute address is determined, fillin the machine code appropriately

•Output of linker: executable filecontaining text and data (plus header)

CS61C L13 CALL I (36) Chae, Summer 2008 © UCB

Static vs Dynamically linked libraries

•What we’ve described is the traditionalway to create a static-linked approach

• The library is now part of the executable,so if the library updates we don’t get thefix (have to recompile if we have source)

• It includes the entire library even if not allof it will be used.

• Executable is self-contained.

•An alternative is dynamically linkedlibraries (DLL), common on Windows& UNIX platforms

• Having executable isn’t enough anymore!

CS61C L13 CALL I (37) Chae, Summer 2008 © UCB

Dynamically linked libraries

• Space/time issues• + Storing a program requires less disk space• + Sending a program requires less time• + Executing two programs requires less

memory (if they share a library)• – At runtime, there’s time overhead to do link

• Upgrades• + Replacing one file (libXYZ.so) upgrades

every program that uses library “XYZ”• – Having the executable isn’t enough anymore

Overall, dynamic linking adds quite a bit of complexity to the compiler, linker, andoperating system.

However, it provides many benefits that often outweigh these.

en.wikipedia.org/wiki/Dynamic_linking