blob: 9f5b7c5ad31b17374fa05c2cc96b42112f31efe1 [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"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000020#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000021#include "llvm/CodeGen/ObjectCodeEmitter.h"
Nate Begemanf8f2c5a2006-08-25 06:36:58 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
Bill Wendling40fab402007-01-24 03:37:18 +000024#include "llvm/Target/TargetMachOWriterInfo.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000025#include <vector>
Dan Gohmanc9235d22008-03-21 23:51:57 +000026#include <map>
Nate Begemaneb883af2006-08-23 21:08:52 +000027
28namespace llvm {
29 class GlobalVariable;
30 class Mangler;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000031 class MachineRelocation;
32 class ObjectCodeEmitter;
Nate Begemaneb883af2006-08-23 21:08:52 +000033 class MachOCodeEmitter;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000034 class TargetData;
35 class TargetMachine;
Bill Wendling0f43b222007-02-03 02:37:51 +000036 class OutputBuffer;
Owen Andersoncb371882008-08-21 00:14:44 +000037 class raw_ostream;
Nate Begemaneb883af2006-08-23 21:08:52 +000038
Nate Begeman94be2482006-09-08 22:42:09 +000039
Nate Begemaneb883af2006-08-23 21:08:52 +000040 /// 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.
43 ///
44 class MachOWriter : public MachineFunctionPass {
45 friend class MachOCodeEmitter;
46 public:
Devang Patel19974732007-05-03 01:11:54 +000047 static char ID;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000048
49 ObjectCodeEmitter *getObjectCodeEmitter() {
50 return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
Nate Begemaneb883af2006-08-23 21:08:52 +000051 }
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000052
Owen Andersoncb371882008-08-21 00:14:44 +000053 MachOWriter(raw_ostream &O, TargetMachine &TM);
Bill Wendling2b721822007-01-24 07:13:56 +000054 virtual ~MachOWriter();
Nate Begemaneb883af2006-08-23 21:08:52 +000055
Bill Wendling2b721822007-01-24 07:13:56 +000056 virtual const char *getPassName() const {
57 return "Mach-O Writer";
58 }
Nate Begemaneb883af2006-08-23 21:08:52 +000059
Nate Begemaneb883af2006-08-23 21:08:52 +000060 protected:
Nate Begemaneb883af2006-08-23 21:08:52 +000061 /// Output stream to send the resultant object file to.
62 ///
Owen Andersoncb371882008-08-21 00:14:44 +000063 raw_ostream &O;
Nate Begemaneb883af2006-08-23 21:08:52 +000064
65 /// Target machine description.
66 ///
67 TargetMachine &TM;
68
69 /// Mang - The object used to perform name mangling for this module.
70 ///
71 Mangler *Mang;
Nate Begeman94be2482006-09-08 22:42:09 +000072
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000073 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit machine
Nate Begemaneb883af2006-08-23 21:08:52 +000074 /// code for functions to the .o file.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000075
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000076 MachOCodeEmitter *MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000077
78 /// is64Bit/isLittleEndian - This information is inferred from the target
79 /// machine directly, indicating what header values and flags to set.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000080
Nate Begemaneb883af2006-08-23 21:08:52 +000081 bool is64Bit, isLittleEndian;
82
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000083 // Target Asm Info
84
85 const TargetAsmInfo *TAI;
86
87 /// Header - An instance of MachOHeader that we will update while we build
88 /// the file, and then emit during finalization.
89
90 MachOHeader Header;
91
Nate Begemaneb883af2006-08-23 21:08:52 +000092 /// doInitialization - Emit the file header and all of the global variables
93 /// for the module to the Mach-O file.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000094
Nate Begemaneb883af2006-08-23 21:08:52 +000095 bool doInitialization(Module &M);
96
97 bool runOnMachineFunction(MachineFunction &MF);
98
99 /// doFinalization - Now that the module has been completely processed, emit
100 /// the Mach-O file to 'O'.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000101
Nate Begemaneb883af2006-08-23 21:08:52 +0000102 bool doFinalization(Module &M);
103
Nate Begemand2030e62006-08-26 15:46:34 +0000104 private:
105
Nate Begemaneb883af2006-08-23 21:08:52 +0000106 /// 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.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000109
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000110 std::vector<MachOSection*> SectionList;
Nate Begemaneb883af2006-08-23 21:08:52 +0000111
112 /// SectionLookup - This is a mapping from section name to SectionList entry
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000113
Nate Begemaneb883af2006-08-23 21:08:52 +0000114 std::map<std::string, MachOSection*> SectionLookup;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000115
116 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
117 /// to aid in emitting relocations.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000118
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000119 std::map<GlobalValue*, MachOSection*> GVSection;
120
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
123 /// relocations.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000124
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000125 std::map<GlobalValue*, intptr_t> GVOffset;
Nate Begemaneb883af2006-08-23 21:08:52 +0000126
127 /// getSection - Return the section with the specified name, creating a new
128 /// section if one does not already exist.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000129
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000130 MachOSection *getSection(const std::string &seg, const std::string &sect,
Nate Begemaneb883af2006-08-23 21:08:52 +0000131 unsigned Flags = 0) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000132 MachOSection *MOS = SectionLookup[seg+sect];
133 if (MOS) return MOS;
Nate Begemaneb883af2006-08-23 21:08:52 +0000134
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000135 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;
140 return MOS;
Nate Begemaneb883af2006-08-23 21:08:52 +0000141 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000142 MachOSection *getTextSection(bool isCode = true) {
Nate Begeman019f8512006-09-10 23:03:44 +0000143 if (isCode)
144 return getSection("__TEXT", "__text",
145 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
146 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
147 else
148 return getSection("__TEXT", "__text");
Nate Begemaneb883af2006-08-23 21:08:52 +0000149 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000150 MachOSection *getBSSSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000151 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
152 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000153 MachOSection *getDataSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000154 return getSection("__DATA", "__data");
155 }
Nate Begeman1257c852007-01-29 21:20:42 +0000156 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);
161
162 const Type *Ty = C->getType();
Chris Lattner42a75512007-01-15 02:27:26 +0000163 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Duncan Sands777d2302009-05-09 07:06:46 +0000164 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000165 switch(Size) {
166 default: break; // Fall through to __TEXT,__const
167 case 4:
168 return getSection("__TEXT", "__literal4",
169 MachOSection::S_4BYTE_LITERALS);
170 case 8:
171 return getSection("__TEXT", "__literal8",
172 MachOSection::S_8BYTE_LITERALS);
173 case 16:
174 return getSection("__TEXT", "__literal16",
175 MachOSection::S_16BYTE_LITERALS);
176 }
177 }
178 return getSection("__TEXT", "__const");
179 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000180 MachOSection *getJumpTableSection() {
Nate Begeman019f8512006-09-10 23:03:44 +0000181 if (TM.getRelocationModel() == Reloc::PIC_)
182 return getTextSection(false);
183 else
184 return getSection("__TEXT", "__const");
185 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000186
187 /// MachOSymTab - This struct contains information about the offsets and
188 /// size of symbol table information.
189 /// segment.
190 struct MachOSymTab {
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
197
198 // Constants for the cmd field
199 // see <mach-o/loader.h>
200 enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
201 };
202
203 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
204 nsyms(0), stroff(0), strsize(0) { }
205 };
206
Nate Begemaneb883af2006-08-23 21:08:52 +0000207 /// SymTab - The "stab" style symbol table information
208 MachOSymTab SymTab;
209 /// DySymTab - symbol table info for the dynamic link editor
210 MachODySymTab DySymTab;
211
Nate Begeman94be2482006-09-08 22:42:09 +0000212 protected:
213
Nate Begemaneb883af2006-08-23 21:08:52 +0000214 /// 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;
218
Nate Begemand2030e62006-08-26 15:46:34 +0000219 /// SymT - A buffer to hold the symbol table before we write it out at the
220 /// appropriate location in the file.
221 DataBuffer SymT;
222
223 /// StrT - A buffer to hold the string table before we write it out at the
224 /// appropriate location in the file.
225 DataBuffer StrT;
226
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000227 /// 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.
Nate Begemanfec910c2007-02-28 07:40:50 +0000230 std::vector<GlobalValue*> PendingGlobals;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000231
Nate Begemaneb883af2006-08-23 21:08:52 +0000232 /// DynamicSymbolTable - This is just a vector of indices into
233 /// SymbolTable to aid in emitting the DYSYMTAB load command.
234 std::vector<unsigned> DynamicSymbolTable;
235
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000236 static void InitMem(const Constant *C,
237 uintptr_t Offset,
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000238 const TargetData *TD,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000239 MachOSection* mos);
Nate Begeman019f8512006-09-10 23:03:44 +0000240
Nate Begemaneb883af2006-08-23 21:08:52 +0000241 private:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000242 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
Nate Begemaneb883af2006-08-23 21:08:52 +0000243 void EmitGlobal(GlobalVariable *GV);
244 void EmitHeaderAndLoadCommands();
245 void EmitSections();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000246 void EmitRelocations();
Nate Begemand2030e62006-08-26 15:46:34 +0000247 void BufferSymbolAndStringTable();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000248 void CalculateRelocations(MachOSection &MOS);
Nate Begeman94be2482006-09-08 22:42:09 +0000249
Bill Wendling40fab402007-01-24 03:37:18 +0000250 MachineRelocation GetJTRelocation(unsigned Offset,
251 MachineBasicBlock *MBB) const {
252 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
253 }
Bill Wendling0f43b222007-02-03 02:37:51 +0000254
255 /// GetTargetRelocation - Returns the number of relocations.
256 unsigned GetTargetRelocation(MachineRelocation &MR,
257 unsigned FromIdx,
258 unsigned ToAddr,
259 unsigned ToIndex,
260 OutputBuffer &RelocOut,
261 OutputBuffer &SecOut,
Nate Begemanfec910c2007-02-28 07:40:50 +0000262 bool Scattered,
263 bool Extern) {
Bill Wendling0f43b222007-02-03 02:37:51 +0000264 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
265 ToIndex, RelocOut,
Nate Begemanfec910c2007-02-28 07:40:50 +0000266 SecOut, Scattered,
267 Extern);
Bill Wendling0f43b222007-02-03 02:37:51 +0000268 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000269 };
270}
271
272#endif