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/MCSectionMachO.h"
13 #include "llvm/Target/TargetMachOWriterInfo.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
24 class MachObjectWriter;
26 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
28 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
29 MachObjectWriter &MOW);
31 class MachObjectWriter {
32 // See <mach-o/loader.h>.
34 Header_Magic32 = 0xFEEDFACE,
35 Header_Magic64 = 0xFEEDFACF
38 static const unsigned Header32Size = 28;
39 static const unsigned Header64Size = 32;
40 static const unsigned SegmentLoadCommand32Size = 56;
41 static const unsigned Section32Size = 68;
42 static const unsigned SymtabLoadCommandSize = 24;
43 static const unsigned DysymtabLoadCommandSize = 80;
44 static const unsigned Nlist32Size = 12;
45 static const unsigned RelocationInfoSize = 8;
52 HF_SubsectionsViaSymbols = 0x2000
55 enum LoadCommandType {
61 // See <mach-o/nlist.h>.
68 enum SymbolTypeFlags {
69 // If any of these bits are set, then the entry is a stab entry number (see
70 // <mach-o/stab.h>. Otherwise the other masks apply.
71 STF_StabsEntryMask = 0xe0,
75 STF_PrivateExtern = 0x10
78 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
80 enum IndirectSymbolFlags {
81 ISF_Local = 0x80000000,
82 ISF_Absolute = 0x40000000
85 /// RelocationFlags - Special flags for addresses.
86 enum RelocationFlags {
87 RF_Scattered = 0x80000000
90 enum RelocationInfoType {
94 RIT_PreboundLazyPointer = 3,
95 RIT_LocalDifference = 4
98 /// MachSymbolData - Helper struct for containing some precomputed information
100 struct MachSymbolData {
101 MCSymbolData *SymbolData;
102 uint64_t StringIndex;
103 uint8_t SectionIndex;
105 // Support lexicographic sorting.
106 bool operator<(const MachSymbolData &RHS) const {
107 const std::string &Name = SymbolData->getSymbol().getName();
108 return Name < RHS.SymbolData->getSymbol().getName();
116 MachObjectWriter(raw_ostream &_OS, bool _IsLSB = true)
117 : OS(_OS), IsLSB(_IsLSB) {
120 /// @name Helper Methods
123 void Write8(uint8_t Value) {
127 void Write16(uint16_t Value) {
129 Write8(uint8_t(Value >> 0));
130 Write8(uint8_t(Value >> 8));
132 Write8(uint8_t(Value >> 8));
133 Write8(uint8_t(Value >> 0));
137 void Write32(uint32_t Value) {
139 Write16(uint16_t(Value >> 0));
140 Write16(uint16_t(Value >> 16));
142 Write16(uint16_t(Value >> 16));
143 Write16(uint16_t(Value >> 0));
147 void Write64(uint64_t Value) {
149 Write32(uint32_t(Value >> 0));
150 Write32(uint32_t(Value >> 32));
152 Write32(uint32_t(Value >> 32));
153 Write32(uint32_t(Value >> 0));
157 void WriteZeros(unsigned N) {
158 const char Zeros[16] = { 0 };
160 for (unsigned i = 0, e = N / 16; i != e; ++i)
161 OS << StringRef(Zeros, 16);
163 OS << StringRef(Zeros, N % 16);
166 void WriteString(const StringRef &Str, unsigned ZeroFillSize = 0) {
169 WriteZeros(ZeroFillSize - Str.size());
174 void WriteHeader32(unsigned NumLoadCommands, unsigned LoadCommandsSize,
175 bool SubsectionsViaSymbols) {
178 if (SubsectionsViaSymbols)
179 Flags |= HF_SubsectionsViaSymbols;
181 // struct mach_header (28 bytes)
183 uint64_t Start = OS.tell();
186 Write32(Header_Magic32);
188 // FIXME: Support cputype.
189 Write32(TargetMachOWriterInfo::HDR_CPU_TYPE_I386);
190 // FIXME: Support cpusubtype.
191 Write32(TargetMachOWriterInfo::HDR_CPU_SUBTYPE_I386_ALL);
193 Write32(NumLoadCommands); // Object files have a single load command, the
195 Write32(LoadCommandsSize);
198 assert(OS.tell() - Start == Header32Size);
201 /// WriteSegmentLoadCommand32 - Write a 32-bit segment load command.
203 /// \arg NumSections - The number of sections in this segment.
204 /// \arg SectionDataSize - The total size of the sections.
205 void WriteSegmentLoadCommand32(unsigned NumSections,
206 uint64_t SectionDataStartOffset,
207 uint64_t SectionDataSize) {
208 // struct segment_command (56 bytes)
210 uint64_t Start = OS.tell();
213 Write32(LCT_Segment);
214 Write32(SegmentLoadCommand32Size + NumSections * Section32Size);
217 Write32(0); // vmaddr
218 Write32(SectionDataSize); // vmsize
219 Write32(SectionDataStartOffset); // file offset
220 Write32(SectionDataSize); // file size
221 Write32(0x7); // maxprot
222 Write32(0x7); // initprot
223 Write32(NumSections);
226 assert(OS.tell() - Start == SegmentLoadCommand32Size);
229 void WriteSection32(const MCSectionData &SD, uint64_t FileOffset,
230 uint64_t RelocationsStart, unsigned NumRelocations) {
231 // struct section (68 bytes)
233 uint64_t Start = OS.tell();
236 // FIXME: cast<> support!
237 const MCSectionMachO &Section =
238 static_cast<const MCSectionMachO&>(SD.getSection());
239 WriteString(Section.getSectionName(), 16);
240 WriteString(Section.getSegmentName(), 16);
241 Write32(SD.getAddress()); // address
242 Write32(SD.getSize()); // size
245 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
246 Write32(Log2_32(SD.getAlignment()));
247 Write32(NumRelocations ? RelocationsStart : 0);
248 Write32(NumRelocations);
249 Write32(Section.getTypeAndAttributes());
250 Write32(0); // reserved1
251 Write32(Section.getStubSize()); // reserved2
253 assert(OS.tell() - Start == Section32Size);
256 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
257 uint32_t StringTableOffset,
258 uint32_t StringTableSize) {
259 // struct symtab_command (24 bytes)
261 uint64_t Start = OS.tell();
265 Write32(SymtabLoadCommandSize);
266 Write32(SymbolOffset);
268 Write32(StringTableOffset);
269 Write32(StringTableSize);
271 assert(OS.tell() - Start == SymtabLoadCommandSize);
274 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
275 uint32_t NumLocalSymbols,
276 uint32_t FirstExternalSymbol,
277 uint32_t NumExternalSymbols,
278 uint32_t FirstUndefinedSymbol,
279 uint32_t NumUndefinedSymbols,
280 uint32_t IndirectSymbolOffset,
281 uint32_t NumIndirectSymbols) {
282 // struct dysymtab_command (80 bytes)
284 uint64_t Start = OS.tell();
287 Write32(LCT_Dysymtab);
288 Write32(DysymtabLoadCommandSize);
289 Write32(FirstLocalSymbol);
290 Write32(NumLocalSymbols);
291 Write32(FirstExternalSymbol);
292 Write32(NumExternalSymbols);
293 Write32(FirstUndefinedSymbol);
294 Write32(NumUndefinedSymbols);
295 Write32(0); // tocoff
297 Write32(0); // modtaboff
298 Write32(0); // nmodtab
299 Write32(0); // extrefsymoff
300 Write32(0); // nextrefsyms
301 Write32(IndirectSymbolOffset);
302 Write32(NumIndirectSymbols);
303 Write32(0); // extreloff
304 Write32(0); // nextrel
305 Write32(0); // locreloff
306 Write32(0); // nlocrel
308 assert(OS.tell() - Start == DysymtabLoadCommandSize);
311 void WriteNlist32(MachSymbolData &MSD) {
312 MCSymbolData &Data = *MSD.SymbolData;
313 MCSymbol &Symbol = Data.getSymbol();
316 // Set the N_TYPE bits. See <mach-o/nlist.h>.
318 // FIXME: Are the prebound or indirect fields possible here?
319 if (Symbol.isUndefined())
320 Type = STT_Undefined;
321 else if (Symbol.isAbsolute())
326 // FIXME: Set STAB bits.
328 if (Data.isPrivateExtern())
329 Type |= STF_PrivateExtern;
332 if (Data.isExternal() || Symbol.isUndefined())
333 Type |= STF_External;
335 // struct nlist (12 bytes)
337 Write32(MSD.StringIndex);
339 Write8(MSD.SectionIndex);
341 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
343 Write16(Data.getFlags() & 0xFFFF);
345 // Write the symbol address.
346 uint32_t Address = 0;
347 if (Symbol.isDefined()) {
348 if (Symbol.isAbsolute()) {
349 llvm_unreachable("FIXME: Not yet implemented!");
351 Address = Data.getFragment()->getAddress() + Data.getOffset();
357 struct MachRelocationEntry {
361 void ComputeScatteredRelocationInfo(MCAssembler &Asm,
362 MCSectionData::Fixup &Fixup,
363 DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
364 std::vector<MachRelocationEntry> &Relocs) {
365 uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset;
366 unsigned IsPCRel = 0;
367 unsigned Type = RIT_Vanilla;
371 const MCSymbol *A = Fixup.Value.getSymA();
372 MCSymbolData *SD = SymbolMap.lookup(A);
373 uint32_t Value = SD->getFragment()->getAddress() + SD->getOffset();
376 if (const MCSymbol *B = Fixup.Value.getSymB()) {
377 Type = RIT_LocalDifference;
379 MCSymbolData *SD = SymbolMap.lookup(B);
380 Value2 = SD->getFragment()->getAddress() + SD->getOffset();
383 unsigned Log2Size = Log2_32(Fixup.Size);
384 assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
386 // The value which goes in the fixup is current value of the expression.
387 Fixup.FixedValue = Value - Value2 + Fixup.Value.getConstant();
389 MachRelocationEntry MRE;
390 MRE.Word0 = ((Address << 0) |
396 Relocs.push_back(MRE);
398 if (Type == RIT_LocalDifference) {
401 MachRelocationEntry MRE;
402 MRE.Word0 = ((0 << 0) |
408 Relocs.push_back(MRE);
412 void ComputeRelocationInfo(MCAssembler &Asm,
413 MCSectionData::Fixup &Fixup,
414 DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap,
415 std::vector<MachRelocationEntry> &Relocs) {
416 // If this is a local symbol plus an offset or a difference, then we need a
417 // scattered relocation entry.
418 if (Fixup.Value.getSymB()) // a - b
419 return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs);
420 if (Fixup.Value.getSymA() && Fixup.Value.getConstant())
421 if (!Fixup.Value.getSymA()->isUndefined())
422 return ComputeScatteredRelocationInfo(Asm, Fixup, SymbolMap, Relocs);
425 uint32_t Address = Fixup.Fragment->getOffset() + Fixup.Offset;
428 unsigned IsPCRel = 0;
429 unsigned IsExtern = 0;
432 if (Fixup.Value.isAbsolute()) { // constant
433 // SymbolNum of 0 indicates the absolute section.
436 llvm_unreachable("FIXME: Not yet implemented!");
438 const MCSymbol *Symbol = Fixup.Value.getSymA();
439 MCSymbolData *SD = SymbolMap.lookup(Symbol);
441 if (Symbol->isUndefined()) {
443 Index = SD->getIndex();
446 // The index is the section ordinal.
450 for (MCAssembler::iterator it = Asm.begin(),
451 ie = Asm.end(); it != ie; ++it, ++Index)
452 if (&*it == SD->getFragment()->getParent())
454 Value = SD->getFragment()->getAddress() + SD->getOffset();
460 // The value which goes in the fixup is current value of the expression.
461 Fixup.FixedValue = Value + Fixup.Value.getConstant();
463 unsigned Log2Size = Log2_32(Fixup.Size);
464 assert((1U << Log2Size) == Fixup.Size && "Invalid fixup size!");
466 // struct relocation_info (8 bytes)
467 MachRelocationEntry MRE;
469 MRE.Word1 = ((Index << 0) |
474 Relocs.push_back(MRE);
477 void BindIndirectSymbols(MCAssembler &Asm,
478 DenseMap<const MCSymbol*,MCSymbolData*> &SymbolMap) {
479 // This is the point where 'as' creates actual symbols for indirect symbols
480 // (in the following two passes). It would be easier for us to do this
481 // sooner when we see the attribute, but that makes getting the order in the
482 // symbol table much more complicated than it is worth.
484 // FIXME: Revisit this when the dust settles.
486 // Bind non lazy symbol pointers first.
487 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
488 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
489 // FIXME: cast<> support!
490 const MCSectionMachO &Section =
491 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
494 Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
495 if (Type != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
498 MCSymbolData *&Entry = SymbolMap[it->Symbol];
500 Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm);
503 // Then lazy symbol pointers and symbol stubs.
504 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
505 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
506 // FIXME: cast<> support!
507 const MCSectionMachO &Section =
508 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
511 Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
512 if (Type != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
513 Type != MCSectionMachO::S_SYMBOL_STUBS)
516 MCSymbolData *&Entry = SymbolMap[it->Symbol];
518 Entry = new MCSymbolData(*it->Symbol, 0, 0, &Asm);
520 // Set the symbol type to undefined lazy, but only on construction.
522 // FIXME: Do not hardcode.
523 Entry->setFlags(Entry->getFlags() | 0x0001);
528 /// ComputeSymbolTable - Compute the symbol table data
530 /// \param StringTable [out] - The string table data.
531 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
533 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
534 std::vector<MachSymbolData> &LocalSymbolData,
535 std::vector<MachSymbolData> &ExternalSymbolData,
536 std::vector<MachSymbolData> &UndefinedSymbolData) {
537 // Build section lookup table.
538 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
540 for (MCAssembler::iterator it = Asm.begin(),
541 ie = Asm.end(); it != ie; ++it, ++Index)
542 SectionIndexMap[&it->getSection()] = Index;
543 assert(Index <= 256 && "Too many sections!");
545 // Index 0 is always the empty string.
546 StringMap<uint64_t> StringIndexMap;
547 StringTable += '\x00';
549 // Build the symbol arrays and the string table, but only for non-local
552 // The particular order that we collect the symbols and create the string
553 // table, then sort the symbols is chosen to match 'as'. Even though it
554 // doesn't matter for correctness, this is important for letting us diff .o
556 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
557 ie = Asm.symbol_end(); it != ie; ++it) {
558 MCSymbol &Symbol = it->getSymbol();
560 // Ignore assembler temporaries.
561 if (it->getSymbol().isTemporary())
564 if (!it->isExternal() && !Symbol.isUndefined())
567 uint64_t &Entry = StringIndexMap[Symbol.getName()];
569 Entry = StringTable.size();
570 StringTable += Symbol.getName();
571 StringTable += '\x00';
576 MSD.StringIndex = Entry;
578 if (Symbol.isUndefined()) {
579 MSD.SectionIndex = 0;
580 UndefinedSymbolData.push_back(MSD);
581 } else if (Symbol.isAbsolute()) {
582 MSD.SectionIndex = 0;
583 ExternalSymbolData.push_back(MSD);
585 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
586 assert(MSD.SectionIndex && "Invalid section index!");
587 ExternalSymbolData.push_back(MSD);
591 // Now add the data for local symbols.
592 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
593 ie = Asm.symbol_end(); it != ie; ++it) {
594 MCSymbol &Symbol = it->getSymbol();
596 // Ignore assembler temporaries.
597 if (it->getSymbol().isTemporary())
600 if (it->isExternal() || Symbol.isUndefined())
603 uint64_t &Entry = StringIndexMap[Symbol.getName()];
605 Entry = StringTable.size();
606 StringTable += Symbol.getName();
607 StringTable += '\x00';
612 MSD.StringIndex = Entry;
614 if (Symbol.isAbsolute()) {
615 MSD.SectionIndex = 0;
616 LocalSymbolData.push_back(MSD);
618 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
619 assert(MSD.SectionIndex && "Invalid section index!");
620 LocalSymbolData.push_back(MSD);
624 // External and undefined symbols are required to be in lexicographic order.
625 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
626 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
628 // Set the symbol indices.
630 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
631 LocalSymbolData[i].SymbolData->setIndex(Index++);
632 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
633 ExternalSymbolData[i].SymbolData->setIndex(Index++);
634 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
635 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
637 // The string table is padded to a multiple of 4.
639 // FIXME: Check to see if this varies per arch.
640 while (StringTable.size() % 4)
641 StringTable += '\x00';
644 void WriteObject(MCAssembler &Asm) {
645 unsigned NumSections = Asm.size();
647 // Compute the symbol -> symbol data map.
649 // FIXME: This should not be here.
650 DenseMap<const MCSymbol*, MCSymbolData *> SymbolMap;
651 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
652 ie = Asm.symbol_end(); it != ie; ++it)
653 SymbolMap[&it->getSymbol()] = it;
655 // Create symbol data for any indirect symbols.
656 BindIndirectSymbols(Asm, SymbolMap);
658 // Compute symbol table information.
659 SmallString<256> StringTable;
660 std::vector<MachSymbolData> LocalSymbolData;
661 std::vector<MachSymbolData> ExternalSymbolData;
662 std::vector<MachSymbolData> UndefinedSymbolData;
663 unsigned NumSymbols = Asm.symbol_size();
665 // No symbol table command is written if there are no symbols.
667 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
668 UndefinedSymbolData);
670 // The section data starts after the header, the segment load command (and
671 // section headers) and the symbol table.
672 unsigned NumLoadCommands = 1;
673 uint64_t LoadCommandsSize =
674 SegmentLoadCommand32Size + NumSections * Section32Size;
676 // Add the symbol table load command sizes, if used.
678 NumLoadCommands += 2;
679 LoadCommandsSize += SymtabLoadCommandSize + DysymtabLoadCommandSize;
682 uint64_t SectionDataStart = Header32Size + LoadCommandsSize;
683 uint64_t SectionDataEnd = SectionDataStart;
684 uint64_t SectionDataSize = 0;
685 if (!Asm.getSectionList().empty()) {
686 MCSectionData &SD = Asm.getSectionList().back();
687 SectionDataSize = SD.getAddress() + SD.getSize();
688 SectionDataEnd = SectionDataStart + SD.getAddress() + SD.getFileSize();
691 // Write the prolog, starting with the header and load command...
692 WriteHeader32(NumLoadCommands, LoadCommandsSize,
693 Asm.getSubsectionsViaSymbols());
694 WriteSegmentLoadCommand32(NumSections, SectionDataStart, SectionDataSize);
696 // ... and then the section headers.
698 // We also compute the section relocations while we do this. Note that
699 // compute relocation info will also update the fixup to have the correct
700 // value; this will be overwrite the appropriate data in the fragment when
702 std::vector<MachRelocationEntry> RelocInfos;
703 uint64_t RelocTableEnd = SectionDataEnd;
704 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie;
706 MCSectionData &SD = *it;
708 // The assembler writes relocations in the reverse order they were seen.
710 // FIXME: It is probably more complicated than this.
711 unsigned NumRelocsStart = RelocInfos.size();
712 for (unsigned i = 0, e = SD.fixup_size(); i != e; ++i)
713 ComputeRelocationInfo(Asm, SD.getFixups()[e - i - 1], SymbolMap,
716 unsigned NumRelocs = RelocInfos.size() - NumRelocsStart;
717 uint64_t SectionStart = SectionDataStart + SD.getAddress();
718 WriteSection32(SD, SectionStart, RelocTableEnd, NumRelocs);
719 RelocTableEnd += NumRelocs * RelocationInfoSize;
722 // Write the symbol table load command, if used.
724 unsigned FirstLocalSymbol = 0;
725 unsigned NumLocalSymbols = LocalSymbolData.size();
726 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
727 unsigned NumExternalSymbols = ExternalSymbolData.size();
728 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
729 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
730 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
731 unsigned NumSymTabSymbols =
732 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
733 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
734 uint64_t IndirectSymbolOffset = 0;
736 // If used, the indirect symbols are written after the section data.
737 if (NumIndirectSymbols)
738 IndirectSymbolOffset = RelocTableEnd;
740 // The symbol table is written after the indirect symbol data.
741 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
743 // The string table is written after symbol table.
744 uint64_t StringTableOffset =
745 SymbolTableOffset + NumSymTabSymbols * Nlist32Size;
746 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
747 StringTableOffset, StringTable.size());
749 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
750 FirstExternalSymbol, NumExternalSymbols,
751 FirstUndefinedSymbol, NumUndefinedSymbols,
752 IndirectSymbolOffset, NumIndirectSymbols);
755 // Write the actual section data.
756 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
757 WriteFileData(OS, *it, *this);
759 // Write the relocation entries.
760 for (unsigned i = 0, e = RelocInfos.size(); i != e; ++i) {
761 Write32(RelocInfos[i].Word0);
762 Write32(RelocInfos[i].Word1);
765 // Write the symbol table data, if used.
767 // Write the indirect symbol entries.
768 for (MCAssembler::indirect_symbol_iterator
769 it = Asm.indirect_symbol_begin(),
770 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
771 // Indirect symbols in the non lazy symbol pointer section have some
773 const MCSectionMachO &Section =
774 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
776 Section.getTypeAndAttributes() & MCSectionMachO::SECTION_TYPE;
777 if (Type == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
778 // If this symbol is defined and internal, mark it as such.
779 if (it->Symbol->isDefined() &&
780 !SymbolMap.lookup(it->Symbol)->isExternal()) {
781 uint32_t Flags = ISF_Local;
782 if (it->Symbol->isAbsolute())
783 Flags |= ISF_Absolute;
789 Write32(SymbolMap[it->Symbol]->getIndex());
792 // FIXME: Check that offsets match computed ones.
794 // Write the symbol table entries.
795 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
796 WriteNlist32(LocalSymbolData[i]);
797 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
798 WriteNlist32(ExternalSymbolData[i]);
799 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
800 WriteNlist32(UndefinedSymbolData[i]);
802 // Write the string table.
803 OS << StringTable.str();
810 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
813 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
816 FileSize(~UINT64_C(0))
819 Parent->getFragmentList().push_back(this);
822 MCFragment::~MCFragment() {
825 uint64_t MCFragment::getAddress() const {
826 assert(getParent() && "Missing Section!");
827 return getParent()->getAddress() + Offset;
832 MCSectionData::MCSectionData() : Section(0) {}
834 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
835 : Section(&_Section),
837 Address(~UINT64_C(0)),
839 FileSize(~UINT64_C(0)),
843 A->getSectionList().push_back(this);
846 const MCSectionData::Fixup *
847 MCSectionData::LookupFixup(const MCFragment *Fragment, uint64_t Offset) const {
848 // Use a one level cache to turn the common case of accessing the fixups in
849 // order into O(1) instead of O(N).
850 unsigned i = LastFixupLookup, Count = Fixups.size(), End = Fixups.size();
854 const Fixup &F = Fixups[i];
855 if (F.Fragment == Fragment && F.Offset == Offset) {
870 MCSymbolData::MCSymbolData() : Symbol(*(MCSymbol*)0) {}
872 MCSymbolData::MCSymbolData(MCSymbol &_Symbol, MCFragment *_Fragment,
873 uint64_t _Offset, MCAssembler *A)
874 : Symbol(_Symbol), Fragment(_Fragment), Offset(_Offset),
875 IsExternal(false), IsPrivateExtern(false), Flags(0), Index(0)
878 A->getSymbolList().push_back(this);
883 MCAssembler::MCAssembler(raw_ostream &_OS)
885 SubsectionsViaSymbols(false)
889 MCAssembler::~MCAssembler() {
892 void MCAssembler::LayoutSection(MCSectionData &SD, unsigned NextAlign) {
893 uint64_t Address = SD.getAddress();
895 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
898 F.setOffset(Address - SD.getAddress());
900 // Evaluate fragment size.
901 switch (F.getKind()) {
902 case MCFragment::FT_Align: {
903 MCAlignFragment &AF = cast<MCAlignFragment>(F);
905 uint64_t Size = RoundUpToAlignment(Address, AF.getAlignment()) - Address;
906 if (Size > AF.getMaxBytesToEmit())
909 AF.setFileSize(Size);
913 case MCFragment::FT_Data:
914 F.setFileSize(F.getMaxFileSize());
917 case MCFragment::FT_Fill: {
918 MCFillFragment &FF = cast<MCFillFragment>(F);
920 F.setFileSize(F.getMaxFileSize());
922 // If the fill value is constant, thats it.
923 if (FF.getValue().isAbsolute())
926 // Otherwise, add fixups for the values.
927 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
928 MCSectionData::Fixup Fix(F, i * FF.getValueSize(),
929 FF.getValue(),FF.getValueSize());
930 SD.getFixups().push_back(Fix);
935 case MCFragment::FT_Org: {
936 MCOrgFragment &OF = cast<MCOrgFragment>(F);
938 if (!OF.getOffset().isAbsolute())
939 llvm_unreachable("FIXME: Not yet implemented!");
940 uint64_t OrgOffset = OF.getOffset().getConstant();
941 uint64_t Offset = Address - SD.getAddress();
943 // FIXME: We need a way to communicate this error.
944 if (OrgOffset < Offset)
945 llvm_report_error("invalid .org offset '" + Twine(OrgOffset) +
946 "' (at offset '" + Twine(Offset) + "'");
948 F.setFileSize(OrgOffset - Offset);
953 Address += F.getFileSize();
956 // Set the section sizes.
957 SD.setSize(Address - SD.getAddress());
958 SD.setFileSize(RoundUpToAlignment(Address, NextAlign) - SD.getAddress());
961 /// WriteFileData - Write the \arg F data to the output file.
962 static void WriteFileData(raw_ostream &OS, const MCFragment &F,
963 MachObjectWriter &MOW) {
964 uint64_t Start = OS.tell();
969 // FIXME: Embed in fragments instead?
970 switch (F.getKind()) {
971 case MCFragment::FT_Align: {
972 MCAlignFragment &AF = cast<MCAlignFragment>(F);
973 uint64_t Count = AF.getFileSize() / AF.getValueSize();
975 // FIXME: This error shouldn't actually occur (the front end should emit
976 // multiple .align directives to enforce the semantics it wants), but is
977 // severe enough that we want to report it. How to handle this?
978 if (Count * AF.getValueSize() != AF.getFileSize())
979 llvm_report_error("undefined .align directive, value size '" +
980 Twine(AF.getValueSize()) +
981 "' is not a divisor of padding size '" +
982 Twine(AF.getFileSize()) + "'");
984 for (uint64_t i = 0; i != Count; ++i) {
985 switch (AF.getValueSize()) {
987 assert(0 && "Invalid size!");
988 case 1: MOW.Write8 (uint8_t (AF.getValue())); break;
989 case 2: MOW.Write16(uint16_t(AF.getValue())); break;
990 case 4: MOW.Write32(uint32_t(AF.getValue())); break;
991 case 8: MOW.Write64(uint64_t(AF.getValue())); break;
997 case MCFragment::FT_Data:
998 OS << cast<MCDataFragment>(F).getContents().str();
1001 case MCFragment::FT_Fill: {
1002 MCFillFragment &FF = cast<MCFillFragment>(F);
1005 if (FF.getValue().isAbsolute())
1006 Value = FF.getValue().getConstant();
1007 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
1008 if (!FF.getValue().isAbsolute()) {
1011 // FIXME: Find a better way to write in the fixes.
1012 const MCSectionData::Fixup *Fixup =
1013 F.getParent()->LookupFixup(&F, i * FF.getValueSize());
1014 assert(Fixup && "Missing fixup for fill value!");
1015 Value = Fixup->FixedValue;
1018 switch (FF.getValueSize()) {
1020 assert(0 && "Invalid size!");
1021 case 1: MOW.Write8 (uint8_t (Value)); break;
1022 case 2: MOW.Write16(uint16_t(Value)); break;
1023 case 4: MOW.Write32(uint32_t(Value)); break;
1024 case 8: MOW.Write64(uint64_t(Value)); break;
1030 case MCFragment::FT_Org: {
1031 MCOrgFragment &OF = cast<MCOrgFragment>(F);
1033 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
1034 MOW.Write8(uint8_t(OF.getValue()));
1040 assert(OS.tell() - Start == F.getFileSize());
1043 /// WriteFileData - Write the \arg SD data to the output file.
1044 static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
1045 MachObjectWriter &MOW) {
1046 uint64_t Start = OS.tell();
1049 for (MCSectionData::const_iterator it = SD.begin(),
1050 ie = SD.end(); it != ie; ++it)
1051 WriteFileData(OS, *it, MOW);
1053 // Add section padding.
1054 assert(SD.getFileSize() >= SD.getSize() && "Invalid section sizes!");
1055 MOW.WriteZeros(SD.getFileSize() - SD.getSize());
1057 assert(OS.tell() - Start == SD.getFileSize());
1060 void MCAssembler::Finish() {
1061 // Layout the sections and fragments.
1062 uint64_t Address = 0;
1063 for (iterator it = begin(), ie = end(); it != ie;) {
1064 MCSectionData &SD = *it;
1066 // Select the amount of padding alignment we need, based on either the next
1067 // sections alignment or the default alignment.
1069 // FIXME: This should probably match the native word size.
1070 unsigned NextAlign = 4;
1073 NextAlign = it->getAlignment();
1075 // Layout the section fragments and its size.
1076 SD.setAddress(Address);
1077 LayoutSection(SD, NextAlign);
1078 Address += SD.getFileSize();
1081 // Write the object file.
1082 MachObjectWriter MOW(OS);
1083 MOW.WriteObject(*this);