blob: 3af2b0af4b78f924938cec0d75429bb5aa452848 [file] [log] [blame]
Nate Begemaneb883af2006-08-23 21:08:52 +00001//=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begemaneb883af2006-08-23 21:08:52 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the MachOWriter class.
11//
12//===----------------------------------------------------------------------===//
13
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000014#ifndef MACHOWRITER_H
15#define MACHOWRITER_H
Nate Begemaneb883af2006-08-23 21:08:52 +000016
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000017#include "MachO.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Nate Begemanf8f2c5a2006-08-25 06:36:58 +000019#include "llvm/Target/TargetData.h"
20#include "llvm/Target/TargetMachine.h"
Bill Wendling40fab402007-01-24 03:37:18 +000021#include "llvm/Target/TargetMachOWriterInfo.h"
Dan Gohmanc9235d22008-03-21 23:51:57 +000022#include <map>
Nate Begemaneb883af2006-08-23 21:08:52 +000023
24namespace llvm {
25 class GlobalVariable;
26 class Mangler;
27 class MachineCodeEmitter;
28 class MachOCodeEmitter;
Bill Wendling0f43b222007-02-03 02:37:51 +000029 class OutputBuffer;
Owen Andersoncb371882008-08-21 00:14:44 +000030 class raw_ostream;
Nate Begemaneb883af2006-08-23 21:08:52 +000031
Nate Begeman94be2482006-09-08 22:42:09 +000032
Nate Begemaneb883af2006-08-23 21:08:52 +000033 /// MachOWriter - This class implements the common target-independent code for
34 /// writing Mach-O files. Targets should derive a class from this to
35 /// parameterize the output format.
36 ///
37 class MachOWriter : public MachineFunctionPass {
38 friend class MachOCodeEmitter;
39 public:
Devang Patel19974732007-05-03 01:11:54 +000040 static char ID;
Nate Begemaneb883af2006-08-23 21:08:52 +000041 MachineCodeEmitter &getMachineCodeEmitter() const {
42 return *(MachineCodeEmitter*)MCE;
43 }
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000044
Owen Andersoncb371882008-08-21 00:14:44 +000045 MachOWriter(raw_ostream &O, TargetMachine &TM);
Bill Wendling2b721822007-01-24 07:13:56 +000046 virtual ~MachOWriter();
Nate Begemaneb883af2006-08-23 21:08:52 +000047
Bill Wendling2b721822007-01-24 07:13:56 +000048 virtual const char *getPassName() const {
49 return "Mach-O Writer";
50 }
Nate Begemaneb883af2006-08-23 21:08:52 +000051
Nate Begemaneb883af2006-08-23 21:08:52 +000052 protected:
Nate Begemaneb883af2006-08-23 21:08:52 +000053 /// Output stream to send the resultant object file to.
54 ///
Owen Andersoncb371882008-08-21 00:14:44 +000055 raw_ostream &O;
Nate Begemaneb883af2006-08-23 21:08:52 +000056
57 /// Target machine description.
58 ///
59 TargetMachine &TM;
60
61 /// Mang - The object used to perform name mangling for this module.
62 ///
63 Mangler *Mang;
Nate Begeman94be2482006-09-08 22:42:09 +000064
Nate Begemaneb883af2006-08-23 21:08:52 +000065 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
66 /// code for functions to the .o file.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000067
Nate Begemaneb883af2006-08-23 21:08:52 +000068 MachOCodeEmitter *MCE;
69
70 /// is64Bit/isLittleEndian - This information is inferred from the target
71 /// machine directly, indicating what header values and flags to set.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000072
Nate Begemaneb883af2006-08-23 21:08:52 +000073 bool is64Bit, isLittleEndian;
74
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000075 // Target Asm Info
76
77 const TargetAsmInfo *TAI;
78
79 /// Header - An instance of MachOHeader that we will update while we build
80 /// the file, and then emit during finalization.
81
82 MachOHeader Header;
83
Nate Begemaneb883af2006-08-23 21:08:52 +000084 /// doInitialization - Emit the file header and all of the global variables
85 /// for the module to the Mach-O file.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000086
Nate Begemaneb883af2006-08-23 21:08:52 +000087 bool doInitialization(Module &M);
88
89 bool runOnMachineFunction(MachineFunction &MF);
90
91 /// doFinalization - Now that the module has been completely processed, emit
92 /// the Mach-O file to 'O'.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000093
Nate Begemaneb883af2006-08-23 21:08:52 +000094 bool doFinalization(Module &M);
95
Nate Begemand2030e62006-08-26 15:46:34 +000096 private:
97
Nate Begemaneb883af2006-08-23 21:08:52 +000098 /// 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.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000101
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000102 std::vector<MachOSection*> SectionList;
Nate Begemaneb883af2006-08-23 21:08:52 +0000103
104 /// SectionLookup - This is a mapping from section name to SectionList entry
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000105
Nate Begemaneb883af2006-08-23 21:08:52 +0000106 std::map<std::string, MachOSection*> SectionLookup;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000107
108 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
109 /// to aid in emitting relocations.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000110
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000111 std::map<GlobalValue*, MachOSection*> GVSection;
112
113 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
114 /// start of the section in which the GV resides, to aid in emitting
115 /// relocations.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000116
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000117 std::map<GlobalValue*, intptr_t> GVOffset;
Nate Begemaneb883af2006-08-23 21:08:52 +0000118
119 /// getSection - Return the section with the specified name, creating a new
120 /// section if one does not already exist.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000121
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000122 MachOSection *getSection(const std::string &seg, const std::string &sect,
Nate Begemaneb883af2006-08-23 21:08:52 +0000123 unsigned Flags = 0) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000124 MachOSection *MOS = SectionLookup[seg+sect];
125 if (MOS) return MOS;
Nate Begemaneb883af2006-08-23 21:08:52 +0000126
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000127 MOS = new MachOSection(seg, sect);
128 SectionList.push_back(MOS);
129 MOS->Index = SectionList.size();
130 MOS->flags = MachOSection::S_REGULAR | Flags;
131 SectionLookup[seg+sect] = MOS;
132 return MOS;
Nate Begemaneb883af2006-08-23 21:08:52 +0000133 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000134 MachOSection *getTextSection(bool isCode = true) {
Nate Begeman019f8512006-09-10 23:03:44 +0000135 if (isCode)
136 return getSection("__TEXT", "__text",
137 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
138 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
139 else
140 return getSection("__TEXT", "__text");
Nate Begemaneb883af2006-08-23 21:08:52 +0000141 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000142 MachOSection *getBSSSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000143 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
144 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000145 MachOSection *getDataSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000146 return getSection("__DATA", "__data");
147 }
Nate Begeman1257c852007-01-29 21:20:42 +0000148 MachOSection *getConstSection(Constant *C) {
149 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
150 if (CVA && CVA->isCString())
151 return getSection("__TEXT", "__cstring",
152 MachOSection::S_CSTRING_LITERALS);
153
154 const Type *Ty = C->getType();
Chris Lattner42a75512007-01-15 02:27:26 +0000155 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Duncan Sands777d2302009-05-09 07:06:46 +0000156 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000157 switch(Size) {
158 default: break; // Fall through to __TEXT,__const
159 case 4:
160 return getSection("__TEXT", "__literal4",
161 MachOSection::S_4BYTE_LITERALS);
162 case 8:
163 return getSection("__TEXT", "__literal8",
164 MachOSection::S_8BYTE_LITERALS);
165 case 16:
166 return getSection("__TEXT", "__literal16",
167 MachOSection::S_16BYTE_LITERALS);
168 }
169 }
170 return getSection("__TEXT", "__const");
171 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000172 MachOSection *getJumpTableSection() {
Nate Begeman019f8512006-09-10 23:03:44 +0000173 if (TM.getRelocationModel() == Reloc::PIC_)
174 return getTextSection(false);
175 else
176 return getSection("__TEXT", "__const");
177 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000178
179 /// MachOSymTab - This struct contains information about the offsets and
180 /// size of symbol table information.
181 /// segment.
182 struct MachOSymTab {
183 uint32_t cmd; // LC_SYMTAB
184 uint32_t cmdsize; // sizeof( MachOSymTab )
185 uint32_t symoff; // symbol table offset
186 uint32_t nsyms; // number of symbol table entries
187 uint32_t stroff; // string table offset
188 uint32_t strsize; // string table size in bytes
189
190 // Constants for the cmd field
191 // see <mach-o/loader.h>
192 enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
193 };
194
195 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
196 nsyms(0), stroff(0), strsize(0) { }
197 };
198
Nate Begemaneb883af2006-08-23 21:08:52 +0000199 /// SymTab - The "stab" style symbol table information
200 MachOSymTab SymTab;
201 /// DySymTab - symbol table info for the dynamic link editor
202 MachODySymTab DySymTab;
203
Nate Begeman94be2482006-09-08 22:42:09 +0000204 protected:
205
Nate Begemaneb883af2006-08-23 21:08:52 +0000206 /// SymbolTable - This is the list of symbols we have emitted to the file.
207 /// This actually gets rearranged before emission to the file (to put the
208 /// local symbols first in the list).
209 std::vector<MachOSym> SymbolTable;
210
Nate Begemand2030e62006-08-26 15:46:34 +0000211 /// SymT - A buffer to hold the symbol table before we write it out at the
212 /// appropriate location in the file.
213 DataBuffer SymT;
214
215 /// StrT - A buffer to hold the string table before we write it out at the
216 /// appropriate location in the file.
217 DataBuffer StrT;
218
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000219 /// PendingSyms - This is a list of externally defined symbols that we have
220 /// been asked to emit, but have not seen a reference to. When a reference
221 /// is seen, the symbol will move from this list to the SymbolTable.
Nate Begemanfec910c2007-02-28 07:40:50 +0000222 std::vector<GlobalValue*> PendingGlobals;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000223
Nate Begemaneb883af2006-08-23 21:08:52 +0000224 /// DynamicSymbolTable - This is just a vector of indices into
225 /// SymbolTable to aid in emitting the DYSYMTAB load command.
226 std::vector<unsigned> DynamicSymbolTable;
227
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000228 static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
229 const TargetData *TD,
230 std::vector<MachineRelocation> &MRs);
Nate Begeman019f8512006-09-10 23:03:44 +0000231
Nate Begemaneb883af2006-08-23 21:08:52 +0000232 private:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000233 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
Nate Begemaneb883af2006-08-23 21:08:52 +0000234 void EmitGlobal(GlobalVariable *GV);
235 void EmitHeaderAndLoadCommands();
236 void EmitSections();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000237 void EmitRelocations();
Nate Begemand2030e62006-08-26 15:46:34 +0000238 void BufferSymbolAndStringTable();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000239 void CalculateRelocations(MachOSection &MOS);
Nate Begeman94be2482006-09-08 22:42:09 +0000240
Bill Wendling40fab402007-01-24 03:37:18 +0000241 MachineRelocation GetJTRelocation(unsigned Offset,
242 MachineBasicBlock *MBB) const {
243 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
244 }
Bill Wendling0f43b222007-02-03 02:37:51 +0000245
246 /// GetTargetRelocation - Returns the number of relocations.
247 unsigned GetTargetRelocation(MachineRelocation &MR,
248 unsigned FromIdx,
249 unsigned ToAddr,
250 unsigned ToIndex,
251 OutputBuffer &RelocOut,
252 OutputBuffer &SecOut,
Nate Begemanfec910c2007-02-28 07:40:50 +0000253 bool Scattered,
254 bool Extern) {
Bill Wendling0f43b222007-02-03 02:37:51 +0000255 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
256 ToIndex, RelocOut,
Nate Begemanfec910c2007-02-28 07:40:50 +0000257 SecOut, Scattered,
258 Extern);
Bill Wendling0f43b222007-02-03 02:37:51 +0000259 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000260 };
261}
262
263#endif