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 MachineRelocation
;
26 class MachOCodeEmitter
;
34 class ObjectCodeEmitter
;
38 /// MachOWriter - This class implements the common target-independent code for
39 /// writing Mach-O files. Targets should derive a class from this to
40 /// parameterize the output format.
42 class MachOWriter
: public MachineFunctionPass
{
43 friend class MachOCodeEmitter
;
47 ObjectCodeEmitter
*getObjectCodeEmitter() {
48 return reinterpret_cast<ObjectCodeEmitter
*>(MachOCE
);
51 MachOWriter(raw_ostream
&O
, TargetMachine
&TM
);
52 virtual ~MachOWriter();
54 virtual const char *getPassName() const {
55 return "Mach-O Writer";
59 /// Output stream to send the resultant object file to.
63 /// Target machine description.
67 /// Mang - The object used to perform name mangling for this module.
71 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit
72 /// machine code for functions to the .o file.
73 MachOCodeEmitter
*MachOCE
;
75 /// is64Bit/isLittleEndian - This information is inferred from the target
76 /// machine directly, indicating what header values and flags to set.
77 bool is64Bit
, isLittleEndian
;
80 const TargetAsmInfo
*TAI
;
82 /// Header - An instance of MachOHeader that we will update while we build
83 /// the file, and then emit during finalization.
86 /// doInitialization - Emit the file header and all of the global variables
87 /// for the module to the Mach-O file.
88 bool doInitialization(Module
&M
);
90 bool runOnMachineFunction(MachineFunction
&MF
);
92 /// doFinalization - Now that the module has been completely processed, emit
93 /// the Mach-O file to 'O'.
94 bool doFinalization(Module
&M
);
98 /// SectionList - This is the list of sections that we have emitted to the
99 /// file. Once the file has been completely built, the segment load command
100 /// SectionCommands are constructed from this info.
101 std::vector
<MachOSection
*> SectionList
;
103 /// SectionLookup - This is a mapping from section name to SectionList entry
104 std::map
<std::string
, MachOSection
*> SectionLookup
;
106 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
107 /// to aid in emitting relocations.
108 std::map
<GlobalValue
*, MachOSection
*> GVSection
;
110 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
111 /// start of the section in which the GV resides, to aid in emitting
113 std::map
<GlobalValue
*, intptr_t> GVOffset
;
115 /// getSection - Return the section with the specified name, creating a new
116 /// section if one does not already exist.
117 MachOSection
*getSection(const std::string
&seg
, const std::string
§
,
120 /// getTextSection - Return text section with different flags for code/data
121 MachOSection
*getTextSection(bool isCode
= true);
123 MachOSection
*getDataSection() {
124 return getSection("__DATA", "__data");
127 MachOSection
*getBSSSection();
128 MachOSection
*getConstSection(Constant
*C
);
129 MachOSection
*getJumpTableSection();
131 /// MachOSymTab - This struct contains information about the offsets and
132 /// size of symbol table information.
135 uint32_t cmd
; // LC_SYMTAB
136 uint32_t cmdsize
; // sizeof( MachOSymTab )
137 uint32_t symoff
; // symbol table offset
138 uint32_t nsyms
; // number of symbol table entries
139 uint32_t stroff
; // string table offset
140 uint32_t strsize
; // string table size in bytes
142 // Constants for the cmd field
143 // see <mach-o/loader.h>
144 enum { LC_SYMTAB
= 0x02 // link-edit stab symbol table info
147 MachOSymTab() : cmd(LC_SYMTAB
), cmdsize(6 * sizeof(uint32_t)), symoff(0),
148 nsyms(0), stroff(0), strsize(0) { }
151 /// SymTab - The "stab" style symbol table information
153 /// DySymTab - symbol table info for the dynamic link editor
154 MachODySymTab DySymTab
;
158 /// SymbolTable - This is the list of symbols we have emitted to the file.
159 /// This actually gets rearranged before emission to the file (to put the
160 /// local symbols first in the list).
161 std::vector
<MachOSym
> SymbolTable
;
163 /// SymT - A buffer to hold the symbol table before we write it out at the
164 /// appropriate location in the file.
165 std::vector
<unsigned char> SymT
;
167 /// StrT - A buffer to hold the string table before we write it out at the
168 /// appropriate location in the file.
169 std::vector
<unsigned char> StrT
;
171 /// PendingSyms - This is a list of externally defined symbols that we have
172 /// been asked to emit, but have not seen a reference to. When a reference
173 /// is seen, the symbol will move from this list to the SymbolTable.
174 std::vector
<GlobalValue
*> PendingGlobals
;
176 /// DynamicSymbolTable - This is just a vector of indices into
177 /// SymbolTable to aid in emitting the DYSYMTAB load command.
178 std::vector
<unsigned> DynamicSymbolTable
;
180 static void InitMem(const Constant
*C
, uintptr_t Offset
,
181 const TargetData
*TD
, MachOSection
* mos
);
184 void AddSymbolToSection(MachOSection
*MOS
, GlobalVariable
*GV
);
185 void EmitGlobal(GlobalVariable
*GV
);
186 void EmitHeaderAndLoadCommands();
188 void EmitRelocations();
189 void BufferSymbolAndStringTable();
190 void CalculateRelocations(MachOSection
&MOS
);
192 // GetJTRelocation - Get a relocation a new BB relocation based
193 // on target information.
194 MachineRelocation
GetJTRelocation(unsigned Offset
,
195 MachineBasicBlock
*MBB
) const;
197 /// GetTargetRelocation - Returns the number of relocations.
198 unsigned GetTargetRelocation(MachineRelocation
&MR
, unsigned FromIdx
,
199 unsigned ToAddr
, unsigned ToIndex
,
200 OutputBuffer
&RelocOut
, OutputBuffer
&SecOut
,
201 bool Scattered
, bool Extern
);