Push LLVMContexts through the IntegerType APIs.
[oota-llvm.git] / lib / CodeGen / MachOWriter.h
1 //=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MachOWriter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef MACHOWRITER_H
15 #define MACHOWRITER_H
16
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include <vector>
19 #include <map>
20
21 namespace llvm {
22   class Constant;
23   class GlobalVariable;
24   class Mangler;
25   class MachineRelocation;
26   class MachOCodeEmitter;
27   struct MachODySymTab;
28   struct MachOHeader;
29   struct MachOSection;
30   struct MachOSym;
31   class TargetData;
32   class TargetMachine;
33   class TargetAsmInfo;
34   class ObjectCodeEmitter;
35   class OutputBuffer;
36   class raw_ostream;
37
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.
41   ///
42   class MachOWriter : public MachineFunctionPass {
43     friend class MachOCodeEmitter;
44   public:
45     static char ID;
46
47     ObjectCodeEmitter *getObjectCodeEmitter() {
48       return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
49     }
50
51     MachOWriter(raw_ostream &O, TargetMachine &TM);
52     virtual ~MachOWriter();
53
54     virtual const char *getPassName() const {
55       return "Mach-O Writer";
56     }
57
58   protected:
59     /// Output stream to send the resultant object file to.
60     ///
61     raw_ostream &O;
62
63     /// Target machine description.
64     ///
65     TargetMachine &TM;
66
67     /// Mang - The object used to perform name mangling for this module.
68     ///
69     Mangler *Mang;
70
71     /// MachOCE - The MachineCodeEmitter object that we are exposing to emit
72     /// machine code for functions to the .o file.
73     MachOCodeEmitter *MachOCE;
74
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;
78
79     // Target Asm Info
80     const TargetAsmInfo *TAI;
81
82     /// Header - An instance of MachOHeader that we will update while we build
83     /// the file, and then emit during finalization.
84     MachOHeader Header;
85
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);
89
90     bool runOnMachineFunction(MachineFunction &MF);
91
92     /// doFinalization - Now that the module has been completely processed, emit
93     /// the Mach-O file to 'O'.
94     bool doFinalization(Module &M);
95
96   private:
97
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;
102
103     /// SectionLookup - This is a mapping from section name to SectionList entry
104     std::map<std::string, MachOSection*> SectionLookup;
105
106     /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
107     /// to aid in emitting relocations.
108     std::map<GlobalValue*, MachOSection*> GVSection;
109
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
112     /// relocations.
113     std::map<GlobalValue*, intptr_t> GVOffset;
114
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 &sect,
118                              unsigned Flags = 0);
119
120     /// getTextSection - Return text section with different flags for code/data
121     MachOSection *getTextSection(bool isCode = true);
122
123     MachOSection *getDataSection() {
124       return getSection("__DATA", "__data");
125     }
126
127     MachOSection *getBSSSection();
128     MachOSection *getConstSection(Constant *C);
129     MachOSection *getJumpTableSection();
130
131     /// MachOSymTab - This struct contains information about the offsets and
132     /// size of symbol table information.
133     /// segment.
134     struct MachOSymTab {
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
141
142       // Constants for the cmd field
143       // see <mach-o/loader.h>
144       enum { LC_SYMTAB = 0x02  // link-edit stab symbol table info
145       };
146
147       MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
148         nsyms(0), stroff(0), strsize(0) { }
149     };
150
151     /// SymTab - The "stab" style symbol table information
152     MachOSymTab SymTab;
153     /// DySymTab - symbol table info for the dynamic link editor
154     MachODySymTab DySymTab;
155
156   protected:
157
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;
162
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;
166
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;
170
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;
175
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;
179
180     static void InitMem(const Constant *C, uintptr_t Offset,
181                         const TargetData *TD, MachOSection* mos);
182
183   private:
184     void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
185     void EmitGlobal(GlobalVariable *GV);
186     void EmitHeaderAndLoadCommands();
187     void EmitSections();
188     void EmitRelocations();
189     void BufferSymbolAndStringTable();
190     void CalculateRelocations(MachOSection &MOS);
191
192     // GetJTRelocation - Get a relocation a new BB relocation based
193     // on target information.
194     MachineRelocation GetJTRelocation(unsigned Offset,
195                                       MachineBasicBlock *MBB) const;
196
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);
202   };
203 }
204
205 #endif