Real-Time Computing and Communications Lab., Hanyang University http://rtcc.hanyang.ac.kr Real-Time Computing and Communications Lab., Hanyang University http://rtcc.hanyang.ac.kr Compiler, Assembler, and Linker Minsoo Ryu Department of Computer Science and Engineering Hanyang University [email protected]
38
Embed
Compiler, Assembler, and Linker - Hanyangrtcc.hanyang.ac.kr/.../Lecture_05_Compiler_Assembler_and_Linker.pdf · Compiler, Assembler, and Linker Minsoo Ryu Department of Computer Science
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Compiler, Assembler, and Linker
Minsoo Ryu
Department of Computer Science and EngineeringHanyang University
2Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 2Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Contents
What is a Compilation? Preprocessor Compiler Assembler Linker Loader
3Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 3Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
What is a Compilation?
A compilation is a translation process that reads a program written in one language--the source language--and translate it into an equivalent program in another language--the target language
CompilationSource Program Target Program
4Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 4Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
translation and SYMTAB constructionassembly into machine code
loading program into memory(optionally with relocation)
relocatablemachine code (.o)
concatenation, reference resolving (relocation)
5Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 5Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Contents
What is a Compilation? Preprocessor Compiler Assembler Linker Loader
6Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 6Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Preprocessor Inclusion
Copies the full content of a included file into the current file at the point at which the directive occurs
Eg.) #include “…” directive in ‘C’
Macros Replace each macro call, in-line, with the corresponding macro
definition E.g.) #define … … directive in ‘C’
Conditional compilation (include or macro guard) In combination with macros, remove code fragments that need
not be compiled E.g) #ifdef, #ifndef, #endif directives in ‘C’
Other compiler directives #pragma once (to ensure single inclusion)
7Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 7Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Notes on #pragma
The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages Pragmas are machine- or operating system-specific by
definition, and are usually different for every compiler Each implementation of C and C++ supports some features
unique to its host machine or operating system Some programs, for instance, need to exercise precise
control over the memory areas where data is placed or to control the way certain functions receive parameters
8Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 8Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Contents
What is a Compilation? Preprocessor Compiler Assembler Linker Loader
9Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 9Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
11Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 11Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Lexical Analysis
Lexical analysis is also known as linear addressing or scanning
Lexical analysis; Reads the source program from left-to-right Identifies the lexemes in the input list of characters and
categorize them into tokens• Lexeme (어휘) and lexicon (어휘집합)
A token is a syntactic category Token = lexeme’s type (+ its value) In English: noun, verb, adjective, … In a programming language: Identifier, Integer, Keyword, …
12Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 12Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
translation and SYMTAB constructionassembly into machine code
concatenation,reference resolving (relocation)
loading program into memory(optionally with relocation)
relocatablemachine code (.o)
35Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 35Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Linker
Linker is also known as linkage editor
Three steps Step 1: concatenate all the text segments from all the .o files Step 2: concatenate all the data/bss segments from all the .o
files Step 3: Resolve references
• Use the relocation tables and the symbol tables to compute all absolute addresses
36Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 36Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Linker
The linker knows The length of each text and data segment The order in which they are
Therefore the linker can compute an absolute address for each label assuming the beginning of the executable file is at address 0x0
For each label being referenced (that is for each line of code that’s pointed to by the relocation table), find where it is defined In the symbol table of a .o file In some specified or standard library file (e.g., fprintf)
If not found, print a “symbol not found” error message and abort If found in multiple tables, print a “multiply defined” error
message and abort If found in exactly one table, replace the label by an absolute
address
37Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 37Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Linker
38Real-Time Computing and Communications Lab., Hanyang University
http://rtcc.hanyang.ac.kr 38Real-Time Computing and Communications Lab., Hanyang Universityhttp://rtcc.hanyang.ac.kr
Loader
With a linked executable, all addresses are known so that the program can run
To actually run the program we need to use a loader, which is part of the O/S
The loader does the following: Read the executable file’s header to find out the size of the text
and data segments Creates a new address space for the program that is large
enough to hold the text and data segments, and to hold the stack (within some bounds)
Copies the text and data segments into the address space Copies arguments passed to the program on the stack Initializes the registers Jump to a standard “start up routine”, which sets the PC and
calls the exit() system call when the program terminates