Remove a FIXME. Don't use strlcpy that isn't available on non-BSD platforms
[oota-llvm.git] / include / llvm / CodeGen / MachOWriter.h
1 //=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MachOWriter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHOWRITER_H
15 #define LLVM_CODEGEN_MACHOWRITER_H
16
17 #include "llvm/CodeGen/MachineRelocation.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include <list>
20
21 namespace llvm {
22   class GlobalVariable;
23   class Mangler;
24   class MachineCodeEmitter;
25   class MachOCodeEmitter;
26
27   /// MachOWriter - This class implements the common target-independent code for
28   /// writing Mach-O files.  Targets should derive a class from this to
29   /// parameterize the output format.
30   ///
31   class MachOWriter : public MachineFunctionPass {
32     friend class MachOCodeEmitter;
33   public:
34     MachineCodeEmitter &getMachineCodeEmitter() const {
35       return *(MachineCodeEmitter*)MCE;
36     }
37
38     ~MachOWriter();
39
40     typedef std::vector<unsigned char> DataBuffer;
41
42   protected:
43     MachOWriter(std::ostream &O, TargetMachine &TM);
44
45     /// Output stream to send the resultant object file to.
46     ///
47     std::ostream &O;
48
49     /// Target machine description.
50     ///
51     TargetMachine &TM;
52
53     /// Mang - The object used to perform name mangling for this module.
54     ///
55     Mangler *Mang;
56
57     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
58     /// code for functions to the .o file.
59     MachOCodeEmitter *MCE;
60
61     /// is64Bit/isLittleEndian - This information is inferred from the target
62     /// machine directly, indicating what header values and flags to set.
63     bool is64Bit, isLittleEndian;
64
65     /// doInitialization - Emit the file header and all of the global variables
66     /// for the module to the Mach-O file.
67     bool doInitialization(Module &M);
68
69     bool runOnMachineFunction(MachineFunction &MF);
70
71     /// doFinalization - Now that the module has been completely processed, emit
72     /// the Mach-O file to 'O'.
73     bool doFinalization(Module &M);
74
75     /// MachOHeader - This struct contains the header information about a
76     /// specific architecture type/subtype pair that is emitted to the file.
77     struct MachOHeader {
78       uint32_t  magic;      // mach magic number identifier
79       uint32_t  cputype;    // cpu specifier
80       uint32_t  cpusubtype; // machine specifier
81       uint32_t  filetype;   // type of file
82       uint32_t  ncmds;      // number of load commands
83       uint32_t  sizeofcmds; // the size of all the load commands
84       uint32_t  flags;      // flags
85       uint32_t  reserved;   // 64-bit only
86       
87       /// HeaderData - The actual data for the header which we are building
88       /// up for emission to the file.
89       DataBuffer HeaderData;
90
91       // Constants for the cputype field
92       // see <mach/machine.h>
93       enum { CPU_TYPE_I386      = 7,
94              CPU_TYPE_X86_64    = 7 | 0x1000000,
95              CPU_TYPE_ARM       = 12,
96              CPU_TYPE_SPARC     = 14,
97              CPU_TYPE_POWERPC   = 18,
98              CPU_TYPE_POWERPC64 = 18 | 0x1000000
99       };
100       
101       // Constants for the cpusubtype field
102       // see <mach/machine.h>
103       enum { CPU_SUBTYPE_I386_ALL    = 3,
104              CPU_SUBTYPE_X86_64_ALL  = 3,
105              CPU_SUBTYPE_ARM_ALL     = 0,
106              CPU_SUBTYPE_SPARC_ALL   = 0,
107              CPU_SUBTYPE_POWERPC_ALL = 0
108       };
109              
110       // Constants for the filetype field
111       // see <mach-o/loader.h> for additional info on the various types
112       enum { MH_OBJECT     = 1, // relocatable object file
113              MH_EXECUTE    = 2, // demand paged executable file
114              MH_FVMLIB     = 3, // fixed VM shared library file
115              MH_CORE       = 4, // core file
116              MH_PRELOAD    = 5, // preloaded executable file
117              MH_DYLIB      = 6, // dynamically bound shared library
118              MH_DYLINKER   = 7, // dynamic link editor
119              MH_BUNDLE     = 8, // dynamically bound bundle file
120              MH_DYLIB_STUB = 9, // shared library stub for static linking only
121              MH_DSYM       = 10 // companion file wiht only debug sections
122       };
123       
124       // Constants for the flags field
125       enum { MH_NOUNDEFS                = 1 << 0,
126                 // the object file has no undefined references
127              MH_INCRLINK                = 1 << 1,
128                 // the object file is the output of an incremental link against
129                 // a base file and cannot be link edited again
130              MH_DYLDLINK                = 1 << 2,
131                 // the object file is input for the dynamic linker and cannot be
132                 // statically link edited again.
133              MH_BINDATLOAD              = 1 << 3,
134                 // the object file's undefined references are bound by the
135                 // dynamic linker when loaded.
136              MH_PREBOUND                = 1 << 4,
137                 // the file has its dynamic undefined references prebound
138              MH_SPLIT_SEGS              = 1 << 5,
139                 // the file has its read-only and read-write segments split
140                 // see <mach/shared_memory_server.h>
141              MH_LAZY_INIT               = 1 << 6,
142                 // the shared library init routine is to be run lazily via
143                 // catching memory faults to its writable segments (obsolete)
144              MH_TWOLEVEL                = 1 << 7,
145                 // the image is using two-level namespace bindings
146              MH_FORCE_FLAT              = 1 << 8,
147                 // the executable is forcing all images to use flat namespace
148                 // bindings.
149              MH_NOMULTIDEFS             = 1 << 8,
150                 // this umbrella guarantees no multiple definitions of symbols
151                 // in its sub-images so the two-level namespace hints can
152                 // always be used.
153              MH_NOFIXPREBINDING         = 1 << 10,
154                 // do not have dyld notify the prebidning agent about this
155                 // executable.
156              MH_PREBINDABLE             = 1 << 11,
157                 // the binary is not prebound but can have its prebinding
158                 // redone.  only used when MH_PREBOUND is not set.
159              MH_ALLMODSBOUND            = 1 << 12,
160                 // indicates that this binary binds to all two-level namespace
161                 // modules of its dependent libraries.  Only used when
162                 // MH_PREBINDABLE and MH_TWOLEVEL are both set.
163              MH_SUBSECTIONS_VIA_SYMBOLS = 1 << 13,
164                 // safe to divide up the sections into sub-sections via symbols
165                 // for dead code stripping.
166              MH_CANONICAL               = 1 << 14,
167                 // the binary has been canonicalized via the unprebind operation
168              MH_WEAK_DEFINES            = 1 << 15,
169                 // the final linked image contains external weak symbols
170              MH_BINDS_TO_WEAK           = 1 << 16,
171                 // the final linked image uses weak symbols
172              MH_ALLOW_STACK_EXECUTION   = 1 << 17
173                 // When this bit is set, all stacks in the task will be given
174                 // stack execution privilege.  Only used in MH_EXECUTE filetype
175       };
176
177       MachOHeader() : magic(0), cputype(0), cpusubtype(0), filetype(0),
178                       ncmds(0), sizeofcmds(0), flags(0), reserved(0) { }
179       
180       /// cmdSize - This routine returns the size of the MachOSection as written
181       /// to disk, depending on whether the destination is a 64 bit Mach-O file.
182       unsigned cmdSize(bool is64Bit) const {
183         if (is64Bit)
184           return 8 * sizeof(uint32_t);
185         else
186           return 7 * sizeof(uint32_t);
187       }
188
189       /// setMagic - This routine sets the appropriate value for the 'magic'
190       /// field based on pointer size and endianness.
191       void setMagic(bool isLittleEndian, bool is64Bit) {
192         if (isLittleEndian)
193           if (is64Bit) magic = 0xcffaedfe;
194           else         magic = 0xcefaedfe;
195         else
196           if (is64Bit) magic = 0xfeedfacf;
197           else         magic = 0xfeedface;
198       }
199     };
200     
201     /// Header - An instance of MachOHeader that we will update while we build
202     /// the file, and then emit during finalization.
203     MachOHeader Header;
204     
205   private:
206
207     /// MachOSegment - This struct contains the necessary information to
208     /// emit the load commands for each section in the file.
209     struct MachOSegment {
210       uint32_t    cmd;      // LC_SEGMENT or LC_SEGMENT_64
211       uint32_t    cmdsize;  // Total size of this struct and section commands
212       std::string segname;  // segment name
213       uint64_t    vmaddr;   // address of this segment
214       uint64_t    vmsize;   // size of this segment, may be larger than filesize
215       uint64_t    fileoff;  // offset in file
216       uint64_t    filesize; // amount to read from file
217       uint32_t    maxprot;  // maximum VM protection
218       uint32_t    initprot; // initial VM protection
219       uint32_t    nsects;   // number of sections in this segment
220       uint32_t    flags;    // flags
221       
222       // Constants for the vm protection fields
223       // see <mach-o/vm_prot.h>
224       enum { VM_PROT_NONE    = 0x00, 
225              VM_PROT_READ    = 0x01, // read permission
226              VM_PROT_WRITE   = 0x02, // write permission
227              VM_PROT_EXECUTE = 0x04, // execute permission,
228              VM_PROT_ALL     = 0x07
229       };
230       
231       // Constants for the cmd field
232       // see <mach-o/loader.h>
233       enum { LC_SEGMENT    = 0x01,  // segment of this file to be mapped
234              LC_SEGMENT_64 = 0x19   // 64-bit segment of this file to be mapped
235       };
236       
237       /// cmdSize - This routine returns the size of the MachOSection as written
238       /// to disk, depending on whether the destination is a 64 bit Mach-O file.
239       unsigned cmdSize(bool is64Bit) const {
240         if (is64Bit)
241           return 6 * sizeof(uint32_t) + 4 * sizeof(uint64_t) + 16;
242         else
243           return 10 * sizeof(uint32_t) + 16;  // addresses only 32 bits
244       }
245
246       MachOSegment(const std::string &seg, bool is64Bit)
247         : cmd(is64Bit ? LC_SEGMENT_64 : LC_SEGMENT), cmdsize(0), segname(seg),
248           vmaddr(0), vmsize(0), fileoff(0), filesize(0), maxprot(VM_PROT_ALL),
249           initprot(VM_PROT_ALL), nsects(0), flags(0) { }
250     };
251
252     /// MachOSection - This struct contains information about each section in a 
253     /// particular segment that is emitted to the file.  This is eventually
254     /// turned into the SectionCommand in the load command for a particlar
255     /// segment.
256     struct MachOSection { 
257       std::string  sectname; // name of this section, 
258       std::string  segname;  // segment this section goes in
259       uint64_t  addr;        // memory address of this section
260       uint64_t  size;        // size in bytes of this section
261       uint32_t  offset;      // file offset of this section
262       uint32_t  align;       // section alignment (power of 2)
263       uint32_t  reloff;      // file offset of relocation entries
264       uint32_t  nreloc;      // number of relocation entries
265       uint32_t  flags;       // flags (section type and attributes)
266       uint32_t  reserved1;   // reserved (for offset or index)
267       uint32_t  reserved2;   // reserved (for count or sizeof)
268       uint32_t  reserved3;   // reserved (64 bit only)
269       
270       /// A unique number for this section, which will be used to match symbols
271       /// to the correct section.
272       uint32_t Index;
273       
274       /// SectionData - The actual data for this section which we are building
275       /// up for emission to the file.
276       DataBuffer SectionData;
277       
278       // Constants for the section types (low 8 bits of flags field)
279       // see <mach-o/loader.h>
280       enum { S_REGULAR = 0,
281                 // regular section
282              S_ZEROFILL = 1,
283                 // zero fill on demand section
284              S_CSTRING_LITERALS = 2,
285                 // section with only literal C strings
286              S_4BYTE_LITERALS = 3,
287                 // section with only 4 byte literals
288              S_8BYTE_LITERALS = 4,
289                 // section with only 8 byte literals
290              S_LITERAL_POINTERS = 5, 
291                 // section with only pointers to literals
292              S_NON_LAZY_SYMBOL_POINTERS = 6,
293                 // section with only non-lazy symbol pointers
294              S_LAZY_SYMBOL_POINTERS = 7,
295                 // section with only lazy symbol pointers
296              S_SYMBOL_STUBS = 8,
297                 // section with only symbol stubs
298                 // byte size of stub in the reserved2 field
299              S_MOD_INIT_FUNC_POINTERS = 9,
300                 // section with only function pointers for initialization
301              S_MOD_TERM_FUNC_POINTERS = 10,
302                 // section with only function pointers for termination
303              S_COALESCED = 11,
304                 // section contains symbols that are coalesced
305              S_GB_ZEROFILL = 12,
306                 // zero fill on demand section (that can be larger than 4GB)
307              S_INTERPOSING = 13,
308                 // section with only pairs of function pointers for interposing
309              S_16BYTE_LITERALS = 14
310                 // section with only 16 byte literals
311       };
312       
313       // Constants for the section flags (high 24 bits of flags field)
314       // see <mach-o/loader.h>
315       enum { S_ATTR_PURE_INSTRUCTIONS   = 1 << 31,
316                 // section contains only true machine instructions
317              S_ATTR_NO_TOC              = 1 << 30,
318                 // section contains coalesced symbols that are not to be in a 
319                 // ranlib table of contents
320              S_ATTR_STRIP_STATIC_SYMS   = 1 << 29,
321                 // ok to strip static symbols in this section in files with the
322                 // MY_DYLDLINK flag
323              S_ATTR_NO_DEAD_STRIP       = 1 << 28,
324                 // no dead stripping
325              S_ATTR_LIVE_SUPPORT        = 1 << 27,
326                 // blocks are live if they reference live blocks
327              S_ATTR_SELF_MODIFYING_CODE = 1 << 26,
328                 // used with i386 code stubs written on by dyld
329              S_ATTR_DEBUG               = 1 << 25,
330                 // a debug section
331              S_ATTR_SOME_INSTRUCTIONS   = 1 << 10,
332                 // section contains some machine instructions
333              S_ATTR_EXT_RELOC           = 1 << 9,
334                 // section has external relocation entries
335              S_ATTR_LOC_RELOC           = 1 << 8
336                 // section has local relocation entries
337       };
338
339       /// cmdSize - This routine returns the size of the MachOSection as written
340       /// to disk, depending on whether the destination is a 64 bit Mach-O file.
341       unsigned cmdSize(bool is64Bit) const {
342         if (is64Bit)
343           return 7 * sizeof(uint32_t) + 2 * sizeof(uint64_t) + 32;
344         else
345           return 9 * sizeof(uint32_t) + 32;  // addresses only 32 bits
346       }
347
348       MachOSection(const std::string &seg, const std::string &sect)
349         : sectname(sect), segname(seg), addr(0), size(0), offset(0), align(0),
350           reloff(0), nreloc(0), flags(0), reserved1(0), reserved2(0),
351           reserved3(0) { }
352     };
353
354     /// SectionList - This is the list of sections that we have emitted to the
355     /// file.  Once the file has been completely built, the segment load command
356     /// SectionCommands are constructed from this info.
357     std::list<MachOSection> SectionList;
358
359     /// SectionLookup - This is a mapping from section name to SectionList entry
360     std::map<std::string, MachOSection*> SectionLookup;
361
362     /// getSection - Return the section with the specified name, creating a new
363     /// section if one does not already exist.
364     MachOSection &getSection(const std::string &seg, const std::string &sect,
365                              unsigned Flags = 0) {
366       MachOSection *&SN = SectionLookup[seg+sect];
367       if (SN) return *SN;
368
369       SectionList.push_back(MachOSection(seg, sect));
370       SN = &SectionList.back();
371       SN->Index = SectionList.size();
372       SN->flags = MachOSection::S_REGULAR | Flags;
373       return *SN;
374     }
375     MachOSection &getTextSection() {
376       return getSection("__TEXT", "__text", 
377                         MachOSection::S_ATTR_PURE_INSTRUCTIONS |
378                         MachOSection::S_ATTR_SOME_INSTRUCTIONS);
379     }
380     
381     /// MachOSymTab - This struct contains information about the offsets and 
382     /// size of symbol table information.
383     /// segment.
384     struct MachOSymTab {
385       uint32_t cmd;     // LC_SYMTAB
386       uint32_t cmdsize; // sizeof( MachOSymTab )
387       uint32_t symoff;  // symbol table offset
388       uint32_t nsyms;   // number of symbol table entries
389       uint32_t stroff;  // string table offset
390       uint32_t strsize; // string table size in bytes
391
392       // Constants for the cmd field
393       // see <mach-o/loader.h>
394       enum { LC_SYMTAB = 0x02  // link-edit stab symbol table info
395       };
396       
397       MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
398         nsyms(0), stroff(0), strsize(0) { }
399     };
400     
401     /// MachOSymTab - This struct contains information about the offsets and 
402     /// size of symbol table information.
403     /// segment.
404     struct MachODySymTab {
405       uint32_t cmd;             // LC_DYSYMTAB
406       uint32_t cmdsize;         // sizeof( MachODySymTab )
407       uint32_t ilocalsym;       // index to local symbols
408       uint32_t nlocalsym;       // number of local symbols
409       uint32_t iextdefsym;      // index to externally defined symbols
410       uint32_t nextdefsym;      // number of externally defined symbols
411       uint32_t iundefsym;       // index to undefined symbols
412       uint32_t nundefsym;       // number of undefined symbols
413       uint32_t tocoff;          // file offset to table of contents
414       uint32_t ntoc;            // number of entries in table of contents
415       uint32_t modtaboff;       // file offset to module table
416       uint32_t nmodtab;         // number of module table entries
417       uint32_t extrefsymoff;    // offset to referenced symbol table
418       uint32_t nextrefsyms;     // number of referenced symbol table entries
419       uint32_t indirectsymoff;  // file offset to the indirect symbol table
420       uint32_t nindirectsyms;   // number of indirect symbol table entries
421       uint32_t extreloff;       // offset to external relocation entries
422       uint32_t nextrel;         // number of external relocation entries
423       uint32_t locreloff;       // offset to local relocation entries
424       uint32_t nlocrel;         // number of local relocation entries
425
426       // Constants for the cmd field
427       // see <mach-o/loader.h>
428       enum { LC_DYSYMTAB = 0x0B  // dynamic link-edit symbol table info
429       };
430       
431       MachODySymTab() : cmd(LC_DYSYMTAB), cmdsize(20 * sizeof(uint32_t)),
432         ilocalsym(0), nlocalsym(0), iextdefsym(0), nextdefsym(0),
433         iundefsym(0), nundefsym(0), tocoff(0), ntoc(0), modtaboff(0),
434         nmodtab(0), extrefsymoff(0), nextrefsyms(0), indirectsymoff(0),
435         nindirectsyms(0), extreloff(0), nextrel(0), locreloff(0), nlocrel(0) { }
436     };
437     
438     /// SymTab - The "stab" style symbol table information
439     MachOSymTab   SymTab;     
440     /// DySymTab - symbol table info for the dynamic link editor
441     MachODySymTab DySymTab;
442
443     /// MachOSym - This struct contains information about each symbol that is
444     /// added to logical symbol table for the module.  This is eventually
445     /// turned into a real symbol table in the file.
446     struct MachOSym {
447       const GlobalValue *GV;    // The global value this corresponds to.
448       uint32_t  n_strx;         // index into the string table
449       uint8_t   n_type;         // type flag
450       uint8_t   n_sect;         // section number or NO_SECT
451       int16_t   n_desc;         // see <mach-o/stab.h>
452       uint64_t  n_value;        // value for this symbol (or stab offset)
453       
454       // Constants for the n_sect field
455       // see <mach-o/nlist.h>
456       enum { NO_SECT = 0 };   // symbol is not in any section
457
458       // Constants for the n_type field
459       // see <mach-o/nlist.h>
460       enum { N_UNDF  = 0x0,  // undefined, n_sect == NO_SECT
461              N_ABS   = 0x2,  // absolute, n_sect == NO_SECT
462              N_SECT  = 0xe,  // defined in section number n_sect
463              N_PBUD  = 0xc,  // prebound undefined (defined in a dylib)
464              N_INDR  = 0xa   // indirect
465       };
466       // The following bits are OR'd into the types above. For example, a type
467       // of 0x0f would be an external N_SECT symbol (0x0e | 0x01).
468       enum { N_EXT  = 0x01,   // external symbol bit
469              N_PEXT = 0x10    // private external symbol bit
470       };
471       
472       // Constants for the n_desc field
473       // see <mach-o/loader.h>
474       enum { REFERENCE_FLAG_UNDEFINED_NON_LAZY          = 0,
475              REFERENCE_FLAG_UNDEFINED_LAZY              = 1,
476              REFERENCE_FLAG_DEFINED                     = 2,
477              REFERENCE_FLAG_PRIVATE_DEFINED             = 3,
478              REFERENCE_FLAG_PRIVATE_UNDEFINED_NON_LAZY  = 4,
479              REFERENCE_FLAG_PRIVATE_UNDEFINED_LAZY      = 5
480       };
481       enum { N_NO_DEAD_STRIP = 0x0020, // symbol is not to be dead stripped
482              N_WEAK_REF      = 0x0040, // symbol is weak referenced
483              N_WEAK_DEF      = 0x0080  // coalesced symbol is a weak definition
484       };
485       
486       /// entrySize - This routine returns the size of a symbol table entry as
487       /// written to disk.
488       static unsigned entrySize() { return 12; }
489
490       MachOSym(const GlobalValue *gv, uint8_t sect) : GV(gv), n_strx(0), 
491         n_type(N_UNDF), n_sect(sect), n_desc(0), n_value(0) {}
492     };
493
494     /// SymbolTable - This is the list of symbols we have emitted to the file.
495     /// This actually gets rearranged before emission to the file (to put the
496     /// local symbols first in the list).
497     std::vector<MachOSym> SymbolTable;
498     
499     /// DynamicSymbolTable - This is just a vector of indices into
500     /// SymbolTable to aid in emitting the DYSYMTAB load command.
501     std::vector<unsigned> DynamicSymbolTable;
502     
503     /// StringTable - The table of strings referenced by SymbolTable entries
504     std::vector<std::string> StringTable;
505
506     // align - Emit padding into the file until the current output position is
507     // aligned to the specified power of two boundary.
508     static void align(DataBuffer &Output, unsigned Boundary) {
509       assert(Boundary && (Boundary & (Boundary-1)) == 0 &&
510              "Must align to 2^k boundary");
511       size_t Size = Output.size();
512       if (Size & (Boundary-1)) {
513         // Add padding to get alignment to the correct place.
514         size_t Pad = Boundary-(Size & (Boundary-1));
515         Output.resize(Size+Pad);
516       }
517     }
518
519     void outbyte(DataBuffer &Output, unsigned char X) {
520       Output.push_back(X);
521     }
522     void outhalf(DataBuffer &Output, unsigned short X) {
523       if (isLittleEndian) {
524         Output.push_back(X&255);
525         Output.push_back(X >> 8);
526       } else {
527         Output.push_back(X >> 8);
528         Output.push_back(X&255);
529       }
530     }
531     void outword(DataBuffer &Output, unsigned X) {
532       if (isLittleEndian) {
533         Output.push_back((X >>  0) & 255);
534         Output.push_back((X >>  8) & 255);
535         Output.push_back((X >> 16) & 255);
536         Output.push_back((X >> 24) & 255);
537       } else {
538         Output.push_back((X >> 24) & 255);
539         Output.push_back((X >> 16) & 255);
540         Output.push_back((X >>  8) & 255);
541         Output.push_back((X >>  0) & 255);
542       }
543     }
544     void outxword(DataBuffer &Output, uint64_t X) {
545       if (isLittleEndian) {
546         Output.push_back(unsigned(X >>  0) & 255);
547         Output.push_back(unsigned(X >>  8) & 255);
548         Output.push_back(unsigned(X >> 16) & 255);
549         Output.push_back(unsigned(X >> 24) & 255);
550         Output.push_back(unsigned(X >> 32) & 255);
551         Output.push_back(unsigned(X >> 40) & 255);
552         Output.push_back(unsigned(X >> 48) & 255);
553         Output.push_back(unsigned(X >> 56) & 255);
554       } else {
555         Output.push_back(unsigned(X >> 56) & 255);
556         Output.push_back(unsigned(X >> 48) & 255);
557         Output.push_back(unsigned(X >> 40) & 255);
558         Output.push_back(unsigned(X >> 32) & 255);
559         Output.push_back(unsigned(X >> 24) & 255);
560         Output.push_back(unsigned(X >> 16) & 255);
561         Output.push_back(unsigned(X >>  8) & 255);
562         Output.push_back(unsigned(X >>  0) & 255);
563       }
564     }
565     void outaddr32(DataBuffer &Output, unsigned X) {
566       outword(Output, X);
567     }
568     void outaddr64(DataBuffer &Output, uint64_t X) {
569       outxword(Output, X);
570     }
571     void outaddr(DataBuffer &Output, uint64_t X) {
572       if (!is64Bit)
573         outword(Output, (unsigned)X);
574       else
575         outxword(Output, X);
576     }
577     void outstring(DataBuffer &Output, std::string &S, unsigned Length) {
578       unsigned len_to_copy = S.length() < Length ? S.length() : Length;
579       unsigned len_to_fill = S.length() < Length ? Length-S.length() : 0;
580       
581       for (unsigned i = 0; i < len_to_copy; ++i)
582         outbyte(Output, S[i]);
583
584       for (unsigned i = 0; i < len_to_fill; ++i)
585         outbyte(Output, 0);
586       
587     }
588   private:
589     void EmitGlobal(GlobalVariable *GV);
590     void EmitHeaderAndLoadCommands();
591     void EmitSections();
592     void EmitRelocations();
593     void EmitSymbolTable();
594     void EmitStringTable();
595   };
596 }
597
598 #endif