blob: 9d8e9d450baab068ca66bef3f5c7cf31a4b9000e [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;
David Greene5f3aeac2009-08-19 22:19:44 +000025 class MachineBasicBlock;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000026 class MachineRelocation;
Nate Begemaneb883af2006-08-23 21:08:52 +000027 class MachOCodeEmitter;
Daniel Dunbar7a80f5f2009-07-13 06:04:06 +000028 struct MachODySymTab;
29 struct MachOHeader;
30 struct MachOSection;
Daniel Dunbar89e12a12009-07-13 06:00:13 +000031 struct MachOSym;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000032 class TargetData;
33 class TargetMachine;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000034 class TargetAsmInfo;
35 class ObjectCodeEmitter;
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
39 /// MachOWriter - This class implements the common target-independent code for
40 /// writing Mach-O files. Targets should derive a class from this to
41 /// parameterize the output format.
42 ///
43 class MachOWriter : public MachineFunctionPass {
44 friend class MachOCodeEmitter;
45 public:
Devang Patel19974732007-05-03 01:11:54 +000046 static char ID;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000047
48 ObjectCodeEmitter *getObjectCodeEmitter() {
49 return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
Nate Begemaneb883af2006-08-23 21:08:52 +000050 }
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000051
Owen Andersoncb371882008-08-21 00:14:44 +000052 MachOWriter(raw_ostream &O, TargetMachine &TM);
Bill Wendling2b721822007-01-24 07:13:56 +000053 virtual ~MachOWriter();
Nate Begemaneb883af2006-08-23 21:08:52 +000054
Bill Wendling2b721822007-01-24 07:13:56 +000055 virtual const char *getPassName() const {
56 return "Mach-O Writer";
57 }
Nate Begemaneb883af2006-08-23 21:08:52 +000058
Nate Begemaneb883af2006-08-23 21:08:52 +000059 protected:
Nate Begemaneb883af2006-08-23 21:08:52 +000060 /// Output stream to send the resultant object file to.
61 ///
Owen Andersoncb371882008-08-21 00:14:44 +000062 raw_ostream &O;
Nate Begemaneb883af2006-08-23 21:08:52 +000063
64 /// Target machine description.
65 ///
66 TargetMachine &TM;
67
68 /// Mang - The object used to perform name mangling for this module.
69 ///
70 Mangler *Mang;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000071
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000072 /// MachOCE - The MachineCodeEmitter object that we are exposing to emit
73 /// machine code for functions to the .o file.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000074 MachOCodeEmitter *MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000075
76 /// is64Bit/isLittleEndian - This information is inferred from the target
77 /// machine directly, indicating what header values and flags to set.
78 bool is64Bit, isLittleEndian;
79
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000080 // Target Asm Info
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000081 const TargetAsmInfo *TAI;
82
83 /// Header - An instance of MachOHeader that we will update while we build
84 /// the file, and then emit during finalization.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000085 MachOHeader Header;
86
Nate Begemaneb883af2006-08-23 21:08:52 +000087 /// doInitialization - Emit the file header and all of the global variables
88 /// for the module to the Mach-O file.
89 bool doInitialization(Module &M);
90
91 bool runOnMachineFunction(MachineFunction &MF);
92
93 /// doFinalization - Now that the module has been completely processed, emit
94 /// the Mach-O file to 'O'.
95 bool doFinalization(Module &M);
96
Nate Begemand2030e62006-08-26 15:46:34 +000097 private:
98
Nate Begemaneb883af2006-08-23 21:08:52 +000099 /// SectionList - This is the list of sections that we have emitted to the
100 /// file. Once the file has been completely built, the segment load command
101 /// SectionCommands are constructed from this info.
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
105 std::map<std::string, MachOSection*> SectionLookup;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000106
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000107 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
108 /// to aid in emitting relocations.
109 std::map<GlobalValue*, MachOSection*> GVSection;
110
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000111 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000112 /// start of the section in which the GV resides, to aid in emitting
113 /// relocations.
114 std::map<GlobalValue*, intptr_t> GVOffset;
Nate Begemaneb883af2006-08-23 21:08:52 +0000115
116 /// getSection - Return the section with the specified name, creating a new
117 /// section if one does not already exist.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000118 MachOSection *getSection(const std::string &seg, const std::string &sect,
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000119 unsigned Flags = 0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000120
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000121 /// getTextSection - Return text section with different flags for code/data
122 MachOSection *getTextSection(bool isCode = true);
123
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000124 MachOSection *getDataSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000125 return getSection("__DATA", "__data");
126 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000127
128 MachOSection *getBSSSection();
129 MachOSection *getConstSection(Constant *C);
130 MachOSection *getJumpTableSection();
131
132 /// MachOSymTab - This struct contains information about the offsets and
Nate Begemaneb883af2006-08-23 21:08:52 +0000133 /// size of symbol table information.
134 /// segment.
135 struct MachOSymTab {
136 uint32_t cmd; // LC_SYMTAB
137 uint32_t cmdsize; // sizeof( MachOSymTab )
138 uint32_t symoff; // symbol table offset
139 uint32_t nsyms; // number of symbol table entries
140 uint32_t stroff; // string table offset
141 uint32_t strsize; // string table size in bytes
142
143 // Constants for the cmd field
144 // see <mach-o/loader.h>
145 enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
146 };
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000147
Nate Begemaneb883af2006-08-23 21:08:52 +0000148 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
149 nsyms(0), stroff(0), strsize(0) { }
150 };
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000151
Nate Begemaneb883af2006-08-23 21:08:52 +0000152 /// SymTab - The "stab" style symbol table information
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000153 MachOSymTab SymTab;
Nate Begemaneb883af2006-08-23 21:08:52 +0000154 /// DySymTab - symbol table info for the dynamic link editor
155 MachODySymTab DySymTab;
156
Nate Begeman94be2482006-09-08 22:42:09 +0000157 protected:
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000158
Nate Begemaneb883af2006-08-23 21:08:52 +0000159 /// SymbolTable - This is the list of symbols we have emitted to the file.
160 /// This actually gets rearranged before emission to the file (to put the
161 /// local symbols first in the list).
162 std::vector<MachOSym> SymbolTable;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000163
Nate Begemand2030e62006-08-26 15:46:34 +0000164 /// SymT - A buffer to hold the symbol table before we write it out at the
165 /// appropriate location in the file.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000166 std::vector<unsigned char> SymT;
167
Nate Begemand2030e62006-08-26 15:46:34 +0000168 /// StrT - A buffer to hold the string table before we write it out at the
169 /// appropriate location in the file.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000170 std::vector<unsigned char> StrT;
171
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000172 /// PendingSyms - This is a list of externally defined symbols that we have
173 /// been asked to emit, but have not seen a reference to. When a reference
174 /// is seen, the symbol will move from this list to the SymbolTable.
Nate Begemanfec910c2007-02-28 07:40:50 +0000175 std::vector<GlobalValue*> PendingGlobals;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000176
Nate Begemaneb883af2006-08-23 21:08:52 +0000177 /// DynamicSymbolTable - This is just a vector of indices into
178 /// SymbolTable to aid in emitting the DYSYMTAB load command.
179 std::vector<unsigned> DynamicSymbolTable;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000180
181 static void InitMem(const Constant *C, uintptr_t Offset,
182 const TargetData *TD, MachOSection* mos);
Nate Begeman019f8512006-09-10 23:03:44 +0000183
Nate Begemaneb883af2006-08-23 21:08:52 +0000184 private:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000185 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
Nate Begemaneb883af2006-08-23 21:08:52 +0000186 void EmitGlobal(GlobalVariable *GV);
187 void EmitHeaderAndLoadCommands();
188 void EmitSections();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000189 void EmitRelocations();
Nate Begemand2030e62006-08-26 15:46:34 +0000190 void BufferSymbolAndStringTable();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000191 void CalculateRelocations(MachOSection &MOS);
Nate Begeman94be2482006-09-08 22:42:09 +0000192
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000193 // GetJTRelocation - Get a relocation a new BB relocation based
194 // on target information.
Bill Wendling40fab402007-01-24 03:37:18 +0000195 MachineRelocation GetJTRelocation(unsigned Offset,
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000196 MachineBasicBlock *MBB) const;
Bill Wendling0f43b222007-02-03 02:37:51 +0000197
198 /// GetTargetRelocation - Returns the number of relocations.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000199 unsigned GetTargetRelocation(MachineRelocation &MR, unsigned FromIdx,
200 unsigned ToAddr, unsigned ToIndex,
201 OutputBuffer &RelocOut, OutputBuffer &SecOut,
202 bool Scattered, bool Extern);
Nate Begemaneb883af2006-08-23 21:08:52 +0000203 };
204}
205
206#endif