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 //===----------------------------------------------------------------------===//
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/ObjectCodeEmitter.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetMachOWriterInfo.h"
31 class MachineRelocation;
32 class ObjectCodeEmitter;
33 class MachOCodeEmitter;
40 /// MachOWriter - This class implements the common target-independent code for
41 /// writing Mach-O files. Targets should derive a class from this to
42 /// parameterize the output format.
44 class MachOWriter : public MachineFunctionPass {
45 friend class MachOCodeEmitter;
49 ObjectCodeEmitter *getObjectCodeEmitter() {
50 return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
53 MachOWriter(raw_ostream &O, TargetMachine &TM);
54 virtual ~MachOWriter();
56 virtual const char *getPassName() const {
57 return "Mach-O Writer";
61 /// Output stream to send the resultant object file to.
65 /// Target machine description.
69 /// Mang - The object used to perform name mangling for this module.
73 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit machine
74 /// code for functions to the .o file.
76 MachOCodeEmitter *MachOCE;
78 /// is64Bit/isLittleEndian - This information is inferred from the target
79 /// machine directly, indicating what header values and flags to set.
81 bool is64Bit, isLittleEndian;
85 const TargetAsmInfo *TAI;
87 /// Header - An instance of MachOHeader that we will update while we build
88 /// the file, and then emit during finalization.
92 /// doInitialization - Emit the file header and all of the global variables
93 /// for the module to the Mach-O file.
95 bool doInitialization(Module &M);
97 bool runOnMachineFunction(MachineFunction &MF);
99 /// doFinalization - Now that the module has been completely processed, emit
100 /// the Mach-O file to 'O'.
102 bool doFinalization(Module &M);
106 /// SectionList - This is the list of sections that we have emitted to the
107 /// file. Once the file has been completely built, the segment load command
108 /// SectionCommands are constructed from this info.
110 std::vector<MachOSection*> SectionList;
112 /// SectionLookup - This is a mapping from section name to SectionList entry
114 std::map<std::string, MachOSection*> SectionLookup;
116 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
117 /// to aid in emitting relocations.
119 std::map<GlobalValue*, MachOSection*> GVSection;
121 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
122 /// start of the section in which the GV resides, to aid in emitting
125 std::map<GlobalValue*, intptr_t> GVOffset;
127 /// getSection - Return the section with the specified name, creating a new
128 /// section if one does not already exist.
130 MachOSection *getSection(const std::string &seg, const std::string §,
131 unsigned Flags = 0) {
132 MachOSection *MOS = SectionLookup[seg+sect];
135 MOS = new MachOSection(seg, sect);
136 SectionList.push_back(MOS);
137 MOS->Index = SectionList.size();
138 MOS->flags = MachOSection::S_REGULAR | Flags;
139 SectionLookup[seg+sect] = MOS;
142 MachOSection *getTextSection(bool isCode = true) {
144 return getSection("__TEXT", "__text",
145 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
146 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
148 return getSection("__TEXT", "__text");
150 MachOSection *getBSSSection() {
151 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
153 MachOSection *getDataSection() {
154 return getSection("__DATA", "__data");
156 MachOSection *getConstSection(Constant *C) {
157 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
158 if (CVA && CVA->isCString())
159 return getSection("__TEXT", "__cstring",
160 MachOSection::S_CSTRING_LITERALS);
162 const Type *Ty = C->getType();
163 if (Ty->isPrimitiveType() || Ty->isInteger()) {
164 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
166 default: break; // Fall through to __TEXT,__const
168 return getSection("__TEXT", "__literal4",
169 MachOSection::S_4BYTE_LITERALS);
171 return getSection("__TEXT", "__literal8",
172 MachOSection::S_8BYTE_LITERALS);
174 return getSection("__TEXT", "__literal16",
175 MachOSection::S_16BYTE_LITERALS);
178 return getSection("__TEXT", "__const");
180 MachOSection *getJumpTableSection() {
181 if (TM.getRelocationModel() == Reloc::PIC_)
182 return getTextSection(false);
184 return getSection("__TEXT", "__const");
187 /// MachOSymTab - This struct contains information about the offsets and
188 /// size of symbol table information.
191 uint32_t cmd; // LC_SYMTAB
192 uint32_t cmdsize; // sizeof( MachOSymTab )
193 uint32_t symoff; // symbol table offset
194 uint32_t nsyms; // number of symbol table entries
195 uint32_t stroff; // string table offset
196 uint32_t strsize; // string table size in bytes
198 // Constants for the cmd field
199 // see <mach-o/loader.h>
200 enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
203 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
204 nsyms(0), stroff(0), strsize(0) { }
207 /// SymTab - The "stab" style symbol table information
209 /// DySymTab - symbol table info for the dynamic link editor
210 MachODySymTab DySymTab;
214 /// SymbolTable - This is the list of symbols we have emitted to the file.
215 /// This actually gets rearranged before emission to the file (to put the
216 /// local symbols first in the list).
217 std::vector<MachOSym> SymbolTable;
219 /// SymT - A buffer to hold the symbol table before we write it out at the
220 /// appropriate location in the file.
223 /// StrT - A buffer to hold the string table before we write it out at the
224 /// appropriate location in the file.
227 /// PendingSyms - This is a list of externally defined symbols that we have
228 /// been asked to emit, but have not seen a reference to. When a reference
229 /// is seen, the symbol will move from this list to the SymbolTable.
230 std::vector<GlobalValue*> PendingGlobals;
232 /// DynamicSymbolTable - This is just a vector of indices into
233 /// SymbolTable to aid in emitting the DYSYMTAB load command.
234 std::vector<unsigned> DynamicSymbolTable;
236 static void InitMem(const Constant *C,
238 const TargetData *TD,
242 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
243 void EmitGlobal(GlobalVariable *GV);
244 void EmitHeaderAndLoadCommands();
246 void EmitRelocations();
247 void BufferSymbolAndStringTable();
248 void CalculateRelocations(MachOSection &MOS);
250 MachineRelocation GetJTRelocation(unsigned Offset,
251 MachineBasicBlock *MBB) const {
252 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
255 /// GetTargetRelocation - Returns the number of relocations.
256 unsigned GetTargetRelocation(MachineRelocation &MR,
260 OutputBuffer &RelocOut,
261 OutputBuffer &SecOut,
264 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,