MCAssembler: Switch MCFillFragment to only taking constant values. Symbolic expressio...
[oota-llvm.git] / lib / MC / MCAssembler.cpp
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #define DEBUG_TYPE "assembler"
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCExpr.h"
13 #include "llvm/MC/MCSectionMachO.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MachO.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Support/Debug.h"
26 #include <vector>
27 using namespace llvm;
28
29 class MachObjectWriter;
30
31 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
32
33 // FIXME FIXME FIXME: There are number of places in this file where we convert
34 // what is a 64-bit assembler value used for computation into a value in the
35 // object file, which may truncate it. We should detect that truncation where
36 // invalid and report errors back.
37
38 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
39                           MachObjectWriter &MOW);
40
41 /// isVirtualSection - Check if this is a section which does not actually exist
42 /// in the object file.
43 static bool isVirtualSection(const MCSection &Section) {
44   // FIXME: Lame.
45   const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
46   unsigned Type = SMO.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
47   return (Type == MCSectionMachO::S_ZEROFILL);
48 }
49
50 class MachObjectWriter {
51   // See <mach-o/loader.h>.
52   enum {
53     Header_Magic32 = 0xFEEDFACE,
54     Header_Magic64 = 0xFEEDFACF
55   };
56
57   static const unsigned Header32Size = 28;
58   static const unsigned Header64Size = 32;
59   static const unsigned SegmentLoadCommand32Size = 56;
60   static const unsigned Section32Size = 68;
61   static const unsigned SymtabLoadCommandSize = 24;
62   static const unsigned DysymtabLoadCommandSize = 80;
63   static const unsigned Nlist32Size = 12;
64   static const unsigned RelocationInfoSize = 8;
65
66   enum HeaderFileType {
67     HFT_Object = 0x1
68   };
69
70   enum HeaderFlags {
71     HF_SubsectionsViaSymbols = 0x2000
72   };
73
74   enum LoadCommandType {
75     LCT_Segment = 0x1,
76     LCT_Symtab = 0x2,
77     LCT_Dysymtab = 0xb
78   };
79
80   // See <mach-o/nlist.h>.
81   enum SymbolTypeType {
82     STT_Undefined = 0x00,
83     STT_Absolute  = 0x02,
84     STT_Section   = 0x0e
85   };
86
87   enum SymbolTypeFlags {
88     // If any of these bits are set, then the entry is a stab entry number (see
89     // <mach-o/stab.h>. Otherwise the other masks apply.
90     STF_StabsEntryMask = 0xe0,
91
92     STF_TypeMask       = 0x0e,
93     STF_External       = 0x01,
94     STF_PrivateExtern  = 0x10
95   };
96
97   /// IndirectSymbolFlags - Flags for encoding special values in the indirect
98   /// symbol entry.
99   enum IndirectSymbolFlags {
100     ISF_Local    = 0x80000000,
101     ISF_Absolute = 0x40000000
102   };
103
104   /// RelocationFlags - Special flags for addresses.
105   enum RelocationFlags {
106     RF_Scattered = 0x80000000
107   };
108
109   enum RelocationInfoType {
110     RIT_Vanilla             = 0,
111     RIT_Pair                = 1,
112     RIT_Difference          = 2,
113     RIT_PreboundLazyPointer = 3,
114     RIT_LocalDifference     = 4
115   };
116
117   /// MachSymbolData - Helper struct for containing some precomputed information
118   /// on symbols.
119   struct MachSymbolData {
120     MCSymbolData *SymbolData;
121     uint64_t StringIndex;
122     uint8_t SectionIndex;
123
124     // Support lexicographic sorting.
125     bool operator<(const MachSymbolData &RHS) const {
126       const std::string &Name = SymbolData->getSymbol().getName();
127       return Name < RHS.SymbolData->getSymbol().getName();
128     }
129   };
130
131   raw_ostream &OS;
132   bool IsLSB;
133
134 public:
135   MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true)
136     : OS(_OS), IsLSB(_IsLSB) {
137   }
138
139   /// @name Helper Methods
140   /// @{
141
142   void Write8(uint8_t Value) {
143     OS << char(Value);
144   }
145
146   void Write16(uint16_t Value) {
147     if (IsLSB) {
148       Write8(uint8_t(Value >> 0));
149       Write8(uint8_t(Value >> 8));
150     } else {
151       Write8(uint8_t(Value >> 8));
152       Write8(uint8_t(Value >> 0));
153     }
154   }
155
156   void Write32(uint32_t Value) {
157     if (IsLSB) {
158       Write16(uint16_t(Value >> 0));
159       Write16(uint16_t(Value >> 16));
160     } else {
161       Write16(uint16_t(Value >> 16));
162       Write16(uint16_t(Value >> 0));
163     }
164   }
165
166   void Write64(uint64_t Value) {
167     if (IsLSB) {
168       Write32(uint32_t(Value >> 0));
169       Write32(uint32_t(Value >> 32));
170     } else {
171       Write32(uint32_t(Value >> 32));
172       Write32(uint32_t(Value >> 0));
173     }
174   }
175
176   void WriteZeros(unsigned N) {
177     const char Zeros[16] = { 0 };
178
179     for (unsigned i = 0, e = N / 16; i != e; ++i)
180       OS << StringRef(Zeros, 16);
181
182     OS << StringRef(Zeros, N % 16);
183   }
184
185   void WriteString(StringRef Str, unsigned ZeroFillSize = 0) {
186     OS << Str;
187     if (ZeroFillSize)
188       WriteZeros(ZeroFillSize - Str.size());
189   }
190
191   /// @}
192
193   void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize,
194                      bool SubsectionsViaSymbols) {
195     uint32_t Flags = 0;
196
197     if (SubsectionsViaSymbols)
198       Flags |= HF_SubsectionsViaSymbols;
199
200     // struct mach_header (28 bytes)
201
202     uint64_t Start = OS.tell();
203     (void) Start;
204
205     Write32(Header_Magic32);
206
207     // FIXME: Support cputype.
208     Write32(MachO::CPUTypeI386);
209     // FIXME: Support cpusubtype.
210     Write32(MachO::CPUSubType_I386_ALL);
211     Write32(HFT_Object);
212     Write32(NumLoadCommands);    // Object files have a single load command, the
213                                  // segment.
214     Write32(LoadCommandsSize);
215     Write32(Flags);
216
217     assert(OS.tell() - Start == Header32Size);
218   }
219
220   /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command.
221   ///
222   /// \arg NumSections - The number of sections in this segment.
223   /// \arg SectionDataSize - The total size of the sections.
224   void WriteSegmentLoadCommand32(unsigned NumSections,
225                                  uint64_t VMSize,
226                                  uint64_t SectionDataStartOffset,
227                                  uint64_t SectionDataSize) {
228     // struct segment_command (56 bytes)
229
230     uint64_t Start = OS.tell();
231     (void) Start;
232
233     Write32(LCT_Segment);
234     Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
235
236     WriteString("", 16);
237     Write32(0); // vmaddr
238     Write32(VMSize); // vmsize
239     Write32(SectionDataStartOffset); // file offset
240     Write32(SectionDataSize); // file size
241     Write32(0x7); // maxprot
242     Write32(0x7); // initprot
243     Write32(NumSections);
244     Write32(0); // flags
245
246     assert(OS.tell() - Start == SegmentLoadCommand32Size);
247   }
248
249   void WriteSection32(const MCSectionData &SD, uint64_t FileOffset,
250                       uint64_t RelocationsStart, unsigned NumRelocations) {
251     // The offset is unused for virtual sections.
252     if (isVirtualSection(SD.getSection())) {
253       assert(SD.getFileSize() == 0 && "Invalid file size!");
254       FileOffset = 0;
255     }
256
257     // struct section (68 bytes)
258
259     uint64_t Start = OS.tell();
260     (void) Start;
261
262     // FIXME: cast<> support!
263     const MCSectionMachO &Section =
264       static_cast<const MCSectionMachO&>(SD.getSection());
265     WriteString(Section.getSectionName(), 16);
266     WriteString(Section.getSegmentName(), 16);
267     Write32(SD.getAddress()); // address
268     Write32(SD.getSize()); // size
269     Write32(FileOffset);
270
271     unsigned Flags = Section.getTypeAndAttributes();
272     if (SD.hasInstructions())
273       Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
274
275     assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
276     Write32(Log2_32(SD.getAlignment()));
277     Write32(NumRelocations ? RelocationsStart : 0);
278     Write32(NumRelocations);
279     Write32(Flags);
280     Write32(0); // reserved1
281     Write32(Section.getStubSize()); // reserved2
282
283     assert(OS.tell() - Start == Section32Size);
284   }
285
286   void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
287                               uint32_t StringTableOffset,
288                               uint32_t StringTableSize) {
289     // struct symtab_command (24 bytes)
290
291     uint64_t Start = OS.tell();
292     (void) Start;
293
294     Write32(LCT_Symtab);
295     Write32(SymtabLoadCommandSize);
296     Write32(SymbolOffset);
297     Write32(NumSymbols);
298     Write32(StringTableOffset);
299     Write32(StringTableSize);
300
301     assert(OS.tell() - Start == SymtabLoadCommandSize);
302   }
303
304   void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
305                                 uint32_t NumLocalSymbols,
306                                 uint32_t FirstExternalSymbol,
307                                 uint32_t NumExternalSymbols,
308                                 uint32_t FirstUndefinedSymbol,
309                                 uint32_t NumUndefinedSymbols,
310                                 uint32_t IndirectSymbolOffset,
311                                 uint32_t NumIndirectSymbols) {
312     // struct dysymtab_command (80 bytes)
313
314     uint64_t Start = OS.tell();
315     (void) Start;
316
317     Write32(LCT_Dysymtab);
318     Write32(DysymtabLoadCommandSize);
319     Write32(FirstLocalSymbol);
320     Write32(NumLocalSymbols);
321     Write32(FirstExternalSymbol);
322     Write32(NumExternalSymbols);
323     Write32(FirstUndefinedSymbol);
324     Write32(NumUndefinedSymbols);
325     Write32(0); // tocoff
326     Write32(0); // ntoc
327     Write32(0); // modtaboff
328     Write32(0); // nmodtab
329     Write32(0); // extrefsymoff
330     Write32(0); // nextrefsyms
331     Write32(IndirectSymbolOffset);
332     Write32(NumIndirectSymbols);
333     Write32(0); // extreloff
334     Write32(0); // nextrel
335     Write32(0); // locreloff
336     Write32(0); // nlocrel
337
338     assert(OS.tell() - Start == DysymtabLoadCommandSize);
339   }
340
341   void WriteNlist32(MachSymbolData &MSD) {
342     MCSymbolData &Data = *MSD.SymbolData;
343     const MCSymbol &Symbol = Data.getSymbol();
344     uint8_t Type = 0;
345     uint16_t Flags = Data.getFlags();
346     uint32_t Address = 0;
347
348     // Set the N_TYPE bits. See <mach-o/nlist.h>.
349     //
350     // FIXME: Are the prebound or indirect fields possible here?
351     if (Symbol.isUndefined())
352       Type = STT_Undefined;
353     else if (Symbol.isAbsolute())
354       Type = STT_Absolute;
355     else
356       Type = STT_Section;
357
358     // FIXME: Set STAB bits.
359
360     if (Data.isPrivateExtern())
361       Type |= STF_PrivateExtern;
362
363     // Set external bit.
364     if (Data.isExternal() || Symbol.isUndefined())
365       Type |= STF_External;
366
367     // Compute the symbol address.
368     if (Symbol.isDefined()) {
369       if (Symbol.isAbsolute()) {
370         llvm_unreachable("FIXME: Not yet implemented!");
371       } else {
372         Address = Data.getFragment()->getAddress() + Data.getOffset();
373       }
374     } else if (Data.isCommon()) {
375       // Common symbols are encoded with the size in the address
376       // field, and their alignment in the flags.
377       Address = Data.getCommonSize();
378
379       // Common alignment is packed into the 'desc' bits.
380       if (unsigned Align = Data.getCommonAlignment()) {
381         unsigned Log2Size = Log2_32(Align);
382         assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
383         if (Log2Size > 15)
384           llvm_report_error("invalid 'common' alignment '" +
385                             Twine(Align) + "'");
386         // FIXME: Keep this mask with the SymbolFlags enumeration.
387         Flags = (Flags & 0xF0FF) | (Log2Size << 8);
388       }
389     }
390
391     // struct nlist (12 bytes)
392
393     Write32(MSD.StringIndex);
394     Write8(Type);
395     Write8(MSD.SectionIndex);
396
397     // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
398     // value.
399     Write16(Flags);
400     Write32(Address);
401   }
402
403   struct MachRelocationEntry {
404     uint32_t Word0;
405     uint32_t Word1;
406   };
407   void ComputeScatteredRelocationInfo(MCAssembler &Asm, MCFragment &Fragment,
408                                       MCAsmFixup &Fixup,
409                                       const MCValue &Target,
410                              DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
411                                      std::vector<MachRelocationEntry> &Relocs) {
412     uint32_t Address = Fragment.getOffset() + Fixup.Offset;
413     unsigned IsPCRel = 0;
414     unsigned Type = RIT_Vanilla;
415
416     // See <reloc.h>.
417     const MCSymbol *A = Target.getSymA();
418     MCSymbolData *SD = SymbolMap.lookup(A);
419     uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset();
420     uint32_t Value2 = 0;
421
422     if (const MCSymbol *B = Target.getSymB()) {
423       Type = RIT_LocalDifference;
424
425       MCSymbolData *SD = SymbolMap.lookup(B);
426       Value2 = SD->getFragment()->getAddress() + SD->getOffset();
427     }
428
429     unsigned Log2Size = Log2_32(Fixup.Size);
430     assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
431
432     // The value which goes in the fixup is current value of the expression.
433     Fixup.FixedValue = Value - Value2 + Target.getConstant();
434
435     MachRelocationEntry MRE;
436     MRE.Word0 = ((Address   <<  0) |
437                  (Type      << 24) |
438                  (Log2Size  << 28) |
439                  (IsPCRel   << 30) |
440                  RF_Scattered);
441     MRE.Word1 = Value;
442     Relocs.push_back(MRE);
443
444     if (Type == RIT_LocalDifference) {
445       Type = RIT_Pair;
446
447       MachRelocationEntry MRE;
448       MRE.Word0 = ((0         <<  0) |
449                    (Type      << 24) |
450                    (Log2Size  << 28) |
451                    (0   << 30) |
452                    RF_Scattered);
453       MRE.Word1 = Value2;
454       Relocs.push_back(MRE);
455     }
456   }
457
458   void ComputeRelocationInfo(MCAssembler &Asm, MCFragment &Fragment,
459                              MCAsmFixup &Fixup,
460                              DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
461                              std::vector<MachRelocationEntry> &Relocs) {
462     MCValue Target;
463     if (!Fixup.Value->EvaluateAsRelocatable(Target))
464       llvm_report_error("expected relocatable expression");
465
466     // If this is a difference or a local symbol plus an offset, then we need a
467     // scattered relocation entry.
468     if (Target.getSymB() ||
469         (Target.getSymA() && !Target.getSymA()->isUndefined() &&
470          Target.getConstant()))
471       return ComputeScatteredRelocationInfo(Asm, Fragment, Fixup, Target,
472                                             SymbolMap, Relocs);
473
474     // See <reloc.h>.
475     uint32_t Address = Fragment.getOffset() + Fixup.Offset;
476     uint32_t Value = 0;
477     unsigned Index = 0;
478     unsigned IsPCRel = 0;
479     unsigned IsExtern = 0;
480     unsigned Type = 0;
481
482     if (Target.isAbsolute()) { // constant
483       // SymbolNum of 0 indicates the absolute section.
484       //
485       // FIXME: When is this generated?
486       Type = RIT_Vanilla;
487       Value = 0;
488       llvm_unreachable("FIXME: Not yet implemented!");
489     } else {
490       const MCSymbol *Symbol = Target.getSymA();
491       MCSymbolData *SD = SymbolMap.lookup(Symbol);
492
493       if (Symbol->isUndefined()) {
494         IsExtern = 1;
495         Index = SD->getIndex();
496         Value = 0;
497       } else {
498         // The index is the section ordinal.
499         //
500         // FIXME: O(N)
501         Index = 1;
502         for (MCAssembler::iterator it = Asm.begin(),
503                ie = Asm.end(); it != ie; ++it, ++Index)
504           if (&*it == SD->getFragment()->getParent())
505             break;
506         Value = SD->getFragment()->getAddress() + SD->getOffset();
507       }
508
509       Type = RIT_Vanilla;
510     }
511
512     // The value which goes in the fixup is current value of the expression.
513     Fixup.FixedValue = Value + Target.getConstant();
514
515     unsigned Log2Size = Log2_32(Fixup.Size);
516     assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
517
518     // struct relocation_info (8 bytes)
519     MachRelocationEntry MRE;
520     MRE.Word0 = Address;
521     MRE.Word1 = ((Index     <<  0) |
522                  (IsPCRel   << 24) |
523                  (Log2Size  << 25) |
524                  (IsExtern  << 27) |
525                  (Type      << 28));
526     Relocs.push_back(MRE);
527   }
528
529   void BindIndirectSymbols(MCAssembler &Asm,
530                            DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap) {
531     // This is the point where 'as' creates actual symbols for indirect symbols
532     // (in the following two passes). It would be easier for us to do this
533     // sooner when we see the attribute, but that makes getting the order in the
534     // symbol table much more complicated than it is worth.
535     //
536     // FIXME: Revisit this when the dust settles.
537
538     // Bind non lazy symbol pointers first.
539     for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
540            ie = Asm.indirect_symbol_end(); it != ie; ++it) {
541       // FIXME: cast<> support!
542       const MCSectionMachO &Section =
543         static_cast<const MCSectionMachO&>(it->SectionData->getSection());
544
545       unsigned Type =
546         Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
547       if (Type != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
548         continue;
549
550       MCSymbolData *&Entry = SymbolMap[it->Symbol];
551       if (!Entry)
552         Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm);
553     }
554
555     // Then lazy symbol pointers and symbol stubs.
556     for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
557            ie = Asm.indirect_symbol_end(); it != ie; ++it) {
558       // FIXME: cast<> support!
559       const MCSectionMachO &Section =
560         static_cast<const MCSectionMachO&>(it->SectionData->getSection());
561
562       unsigned Type =
563         Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
564       if (Type != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
565           Type != MCSectionMachO::S_SYMBOL_STUBS)
566         continue;
567
568       MCSymbolData *&Entry = SymbolMap[it->Symbol];
569       if (!Entry) {
570         Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm);
571
572         // Set the symbol type to undefined lazy, but only on construction.
573         //
574         // FIXME: Do not hardcode.
575         Entry->setFlags(Entry->getFlags() | 0x0001);
576       }
577     }
578   }
579
580   /// ComputeSymbolTable - Compute the symbol table data
581   ///
582   /// \param StringTable [out] - The string table data.
583   /// \param StringIndexMap [out] - Map from symbol names to offsets in the
584   /// string table.
585   void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
586                           std::vector<MachSymbolData> &LocalSymbolData,
587                           std::vector<MachSymbolData> &ExternalSymbolData,
588                           std::vector<MachSymbolData> &UndefinedSymbolData) {
589     // Build section lookup table.
590     DenseMap<const MCSection*, uint8_t> SectionIndexMap;
591     unsigned Index = 1;
592     for (MCAssembler::iterator it = Asm.begin(),
593            ie = Asm.end(); it != ie; ++it, ++Index)
594       SectionIndexMap[&it->getSection()] = Index;
595     assert(Index <= 256 && "Too many sections!");
596
597     // Index 0 is always the empty string.
598     StringMap<uint64_t> StringIndexMap;
599     StringTable += '\x00';
600
601     // Build the symbol arrays and the string table, but only for non-local
602     // symbols.
603     //
604     // The particular order that we collect the symbols and create the string
605     // table, then sort the symbols is chosen to match 'as'. Even though it
606     // doesn't matter for correctness, this is important for letting us diff .o
607     // files.
608     for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
609            ie = Asm.symbol_end(); it != ie; ++it) {
610       const MCSymbol &Symbol = it->getSymbol();
611
612       // Ignore assembler temporaries.
613       if (it->getSymbol().isTemporary())
614         continue;
615
616       if (!it->isExternal() && !Symbol.isUndefined())
617         continue;
618
619       uint64_t &Entry = StringIndexMap[Symbol.getName()];
620       if (!Entry) {
621         Entry = StringTable.size();
622         StringTable += Symbol.getName();
623         StringTable += '\x00';
624       }
625
626       MachSymbolData MSD;
627       MSD.SymbolData = it;
628       MSD.StringIndex = Entry;
629
630       if (Symbol.isUndefined()) {
631         MSD.SectionIndex = 0;
632         UndefinedSymbolData.push_back(MSD);
633       } else if (Symbol.isAbsolute()) {
634         MSD.SectionIndex = 0;
635         ExternalSymbolData.push_back(MSD);
636       } else {
637         MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
638         assert(MSD.SectionIndex && "Invalid section index!");
639         ExternalSymbolData.push_back(MSD);
640       }
641     }
642
643     // Now add the data for local symbols.
644     for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
645            ie = Asm.symbol_end(); it != ie; ++it) {
646       const MCSymbol &Symbol = it->getSymbol();
647
648       // Ignore assembler temporaries.
649       if (it->getSymbol().isTemporary())
650         continue;
651
652       if (it->isExternal() || Symbol.isUndefined())
653         continue;
654
655       uint64_t &Entry = StringIndexMap[Symbol.getName()];
656       if (!Entry) {
657         Entry = StringTable.size();
658         StringTable += Symbol.getName();
659         StringTable += '\x00';
660       }
661
662       MachSymbolData MSD;
663       MSD.SymbolData = it;
664       MSD.StringIndex = Entry;
665
666       if (Symbol.isAbsolute()) {
667         MSD.SectionIndex = 0;
668         LocalSymbolData.push_back(MSD);
669       } else {
670         MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
671         assert(MSD.SectionIndex && "Invalid section index!");
672         LocalSymbolData.push_back(MSD);
673       }
674     }
675
676     // External and undefined symbols are required to be in lexicographic order.
677     std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
678     std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
679
680     // Set the symbol indices.
681     Index = 0;
682     for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
683       LocalSymbolData[i].SymbolData->setIndex(Index++);
684     for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
685       ExternalSymbolData[i].SymbolData->setIndex(Index++);
686     for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
687       UndefinedSymbolData[i].SymbolData->setIndex(Index++);
688
689     // The string table is padded to a multiple of 4.
690     while (StringTable.size() % 4)
691       StringTable += '\x00';
692   }
693
694   void WriteObject(MCAssembler &Asm) {
695     unsigned NumSections = Asm.size();
696
697     // Compute the symbol -> symbol data map.
698     //
699     // FIXME: This should not be here.
700     DenseMap<const MCSymbol*, MCSymbolData *> SymbolMap;
701     for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
702            ie = Asm.symbol_end(); it != ie; ++it)
703       SymbolMap[&it->getSymbol()] = it;
704
705     // Create symbol data for any indirect symbols.
706     BindIndirectSymbols(Asm, SymbolMap);
707
708     // Compute symbol table information.
709     SmallString<256> StringTable;
710     std::vector<MachSymbolData> LocalSymbolData;
711     std::vector<MachSymbolData> ExternalSymbolData;
712     std::vector<MachSymbolData> UndefinedSymbolData;
713     unsigned NumSymbols = Asm.symbol_size();
714
715     // No symbol table command is written if there are no symbols.
716     if (NumSymbols)
717       ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
718                          UndefinedSymbolData);
719
720     // The section data starts after the header, the segment load command (and
721     // section headers) and the symbol table.
722     unsigned NumLoadCommands = 1;
723     uint64_t LoadCommandsSize =
724       SegmentLoadCommand32Size + NumSections * Section32Size;
725
726     // Add the symbol table load command sizes, if used.
727     if (NumSymbols) {
728       NumLoadCommands += 2;
729       LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
730     }
731
732     // Compute the total size of the section data, as well as its file size and
733     // vm size.
734     uint64_t SectionDataStart = Header32Size + LoadCommandsSize;
735     uint64_t SectionDataSize = 0;
736     uint64_t SectionDataFileSize = 0;
737     uint64_t VMSize = 0;
738     for (MCAssembler::iterator it = Asm.begin(),
739            ie = Asm.end(); it != ie; ++it) {
740       MCSectionData &SD = *it;
741
742       VMSize = std::max(VMSize, SD.getAddress() + SD.getSize());
743
744       if (isVirtualSection(SD.getSection()))
745         continue;
746
747       SectionDataSize = std::max(SectionDataSize,
748                                  SD.getAddress() + SD.getSize());
749       SectionDataFileSize = std::max(SectionDataFileSize,
750                                      SD.getAddress() + SD.getFileSize());
751     }
752
753     // The section data is padded to 4 bytes.
754     //
755     // FIXME: Is this machine dependent?
756     unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
757     SectionDataFileSize += SectionDataPadding;
758
759     // Write the prolog, starting with the header and load command...
760     WriteHeader32(NumLoadCommands, LoadCommandsSize,
761                   Asm.getSubsectionsViaSymbols());
762     WriteSegmentLoadCommand32(NumSections, VMSize,
763                               SectionDataStart, SectionDataSize);
764
765     // ... and then the section headers.
766     //
767     // We also compute the section relocations while we do this. Note that
768     // computing relocation info will also update the fixup to have the correct
769     // value; this will overwrite the appropriate data in the fragment when it
770     // is written.
771     std::vector<MachRelocationEntry> RelocInfos;
772     uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
773     for (MCAssembler::iterator it = Asm.begin(),
774            ie = Asm.end(); it != ie; ++it) {
775       MCSectionData &SD = *it;
776
777       // The assembler writes relocations in the reverse order they were seen.
778       //
779       // FIXME: It is probably more complicated than this.
780       unsigned NumRelocsStart = RelocInfos.size();
781       for (MCSectionData::reverse_iterator it2 = SD.rbegin(),
782              ie2 = SD.rend(); it2 != ie2; ++it2)
783         for (unsigned i = 0, e = it2->fixup_size(); i != e; ++i)
784           ComputeRelocationInfo(Asm, *it2, it2->getFixups()[e - i - 1],
785                                 SymbolMap, RelocInfos);
786
787       unsigned NumRelocs = RelocInfos.size() - NumRelocsStart;
788       uint64_t SectionStart = SectionDataStart + SD.getAddress();
789       WriteSection32(SD, SectionStart, RelocTableEnd, NumRelocs);
790       RelocTableEnd += NumRelocs * RelocationInfoSize;
791     }
792
793     // Write the symbol table load command, if used.
794     if (NumSymbols) {
795       unsigned FirstLocalSymbol = 0;
796       unsigned NumLocalSymbols = LocalSymbolData.size();
797       unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
798       unsigned NumExternalSymbols = ExternalSymbolData.size();
799       unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
800       unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
801       unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
802       unsigned NumSymTabSymbols =
803         NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
804       uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
805       uint64_t IndirectSymbolOffset = 0;
806
807       // If used, the indirect symbols are written after the section data.
808       if (NumIndirectSymbols)
809         IndirectSymbolOffset = RelocTableEnd;
810
811       // The symbol table is written after the indirect symbol data.
812       uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
813
814       // The string table is written after symbol table.
815       uint64_t StringTableOffset =
816         SymbolTableOffset + NumSymTabSymbols * Nlist32Size;
817       WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
818                              StringTableOffset, StringTable.size());
819
820       WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
821                                FirstExternalSymbol, NumExternalSymbols,
822                                FirstUndefinedSymbol, NumUndefinedSymbols,
823                                IndirectSymbolOffset, NumIndirectSymbols);
824     }
825
826     // Write the actual section data.
827     for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
828       WriteFileData(OS, *it, *this);
829
830     // Write the extra padding.
831     WriteZeros(SectionDataPadding);
832
833     // Write the relocation entries.
834     for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) {
835       Write32(RelocInfos[i].Word0);
836       Write32(RelocInfos[i].Word1);
837     }
838
839     // Write the symbol table data, if used.
840     if (NumSymbols) {
841       // Write the indirect symbol entries.
842       for (MCAssembler::indirect_symbol_iterator
843              it = Asm.indirect_symbol_begin(),
844              ie = Asm.indirect_symbol_end(); it != ie; ++it) {
845         // Indirect symbols in the non lazy symbol pointer section have some
846         // special handling.
847         const MCSectionMachO &Section =
848           static_cast<const MCSectionMachO&>(it->SectionData->getSection());
849         unsigned Type =
850           Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
851         if (Type == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
852           // If this symbol is defined and internal, mark it as such.
853           if (it->Symbol->isDefined() &&
854               !SymbolMap.lookup(it->Symbol)->isExternal()) {
855             uint32_t Flags = ISF_Local;
856             if (it->Symbol->isAbsolute())
857               Flags |= ISF_Absolute;
858             Write32(Flags);
859             continue;
860           }
861         }
862
863         Write32(SymbolMap[it->Symbol]->getIndex());
864       }
865
866       // FIXME: Check that offsets match computed ones.
867
868       // Write the symbol table entries.
869       for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
870         WriteNlist32(LocalSymbolData[i]);
871       for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
872         WriteNlist32(ExternalSymbolData[i]);
873       for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
874         WriteNlist32(UndefinedSymbolData[i]);
875
876       // Write the string table.
877       OS << StringTable.str();
878     }
879   }
880
881   void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF) {
882     // FIXME: Endianness assumption.
883     for (unsigned i = 0; i != Fixup.Size; ++i)
884       DF.getContents()[Fixup.Offset + i] = uint8_t(Fixup.FixedValue >> (i * 8));
885   }
886 };
887
888 /* *** */
889
890 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
891 }
892
893 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
894   : Kind(_Kind),
895     Parent(_Parent),
896     FileSize(~UINT64_C(0))
897 {
898   if (Parent)
899     Parent->getFragmentList().push_back(this);
900 }
901
902 MCFragment::~MCFragment() {
903 }
904
905 uint64_t MCFragment::getAddress() const {
906   assert(getParent() && "Missing Section!");
907   return getParent()->getAddress() + Offset;
908 }
909
910 /* *** */
911
912 MCSectionData::MCSectionData() : Section(0) {}
913
914 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
915   : Section(&_Section),
916     Alignment(1),
917     Address(~UINT64_C(0)),
918     Size(~UINT64_C(0)),
919     FileSize(~UINT64_C(0)),
920     HasInstructions(false)
921 {
922   if (A)
923     A->getSectionList().push_back(this);
924 }
925
926 /* *** */
927
928 MCSymbolData::MCSymbolData() : Symbol(0) {}
929
930 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
931                            uint64_t _Offset, MCAssembler *A)
932   : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
933     IsExternal(false), IsPrivateExtern(false),
934     CommonSize(0), CommonAlign(0), Flags(0), Index(0)
935 {
936   if (A)
937     A->getSymbolList().push_back(this);
938 }
939
940 /* *** */
941
942 MCAssembler::MCAssembler(MCContext &_Context, raw_ostream &_OS)
943   : Context(_Context), OS(_OS), SubsectionsViaSymbols(false)
944 {
945 }
946
947 MCAssembler::~MCAssembler() {
948 }
949
950 void MCAssembler::LayoutSection(MCSectionData &SD) {
951   uint64_t Address = SD.getAddress();
952
953   for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
954     MCFragment &F = *it;
955
956     F.setOffset(Address - SD.getAddress());
957
958     // Evaluate fragment size.
959     switch (F.getKind()) {
960     case MCFragment::FT_Align: {
961       MCAlignFragment &AF = cast<MCAlignFragment>(F);
962
963       uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
964       if (Size > AF.getMaxBytesToEmit())
965         AF.setFileSize(0);
966       else
967         AF.setFileSize(Size);
968       break;
969     }
970
971     case MCFragment::FT_Data:
972     case MCFragment::FT_Fill:
973       F.setFileSize(F.getMaxFileSize());
974       break;
975
976     case MCFragment::FT_Org: {
977       MCOrgFragment &OF = cast<MCOrgFragment>(F);
978
979       MCValue Target;
980       if (!OF.getOffset().EvaluateAsRelocatable(Target))
981         llvm_report_error("expected relocatable expression");
982
983       if (!Target.isAbsolute())
984         llvm_unreachable("FIXME: Not yet implemented!");
985       uint64_t OrgOffset = Target.getConstant();
986       uint64_t Offset = Address - SD.getAddress();
987
988       // FIXME: We need a way to communicate this error.
989       if (OrgOffset < Offset)
990         llvm_report_error("invalid .org offset '" + Twine(OrgOffset) +
991                           "' (at offset '" + Twine(Offset) + "'");
992
993       F.setFileSize(OrgOffset - Offset);
994       break;
995     }
996
997     case MCFragment::FT_ZeroFill: {
998       MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
999
1000       // Align the fragment offset; it is safe to adjust the offset freely since
1001       // this is only in virtual sections.
1002       uint64_t Aligned = RoundUpToAlignment(Address, ZFF.getAlignment());
1003       F.setOffset(Aligned - SD.getAddress());
1004
1005       // FIXME: This is misnamed.
1006       F.setFileSize(ZFF.getSize());
1007       break;
1008     }
1009     }
1010
1011     Address += F.getFileSize();
1012   }
1013
1014   // Set the section sizes.
1015   SD.setSize(Address - SD.getAddress());
1016   if (isVirtualSection(SD.getSection()))
1017     SD.setFileSize(0);
1018   else
1019     SD.setFileSize(Address - SD.getAddress());
1020 }
1021
1022 /// WriteFileData - Write the \arg F data to the output file.
1023 static void WriteFileData(raw_ostream &OS, const MCFragment &F,
1024                           MachObjectWriter &MOW) {
1025   uint64_t Start = OS.tell();
1026   (void) Start;
1027
1028   ++EmittedFragments;
1029
1030   // FIXME: Embed in fragments instead?
1031   switch (F.getKind()) {
1032   case MCFragment::FT_Align: {
1033     MCAlignFragment &AF = cast<MCAlignFragment>(F);
1034     uint64_t Count = AF.getFileSize() / AF.getValueSize();
1035
1036     // FIXME: This error shouldn't actually occur (the front end should emit
1037     // multiple .align directives to enforce the semantics it wants), but is
1038     // severe enough that we want to report it. How to handle this?
1039     if (Count * AF.getValueSize() != AF.getFileSize())
1040       llvm_report_error("undefined .align directive, value size '" +
1041                         Twine(AF.getValueSize()) +
1042                         "' is not a divisor of padding size '" +
1043                         Twine(AF.getFileSize()) + "'");
1044
1045     for (uint64_t i = 0; i != Count; ++i) {
1046       switch (AF.getValueSize()) {
1047       default:
1048         assert(0 && "Invalid size!");
1049       case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
1050       case 2: MOW.Write16(uint16_t(AF.getValue())); break;
1051       case 4: MOW.Write32(uint32_t(AF.getValue())); break;
1052       case 8: MOW.Write64(uint64_t(AF.getValue())); break;
1053       }
1054     }
1055     break;
1056   }
1057
1058   case MCFragment::FT_Data: {
1059     MCDataFragment &DF = cast<MCDataFragment>(F);
1060
1061     // Apply the fixups.
1062     //
1063     // FIXME: Move elsewhere.
1064     for (MCDataFragment::const_fixup_iterator it = DF.fixup_begin(),
1065            ie = DF.fixup_end(); it != ie; ++it)
1066       MOW.ApplyFixup(*it, DF);
1067
1068     OS << cast<MCDataFragment>(F).getContents().str();
1069     break;
1070   }
1071
1072   case MCFragment::FT_Fill: {
1073     MCFillFragment &FF = cast<MCFillFragment>(F);
1074     for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
1075       switch (FF.getValueSize()) {
1076       default:
1077         assert(0 && "Invalid size!");
1078       case 1: MOW.Write8 (uint8_t (FF.getValue())); break;
1079       case 2: MOW.Write16(uint16_t(FF.getValue())); break;
1080       case 4: MOW.Write32(uint32_t(FF.getValue())); break;
1081       case 8: MOW.Write64(uint64_t(FF.getValue())); break;
1082       }
1083     }
1084     break;
1085   }
1086
1087   case MCFragment::FT_Org: {
1088     MCOrgFragment &OF = cast<MCOrgFragment>(F);
1089
1090     for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
1091       MOW.Write8(uint8_t(OF.getValue()));
1092
1093     break;
1094   }
1095
1096   case MCFragment::FT_ZeroFill: {
1097     assert(0 && "Invalid zero fill fragment in concrete section!");
1098     break;
1099   }
1100   }
1101
1102   assert(OS.tell() - Start == F.getFileSize());
1103 }
1104
1105 /// WriteFileData - Write the \arg SD data to the output file.
1106 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
1107                           MachObjectWriter &MOW) {
1108   // Ignore virtual sections.
1109   if (isVirtualSection(SD.getSection())) {
1110     assert(SD.getFileSize() == 0);
1111     return;
1112   }
1113
1114   uint64_t Start = OS.tell();
1115   (void) Start;
1116
1117   for (MCSectionData::const_iterator it = SD.begin(),
1118          ie = SD.end(); it != ie; ++it)
1119     WriteFileData(OS, *it, MOW);
1120
1121   // Add section padding.
1122   assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
1123   MOW.WriteZeros(SD.getFileSize() - SD.getSize());
1124
1125   assert(OS.tell() - Start == SD.getFileSize());
1126 }
1127
1128 void MCAssembler::Finish() {
1129   DEBUG_WITH_TYPE("mc-dump", {
1130       llvm::errs() << "assembler backend - pre-layout\n--\n";
1131       dump(); });
1132
1133   // Layout the concrete sections and fragments.
1134   uint64_t Address = 0;
1135   MCSectionData *Prev = 0;
1136   for (iterator it = begin(), ie = end(); it != ie; ++it) {
1137     MCSectionData &SD = *it;
1138
1139     // Skip virtual sections.
1140     if (isVirtualSection(SD.getSection()))
1141       continue;
1142
1143     // Align this section if necessary by adding padding bytes to the previous
1144     // section.
1145     if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
1146       assert(Prev && "Missing prev section!");
1147       Prev->setFileSize(Prev->getFileSize() + Pad);
1148       Address += Pad;
1149     }
1150
1151     // Layout the section fragments and its size.
1152     SD.setAddress(Address);
1153     LayoutSection(SD);
1154     Address += SD.getFileSize();
1155
1156     Prev = &SD;
1157   }
1158
1159   // Layout the virtual sections.
1160   for (iterator it = begin(), ie = end(); it != ie; ++it) {
1161     MCSectionData &SD = *it;
1162
1163     if (!isVirtualSection(SD.getSection()))
1164       continue;
1165
1166     SD.setAddress(Address);
1167     LayoutSection(SD);
1168     Address += SD.getSize();
1169   }
1170
1171   DEBUG_WITH_TYPE("mc-dump", {
1172       llvm::errs() << "assembler backend - post-layout\n--\n";
1173       dump(); });
1174
1175   // Write the object file.
1176   MachObjectWriter MOW(OS);
1177   MOW.WriteObject(*this);
1178
1179   OS.flush();
1180 }
1181
1182
1183 // Debugging methods
1184
1185 namespace llvm {
1186
1187 raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
1188   OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << AF.Value
1189      << " Size:" << AF.Size << ">";
1190   return OS;
1191 }
1192
1193 }
1194
1195 void MCFragment::dump() {
1196   raw_ostream &OS = llvm::errs();
1197
1198   OS << "<MCFragment " << (void*) this << " Offset:" << Offset
1199      << " FileSize:" << FileSize;
1200
1201   if (!Fixups.empty()) {
1202     OS << "\n";
1203     OS << "          Fixups:[";
1204     for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
1205       if (it != fixup_begin()) OS << ",\n            ";
1206       OS << *it;
1207     }
1208     OS << "]";
1209   }
1210
1211   OS << ">";
1212 }
1213
1214 void MCAlignFragment::dump() {
1215   raw_ostream &OS = llvm::errs();
1216
1217   OS << "<MCAlignFragment ";
1218   this->MCFragment::dump();
1219   OS << "\n       ";
1220   OS << " Alignment:" << getAlignment()
1221      << " Value:" << getValue() << " ValueSize:" << getValueSize()
1222      << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
1223 }
1224
1225 void MCDataFragment::dump() {
1226   raw_ostream &OS = llvm::errs();
1227
1228   OS << "<MCDataFragment ";
1229   this->MCFragment::dump();
1230   OS << "\n       ";
1231   OS << " Contents:[";
1232   for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
1233     if (i) OS << ",";
1234     OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1235   }
1236   OS << "]>";
1237 }
1238
1239 void MCFillFragment::dump() {
1240   raw_ostream &OS = llvm::errs();
1241
1242   OS << "<MCFillFragment ";
1243   this->MCFragment::dump();
1244   OS << "\n       ";
1245   OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
1246      << " Count:" << getCount() << ">";
1247 }
1248
1249 void MCOrgFragment::dump() {
1250   raw_ostream &OS = llvm::errs();
1251
1252   OS << "<MCOrgFragment ";
1253   this->MCFragment::dump();
1254   OS << "\n       ";
1255   OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
1256 }
1257
1258 void MCZeroFillFragment::dump() {
1259   raw_ostream &OS = llvm::errs();
1260
1261   OS << "<MCZeroFillFragment ";
1262   this->MCFragment::dump();
1263   OS << "\n       ";
1264   OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
1265 }
1266
1267 void MCSectionData::dump() {
1268   raw_ostream &OS = llvm::errs();
1269
1270   OS << "<MCSectionData";
1271   OS << " Alignment:" << getAlignment() << " Address:" << Address
1272      << " Size:" << Size << " FileSize:" << FileSize
1273      << " Fragments:[";
1274   for (iterator it = begin(), ie = end(); it != ie; ++it) {
1275     if (it != begin()) OS << ",\n      ";
1276     it->dump();
1277   }
1278   OS << "]>";
1279 }
1280
1281 void MCSymbolData::dump() {
1282   raw_ostream &OS = llvm::errs();
1283
1284   OS << "<MCSymbolData Symbol:" << getSymbol()
1285      << " Fragment:" << getFragment() << " Offset:" << getOffset()
1286      << " Flags:" << getFlags() << " Index:" << getIndex();
1287   if (isCommon())
1288     OS << " (common, size:" << getCommonSize()
1289        << " align: " << getCommonAlignment() << ")";
1290   if (isExternal())
1291     OS << " (external)";
1292   if (isPrivateExtern())
1293     OS << " (private extern)";
1294   OS << ">";
1295 }
1296
1297 void MCAssembler::dump() {
1298   raw_ostream &OS = llvm::errs();
1299
1300   OS << "<MCAssembler\n";
1301   OS << "  Sections:[";
1302   for (iterator it = begin(), ie = end(); it != ie; ++it) {
1303     if (it != begin()) OS << ",\n    ";
1304     it->dump();
1305   }
1306   OS << "],\n";
1307   OS << "  Symbols:[";
1308
1309   for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1310     if (it != symbol_begin()) OS << ",\n    ";
1311     it->dump();
1312   }
1313   OS << "]>\n";
1314 }