1 Assemblers and Linkers Professor Jennifer Rexford COS 217.

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Assemblers and Linkers CS Goals of This Lecture Compilation process  Compile, assemble, archive, link, execute Assembling  Representing instructions.
1 Assembly Language Professor Jennifer Rexford COS 217.
1 Assemblers and Linkers Professor Jennifer Rexford
1 Assemblers and Linkers. 2 Goals for this Lecture Help you to learn about: The assembly process IA-32 machine language Why? Machine language is the last.
X86 ISA Compiler Baojian Hua Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer.
High-Level Language Interface Chapter 17 S. Dandamudi.
6.828: PC hardware and x86 Frans Kaashoek
Y86 Processor State Program Registers
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
MIPS coding. SPIM Some links can be found such as:
CSC 221 Computer Organization and Assembly Language
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Chapter 1 Computer architecture Languages: machine, assembly, high
Languages and the Machine Chapter 5 CS221. Topics The Compilation Process The Assembly Process Linking and Loading Macros We will skip –Case Study: Extensions.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
1 Assemblers System Programming by Leland L. Beck Chapter 2.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
1 Assemblers and Linkers Jennifer Rexford. 2 Goals for this Lecture Machine language The last stop on the “language levels” tour IA-32 machine language.
8086/8088 Instruction Set, Machine Codes and Addressing Modes.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
CSc 453 Linking and Loading
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 4 - Assembler 1.
Lecture 3 Translation.
Instruction Set Architecture
Assemblers, linkers, loaders
Instruction Set Architectures
Assembly language.
Credits and Disclaimers
Chapter 7 Assembly Language
IA32 Processors Evolutionary Design
Computer Architecture and Assembly Language
Aaron Miller David Cohen Spring 2011
Chapter 7 Assembly Language
Homework In-line Assembly Code Machine Language
Assembly Language Programming V: In-line Assembly Code
Y86 Processor State Program Registers
BIC 10503: COMPUTER ARCHITECTURE
Assembly Language Programming I: Introduction
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Practical Session 4.
Machine-Level Programming: Introduction
System Programming by Leland L. Beck Chapter 2
Chapter 1 Computer architecture Languages: machine, assembly, high
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Low level Programming.
Presentation transcript:

1 Assemblers and Linkers Professor Jennifer Rexford COS 217

2 Goals of This Lecture Compilation process  Compile, assemble, archive, link, execute Assembling  Representing instructions –Prefix, opcode, addressing modes, operands  Translating labels into memory addresses –Symbol table, and filling in local addresses  Connecting symbolic references with definitions –Relocation records  Specifying the regions of memory –Generating sections (data, BSS, text, etc.) Linking  Concatenating object files  Patching references

3 Compilation Pipeline Compiler (gcc):.c .s  High-level to assembly language Assembler (as):.s .o  Assembly to machine language Archiver (ar):.o .a  Object files into single library Linker (ld):.o +.a  a.out  Builds an executable file Execution (execlp)  Loads executable and starts it Compiler Assembler Archiver Linker/Loader Execution.c.s.o.a a.out

4 Assembler Assembly language  A symbolic representation of machine instructions Machine language  Contains everything needed to link, load, and execute the program Assembler  Translates assembly language into machine language –Translate instruction mnemonics into op-codes –Translate symbolic names for memory locations  Stores the result in an object file (.o)

5 General IA32 Instruction Format Prefixes: we won’t worry about these for now Opcode ModR/M and SIB (scale-index-base): for memory operands Displacement and immediate: depending on opcode, ModR/M and SIB Note: byte order is little-endian (low-order byte of word at lower addresses) Instruction prefixes OpcodeModR/MSIBDisplacementImmediate Up to 4 prefixes of 1 byte each (optional) 1, 2, or 3 byte opcode 1 byte (if required) 1 byte (if required) 0, 1, 2, or 4 bytes Mod Reg/ Opcode R/M ScaleIndexBase

6 Example: Push on to Stack Assembly language: pushl %edx Machine code:  IA32 has a separate opcode for push for each register operand – 50: pushl %eax – 51: pushl %ecx – 52: pushl %edx – …  Results in a one-byte instruction Observe: sometimes one assembly language instruction can map to a group of different opcodes

7 Example: Load Effective Address Assembly language: leal (%eax,%eax,4), %eax Machine code:  Byte 1: 8D (opcode for “load effective address”)  Byte 2: 04 (dest %eax, with scale-index-base)  Byte 3: 80 (scale=4, index=%eax, base=%eax) Load the address %eax + 4 * %eax into register %eax

8 Example: Movl (Opcode 44) Mod=11 table EAX 0 ECX 1 EDX 2 EBX 3 ESP 4 EBP 5 ESI 6 EDI 7 Instruction prefixes OpcodeDisplacementImmediate ModR/M ModMod regR/M SIB S I B movl %ecx, %ebx mode %_, %_ ebxecx mov r/m32,r movl 12(%ecx), %ebx mode disp8(%_), %_ Mod=01 table [EAX]  disp8 0 [ECX]  disp8 1 [EDX]  disp8 2 [EBX]  disp8 3 [--][--]  disp8 4 [EBP]  disp8 5 [ESI]  disp8 6 [EDI]  disp8 7 ebx(ecx) 12 Reference: IA-32 Intel Architecture Software Developer’s Manual, volume 2, page 2-1, page 2-6, and page 3-441

