blob: 3067ff122997d3de2e580467f6a43d6a0cbeca52 [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
Nate Begemaneb883af2006-08-23 21:08:52 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000018#include <vector>
Dan Gohmanc9235d22008-03-21 23:51:57 +000019#include <map>
Nate Begemaneb883af2006-08-23 21:08:52 +000020
21namespace llvm {
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000022 class Constant;
Nate Begemaneb883af2006-08-23 21:08:52 +000023 class GlobalVariable;
24 class Mangler;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000025 class MachineRelocation;
Nate Begemaneb883af2006-08-23 21:08:52 +000026 class MachOCodeEmitter;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000027 class MachODySymTab;
28 class MachOHeader;
29 class MachOSection;
30 class MachOSym;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000031 class TargetData;
32 class TargetMachine;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000033 class TargetAsmInfo;
34 class ObjectCodeEmitter;
Bill Wendling0f43b222007-02-03 02:37:51 +000035 class OutputBuffer;
Owen Andersoncb371882008-08-21 00:14:44 +000036 class raw_ostream;
Nate Begemaneb883af2006-08-23 21:08:52 +000037
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:
Devang Patel19974732007-05-03 01:11:54 +000045 static char ID;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000046
47 ObjectCodeEmitter *getObjectCodeEmitter() {
48 return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
Nate Begemaneb883af2006-08-23 21:08:52 +000049 }
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000050
Owen Andersoncb371882008-08-21 00:14:44 +000051 MachOWriter(raw_ostream &O, TargetMachine &TM);
Bill Wendling2b721822007-01-24 07:13:56 +000052 virtual ~MachOWriter();
Nate Begemaneb883af2006-08-23 21:08:52 +000053
Bill Wendling2b721822007-01-24 07:13:56 +000054 virtual const char *getPassName() const {
55 return "Mach-O Writer";
56 }
Nate Begemaneb883af2006-08-23 21:08:52 +000057
Nate Begemaneb883af2006-08-23 21:08:52 +000058 protected:
Nate Begemaneb883af2006-08-23 21:08:52 +000059 /// Output stream to send the resultant object file to.
60 ///
Owen Andersoncb371882008-08-21 00:14:44 +000061 raw_ostream &O;
Nate Begemaneb883af2006-08-23 21:08:52 +000062
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;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000070
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000071 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit
72 /// machine code for functions to the .o file.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000073 MachOCodeEmitter *MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000074
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
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000079 // Target Asm Info
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000080 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.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000084 MachOHeader Header;
85
Nate Begemaneb883af2006-08-23 21:08:52 +000086 /// 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
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.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000101 std::vector<MachOSection*> SectionList;
Nate Begemaneb883af2006-08-23 21:08:52 +0000102
103 /// SectionLookup - This is a mapping from section name to SectionList entry
104 std::map<std::string, MachOSection*> SectionLookup;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000105
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000106 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
107 /// to aid in emitting relocations.
108 std::map<GlobalValue*, MachOSection*> GVSection;
109
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000110 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000111 /// start of the section in which the GV resides, to aid in emitting
112 /// relocations.
113 std::map<GlobalValue*, intptr_t> GVOffset;
Nate Begemaneb883af2006-08-23 21:08:52 +0000114
115 /// getSection - Return the section with the specified name, creating a new
116 /// section if one does not already exist.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000117 MachOSection *getSection(const std::string &seg, const std::string &sect,
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000118 unsigned Flags = 0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000119
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000120 /// getTextSection - Return text section with different flags for code/data
121 MachOSection *getTextSection(bool isCode = true);
122
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000123 MachOSection *getDataSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000124 return getSection("__DATA", "__data");
125 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000126
127 MachOSection *getBSSSection();
128 MachOSection *getConstSection(Constant *C);
129 MachOSection *getJumpTableSection();
130
131 /// MachOSymTab - This struct contains information about the offsets and
Nate Begemaneb883af2006-08-23 21:08:52 +0000132 /// 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 };
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000146
Nate Begemaneb883af2006-08-23 21:08:52 +0000147 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
148 nsyms(0), stroff(0), strsize(0) { }
149 };
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000150
Nate Begemaneb883af2006-08-23 21:08:52 +0000151 /// SymTab - The "stab" style symbol table information
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000152 MachOSymTab SymTab;
Nate Begemaneb883af2006-08-23 21:08:52 +0000153 /// DySymTab - symbol table info for the dynamic link editor
154 MachODySymTab DySymTab;
155
Nate Begeman94be2482006-09-08 22:42:09 +0000156 protected:
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000157
Nate Begemaneb883af2006-08-23 21:08:52 +0000158 /// 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;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000162
Nate Begemand2030e62006-08-26 15:46:34 +0000163 /// SymT - A buffer to hold the symbol table before we write it out at the
164 /// appropriate location in the file.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000165 std::vector<unsigned char> SymT;
166
Nate Begemand2030e62006-08-26 15:46:34 +0000167 /// StrT - A buffer to hold the string table before we write it out at the
168 /// appropriate location in the file.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000169 std::vector<unsigned char> StrT;
170
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000171 /// 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.
Nate Begemanfec910c2007-02-28 07:40:50 +0000174 std::vector<GlobalValue*> PendingGlobals;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000175
Nate Begemaneb883af2006-08-23 21:08:52 +0000176 /// DynamicSymbolTable - This is just a vector of indices into
177 /// SymbolTable to aid in emitting the DYSYMTAB load command.
178 std::vector<unsigned> DynamicSymbolTable;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000179
180 static void InitMem(const Constant *C, uintptr_t Offset,
181 const TargetData *TD, MachOSection* mos);
Nate Begeman019f8512006-09-10 23:03:44 +0000182
Nate Begemaneb883af2006-08-23 21:08:52 +0000183 private:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000184 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
Nate Begemaneb883af2006-08-23 21:08:52 +0000185 void EmitGlobal(GlobalVariable *GV);
186 void EmitHeaderAndLoadCommands();
187 void EmitSections();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000188 void EmitRelocations();
Nate Begemand2030e62006-08-26 15:46:34 +0000189 void BufferSymbolAndStringTable();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000190 void CalculateRelocations(MachOSection &MOS);
Nate Begeman94be2482006-09-08 22:42:09 +0000191
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000192 // GetJTRelocation - Get a relocation a new BB relocation based
193 // on target information.
Bill Wendling40fab402007-01-24 03:37:18 +0000194 MachineRelocation GetJTRelocation(unsigned Offset,
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000195 MachineBasicBlock *MBB) const;
Bill Wendling0f43b222007-02-03 02:37:51 +0000196
197 /// GetTargetRelocation - Returns the number of relocations.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000198 unsigned GetTargetRelocation(MachineRelocation &MR, unsigned FromIdx,
199 unsigned ToAddr, unsigned ToIndex,
200 OutputBuffer &RelocOut, OutputBuffer &SecOut,
201 bool Scattered, bool Extern);
Nate Begemaneb883af2006-08-23 21:08:52 +0000202 };
203}
204
205#endif