Together, We make the difference. MCLinker Intermediate Representation Luba Tang 2013/2/24.

Slides:



Advertisements
Similar presentations
lld The LLVM Linker Friday, April 13, 2012
Advertisements

Target Code Generation
Program Development Tools The GNU (GNU’s Not Unix) Toolchain The GNU toolchain has played a vital role in the development of the Linux kernel, BSD, and.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 3 Memory Management Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
Linkage Editors Difference between a linkage editor and a linking loader: Linking loader performs all linking and relocation operations, including automatic.
3. Loaders & Linkers1 Chapter III: Loaders and Linkers Chapter goal: r To realize how a source program be loaded into memory m Loading m Relocation m Linking.
Chapter 3 Loaders and Linkers
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
SYSTEM PROGRAMMING & SYSTEM ADMINISTRATION
Mr. D. J. Patel, AITS, Rajkot 1 Operating Systems, by Dhananjay Dhamdhere1 Static and Dynamic Memory Allocation Memory allocation is an aspect of a more.
Linkers and Loaders 1 Linkers & Loaders – A Programmers Perspective.
Machine Independent Assembler Features
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
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++
Together, we can make difference The Theory, History and Future of System Linkers Luba Tang CEO & Founder, Skymizer Inc.
CPSC Compiler Tutorial 9 Review of Compiler.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Disk Storage, Basic File Structures, and Hashing by Pinar Senkul resources: mostly froom.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 13 Disk Storage, Basic File Structures, and Hashing.
Improving Code Generation Honors Compilers April 16 th 2002.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
Together, We make the difference. Introduction to LLVM and MCLinker Luba Tang.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 Disk Storage, Basic File Structures, and Hashing.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-7 Memory Management (1) Department of Computer Science and Software.
MIPS coding. SPIM Some links can be found such as:
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Together, We make the difference. Create a Working Linker with the MCLinker framework Luba Tang 2013/02/24.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Purpose  This training module provides an overview of optimization techniques used in.
1 Copyright © 2011, Elsevier Inc. All rights Reserved. Appendix A Authors: John Hennessy & David Patterson.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
2 : Assembler 1 Chapter II: Assembler Chapter goal: r Introduce the fundamental functions that any assembler must perform. m Assign machine address m Translate.
File Structures. 2 Chapter - Objectives Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and.
Static Shared Library. Non-shared v.s. Shared Library A library is a collection of pre-written function calls. Using existing libraries can save a programmer.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Loader and Linker.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Chapter 5 Record Storage and Primary File Organizations
CSc 453 Linking and Loading
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 –Linkers.
Lecture 3 Translation.
Assemblers, linkers, loaders
Advanced Computer Systems
Component 1.6.
Computer Science 210 Computer Organization
A Simple Syntax-Directed Translator
SOFTWARE DESIGN AND ARCHITECTURE
System Programming and administration
The University of Adelaide, School of Computer Science
Chapter 9 – Real Memory Organization and Management
-by Nisarg Vasavada (Compiled*)
Program Execution in Linux
Assembler Design Options
Computer Science 210 Computer Organization
Loaders and Linkers: Features
Computer Organization and Design Assembly & Compilation
The Assembly Language Level
Program Execution in Linux
Target Code Generation
Presentation transcript:

Together, We make the difference. MCLinker Intermediate Representation Luba Tang 2013/2/24

Agenda ▪ Introduction to Linkers and Loaders –Related works –The value of MCLinker Intermediate Representation ▪ MCLinker Intermediate Representation –Module and Input Tree –Fragments and Reference Graph ▪ MCLinker linking algorithm –Normalization –Resolution –Layout –Emit Luba Tang, software architect of MCLinker MediaTek, inc.

Together, We make the difference. Introduction to MCLinker Intermediate Representation ▪ MCLinker is the first *ELF linker to provide an intermediate representation (IR) for efficient transformation and analysis ▪ MCLinker provides IR on two levels –Linker Command Line Language –Fragment-Reference Graph ▪ Fragment is the basic linking unit, it can be –A section (coarse granularity) –A block of code or instructions (middle granularity) –An individual symbol and its code/data (fine granularity) ▪ MCLinker can trade linking time for the output quality. –The finer granularity, Fast, smaller program Longer link time * Nick Kledzik invents the Atom IR in ld64 for MachO. ld64 inspires MCLinker IRs

