1 //=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOWriter class.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineFunctionPass.h"
25 class MachineBasicBlock
;
26 class MachineRelocation
;
27 class MachOCodeEmitter
;
35 class ObjectCodeEmitter
;
39 /// MachOWriter - This class implements the common target-independent code for
40 /// writing Mach-O files. Targets should derive a class from this to
41 /// parameterize the output format.
43 class MachOWriter
: public MachineFunctionPass
{
44 friend class MachOCodeEmitter
;
48 ObjectCodeEmitter
*getObjectCodeEmitter() {
49 return reinterpret_cast<ObjectCodeEmitter
*>(MachOCE
);
52 MachOWriter(raw_ostream
&O
, TargetMachine
&TM
);
53 virtual ~MachOWriter();
55 virtual const char *getPassName() const {
56 return "Mach-O Writer";
60 /// Output stream to send the resultant object file to.
64 /// Target machine description.
68 /// Mang - The object used to perform name mangling for this module.
72 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit
73 /// machine code for functions to the .o file.
74 MachOCodeEmitter
*MachOCE
;
76 /// is64Bit/isLittleEndian - This information is inferred from the target
77 /// machine directly, indicating what header values and flags to set.
78 bool is64Bit
, isLittleEndian
;
83 /// Header - An instance of MachOHeader that we will update while we build
84 /// the file, and then emit during finalization.
87 /// doInitialization - Emit the file header and all of the global variables
88 /// for the module to the Mach-O file.
89 bool doInitialization(Module
&M
);
91 bool runOnMachineFunction(MachineFunction
&MF
);
93 /// doFinalization - Now that the module has been completely processed, emit
94 /// the Mach-O file to 'O'.
95 bool doFinalization(Module
&M
);
99 /// SectionList - This is the list of sections that we have emitted to the
100 /// file. Once the file has been completely built, the segment load command
101 /// SectionCommands are constructed from this info.
102 std::vector
<MachOSection
*> SectionList
;
104 /// SectionLookup - This is a mapping from section name to SectionList entry
105 std::map
<std::string
, MachOSection
*> SectionLookup
;
107 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
108 /// to aid in emitting relocations.
109 std::map
<GlobalValue
*, MachOSection
*> GVSection
;
111 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
112 /// start of the section in which the GV resides, to aid in emitting
114 std::map
<GlobalValue
*, intptr_t> GVOffset
;
116 /// getSection - Return the section with the specified name, creating a new
117 /// section if one does not already exist.
118 MachOSection
*getSection(const std::string
&seg
, const std::string
§
,
121 /// getTextSection - Return text section with different flags for code/data
122 MachOSection
*getTextSection(bool isCode
= true);
124 MachOSection
*getDataSection() {
125 return getSection("__DATA", "__data");
128 MachOSection
*getBSSSection();
129 MachOSection
*getConstSection(Constant
*C
);
130 MachOSection
*getJumpTableSection();
132 /// MachOSymTab - This struct contains information about the offsets and
133 /// size of symbol table information.
136 uint32_t cmd
; // LC_SYMTAB
137 uint32_t cmdsize
; // sizeof( MachOSymTab )
138 uint32_t symoff
; // symbol table offset
139 uint32_t nsyms
; // number of symbol table entries
140 uint32_t stroff
; // string table offset
141 uint32_t strsize
; // string table size in bytes
143 // Constants for the cmd field
144 // see <mach-o/loader.h>
145 enum { LC_SYMTAB
= 0x02 // link-edit stab symbol table info
148 MachOSymTab() : cmd(LC_SYMTAB
), cmdsize(6 * sizeof(uint32_t)), symoff(0),
149 nsyms(0), stroff(0), strsize(0) { }
152 /// SymTab - The "stab" style symbol table information
154 /// DySymTab - symbol table info for the dynamic link editor
155 MachODySymTab DySymTab
;
159 /// SymbolTable - This is the list of symbols we have emitted to the file.
160 /// This actually gets rearranged before emission to the file (to put the
161 /// local symbols first in the list).
162 std::vector
<MachOSym
> SymbolTable
;
164 /// SymT - A buffer to hold the symbol table before we write it out at the
165 /// appropriate location in the file.
166 std::vector
<unsigned char> SymT
;
168 /// StrT - A buffer to hold the string table before we write it out at the
169 /// appropriate location in the file.
170 std::vector
<unsigned char> StrT
;
172 /// PendingSyms - This is a list of externally defined symbols that we have
173 /// been asked to emit, but have not seen a reference to. When a reference
174 /// is seen, the symbol will move from this list to the SymbolTable.
175 std::vector
<GlobalValue
*> PendingGlobals
;
177 /// DynamicSymbolTable - This is just a vector of indices into
178 /// SymbolTable to aid in emitting the DYSYMTAB load command.
179 std::vector
<unsigned> DynamicSymbolTable
;
181 static void InitMem(const Constant
*C
, uintptr_t Offset
,
182 const TargetData
*TD
, MachOSection
* mos
);
185 void AddSymbolToSection(MachOSection
*MOS
, GlobalVariable
*GV
);
186 void EmitGlobal(GlobalVariable
*GV
);
187 void EmitHeaderAndLoadCommands();
189 void EmitRelocations();
190 void BufferSymbolAndStringTable();
191 void CalculateRelocations(MachOSection
&MOS
);
193 // GetJTRelocation - Get a relocation a new BB relocation based
194 // on target information.
195 MachineRelocation
GetJTRelocation(unsigned Offset
,
196 MachineBasicBlock
*MBB
) const;
198 /// GetTargetRelocation - Returns the number of relocations.
199 unsigned GetTargetRelocation(MachineRelocation
&MR
, unsigned FromIdx
,
200 unsigned ToAddr
, unsigned ToIndex
,
201 OutputBuffer
&RelocOut
, OutputBuffer
&SecOut
,
202 bool Scattered
, bool Extern
);