1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "assembler"
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCAsmLayout.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCSectionMachO.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/MachO.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Target/TargetRegistry.h"
28 #include "llvm/Target/TargetAsmBackend.h"
31 #include "../Target/X86/X86FixupKinds.h"
36 class MachObjectWriter;
38 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
40 // FIXME FIXME FIXME: There are number of places in this file where we convert
41 // what is a 64-bit assembler value used for computation into a value in the
42 // object file, which may truncate it. We should detect that truncation where
43 // invalid and report errors back.
45 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
46 MachObjectWriter &MOW);
48 static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW);
50 /// isVirtualSection - Check if this is a section which does not actually exist
51 /// in the object file.
52 static bool isVirtualSection(const MCSection &Section) {
54 const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section);
55 return (SMO.getType() == MCSectionMachO::S_ZEROFILL);
58 static unsigned getFixupKindLog2Size(unsigned Kind) {
60 default: llvm_unreachable("invalid fixup kind!");
61 case X86::reloc_pcrel_1byte:
62 case FK_Data_1: return 0;
63 case FK_Data_2: return 1;
64 case X86::reloc_pcrel_4byte:
65 case X86::reloc_riprel_4byte:
66 case FK_Data_4: return 2;
67 case FK_Data_8: return 3;
71 static bool isFixupKindPCRel(unsigned Kind) {
75 case X86::reloc_pcrel_1byte:
76 case X86::reloc_pcrel_4byte:
77 case X86::reloc_riprel_4byte:
82 class MachObjectWriter {
83 // See <mach-o/loader.h>.
85 Header_Magic32 = 0xFEEDFACE,
86 Header_Magic64 = 0xFEEDFACF
92 SegmentLoadCommand32Size = 56,
93 SegmentLoadCommand64Size = 72,
96 SymtabLoadCommandSize = 24,
97 DysymtabLoadCommandSize = 80,
100 RelocationInfoSize = 8
103 enum HeaderFileType {
108 HF_SubsectionsViaSymbols = 0x2000
111 enum LoadCommandType {
118 // See <mach-o/nlist.h>.
119 enum SymbolTypeType {
120 STT_Undefined = 0x00,
125 enum SymbolTypeFlags {
126 // If any of these bits are set, then the entry is a stab entry number (see
127 // <mach-o/stab.h>. Otherwise the other masks apply.
128 STF_StabsEntryMask = 0xe0,
132 STF_PrivateExtern = 0x10
135 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
137 enum IndirectSymbolFlags {
138 ISF_Local = 0x80000000,
139 ISF_Absolute = 0x40000000
142 /// RelocationFlags - Special flags for addresses.
143 enum RelocationFlags {
144 RF_Scattered = 0x80000000
147 enum RelocationInfoType {
151 RIT_PreboundLazyPointer = 3,
152 RIT_LocalDifference = 4
155 /// MachSymbolData - Helper struct for containing some precomputed information
157 struct MachSymbolData {
158 MCSymbolData *SymbolData;
159 uint64_t StringIndex;
160 uint8_t SectionIndex;
162 // Support lexicographic sorting.
163 bool operator<(const MachSymbolData &RHS) const {
164 const std::string &Name = SymbolData->getSymbol().getName();
165 return Name < RHS.SymbolData->getSymbol().getName();
170 unsigned Is64Bit : 1;
174 MachObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool _IsLSB = true)
175 : OS(_OS), Is64Bit(_Is64Bit), IsLSB(_IsLSB) {
178 /// @name Helper Methods
181 void Write8(uint8_t Value) {
185 void Write16(uint16_t Value) {
187 Write8(uint8_t(Value >> 0));
188 Write8(uint8_t(Value >> 8));
190 Write8(uint8_t(Value >> 8));
191 Write8(uint8_t(Value >> 0));
195 void Write32(uint32_t Value) {
197 Write16(uint16_t(Value >> 0));
198 Write16(uint16_t(Value >> 16));
200 Write16(uint16_t(Value >> 16));
201 Write16(uint16_t(Value >> 0));
205 void Write64(uint64_t Value) {
207 Write32(uint32_t(Value >> 0));
208 Write32(uint32_t(Value >> 32));
210 Write32(uint32_t(Value >> 32));
211 Write32(uint32_t(Value >> 0));
215 void WriteZeros(unsigned N) {
216 const char Zeros[16] = { 0 };
218 for (unsigned i = 0, e = N / 16; i != e; ++i)
219 OS << StringRef(Zeros, 16);
221 OS << StringRef(Zeros, N % 16);
224 void WriteString(StringRef Str, unsigned ZeroFillSize = 0) {
227 WriteZeros(ZeroFillSize - Str.size());
232 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
233 bool SubsectionsViaSymbols) {
236 if (SubsectionsViaSymbols)
237 Flags |= HF_SubsectionsViaSymbols;
239 // struct mach_header (28 bytes) or
240 // struct mach_header_64 (32 bytes)
242 uint64_t Start = OS.tell();
245 Write32(Is64Bit ? Header_Magic64 : Header_Magic32);
247 // FIXME: Support cputype.
248 Write32(Is64Bit ? MachO::CPUTypeX86_64 : MachO::CPUTypeI386);
249 // FIXME: Support cpusubtype.
250 Write32(MachO::CPUSubType_I386_ALL);
252 Write32(NumLoadCommands); // Object files have a single load command, the
254 Write32(LoadCommandsSize);
257 Write32(0); // reserved
259 assert(OS.tell() - Start == Is64Bit ? Header64Size : Header32Size);
262 /// WriteSegmentLoadCommand - Write a segment load command.
264 /// \arg NumSections - The number of sections in this segment.
265 /// \arg SectionDataSize - The total size of the sections.
266 void WriteSegmentLoadCommand(unsigned NumSections,
268 uint64_t SectionDataStartOffset,
269 uint64_t SectionDataSize) {
270 // struct segment_command (56 bytes) or
271 // struct segment_command_64 (72 bytes)
273 uint64_t Start = OS.tell();
276 unsigned SegmentLoadCommandSize = Is64Bit ? SegmentLoadCommand64Size :
277 SegmentLoadCommand32Size;
278 Write32(Is64Bit ? LCT_Segment64 : LCT_Segment);
279 Write32(SegmentLoadCommandSize +
280 NumSections * (Is64Bit ? Section64Size : Section32Size));
284 Write64(0); // vmaddr
285 Write64(VMSize); // vmsize
286 Write64(SectionDataStartOffset); // file offset
287 Write64(SectionDataSize); // file size
289 Write32(0); // vmaddr
290 Write32(VMSize); // vmsize
291 Write32(SectionDataStartOffset); // file offset
292 Write32(SectionDataSize); // file size
294 Write32(0x7); // maxprot
295 Write32(0x7); // initprot
296 Write32(NumSections);
299 assert(OS.tell() - Start == SegmentLoadCommandSize);
302 void WriteSection(const MCSectionData &SD, uint64_t FileOffset,
303 uint64_t RelocationsStart, unsigned NumRelocations) {
304 // The offset is unused for virtual sections.
305 if (isVirtualSection(SD.getSection())) {
306 assert(SD.getFileSize() == 0 && "Invalid file size!");
310 // struct section (68 bytes) or
311 // struct section_64 (80 bytes)
313 uint64_t Start = OS.tell();
316 // FIXME: cast<> support!
317 const MCSectionMachO &Section =
318 static_cast<const MCSectionMachO&>(SD.getSection());
319 WriteString(Section.getSectionName(), 16);
320 WriteString(Section.getSegmentName(), 16);
322 Write64(SD.getAddress()); // address
323 Write64(SD.getSize()); // size
325 Write32(SD.getAddress()); // address
326 Write32(SD.getSize()); // size
330 unsigned Flags = Section.getTypeAndAttributes();
331 if (SD.hasInstructions())
332 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
334 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
335 Write32(Log2_32(SD.getAlignment()));
336 Write32(NumRelocations ? RelocationsStart : 0);
337 Write32(NumRelocations);
339 Write32(0); // reserved1
340 Write32(Section.getStubSize()); // reserved2
342 Write32(0); // reserved3
344 assert(OS.tell() - Start == Is64Bit ? Section64Size : Section32Size);
347 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
348 uint32_t StringTableOffset,
349 uint32_t StringTableSize) {
350 // struct symtab_command (24 bytes)
352 uint64_t Start = OS.tell();
356 Write32(SymtabLoadCommandSize);
357 Write32(SymbolOffset);
359 Write32(StringTableOffset);
360 Write32(StringTableSize);
362 assert(OS.tell() - Start == SymtabLoadCommandSize);
365 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
366 uint32_t NumLocalSymbols,
367 uint32_t FirstExternalSymbol,
368 uint32_t NumExternalSymbols,
369 uint32_t FirstUndefinedSymbol,
370 uint32_t NumUndefinedSymbols,
371 uint32_t IndirectSymbolOffset,
372 uint32_t NumIndirectSymbols) {
373 // struct dysymtab_command (80 bytes)
375 uint64_t Start = OS.tell();
378 Write32(LCT_Dysymtab);
379 Write32(DysymtabLoadCommandSize);
380 Write32(FirstLocalSymbol);
381 Write32(NumLocalSymbols);
382 Write32(FirstExternalSymbol);
383 Write32(NumExternalSymbols);
384 Write32(FirstUndefinedSymbol);
385 Write32(NumUndefinedSymbols);
386 Write32(0); // tocoff
388 Write32(0); // modtaboff
389 Write32(0); // nmodtab
390 Write32(0); // extrefsymoff
391 Write32(0); // nextrefsyms
392 Write32(IndirectSymbolOffset);
393 Write32(NumIndirectSymbols);
394 Write32(0); // extreloff
395 Write32(0); // nextrel
396 Write32(0); // locreloff
397 Write32(0); // nlocrel
399 assert(OS.tell() - Start == DysymtabLoadCommandSize);
402 void WriteNlist(MachSymbolData &MSD) {
403 MCSymbolData &Data = *MSD.SymbolData;
404 const MCSymbol &Symbol = Data.getSymbol();
406 uint16_t Flags = Data.getFlags();
407 uint32_t Address = 0;
409 // Set the N_TYPE bits. See <mach-o/nlist.h>.
411 // FIXME: Are the prebound or indirect fields possible here?
412 if (Symbol.isUndefined())
413 Type = STT_Undefined;
414 else if (Symbol.isAbsolute())
419 // FIXME: Set STAB bits.
421 if (Data.isPrivateExtern())
422 Type |= STF_PrivateExtern;
425 if (Data.isExternal() || Symbol.isUndefined())
426 Type |= STF_External;
428 // Compute the symbol address.
429 if (Symbol.isDefined()) {
430 if (Symbol.isAbsolute()) {
431 llvm_unreachable("FIXME: Not yet implemented!");
433 Address = Data.getAddress();
435 } else if (Data.isCommon()) {
436 // Common symbols are encoded with the size in the address
437 // field, and their alignment in the flags.
438 Address = Data.getCommonSize();
440 // Common alignment is packed into the 'desc' bits.
441 if (unsigned Align = Data.getCommonAlignment()) {
442 unsigned Log2Size = Log2_32(Align);
443 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
445 llvm_report_error("invalid 'common' alignment '" +
447 // FIXME: Keep this mask with the SymbolFlags enumeration.
448 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
452 // struct nlist (12 bytes)
454 Write32(MSD.StringIndex);
456 Write8(MSD.SectionIndex);
458 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
467 struct MachRelocationEntry {
471 void ComputeScatteredRelocationInfo(MCAssembler &Asm, MCFragment &Fragment,
473 const MCValue &Target,
474 std::vector<MachRelocationEntry> &Relocs) {
475 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
476 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
477 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
478 unsigned Type = RIT_Vanilla;
481 const MCSymbol *A = Target.getSymA();
482 MCSymbolData *A_SD = &Asm.getSymbolData(*A);
484 if (!A_SD->getFragment())
485 llvm_report_error("symbol '" + A->getName() +
486 "' can not be undefined in a subtraction expression");
488 uint32_t Value = A_SD->getAddress();
491 if (const MCSymbol *B = Target.getSymB()) {
492 MCSymbolData *B_SD = &Asm.getSymbolData(*B);
494 if (!B_SD->getFragment())
495 llvm_report_error("symbol '" + B->getName() +
496 "' can not be undefined in a subtraction expression");
498 // Select the appropriate difference relocation type.
500 // Note that there is no longer any semantic difference between these two
501 // relocation types from the linkers point of view, this is done solely
502 // for pedantic compatibility with 'as'.
503 Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference;
504 Value2 = B_SD->getAddress();
507 MachRelocationEntry MRE;
508 MRE.Word0 = ((Address << 0) |
514 Relocs.push_back(MRE);
516 if (Type == RIT_Difference || Type == RIT_LocalDifference) {
517 MachRelocationEntry MRE;
518 MRE.Word0 = ((0 << 0) |
524 Relocs.push_back(MRE);
528 void ComputeRelocationInfo(MCAssembler &Asm, MCDataFragment &Fragment,
530 std::vector<MachRelocationEntry> &Relocs) {
531 unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind);
532 unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind);
534 // FIXME: Share layout object.
535 MCAsmLayout Layout(Asm);
537 // Evaluate the fixup; if the value was resolved, no relocation is needed.
539 if (Asm.EvaluateFixup(Layout, Fixup, &Fragment, Target, Fixup.FixedValue))
542 // If this is a difference or a defined symbol plus an offset, then we need
543 // a scattered relocation entry.
544 uint32_t Offset = Target.getConstant();
546 Offset += 1 << Log2Size;
547 if (Target.getSymB() ||
548 (Target.getSymA() && !Target.getSymA()->isUndefined() &&
550 return ComputeScatteredRelocationInfo(Asm, Fragment, Fixup, Target,
554 uint32_t Address = Fragment.getOffset() + Fixup.Offset;
557 unsigned IsExtern = 0;
560 if (Target.isAbsolute()) { // constant
561 // SymbolNum of 0 indicates the absolute section.
563 // FIXME: Currently, these are never generated (see code below). I cannot
564 // find a case where they are actually emitted.
568 const MCSymbol *Symbol = Target.getSymA();
569 MCSymbolData *SD = &Asm.getSymbolData(*Symbol);
571 if (Symbol->isUndefined()) {
573 Index = SD->getIndex();
576 // The index is the section ordinal.
580 MCAssembler::iterator it = Asm.begin(), ie = Asm.end();
581 for (; it != ie; ++it, ++Index)
582 if (&*it == SD->getFragment()->getParent())
584 assert(it != ie && "Unable to find section index!");
585 Value = SD->getAddress();
591 // struct relocation_info (8 bytes)
592 MachRelocationEntry MRE;
594 MRE.Word1 = ((Index << 0) |
599 Relocs.push_back(MRE);
602 void BindIndirectSymbols(MCAssembler &Asm) {
603 // This is the point where 'as' creates actual symbols for indirect symbols
604 // (in the following two passes). It would be easier for us to do this
605 // sooner when we see the attribute, but that makes getting the order in the
606 // symbol table much more complicated than it is worth.
608 // FIXME: Revisit this when the dust settles.
610 // Bind non lazy symbol pointers first.
611 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
612 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
613 // FIXME: cast<> support!
614 const MCSectionMachO &Section =
615 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
617 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
620 Asm.getOrCreateSymbolData(*it->Symbol);
623 // Then lazy symbol pointers and symbol stubs.
624 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
625 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
626 // FIXME: cast<> support!
627 const MCSectionMachO &Section =
628 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
630 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
631 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
634 // Set the symbol type to undefined lazy, but only on construction.
636 // FIXME: Do not hardcode.
638 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
640 Entry.setFlags(Entry.getFlags() | 0x0001);
644 /// ComputeSymbolTable - Compute the symbol table data
646 /// \param StringTable [out] - The string table data.
647 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
649 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
650 std::vector<MachSymbolData> &LocalSymbolData,
651 std::vector<MachSymbolData> &ExternalSymbolData,
652 std::vector<MachSymbolData> &UndefinedSymbolData) {
653 // Build section lookup table.
654 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
656 for (MCAssembler::iterator it = Asm.begin(),
657 ie = Asm.end(); it != ie; ++it, ++Index)
658 SectionIndexMap[&it->getSection()] = Index;
659 assert(Index <= 256 && "Too many sections!");
661 // Index 0 is always the empty string.
662 StringMap<uint64_t> StringIndexMap;
663 StringTable += '\x00';
665 // Build the symbol arrays and the string table, but only for non-local
668 // The particular order that we collect the symbols and create the string
669 // table, then sort the symbols is chosen to match 'as'. Even though it
670 // doesn't matter for correctness, this is important for letting us diff .o
672 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
673 ie = Asm.symbol_end(); it != ie; ++it) {
674 const MCSymbol &Symbol = it->getSymbol();
676 // Ignore assembler temporaries.
677 if (it->getSymbol().isTemporary())
680 if (!it->isExternal() && !Symbol.isUndefined())
683 uint64_t &Entry = StringIndexMap[Symbol.getName()];
685 Entry = StringTable.size();
686 StringTable += Symbol.getName();
687 StringTable += '\x00';
692 MSD.StringIndex = Entry;
694 if (Symbol.isUndefined()) {
695 MSD.SectionIndex = 0;
696 UndefinedSymbolData.push_back(MSD);
697 } else if (Symbol.isAbsolute()) {
698 MSD.SectionIndex = 0;
699 ExternalSymbolData.push_back(MSD);
701 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
702 assert(MSD.SectionIndex && "Invalid section index!");
703 ExternalSymbolData.push_back(MSD);
707 // Now add the data for local symbols.
708 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
709 ie = Asm.symbol_end(); it != ie; ++it) {
710 const MCSymbol &Symbol = it->getSymbol();
712 // Ignore assembler temporaries.
713 if (it->getSymbol().isTemporary())
716 if (it->isExternal() || Symbol.isUndefined())
719 uint64_t &Entry = StringIndexMap[Symbol.getName()];
721 Entry = StringTable.size();
722 StringTable += Symbol.getName();
723 StringTable += '\x00';
728 MSD.StringIndex = Entry;
730 if (Symbol.isAbsolute()) {
731 MSD.SectionIndex = 0;
732 LocalSymbolData.push_back(MSD);
734 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
735 assert(MSD.SectionIndex && "Invalid section index!");
736 LocalSymbolData.push_back(MSD);
740 // External and undefined symbols are required to be in lexicographic order.
741 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
742 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
744 // Set the symbol indices.
746 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
747 LocalSymbolData[i].SymbolData->setIndex(Index++);
748 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
749 ExternalSymbolData[i].SymbolData->setIndex(Index++);
750 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
751 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
753 // The string table is padded to a multiple of 4.
754 while (StringTable.size() % 4)
755 StringTable += '\x00';
758 void WriteObject(MCAssembler &Asm) {
759 unsigned NumSections = Asm.size();
761 // Create symbol data for any indirect symbols.
762 BindIndirectSymbols(Asm);
764 // Compute symbol table information.
765 SmallString<256> StringTable;
766 std::vector<MachSymbolData> LocalSymbolData;
767 std::vector<MachSymbolData> ExternalSymbolData;
768 std::vector<MachSymbolData> UndefinedSymbolData;
769 unsigned NumSymbols = Asm.symbol_size();
771 // No symbol table command is written if there are no symbols.
773 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
774 UndefinedSymbolData);
776 // The section data starts after the header, the segment load command (and
777 // section headers) and the symbol table.
778 unsigned NumLoadCommands = 1;
779 uint64_t LoadCommandsSize = Is64Bit ?
780 SegmentLoadCommand64Size + NumSections * Section64Size :
781 SegmentLoadCommand32Size + NumSections * Section32Size;
783 // Add the symbol table load command sizes, if used.
785 NumLoadCommands += 2;
786 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
789 // Compute the total size of the section data, as well as its file size and
791 uint64_t SectionDataStart = (Is64Bit ? Header64Size : Header32Size)
793 uint64_t SectionDataSize = 0;
794 uint64_t SectionDataFileSize = 0;
796 for (MCAssembler::iterator it = Asm.begin(),
797 ie = Asm.end(); it != ie; ++it) {
798 MCSectionData &SD = *it;
800 VMSize = std::max(VMSize, SD.getAddress() + SD.getSize());
802 if (isVirtualSection(SD.getSection()))
805 SectionDataSize = std::max(SectionDataSize,
806 SD.getAddress() + SD.getSize());
807 SectionDataFileSize = std::max(SectionDataFileSize,
808 SD.getAddress() + SD.getFileSize());
811 // The section data is padded to 4 bytes.
813 // FIXME: Is this machine dependent?
814 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
815 SectionDataFileSize += SectionDataPadding;
817 // Write the prolog, starting with the header and load command...
818 WriteHeader(NumLoadCommands, LoadCommandsSize,
819 Asm.getSubsectionsViaSymbols());
820 WriteSegmentLoadCommand(NumSections, VMSize,
821 SectionDataStart, SectionDataSize);
823 // ... and then the section headers.
825 // We also compute the section relocations while we do this. Note that
826 // computing relocation info will also update the fixup to have the correct
827 // value; this will overwrite the appropriate data in the fragment when it
829 std::vector<MachRelocationEntry> RelocInfos;
830 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
831 for (MCAssembler::iterator it = Asm.begin(),
832 ie = Asm.end(); it != ie; ++it) {
833 MCSectionData &SD = *it;
835 // The assembler writes relocations in the reverse order they were seen.
837 // FIXME: It is probably more complicated than this.
838 unsigned NumRelocsStart = RelocInfos.size();
839 for (MCSectionData::reverse_iterator it2 = SD.rbegin(),
840 ie2 = SD.rend(); it2 != ie2; ++it2)
841 if (MCDataFragment *DF = dyn_cast<MCDataFragment>(&*it2))
842 for (unsigned i = 0, e = DF->fixup_size(); i != e; ++i)
843 ComputeRelocationInfo(Asm, *DF, DF->getFixups()[e - i - 1],
846 unsigned NumRelocs = RelocInfos.size() - NumRelocsStart;
847 uint64_t SectionStart = SectionDataStart + SD.getAddress();
848 WriteSection(SD, SectionStart, RelocTableEnd, NumRelocs);
849 RelocTableEnd += NumRelocs * RelocationInfoSize;
852 // Write the symbol table load command, if used.
854 unsigned FirstLocalSymbol = 0;
855 unsigned NumLocalSymbols = LocalSymbolData.size();
856 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
857 unsigned NumExternalSymbols = ExternalSymbolData.size();
858 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
859 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
860 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
861 unsigned NumSymTabSymbols =
862 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
863 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
864 uint64_t IndirectSymbolOffset = 0;
866 // If used, the indirect symbols are written after the section data.
867 if (NumIndirectSymbols)
868 IndirectSymbolOffset = RelocTableEnd;
870 // The symbol table is written after the indirect symbol data.
871 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
873 // The string table is written after symbol table.
874 uint64_t StringTableOffset =
875 SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? Nlist64Size :
877 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
878 StringTableOffset, StringTable.size());
880 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
881 FirstExternalSymbol, NumExternalSymbols,
882 FirstUndefinedSymbol, NumUndefinedSymbols,
883 IndirectSymbolOffset, NumIndirectSymbols);
886 // Write the actual section data.
887 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
888 WriteFileData(OS, *it, *this);
890 // Write the extra padding.
891 WriteZeros(SectionDataPadding);
893 // Write the relocation entries.
894 for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) {
895 Write32(RelocInfos[i].Word0);
896 Write32(RelocInfos[i].Word1);
899 // Write the symbol table data, if used.
901 // Write the indirect symbol entries.
902 for (MCAssembler::indirect_symbol_iterator
903 it = Asm.indirect_symbol_begin(),
904 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
905 // Indirect symbols in the non lazy symbol pointer section have some
907 const MCSectionMachO &Section =
908 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
909 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
910 // If this symbol is defined and internal, mark it as such.
911 if (it->Symbol->isDefined() &&
912 !Asm.getSymbolData(*it->Symbol).isExternal()) {
913 uint32_t Flags = ISF_Local;
914 if (it->Symbol->isAbsolute())
915 Flags |= ISF_Absolute;
921 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
924 // FIXME: Check that offsets match computed ones.
926 // Write the symbol table entries.
927 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
928 WriteNlist(LocalSymbolData[i]);
929 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
930 WriteNlist(ExternalSymbolData[i]);
931 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
932 WriteNlist(UndefinedSymbolData[i]);
934 // Write the string table.
935 OS << StringTable.str();
939 void ApplyFixup(const MCAsmFixup &Fixup, MCDataFragment &DF) {
940 unsigned Size = 1 << getFixupKindLog2Size(Fixup.Kind);
942 // FIXME: Endianness assumption.
943 assert(Fixup.Offset + Size <= DF.getContents().size() &&
944 "Invalid fixup offset!");
945 for (unsigned i = 0; i != Size; ++i)
946 DF.getContents()[Fixup.Offset + i] = uint8_t(Fixup.FixedValue >> (i * 8));
952 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
955 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
958 FileSize(~UINT64_C(0))
961 Parent->getFragmentList().push_back(this);
964 MCFragment::~MCFragment() {
967 uint64_t MCFragment::getAddress() const {
968 assert(getParent() && "Missing Section!");
969 return getParent()->getAddress() + Offset;
974 MCSectionData::MCSectionData() : Section(0) {}
976 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
977 : Section(&_Section),
979 Address(~UINT64_C(0)),
981 FileSize(~UINT64_C(0)),
982 HasInstructions(false)
985 A->getSectionList().push_back(this);
990 MCSymbolData::MCSymbolData() : Symbol(0) {}
992 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
993 uint64_t _Offset, MCAssembler *A)
994 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
995 IsExternal(false), IsPrivateExtern(false),
996 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
999 A->getSymbolList().push_back(this);
1004 MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
1006 : Context(_Context), Backend(_Backend), OS(_OS), SubsectionsViaSymbols(false)
1010 MCAssembler::~MCAssembler() {
1013 bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, MCAsmFixup &Fixup,
1015 MCValue &Target, uint64_t &Value) const {
1016 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
1017 llvm_report_error("expected relocatable expression");
1019 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
1020 // doesn't support small relocations, but then under what criteria does the
1021 // assembler allow symbol differences?
1023 Value = Target.getConstant();
1025 // FIXME: This "resolved" check isn't quite right. The assumption is that if
1026 // we have a PCrel access to a temporary, then that temporary is in the same
1027 // atom, and so the value is resolved. We need explicit atom's to implement
1028 // this more precisely.
1029 bool IsResolved = true, IsPCRel = isFixupKindPCRel(Fixup.Kind);
1030 if (const MCSymbol *Symbol = Target.getSymA()) {
1031 if (Symbol->isDefined())
1032 Value += getSymbolData(*Symbol).getAddress();
1036 // With scattered symbols, we assume anything that isn't a PCrel temporary
1037 // access can have an arbitrary value.
1038 if (getBackend().hasScatteredSymbols() &&
1039 (!IsPCRel || !Symbol->isTemporary()))
1042 if (const MCSymbol *Symbol = Target.getSymB()) {
1043 if (Symbol->isDefined())
1044 Value -= getSymbolData(*Symbol).getAddress();
1048 // With scattered symbols, we assume anything that isn't a PCrel temporary
1049 // access can have an arbitrary value.
1050 if (getBackend().hasScatteredSymbols() &&
1051 (!IsPCRel || !Symbol->isTemporary()))
1056 Value -= DF->getAddress() + Fixup.Offset;
1061 void MCAssembler::LayoutSection(MCSectionData &SD) {
1062 MCAsmLayout Layout(*this);
1063 uint64_t Address = SD.getAddress();
1065 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
1066 MCFragment &F = *it;
1068 F.setOffset(Address - SD.getAddress());
1070 // Evaluate fragment size.
1071 switch (F.getKind()) {
1072 case MCFragment::FT_Align: {
1073 MCAlignFragment &AF = cast<MCAlignFragment>(F);
1075 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
1076 if (Size > AF.getMaxBytesToEmit())
1079 AF.setFileSize(Size);
1083 case MCFragment::FT_Data:
1084 case MCFragment::FT_Fill:
1085 F.setFileSize(F.getMaxFileSize());
1088 case MCFragment::FT_Org: {
1089 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1091 int64_t TargetLocation;
1092 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
1093 llvm_report_error("expected assembly-time absolute expression");
1095 // FIXME: We need a way to communicate this error.
1096 int64_t Offset = TargetLocation - F.getOffset();
1098 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
1099 "' (at offset '" + Twine(F.getOffset()) + "'");
1101 F.setFileSize(Offset);
1105 case MCFragment::FT_ZeroFill: {
1106 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
1108 // Align the fragment offset; it is safe to adjust the offset freely since
1109 // this is only in virtual sections.
1110 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
1111 F.setOffset(Address - SD.getAddress());
1113 // FIXME: This is misnamed.
1114 F.setFileSize(ZFF.getSize());
1119 Address += F.getFileSize();
1122 // Set the section sizes.
1123 SD.setSize(Address - SD.getAddress());
1124 if (isVirtualSection(SD.getSection()))
1127 SD.setFileSize(Address - SD.getAddress());
1130 /// WriteNopData - Write optimal nops to the output file for the \arg Count
1131 /// bytes. This returns the number of bytes written. It may return 0 if
1132 /// the \arg Count is more than the maximum optimal nops.
1134 /// FIXME this is X86 32-bit specific and should move to a better place.
1135 static uint64_t WriteNopData(uint64_t Count, MachObjectWriter &MOW) {
1136 static const uint8_t Nops[16][16] = {
1144 {0x0f, 0x1f, 0x40, 0x00},
1145 // nopl 0(%[re]ax,%[re]ax,1)
1146 {0x0f, 0x1f, 0x44, 0x00, 0x00},
1147 // nopw 0(%[re]ax,%[re]ax,1)
1148 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1150 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1151 // nopl 0L(%[re]ax,%[re]ax,1)
1152 {0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1153 // nopw 0L(%[re]ax,%[re]ax,1)
1154 {0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1155 // nopw %cs:0L(%[re]ax,%[re]ax,1)
1156 {0x66, 0x2e, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00},
1157 // nopl 0(%[re]ax,%[re]ax,1)
1158 // nopw 0(%[re]ax,%[re]ax,1)
1159 {0x0f, 0x1f, 0x44, 0x00, 0x00,
1160 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1161 // nopw 0(%[re]ax,%[re]ax,1)
1162 // nopw 0(%[re]ax,%[re]ax,1)
1163 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1164 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00},
1165 // nopw 0(%[re]ax,%[re]ax,1)
1166 // nopl 0L(%[re]ax) */
1167 {0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00,
1168 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1171 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1172 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00},
1174 // nopl 0L(%[re]ax,%[re]ax,1)
1175 {0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00,
1176 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00}
1182 for (uint64_t i = 0; i < Count; i++)
1183 MOW.Write8 (uint8_t(Nops[Count - 1][i]));
1188 /// WriteFileData - Write the \arg F data to the output file.
1189 static void WriteFileData(raw_ostream &OS, const MCFragment &F,
1190 MachObjectWriter &MOW) {
1191 uint64_t Start = OS.tell();
1196 // FIXME: Embed in fragments instead?
1197 switch (F.getKind()) {
1198 case MCFragment::FT_Align: {
1199 MCAlignFragment &AF = cast<MCAlignFragment>(F);
1200 uint64_t Count = AF.getFileSize() / AF.getValueSize();
1202 // FIXME: This error shouldn't actually occur (the front end should emit
1203 // multiple .align directives to enforce the semantics it wants), but is
1204 // severe enough that we want to report it. How to handle this?
1205 if (Count * AF.getValueSize() != AF.getFileSize())
1206 llvm_report_error("undefined .align directive, value size '" +
1207 Twine(AF.getValueSize()) +
1208 "' is not a divisor of padding size '" +
1209 Twine(AF.getFileSize()) + "'");
1211 // See if we are aligning with nops, and if so do that first to try to fill
1212 // the Count bytes. Then if that did not fill any bytes or there are any
1213 // bytes left to fill use the the Value and ValueSize to fill the rest.
1214 if (AF.getEmitNops()) {
1215 uint64_t NopByteCount = WriteNopData(Count, MOW);
1216 Count -= NopByteCount;
1219 for (uint64_t i = 0; i != Count; ++i) {
1220 switch (AF.getValueSize()) {
1222 assert(0 && "Invalid size!");
1223 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
1224 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
1225 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
1226 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
1232 case MCFragment::FT_Data: {
1233 MCDataFragment &DF = cast<MCDataFragment>(F);
1235 // Apply the fixups.
1237 // FIXME: Move elsewhere.
1238 for (MCDataFragment::const_fixup_iterator it = DF.fixup_begin(),
1239 ie = DF.fixup_end(); it != ie; ++it)
1240 MOW.ApplyFixup(*it, DF);
1242 OS << cast<MCDataFragment>(F).getContents().str();
1246 case MCFragment::FT_Fill: {
1247 MCFillFragment &FF = cast<MCFillFragment>(F);
1248 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
1249 switch (FF.getValueSize()) {
1251 assert(0 && "Invalid size!");
1252 case 1: MOW.Write8 (uint8_t (FF.getValue())); break;
1253 case 2: MOW.Write16(uint16_t(FF.getValue())); break;
1254 case 4: MOW.Write32(uint32_t(FF.getValue())); break;
1255 case 8: MOW.Write64(uint64_t(FF.getValue())); break;
1261 case MCFragment::FT_Org: {
1262 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1264 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
1265 MOW.Write8(uint8_t(OF.getValue()));
1270 case MCFragment::FT_ZeroFill: {
1271 assert(0 && "Invalid zero fill fragment in concrete section!");
1276 assert(OS.tell() - Start == F.getFileSize());
1279 /// WriteFileData - Write the \arg SD data to the output file.
1280 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
1281 MachObjectWriter &MOW) {
1282 // Ignore virtual sections.
1283 if (isVirtualSection(SD.getSection())) {
1284 assert(SD.getFileSize() == 0);
1288 uint64_t Start = OS.tell();
1291 for (MCSectionData::const_iterator it = SD.begin(),
1292 ie = SD.end(); it != ie; ++it)
1293 WriteFileData(OS, *it, MOW);
1295 // Add section padding.
1296 assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
1297 MOW.WriteZeros(SD.getFileSize() - SD.getSize());
1299 assert(OS.tell() - Start == SD.getFileSize());
1302 void MCAssembler::Finish() {
1303 DEBUG_WITH_TYPE("mc-dump", {
1304 llvm::errs() << "assembler backend - pre-layout\n--\n";
1307 // Layout until everything fits.
1308 while (LayoutOnce())
1311 DEBUG_WITH_TYPE("mc-dump", {
1312 llvm::errs() << "assembler backend - post-layout\n--\n";
1315 // Write the object file.
1317 // FIXME: Factor out MCObjectWriter.
1318 bool Is64Bit = StringRef(getBackend().getTarget().getName()) == "x86-64";
1319 MachObjectWriter MOW(OS, Is64Bit);
1320 MOW.WriteObject(*this);
1325 bool MCAssembler::FixupNeedsRelaxation(MCAsmFixup &Fixup, MCDataFragment *DF) {
1326 // FIXME: Share layout object.
1327 MCAsmLayout Layout(*this);
1329 // Currently we only need to relax X86::reloc_pcrel_1byte.
1330 if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte)
1333 // If we cannot resolve the fixup value, it requires relaxation.
1336 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
1339 // Otherwise, relax if the value is too big for a (signed) i8.
1340 return int64_t(Value) != int64_t(int8_t(Value));
1343 bool MCAssembler::LayoutOnce() {
1344 // Layout the concrete sections and fragments.
1345 uint64_t Address = 0;
1346 MCSectionData *Prev = 0;
1347 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1348 MCSectionData &SD = *it;
1350 // Skip virtual sections.
1351 if (isVirtualSection(SD.getSection()))
1354 // Align this section if necessary by adding padding bytes to the previous
1356 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
1357 assert(Prev && "Missing prev section!");
1358 Prev->setFileSize(Prev->getFileSize() + Pad);
1362 // Layout the section fragments and its size.
1363 SD.setAddress(Address);
1365 Address += SD.getFileSize();
1370 // Layout the virtual sections.
1371 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1372 MCSectionData &SD = *it;
1374 if (!isVirtualSection(SD.getSection()))
1377 // Align this section if necessary by adding padding bytes to the previous
1379 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
1382 SD.setAddress(Address);
1384 Address += SD.getSize();
1387 // Scan the fixups in order and relax any that don't fit.
1388 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1389 MCSectionData &SD = *it;
1391 for (MCSectionData::iterator it2 = SD.begin(),
1392 ie2 = SD.end(); it2 != ie2; ++it2) {
1393 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
1397 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
1398 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
1399 MCAsmFixup &Fixup = *it3;
1401 // Check whether we need to relax this fixup.
1402 if (!FixupNeedsRelaxation(Fixup, DF))
1405 // Relax the instruction.
1407 // FIXME: This is a huge temporary hack which just looks for x86
1408 // branches; the only thing we need to relax on x86 is
1409 // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be
1410 // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax
1411 // an individual MCInst.
1412 SmallVectorImpl<char> &C = DF->getContents();
1413 uint64_t PrevOffset = Fixup.Offset;
1417 if (unsigned(C[Fixup.Offset-1]) >= 0x70 &&
1418 unsigned(C[Fixup.Offset-1]) <= 0x7f) {
1419 C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10;
1420 C[Fixup.Offset-1] = char(0x0f);
1425 } else if (C[Fixup.Offset-1] == char(0xeb)) {
1426 C[Fixup.Offset-1] = char(0xe9);
1430 llvm_unreachable("unknown 1 byte pcrel instruction!");
1432 Fixup.Value = MCBinaryExpr::Create(
1433 MCBinaryExpr::Sub, Fixup.Value,
1434 MCConstantExpr::Create(3, getContext()),
1436 C.insert(C.begin() + Fixup.Offset, Amt, char(0));
1437 Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte);
1439 // Update the remaining fixups, which have slid.
1441 // FIXME: This is bad for performance, but will be eliminated by the
1442 // move to MCInst specific fragments.
1444 for (; it3 != ie3; ++it3)
1447 // Update all the symbols for this fragment, which may have slid.
1449 // FIXME: This is really really bad for performance, but will be
1450 // eliminated by the move to MCInst specific fragments.
1451 for (MCAssembler::symbol_iterator it = symbol_begin(),
1452 ie = symbol_end(); it != ie; ++it) {
1453 MCSymbolData &SD = *it;
1455 if (it->getFragment() != DF)
1458 if (SD.getOffset() > PrevOffset)
1459 SD.setOffset(SD.getOffset() + Amt);
1464 // FIXME: This is O(N^2), but will be eliminated once we have a smart
1465 // MCAsmLayout object.
1474 // Debugging methods
1478 raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
1479 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
1480 << " Kind:" << AF.Kind << ">";
1486 void MCFragment::dump() {
1487 raw_ostream &OS = llvm::errs();
1489 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
1490 << " FileSize:" << FileSize;
1495 void MCAlignFragment::dump() {
1496 raw_ostream &OS = llvm::errs();
1498 OS << "<MCAlignFragment ";
1499 this->MCFragment::dump();
1501 OS << " Alignment:" << getAlignment()
1502 << " Value:" << getValue() << " ValueSize:" << getValueSize()
1503 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
1506 void MCDataFragment::dump() {
1507 raw_ostream &OS = llvm::errs();
1509 OS << "<MCDataFragment ";
1510 this->MCFragment::dump();
1512 OS << " Contents:[";
1513 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
1515 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1517 OS << "] (" << getContents().size() << " bytes)";
1519 if (!getFixups().empty()) {
1522 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
1523 if (it != fixup_begin()) OS << ",\n ";
1532 void MCFillFragment::dump() {
1533 raw_ostream &OS = llvm::errs();
1535 OS << "<MCFillFragment ";
1536 this->MCFragment::dump();
1538 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
1539 << " Count:" << getCount() << ">";
1542 void MCOrgFragment::dump() {
1543 raw_ostream &OS = llvm::errs();
1545 OS << "<MCOrgFragment ";
1546 this->MCFragment::dump();
1548 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
1551 void MCZeroFillFragment::dump() {
1552 raw_ostream &OS = llvm::errs();
1554 OS << "<MCZeroFillFragment ";
1555 this->MCFragment::dump();
1557 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
1560 void MCSectionData::dump() {
1561 raw_ostream &OS = llvm::errs();
1563 OS << "<MCSectionData";
1564 OS << " Alignment:" << getAlignment() << " Address:" << Address
1565 << " Size:" << Size << " FileSize:" << FileSize
1566 << " Fragments:[\n ";
1567 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1568 if (it != begin()) OS << ",\n ";
1574 void MCSymbolData::dump() {
1575 raw_ostream &OS = llvm::errs();
1577 OS << "<MCSymbolData Symbol:" << getSymbol()
1578 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1579 << " Flags:" << getFlags() << " Index:" << getIndex();
1581 OS << " (common, size:" << getCommonSize()
1582 << " align: " << getCommonAlignment() << ")";
1584 OS << " (external)";
1585 if (isPrivateExtern())
1586 OS << " (private extern)";
1590 void MCAssembler::dump() {
1591 raw_ostream &OS = llvm::errs();
1593 OS << "<MCAssembler\n";
1594 OS << " Sections:[\n ";
1595 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1596 if (it != begin()) OS << ",\n ";
1602 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1603 if (it != symbol_begin()) OS << ",\n ";