Click here to load reader

IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking

  • View

  • Download

Embed Size (px)


IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking. Sumber : 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization , ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB. 21 April 2004 - PowerPoint PPT Presentation

Text of IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking

Pengantar Organisasi Komputer2. Hamacher. Computer Organization, ed-5
3. Materi kuliah CS61C/2000 & CS152/1997, UCB
21 April 2004
C program: foo.c
Assembly program: foo.s
#include <stdio.h>
int i;
sum = sum + i * i;
segment .text
All necessary info is within the instruction already.
What about Branches?
So once pseudoinstructions are replaced by real ones, we know by how many instructions to branch.
What about jumps?
These will require the full 32-bit address of the data.
Addresses can’t be determined yet, so we create two tables…
Symbol Table
List of “items” in this file that may be used by other files.
What are they?
Labels: function calling
Data: anything in the .data section; variables which may be accessed across files
First Pass: record label-address pairs
Second Pass: produce machine code
Result: can jump to a later label without first declaring it
List of “items” for which this file needs the address.
What are they?
Object File Format
object file header: size and position of the other pieces of the object file
text segment: the machine code
data segment: binary representation of the data in the source file
relocation information: identifies lines of code that need to be “handled”
symbol table: list of this file’s labels and data that can be referenced
debugging information
segment .text
0x00000064 DISP32 _printf
Link Editor/Linker
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, and concatenate this onto end of text segments.
Step 3: Resolve References
That is, fill in all absolute addresses
Data Reference: always relocate
Resolving References
Linker assumes first word of first text segment is at address 0x00000000.
Linker knows:
ordering of text and data segments
Linker calculates:
absolute address of each label to be jumped to (internal or external) and each piece of data being referenced
To resolve references:
search for reference (data or label) in all symbol tables
if not found, search library files (for example, for printf)
once absolute address is determined, fill in the machine code appropriately
Output of linker: executable file containing text and data (plus header)
segment .text
Executable files are stored on disk.
When one is run, loader’s job is to load it into memory and start it running.
In reality, loader is the operating system (OS)
loading is one of the OS tasks
So what does a loader do?
Reads executable file’s header to determine size of text and data segments
Creates new address space for program large enough to hold text and data segments, along with a stack segment
Copies instructions and data from executable file into the new address space (this may be anywhere in memory)
Initializes machine registers
Most registers cleared, but stack pointer assigned address of 1st free stack location
Jumps to start-up routine that copies program’s arguments from stack to registers and sets the PC
If main routine returns, start-up routine terminates program with the exit system call
0x000015c0: 0x20656854 0x206d7573 0x6d6f7266 0x2e203020
0x000015d0: 0x3031202e 0x73692030 0x0a642520 0xe5895500
0x000015e0: 0x0018ec81 0x45c70000 0x000000f8 0xfc45c700
0x000015f0: 0x00000000 0x64fc7d81 0x7e000000 0x0012e905
0x00001600: 0x458b0000 0x45af0ffc 0xf84501fc 0xe9fc45ff
0x00001610: 0xffffffe0 0xfff8c481 0x458bffff 0xc06850f8
0x00001620: 0xe8000015 0x00001778 0x0010c481 0xec890000
0x00001630: 0x0000c35d
0x000015c0: 54 68 65 20 73 75 62 20 66 72 6f 6d 20 30 20 2e
T h e s u m f r o m 0 .
000015dd: 55 push ebp
000015de: 89e5 mov ebp,esp
000015e0: 81ec18000000 sub esp,0x18
000015fb: 7e05 jle 0x1602
000015fd: e912000000 jmp 0x1614
.ascii "The sum from 0 .. 100 is %d\12\0"
.ascii "The sum from 0 .. 100 is %d\12\0"
.ascii "The sum from 0 .. 100 is %d\12\0"

Search related