Together, We make the difference. The Linker Command Line Language ▪ Linker’s command line options is a kind of language –The meaning of a option depends on their positions the other potions –Some options have its own grammar ▪ Four categories of the options –Input files –Attributes of the input files –Linker script options –General options ▪ Examples ld /tmp/xxx.o –lpthread ld –as-needed./yyy.so ld –defsym=cgo13=0x224 ld –L/opt/lib –T./my.x

Together, We make the difference. The GNU ld Linker ▪ The GNU ld linker is an interpreter of the command line language –Processing is recursive. –No clear separation between individual steps –Binary File Descriptor (BFD) is the only IR

Together, We make the difference. The Google gold Linker ▪ The Google gold linker separates linking into two stages –Symbol resolution –Relocation of instructions and data ▪ Although it has separated the linking processes, it does not provide reusable IR for optimization and analysis ▪ The Google gold linker illustrates an efficient linking algorithm –It’s x2 faster than the GNU ld linker –Support multiple threads. Appropriate to cloud computing

Together, We make the difference. MCLinker ▪ MCLinker separates the linking into four distinct stages –Normalization – parse the command line language –Resolution – resolve symbols –Layout – relocate instructions and data –Emission – emit file by various formats ▪ MCLinker provides two level intermediate representation (IR) –The command line language level –The reference graph level

Together, We make the difference. Input Files on The Command Line ▪ An input file can be an object file, an archive, or a linker script ▪ Some input files can be defined multiple times ▪ The result of linking depends on the positions of inputs on the command line. –Weak symbols are first-come-first-served –COMDAT sections are first-come-first-served ▪ Two semantics to read input files –INPUT( file1, file2, file3,...) –GROUP( archive1, archive2, archive3,...) ▪ Archives in a group are searched repeatedly until no new undefined references are created $ ld a.o –start-group b.a c.a –end-group d.o e.o

Together, We make the difference. The Input File Tree ▪ We can represent the input files on the command line by a tree structure –Vertices describes input files and groups on the command line Object files Archives Linker scripts Entrances of groups ▪ Edges describe the relationships between vertices –Positional edges –Inclusive edges ▪ Linkers resolve symbols by DFS and merge sections by BFS ▪ Example $ ld a.o –start-group b.a c.a –end-group d.o e.o

Together, We make the difference. Attributes of Input Files ▪ Attributes change the way that a linker handles the input files ▪ Attributes affect the input files after the attribute options FunctionsOptionsMeanings Whole archives--whole-archiveIncludes every file in the archive Link against dynamic libraries -BdynamicSearch shared libraries for -l option As needed--as-neededOnly add the necessary shared libraries to resolve symbols Input format--format=The format of the following input files

Together, We make the difference. Attributes in The Input File Tree ▪ Every input has a set of attributes ▪ In the MCLinker implementation, we give every vertex a reference to its attribute set ▪ If two vertices have identical attributes, they can share a common attribute set. ▪ Example $ld./ a. o -- whole - archive -- start - group./ b. a./ c. a -- end - group -- no - who le - archive./ d. o./ e. o

Together, We make the difference. Normalization ▪ Transform the command line language into the input file tree –Parse command line options –Recognize input files to build up sub-trees –Merge all sub-trees to a form the input file tree

Together, We make the difference. Steps of Normalization ▪ Step of normalization 1.Parse the command line options 2.Recognize archives and linker scripts 3.Read the linker scripts and archives to create sub-trees 4.Merge all sub-trees ▪Example $ ld./ a. o./ b. a./ c. o

Together, We make the difference. Traverse the Input File Tree ▪ MCLinker provides different iterators for different purposes –For symbol resolution Depth first search for correctness –For section merging Breadth first search for cache locality of the output file

Together, We make the difference. Resolution ▪ Transform the input file tree into the reference graph –Resolves symbols –Reads relocation –Builds the reference graph

Together, We make the difference. Symbols and Relocations ▪ A fragment is a block of instruction code or data in a module –A fragment may be a function, a label (Basic block), a 32-bit integer data, and so on. ▪ A defined symbol indicates a fragment ▪ A relocation represents an use-define relationship between two fragments … 0x1, 0x2 = global i32 0 … Module X Module Y relocation use define