9 Example: Mov Immediate to Memory Instruction prefixes OpcodeDisplacementImmediate ModR/M ModMod regR/M SIB S IB movb $97, mov r/m8,imm8 mode disp32 Mod=00 table [EAX] 0 [ECX] 1 [EDX] 2 [EBX] 3 [--][--] 4 disp32 5 [ESI] 6 [EDI]

10 Encoding as Byte String Instruction prefixes OpcodeDisplacementImmediate ModR/M ModMod regR/M SIB S IB movb $97, C6 05 E little-endian

11 Assembly Language movb $97, C6 05 E globl grade.data grade:.byte67.text. movb$97, grade. located at address 999 char grade = 67; … grade = ‘a’;

12 Symbol Manipulation.text... movl count, %eax....data count:.word 0....globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: Create labels and remember their addresses Deal with the “forward reference problem”

13 Dealing with Forward References Most assemblers have two passes  Pass 1: symbol definition  Pass 2: instruction assembly Or, alternatively,  Pass 1: instruction assembly  Pass 2: patch the cross-reference

14 Implementing an Assembler.s file instruction.o file input assembleoutput symbol table data section text section bss section diskin memory structure diskin memory structure

15 Input Functions Read assembly language and produce list of instructions.s file instruction.o file input assembleoutput symbol table data section text section bss section

16 Input Functions Lexical analyzer  Group a stream of characters into tokens add %g1, 10, %g2 Syntactic analyzer  Check the syntax of the program Instruction list producer  Produce an in-memory list of instruction data structures instruction

17 Instruction Assembly... loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D disp? 5 2 E 8 E 9 disp? [0] [2] [4] [5] [10] [15] 1 byte 4 bytes How to compute the address displacements?

18 Symbol Table.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D disp_s 5 2 E 8 E 9 disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress

19 Symbol Table.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D disp_s 5 2 E 8 E 9 disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress

20 Symbol Table.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D disp_s 5 2 E 8 E 9 disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress +13

21 Symbol Table.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D disp_s 5 2 E 8 E 9 disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress +13

22 Filling in Local Addresses.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D E 8 E disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress

23 Filling in Local Addresses.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D E 8 E disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress

24 Filling in Local Addresses.globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D E 8 E disp_l [0] [2] [4] [5] [10] [15] defloop0 disp_sdone2 disp_lfoo5 disp_lloop10 defdone15 loop done typelabeladdress

25 Relocation Records....globl loop loop: cmpl %edx, %eax jge done pushl %edx call foo jmp loop done: 3 9D 0 7 D E 8 E disp_l loop0 foo5 def disp_l [0] [2] [4] [5] [10] [15]

26 Assembler Directives Delineate segments .section Allocate/initialize data and bss segments .word.half.byte .ascii.asciz .align.skip Make symbols in text externally visible .global

27 Assemble into Sections Process instructions and directives to produce object file output structures.s file instruction.o file input assemble output symbol table data section text section bss section

28 Output Functions Machine language output  Write symbol table and sections into object file.s file instruction.o file input assemble output symbol table data section text section bss section

29 ELF: Executable and Linking Format Format of.o and a.out files  Output by the assembler  Input and output of linker ELF Header Program Hdr Table Section 1 Section n... Section Hdr Table optional for.o files optional for a.out files

30 Invoking the Linker ld bar.o main.o –l libc.a –o a.out compiled program modules library (contains more.o files) output (also in “.o” format, but no undefined symbols) Invoked automatically by gcc, but you can call it directly if you like.

31 Multiple Object Files 3 9D 0 7 D E 8 E disp_l loop0 foo5 def disp_l main0 foo8 start def loop15disp_l bar.omain.o [0] [2] [4] [5] [10] [15] [0] [2] [4] [7] [8] [12] [15] [20]

32 Step 1: Pick An Order 3 9D 0 7 D E 8 E disp_l loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35]

33 Step 1: Pick An Order 3 9D 0 7 D E 8 E disp_l loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35]

34 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35] disp_l

35 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35] disp_l

36 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35] disp_l

37 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35] disp_l

38 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35]

39 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35]

40 Step 2: Patch 3 9D 0 7 D E 8 E =18 loop0 foo5 def disp_l main15+0 foo15+8 start def loop15+15disp_l 0-(15+15)=-30 bar.omain.o [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35]

41 Step 3: Concatenate 3 9D 0 7 D E 8 E main15+0start -30 a.out [0] [2] [4] [5] [10] [15] [17] [19] [22] [23] [27] [30] [35]

42 Summary Assember  Read assembly language  Two-pass execution (resolve symbols)  Produce object file Linker  Order object codes  Patch and resolve displacements  Produce executable