1 //=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Nate Begeman and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOWriter class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHOWRITER_H
15 #define LLVM_CODEGEN_MACHOWRITER_H
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineRelocation.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetMachine.h"
26 class MachineCodeEmitter;
27 class MachOCodeEmitter;
29 /// MachOSym - This struct contains information about each symbol that is
30 /// added to logical symbol table for the module. This is eventually
31 /// turned into a real symbol table in the file.
33 const GlobalValue *GV; // The global value this corresponds to.
34 std::string GVName; // The mangled name of the global value.
35 uint32_t n_strx; // index into the string table
36 uint8_t n_type; // type flag
37 uint8_t n_sect; // section number or NO_SECT
38 int16_t n_desc; // see <mach-o/stab.h>
39 uint64_t n_value; // value for this symbol (or stab offset)
41 // Constants for the n_sect field
42 // see <mach-o/nlist.h>
43 enum { NO_SECT = 0 }; // symbol is not in any section
45 // Constants for the n_type field
46 // see <mach-o/nlist.h>
47 enum { N_UNDF = 0x0, // undefined, n_sect == NO_SECT
48 N_ABS = 0x2, // absolute, n_sect == NO_SECT
49 N_SECT = 0xe, // defined in section number n_sect
50 N_PBUD = 0xc, // prebound undefined (defined in a dylib)
51 N_INDR = 0xa // indirect
53 // The following bits are OR'd into the types above. For example, a type
54 // of 0x0f would be an external N_SECT symbol (0x0e | 0x01).
55 enum { N_EXT = 0x01, // external symbol bit
56 N_PEXT = 0x10 // private external symbol bit
59 // Constants for the n_desc field
60 // see <mach-o/loader.h>
61 enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY = 0,
62 REFERENCE_FLAG_UNDEFINED_LAZY = 1,
63 REFERENCE_FLAG_DEFINED = 2,
64 REFERENCE_FLAG_PRIVATE_DEFINED = 3,
65 REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY = 4,
66 REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY = 5
68 enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped
69 N_WEAK_REF = 0x0040, // symbol is weak referenced
70 N_WEAK_DEF = 0x0080 // coalesced symbol is a weak definition
73 MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
77 /// MachOWriter - This class implements the common target-independent code for
78 /// writing Mach-O files. Targets should derive a class from this to
79 /// parameterize the output format.
81 class MachOWriter : public MachineFunctionPass {
82 friend class MachOCodeEmitter;
84 MachineCodeEmitter &getMachineCodeEmitter() const {
85 return *(MachineCodeEmitter*)MCE;
90 typedef std::vector<unsigned char> DataBuffer;
93 MachOWriter(std::ostream &O, TargetMachine &TM);
95 /// Output stream to send the resultant object file to.
99 /// Target machine description.
103 /// Mang - The object used to perform name mangling for this module.
107 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
108 /// code for functions to the .o file.
109 MachOCodeEmitter *MCE;
111 /// is64Bit/isLittleEndian - This information is inferred from the target
112 /// machine directly, indicating what header values and flags to set.
113 bool is64Bit, isLittleEndian;
115 /// doInitialization - Emit the file header and all of the global variables
116 /// for the module to the Mach-O file.
117 bool doInitialization(Module &M);
119 bool runOnMachineFunction(MachineFunction &MF);
121 /// doFinalization - Now that the module has been completely processed, emit
122 /// the Mach-O file to 'O'.
123 bool doFinalization(Module &M);
125 /// MachOHeader - This struct contains the header information about a
126 /// specific architecture type/subtype pair that is emitted to the file.
128 uint32_t magic; // mach magic number identifier
129 uint32_t cputype; // cpu specifier
130 uint32_t cpusubtype; // machine specifier
131 uint32_t filetype; // type of file
132 uint32_t ncmds; // number of load commands
133 uint32_t sizeofcmds; // the size of all the load commands
134 uint32_t flags; // flags
135 uint32_t reserved; // 64-bit only
137 /// HeaderData - The actual data for the header which we are building
138 /// up for emission to the file.
139 DataBuffer HeaderData;
141 // The various CPU_TYPE_* constants are already defined by at least one
142 // system header file and create compilation errors if not respected.
143 #if !defined(CPU_TYPE_I386)
144 #define CPU_TYPE_I386 7
146 #if !defined(CPU_TYPE_X86_64)
147 #define CPU_TYPE_X86_64 (CPU_TYPE_I386 | 0x1000000)
149 #if !defined(CPU_TYPE_ARM)
150 #define CPU_TYPE_ARM 12
152 #if !defined(CPU_TYPE_SPARC)
153 #define CPU_TYPE_SPARC 14
155 #if !defined(CPU_TYPE_POWERPC)
156 #define CPU_TYPE_POWERPC 18
158 #if !defined(CPU_TYPE_POWERPC64)
159 #define CPU_TYPE_POWERPC64 (CPU_TYPE_POWERPC | 0x1000000)
162 // Constants for the cputype field
163 // see <mach/machine.h>
164 enum { HDR_CPU_TYPE_I386 = CPU_TYPE_I386,
165 HDR_CPU_TYPE_X86_64 = CPU_TYPE_X86_64,
166 HDR_CPU_TYPE_ARM = CPU_TYPE_ARM,
167 HDR_CPU_TYPE_SPARC = CPU_TYPE_SPARC,
168 HDR_CPU_TYPE_POWERPC = CPU_TYPE_POWERPC,
169 HDR_CPU_TYPE_POWERPC64 = CPU_TYPE_POWERPC64
172 #if !defined(CPU_SUBTYPE_I386_ALL)
173 #define CPU_SUBTYPE_I386_ALL 3
175 #if !defined(CPU_SUBTYPE_X86_64_ALL)
176 #define CPU_SUBTYPE_X86_64_ALL 3
178 #if !defined(CPU_SUBTYPE_ARM_ALL)
179 #define CPU_SUBTYPE_ARM_ALL 0
181 #if !defined(CPU_SUBTYPE_SPARC_ALL)
182 #define CPU_SUBTYPE_SPARC_ALL 0
184 #if !defined(CPU_SUBTYPE_POWERPC_ALL)
185 #define CPU_SUBTYPE_POWERPC_ALL 0
188 // Constants for the cpusubtype field
189 // see <mach/machine.h>
190 enum { HDR_CPU_SUBTYPE_I386_ALL = CPU_SUBTYPE_I386_ALL,
191 HDR_CPU_SUBTYPE_X86_64_ALL = CPU_SUBTYPE_X86_64_ALL,
192 HDR_CPU_SUBTYPE_ARM_ALL = CPU_SUBTYPE_ARM_ALL,
193 HDR_CPU_SUBTYPE_SPARC_ALL = CPU_SUBTYPE_SPARC_ALL,
194 HDR_CPU_SUBTYPE_POWERPC_ALL = CPU_SUBTYPE_POWERPC_ALL
197 // Constants for the filetype field
198 // see <mach-o/loader.h> for additional info on the various types
199 enum { MH_OBJECT = 1, // relocatable object file
200 MH_EXECUTE = 2, // demand paged executable file
201 MH_FVMLIB = 3, // fixed VM shared library file
202 MH_CORE = 4, // core file
203 MH_PRELOAD = 5, // preloaded executable file
204 MH_DYLIB = 6, // dynamically bound shared library
205 MH_DYLINKER = 7, // dynamic link editor
206 MH_BUNDLE = 8, // dynamically bound bundle file
207 MH_DYLIB_STUB = 9, // shared library stub for static linking only
208 MH_DSYM = 10 // companion file wiht only debug sections
211 // Constants for the flags field
212 enum { MH_NOUNDEFS = 1 << 0,
213 // the object file has no undefined references
214 MH_INCRLINK = 1 << 1,
215 // the object file is the output of an incremental link against
216 // a base file and cannot be link edited again
217 MH_DYLDLINK = 1 << 2,
218 // the object file is input for the dynamic linker and cannot be
219 // statically link edited again.
220 MH_BINDATLOAD = 1 << 3,
221 // the object file's undefined references are bound by the
222 // dynamic linker when loaded.
223 MH_PREBOUND = 1 << 4,
224 // the file has its dynamic undefined references prebound
225 MH_SPLIT_SEGS = 1 << 5,
226 // the file has its read-only and read-write segments split
227 // see <mach/shared_memory_server.h>
228 MH_LAZY_INIT = 1 << 6,
229 // the shared library init routine is to be run lazily via
230 // catching memory faults to its writable segments (obsolete)
231 MH_TWOLEVEL = 1 << 7,
232 // the image is using two-level namespace bindings
233 MH_FORCE_FLAT = 1 << 8,
234 // the executable is forcing all images to use flat namespace
236 MH_NOMULTIDEFS = 1 << 8,
237 // this umbrella guarantees no multiple definitions of symbols
238 // in its sub-images so the two-level namespace hints can
240 MH_NOFIXPREBINDING = 1 << 10,
241 // do not have dyld notify the prebidning agent about this
243 MH_PREBINDABLE = 1 << 11,
244 // the binary is not prebound but can have its prebinding
245 // redone. only used when MH_PREBOUND is not set.
246 MH_ALLMODSBOUND = 1 << 12,
247 // indicates that this binary binds to all two-level namespace
248 // modules of its dependent libraries. Only used when
249 // MH_PREBINDABLE and MH_TWOLEVEL are both set.
250 MH_SUBSECTIONS_VIA_SYMBOLS = 1 << 13,
251 // safe to divide up the sections into sub-sections via symbols
252 // for dead code stripping.
253 MH_CANONICAL = 1 << 14,
254 // the binary has been canonicalized via the unprebind operation
255 MH_WEAK_DEFINES = 1 << 15,
256 // the final linked image contains external weak symbols
257 MH_BINDS_TO_WEAK = 1 << 16,
258 // the final linked image uses weak symbols
259 MH_ALLOW_STACK_EXECUTION = 1 << 17
260 // When this bit is set, all stacks in the task will be given
261 // stack execution privilege. Only used in MH_EXECUTE filetype
264 MachOHeader() : magic(0), cputype(0), cpusubtype(0), filetype(0),
265 ncmds(0), sizeofcmds(0), flags(0), reserved(0) { }
267 /// cmdSize - This routine returns the size of the MachOSection as written
268 /// to disk, depending on whether the destination is a 64 bit Mach-O file.
269 unsigned cmdSize(bool is64Bit) const {
271 return 8 * sizeof(uint32_t);
273 return 7 * sizeof(uint32_t);
276 /// setMagic - This routine sets the appropriate value for the 'magic'
277 /// field based on pointer size and endianness.
278 void setMagic(bool isLittleEndian, bool is64Bit) {
280 if (is64Bit) magic = 0xcffaedfe;
281 else magic = 0xcefaedfe;
283 if (is64Bit) magic = 0xfeedfacf;
284 else magic = 0xfeedface;
288 /// Header - An instance of MachOHeader that we will update while we build
289 /// the file, and then emit during finalization.
292 /// MachOSegment - This struct contains the necessary information to
293 /// emit the load commands for each section in the file.
294 struct MachOSegment {
295 uint32_t cmd; // LC_SEGMENT or LC_SEGMENT_64
296 uint32_t cmdsize; // Total size of this struct and section commands
297 std::string segname; // segment name
298 uint64_t vmaddr; // address of this segment
299 uint64_t vmsize; // size of this segment, may be larger than filesize
300 uint64_t fileoff; // offset in file
301 uint64_t filesize; // amount to read from file
302 uint32_t maxprot; // maximum VM protection
303 uint32_t initprot; // initial VM protection
304 uint32_t nsects; // number of sections in this segment
305 uint32_t flags; // flags
307 // The following constants are getting pulled in by one of the
308 // system headers, which creates a neat clash with the enum.
309 #if !defined(VM_PROT_NONE)
310 #define VM_PROT_NONE 0x00
312 #if !defined(VM_PROT_READ)
313 #define VM_PROT_READ 0x01
315 #if !defined(VM_PROT_WRITE)
316 #define VM_PROT_WRITE 0x02
318 #if !defined(VM_PROT_EXECUTE)
319 #define VM_PROT_EXECUTE 0x04
321 #if !defined(VM_PROT_ALL)
322 #define VM_PROT_ALL 0x07
325 // Constants for the vm protection fields
326 // see <mach-o/vm_prot.h>
327 enum { SEG_VM_PROT_NONE = VM_PROT_NONE,
328 SEG_VM_PROT_READ = VM_PROT_READ, // read permission
329 SEG_VM_PROT_WRITE = VM_PROT_WRITE, // write permission
330 SEG_VM_PROT_EXECUTE = VM_PROT_EXECUTE,
331 SEG_VM_PROT_ALL = VM_PROT_ALL
334 // Constants for the cmd field
335 // see <mach-o/loader.h>
336 enum { LC_SEGMENT = 0x01, // segment of this file to be mapped
337 LC_SEGMENT_64 = 0x19 // 64-bit segment of this file to be mapped
340 /// cmdSize - This routine returns the size of the MachOSection as written
341 /// to disk, depending on whether the destination is a 64 bit Mach-O file.
342 unsigned cmdSize(bool is64Bit) const {
344 return 6 * sizeof(uint32_t) + 4 * sizeof(uint64_t) + 16;
346 return 10 * sizeof(uint32_t) + 16; // addresses only 32 bits
349 MachOSegment(const std::string &seg, bool is64Bit)
350 : cmd(is64Bit ? LC_SEGMENT_64 : LC_SEGMENT), cmdsize(0), segname(seg),
351 vmaddr(0), vmsize(0), fileoff(0), filesize(0), maxprot(VM_PROT_ALL),
352 initprot(VM_PROT_ALL), nsects(0), flags(0) { }
355 /// MachORelocation - This struct contains information about each relocation
356 /// that needs to be emitted to the file.
357 /// see <mach-o/reloc.h>
358 struct MachORelocation {
359 uint32_t r_address; // offset in the section to what is being relocated
360 uint32_t r_symbolnum; // symbol index if r_extern == 1 else section index
361 bool r_pcrel; // was relocated pc-relative already
362 uint8_t r_length; // length = 2 ^ r_length
364 uint8_t r_type; // if not 0, machine-specific relocation type.
366 uint32_t getPackedFields() {
367 return (r_symbolnum << 8) | (r_pcrel << 7) | ((r_length & 3) << 5) |
368 (r_extern << 4) | (r_type & 15);
371 MachORelocation(uint32_t addr, uint32_t index, bool pcrel, uint8_t len,
372 bool ext, uint8_t type) : r_address(addr),
373 r_symbolnum(index), r_pcrel(pcrel), r_length(len), r_extern(ext),
377 /// MachOSection - This struct contains information about each section in a
378 /// particular segment that is emitted to the file. This is eventually
379 /// turned into the SectionCommand in the load command for a particlar
381 struct MachOSection {
382 std::string sectname; // name of this section,
383 std::string segname; // segment this section goes in
384 uint64_t addr; // memory address of this section
385 uint64_t size; // size in bytes of this section
386 uint32_t offset; // file offset of this section
387 uint32_t align; // section alignment (power of 2)
388 uint32_t reloff; // file offset of relocation entries
389 uint32_t nreloc; // number of relocation entries
390 uint32_t flags; // flags (section type and attributes)
391 uint32_t reserved1; // reserved (for offset or index)
392 uint32_t reserved2; // reserved (for count or sizeof)
393 uint32_t reserved3; // reserved (64 bit only)
395 /// A unique number for this section, which will be used to match symbols
396 /// to the correct section.
399 /// SectionData - The actual data for this section which we are building
400 /// up for emission to the file.
401 DataBuffer SectionData;
403 /// RelocBuffer - A buffer to hold the mach-o relocations before we write
404 /// them out at the appropriate location in the file.
405 DataBuffer RelocBuffer;
407 /// Relocations - The relocations that we have encountered so far in this
408 /// section that we will need to convert to MachORelocation entries when
409 /// the file is written.
410 std::vector<MachineRelocation> Relocations;
412 // Constants for the section types (low 8 bits of flags field)
413 // see <mach-o/loader.h>
414 enum { S_REGULAR = 0,
417 // zero fill on demand section
418 S_CSTRING_LITERALS = 2,
419 // section with only literal C strings
420 S_4BYTE_LITERALS = 3,
421 // section with only 4 byte literals
422 S_8BYTE_LITERALS = 4,
423 // section with only 8 byte literals
424 S_LITERAL_POINTERS = 5,
425 // section with only pointers to literals
426 S_NON_LAZY_SYMBOL_POINTERS = 6,
427 // section with only non-lazy symbol pointers
428 S_LAZY_SYMBOL_POINTERS = 7,
429 // section with only lazy symbol pointers
431 // section with only symbol stubs
432 // byte size of stub in the reserved2 field
433 S_MOD_INIT_FUNC_POINTERS = 9,
434 // section with only function pointers for initialization
435 S_MOD_TERM_FUNC_POINTERS = 10,
436 // section with only function pointers for termination
438 // section contains symbols that are coalesced
440 // zero fill on demand section (that can be larger than 4GB)
442 // section with only pairs of function pointers for interposing
443 S_16BYTE_LITERALS = 14
444 // section with only 16 byte literals
447 // Constants for the section flags (high 24 bits of flags field)
448 // see <mach-o/loader.h>
449 enum { S_ATTR_PURE_INSTRUCTIONS = 1 << 31,
450 // section contains only true machine instructions
451 S_ATTR_NO_TOC = 1 << 30,
452 // section contains coalesced symbols that are not to be in a
453 // ranlib table of contents
454 S_ATTR_STRIP_STATIC_SYMS = 1 << 29,
455 // ok to strip static symbols in this section in files with the
457 S_ATTR_NO_DEAD_STRIP = 1 << 28,
459 S_ATTR_LIVE_SUPPORT = 1 << 27,
460 // blocks are live if they reference live blocks
461 S_ATTR_SELF_MODIFYING_CODE = 1 << 26,
462 // used with i386 code stubs written on by dyld
463 S_ATTR_DEBUG = 1 << 25,
465 S_ATTR_SOME_INSTRUCTIONS = 1 << 10,
466 // section contains some machine instructions
467 S_ATTR_EXT_RELOC = 1 << 9,
468 // section has external relocation entries
469 S_ATTR_LOC_RELOC = 1 << 8
470 // section has local relocation entries
473 /// cmdSize - This routine returns the size of the MachOSection as written
474 /// to disk, depending on whether the destination is a 64 bit Mach-O file.
475 unsigned cmdSize(bool is64Bit) const {
477 return 7 * sizeof(uint32_t) + 2 * sizeof(uint64_t) + 32;
479 return 9 * sizeof(uint32_t) + 32; // addresses only 32 bits
482 MachOSection(const std::string &seg, const std::string §)
483 : sectname(sect), segname(seg), addr(0), size(0), offset(0), align(2),
484 reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0),
490 /// SectionList - This is the list of sections that we have emitted to the
491 /// file. Once the file has been completely built, the segment load command
492 /// SectionCommands are constructed from this info.
493 std::vector<MachOSection*> SectionList;
495 /// SectionLookup - This is a mapping from section name to SectionList entry
496 std::map<std::string, MachOSection*> SectionLookup;
498 /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
499 /// to aid in emitting relocations.
500 std::map<GlobalValue*, MachOSection*> GVSection;
502 /// GVOffset - This is a mapping from a GlobalValue to an offset from the
503 /// start of the section in which the GV resides, to aid in emitting
505 std::map<GlobalValue*, intptr_t> GVOffset;
507 /// getSection - Return the section with the specified name, creating a new
508 /// section if one does not already exist.
509 MachOSection *getSection(const std::string &seg, const std::string §,
510 unsigned Flags = 0) {
511 MachOSection *MOS = SectionLookup[seg+sect];
514 MOS = new MachOSection(seg, sect);
515 SectionList.push_back(MOS);
516 MOS->Index = SectionList.size();
517 MOS->flags = MachOSection::S_REGULAR | Flags;
518 SectionLookup[seg+sect] = MOS;
521 MachOSection *getTextSection(bool isCode = true) {
523 return getSection("__TEXT", "__text",
524 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
525 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
527 return getSection("__TEXT", "__text");
529 MachOSection *getBSSSection() {
530 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
532 MachOSection *getDataSection() {
533 return getSection("__DATA", "__data");
535 MachOSection *getConstSection(const Type *Ty) {
536 // FIXME: support cstring literals and pointer literal
537 if (Ty->isPrimitiveType()) {
538 unsigned Size = TM.getTargetData()->getTypeSize(Ty);
540 default: break; // Fall through to __TEXT,__const
542 return getSection("__TEXT", "__literal4",
543 MachOSection::S_4BYTE_LITERALS);
545 return getSection("__TEXT", "__literal8",
546 MachOSection::S_8BYTE_LITERALS);
548 return getSection("__TEXT", "__literal16",
549 MachOSection::S_16BYTE_LITERALS);
552 return getSection("__TEXT", "__const");
554 MachOSection *getJumpTableSection() {
555 if (TM.getRelocationModel() == Reloc::PIC_)
556 return getTextSection(false);
558 return getSection("__TEXT", "__const");
561 /// MachOSymTab - This struct contains information about the offsets and
562 /// size of symbol table information.
565 uint32_t cmd; // LC_SYMTAB
566 uint32_t cmdsize; // sizeof( MachOSymTab )
567 uint32_t symoff; // symbol table offset
568 uint32_t nsyms; // number of symbol table entries
569 uint32_t stroff; // string table offset
570 uint32_t strsize; // string table size in bytes
572 // Constants for the cmd field
573 // see <mach-o/loader.h>
574 enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info
577 MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
578 nsyms(0), stroff(0), strsize(0) { }
581 /// MachOSymTab - This struct contains information about the offsets and
582 /// size of symbol table information.
584 struct MachODySymTab {
585 uint32_t cmd; // LC_DYSYMTAB
586 uint32_t cmdsize; // sizeof( MachODySymTab )
587 uint32_t ilocalsym; // index to local symbols
588 uint32_t nlocalsym; // number of local symbols
589 uint32_t iextdefsym; // index to externally defined symbols
590 uint32_t nextdefsym; // number of externally defined symbols
591 uint32_t iundefsym; // index to undefined symbols
592 uint32_t nundefsym; // number of undefined symbols
593 uint32_t tocoff; // file offset to table of contents
594 uint32_t ntoc; // number of entries in table of contents
595 uint32_t modtaboff; // file offset to module table
596 uint32_t nmodtab; // number of module table entries
597 uint32_t extrefsymoff; // offset to referenced symbol table
598 uint32_t nextrefsyms; // number of referenced symbol table entries
599 uint32_t indirectsymoff; // file offset to the indirect symbol table
600 uint32_t nindirectsyms; // number of indirect symbol table entries
601 uint32_t extreloff; // offset to external relocation entries
602 uint32_t nextrel; // number of external relocation entries
603 uint32_t locreloff; // offset to local relocation entries
604 uint32_t nlocrel; // number of local relocation entries
606 // Constants for the cmd field
607 // see <mach-o/loader.h>
608 enum { LC_DYSYMTAB = 0x0B // dynamic link-edit symbol table info
611 MachODySymTab() : cmd(LC_DYSYMTAB), cmdsize(20 * sizeof(uint32_t)),
612 ilocalsym(0), nlocalsym(0), iextdefsym(0), nextdefsym(0),
613 iundefsym(0), nundefsym(0), tocoff(0), ntoc(0), modtaboff(0),
614 nmodtab(0), extrefsymoff(0), nextrefsyms(0), indirectsymoff(0),
615 nindirectsyms(0), extreloff(0), nextrel(0), locreloff(0), nlocrel(0) { }
618 /// SymTab - The "stab" style symbol table information
620 /// DySymTab - symbol table info for the dynamic link editor
621 MachODySymTab DySymTab;
624 // FIXME: this does not appear to be sorting 'f' after 'F'
625 bool operator()(const MachOSym &LHS, const MachOSym &RHS) {
626 return LHS.GVName < RHS.GVName;
630 /// PartitionByLocal - Simple boolean predicate that returns true if Sym is
631 /// a local symbol rather than an external symbol.
632 static bool PartitionByLocal(const MachOSym &Sym);
634 /// PartitionByDefined - Simple boolean predicate that returns true if Sym
635 /// is defined in this module.
636 static bool PartitionByDefined(const MachOSym &Sym);
640 /// SymbolTable - This is the list of symbols we have emitted to the file.
641 /// This actually gets rearranged before emission to the file (to put the
642 /// local symbols first in the list).
643 std::vector<MachOSym> SymbolTable;
645 /// SymT - A buffer to hold the symbol table before we write it out at the
646 /// appropriate location in the file.
649 /// StrT - A buffer to hold the string table before we write it out at the
650 /// appropriate location in the file.
653 /// PendingSyms - This is a list of externally defined symbols that we have
654 /// been asked to emit, but have not seen a reference to. When a reference
655 /// is seen, the symbol will move from this list to the SymbolTable.
656 std::vector<MachOSym> PendingSyms;
658 /// DynamicSymbolTable - This is just a vector of indices into
659 /// SymbolTable to aid in emitting the DYSYMTAB load command.
660 std::vector<unsigned> DynamicSymbolTable;
662 // align - Emit padding into the file until the current output position is
663 // aligned to the specified power of two boundary.
664 static void align(DataBuffer &Output, unsigned Boundary) {
665 assert(Boundary && (Boundary & (Boundary-1)) == 0 &&
666 "Must align to 2^k boundary");
667 size_t Size = Output.size();
668 if (Size & (Boundary-1)) {
669 // Add padding to get alignment to the correct place.
670 size_t Pad = Boundary-(Size & (Boundary-1));
671 Output.resize(Size+Pad);
675 void outbyte(DataBuffer &Output, unsigned char X) {
678 void outhalf(DataBuffer &Output, unsigned short X) {
679 if (isLittleEndian) {
680 Output.push_back(X&255);
681 Output.push_back(X >> 8);
683 Output.push_back(X >> 8);
684 Output.push_back(X&255);
687 void outword(DataBuffer &Output, unsigned X) {
688 if (isLittleEndian) {
689 Output.push_back((X >> 0) & 255);
690 Output.push_back((X >> 8) & 255);
691 Output.push_back((X >> 16) & 255);
692 Output.push_back((X >> 24) & 255);
694 Output.push_back((X >> 24) & 255);
695 Output.push_back((X >> 16) & 255);
696 Output.push_back((X >> 8) & 255);
697 Output.push_back((X >> 0) & 255);
700 void outxword(DataBuffer &Output, uint64_t X) {
701 if (isLittleEndian) {
702 Output.push_back(unsigned(X >> 0) & 255);
703 Output.push_back(unsigned(X >> 8) & 255);
704 Output.push_back(unsigned(X >> 16) & 255);
705 Output.push_back(unsigned(X >> 24) & 255);
706 Output.push_back(unsigned(X >> 32) & 255);
707 Output.push_back(unsigned(X >> 40) & 255);
708 Output.push_back(unsigned(X >> 48) & 255);
709 Output.push_back(unsigned(X >> 56) & 255);
711 Output.push_back(unsigned(X >> 56) & 255);
712 Output.push_back(unsigned(X >> 48) & 255);
713 Output.push_back(unsigned(X >> 40) & 255);
714 Output.push_back(unsigned(X >> 32) & 255);
715 Output.push_back(unsigned(X >> 24) & 255);
716 Output.push_back(unsigned(X >> 16) & 255);
717 Output.push_back(unsigned(X >> 8) & 255);
718 Output.push_back(unsigned(X >> 0) & 255);
721 void outaddr32(DataBuffer &Output, unsigned X) {
724 void outaddr64(DataBuffer &Output, uint64_t X) {
727 void outaddr(DataBuffer &Output, uint64_t X) {
729 outword(Output, (unsigned)X);
733 void outstring(DataBuffer &Output, std::string &S, unsigned Length) {
734 unsigned len_to_copy = S.length() < Length ? S.length() : Length;
735 unsigned len_to_fill = S.length() < Length ? Length-S.length() : 0;
737 for (unsigned i = 0; i < len_to_copy; ++i)
738 outbyte(Output, S[i]);
740 for (unsigned i = 0; i < len_to_fill; ++i)
744 void fixhalf(DataBuffer &Output, unsigned short X, unsigned Offset) {
745 unsigned char *P = &Output[Offset];
746 P[0] = (X >> (isLittleEndian ? 0 : 8)) & 255;
747 P[1] = (X >> (isLittleEndian ? 8 : 0)) & 255;
749 void fixword(DataBuffer &Output, unsigned X, unsigned Offset) {
750 unsigned char *P = &Output[Offset];
751 P[0] = (X >> (isLittleEndian ? 0 : 24)) & 255;
752 P[1] = (X >> (isLittleEndian ? 8 : 16)) & 255;
753 P[2] = (X >> (isLittleEndian ? 16 : 8)) & 255;
754 P[3] = (X >> (isLittleEndian ? 24 : 0)) & 255;
757 static void InitMem(const Constant *C, void *Addr, intptr_t Offset,
758 const TargetData *TD,
759 std::vector<MachineRelocation> &MRs);
762 void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
763 void EmitGlobal(GlobalVariable *GV);
764 void EmitHeaderAndLoadCommands();
766 void BufferSymbolAndStringTable();
767 void CalculateRelocations(MachOSection &MOS);
769 virtual MachineRelocation GetJTRelocation(unsigned Offset,
770 MachineBasicBlock *MBB) = 0;
771 virtual void GetTargetRelocation(MachineRelocation &MR, MachOSection &From,
772 MachOSection &To) = 0;