Together, We make the difference. Fragment-Reference Graph (1/2) ▪ A reference is a symbolic linkage between two fragments –A reference is an directed edge from use to define ▪ MCLinker represents the input modules as a graph structure –Vertices describe the fragments of modules –Edges describe the references between two fragments relocation usedefine symboldefine fragment use fragment a reference

Together, We make the difference. Fragment-Reference Graph (2/2) ▪ A Fragment-Reference Graph is a digraph, FRG = (V, E, S, O) –V is a set of fragments –E is a set of references, from use to define –S is a set of define symbols. They are the entrances of the graph –O is a set of exits and explains later. __start __global fragmen t edge

Together, We make the difference. Symbol Resolution ▪ Determine the topology of the reference graph –Relocation is a plug –Define symbol is a slot –Symbol resolution connects plugs and slots. ▪ Symbols has a set of attributes to help linkers determine the correct topology relocation use define symboldefine fragment use fragment Undefine symbol define symboldefine fragment define Which one?

Together, We make the difference. Optimizations on the Fragment-Reference Graph ▪ Fragment stripping –Remove unused fragment for shrink code size (Reachability problem) –Traditional linkers strip coarse sections. But MCLinker can strips finer-grained fragments. –The finer granularity, the smaller code size ▪ Branch optimization –Replace high cost branch by low cost branch –Optimizing by change of the relocation type ▪ Low-level inlining ▪ Fragment duplication for TLS optimization and copy relocations

Together, We make the difference. Layout ▪ To serialize the reference graph into a address space –Scan relocations –Layout –Apply relocations

Together, We make the difference. Exits of The Fragment-Reference Graph ▪ A Fragment-Reference Graph is a digraph, FRG = (V, E, S, O) –O is a set of exits. An exit represents a dynamic relocation to GOT. –Represent to access external variables or to call an external function exits the FRG ▪ If the defining fragment is in an external module, then MCLinker will add exits for the references to the outside module. –We have no way to know the memory address of the external module until the load time –We add the Global Offset Table (GOT) for the unknown addresses –We add dynamic relocations for all entries of the GOT –Loader will apply the dynamic relocations and set the correct address in the GOT. –The program use the GOT to accesses the external module indirectly __start __global GOT relocation use define relocation exit

Together, We make the difference. Layout ▪ Layout is a process to finalize the address of fragment and symbols –Sorts FRG=(V, E, S, O) topologically –Assigns addresses to {V, S, O} ▪ Before layout, we must calculate the sizes of all elements of the graph –Relocation scanning Reserve exits and calculate the sizes of all exits Undefined global symbol, GOT, and dynamic relocations –*Pre-layout Calculate the size of all fragments Calculate the size of all entrances –Global symbols and the hash table * MCLinker follows the Google gold linker’s naming. But pre-layout is opaque and may be renamed.

Together, We make the difference. Apply relocation (1/2) ▪ Adjusts the content of using fragments –Final addresses of symbol is known after layout –Correct use fragment by accessed address 0x1, 0x2 … … Symbol Table Module Y relocation use define 0x24

Together, We make the difference. Apply relocation (2/2) ▪ Replaces absolute addresses by PC-related offset if supported by the target ▪ Basic Relocation Formula S – P + A –S: the symbol value –P: the place of the use instruction –A: addend, adjustment (by the instruction format) … 0x1, 0x2 S S P P S - P A address space

Together, We make the difference. Optimizations on Layout ▪ Dynamic Prelinking – If the system puts shared libraries at a fixed memory location, we can fill GOT with fixed addresses to avoid symbol look up in the loader ▪ Static Prelinking –If the system puts shared libraries at a fixed memory location, we can directly refer to the fixed addresses without any exits ▪ Symbol Stripping –Strip the undefined symbols which is not a exit

Together, We make the difference. Emission ▪ Emits the module in the output formats –Adds format information –Writes down the IR ▪ In order to improve both cache and page locality, MCLinker collects and performs most file operations in this stage. –MCLinker copies the content in the inputs and applies the resolved reference in this stage.

Together, We make the difference. Conclusion ▪ We’ve introduced MCLinker intermediate representations –The Linker Command Line Language –The Fragment-Reference Graph ▪ We’ve indicated many optimization opportunities in MCLinker IRs.