blob: e2b6fa8dd4d9373b5eaedc1619e04ea7ec56fbc7 [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 Begeman1257c852007-01-29 21:20:42 +000017#include "llvm/Constants.h"
Nate Begemanf8f2c5a2006-08-25 06:36:58 +000018#include "llvm/DerivedTypes.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Nate Begeman94be2482006-09-08 22:42:09 +000020#include "llvm/CodeGen/MachineRelocation.h"
Nate Begemanf8f2c5a2006-08-25 06:36:58 +000021#include "llvm/Target/TargetData.h"
22#include "llvm/Target/TargetMachine.h"
Bill Wendling40fab402007-01-24 03:37:18 +000023#include "llvm/Target/TargetMachOWriterInfo.h"
Dan Gohmanc9235d22008-03-21 23:51:57 +000024#include <map>
Nate Begemaneb883af2006-08-23 21:08:52 +000025
26namespace llvm {
27 class GlobalVariable;
28 class Mangler;
29 class MachineCodeEmitter;
30 class MachOCodeEmitter;
Bill Wendling0f43b222007-02-03 02:37:51 +000031 class OutputBuffer;
Owen Andersoncb371882008-08-21 00:14:44 +000032 class raw_ostream;
Nate Begemaneb883af2006-08-23 21:08:52 +000033
Nate Begeman94be2482006-09-08 22:42:09 +000034 /// MachOSym - This struct contains information about each symbol that is
35 /// added to logical symbol table for the module. This is eventually
36 /// turned into a real symbol table in the file.
37 struct MachOSym {
38 const GlobalValue *GV; // The global value this corresponds to.
39 std::string GVName; // The mangled name of the global value.
40 uint32_t n_strx; // index into the string table
41 uint8_t n_type; // type flag
42 uint8_t n_sect; // section number or NO_SECT
43 int16_t n_desc; // see <mach-o/stab.h>
44 uint64_t n_value; // value for this symbol (or stab offset)
45
46 // Constants for the n_sect field
47 // see <mach-o/nlist.h>
48 enum { NO_SECT = 0 }; // symbol is not in any section
49
50 // Constants for the n_type field
51 // see <mach-o/nlist.h>
52 enum { N_UNDF = 0x0, // undefined, n_sect == NO_SECT
53 N_ABS = 0x2, // absolute, n_sect == NO_SECT
54 N_SECT = 0xe, // defined in section number n_sect
55 N_PBUD = 0xc, // prebound undefined (defined in a dylib)
56 N_INDR = 0xa // indirect
57 };
58 // The following bits are OR'd into the types above. For example, a type
59 // of 0x0f would be an external N_SECT symbol (0x0e | 0x01).
60 enum { N_EXT = 0x01, // external symbol bit
61 N_PEXT = 0x10 // private external symbol bit
62 };
63
64 // Constants for the n_desc field
65 // see <mach-o/loader.h>
66 enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
67 REFERENCE_FLAG_UNDEFINED_LAZY = 1,
68 REFERENCE_FLAG_DEFINED = 2,
69 REFERENCE_FLAG_PRIVATE_DEFINED = 3,
70 REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
71 REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5
72 };
73 enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped
74 N_WEAK_REF = 0x0040, // symbol is weak referenced
75 N_WEAK_DEF = 0x0080 // coalesced symbol is a weak definition
76 };
77
Nate Begemanbfaaaa62006-12-11 02:20:45 +000078 MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
79 TargetMachine &TM);
Nate Begeman94be2482006-09-08 22:42:09 +000080 };
81
Nate Begemaneb883af2006-08-23 21:08:52 +000082 /// MachOWriter - This class implements the common target-independent code for
83 /// writing Mach-O files. Targets should derive a class from this to
84 /// parameterize the output format.
85 ///
86 class MachOWriter : public MachineFunctionPass {
87 friend class MachOCodeEmitter;
88 public:
Devang Patel19974732007-05-03 01:11:54 +000089 static char ID;
Nate Begemaneb883af2006-08-23 21:08:52 +000090 MachineCodeEmitter &getMachineCodeEmitter() const {
91 return *(MachineCodeEmitter*)MCE;
92 }
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000093
Owen Andersoncb371882008-08-21 00:14:44 +000094 MachOWriter(raw_ostream &O, TargetMachine &TM);
Bill Wendling2b721822007-01-24 07:13:56 +000095 virtual ~MachOWriter();
Nate Begemaneb883af2006-08-23 21:08:52 +000096
Bill Wendling2b721822007-01-24 07:13:56 +000097 virtual const char *getPassName() const {
98 return "Mach-O Writer";
99 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000100
101 typedef std::vector<unsigned char> DataBuffer;
Nate Begemaneb883af2006-08-23 21:08:52 +0000102 protected:
Nate Begemaneb883af2006-08-23 21:08:52 +0000103 /// Output stream to send the resultant object file to.
104 ///
Owen Andersoncb371882008-08-21 00:14:44 +0000105 raw_ostream &O;
Nate Begemaneb883af2006-08-23 21:08:52 +0000106
107 /// Target machine description.
108 ///
109 TargetMachine &TM;
110
111 /// Mang - The object used to perform name mangling for this module.
112 ///
113 Mangler *Mang;
Nate Begeman94be2482006-09-08 22:42:09 +0000114
Nate Begemaneb883af2006-08-23 21:08:52 +0000115 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
116 /// code for functions to the .o file.
117 MachOCodeEmitter *MCE;
118
119 /// is64Bit/isLittleEndian - This information is inferred from the target
120 /// machine directly, indicating what header values and flags to set.
121 bool is64Bit, isLittleEndian;
122
123 /// doInitialization - Emit the file header and all of the global variables
124 /// for the module to the Mach-O file.
125 bool doInitialization(Module &M);
126
127 bool runOnMachineFunction(MachineFunction &MF);
128
129 /// doFinalization - Now that the module has been completely processed, emit
130 /// the Mach-O file to 'O'.
131 bool doFinalization(Module &M);
132
133 /// MachOHeader - This struct contains the header information about a
134 /// specific architecture type/subtype pair that is emitted to the file.
135 struct MachOHeader {
Nate Begemanea7b8cf2006-08-23 21:33:27 +0000136 uint32_t magic; // mach magic number identifier
Nate Begemanea7b8cf2006-08-23 21:33:27 +0000137 uint32_t filetype; // type of file
138 uint32_t ncmds; // number of load commands
139 uint32_t sizeofcmds; // the size of all the load commands
140 uint32_t flags; // flags
Nate Begemaneb883af2006-08-23 21:08:52 +0000141 uint32_t reserved; // 64-bit only
142
143 /// HeaderData - The actual data for the header which we are building
144 /// up for emission to the file.
145 DataBuffer HeaderData;
146
Nate Begemaneb883af2006-08-23 21:08:52 +0000147 // Constants for the filetype field
148 // see <mach-o/loader.h> for additional info on the various types
149 enum { MH_OBJECT = 1, // relocatable object file
150 MH_EXECUTE = 2, // demand paged executable file
151 MH_FVMLIB = 3, // fixed VM shared library file
152 MH_CORE = 4, // core file
153 MH_PRELOAD = 5, // preloaded executable file
154 MH_DYLIB = 6, // dynamically bound shared library
155 MH_DYLINKER = 7, // dynamic link editor
156 MH_BUNDLE = 8, // dynamically bound bundle file
157 MH_DYLIB_STUB = 9, // shared library stub for static linking only
158 MH_DSYM = 10 // companion file wiht only debug sections
159 };
160
161 // Constants for the flags field
162 enum { MH_NOUNDEFS = 1 << 0,
163 // the object file has no undefined references
164 MH_INCRLINK = 1 << 1,
165 // the object file is the output of an incremental link against
166 // a base file and cannot be link edited again
167 MH_DYLDLINK = 1 << 2,
168 // the object file is input for the dynamic linker and cannot be
169 // statically link edited again.
170 MH_BINDATLOAD = 1 << 3,
171 // the object file's undefined references are bound by the
172 // dynamic linker when loaded.
173 MH_PREBOUND = 1 << 4,
174 // the file has its dynamic undefined references prebound
175 MH_SPLIT_SEGS = 1 << 5,
176 // the file has its read-only and read-write segments split
177 // see <mach/shared_memory_server.h>
178 MH_LAZY_INIT = 1 << 6,
179 // the shared library init routine is to be run lazily via
180 // catching memory faults to its writable segments (obsolete)
181 MH_TWOLEVEL = 1 << 7,
182 // the image is using two-level namespace bindings
183 MH_FORCE_FLAT = 1 << 8,
184 // the executable is forcing all images to use flat namespace
185 // bindings.
186 MH_NOMULTIDEFS = 1 << 8,
187 // this umbrella guarantees no multiple definitions of symbols
188 // in its sub-images so the two-level namespace hints can
189 // always be used.
190 MH_NOFIXPREBINDING = 1 << 10,
191 // do not have dyld notify the prebidning agent about this
192 // executable.
193 MH_PREBINDABLE = 1 << 11,
194 // the binary is not prebound but can have its prebinding
195 // redone. only used when MH_PREBOUND is not set.
196 MH_ALLMODSBOUND = 1 << 12,
197 // indicates that this binary binds to all two-level namespace
198 // modules of its dependent libraries. Only used when
199 // MH_PREBINDABLE and MH_TWOLEVEL are both set.
200 MH_SUBSECTIONS_VIA_SYMBOLS = 1 << 13,
201 // safe to divide up the sections into sub-sections via symbols
202 // for dead code stripping.
203 MH_CANONICAL = 1 << 14,
204 // the binary has been canonicalized via the unprebind operation
205 MH_WEAK_DEFINES = 1 << 15,
206 // the final linked image contains external weak symbols
207 MH_BINDS_TO_WEAK = 1 << 16,
208 // the final linked image uses weak symbols
209 MH_ALLOW_STACK_EXECUTION = 1 << 17
210 // When this bit is set, all stacks in the task will be given
211 // stack execution privilege. Only used in MH_EXECUTE filetype
212 };
213
Bill Wendling40fab402007-01-24 03:37:18 +0000214 MachOHeader() : magic(0), filetype(0), ncmds(0), sizeofcmds(0), flags(0),
215 reserved(0) { }
Nate Begemaneb883af2006-08-23 21:08:52 +0000216
217 /// cmdSize - This routine returns the size of the MachOSection as written
218 /// to disk, depending on whether the destination is a 64 bit Mach-O file.
219 unsigned cmdSize(bool is64Bit) const {
220 if (is64Bit)
221 return 8 * sizeof(uint32_t);
222 else
223 return 7 * sizeof(uint32_t);
224 }
225
226 /// setMagic - This routine sets the appropriate value for the 'magic'
227 /// field based on pointer size and endianness.
228 void setMagic(bool isLittleEndian, bool is64Bit) {
229 if (isLittleEndian)
230 if (is64Bit) magic = 0xcffaedfe;
231 else magic = 0xcefaedfe;
232 else
233 if (is64Bit) magic = 0xfeedfacf;
234 else magic = 0xfeedface;
235 }
236 };
237
238 /// Header - An instance of MachOHeader that we will update while we build
239 /// the file, and then emit during finalization.
240 MachOHeader Header;
241
Nate Begemaneb883af2006-08-23 21:08:52 +0000242 /// MachOSegment - This struct contains the necessary information to
243 /// emit the load commands for each section in the file.
244 struct MachOSegment {
245 uint32_t cmd; // LC_SEGMENT or LC_SEGMENT_64
246 uint32_t cmdsize; // Total size of this struct and section commands
247 std::string segname; // segment name
248 uint64_t vmaddr; // address of this segment
249 uint64_t vmsize; // size of this segment, may be larger than filesize
250 uint64_t fileoff; // offset in file
251 uint64_t filesize; // amount to read from file
252 uint32_t maxprot; // maximum VM protection
253 uint32_t initprot; // initial VM protection
254 uint32_t nsects; // number of sections in this segment
255 uint32_t flags; // flags
256
Chris Lattner3381f0a2006-12-16 20:23:42 +0000257 // The following constants are getting pulled in by one of the
258 // system headers, which creates a neat clash with the enum.
259#if !defined(VM_PROT_NONE)
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000260#define VM_PROT_NONE 0x00
Chris Lattner3381f0a2006-12-16 20:23:42 +0000261#endif
262#if !defined(VM_PROT_READ)
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000263#define VM_PROT_READ 0x01
Chris Lattner3381f0a2006-12-16 20:23:42 +0000264#endif
265#if !defined(VM_PROT_WRITE)
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000266#define VM_PROT_WRITE 0x02
Chris Lattner3381f0a2006-12-16 20:23:42 +0000267#endif
268#if !defined(VM_PROT_EXECUTE)
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000269#define VM_PROT_EXECUTE 0x04
Chris Lattner3381f0a2006-12-16 20:23:42 +0000270#endif
271#if !defined(VM_PROT_ALL)
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000272#define VM_PROT_ALL 0x07
Chris Lattner3381f0a2006-12-16 20:23:42 +0000273#endif
274
Nate Begemaneb883af2006-08-23 21:08:52 +0000275 // Constants for the vm protection fields
276 // see <mach-o/vm_prot.h>
Chris Lattner3381f0a2006-12-16 20:23:42 +0000277 enum { SEG_VM_PROT_NONE = VM_PROT_NONE,
278 SEG_VM_PROT_READ = VM_PROT_READ, // read permission
279 SEG_VM_PROT_WRITE = VM_PROT_WRITE, // write permission
280 SEG_VM_PROT_EXECUTE = VM_PROT_EXECUTE,
281 SEG_VM_PROT_ALL = VM_PROT_ALL
Nate Begemaneb883af2006-08-23 21:08:52 +0000282 };
283
284 // Constants for the cmd field
285 // see <mach-o/loader.h>
286 enum { LC_SEGMENT = 0x01, // segment of this file to be mapped
287 LC_SEGMENT_64 = 0x19 // 64-bit segment of this file to be mapped
288 };
289
290 /// cmdSize - This routine returns the size of the MachOSection as written
291 /// to disk, depending on whether the destination is a 64 bit Mach-O file.
292 unsigned cmdSize(bool is64Bit) const {
293 if (is64Bit)
294 return 6 * sizeof(uint32_t) + 4 * sizeof(uint64_t) + 16;
295 else
296 return 10 * sizeof(uint32_t) + 16; // addresses only 32 bits
297 }
298
299 MachOSegment(const std::string &seg, bool is64Bit)
300 : cmd(is64Bit ? LC_SEGMENT_64 : LC_SEGMENT), cmdsize(0), segname(seg),
301 vmaddr(0), vmsize(0), fileoff(0), filesize(0), maxprot(VM_PROT_ALL),
302 initprot(VM_PROT_ALL), nsects(0), flags(0) { }
303 };
304
305 /// MachOSection - This struct contains information about each section in a
306 /// particular segment that is emitted to the file. This is eventually
307 /// turned into the SectionCommand in the load command for a particlar
308 /// segment.
309 struct MachOSection {
Nate Begemanea7b8cf2006-08-23 21:33:27 +0000310 std::string sectname; // name of this section,
311 std::string segname; // segment this section goes in
312 uint64_t addr; // memory address of this section
313 uint64_t size; // size in bytes of this section
314 uint32_t offset; // file offset of this section
315 uint32_t align; // section alignment (power of 2)
316 uint32_t reloff; // file offset of relocation entries
317 uint32_t nreloc; // number of relocation entries
318 uint32_t flags; // flags (section type and attributes)
319 uint32_t reserved1; // reserved (for offset or index)
320 uint32_t reserved2; // reserved (for count or sizeof)
321 uint32_t reserved3; // reserved (64 bit only)
Nate Begemaneb883af2006-08-23 21:08:52 +0000322
323 /// A unique number for this section, which will be used to match symbols
324 /// to the correct section.
325 uint32_t Index;
326
327 /// SectionData - The actual data for this section which we are building
328 /// up for emission to the file.
329 DataBuffer SectionData;
Nate Begeman019f8512006-09-10 23:03:44 +0000330
331 /// RelocBuffer - A buffer to hold the mach-o relocations before we write
332 /// them out at the appropriate location in the file.
333 DataBuffer RelocBuffer;
Nate Begemaneb883af2006-08-23 21:08:52 +0000334
Nate Begeman94be2482006-09-08 22:42:09 +0000335 /// Relocations - The relocations that we have encountered so far in this
336 /// section that we will need to convert to MachORelocation entries when
337 /// the file is written.
338 std::vector<MachineRelocation> Relocations;
339
Nate Begemaneb883af2006-08-23 21:08:52 +0000340 // Constants for the section types (low 8 bits of flags field)
341 // see <mach-o/loader.h>
342 enum { S_REGULAR = 0,
343 // regular section
344 S_ZEROFILL = 1,
345 // zero fill on demand section
346 S_CSTRING_LITERALS = 2,
347 // section with only literal C strings
348 S_4BYTE_LITERALS = 3,
349 // section with only 4 byte literals
350 S_8BYTE_LITERALS = 4,
351 // section with only 8 byte literals
352 S_LITERAL_POINTERS = 5,
353 // section with only pointers to literals
354 S_NON_LAZY_SYMBOL_POINTERS = 6,
355 // section with only non-lazy symbol pointers
356 S_LAZY_SYMBOL_POINTERS = 7,
357 // section with only lazy symbol pointers
358 S_SYMBOL_STUBS = 8,
359 // section with only symbol stubs
360 // byte size of stub in the reserved2 field
361 S_MOD_INIT_FUNC_POINTERS = 9,
362 // section with only function pointers for initialization
363 S_MOD_TERM_FUNC_POINTERS = 10,
364 // section with only function pointers for termination
365 S_COALESCED = 11,
366 // section contains symbols that are coalesced
367 S_GB_ZEROFILL = 12,
368 // zero fill on demand section (that can be larger than 4GB)
369 S_INTERPOSING = 13,
370 // section with only pairs of function pointers for interposing
371 S_16BYTE_LITERALS = 14
372 // section with only 16 byte literals
373 };
374
375 // Constants for the section flags (high 24 bits of flags field)
376 // see <mach-o/loader.h>
377 enum { S_ATTR_PURE_INSTRUCTIONS = 1 << 31,
378 // section contains only true machine instructions
379 S_ATTR_NO_TOC = 1 << 30,
380 // section contains coalesced symbols that are not to be in a
381 // ranlib table of contents
382 S_ATTR_STRIP_STATIC_SYMS = 1 << 29,
383 // ok to strip static symbols in this section in files with the
384 // MY_DYLDLINK flag
385 S_ATTR_NO_DEAD_STRIP = 1 << 28,
386 // no dead stripping
387 S_ATTR_LIVE_SUPPORT = 1 << 27,
388 // blocks are live if they reference live blocks
389 S_ATTR_SELF_MODIFYING_CODE = 1 << 26,
390 // used with i386 code stubs written on by dyld
391 S_ATTR_DEBUG = 1 << 25,
392 // a debug section
393 S_ATTR_SOME_INSTRUCTIONS = 1 << 10,
394 // section contains some machine instructions
395 S_ATTR_EXT_RELOC = 1 << 9,
396 // section has external relocation entries
397 S_ATTR_LOC_RELOC = 1 << 8
398 // section has local relocation entries
399 };
400
401 /// cmdSize - This routine returns the size of the MachOSection as written
402 /// to disk, depending on whether the destination is a 64 bit Mach-O file.
403 unsigned cmdSize(bool is64Bit) const {
404 if (is64Bit)
405 return 7 * sizeof(uint32_t) + 2 * sizeof(uint64_t) + 32;
406 else
407 return 9 * sizeof(uint32_t) + 32; // addresses only 32 bits
408 }
409
410 MachOSection(const std::string &seg, const std::string &sect)
Nate Begeman019f8512006-09-10 23:03:44 +0000411 : sectname(sect), segname(seg), addr(0), size(0), offset(0), align(2),
Nate Begemaneb883af2006-08-23 21:08:52 +0000412 reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0),
413 reserved3(0) { }
414 };
415
Nate Begemand2030e62006-08-26 15:46:34 +0000416 private:
417
Nate Begemaneb883af2006-08-23 21:08:52 +0000418 /// SectionList - This is the list of sections that we have emitted to the
419 /// file. Once the file has been completely built, the segment load command
420 /// SectionCommands are constructed from this info.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000421 std::vector<MachOSection*> SectionList;
Nate Begemaneb883af2006-08-23 21:08:52 +0000422
423 /// SectionLookup - This is a mapping from section name to SectionList entry
424 std::map<std::string, MachOSection*> SectionLookup;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000425
426 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
427 /// to aid in emitting relocations.
428 std::map<GlobalValue*, MachOSection*> GVSection;
429
430 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
431 /// start of the section in which the GV resides, to aid in emitting
432 /// relocations.
433 std::map<GlobalValue*, intptr_t> GVOffset;
Nate Begemaneb883af2006-08-23 21:08:52 +0000434
435 /// getSection - Return the section with the specified name, creating a new
436 /// section if one does not already exist.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000437 MachOSection *getSection(const std::string &seg, const std::string &sect,
Nate Begemaneb883af2006-08-23 21:08:52 +0000438 unsigned Flags = 0) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000439 MachOSection *MOS = SectionLookup[seg+sect];
440 if (MOS) return MOS;
Nate Begemaneb883af2006-08-23 21:08:52 +0000441
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000442 MOS = new MachOSection(seg, sect);
443 SectionList.push_back(MOS);
444 MOS->Index = SectionList.size();
445 MOS->flags = MachOSection::S_REGULAR | Flags;
446 SectionLookup[seg+sect] = MOS;
447 return MOS;
Nate Begemaneb883af2006-08-23 21:08:52 +0000448 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000449 MachOSection *getTextSection(bool isCode = true) {
Nate Begeman019f8512006-09-10 23:03:44 +0000450 if (isCode)
451 return getSection("__TEXT", "__text",
452 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
453 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
454 else
455 return getSection("__TEXT", "__text");
Nate Begemaneb883af2006-08-23 21:08:52 +0000456 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000457 MachOSection *getBSSSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000458 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
459 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000460 MachOSection *getDataSection() {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000461 return getSection("__DATA", "__data");
462 }
Nate Begeman1257c852007-01-29 21:20:42 +0000463 MachOSection *getConstSection(Constant *C) {
464 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
465 if (CVA && CVA->isCString())
466 return getSection("__TEXT", "__cstring",
467 MachOSection::S_CSTRING_LITERALS);
468
469 const Type *Ty = C->getType();
Chris Lattner42a75512007-01-15 02:27:26 +0000470 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000471 unsigned Size = TM.getTargetData()->getABITypeSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000472 switch(Size) {
473 default: break; // Fall through to __TEXT,__const
474 case 4:
475 return getSection("__TEXT", "__literal4",
476 MachOSection::S_4BYTE_LITERALS);
477 case 8:
478 return getSection("__TEXT", "__literal8",
479 MachOSection::S_8BYTE_LITERALS);
480 case 16:
481 return getSection("__TEXT", "__literal16",
482 MachOSection::S_16BYTE_LITERALS);
483 }
484 }
485 return getSection("__TEXT", "__const");
486 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000487 MachOSection *getJumpTableSection() {
Nate Begeman019f8512006-09-10 23:03:44 +0000488 if (TM.getRelocationModel() == Reloc::PIC_)
489 return getTextSection(false);
490 else
491 return getSection("__TEXT", "__const");
492 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000493
494 /// MachOSymTab - This struct contains information about the offsets and
495 /// size of symbol table information.
496 /// segment.
497 struct MachOSymTab {
498 uint32_t cmd; // LC_SYMTAB
499 uint32_t cmdsize; // sizeof( MachOSymTab )
500 uint32_t symoff; // symbol table offset
501 uint32_t nsyms; // number of symbol table entries
502 uint32_t stroff; // string table offset
503 uint32_t strsize; // string table size in bytes
504
505 // Constants for the cmd field
506 // see <mach-o/loader.h>
507 enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
508 };
509
510 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
511 nsyms(0), stroff(0), strsize(0) { }
512 };
513
514 /// MachOSymTab - This struct contains information about the offsets and
515 /// size of symbol table information.
516 /// segment.
517 struct MachODySymTab {
518 uint32_t cmd; // LC_DYSYMTAB
519 uint32_t cmdsize; // sizeof( MachODySymTab )
520 uint32_t ilocalsym; // index to local symbols
521 uint32_t nlocalsym; // number of local symbols
522 uint32_t iextdefsym; // index to externally defined symbols
523 uint32_t nextdefsym; // number of externally defined symbols
524 uint32_t iundefsym; // index to undefined symbols
525 uint32_t nundefsym; // number of undefined symbols
526 uint32_t tocoff; // file offset to table of contents
527 uint32_t ntoc; // number of entries in table of contents
528 uint32_t modtaboff; // file offset to module table
529 uint32_t nmodtab; // number of module table entries
530 uint32_t extrefsymoff; // offset to referenced symbol table
531 uint32_t nextrefsyms; // number of referenced symbol table entries
532 uint32_t indirectsymoff; // file offset to the indirect symbol table
533 uint32_t nindirectsyms; // number of indirect symbol table entries
534 uint32_t extreloff; // offset to external relocation entries
535 uint32_t nextrel; // number of external relocation entries
536 uint32_t locreloff; // offset to local relocation entries
537 uint32_t nlocrel; // number of local relocation entries
538
539 // Constants for the cmd field
540 // see <mach-o/loader.h>
541 enum { LC_DYSYMTAB = 0x0B // dynamic link-edit symbol table info
542 };
543
544 MachODySymTab() : cmd(LC_DYSYMTAB), cmdsize(20 * sizeof(uint32_t)),
545 ilocalsym(0), nlocalsym(0), iextdefsym(0), nextdefsym(0),
546 iundefsym(0), nundefsym(0), tocoff(0), ntoc(0), modtaboff(0),
547 nmodtab(0), extrefsymoff(0), nextrefsyms(0), indirectsymoff(0),
548 nindirectsyms(0), extreloff(0), nextrel(0), locreloff(0), nlocrel(0) { }
549 };
550
551 /// SymTab - The "stab" style symbol table information
552 MachOSymTab SymTab;
553 /// DySymTab - symbol table info for the dynamic link editor
554 MachODySymTab DySymTab;
555
Nate Begemand2030e62006-08-26 15:46:34 +0000556 struct MachOSymCmp {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000557 // FIXME: this does not appear to be sorting 'f' after 'F'
Nate Begemand2030e62006-08-26 15:46:34 +0000558 bool operator()(const MachOSym &LHS, const MachOSym &RHS) {
559 return LHS.GVName < RHS.GVName;
560 }
561 };
562
563 /// PartitionByLocal - Simple boolean predicate that returns true if Sym is
564 /// a local symbol rather than an external symbol.
565 static bool PartitionByLocal(const MachOSym &Sym);
566
567 /// PartitionByDefined - Simple boolean predicate that returns true if Sym
568 /// is defined in this module.
Nate Begeman94be2482006-09-08 22:42:09 +0000569 static bool PartitionByDefined(const MachOSym &Sym);
Nate Begemand2030e62006-08-26 15:46:34 +0000570
Nate Begeman94be2482006-09-08 22:42:09 +0000571 protected:
572
Nate Begemaneb883af2006-08-23 21:08:52 +0000573 /// SymbolTable - This is the list of symbols we have emitted to the file.
574 /// This actually gets rearranged before emission to the file (to put the
575 /// local symbols first in the list).
576 std::vector<MachOSym> SymbolTable;
577
Nate Begemand2030e62006-08-26 15:46:34 +0000578 /// SymT - A buffer to hold the symbol table before we write it out at the
579 /// appropriate location in the file.
580 DataBuffer SymT;
581
582 /// StrT - A buffer to hold the string table before we write it out at the
583 /// appropriate location in the file.
584 DataBuffer StrT;
585
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000586 /// PendingSyms - This is a list of externally defined symbols that we have
587 /// been asked to emit, but have not seen a reference to. When a reference
588 /// is seen, the symbol will move from this list to the SymbolTable.
Nate Begemanfec910c2007-02-28 07:40:50 +0000589 std::vector<GlobalValue*> PendingGlobals;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000590
Nate Begemaneb883af2006-08-23 21:08:52 +0000591 /// DynamicSymbolTable - This is just a vector of indices into
592 /// SymbolTable to aid in emitting the DYSYMTAB load command.
593 std::vector<unsigned> DynamicSymbolTable;
594
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000595 static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
596 const TargetData *TD,
597 std::vector<MachineRelocation> &MRs);
Nate Begeman019f8512006-09-10 23:03:44 +0000598
Nate Begemaneb883af2006-08-23 21:08:52 +0000599 private:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000600 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
Nate Begemaneb883af2006-08-23 21:08:52 +0000601 void EmitGlobal(GlobalVariable *GV);
602 void EmitHeaderAndLoadCommands();
603 void EmitSections();
Nate Begemand2030e62006-08-26 15:46:34 +0000604 void BufferSymbolAndStringTable();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000605 void CalculateRelocations(MachOSection &MOS);
Nate Begeman94be2482006-09-08 22:42:09 +0000606
Bill Wendling40fab402007-01-24 03:37:18 +0000607 MachineRelocation GetJTRelocation(unsigned Offset,
608 MachineBasicBlock *MBB) const {
609 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
610 }
Bill Wendling0f43b222007-02-03 02:37:51 +0000611
612 /// GetTargetRelocation - Returns the number of relocations.
613 unsigned GetTargetRelocation(MachineRelocation &MR,
614 unsigned FromIdx,
615 unsigned ToAddr,
616 unsigned ToIndex,
617 OutputBuffer &RelocOut,
618 OutputBuffer &SecOut,
Nate Begemanfec910c2007-02-28 07:40:50 +0000619 bool Scattered,
620 bool Extern) {
Bill Wendling0f43b222007-02-03 02:37:51 +0000621 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
622 ToIndex, RelocOut,
Nate Begemanfec910c2007-02-28 07:40:50 +0000623 SecOut, Scattered,
624 Extern);
Bill Wendling0f43b222007-02-03 02:37:51 +0000625 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000626 };
627}
628
629#endif