Download presentation
Presentation is loading. Please wait.
Published byGriffin Roberts Modified over 9 years ago
1
The Assembly Language Level Part C – Linking and Loading
2
Linking and loading Note that we didn’t have to write System.out.println() or Math.sin() (Java) or printf() or sin() (C/C++) yet we were able to use it! Where is the code for these function? – Is it source code or binary? By what mechanism are we able to call them?
3
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Linking and Loading Generation of an executable binary program from a collection of independently translated source procedures requires using a linker. Note: Translator causes a level change but linker does not.
4
Purpose of linker To collect separately translated functions and data and link them to run as a unit (executable binary program). – ILC (Instruction Location Counter) starts at 0 when assembling each module. – But 2 or more modules can’t all start at 0 in the executable! – Linker’s job is to resolve this via relocation.
5
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (1) Each module has its own address space, starting at 0. external reference to B
6
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (1) Each module has its own address space, starting at 0!
7
Relocation problem Each object module represents a separate address space. But we need a single, linear address space.
8
Linker steps: 1.Construct a table of all the object modules and their lengths. 2.Assign a starting address to each object module. 3.For all instructions that reference memory, add a relocation constant equal to the starting address of the module. 4.For all instructions that reference other procedures, insert the address of these procedures in place.
9
Linker steps: 1.Construct a table of all the object modules and their lengths. 2.Assign a starting address to each object module. 3.For all instructions that reference memory, add a relocation constant equal to the starting address of the module. modulelengthstarting address A400100 B600500 C5001100 D3001600
10
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (5) The object modules after being positioned in the binary image but before being relocated and linked. X Note: Exe’s are often linked to start above address 0 to catch bad pointers and for other special purposes.
11
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (5) The branching structure we want appears above.
12
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (5) Unfortunately, the branching structure that we have appears below!
13
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (5) Note that the references to data are bollixed up as well! (From webster.com: to throw into disorder; also: bungle — usually used with up.)
14
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Tasks Performed by the Linker (6) The same object modules after linking and after relocation (of code and data) has been performed. Together they form an executable binary program, ready to run.
15
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Structure of an Object Module The internal structure of an object module produced by a translator.
16
Parts of an object module 1.module name, date, lengths 2.list of public symbols and values in module 3.list of extern(al) symbols used by module – includes list of location of instructions that refer to these symbols 4.code and constants – this is the only part that is actually loaded and executed 5.relocation dictionary – list of memory addresses that require the addition of a relocation constant 6.end-of-module mark, checksum
17
Linker passes Pass 1: – read all object modules – build table of module names and lengths – build global symbol table of entry points and external references Pass 2: – read all object modules (again) – relocate their contents – write all of them out to one file (executable file)
18
Linking with MASM \masm32\bin\ml /c /coff /Cp /nologo /Zd /Zi /Fl /Fm /FR /DDebug test.asm – creates test.obj from test.asm \masm32\bin\ml /c /coff /Cp /nologo /Zd /Zi /Fl /Fm /FR /DDebug utilities.asm – creates utilities.obj from utilities.asm \masm32\bin\ link /nologo /map /debug /subsystem:console test.obj utilities.obj – creates test.exe from test.obj and utilities.obj
19
THE END skip 7.4.3 and 7.4.4
20
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Binding Time and Dynamic Relocation The relocated binary program of Fig. 7-15(b) moved up 300 addresses. Many instructions now refer to an incorrect memory address.
21
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Dynamic Linking in MULTICS (1) Before EARTH is called.
22
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Dynamic Linking in MULTICS (2) After EARTH has been called and linked.
23
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved. 0-13-148521-0 Dynamic Linking in Windows Use of a DLL file by two processes.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.