diff options
Diffstat (limited to 'lib/MC')
43 files changed, 1816 insertions, 1426 deletions
diff --git a/lib/MC/CMakeLists.txt b/lib/MC/CMakeLists.txt index 6554d6a9e60e..8c015644d8ad 100644 --- a/lib/MC/CMakeLists.txt +++ b/lib/MC/CMakeLists.txt @@ -15,6 +15,7 @@ add_llvm_library(LLVMMC MCELFObjectTargetWriter.cpp MCELFStreamer.cpp MCExpr.cpp + MCFragment.cpp MCInst.cpp MCInstPrinter.cpp MCInstrAnalysis.cpp diff --git a/lib/MC/ConstantPools.cpp b/lib/MC/ConstantPools.cpp index f7649fba6e89..9643b7594682 100644 --- a/lib/MC/ConstantPools.cpp +++ b/lib/MC/ConstantPools.cpp @@ -29,17 +29,17 @@ void ConstantPool::emitEntries(MCStreamer &Streamer) { I != E; ++I) { Streamer.EmitCodeAlignment(I->Size); // align naturally Streamer.EmitLabel(I->Label); - Streamer.EmitValue(I->Value, I->Size); + Streamer.EmitValue(I->Value, I->Size, I->Loc); } Streamer.EmitDataRegion(MCDR_DataRegionEnd); Entries.clear(); } const MCExpr *ConstantPool::addEntry(const MCExpr *Value, MCContext &Context, - unsigned Size) { + unsigned Size, SMLoc Loc) { MCSymbol *CPEntryLabel = Context.createTempSymbol(); - Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size)); + Entries.push_back(ConstantPoolEntry(CPEntryLabel, Value, Size, Loc)); return MCSymbolRefExpr::create(CPEntryLabel, Context); } @@ -90,8 +90,8 @@ void AssemblerConstantPools::emitForCurrentSection(MCStreamer &Streamer) { const MCExpr *AssemblerConstantPools::addEntry(MCStreamer &Streamer, const MCExpr *Expr, - unsigned Size) { + unsigned Size, SMLoc Loc) { MCSection *Section = Streamer.getCurrentSection().first; return getOrCreateConstantPool(Section).addEntry(Expr, Streamer.getContext(), - Size); + Size, Loc); } diff --git a/lib/MC/ELFObjectWriter.cpp b/lib/MC/ELFObjectWriter.cpp index e925bc272dc8..e6552beefd01 100644 --- a/lib/MC/ELFObjectWriter.cpp +++ b/lib/MC/ELFObjectWriter.cpp @@ -33,6 +33,7 @@ #include "llvm/Support/ELF.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/StringSaver.h" #include <vector> using namespace llvm; @@ -106,7 +107,9 @@ class ELFObjectWriter : public MCObjectWriter { /// @name Symbol Table Data /// @{ - StringTableBuilder StrTabBuilder; + BumpPtrAllocator Alloc; + StringSaver VersionSymSaver{Alloc}; + StringTableBuilder StrTabBuilder{StringTableBuilder::ELF}; /// @} @@ -157,9 +160,9 @@ class ELFObjectWriter : public MCObjectWriter { template <typename T> void write(T Val) { if (IsLittleEndian) - support::endian::Writer<support::little>(OS).write(Val); + support::endian::Writer<support::little>(getStream()).write(Val); else - support::endian::Writer<support::big>(OS).write(Val); + support::endian::Writer<support::big>(getStream()).write(Val); } void writeHeader(const MCAssembler &Asm); @@ -232,7 +235,7 @@ class ELFObjectWriter : public MCObjectWriter { } void ELFObjectWriter::align(unsigned Alignment) { - uint64_t Padding = OffsetToAlignment(OS.tell(), Alignment); + uint64_t Padding = OffsetToAlignment(getStream().tell(), Alignment); WriteZeros(Padding); } @@ -447,9 +450,6 @@ void ELFObjectWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, ELFSymbolData &MSD, const MCAsmLayout &Layout) { const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol); - assert((!Symbol.getFragment() || - (Symbol.getFragment()->getParent() == &Symbol.getSection())) && - "The symbol's section doesn't match the fragment's symbol"); const MCSymbolELF *Base = cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol)); @@ -630,28 +630,36 @@ void ELFObjectWriter::recordRelocation(MCAssembler &Asm, // In general, ELF has no relocations for -B. It can only represent (A + C) // or (A + C - R). If B = R + K and the relocation is not pcrel, we can // replace B to implement it: (A - R - K + C) - if (IsPCRel) - Asm.getContext().reportFatalError( + if (IsPCRel) { + Asm.getContext().reportError( Fixup.getLoc(), "No relocation available to represent this relative expression"); + return; + } const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol()); - if (SymB.isUndefined()) - Asm.getContext().reportFatalError( + if (SymB.isUndefined()) { + Asm.getContext().reportError( Fixup.getLoc(), Twine("symbol '") + SymB.getName() + "' can not be undefined in a subtraction expression"); + return; + } assert(!SymB.isAbsolute() && "Should have been folded"); const MCSection &SecB = SymB.getSection(); - if (&SecB != &FixupSection) - Asm.getContext().reportFatalError( + if (&SecB != &FixupSection) { + Asm.getContext().reportError( Fixup.getLoc(), "Cannot represent a difference across sections"); + return; + } - if (::isWeak(SymB)) - Asm.getContext().reportFatalError( + if (::isWeak(SymB)) { + Asm.getContext().reportError( Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol"); + return; + } uint64_t SymBOffset = Layout.getSymbolOffset(SymB); uint64_t K = SymBOffset - FixupOffset; @@ -764,7 +772,7 @@ void ELFObjectWriter::computeSymbolTable( SymbolTableIndex = addToSectionTable(SymtabSection); align(SymtabSection->getAlignment()); - uint64_t SecStart = OS.tell(); + uint64_t SecStart = getStream().tell(); // The first entry is the undefined symbol entry. Writer.writeSymbol(0, 0, 0, 0, 0, 0, false); @@ -784,8 +792,10 @@ void ELFObjectWriter::computeSymbolTable( Renames.count(&Symbol))) continue; - if (Symbol.isTemporary() && Symbol.isUndefined()) - Ctx.reportFatalError(SMLoc(), "Undefined temporary"); + if (Symbol.isTemporary() && Symbol.isUndefined()) { + Ctx.reportError(SMLoc(), "Undefined temporary symbol"); + continue; + } ELFSymbolData MSD; MSD.Symbol = cast<MCSymbolELF>(&Symbol); @@ -850,13 +860,15 @@ void ELFObjectWriter::computeSymbolTable( Buf += Name.substr(0, Pos); unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; Buf += Name.substr(Pos + Skip); - Name = Buf; + Name = VersionSymSaver.save(Buf.c_str()); } } // Sections have their own string table - if (Symbol.getType() != ELF::STT_SECTION) - MSD.Name = StrTabBuilder.add(Name); + if (Symbol.getType() != ELF::STT_SECTION) { + MSD.Name = Name; + StrTabBuilder.add(Name); + } if (Local) LocalSymbolData.push_back(MSD); @@ -878,7 +890,7 @@ void ELFObjectWriter::computeSymbolTable( for (const std::string &Name : FileNames) StrTabBuilder.add(Name); - StrTabBuilder.finalize(StringTableBuilder::ELF); + StrTabBuilder.finalize(); for (const std::string &Name : FileNames) Writer.writeSymbol(StrTabBuilder.getOffset(Name), @@ -911,7 +923,7 @@ void ELFObjectWriter::computeSymbolTable( assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL); } - uint64_t SecEnd = OS.tell(); + uint64_t SecEnd = getStream().tell(); SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd); ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes(); @@ -921,12 +933,12 @@ void ELFObjectWriter::computeSymbolTable( } assert(SymtabShndxSectionIndex != 0); - SecStart = OS.tell(); + SecStart = getStream().tell(); const MCSectionELF *SymtabShndxSection = SectionTable[SymtabShndxSectionIndex - 1]; for (uint32_t Index : ShndxIndexes) write(Index); - SecEnd = OS.tell(); + SecEnd = getStream().tell(); SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd); } @@ -957,31 +969,6 @@ ELFObjectWriter::createRelocationSection(MCContext &Ctx, return RelaSection; } -static SmallVector<char, 128> -getUncompressedData(const MCAsmLayout &Layout, - const MCSection::FragmentListType &Fragments) { - SmallVector<char, 128> UncompressedData; - for (const MCFragment &F : Fragments) { - const SmallVectorImpl<char> *Contents; - switch (F.getKind()) { - case MCFragment::FT_Data: - Contents = &cast<MCDataFragment>(F).getContents(); - break; - case MCFragment::FT_Dwarf: - Contents = &cast<MCDwarfLineAddrFragment>(F).getContents(); - break; - case MCFragment::FT_DwarfFrame: - Contents = &cast<MCDwarfCallFrameFragment>(F).getContents(); - break; - default: - llvm_unreachable( - "Not expecting any other fragment types in a debug_* section"); - } - UncompressedData.append(Contents->begin(), Contents->end()); - } - return UncompressedData; -} - // Include the debug info compression header: // "ZLIB" followed by 8 bytes representing the uncompressed size of the section, // useful for consumers to preallocate a buffer to decompress into. @@ -1016,27 +1003,29 @@ void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec, return; } - // Gather the uncompressed data from all the fragments. - const MCSection::FragmentListType &Fragments = Section.getFragmentList(); - SmallVector<char, 128> UncompressedData = - getUncompressedData(Layout, Fragments); + SmallVector<char, 128> UncompressedData; + raw_svector_ostream VecOS(UncompressedData); + raw_pwrite_stream &OldStream = getStream(); + setStream(VecOS); + Asm.writeSectionData(&Section, Layout); + setStream(OldStream); SmallVector<char, 128> CompressedContents; zlib::Status Success = zlib::compress( StringRef(UncompressedData.data(), UncompressedData.size()), CompressedContents); if (Success != zlib::StatusOK) { - Asm.writeSectionData(&Section, Layout); + getStream() << UncompressedData; return; } if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) { - Asm.writeSectionData(&Section, Layout); + getStream() << UncompressedData; return; } Asm.getContext().renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str()); - OS << CompressedContents; + getStream() << CompressedContents; } void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, @@ -1061,8 +1050,13 @@ void ELFObjectWriter::writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec) { std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec]; - // Sort the relocation entries. Most targets just sort by Offset, but some - // (e.g., MIPS) have additional constraints. + // We record relocations by pushing to the end of a vector. Reverse the vector + // to get the relocations in the order they were created. + // In most cases that is not important, but it can be for special sections + // (.eh_frame) or specific relocations (TLS optimizations on SystemZ). + std::reverse(Relocs.begin(), Relocs.end()); + + // Sort the relocation entries. MIPS needs this. TargetObjectWriter->sortRelocs(Asm, Relocs); for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { @@ -1100,7 +1094,7 @@ void ELFObjectWriter::writeRelocations(const MCAssembler &Asm, const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) { const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1]; - OS << StrTabBuilder.data(); + getStream() << StrTabBuilder.data(); return StrtabSection; } @@ -1209,12 +1203,12 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm, align(Section.getAlignment()); // Remember the offset into the file for this section. - uint64_t SecStart = OS.tell(); + uint64_t SecStart = getStream().tell(); const MCSymbolELF *SignatureSymbol = Section.getGroup(); writeSectionData(Asm, Section, Layout); - uint64_t SecEnd = OS.tell(); + uint64_t SecEnd = getStream().tell(); SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd); MCSectionELF *RelSection = createRelocationSection(Ctx, Section); @@ -1246,7 +1240,7 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm, align(Group->getAlignment()); // Remember the offset into the file for this section. - uint64_t SecStart = OS.tell(); + uint64_t SecStart = getStream().tell(); const MCSymbol *SignatureSymbol = Group->getGroup(); assert(SignatureSymbol); @@ -1256,7 +1250,7 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm, write(SecIndex); } - uint64_t SecEnd = OS.tell(); + uint64_t SecEnd = getStream().tell(); SectionOffsets[Group] = std::make_pair(SecStart, SecEnd); } @@ -1267,25 +1261,25 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm, align(RelSection->getAlignment()); // Remember the offset into the file for this section. - uint64_t SecStart = OS.tell(); + uint64_t SecStart = getStream().tell(); writeRelocations(Asm, *RelSection->getAssociatedSection()); - uint64_t SecEnd = OS.tell(); + uint64_t SecEnd = getStream().tell(); SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd); } { - uint64_t SecStart = OS.tell(); + uint64_t SecStart = getStream().tell(); const MCSectionELF *Sec = createStringTable(Ctx); - uint64_t SecEnd = OS.tell(); + uint64_t SecEnd = getStream().tell(); SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd); } uint64_t NaturalAlignment = is64Bit() ? 8 : 4; align(NaturalAlignment); - const unsigned SectionHeaderOffset = OS.tell(); + const unsigned SectionHeaderOffset = getStream().tell(); // ... then the section header table ... writeSectionHeader(Layout, SectionIndexMap, SectionOffsets); @@ -1301,19 +1295,19 @@ void ELFObjectWriter::writeObject(MCAssembler &Asm, uint64_t Val = SectionHeaderOffset; if (sys::IsLittleEndianHost != IsLittleEndian) sys::swapByteOrder(Val); - OS.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), - offsetof(ELF::Elf64_Ehdr, e_shoff)); + getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), + offsetof(ELF::Elf64_Ehdr, e_shoff)); NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum); } else { uint32_t Val = SectionHeaderOffset; if (sys::IsLittleEndianHost != IsLittleEndian) sys::swapByteOrder(Val); - OS.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), - offsetof(ELF::Elf32_Ehdr, e_shoff)); + getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), + offsetof(ELF::Elf32_Ehdr, e_shoff)); NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum); } - OS.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections), - NumSectionsOffset); + getStream().pwrite(reinterpret_cast<char *>(&NumSections), + sizeof(NumSections), NumSectionsOffset); } bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( diff --git a/lib/MC/MCAsmBackend.cpp b/lib/MC/MCAsmBackend.cpp index 36c65b7bcd49..fcf139b72537 100644 --- a/lib/MC/MCAsmBackend.cpp +++ b/lib/MC/MCAsmBackend.cpp @@ -16,6 +16,10 @@ MCAsmBackend::MCAsmBackend() : HasDataInCodeSupport(false) {} MCAsmBackend::~MCAsmBackend() {} +bool MCAsmBackend::getFixupKind(StringRef Name, MCFixupKind &MappedKind) const { + return false; +} + const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { static const MCFixupKindInfo Builtins[] = { {"FK_Data_1", 0, 8, 0}, diff --git a/lib/MC/MCAsmInfo.cpp b/lib/MC/MCAsmInfo.cpp index 100dc7c3dc60..36e10b3c6a07 100644 --- a/lib/MC/MCAsmInfo.cpp +++ b/lib/MC/MCAsmInfo.cpp @@ -157,3 +157,9 @@ bool MCAsmInfo::isValidUnquotedName(StringRef Name) const { return true; } + +bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const { + // FIXME: Does .section .bss/.data/.text work everywhere?? + return SectionName == ".text" || SectionName == ".data" || + (SectionName == ".bss" && !usesELFSectionDirectiveForBSS()); +} diff --git a/lib/MC/MCAsmInfoCOFF.cpp b/lib/MC/MCAsmInfoCOFF.cpp index 97fc76a9adb1..5b9dd2009f8b 100644 --- a/lib/MC/MCAsmInfoCOFF.cpp +++ b/lib/MC/MCAsmInfoCOFF.cpp @@ -37,8 +37,7 @@ MCAsmInfoCOFF::MCAsmInfoCOFF() { UseIntegratedAssembler = true; - // FIXME: For now keep the previous behavior, AShr. Need to double-check - // other COFF-targeting assemblers and change this if necessary. + // At least MSVC inline-asm does AShr. UseLogicalShr = false; } diff --git a/lib/MC/MCAsmInfoDarwin.cpp b/lib/MC/MCAsmInfoDarwin.cpp index bb90ff2c350a..ae9486d3db4d 100644 --- a/lib/MC/MCAsmInfoDarwin.cpp +++ b/lib/MC/MCAsmInfoDarwin.cpp @@ -93,9 +93,4 @@ MCAsmInfoDarwin::MCAsmInfoDarwin() { UseIntegratedAssembler = true; SetDirectiveSuppressesReloc = true; - - // FIXME: For now keep the previous behavior, AShr, matching the previous - // behavior of as(1) (both -q and -Q: resp. LLVM and gas v1.38). - // If/when this changes, the AArch64 Darwin special case can go away. - UseLogicalShr = false; } diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp index 227c937e8d1b..c99ce7752b30 100644 --- a/lib/MC/MCAsmStreamer.cpp +++ b/lib/MC/MCAsmStreamer.cpp @@ -1,4 +1,4 @@ -//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===// +//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output ----------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -29,9 +29,11 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Format.h" #include "llvm/Support/FormattedStream.h" +#include "llvm/Support/LEB128.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/Path.h" #include <cctype> + using namespace llvm; namespace { @@ -78,6 +80,9 @@ public: } EmitCommentsAndEOL(); } + + void EmitSyntaxDirective() override; + void EmitCommentsAndEOL(); /// isVerboseAsm - Return true if this streamer supports verbose assembly at @@ -160,7 +165,7 @@ public: void EmitBytes(StringRef Data) override; void EmitValueImpl(const MCExpr *Value, unsigned Size, - const SMLoc &Loc = SMLoc()) override; + SMLoc Loc = SMLoc()) override; void EmitIntValue(uint64_t Value, unsigned Size) override; void EmitULEB128Value(const MCExpr *Value) override; @@ -181,7 +186,7 @@ public: void EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit = 0) override; - bool EmitValueToOffset(const MCExpr *Offset, + void emitValueToOffset(const MCExpr *Offset, unsigned char Value = 0) override; void EmitFileDirective(StringRef Filename) override; @@ -207,6 +212,8 @@ public: void EmitCFISameValue(int64_t Register) override; void EmitCFIRelOffset(int64_t Register, int64_t Offset) override; void EmitCFIAdjustCfaOffset(int64_t Adjustment) override; + void EmitCFIEscape(StringRef Values) override; + void EmitCFIGnuArgsSize(int64_t Size) override; void EmitCFISignalFrame() override; void EmitCFIUndefined(int64_t Register) override; void EmitCFIRegister(int64_t Register1, int64_t Register2) override; @@ -233,6 +240,9 @@ public: void EmitBundleLock(bool AlignToEnd) override; void EmitBundleUnlock() override; + bool EmitRelocDirective(const MCExpr &Offset, StringRef Name, + const MCExpr *Expr, SMLoc Loc) override; + /// EmitRawText - If this file is backed by an assembly streamer, this dumps /// the specified string in the output .s file. This capability is /// indicated by the hasRawTextSupport() predicate. @@ -250,15 +260,9 @@ public: void MCAsmStreamer::AddComment(const Twine &T) { if (!IsVerboseAsm) return; - // Make sure that CommentStream is flushed. - CommentStream.flush(); - T.toVector(CommentToEmit); // Each comment goes on its own line. CommentToEmit.push_back('\n'); - - // Tell the comment stream that the vector changed underneath it. - CommentStream.resync(); } void MCAsmStreamer::EmitCommentsAndEOL() { @@ -267,7 +271,6 @@ void MCAsmStreamer::EmitCommentsAndEOL() { return; } - CommentStream.flush(); StringRef Comments = CommentToEmit; assert(Comments.back() == '\n' && @@ -282,8 +285,6 @@ void MCAsmStreamer::EmitCommentsAndEOL() { } while (!Comments.empty()); CommentToEmit.clear(); - // Tell the comment stream that the vector changed underneath it. - CommentStream.resync(); } static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) { @@ -372,6 +373,8 @@ void MCAsmStreamer::EmitDataRegion(MCDataRegionType Kind) { void MCAsmStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor, unsigned Update) { switch (Kind) { + case MCVM_WatchOSVersionMin: OS << "\t.watchos_version_min"; break; + case MCVM_TvOSVersionMin: OS << "\t.tvos_version_min"; break; case MCVM_IOSVersionMin: OS << "\t.ios_version_min"; break; case MCVM_OSXVersionMin: OS << "\t.macosx_version_min"; break; } @@ -480,6 +483,14 @@ void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { EmitEOL(); } +void MCAsmStreamer::EmitSyntaxDirective() { + if (MAI->getAssemblerDialect() == 1) + OS << "\t.intel_syntax noprefix\n"; + // FIXME: Currently emit unprefix'ed registers. + // The intel_syntax directive has one optional argument + // with may have a value of prefix or noprefix. +} + void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { OS << "\t.def\t "; Symbol->print(OS, MAI); @@ -531,9 +542,6 @@ void MCAsmStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) { void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { - // Common symbols do not belong to any actual section. - AssignSection(Symbol, nullptr); - OS << "\t.comm\t"; Symbol->print(OS, MAI); OS << ',' << Size; @@ -553,9 +561,6 @@ void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, /// @param Size - The size of the common symbol. void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlign) { - // Common symbols do not belong to any actual section. - AssignSection(Symbol, nullptr); - OS << "\t.lcomm\t"; Symbol->print(OS, MAI); OS << ',' << Size; @@ -579,7 +584,7 @@ void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { if (Symbol) - AssignSection(Symbol, Section); + AssignFragment(Symbol, &Section->getDummyFragment()); // Note: a .zerofill directive does not switch sections. OS << ".zerofill "; @@ -603,7 +608,7 @@ void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, // e.g. _a. void MCAsmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { - AssignSection(Symbol, Section); + AssignFragment(Symbol, &Section->getDummyFragment()); assert(Symbol && "Symbol shouldn't be NULL!"); // Instead of using the Section we'll just use the shortcut. @@ -654,7 +659,6 @@ static void PrintQuotedString(StringRef Data, raw_ostream &OS) { OS << '"'; } - void MCAsmStreamer::EmitBytes(StringRef Data) { assert(getCurrentSection().first && "Cannot emit contents before setting section!"); @@ -685,7 +689,7 @@ void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size) { } void MCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, - const SMLoc &Loc) { + SMLoc Loc) { assert(Size <= 8 && "Invalid size"); assert(getCurrentSection().first && "Cannot emit contents before setting section!"); @@ -776,7 +780,6 @@ void MCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) { EmitEOL(); } - /// EmitFill - Emit NumBytes bytes worth of the value specified by /// FillValue. This implements directives such as '.space'. void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { @@ -856,17 +859,15 @@ void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment, 1, MaxBytesToEmit); } -bool MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset, +void MCAsmStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value) { // FIXME: Verify that Offset is associated with the current section. OS << ".org "; Offset->print(OS, MAI); OS << ", " << (unsigned)Value; EmitEOL(); - return false; } - void MCAsmStreamer::EmitFileDirective(StringRef Filename) { assert(MAI->hasSingleParameterDotFile()); OS << "\t.file\t"; @@ -1014,6 +1015,32 @@ void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) { EmitEOL(); } +static void PrintCFIEscape(llvm::formatted_raw_ostream &OS, StringRef Values) { + OS << "\t.cfi_escape "; + if (!Values.empty()) { + size_t e = Values.size() - 1; + for (size_t i = 0; i < e; ++i) + OS << format("0x%02x", uint8_t(Values[i])) << ", "; + OS << format("0x%02x", uint8_t(Values[e])); + } +} + +void MCAsmStreamer::EmitCFIEscape(StringRef Values) { + MCStreamer::EmitCFIEscape(Values); + PrintCFIEscape(OS, Values); + EmitEOL(); +} + +void MCAsmStreamer::EmitCFIGnuArgsSize(int64_t Size) { + MCStreamer::EmitCFIGnuArgsSize(Size); + + uint8_t Buffer[16] = { dwarf::DW_CFA_GNU_args_size }; + unsigned Len = encodeULEB128(Size, Buffer + 1) + 1; + + PrintCFIEscape(OS, StringRef((const char *)&Buffer[0], Len)); + EmitEOL(); +} + void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) { MCStreamer::EmitCFIDefCfaRegister(Register); OS << "\t.cfi_def_cfa_register "; @@ -1203,7 +1230,7 @@ void MCAsmStreamer::EmitWinCFIPushFrame(bool Code) { EmitEOL(); } -void MCAsmStreamer::EmitWinCFIEndProlog(void) { +void MCAsmStreamer::EmitWinCFIEndProlog() { MCStreamer::EmitWinCFIEndProlog(); OS << "\t.seh_endprologue"; @@ -1217,7 +1244,6 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst, SmallVector<MCFixup, 4> Fixups; raw_svector_ostream VecOS(Code); Emitter->encodeInstruction(Inst, VecOS, Fixups, STI); - VecOS.flush(); // If we are showing fixups, create symbolic markers in the encoded // representation. We do this by making a per-bit map to the fixup item index, @@ -1334,6 +1360,19 @@ void MCAsmStreamer::EmitBundleUnlock() { EmitEOL(); } +bool MCAsmStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name, + const MCExpr *Expr, SMLoc) { + OS << "\t.reloc "; + Offset.print(OS, MAI); + OS << ", " << Name; + if (Expr) { + OS << ", "; + Expr->print(OS, MAI); + } + EmitEOL(); + return false; +} + /// EmitRawText - If this file is backed by an assembly streamer, this dumps /// the specified string in the output .s file. This capability is /// indicated by the hasRawTextSupport() predicate. diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index f53b589e1aea..15e82fa49388 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -64,272 +64,11 @@ STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); /* *** */ -MCAsmLayout::MCAsmLayout(MCAssembler &Asm) - : Assembler(Asm), LastValidFragment() - { - // Compute the section layout order. Virtual sections must go last. - for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) - if (!it->isVirtualSection()) - SectionOrder.push_back(&*it); - for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) - if (it->isVirtualSection()) - SectionOrder.push_back(&*it); -} - -bool MCAsmLayout::isFragmentValid(const MCFragment *F) const { - const MCSection *Sec = F->getParent(); - const MCFragment *LastValid = LastValidFragment.lookup(Sec); - if (!LastValid) - return false; - assert(LastValid->getParent() == Sec); - return F->getLayoutOrder() <= LastValid->getLayoutOrder(); -} - -void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) { - // If this fragment wasn't already valid, we don't need to do anything. - if (!isFragmentValid(F)) - return; - - // Otherwise, reset the last valid fragment to the previous fragment - // (if this is the first fragment, it will be NULL). - LastValidFragment[F->getParent()] = F->getPrevNode(); -} - -void MCAsmLayout::ensureValid(const MCFragment *F) const { - MCSection *Sec = F->getParent(); - MCFragment *Cur = LastValidFragment[Sec]; - if (!Cur) - Cur = Sec->begin(); - else - Cur = Cur->getNextNode(); - - // Advance the layout position until the fragment is valid. - while (!isFragmentValid(F)) { - assert(Cur && "Layout bookkeeping error"); - const_cast<MCAsmLayout*>(this)->layoutFragment(Cur); - Cur = Cur->getNextNode(); - } -} - -uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const { - ensureValid(F); - assert(F->Offset != ~UINT64_C(0) && "Address not set!"); - return F->Offset; -} - -// Simple getSymbolOffset helper for the non-varibale case. -static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S, - bool ReportError, uint64_t &Val) { - if (!S.getFragment()) { - if (ReportError) - report_fatal_error("unable to evaluate offset to undefined symbol '" + - S.getName() + "'"); - return false; - } - Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset(); - return true; -} - -static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S, - bool ReportError, uint64_t &Val) { - if (!S.isVariable()) - return getLabelOffset(Layout, S, ReportError, Val); - - // If SD is a variable, evaluate it. - MCValue Target; - if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr)) - report_fatal_error("unable to evaluate offset for variable '" + - S.getName() + "'"); - - uint64_t Offset = Target.getConstant(); - - const MCSymbolRefExpr *A = Target.getSymA(); - if (A) { - uint64_t ValA; - if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA)) - return false; - Offset += ValA; - } - - const MCSymbolRefExpr *B = Target.getSymB(); - if (B) { - uint64_t ValB; - if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB)) - return false; - Offset -= ValB; - } - - Val = Offset; - return true; -} - -bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const { - return getSymbolOffsetImpl(*this, S, false, Val); -} - -uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const { - uint64_t Val; - getSymbolOffsetImpl(*this, S, true, Val); - return Val; -} - -const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const { - if (!Symbol.isVariable()) - return &Symbol; - - const MCExpr *Expr = Symbol.getVariableValue(); - MCValue Value; - if (!Expr->evaluateAsValue(Value, *this)) - llvm_unreachable("Invalid Expression"); - - const MCSymbolRefExpr *RefB = Value.getSymB(); - if (RefB) - Assembler.getContext().reportFatalError( - SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() + - "' could not be evaluated in a subtraction expression"); - - const MCSymbolRefExpr *A = Value.getSymA(); - if (!A) - return nullptr; - - const MCSymbol &ASym = A->getSymbol(); - const MCAssembler &Asm = getAssembler(); - if (ASym.isCommon()) { - // FIXME: we should probably add a SMLoc to MCExpr. - Asm.getContext().reportFatalError(SMLoc(), - "Common symbol " + ASym.getName() + - " cannot be used in assignment expr"); - } - - return &ASym; -} - -uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const { - // The size is the last fragment's end offset. - const MCFragment &F = Sec->getFragmentList().back(); - return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F); -} - -uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const { - // Virtual sections have no file size. - if (Sec->isVirtualSection()) - return 0; - - // Otherwise, the file size is the same as the address space size. - return getSectionAddressSize(Sec); -} - -uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler, - const MCFragment *F, - uint64_t FOffset, uint64_t FSize) { - uint64_t BundleSize = Assembler.getBundleAlignSize(); - assert(BundleSize > 0 && - "computeBundlePadding should only be called if bundling is enabled"); - uint64_t BundleMask = BundleSize - 1; - uint64_t OffsetInBundle = FOffset & BundleMask; - uint64_t EndOfFragment = OffsetInBundle + FSize; - - // There are two kinds of bundling restrictions: - // - // 1) For alignToBundleEnd(), add padding to ensure that the fragment will - // *end* on a bundle boundary. - // 2) Otherwise, check if the fragment would cross a bundle boundary. If it - // would, add padding until the end of the bundle so that the fragment - // will start in a new one. - if (F->alignToBundleEnd()) { - // Three possibilities here: - // - // A) The fragment just happens to end at a bundle boundary, so we're good. - // B) The fragment ends before the current bundle boundary: pad it just - // enough to reach the boundary. - // C) The fragment ends after the current bundle boundary: pad it until it - // reaches the end of the next bundle boundary. - // - // Note: this code could be made shorter with some modulo trickery, but it's - // intentionally kept in its more explicit form for simplicity. - if (EndOfFragment == BundleSize) - return 0; - else if (EndOfFragment < BundleSize) - return BundleSize - EndOfFragment; - else { // EndOfFragment > BundleSize - return 2 * BundleSize - EndOfFragment; - } - } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize) - return BundleSize - OffsetInBundle; - else - return 0; -} - -/* *** */ - -void ilist_node_traits<MCFragment>::deleteNode(MCFragment *V) { - V->destroy(); -} - -MCFragment::MCFragment() : Kind(FragmentType(~0)), HasInstructions(false), - AlignToBundleEnd(false), BundlePadding(0) { -} - -MCFragment::~MCFragment() { } - -MCFragment::MCFragment(FragmentType Kind, bool HasInstructions, - uint8_t BundlePadding, MCSection *Parent) - : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false), - BundlePadding(BundlePadding), Parent(Parent), Atom(nullptr), - Offset(~UINT64_C(0)) { - if (Parent) - Parent->getFragmentList().push_back(this); -} - -void MCFragment::destroy() { - // First check if we are the sentinal. - if (Kind == FragmentType(~0)) { - delete this; - return; - } - - switch (Kind) { - case FT_Align: - delete cast<MCAlignFragment>(this); - return; - case FT_Data: - delete cast<MCDataFragment>(this); - return; - case FT_CompactEncodedInst: - delete cast<MCCompactEncodedInstFragment>(this); - return; - case FT_Fill: - delete cast<MCFillFragment>(this); - return; - case FT_Relaxable: - delete cast<MCRelaxableFragment>(this); - return; - case FT_Org: - delete cast<MCOrgFragment>(this); - return; - case FT_Dwarf: - delete cast<MCDwarfLineAddrFragment>(this); - return; - case FT_DwarfFrame: - delete cast<MCDwarfCallFrameFragment>(this); - return; - case FT_LEB: - delete cast<MCLEBFragment>(this); - return; - case FT_SafeSEH: - delete cast<MCSafeSEHFragment>(this); - return; - } -} - -/* *** */ - MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_, - MCCodeEmitter &Emitter_, MCObjectWriter &Writer_, - raw_ostream &OS_) + MCCodeEmitter &Emitter_, MCObjectWriter &Writer_) : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_), - OS(OS_), BundleAlignSize(0), RelaxAll(false), - SubsectionsViaSymbols(false), ELFHeaderEFlags(0) { + BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false), + IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) { VersionMinInfo.Major = 0; // Major version == 0 for "none specified" } @@ -347,6 +86,7 @@ void MCAssembler::reset() { BundleAlignSize = 0; RelaxAll = false; SubsectionsViaSymbols = false; + IncrementalLinkerCompatible = false; ELFHeaderEFlags = 0; LOHContainer.reset(); VersionMinInfo.Major = 0; @@ -358,6 +98,14 @@ void MCAssembler::reset() { getLOHContainer().reset(); } +bool MCAssembler::registerSection(MCSection &Section) { + if (Section.isRegistered()) + return false; + Sections.push_back(&Section); + Section.setIsRegistered(true); + return true; +} + bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { if (ThumbFuncs.count(Symbol)) return true; @@ -404,7 +152,7 @@ const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const { return &S; // Absolute and undefined symbols have no defining atom. - if (!S.getFragment()) + if (!S.isInSection()) return nullptr; // Non-linker visible symbols in sections which can't be atomized have no @@ -426,8 +174,13 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, // probably merge the two into a single callback that tries to evaluate a // fixup and records a relocation if one is needed. const MCExpr *Expr = Fixup.getValue(); - if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) - getContext().reportFatalError(Fixup.getLoc(), "expected relocatable expression"); + if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) { + getContext().reportError(Fixup.getLoc(), "expected relocatable expression"); + // Claim to have completely evaluated the fixup, to prevent any further + // processing from being done. + Value = 0; + return true; + } bool IsPCRel = Backend.getFixupKindInfo( Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel; @@ -523,12 +276,19 @@ uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, case MCFragment::FT_Org: { const MCOrgFragment &OF = cast<MCOrgFragment>(F); - int64_t TargetLocation; - if (!OF.getOffset().evaluateAsAbsolute(TargetLocation, Layout)) + MCValue Value; + if (!OF.getOffset().evaluateAsValue(Value, Layout)) report_fatal_error("expected assembly-time absolute expression"); // FIXME: We need a way to communicate this error. uint64_t FragmentOffset = Layout.getFragmentOffset(&OF); + int64_t TargetLocation = Value.getConstant(); + if (const MCSymbolRefExpr *A = Value.getSymA()) { + uint64_t Val; + if (!Layout.getSymbolOffset(A->getSymbol(), Val)) + report_fatal_error("expected absolute expression"); + TargetLocation += Val; + } int64_t Size = TargetLocation - FragmentOffset; if (Size < 0 || Size >= 0x40000000) report_fatal_error("invalid .org offset '" + Twine(TargetLocation) + @@ -540,6 +300,8 @@ uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout, return cast<MCDwarfLineAddrFragment>(F).getContents().size(); case MCFragment::FT_DwarfFrame: return cast<MCDwarfCallFrameFragment>(F).getContents().size(); + case MCFragment::FT_Dummy: + llvm_unreachable("Should not have been added"); } llvm_unreachable("invalid fragment kind"); @@ -773,6 +535,8 @@ static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout, OW->writeBytes(CF.getContents()); break; } + case MCFragment::FT_Dummy: + llvm_unreachable("Should not have been added"); } assert(OW->getStream().tell() - Start == FragmentSize && @@ -786,15 +550,14 @@ void MCAssembler::writeSectionData(const MCSection *Sec, assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!"); // Check that contents are only things legal inside a virtual section. - for (MCSection::const_iterator it = Sec->begin(), ie = Sec->end(); it != ie; - ++it) { - switch (it->getKind()) { + for (const MCFragment &F : *Sec) { + switch (F.getKind()) { default: llvm_unreachable("Invalid fragment in virtual section!"); case MCFragment::FT_Data: { // Check that we aren't trying to write a non-zero contents (or fixups) // into a virtual section. This is to support clients which use standard // directives to fill the contents of virtual sections. - const MCDataFragment &DF = cast<MCDataFragment>(*it); + const MCDataFragment &DF = cast<MCDataFragment>(F); assert(DF.fixup_begin() == DF.fixup_end() && "Cannot have fixups in virtual section!"); for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) @@ -810,13 +573,13 @@ void MCAssembler::writeSectionData(const MCSection *Sec, case MCFragment::FT_Align: // Check that we aren't trying to write a non-zero value into a virtual // section. - assert((cast<MCAlignFragment>(it)->getValueSize() == 0 || - cast<MCAlignFragment>(it)->getValue() == 0) && + assert((cast<MCAlignFragment>(F).getValueSize() == 0 || + cast<MCAlignFragment>(F).getValue() == 0) && "Invalid align in virtual section!"); break; case MCFragment::FT_Fill: - assert((cast<MCFillFragment>(it)->getValueSize() == 0 || - cast<MCFillFragment>(it)->getValue() == 0) && + assert((cast<MCFillFragment>(F).getValueSize() == 0 || + cast<MCFillFragment>(F).getValue() == 0) && "Invalid fill in virtual section!"); break; } @@ -828,9 +591,8 @@ void MCAssembler::writeSectionData(const MCSection *Sec, uint64_t Start = getWriter().getStream().tell(); (void)Start; - for (MCSection::const_iterator it = Sec->begin(), ie = Sec->end(); it != ie; - ++it) - writeFragment(*this, Layout, *it); + for (const MCFragment &F : *Sec) + writeFragment(*this, Layout, F); assert(getWriter().getStream().tell() - Start == Layout.getSectionAddressSize(Sec)); @@ -854,23 +616,20 @@ std::pair<uint64_t, bool> MCAssembler::handleFixup(const MCAsmLayout &Layout, return std::make_pair(FixedValue, IsPCRel); } -void MCAssembler::Finish() { +void MCAssembler::layout(MCAsmLayout &Layout) { DEBUG_WITH_TYPE("mc-dump", { llvm::errs() << "assembler backend - pre-layout\n--\n"; dump(); }); - // Create the layout object. - MCAsmLayout Layout(*this); - // Create dummy fragments and assign section ordinals. unsigned SectionIndex = 0; - for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { + for (MCSection &Sec : *this) { // Create dummy fragments to eliminate any empty sections, this simplifies // layout. - if (it->getFragmentList().empty()) - new MCDataFragment(&*it); + if (Sec.getFragmentList().empty()) + new MCDataFragment(&Sec); - it->setOrdinal(SectionIndex++); + Sec.setOrdinal(SectionIndex++); } // Assign layout order indices to sections and fragments. @@ -879,9 +638,8 @@ void MCAssembler::Finish() { Sec->setLayoutOrder(i); unsigned FragmentIndex = 0; - for (MCSection::iterator iFrag = Sec->begin(), iFragEnd = Sec->end(); - iFrag != iFragEnd; ++iFrag) - iFrag->setLayoutOrder(FragmentIndex++); + for (MCFragment &Frag : *Sec) + Frag.setLayoutOrder(FragmentIndex++); } // Layout until everything fits. @@ -899,17 +657,14 @@ void MCAssembler::Finish() { llvm::errs() << "assembler backend - final-layout\n--\n"; dump(); }); - uint64_t StartOffset = OS.tell(); - // Allow the object writer a chance to perform post-layout binding (for // example, to set the index fields in the symbol data). getWriter().executePostLayoutBinding(*this, Layout); // Evaluate and apply the fixups, generating relocation entries as necessary. - for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) { - for (MCSection::iterator it2 = it->begin(), ie2 = it->end(); it2 != ie2; - ++it2) { - MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(it2); + for (MCSection &Sec : *this) { + for (MCFragment &Frag : Sec) { + MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(&Frag); // Data and relaxable fragments both have fixups. So only process // those here. // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups @@ -935,6 +690,15 @@ void MCAssembler::Finish() { } } } +} + +void MCAssembler::Finish() { + // Create the layout object. + MCAsmLayout Layout(*this); + layout(Layout); + + raw_ostream &OS = getWriter().getStream(); + uint64_t StartOffset = OS.tell(); // Write the object file. getWriter().writeObject(*this, Layout); @@ -960,9 +724,8 @@ bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F, if (!getBackend().mayNeedRelaxation(F->getInst())) return false; - for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(), - ie = F->fixup_end(); it != ie; ++it) - if (fixupNeedsRelaxation(*it, F, Layout)) + for (const MCFixup &Fixup : F->getFixups()) + if (fixupNeedsRelaxation(Fixup, F, Layout)) return true; return false; @@ -991,7 +754,6 @@ bool MCAssembler::relaxInstruction(MCAsmLayout &Layout, SmallString<256> Code; raw_svector_ostream VecOS(Code); getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo()); - VecOS.flush(); // Update the fragment. F.setInst(Relaxed); @@ -1014,7 +776,6 @@ bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { encodeSLEB128(Value, OSE); else encodeULEB128(Value, OSE); - OSE.flush(); return OldSize != LF.getContents().size(); } @@ -1031,8 +792,8 @@ bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout, SmallString<8> &Data = DF.getContents(); Data.clear(); raw_svector_ostream OSE(Data); - MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OSE); - OSE.flush(); + MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta, + AddrDelta, OSE); return OldSize != Data.size(); } @@ -1048,7 +809,6 @@ bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout, Data.clear(); raw_svector_ostream OSE(Data); MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE); - OSE.flush(); return OldSize != Data.size(); } @@ -1085,7 +845,7 @@ bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) { break; } if (RelaxedFrag && !FirstRelaxedFragment) - FirstRelaxedFragment = I; + FirstRelaxedFragment = &*I; } if (FirstRelaxedFragment) { Layout.invalidateFragmentsFrom(FirstRelaxedFragment); @@ -1113,158 +873,3 @@ void MCAssembler::finishLayout(MCAsmLayout &Layout) { Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin()); } } - -// Debugging methods - -namespace llvm { - -raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { - OS << "<MCFixup" << " Offset:" << AF.getOffset() - << " Value:" << *AF.getValue() - << " Kind:" << AF.getKind() << ">"; - return OS; -} - -} - -#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) -void MCFragment::dump() { - raw_ostream &OS = llvm::errs(); - - OS << "<"; - switch (getKind()) { - case MCFragment::FT_Align: OS << "MCAlignFragment"; break; - case MCFragment::FT_Data: OS << "MCDataFragment"; break; - case MCFragment::FT_CompactEncodedInst: - OS << "MCCompactEncodedInstFragment"; break; - case MCFragment::FT_Fill: OS << "MCFillFragment"; break; - case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break; - case MCFragment::FT_Org: OS << "MCOrgFragment"; break; - case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break; - case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break; - case MCFragment::FT_LEB: OS << "MCLEBFragment"; break; - case MCFragment::FT_SafeSEH: OS << "MCSafeSEHFragment"; break; - } - - OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder - << " Offset:" << Offset - << " HasInstructions:" << hasInstructions() - << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">"; - - switch (getKind()) { - case MCFragment::FT_Align: { - const MCAlignFragment *AF = cast<MCAlignFragment>(this); - if (AF->hasEmitNops()) - OS << " (emit nops)"; - OS << "\n "; - OS << " Alignment:" << AF->getAlignment() - << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize() - << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">"; - break; - } - case MCFragment::FT_Data: { - const MCDataFragment *DF = cast<MCDataFragment>(this); - OS << "\n "; - OS << " Contents:["; - const SmallVectorImpl<char> &Contents = DF->getContents(); - for (unsigned i = 0, e = Contents.size(); i != e; ++i) { - if (i) OS << ","; - OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); - } - OS << "] (" << Contents.size() << " bytes)"; - - if (DF->fixup_begin() != DF->fixup_end()) { - OS << ",\n "; - OS << " Fixups:["; - for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(), - ie = DF->fixup_end(); it != ie; ++it) { - if (it != DF->fixup_begin()) OS << ",\n "; - OS << *it; - } - OS << "]"; - } - break; - } - case MCFragment::FT_CompactEncodedInst: { - const MCCompactEncodedInstFragment *CEIF = - cast<MCCompactEncodedInstFragment>(this); - OS << "\n "; - OS << " Contents:["; - const SmallVectorImpl<char> &Contents = CEIF->getContents(); - for (unsigned i = 0, e = Contents.size(); i != e; ++i) { - if (i) OS << ","; - OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); - } - OS << "] (" << Contents.size() << " bytes)"; - break; - } - case MCFragment::FT_Fill: { - const MCFillFragment *FF = cast<MCFillFragment>(this); - OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize() - << " Size:" << FF->getSize(); - break; - } - case MCFragment::FT_Relaxable: { - const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this); - OS << "\n "; - OS << " Inst:"; - F->getInst().dump_pretty(OS); - break; - } - case MCFragment::FT_Org: { - const MCOrgFragment *OF = cast<MCOrgFragment>(this); - OS << "\n "; - OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue(); - break; - } - case MCFragment::FT_Dwarf: { - const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this); - OS << "\n "; - OS << " AddrDelta:" << OF->getAddrDelta() - << " LineDelta:" << OF->getLineDelta(); - break; - } - case MCFragment::FT_DwarfFrame: { - const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this); - OS << "\n "; - OS << " AddrDelta:" << CF->getAddrDelta(); - break; - } - case MCFragment::FT_LEB: { - const MCLEBFragment *LF = cast<MCLEBFragment>(this); - OS << "\n "; - OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned(); - break; - } - case MCFragment::FT_SafeSEH: { - const MCSafeSEHFragment *F = cast<MCSafeSEHFragment>(this); - OS << "\n "; - OS << " Sym:" << F->getSymbol(); - break; - } - } - OS << ">"; -} - -void MCAssembler::dump() { - raw_ostream &OS = llvm::errs(); - - OS << "<MCAssembler\n"; - OS << " Sections:[\n "; - for (iterator it = begin(), ie = end(); it != ie; ++it) { - if (it != begin()) OS << ",\n "; - it->dump(); - } - OS << "],\n"; - OS << " Symbols:["; - - for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { - if (it != symbol_begin()) OS << ",\n "; - OS << "("; - it->dump(); - OS << ", Index:" << it->getIndex() << ", "; - OS << ")"; - } - OS << "]>\n"; -} -#endif diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index a85796cfbad9..b5ad518d0330 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -23,6 +23,7 @@ #include "llvm/MC/MCSymbolCOFF.h" #include "llvm/MC/MCSymbolELF.h" #include "llvm/MC/MCSymbolMachO.h" +#include "llvm/Support/COFF.h" #include "llvm/Support/ELF.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" @@ -41,7 +42,7 @@ MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false), GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4), AllowTemporaryLabels(true), DwarfCompileUnitID(0), - AutoReset(DoAutoReset) { + AutoReset(DoAutoReset), HadError(false) { std::error_code EC = llvm::sys::fs::current_path(CompilationDir); if (EC) @@ -62,9 +63,6 @@ MCContext::~MCContext() { // NOTE: The symbols are all allocated out of a bump pointer allocator, // we don't need to free them here. - - // If the stream for the .secure_log_unique directive was created free it. - delete (raw_ostream *)SecureLog; } //===----------------------------------------------------------------------===// @@ -73,13 +71,11 @@ MCContext::~MCContext() { void MCContext::reset() { // Call the destructors so the fragments are freed - for (auto &I : ELFUniquingMap) - I.second->~MCSectionELF(); - for (auto &I : COFFUniquingMap) - I.second->~MCSectionCOFF(); - for (auto &I : MachOUniquingMap) - I.second->~MCSectionMachO(); + COFFAllocator.DestroyAll(); + ELFAllocator.DestroyAll(); + MachOAllocator.DestroyAll(); + MCSubtargetAllocator.DestroyAll(); UsedNames.clear(); Symbols.clear(); SectionSymbols.clear(); @@ -103,6 +99,8 @@ void MCContext::reset() { DwarfLocSeen = false; GenDwarfForAssembly = false; GenDwarfFileNumber = 0; + + HadError = false; } //===----------------------------------------------------------------------===// @@ -294,8 +292,8 @@ MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section, Begin = createTempSymbol(BeginSymName, false); // Otherwise, return a new section. - return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes, - Reserved2, Kind, Begin); + return Entry = new (MachOAllocator.Allocate()) MCSectionMachO( + Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin); } void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) { @@ -322,7 +320,7 @@ MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type, bool Inserted; std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true)); - return new (*this) + return new (ELFAllocator.Allocate()) MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group, true, nullptr, Associated); } @@ -367,15 +365,15 @@ MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type, if (BeginSymName) Begin = createTempSymbol(BeginSymName, false); - MCSectionELF *Result = - new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, - GroupSym, UniqueID, Begin, Associated); + MCSectionELF *Result = new (ELFAllocator.Allocate()) + MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, + Begin, Associated); Entry.second = Result; return Result; } MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) { - MCSectionELF *Result = new (*this) + MCSectionELF *Result = new (ELFAllocator.Allocate()) MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4, Group, ~0, nullptr, nullptr); return Result; @@ -404,7 +402,7 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, Begin = createTempSymbol(BeginSymName, false); StringRef CachedName = Iter->first.SectionName; - MCSectionCOFF *Result = new (*this) MCSectionCOFF( + MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF( CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin); Iter->second = Result; @@ -441,6 +439,10 @@ MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec, COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); } +MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) { + return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI); +} + //===----------------------------------------------------------------------===// // Dwarf Management //===----------------------------------------------------------------------===// @@ -472,14 +474,24 @@ void MCContext::finalizeDwarfSections(MCStreamer &MCOS) { [&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); }); } -void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) const { - // If we have a source manager and a location, use it. Otherwise just - // use the generic report_fatal_error(). - if (!SrcMgr || Loc == SMLoc()) +//===----------------------------------------------------------------------===// +// Error Reporting +//===----------------------------------------------------------------------===// + +void MCContext::reportError(SMLoc Loc, const Twine &Msg) { + HadError = true; + + // If we have a source manager use it. Otherwise just use the generic + // report_fatal_error(). + if (!SrcMgr) report_fatal_error(Msg, false); // Use the source manager to print the message. SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg); +} + +void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) { + reportError(Loc, Msg); // If we reached here, we are failing ungracefully. Run the interrupt handlers // to make sure any special cleanups get done, in particular that we remove diff --git a/lib/MC/MCDisassembler/Disassembler.cpp b/lib/MC/MCDisassembler/Disassembler.cpp index 716d76a79fe3..82063fb74696 100644 --- a/lib/MC/MCDisassembler/Disassembler.cpp +++ b/lib/MC/MCDisassembler/Disassembler.cpp @@ -125,7 +125,6 @@ void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ static void emitComments(LLVMDisasmContext *DC, formatted_raw_ostream &FormattedOS) { // Flush the stream before taking its content. - DC->CommentStream.flush(); StringRef Comments = DC->CommentsToEmit.str(); // Get the default information for printing a comment. const MCAsmInfo *MAI = DC->getAsmInfo(); @@ -147,7 +146,6 @@ static void emitComments(LLVMDisasmContext *DC, // Tell the comment stream that the vector changed underneath it. DC->CommentsToEmit.clear(); - DC->CommentStream.resync(); } /// \brief Gets latency information for \p Inst from the itinerary @@ -261,7 +259,6 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, return 0; case MCDisassembler::Success: { - Annotations.flush(); StringRef AnnotationsStr = Annotations.str(); SmallVector<char, 64> InsnStr; @@ -273,7 +270,6 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, emitLatency(DC, Inst); emitComments(DC, FormattedOS); - OS.flush(); assert(OutStringSize != 0 && "Output buffer cannot be zero size"); size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); diff --git a/lib/MC/MCDwarf.cpp b/lib/MC/MCDwarf.cpp index c84c4865f51e..a99ac4eca59e 100644 --- a/lib/MC/MCDwarf.cpp +++ b/lib/MC/MCDwarf.cpp @@ -27,26 +27,8 @@ #include "llvm/Support/Path.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" -using namespace llvm; - -// Given a special op, return the address skip amount (in units of -// DWARF2_LINE_MIN_INSN_LENGTH. -#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) - -// The maximum address skip amount that can be encoded with a special op. -#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) - -// First special line opcode - leave room for the standard opcodes. -// Note: If you want to change this, you'll have to update the -// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). -#define DWARF2_LINE_OPCODE_BASE 13 - -// Minimum line offset in a special line info. opcode. This value -// was chosen to give a reasonable range of values. -#define DWARF2_LINE_BASE -5 -// Range of line offsets in a special line info. opcode. -#define DWARF2_LINE_RANGE 14 +using namespace llvm; static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) { unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment(); @@ -197,7 +179,8 @@ EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section, // // This emits the Dwarf file and the line tables. // -void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS) { +void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS, + MCDwarfLineTableParams Params) { MCContext &context = MCOS->getContext(); auto &LineTables = context.getMCDwarfLineTables(); @@ -212,14 +195,17 @@ void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS) { // Handle the rest of the Compile Units. for (const auto &CUIDTablePair : LineTables) - CUIDTablePair.second.EmitCU(MCOS); + CUIDTablePair.second.EmitCU(MCOS, Params); } -void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS) const { - MCOS.EmitLabel(Header.Emit(&MCOS, None).second); +void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, + MCDwarfLineTableParams Params) const { + MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second); } -std::pair<MCSymbol *, MCSymbol *> MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) const { +std::pair<MCSymbol *, MCSymbol *> +MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, + MCDwarfLineTableParams Params) const { static const char StandardOpcodeLengths[] = { 0, // length of DW_LNS_copy 1, // length of DW_LNS_advance_pc @@ -234,9 +220,10 @@ std::pair<MCSymbol *, MCSymbol *> MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) 0, // length of DW_LNS_set_epilogue_begin 1 // DW_LNS_set_isa }; - assert(array_lengthof(StandardOpcodeLengths) == - (DWARF2_LINE_OPCODE_BASE - 1)); - return Emit(MCOS, StandardOpcodeLengths); + assert(array_lengthof(StandardOpcodeLengths) >= + (Params.DWARF2LineOpcodeBase - 1U)); + return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths, + Params.DWARF2LineOpcodeBase - 1)); } static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) { @@ -256,9 +243,8 @@ static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) { } std::pair<MCSymbol *, MCSymbol *> -MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, +MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, ArrayRef<char> StandardOpcodeLengths) const { - MCContext &context = MCOS->getContext(); // Create a symbol at the beginning of the line table. @@ -293,8 +279,8 @@ MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, // Parameters of the state machine, are next. MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1); MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); - MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); - MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); + MCOS->EmitIntValue(Params.DWARF2LineBase, 1); + MCOS->EmitIntValue(Params.DWARF2LineRange, 1); MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1); // Standard opcode lengths @@ -329,8 +315,9 @@ MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, return std::make_pair(LineStartSym, LineEndSym); } -void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS) const { - MCSymbol *LineEndSym = Header.Emit(MCOS).second; +void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS, + MCDwarfLineTableParams Params) const { + MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second; // Put out the line tables. for (const auto &LineSec : MCLineSections.getMCLineEntries()) @@ -416,21 +403,31 @@ unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory, } /// Utility function to emit the encoding to a streamer. -void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, - uint64_t AddrDelta) { +void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, + int64_t LineDelta, uint64_t AddrDelta) { MCContext &Context = MCOS->getContext(); SmallString<256> Tmp; raw_svector_ostream OS(Tmp); - MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS); + MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS); MCOS->EmitBytes(OS.str()); } +/// Given a special op, return the address skip amount (in units of +/// DWARF2_LINE_MIN_INSN_LENGTH). +static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) { + return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange; +} + /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. -void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, - uint64_t AddrDelta, raw_ostream &OS) { +void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params, + int64_t LineDelta, uint64_t AddrDelta, + raw_ostream &OS) { uint64_t Temp, Opcode; bool NeedCopy = false; + // The maximum address skip amount that can be encoded with a special op. + uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255); + // Scale the address delta by the minimum instruction length. AddrDelta = ScaleAddrDelta(Context, AddrDelta); @@ -438,7 +435,7 @@ void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the // end_sequence to emit the matrix entry. if (LineDelta == INT64_MAX) { - if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) + if (AddrDelta == MaxSpecialAddrDelta) OS << char(dwarf::DW_LNS_const_add_pc); else if (AddrDelta) { OS << char(dwarf::DW_LNS_advance_pc); @@ -451,16 +448,16 @@ void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, } // Bias the line delta by the base. - Temp = LineDelta - DWARF2_LINE_BASE; + Temp = LineDelta - Params.DWARF2LineBase; // If the line increment is out of range of a special opcode, we must encode // it with DW_LNS_advance_line. - if (Temp >= DWARF2_LINE_RANGE) { + if (Temp >= Params.DWARF2LineRange) { OS << char(dwarf::DW_LNS_advance_line); encodeSLEB128(LineDelta, OS); LineDelta = 0; - Temp = 0 - DWARF2_LINE_BASE; + Temp = 0 - Params.DWARF2LineBase; NeedCopy = true; } @@ -471,19 +468,19 @@ void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, } // Bias the opcode by the special opcode base. - Temp += DWARF2_LINE_OPCODE_BASE; + Temp += Params.DWARF2LineOpcodeBase; // Avoid overflow when addr_delta is large. - if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { + if (AddrDelta < 256 + MaxSpecialAddrDelta) { // Try using a special opcode. - Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; + Opcode = Temp + AddrDelta * Params.DWARF2LineRange; if (Opcode <= 255) { OS << char(Opcode); return; } // Try using DW_LNS_const_add_pc followed by special op. - Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; + Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange; if (Opcode <= 255) { OS << char(dwarf::DW_LNS_const_add_pc); OS << char(Opcode); @@ -517,10 +514,14 @@ static void EmitGenDwarfAbbrev(MCStreamer *MCOS) { MCOS->EmitULEB128IntValue(1); MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit); MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1); - EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4); - if (MCOS->getContext().getGenDwarfSectionSyms().size() > 1 && - MCOS->getContext().getDwarfVersion() >= 3) { - EmitAbbrev(MCOS, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4); + EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, + context.getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset + : dwarf::DW_FORM_data4); + if (context.getGenDwarfSectionSyms().size() > 1 && + context.getDwarfVersion() >= 3) { + EmitAbbrev(MCOS, dwarf::DW_AT_ranges, + context.getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset + : dwarf::DW_FORM_data4); } else { EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr); EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr); @@ -845,7 +846,7 @@ void MCGenDwarfInfo::Emit(MCStreamer *MCOS) { LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0); MCSymbol *AbbrevSectionSymbol = nullptr; MCSymbol *InfoSectionSymbol = nullptr; - MCSymbol *RangesSectionSymbol = NULL; + MCSymbol *RangesSectionSymbol = nullptr; // Create end symbols for each section, and remove empty sections MCOS->getContext().finalizeDwarfSections(*MCOS); @@ -998,38 +999,29 @@ static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol, } namespace { - class FrameEmitterImpl { - int CFAOffset; - int InitialCFAOffset; - bool IsEH; - const MCSymbol *SectionStart; - public: - FrameEmitterImpl(bool isEH) - : CFAOffset(0), InitialCFAOffset(0), IsEH(isEH), SectionStart(nullptr) { - } - - void setSectionStart(const MCSymbol *Label) { SectionStart = Label; } - - /// Emit the unwind information in a compact way. - void EmitCompactUnwind(MCObjectStreamer &streamer, - const MCDwarfFrameInfo &frame); - - const MCSymbol &EmitCIE(MCObjectStreamer &streamer, - const MCSymbol *personality, - unsigned personalityEncoding, - const MCSymbol *lsda, - bool IsSignalFrame, - unsigned lsdaEncoding, - bool IsSimple); - MCSymbol *EmitFDE(MCObjectStreamer &streamer, - const MCSymbol &cieStart, - const MCDwarfFrameInfo &frame); - void EmitCFIInstructions(MCObjectStreamer &streamer, - ArrayRef<MCCFIInstruction> Instrs, - MCSymbol *BaseLabel); - void EmitCFIInstruction(MCObjectStreamer &Streamer, - const MCCFIInstruction &Instr); - }; +class FrameEmitterImpl { + int CFAOffset = 0; + int InitialCFAOffset = 0; + bool IsEH; + MCObjectStreamer &Streamer; + +public: + FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer) + : IsEH(IsEH), Streamer(Streamer) {} + + /// Emit the unwind information in a compact way. + void EmitCompactUnwind(const MCDwarfFrameInfo &frame); + + const MCSymbol &EmitCIE(const MCSymbol *personality, + unsigned personalityEncoding, const MCSymbol *lsda, + bool IsSignalFrame, unsigned lsdaEncoding, + bool IsSimple); + void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame, + bool LastInSection, const MCSymbol &SectionStart); + void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs, + MCSymbol *BaseLabel); + void EmitCFIInstruction(const MCCFIInstruction &Instr); +}; } // end anonymous namespace @@ -1037,8 +1029,7 @@ static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) { Streamer.EmitIntValue(Encoding, 1); } -void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, - const MCCFIInstruction &Instr) { +void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) { int dataAlignmentFactor = getDataAlignmentFactor(Streamer); auto *MRI = Streamer.getContext().getRegisterInfo(); @@ -1150,6 +1141,11 @@ void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1); return; } + case MCCFIInstruction::OpGnuArgsSize: { + Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1); + Streamer.EmitULEB128IntValue(Instr.getOffset()); + return; + } case MCCFIInstruction::OpEscape: Streamer.EmitBytes(Instr.getValues()); return; @@ -1158,8 +1154,7 @@ void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer, } /// Emit frame instructions to describe the layout of the frame. -void FrameEmitterImpl::EmitCFIInstructions(MCObjectStreamer &streamer, - ArrayRef<MCCFIInstruction> Instrs, +void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs, MCSymbol *BaseLabel) { for (unsigned i = 0, N = Instrs.size(); i < N; ++i) { const MCCFIInstruction &Instr = Instrs[i]; @@ -1171,18 +1166,17 @@ void FrameEmitterImpl::EmitCFIInstructions(MCObjectStreamer &streamer, if (BaseLabel && Label) { MCSymbol *ThisSym = Label; if (ThisSym != BaseLabel) { - streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym); + Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym); BaseLabel = ThisSym; } } - EmitCFIInstruction(streamer, Instr); + EmitCFIInstruction(Instr); } } /// Emit the unwind information in a compact way. -void FrameEmitterImpl::EmitCompactUnwind(MCObjectStreamer &Streamer, - const MCDwarfFrameInfo &Frame) { +void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) { MCContext &Context = Streamer.getContext(); const MCObjectFileInfo *MOFI = Context.getObjectFileInfo(); @@ -1254,39 +1248,39 @@ static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) { case 3: return 3; case 4: + case 5: return 4; } llvm_unreachable("Unknown version"); } -const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer, - const MCSymbol *personality, +const MCSymbol &FrameEmitterImpl::EmitCIE(const MCSymbol *personality, unsigned personalityEncoding, const MCSymbol *lsda, bool IsSignalFrame, unsigned lsdaEncoding, bool IsSimple) { - MCContext &context = streamer.getContext(); + MCContext &context = Streamer.getContext(); const MCRegisterInfo *MRI = context.getRegisterInfo(); const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); MCSymbol *sectionStart = context.createTempSymbol(); - streamer.EmitLabel(sectionStart); + Streamer.EmitLabel(sectionStart); MCSymbol *sectionEnd = context.createTempSymbol(); // Length - const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart, - *sectionEnd, 4); - emitAbsValue(streamer, Length, 4); + const MCExpr *Length = + MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4); + emitAbsValue(Streamer, Length, 4); // CIE ID unsigned CIE_ID = IsEH ? 0 : -1; - streamer.EmitIntValue(CIE_ID, 4); + Streamer.EmitIntValue(CIE_ID, 4); // Version uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion()); - streamer.EmitIntValue(CIEVersion, 1); + Streamer.EmitIntValue(CIEVersion, 1); // Augmentation String SmallString<8> Augmentation; @@ -1299,31 +1293,31 @@ const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer, Augmentation += "R"; if (IsSignalFrame) Augmentation += "S"; - streamer.EmitBytes(Augmentation); + Streamer.EmitBytes(Augmentation); } - streamer.EmitIntValue(0, 1); + Streamer.EmitIntValue(0, 1); if (CIEVersion >= 4) { // Address Size - streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1); + Streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1); // Segment Descriptor Size - streamer.EmitIntValue(0, 1); + Streamer.EmitIntValue(0, 1); } // Code Alignment Factor - streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment()); + Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment()); // Data Alignment Factor - streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer)); + Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer)); // Return Address Register if (CIEVersion == 1) { assert(MRI->getRARegister() <= 255 && "DWARF 2 encodes return_address_register in one byte"); - streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1); + Streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1); } else { - streamer.EmitULEB128IntValue( + Streamer.EmitULEB128IntValue( MRI->getDwarfRegNum(MRI->getRARegister(), IsEH)); } @@ -1335,28 +1329,28 @@ const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer, // Personality Encoding augmentationLength += 1; // Personality - augmentationLength += getSizeForEncoding(streamer, personalityEncoding); + augmentationLength += getSizeForEncoding(Streamer, personalityEncoding); } if (lsda) augmentationLength += 1; // Encoding of the FDE pointers augmentationLength += 1; - streamer.EmitULEB128IntValue(augmentationLength); + Streamer.EmitULEB128IntValue(augmentationLength); // Augmentation Data (optional) if (personality) { // Personality Encoding - emitEncodingByte(streamer, personalityEncoding); + emitEncodingByte(Streamer, personalityEncoding); // Personality - EmitPersonality(streamer, *personality, personalityEncoding); + EmitPersonality(Streamer, *personality, personalityEncoding); } if (lsda) - emitEncodingByte(streamer, lsdaEncoding); + emitEncodingByte(Streamer, lsdaEncoding); // Encoding of the FDE pointers - emitEncodingByte(streamer, MOFI->getFDEEncoding()); + emitEncodingByte(Streamer, MOFI->getFDEEncoding()); } // Initial Instructions @@ -1365,22 +1359,23 @@ const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer, if (!IsSimple) { const std::vector<MCCFIInstruction> &Instructions = MAI->getInitialFrameState(); - EmitCFIInstructions(streamer, Instructions, nullptr); + EmitCFIInstructions(Instructions, nullptr); } InitialCFAOffset = CFAOffset; // Padding - streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize()); + Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize()); - streamer.EmitLabel(sectionEnd); + Streamer.EmitLabel(sectionEnd); return *sectionStart; } -MCSymbol *FrameEmitterImpl::EmitFDE(MCObjectStreamer &streamer, - const MCSymbol &cieStart, - const MCDwarfFrameInfo &frame) { - MCContext &context = streamer.getContext(); +void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart, + const MCDwarfFrameInfo &frame, + bool LastInSection, + const MCSymbol &SectionStart) { + MCContext &context = Streamer.getContext(); MCSymbol *fdeStart = context.createTempSymbol(); MCSymbol *fdeEnd = context.createTempSymbol(); const MCObjectFileInfo *MOFI = context.getObjectFileInfo(); @@ -1388,107 +1383,103 @@ MCSymbol *FrameEmitterImpl::EmitFDE(MCObjectStreamer &streamer, CFAOffset = InitialCFAOffset; // Length - const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0); - emitAbsValue(streamer, Length, 4); + const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0); + emitAbsValue(Streamer, Length, 4); - streamer.EmitLabel(fdeStart); + Streamer.EmitLabel(fdeStart); // CIE Pointer const MCAsmInfo *asmInfo = context.getAsmInfo(); if (IsEH) { - const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart, - 0); - emitAbsValue(streamer, offset, 4); + const MCExpr *offset = + MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0); + emitAbsValue(Streamer, offset, 4); } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) { - const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart, - cieStart, 0); - emitAbsValue(streamer, offset, 4); + const MCExpr *offset = + MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0); + emitAbsValue(Streamer, offset, 4); } else { - streamer.EmitSymbolValue(&cieStart, 4); + Streamer.EmitSymbolValue(&cieStart, 4); } // PC Begin unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr; - unsigned PCSize = getSizeForEncoding(streamer, PCEncoding); - emitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH); + unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding); + emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH); // PC Range - const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin, - *frame.End, 0); - emitAbsValue(streamer, Range, PCSize); + const MCExpr *Range = + MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0); + emitAbsValue(Streamer, Range, PCSize); if (IsEH) { // Augmentation Data Length unsigned augmentationLength = 0; if (frame.Lsda) - augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding); + augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding); - streamer.EmitULEB128IntValue(augmentationLength); + Streamer.EmitULEB128IntValue(augmentationLength); // Augmentation Data if (frame.Lsda) - emitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true); + emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true); } // Call Frame Instructions - EmitCFIInstructions(streamer, frame.Instructions, frame.Begin); + EmitCFIInstructions(frame.Instructions, frame.Begin); // Padding - streamer.EmitValueToAlignment(PCSize); + // The size of a .eh_frame section has to be a multiple of the alignment + // since a null CIE is interpreted as the end. Old systems overaligned + // .eh_frame, so we do too and account for it in the last FDE. + unsigned Align = LastInSection ? asmInfo->getPointerSize() : PCSize; + Streamer.EmitValueToAlignment(Align); - return fdeEnd; + Streamer.EmitLabel(fdeEnd); } namespace { - struct CIEKey { - static const CIEKey getEmptyKey() { - return CIEKey(nullptr, 0, -1, false, false); - } - static const CIEKey getTombstoneKey() { - return CIEKey(nullptr, -1, 0, false, false); - } +struct CIEKey { + static const CIEKey getEmptyKey() { + return CIEKey(nullptr, 0, -1, false, false); + } + static const CIEKey getTombstoneKey() { + return CIEKey(nullptr, -1, 0, false, false); + } - CIEKey(const MCSymbol *Personality_, unsigned PersonalityEncoding_, - unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) - : Personality(Personality_), PersonalityEncoding(PersonalityEncoding_), - LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_), - IsSimple(IsSimple_) {} - const MCSymbol *Personality; - unsigned PersonalityEncoding; - unsigned LsdaEncoding; - bool IsSignalFrame; - bool IsSimple; - }; -} + CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding, + unsigned LsdaEncoding, bool IsSignalFrame, bool IsSimple) + : Personality(Personality), PersonalityEncoding(PersonalityEncoding), + LsdaEncoding(LsdaEncoding), IsSignalFrame(IsSignalFrame), + IsSimple(IsSimple) {} + const MCSymbol *Personality; + unsigned PersonalityEncoding; + unsigned LsdaEncoding; + bool IsSignalFrame; + bool IsSimple; +}; +} // anonymous namespace namespace llvm { - template <> - struct DenseMapInfo<CIEKey> { - static CIEKey getEmptyKey() { - return CIEKey::getEmptyKey(); - } - static CIEKey getTombstoneKey() { - return CIEKey::getTombstoneKey(); - } - static unsigned getHashValue(const CIEKey &Key) { - return static_cast<unsigned>(hash_combine(Key.Personality, - Key.PersonalityEncoding, - Key.LsdaEncoding, - Key.IsSignalFrame, - Key.IsSimple)); - } - static bool isEqual(const CIEKey &LHS, - const CIEKey &RHS) { - return LHS.Personality == RHS.Personality && - LHS.PersonalityEncoding == RHS.PersonalityEncoding && - LHS.LsdaEncoding == RHS.LsdaEncoding && - LHS.IsSignalFrame == RHS.IsSignalFrame && - LHS.IsSimple == RHS.IsSimple; - } - }; -} +template <> struct DenseMapInfo<CIEKey> { + static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); } + static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); } + static unsigned getHashValue(const CIEKey &Key) { + return static_cast<unsigned>( + hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding, + Key.IsSignalFrame, Key.IsSimple)); + } + static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) { + return LHS.Personality == RHS.Personality && + LHS.PersonalityEncoding == RHS.PersonalityEncoding && + LHS.LsdaEncoding == RHS.LsdaEncoding && + LHS.IsSignalFrame == RHS.IsSignalFrame && + LHS.IsSimple == RHS.IsSimple; + } +}; +} // namespace llvm void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB, bool IsEH) { @@ -1496,7 +1487,7 @@ void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB, MCContext &Context = Streamer.getContext(); const MCObjectFileInfo *MOFI = Context.getObjectFileInfo(); - FrameEmitterImpl Emitter(IsEH); + FrameEmitterImpl Emitter(IsEH, Streamer); ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos(); // Emit the compact unwind info if available. @@ -1514,7 +1505,7 @@ void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB, NeedsEHFrameSection |= Frame.CompactUnwindEncoding == MOFI->getCompactUnwindDwarfEHFrameOnly(); - Emitter.EmitCompactUnwind(Streamer, Frame); + Emitter.EmitCompactUnwind(Frame); } } @@ -1527,23 +1518,15 @@ void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB, Streamer.SwitchSection(&Section); MCSymbol *SectionStart = Context.createTempSymbol(); Streamer.EmitLabel(SectionStart); - Emitter.setSectionStart(SectionStart); - MCSymbol *FDEEnd = nullptr; DenseMap<CIEKey, const MCSymbol *> CIEStarts; const MCSymbol *DummyDebugKey = nullptr; - NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame(); - for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) { - const MCDwarfFrameInfo &Frame = FrameArray[i]; - - // Emit the label from the previous iteration - if (FDEEnd) { - Streamer.EmitLabel(FDEEnd); - FDEEnd = nullptr; - } - - if (!NeedsEHFrameSection && Frame.CompactUnwindEncoding != + bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind(); + for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) { + const MCDwarfFrameInfo &Frame = *I; + ++I; + if (CanOmitDwarf && Frame.CompactUnwindEncoding != MOFI->getCompactUnwindDwarfEHFrameOnly()) // Don't generate an EH frame if we don't need one. I.e., it's taken care // of by the compact unwind encoding. @@ -1553,18 +1536,12 @@ void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB, Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple); const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey; if (!CIEStart) - CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality, - Frame.PersonalityEncoding, Frame.Lsda, - Frame.IsSignalFrame, - Frame.LsdaEncoding, - Frame.IsSimple); + CIEStart = &Emitter.EmitCIE(Frame.Personality, Frame.PersonalityEncoding, + Frame.Lsda, Frame.IsSignalFrame, + Frame.LsdaEncoding, Frame.IsSimple); - FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame); + Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart); } - - Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize()); - if (FDEEnd) - Streamer.EmitLabel(FDEEnd); } void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer, diff --git a/lib/MC/MCELFObjectTargetWriter.cpp b/lib/MC/MCELFObjectTargetWriter.cpp index bc0ba85a8ff6..de645cac7370 100644 --- a/lib/MC/MCELFObjectTargetWriter.cpp +++ b/lib/MC/MCELFObjectTargetWriter.cpp @@ -29,23 +29,7 @@ bool MCELFObjectTargetWriter::needsRelocateWithSymbol(const MCSymbol &Sym, return false; } -// ELF doesn't require relocations to be in any order. We sort by the Offset, -// just to match gnu as for easier comparison. The use type is an arbitrary way -// of making the sort deterministic. -static int cmpRel(const ELFRelocationEntry *AP, const ELFRelocationEntry *BP) { - const ELFRelocationEntry &A = *AP; - const ELFRelocationEntry &B = *BP; - if (A.Offset != B.Offset) - return B.Offset - A.Offset; - if (B.Type != A.Type) - return A.Type - B.Type; - //llvm_unreachable("ELFRelocs might be unstable!"); - return 0; -} - - void MCELFObjectTargetWriter::sortRelocs(const MCAssembler &Asm, std::vector<ELFRelocationEntry> &Relocs) { - array_pod_sort(Relocs.begin(), Relocs.end(), cmpRel); } diff --git a/lib/MC/MCELFStreamer.cpp b/lib/MC/MCELFStreamer.cpp index fe9ac21e17fc..06d161bccab4 100644 --- a/lib/MC/MCELFStreamer.cpp +++ b/lib/MC/MCELFStreamer.cpp @@ -68,7 +68,6 @@ void MCELFStreamer::mergeFragment(MCDataFragment *DF, EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding)); Assembler.writeFragmentPadding(*EF, FSize, OW); - VecOS.flush(); delete OW; DF->getContents().append(Code.begin(), Code.end()); @@ -87,20 +86,10 @@ void MCELFStreamer::mergeFragment(MCDataFragment *DF, } void MCELFStreamer::InitSections(bool NoExecStack) { - // This emulates the same behavior of GNU as. This makes it easier - // to compare the output as the major sections are in the same order. MCContext &Ctx = getContext(); SwitchSection(Ctx.getObjectFileInfo()->getTextSection()); EmitCodeAlignment(4); - SwitchSection(Ctx.getObjectFileInfo()->getDataSection()); - EmitCodeAlignment(4); - - SwitchSection(Ctx.getObjectFileInfo()->getBSSSection()); - EmitCodeAlignment(4); - - SwitchSection(Ctx.getObjectFileInfo()->getTextSection()); - if (NoExecStack) SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx)); } @@ -112,7 +101,7 @@ void MCELFStreamer::EmitLabel(MCSymbol *S) { MCObjectStreamer::EmitLabel(Symbol); const MCSectionELF &Section = - static_cast<const MCSectionELF&>(Symbol->getSection()); + static_cast<const MCSectionELF &>(*getCurrentSectionOnly()); if (Section.getFlags() & ELF::SHF_TLS) Symbol->setType(ELF::STT_TLS); } @@ -134,7 +123,7 @@ void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { llvm_unreachable("invalid assembler flag!"); } -// If bundle aligment is used and there are any instructions in the section, it +// If bundle alignment is used and there are any instructions in the section, it // needs to be aligned to at least the bundle size. static void setSectionAlignmentForBundling(const MCAssembler &Assembler, MCSection *Section) { @@ -312,13 +301,20 @@ void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size, Symbol->setType(ELF::STT_OBJECT); if (Symbol->getBinding() == ELF::STB_LOCAL) { - MCSection *Section = getAssembler().getContext().getELFSection( + MCSection &Section = *getAssembler().getContext().getELFSection( ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); + MCSectionSubPair P = getCurrentSection(); + SwitchSection(&Section); - AssignSection(Symbol, Section); + EmitValueToAlignment(ByteAlignment, 0, 1, 0); + EmitLabel(Symbol); + EmitZeros(Size); - struct LocalCommon L = {Symbol, Size, ByteAlignment}; - LocalCommons.push_back(L); + // Update the maximum alignment of the section if necessary. + if (ByteAlignment > Section.getAlignment()) + Section.setAlignment(ByteAlignment); + + SwitchSection(P.first, P.second); } else { if(Symbol->declareCommon(Size, ByteAlignment)) report_fatal_error("Symbol: " + Symbol->getName() + @@ -344,7 +340,7 @@ void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size, } void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, - const SMLoc &Loc) { + SMLoc Loc) { if (isBundleLocked()) report_fatal_error("Emitting values inside a locked bundle is forbidden"); fixSymbolsInTLSFixups(Value); @@ -480,7 +476,6 @@ void MCELFStreamer::EmitInstToData(const MCInst &Inst, SmallString<256> Code; raw_svector_ostream VecOS(Code); Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); - VecOS.flush(); for (unsigned i = 0, e = Fixups.size(); i != e; ++i) fixSymbolsInTLSFixups(Fixups[i].getValue()); @@ -603,7 +598,7 @@ void MCELFStreamer::EmitBundleUnlock() { report_fatal_error("Empty bundle-locked group is forbidden"); // When the -mc-relax-all flag is used, we emit instructions to fragments - // stored on a stack. When the bundle unlock is emited, we pop a fragment + // stored on a stack. When the bundle unlock is emitted, we pop a fragment // from the stack a merge it to the one below. if (getAssembler().getRelaxAll()) { assert(!BundleGroups.empty() && "There are no bundle groups"); @@ -625,29 +620,6 @@ void MCELFStreamer::EmitBundleUnlock() { Sec.setBundleLockState(MCSection::NotBundleLocked); } -void MCELFStreamer::Flush() { - for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), - e = LocalCommons.end(); - i != e; ++i) { - const MCSymbol &Symbol = *i->Symbol; - uint64_t Size = i->Size; - unsigned ByteAlignment = i->ByteAlignment; - MCSection &Section = Symbol.getSection(); - - getAssembler().registerSection(Section); - new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &Section); - - MCFragment *F = new MCFillFragment(0, 0, Size, &Section); - Symbol.setFragment(F); - - // Update the maximum alignment of the section if necessary. - if (ByteAlignment > Section.getAlignment()) - Section.setAlignment(ByteAlignment); - } - - LocalCommons.clear(); -} - void MCELFStreamer::FinishImpl() { // Ensure the last section gets aligned if necessary. MCSection *CurSection = getCurrentSectionOnly(); @@ -655,8 +627,6 @@ void MCELFStreamer::FinishImpl() { EmitFrames(nullptr); - Flush(); - this->MCObjectStreamer::FinishImpl(); } diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index a30ceecc952b..0f26b38c29d7 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -43,7 +43,7 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const { const MCSymbol &Sym = SRE.getSymbol(); // Parenthesize names that start with $ so that they don't look like // absolute names. - bool UseParens = Sym.getName()[0] == '$'; + bool UseParens = Sym.getName().size() && Sym.getName()[0] == '$'; if (UseParens) { OS << '('; Sym.print(OS, MAI); @@ -202,6 +202,7 @@ StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { case VK_SIZE: return "SIZE"; case VK_WEAKREF: return "WEAKREF"; case VK_ARM_NONE: return "none"; + case VK_ARM_GOT_PREL: return "GOT_PREL"; case VK_ARM_TARGET1: return "target1"; case VK_ARM_TARGET2: return "target2"; case VK_ARM_PREL31: return "prel31"; @@ -311,7 +312,6 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) { .Case("got", VK_GOT) .Case("gotoff", VK_GOTOFF) .Case("gotpcrel", VK_GOTPCREL) - .Case("got_prel", VK_GOTPCREL) .Case("gottpoff", VK_GOTTPOFF) .Case("indntpoff", VK_INDNTPOFF) .Case("ntpoff", VK_NTPOFF) @@ -382,7 +382,15 @@ MCSymbolRefExpr::getVariantKindForName(StringRef Name) { .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO) .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI) .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA) + .Case("gdgot", VK_Hexagon_GD_GOT) + .Case("gdplt", VK_Hexagon_GD_PLT) + .Case("iegot", VK_Hexagon_IE_GOT) + .Case("ie", VK_Hexagon_IE) + .Case("ldgot", VK_Hexagon_LD_GOT) + .Case("ldplt", VK_Hexagon_LD_PLT) + .Case("pcrel", VK_Hexagon_PCREL) .Case("none", VK_ARM_NONE) + .Case("got_prel", VK_ARM_GOT_PREL) .Case("target1", VK_ARM_TARGET1) .Case("target2", VK_ARM_TARGET2) .Case("prel31", VK_ARM_PREL31) @@ -477,7 +485,8 @@ static void AttemptToFoldSymbolOffsetDifference( if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet)) return; - if (SA.getFragment() == SB.getFragment()) { + if (SA.getFragment() == SB.getFragment() && !SA.isVariable() && + !SB.isVariable()) { Addend += (SA.getOffset() - SB.getOffset()); // Pointers to Thumb symbols need to have their low-bit set to allow @@ -583,11 +592,6 @@ EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout, const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A; const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B; - // If we have a negated symbol, then we must have also have a non-negated - // symbol in order to encode the expression. - if (B && !A) - return false; - Res = MCValue::get(A, B, Result_Cst); return true; } @@ -606,7 +610,7 @@ bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const { true); } -static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) { +static bool canExpand(const MCSymbol &Sym, bool InSet) { const MCExpr *Expr = Sym.getVariableValue(); const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr); if (Inner) { @@ -616,9 +620,7 @@ static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) { if (InSet) return true; - if (!Asm) - return false; - return !Asm->getWriter().isWeak(Sym); + return !Sym.isInSection(); } bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, @@ -643,7 +645,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, // Evaluate recursively if this is a variable. if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None && - canExpand(Sym, Asm, InSet)) { + canExpand(Sym, InSet)) { bool IsMachO = SRE->hasSubsectionsViaSymbols(); if (Sym.getVariableValue()->evaluateAsRelocatableImpl( Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) { @@ -739,7 +741,17 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, case MCBinaryExpr::AShr: Result = LHS >> RHS; break; case MCBinaryExpr::Add: Result = LHS + RHS; break; case MCBinaryExpr::And: Result = LHS & RHS; break; - case MCBinaryExpr::Div: Result = LHS / RHS; break; + case MCBinaryExpr::Div: + // Handle division by zero. gas just emits a warning and keeps going, + // we try to be stricter. + // FIXME: Currently the caller of this function has no way to understand + // we're bailing out because of 'division by zero'. Therefore, it will + // emit a 'expected relocatable expression' error. It would be nice to + // change this code to emit a better diagnostic. + if (RHS == 0) + return false; + Result = LHS / RHS; + break; case MCBinaryExpr::EQ: Result = LHS == RHS; break; case MCBinaryExpr::GT: Result = LHS > RHS; break; case MCBinaryExpr::GTE: Result = LHS >= RHS; break; @@ -765,45 +777,41 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, llvm_unreachable("Invalid assembly expression kind!"); } -MCSection *MCExpr::findAssociatedSection() const { +MCFragment *MCExpr::findAssociatedFragment() const { switch (getKind()) { case Target: // We never look through target specific expressions. - return cast<MCTargetExpr>(this)->findAssociatedSection(); + return cast<MCTargetExpr>(this)->findAssociatedFragment(); case Constant: - return MCSymbol::AbsolutePseudoSection; + return MCSymbol::AbsolutePseudoFragment; case SymbolRef: { const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this); const MCSymbol &Sym = SRE->getSymbol(); - - if (Sym.isDefined()) - return &Sym.getSection(); - - return nullptr; + return Sym.getFragment(); } case Unary: - return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedSection(); + return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment(); case Binary: { const MCBinaryExpr *BE = cast<MCBinaryExpr>(this); - MCSection *LHS_S = BE->getLHS()->findAssociatedSection(); - MCSection *RHS_S = BE->getRHS()->findAssociatedSection(); + MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment(); + MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment(); - // If either section is absolute, return the other. - if (LHS_S == MCSymbol::AbsolutePseudoSection) - return RHS_S; - if (RHS_S == MCSymbol::AbsolutePseudoSection) - return LHS_S; + // If either is absolute, return the other. + if (LHS_F == MCSymbol::AbsolutePseudoFragment) + return RHS_F; + if (RHS_F == MCSymbol::AbsolutePseudoFragment) + return LHS_F; // Not always correct, but probably the best we can do without more context. if (BE->getOpcode() == MCBinaryExpr::Sub) - return MCSymbol::AbsolutePseudoSection; + return MCSymbol::AbsolutePseudoFragment; - // Otherwise, return the first non-null section. - return LHS_S ? LHS_S : RHS_S; + // Otherwise, return the first non-null fragment. + return LHS_F ? LHS_F : RHS_F; } } diff --git a/lib/MC/MCFragment.cpp b/lib/MC/MCFragment.cpp new file mode 100644 index 000000000000..efdb7049203a --- /dev/null +++ b/lib/MC/MCFragment.cpp @@ -0,0 +1,458 @@ +//===- lib/MC/MCFragment.cpp - Assembler Fragment Implementation ----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/MC/MCFragment.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/Twine.h" +#include "llvm/MC/MCAsmBackend.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCAsmLayout.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCDwarf.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCFixupKindInfo.h" +#include "llvm/MC/MCSection.h" +#include "llvm/MC/MCSectionELF.h" +#include "llvm/MC/MCSymbol.h" +#include "llvm/MC/MCValue.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/LEB128.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/raw_ostream.h" +#include <tuple> +using namespace llvm; + +MCAsmLayout::MCAsmLayout(MCAssembler &Asm) + : Assembler(Asm), LastValidFragment() + { + // Compute the section layout order. Virtual sections must go last. + for (MCSection &Sec : Asm) + if (!Sec.isVirtualSection()) + SectionOrder.push_back(&Sec); + for (MCSection &Sec : Asm) + if (Sec.isVirtualSection()) + SectionOrder.push_back(&Sec); +} + +bool MCAsmLayout::isFragmentValid(const MCFragment *F) const { + const MCSection *Sec = F->getParent(); + const MCFragment *LastValid = LastValidFragment.lookup(Sec); + if (!LastValid) + return false; + assert(LastValid->getParent() == Sec); + return F->getLayoutOrder() <= LastValid->getLayoutOrder(); +} + +void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) { + // If this fragment wasn't already valid, we don't need to do anything. + if (!isFragmentValid(F)) + return; + + // Otherwise, reset the last valid fragment to the previous fragment + // (if this is the first fragment, it will be NULL). + LastValidFragment[F->getParent()] = F->getPrevNode(); +} + +void MCAsmLayout::ensureValid(const MCFragment *F) const { + MCSection *Sec = F->getParent(); + MCSection::iterator I; + if (MCFragment *Cur = LastValidFragment[Sec]) + I = ++MCSection::iterator(Cur); + else + I = Sec->begin(); + + // Advance the layout position until the fragment is valid. + while (!isFragmentValid(F)) { + assert(I != Sec->end() && "Layout bookkeeping error"); + const_cast<MCAsmLayout *>(this)->layoutFragment(&*I); + ++I; + } +} + +uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const { + ensureValid(F); + assert(F->Offset != ~UINT64_C(0) && "Address not set!"); + return F->Offset; +} + +// Simple getSymbolOffset helper for the non-varibale case. +static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S, + bool ReportError, uint64_t &Val) { + if (!S.getFragment()) { + if (ReportError) + report_fatal_error("unable to evaluate offset to undefined symbol '" + + S.getName() + "'"); + return false; + } + Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset(); + return true; +} + +static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S, + bool ReportError, uint64_t &Val) { + if (!S.isVariable()) + return getLabelOffset(Layout, S, ReportError, Val); + + // If SD is a variable, evaluate it. + MCValue Target; + if (!S.getVariableValue()->evaluateAsValue(Target, Layout)) + report_fatal_error("unable to evaluate offset for variable '" + + S.getName() + "'"); + + uint64_t Offset = Target.getConstant(); + + const MCSymbolRefExpr *A = Target.getSymA(); + if (A) { + uint64_t ValA; + if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA)) + return false; + Offset += ValA; + } + + const MCSymbolRefExpr *B = Target.getSymB(); + if (B) { + uint64_t ValB; + if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB)) + return false; + Offset -= ValB; + } + + Val = Offset; + return true; +} + +bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const { + return getSymbolOffsetImpl(*this, S, false, Val); +} + +uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const { + uint64_t Val; + getSymbolOffsetImpl(*this, S, true, Val); + return Val; +} + +const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const { + if (!Symbol.isVariable()) + return &Symbol; + + const MCExpr *Expr = Symbol.getVariableValue(); + MCValue Value; + if (!Expr->evaluateAsValue(Value, *this)) { + Assembler.getContext().reportError( + SMLoc(), "expression could not be evaluated"); + return nullptr; + } + + const MCSymbolRefExpr *RefB = Value.getSymB(); + if (RefB) { + Assembler.getContext().reportError( + SMLoc(), Twine("symbol '") + RefB->getSymbol().getName() + + "' could not be evaluated in a subtraction expression"); + return nullptr; + } + + const MCSymbolRefExpr *A = Value.getSymA(); + if (!A) + return nullptr; + + const MCSymbol &ASym = A->getSymbol(); + const MCAssembler &Asm = getAssembler(); + if (ASym.isCommon()) { + // FIXME: we should probably add a SMLoc to MCExpr. + Asm.getContext().reportError(SMLoc(), + "Common symbol '" + ASym.getName() + + "' cannot be used in assignment expr"); + return nullptr; + } + + return &ASym; +} + +uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const { + // The size is the last fragment's end offset. + const MCFragment &F = Sec->getFragmentList().back(); + return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F); +} + +uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const { + // Virtual sections have no file size. + if (Sec->isVirtualSection()) + return 0; + + // Otherwise, the file size is the same as the address space size. + return getSectionAddressSize(Sec); +} + +uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler, + const MCFragment *F, + uint64_t FOffset, uint64_t FSize) { + uint64_t BundleSize = Assembler.getBundleAlignSize(); + assert(BundleSize > 0 && + "computeBundlePadding should only be called if bundling is enabled"); + uint64_t BundleMask = BundleSize - 1; + uint64_t OffsetInBundle = FOffset & BundleMask; + uint64_t EndOfFragment = OffsetInBundle + FSize; + + // There are two kinds of bundling restrictions: + // + // 1) For alignToBundleEnd(), add padding to ensure that the fragment will + // *end* on a bundle boundary. + // 2) Otherwise, check if the fragment would cross a bundle boundary. If it + // would, add padding until the end of the bundle so that the fragment + // will start in a new one. + if (F->alignToBundleEnd()) { + // Three possibilities here: + // + // A) The fragment just happens to end at a bundle boundary, so we're good. + // B) The fragment ends before the current bundle boundary: pad it just + // enough to reach the boundary. + // C) The fragment ends after the current bundle boundary: pad it until it + // reaches the end of the next bundle boundary. + // + // Note: this code could be made shorter with some modulo trickery, but it's + // intentionally kept in its more explicit form for simplicity. + if (EndOfFragment == BundleSize) + return 0; + else if (EndOfFragment < BundleSize) + return BundleSize - EndOfFragment; + else { // EndOfFragment > BundleSize + return 2 * BundleSize - EndOfFragment; + } + } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize) + return BundleSize - OffsetInBundle; + else + return 0; +} + +/* *** */ + +void ilist_node_traits<MCFragment>::deleteNode(MCFragment *V) { + V->destroy(); +} + +MCFragment::MCFragment() : Kind(FragmentType(~0)), HasInstructions(false), + AlignToBundleEnd(false), BundlePadding(0) { +} + +MCFragment::~MCFragment() { } + +MCFragment::MCFragment(FragmentType Kind, bool HasInstructions, + uint8_t BundlePadding, MCSection *Parent) + : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false), + BundlePadding(BundlePadding), Parent(Parent), Atom(nullptr), + Offset(~UINT64_C(0)) { + if (Parent && !isDummy()) + Parent->getFragmentList().push_back(this); +} + +void MCFragment::destroy() { + // First check if we are the sentinal. + if (Kind == FragmentType(~0)) { + delete this; + return; + } + + switch (Kind) { + case FT_Align: + delete cast<MCAlignFragment>(this); + return; + case FT_Data: + delete cast<MCDataFragment>(this); + return; + case FT_CompactEncodedInst: + delete cast<MCCompactEncodedInstFragment>(this); + return; + case FT_Fill: + delete cast<MCFillFragment>(this); + return; + case FT_Relaxable: + delete cast<MCRelaxableFragment>(this); + return; + case FT_Org: + delete cast<MCOrgFragment>(this); + return; + case FT_Dwarf: + delete cast<MCDwarfLineAddrFragment>(this); + return; + case FT_DwarfFrame: + delete cast<MCDwarfCallFrameFragment>(this); + return; + case FT_LEB: + delete cast<MCLEBFragment>(this); + return; + case FT_SafeSEH: + delete cast<MCSafeSEHFragment>(this); + return; + case FT_Dummy: + delete cast<MCDummyFragment>(this); + return; + } +} + +/* *** */ + +// Debugging methods + +namespace llvm { + +raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { + OS << "<MCFixup" << " Offset:" << AF.getOffset() + << " Value:" << *AF.getValue() + << " Kind:" << AF.getKind() << ">"; + return OS; +} + +} + +#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) +void MCFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "<"; + switch (getKind()) { + case MCFragment::FT_Align: OS << "MCAlignFragment"; break; + case MCFragment::FT_Data: OS << "MCDataFragment"; break; + case MCFragment::FT_CompactEncodedInst: + OS << "MCCompactEncodedInstFragment"; break; + case MCFragment::FT_Fill: OS << "MCFillFragment"; break; + case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break; + case MCFragment::FT_Org: OS << "MCOrgFragment"; break; + case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break; + case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break; + case MCFragment::FT_LEB: OS << "MCLEBFragment"; break; + case MCFragment::FT_SafeSEH: OS << "MCSafeSEHFragment"; break; + case MCFragment::FT_Dummy: + OS << "MCDummyFragment"; + break; + } + + OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder + << " Offset:" << Offset + << " HasInstructions:" << hasInstructions() + << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">"; + + switch (getKind()) { + case MCFragment::FT_Align: { + const MCAlignFragment *AF = cast<MCAlignFragment>(this); + if (AF->hasEmitNops()) + OS << " (emit nops)"; + OS << "\n "; + OS << " Alignment:" << AF->getAlignment() + << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize() + << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">"; + break; + } + case MCFragment::FT_Data: { + const MCDataFragment *DF = cast<MCDataFragment>(this); + OS << "\n "; + OS << " Contents:["; + const SmallVectorImpl<char> &Contents = DF->getContents(); + for (unsigned i = 0, e = Contents.size(); i != e; ++i) { + if (i) OS << ","; + OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); + } + OS << "] (" << Contents.size() << " bytes)"; + + if (DF->fixup_begin() != DF->fixup_end()) { + OS << ",\n "; + OS << " Fixups:["; + for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(), + ie = DF->fixup_end(); it != ie; ++it) { + if (it != DF->fixup_begin()) OS << ",\n "; + OS << *it; + } + OS << "]"; + } + break; + } + case MCFragment::FT_CompactEncodedInst: { + const MCCompactEncodedInstFragment *CEIF = + cast<MCCompactEncodedInstFragment>(this); + OS << "\n "; + OS << " Contents:["; + const SmallVectorImpl<char> &Contents = CEIF->getContents(); + for (unsigned i = 0, e = Contents.size(); i != e; ++i) { + if (i) OS << ","; + OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); + } + OS << "] (" << Contents.size() << " bytes)"; + break; + } + case MCFragment::FT_Fill: { + const MCFillFragment *FF = cast<MCFillFragment>(this); + OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize() + << " Size:" << FF->getSize(); + break; + } + case MCFragment::FT_Relaxable: { + const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this); + OS << "\n "; + OS << " Inst:"; + F->getInst().dump_pretty(OS); + break; + } + case MCFragment::FT_Org: { + const MCOrgFragment *OF = cast<MCOrgFragment>(this); + OS << "\n "; + OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue(); + break; + } + case MCFragment::FT_Dwarf: { + const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this); + OS << "\n "; + OS << " AddrDelta:" << OF->getAddrDelta() + << " LineDelta:" << OF->getLineDelta(); + break; + } + case MCFragment::FT_DwarfFrame: { + const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this); + OS << "\n "; + OS << " AddrDelta:" << CF->getAddrDelta(); + break; + } + case MCFragment::FT_LEB: { + const MCLEBFragment *LF = cast<MCLEBFragment>(this); + OS << "\n "; + OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned(); + break; + } + case MCFragment::FT_SafeSEH: { + const MCSafeSEHFragment *F = cast<MCSafeSEHFragment>(this); + OS << "\n "; + OS << " Sym:" << F->getSymbol(); + break; + } + case MCFragment::FT_Dummy: + break; + } + OS << ">"; +} + +void MCAssembler::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "<MCAssembler\n"; + OS << " Sections:[\n "; + for (iterator it = begin(), ie = end(); it != ie; ++it) { + if (it != begin()) OS << ",\n "; + it->dump(); + } + OS << "],\n"; + OS << " Symbols:["; + + for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { + if (it != symbol_begin()) OS << ",\n "; + OS << "("; + it->dump(); + OS << ", Index:" << it->getIndex() << ", "; + OS << ")"; + } + OS << "]>\n"; +} +#endif diff --git a/lib/MC/MCInst.cpp b/lib/MC/MCInst.cpp index 7ef69be66df6..5f829aeb339c 100644 --- a/lib/MC/MCInst.cpp +++ b/lib/MC/MCInst.cpp @@ -23,6 +23,8 @@ void MCOperand::print(raw_ostream &OS) const { OS << "Reg:" << getReg(); else if (isImm()) OS << "Imm:" << getImm(); + else if (isFPImm()) + OS << "FPImm:" << getFPImm(); else if (isExpr()) { OS << "Expr:(" << *getExpr() << ")"; } else if (isInst()) { diff --git a/lib/MC/MCInstrDesc.cpp b/lib/MC/MCInstrDesc.cpp index 5be2fa1b30b6..ee55f3eff3ac 100644 --- a/lib/MC/MCInstrDesc.cpp +++ b/lib/MC/MCInstrDesc.cpp @@ -53,7 +53,7 @@ bool MCInstrDesc::mayAffectControlFlow(const MCInst &MI, bool MCInstrDesc::hasImplicitDefOfPhysReg(unsigned Reg, const MCRegisterInfo *MRI) const { - if (const uint16_t *ImpDefs = ImplicitDefs) + if (const MCPhysReg *ImpDefs = ImplicitDefs) for (; *ImpDefs; ++ImpDefs) if (*ImpDefs == Reg || (MRI && MRI->isSubRegister(Reg, *ImpDefs))) return true; diff --git a/lib/MC/MCMachOStreamer.cpp b/lib/MC/MCMachOStreamer.cpp index 53cd1317a3d7..21f7571eec4a 100644 --- a/lib/MC/MCMachOStreamer.cpp +++ b/lib/MC/MCMachOStreamer.cpp @@ -60,6 +60,7 @@ public: /// state management void reset() override { + CreatedADWARFSection = false; HasSectionLabel.clear(); MCObjectStreamer::reset(); } @@ -180,8 +181,6 @@ void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol, void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) { assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); - // isSymbolLinkerVisible uses the section. - AssignSection(Symbol, getCurrentSection().first); // We have to create a new fragment if this is an atom defining symbol, // fragments cannot span atoms. if (getAssembler().isSymbolLinkerVisible(*Symbol)) @@ -384,8 +383,6 @@ void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); - AssignSection(Symbol, nullptr); - getAssembler().registerSymbol(*Symbol); Symbol->setExternal(true); Symbol->setCommon(Size, ByteAlignment); @@ -417,8 +414,6 @@ void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol, if (ByteAlignment != 1) new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section); - AssignSection(Symbol, Section); - MCFragment *F = new MCFillFragment(0, 0, Size, Section); Symbol->setFragment(F); @@ -443,12 +438,11 @@ void MCMachOStreamer::EmitInstToData(const MCInst &Inst, SmallString<256> Code; raw_svector_ostream VecOS(Code); getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); - VecOS.flush(); // Add the fixups and data. - for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { - Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); - DF->getFixups().push_back(Fixups[i]); + for (MCFixup &Fixup : Fixups) { + Fixup.setOffset(Fixup.getOffset() + DF->getContents().size()); + DF->getFixups().push_back(Fixup); } DF->getContents().append(Code.begin(), Code.end()); } @@ -463,7 +457,8 @@ void MCMachOStreamer::FinishImpl() { // defining symbols. DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap; for (const MCSymbol &Symbol : getAssembler().symbols()) { - if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.getFragment()) { + if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() && + !Symbol.isVariable()) { // An atom defining symbol should never be internal to a fragment. assert(Symbol.getOffset() == 0 && "Invalid offset in atom defining symbol!"); @@ -473,14 +468,12 @@ void MCMachOStreamer::FinishImpl() { // Set the fragment atom associations by tracking the last seen atom defining // symbol. - for (MCAssembler::iterator it = getAssembler().begin(), - ie = getAssembler().end(); it != ie; ++it) { + for (MCSection &Sec : getAssembler()) { const MCSymbol *CurrentAtom = nullptr; - for (MCSection::iterator it2 = it->begin(), ie2 = it->end(); it2 != ie2; - ++it2) { - if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(it2)) + for (MCFragment &Frag : Sec) { + if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag)) CurrentAtom = Symbol; - it2->setAtom(CurrentAtom); + Frag.setAtom(CurrentAtom); } } @@ -493,6 +486,26 @@ MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB, bool LabelSections) { MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE, DWARFMustBeAtTheEnd, LabelSections); + const Triple &TT = Context.getObjectFileInfo()->getTargetTriple(); + if (TT.isOSDarwin()) { + unsigned Major, Minor, Update; + TT.getOSVersion(Major, Minor, Update); + // If there is a version specified, Major will be non-zero. + if (Major) { + MCVersionMinType VersionType; + if (TT.isWatchOS()) + VersionType = MCVM_WatchOSVersionMin; + else if (TT.isTvOS()) + VersionType = MCVM_TvOSVersionMin; + else if (TT.isMacOSX()) + VersionType = MCVM_OSXVersionMin; + else { + assert(TT.isiOS() && "Must only be iOS platform left"); + VersionType = MCVM_IOSVersionMin; + } + S->EmitVersionMin(VersionType, Major, Minor, Update); + } + } if (RelaxAll) S->getAssembler().setRelaxAll(true); return S; diff --git a/lib/MC/MCObjectFileInfo.cpp b/lib/MC/MCObjectFileInfo.cpp index 576827a72d56..028f2e955b21 100644 --- a/lib/MC/MCObjectFileInfo.cpp +++ b/lib/MC/MCObjectFileInfo.cpp @@ -16,6 +16,8 @@ #include "llvm/MC/MCSectionCOFF.h" #include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCSectionMachO.h" +#include "llvm/Support/COFF.h" + using namespace llvm; static bool useCompactUnwind(const Triple &T) { @@ -27,6 +29,10 @@ static bool useCompactUnwind(const Triple &T) { if (T.getArch() == Triple::aarch64) return true; + // armv7k always has it. + if (T.isWatchOS()) + return true; + // Use it on newer version of OS X. if (T.isMacOSX() && !T.isMacOSXVersionLT(10, 6)) return true; @@ -43,9 +49,18 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { // MachO SupportsWeakOmittedEHFrame = false; + EHFrameSection = Ctx->getMachOSection( + "__TEXT", "__eh_frame", + MachO::S_COALESCED | MachO::S_ATTR_NO_TOC | + MachO::S_ATTR_STRIP_STATIC_SYMS | MachO::S_ATTR_LIVE_SUPPORT, + SectionKind::getReadOnly()); + if (T.isOSDarwin() && T.getArch() == Triple::aarch64) SupportsCompactUnwindWithoutEHFrame = true; + if (T.isWatchOS()) + OmitDwarfIfHaveCompactUnwind = true; + PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; LSDAEncoding = FDECFIEncoding = dwarf::DW_EH_PE_pcrel; @@ -61,16 +76,15 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { MachO::S_ATTR_PURE_INSTRUCTIONS, SectionKind::getText()); DataSection // .data - = Ctx->getMachOSection("__DATA", "__data", 0, - SectionKind::getDataRel()); + = Ctx->getMachOSection("__DATA", "__data", 0, SectionKind::getData()); // BSSSection might not be expected initialized on msvc. BSSSection = nullptr; TLSDataSection // .tdata - = Ctx->getMachOSection("__DATA", "__thread_data", - MachO::S_THREAD_LOCAL_REGULAR, - SectionKind::getDataRel()); + = Ctx->getMachOSection("__DATA", "__thread_data", + MachO::S_THREAD_LOCAL_REGULAR, + SectionKind::getData()); TLSBSSSection // .tbss = Ctx->getMachOSection("__DATA", "__thread_bss", MachO::S_THREAD_LOCAL_ZEROFILL, @@ -78,14 +92,13 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { // TODO: Verify datarel below. TLSTLVSection // .tlv - = Ctx->getMachOSection("__DATA", "__thread_vars", - MachO::S_THREAD_LOCAL_VARIABLES, - SectionKind::getDataRel()); + = Ctx->getMachOSection("__DATA", "__thread_vars", + MachO::S_THREAD_LOCAL_VARIABLES, + SectionKind::getData()); - TLSThreadInitSection - = Ctx->getMachOSection("__DATA", "__thread_init", - MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS, - SectionKind::getDataRel()); + TLSThreadInitSection = Ctx->getMachOSection( + "__DATA", "__thread_init", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS, + SectionKind::getData()); CStringSection // .cstring = Ctx->getMachOSection("__TEXT", "__cstring", @@ -112,22 +125,35 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { = Ctx->getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly()); - TextCoalSection - = Ctx->getMachOSection("__TEXT", "__textcoal_nt", - MachO::S_COALESCED | - MachO::S_ATTR_PURE_INSTRUCTIONS, - SectionKind::getText()); - ConstTextCoalSection - = Ctx->getMachOSection("__TEXT", "__const_coal", - MachO::S_COALESCED, - SectionKind::getReadOnly()); + // If the target is not powerpc, map the coal sections to the non-coal + // sections. + // + // "__TEXT/__textcoal_nt" => section "__TEXT/__text" + // "__TEXT/__const_coal" => section "__TEXT/__const" + // "__DATA/__datacoal_nt" => section "__DATA/__data" + Triple::ArchType ArchTy = T.getArch(); + + if (ArchTy == Triple::ppc || ArchTy == Triple::ppc64) { + TextCoalSection + = Ctx->getMachOSection("__TEXT", "__textcoal_nt", + MachO::S_COALESCED | + MachO::S_ATTR_PURE_INSTRUCTIONS, + SectionKind::getText()); + ConstTextCoalSection + = Ctx->getMachOSection("__TEXT", "__const_coal", + MachO::S_COALESCED, + SectionKind::getReadOnly()); + DataCoalSection = Ctx->getMachOSection( + "__DATA", "__datacoal_nt", MachO::S_COALESCED, SectionKind::getData()); + } else { + TextCoalSection = TextSection; + ConstTextCoalSection = ReadOnlySection; + DataCoalSection = DataSection; + } + ConstDataSection // .const_data = Ctx->getMachOSection("__DATA", "__const", 0, SectionKind::getReadOnlyWithRel()); - DataCoalSection - = Ctx->getMachOSection("__DATA","__datacoal_nt", - MachO::S_COALESCED, - SectionKind::getDataRel()); DataCommonSection = Ctx->getMachOSection("__DATA","__common", MachO::S_ZEROFILL, @@ -147,21 +173,17 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { SectionKind::getMetadata()); if (RelocM == Reloc::Static) { - StaticCtorSection - = Ctx->getMachOSection("__TEXT", "__constructor", 0, - SectionKind::getDataRel()); - StaticDtorSection - = Ctx->getMachOSection("__TEXT", "__destructor", 0, - SectionKind::getDataRel()); + StaticCtorSection = Ctx->getMachOSection("__TEXT", "__constructor", 0, + SectionKind::getData()); + StaticDtorSection = Ctx->getMachOSection("__TEXT", "__destructor", 0, + SectionKind::getData()); } else { - StaticCtorSection - = Ctx->getMachOSection("__DATA", "__mod_init_func", - MachO::S_MOD_INIT_FUNC_POINTERS, - SectionKind::getDataRel()); - StaticDtorSection - = Ctx->getMachOSection("__DATA", "__mod_term_func", - MachO::S_MOD_TERM_FUNC_POINTERS, - SectionKind::getDataRel()); + StaticCtorSection = Ctx->getMachOSection("__DATA", "__mod_init_func", + MachO::S_MOD_INIT_FUNC_POINTERS, + SectionKind::getData()); + StaticDtorSection = Ctx->getMachOSection("__DATA", "__mod_term_func", + MachO::S_MOD_TERM_FUNC_POINTERS, + SectionKind::getData()); } // Exception Handling. @@ -176,9 +198,11 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { SectionKind::getReadOnly()); if (T.getArch() == Triple::x86_64 || T.getArch() == Triple::x86) - CompactUnwindDwarfEHFrameOnly = 0x04000000; + CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_X86_64_MODE_DWARF else if (T.getArch() == Triple::aarch64) - CompactUnwindDwarfEHFrameOnly = 0x03000000; + CompactUnwindDwarfEHFrameOnly = 0x03000000; // UNWIND_ARM64_MODE_DWARF + else if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb) + CompactUnwindDwarfEHFrameOnly = 0x04000000; // UNWIND_ARM_MODE_DWARF } // Debug Information. @@ -235,6 +259,12 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(Triple T) { DwarfDebugInlineSection = Ctx->getMachOSection("__DWARF", "__debug_inlined", MachO::S_ATTR_DEBUG, SectionKind::getMetadata()); + DwarfCUIndexSection = + Ctx->getMachOSection("__DWARF", "__debug_cu_index", MachO::S_ATTR_DEBUG, + SectionKind::getMetadata()); + DwarfTUIndexSection = + Ctx->getMachOSection("__DWARF", "__debug_tu_index", MachO::S_ATTR_DEBUG, + SectionKind::getMetadata()); StackMapSection = Ctx->getMachOSection("__LLVM_STACKMAPS", "__llvm_stackmaps", 0, SectionKind::getMetadata()); @@ -258,7 +288,6 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) { FDECFIEncoding = dwarf::DW_EH_PE_pcrel | ((CMModel == CodeModel::Large) ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); - break; default: FDECFIEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; @@ -391,17 +420,15 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) { break; } + unsigned EHSectionType = T.getArch() == Triple::x86_64 + ? ELF::SHT_X86_64_UNWIND + : ELF::SHT_PROGBITS; + // Solaris requires different flags for .eh_frame to seemingly every other // platform. - EHSectionType = ELF::SHT_PROGBITS; - EHSectionFlags = ELF::SHF_ALLOC; - if (T.isOSSolaris()) { - if (T.getArch() == Triple::x86_64) - EHSectionType = ELF::SHT_X86_64_UNWIND; - else - EHSectionFlags |= ELF::SHF_WRITE; - } - + unsigned EHSectionFlags = ELF::SHF_ALLOC; + if (T.isOSSolaris() && T.getArch() != Triple::x86_64) + EHSectionFlags |= ELF::SHF_WRITE; // ELF BSSSection = Ctx->getELFSection(".bss", ELF::SHT_NOBITS, @@ -423,18 +450,9 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) { TLSBSSSection = Ctx->getELFSection( ".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE); - DataRelSection = Ctx->getELFSection(".data.rel", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC | ELF::SHF_WRITE); - - DataRelLocalSection = Ctx->getELFSection(".data.rel.local", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC | ELF::SHF_WRITE); - DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_WRITE); - DataRelROLocalSection = Ctx->getELFSection( - ".data.rel.ro.local", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_WRITE); - MergeableConst4Section = Ctx->getELFSection(".rodata.cst4", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | ELF::SHF_MERGE, 4, ""); @@ -519,14 +537,28 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(Triple T) { DwarfAddrSection = Ctx->getELFSection(".debug_addr", ELF::SHT_PROGBITS, 0, "addr_sec"); + // DWP Sections + DwarfCUIndexSection = + Ctx->getELFSection(".debug_cu_index", ELF::SHT_PROGBITS, 0); + DwarfTUIndexSection = + Ctx->getELFSection(".debug_tu_index", ELF::SHT_PROGBITS, 0); + StackMapSection = Ctx->getELFSection(".llvm_stackmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); FaultMapSection = Ctx->getELFSection(".llvm_faultmaps", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); + + EHFrameSection = + Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags); } void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { + EHFrameSection = Ctx->getCOFFSection( + ".eh_frame", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, + SectionKind::getData()); + bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb; CommDirectiveSupportsAlignment = true; @@ -545,7 +577,7 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { DataSection = Ctx->getCOFFSection( ".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); + SectionKind::getData()); ReadOnlySection = Ctx->getCOFFSection( ".rdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getReadOnly()); @@ -563,21 +595,20 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { StaticCtorSection = Ctx->getCOFFSection( ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); + SectionKind::getData()); StaticDtorSection = Ctx->getCOFFSection( ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); + SectionKind::getData()); } // FIXME: We're emitting LSDA info into a readonly section on COFF, even // though it contains relocatable pointers. In PIC mode, this is probably a // big runtime hit for C++ apps. Either the contents of the LSDA need to be // adjusted or this should be a data section. - assert(T.isOSWindows() && "Windows is the only supported COFF target"); if (T.getArch() == Triple::x86_64) { // On Windows 64 with SEH, the LSDA is emitted into the .xdata section - LSDASection = 0; + LSDASection = nullptr; } else { LSDASection = Ctx->getCOFFSection(".gcc_except_table", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | @@ -693,6 +724,16 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, SectionKind::getMetadata(), "addr_sec"); + DwarfCUIndexSection = Ctx->getCOFFSection( + ".debug_cu_index", + COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); + DwarfTUIndexSection = Ctx->getCOFFSection( + ".debug_tu_index", + COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getMetadata()); DwarfAccelNamesSection = Ctx->getCOFFSection( ".apple_names", COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | @@ -720,11 +761,11 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { PDataSection = Ctx->getCOFFSection( ".pdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, - SectionKind::getDataRel()); + SectionKind::getData()); XDataSection = Ctx->getCOFFSection( ".xdata", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, - SectionKind::getDataRel()); + SectionKind::getData()); SXDataSection = Ctx->getCOFFSection(".sxdata", COFF::IMAGE_SCN_LNK_INFO, SectionKind::getMetadata()); @@ -732,12 +773,12 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(Triple T) { TLSDataSection = Ctx->getCOFFSection( ".tls$", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); - + SectionKind::getData()); + StackMapSection = Ctx->getCOFFSection(".llvm_stackmaps", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, - SectionKind::getReadOnly()); + SectionKind::getReadOnly()); } void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, @@ -752,6 +793,7 @@ void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, CommDirectiveSupportsAlignment = true; SupportsWeakOmittedEHFrame = true; SupportsCompactUnwindWithoutEHFrame = false; + OmitDwarfIfHaveCompactUnwind = false; PersonalityEncoding = LSDAEncoding = FDECFIEncoding = TTypeEncoding = dwarf::DW_EH_PE_absptr; @@ -767,25 +809,26 @@ void MCObjectFileInfo::InitMCObjectFileInfo(const Triple &TheTriple, TT = TheTriple; - Triple::ArchType Arch = TT.getArch(); - // FIXME: Checking for Arch here to filter out bogus triples such as - // cellspu-apple-darwin. Perhaps we should fix in Triple? - if ((Arch == Triple::x86 || Arch == Triple::x86_64 || - Arch == Triple::arm || Arch == Triple::thumb || - Arch == Triple::aarch64 || - Arch == Triple::ppc || Arch == Triple::ppc64 || - Arch == Triple::UnknownArch) && - TT.isOSBinFormatMachO()) { + switch (TT.getObjectFormat()) { + case Triple::MachO: Env = IsMachO; initMachOMCObjectFileInfo(TT); - } else if ((Arch == Triple::x86 || Arch == Triple::x86_64 || - Arch == Triple::arm || Arch == Triple::thumb) && - (TT.isOSWindows() && TT.getObjectFormat() == Triple::COFF)) { + break; + case Triple::COFF: + if (!TT.isOSWindows()) + report_fatal_error( + "Cannot initialize MC for non-Windows COFF object files."); + Env = IsCOFF; initCOFFMCObjectFileInfo(TT); - } else { + break; + case Triple::ELF: Env = IsELF; initELFMCObjectFileInfo(TT); + break; + case Triple::UnknownObjectFormat: + report_fatal_error("Cannot initialize MC for unknown object file format."); + break; } } @@ -799,24 +842,3 @@ MCSection *MCObjectFileInfo::getDwarfTypesSection(uint64_t Hash) const { return Ctx->getELFSection(".debug_types", ELF::SHT_PROGBITS, ELF::SHF_GROUP, 0, utostr(Hash)); } - -void MCObjectFileInfo::InitEHFrameSection() { - if (Env == IsMachO) - EHFrameSection = - Ctx->getMachOSection("__TEXT", "__eh_frame", - MachO::S_COALESCED | - MachO::S_ATTR_NO_TOC | - MachO::S_ATTR_STRIP_STATIC_SYMS | - MachO::S_ATTR_LIVE_SUPPORT, - SectionKind::getReadOnly()); - else if (Env == IsELF) - EHFrameSection = - Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags); - else - EHFrameSection = - Ctx->getCOFFSection(".eh_frame", - COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | - COFF::IMAGE_SCN_MEM_READ | - COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); -} diff --git a/lib/MC/MCObjectStreamer.cpp b/lib/MC/MCObjectStreamer.cpp index 0a637775d4ee..d0a7dafa15b8 100644 --- a/lib/MC/MCObjectStreamer.cpp +++ b/lib/MC/MCObjectStreamer.cpp @@ -28,7 +28,7 @@ MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, MCCodeEmitter *Emitter_) : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB, *Emitter_, - *TAB.createObjectWriter(OS), OS)), + *TAB.createObjectWriter(OS))), EmitEHFrame(true), EmitDebugFrame(false) {} MCObjectStreamer::~MCObjectStreamer() { @@ -39,26 +39,27 @@ MCObjectStreamer::~MCObjectStreamer() { } void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) { - if (PendingLabels.size()) { - if (!F) { - F = new MCDataFragment(); - MCSection *CurSection = getCurrentSectionOnly(); - CurSection->getFragmentList().insert(CurInsertionPoint, F); - F->setParent(CurSection); - } - for (MCSymbol *Sym : PendingLabels) { - Sym->setFragment(F); - Sym->setOffset(FOffset); - } - PendingLabels.clear(); + if (PendingLabels.empty()) + return; + if (!F) { + F = new MCDataFragment(); + MCSection *CurSection = getCurrentSectionOnly(); + CurSection->getFragmentList().insert(CurInsertionPoint, F); + F->setParent(CurSection); + } + for (MCSymbol *Sym : PendingLabels) { + Sym->setFragment(F); + Sym->setOffset(FOffset); } + PendingLabels.clear(); } void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) { // If not assigned to the same (valid) fragment, fallback. - if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment()) { + if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() || + Hi->isVariable() || Lo->isVariable()) { MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size); return; } @@ -93,7 +94,7 @@ MCFragment *MCObjectStreamer::getCurrentFragment() const { assert(getCurrentSectionOnly() && "No current section!"); if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin()) - return std::prev(CurInsertionPoint); + return &*std::prev(CurInsertionPoint); return nullptr; } @@ -121,7 +122,7 @@ void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) { } void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, - const SMLoc &Loc) { + SMLoc Loc) { MCStreamer::EmitValueImpl(Value, Size, Loc); MCDataFragment *DF = getOrCreateDataFragment(); flushPendingLabels(DF, DF->getContents().size()); @@ -155,7 +156,6 @@ void MCObjectStreamer::EmitLabel(MCSymbol *Symbol) { MCStreamer::EmitLabel(Symbol); getAssembler().registerSymbol(*Symbol); - assert(!Symbol->getFragment() && "Unexpected fragment on symbol data!"); // If there is a current fragment, mark the symbol as pointing into it. // Otherwise queue the label and set its fragment pointer when we emit the @@ -276,7 +276,6 @@ void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst, raw_svector_ostream VecOS(Code); getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(), STI); - VecOS.flush(); IF->getContents().append(Code.begin(), Code.end()); } @@ -321,8 +320,10 @@ static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, return AddrDelta; } -static void emitDwarfSetLineAddr(MCObjectStreamer &OS, int64_t LineDelta, - const MCSymbol *Label, int PointerSize) { +static void emitDwarfSetLineAddr(MCObjectStreamer &OS, + MCDwarfLineTableParams Params, + int64_t LineDelta, const MCSymbol *Label, + int PointerSize) { // emit the sequence to set the address OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1); OS.EmitULEB128IntValue(PointerSize + 1); @@ -330,7 +331,7 @@ static void emitDwarfSetLineAddr(MCObjectStreamer &OS, int64_t LineDelta, OS.EmitSymbolValue(Label, PointerSize); // emit the sequence for the LineDelta (from 1) and a zero address delta. - MCDwarfLineAddr::Emit(&OS, LineDelta, 0); + MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0); } void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, @@ -338,13 +339,15 @@ void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *Label, unsigned PointerSize) { if (!LastLabel) { - emitDwarfSetLineAddr(*this, LineDelta, Label, PointerSize); + emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta, + Label, PointerSize); return; } const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); int64_t Res; if (AddrDelta->evaluateAsAbsolute(Res, getAssembler())) { - MCDwarfLineAddr::Emit(this, LineDelta, Res); + MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta, + Res); return; } insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); @@ -388,26 +391,9 @@ void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment, cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true); } -bool MCObjectStreamer::EmitValueToOffset(const MCExpr *Offset, +void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value) { - int64_t Res; - if (Offset->evaluateAsAbsolute(Res, getAssembler())) { - insert(new MCOrgFragment(*Offset, Value)); - return false; - } - - MCSymbol *CurrentPos = getContext().createTempSymbol(); - EmitLabel(CurrentPos); - MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; - const MCExpr *Ref = - MCSymbolRefExpr::create(CurrentPos, Variant, getContext()); - const MCExpr *Delta = - MCBinaryExpr::create(MCBinaryExpr::Sub, Offset, Ref, getContext()); - - if (!Delta->evaluateAsAbsolute(Res, getAssembler())) - return true; - EmitFill(Res, Value); - return false; + insert(new MCOrgFragment(*Offset, Value)); } // Associate GPRel32 fixup with data and resize data area @@ -430,19 +416,31 @@ void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) { DF->getContents().resize(DF->getContents().size() + 8, 0); } -void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { - // FIXME: A MCFillFragment would be more memory efficient but MCExpr has - // problems evaluating expressions across multiple fragments. +bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name, + const MCExpr *Expr, SMLoc Loc) { + int64_t OffsetValue; + if (!Offset.evaluateAsAbsolute(OffsetValue)) + llvm_unreachable("Offset is not absolute"); + MCDataFragment *DF = getOrCreateDataFragment(); flushPendingLabels(DF, DF->getContents().size()); - DF->getContents().append(NumBytes, FillValue); + + MCFixupKind Kind; + if (!Assembler->getBackend().getFixupKind(Name, Kind)) + return true; + + if (Expr == nullptr) + Expr = + MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext()); + DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc)); + return false; } -void MCObjectStreamer::EmitZeros(uint64_t NumBytes) { +void MCObjectStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) { const MCSection *Sec = getCurrentSection().first; assert(Sec && "need a section"); unsigned ItemSize = Sec->isVirtualSection() ? 0 : 1; - insert(new MCFillFragment(0, ItemSize, NumBytes)); + insert(new MCFillFragment(FillValue, ItemSize, NumBytes)); } void MCObjectStreamer::FinishImpl() { @@ -451,7 +449,7 @@ void MCObjectStreamer::FinishImpl() { MCGenDwarfInfo::Emit(this); // Dump out the dwarf file & directory tables and line tables. - MCDwarfLineTable::Emit(this); + MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams()); flushPendingLabels(nullptr); getAssembler().Finish(); diff --git a/lib/MC/MCObjectWriter.cpp b/lib/MC/MCObjectWriter.cpp index 347903408737..e84f74ae81d6 100644 --- a/lib/MC/MCObjectWriter.cpp +++ b/lib/MC/MCObjectWriter.cpp @@ -33,8 +33,14 @@ bool MCObjectWriter::isSymbolRefDifferenceFullyResolved( if (!SA.getFragment() || !SB.getFragment()) return false; - return isSymbolRefDifferenceFullyResolvedImpl(Asm, SA, *SB.getFragment(), - InSet, false); + return isSymbolRefDifferenceFullyResolvedImpl(Asm, SA, SB, InSet); +} + +bool MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( + const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, + bool InSet) const { + return isSymbolRefDifferenceFullyResolvedImpl(Asm, A, *B.getFragment(), InSet, + false); } bool MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( diff --git a/lib/MC/MCParser/AsmLexer.cpp b/lib/MC/MCParser/AsmLexer.cpp index b983d9995f4d..36c192026856 100644 --- a/lib/MC/MCParser/AsmLexer.cpp +++ b/lib/MC/MCParser/AsmLexer.cpp @@ -436,7 +436,8 @@ StringRef AsmLexer::LexUntilEndOfLine() { return StringRef(TokStart, CurPtr-TokStart); } -const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) { +size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf, + bool ShouldSkipSpace) { const char *SavedTokStart = TokStart; const char *SavedCurPtr = CurPtr; bool SavedAtStartOfLine = isAtStartOfLine; @@ -446,7 +447,16 @@ const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) { SMLoc SavedErrLoc = getErrLoc(); SkipSpace = ShouldSkipSpace; - AsmToken Token = LexToken(); + + size_t ReadCount; + for (ReadCount = 0; ReadCount < Buf.size(); ++ReadCount) { + AsmToken Token = LexToken(); + + Buf[ReadCount] = Token; + + if (Token.is(AsmToken::Eof)) + break; + } SetError(SavedErrLoc, SavedErr); @@ -455,7 +465,7 @@ const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) { CurPtr = SavedCurPtr; TokStart = SavedTokStart; - return Token; + return ReadCount; } bool AsmLexer::isAtStartOfComment(const char *Ptr) { diff --git a/lib/MC/MCParser/AsmParser.cpp b/lib/MC/MCParser/AsmParser.cpp index 04d141389c92..646cbb43cae8 100644 --- a/lib/MC/MCParser/AsmParser.cpp +++ b/lib/MC/MCParser/AsmParser.cpp @@ -33,6 +33,7 @@ #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" #include "llvm/MC/MCTargetAsmParser.h" +#include "llvm/MC/MCValue.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MathExtras.h" @@ -251,14 +252,14 @@ private: bool parseStatement(ParseStatementInfo &Info, MCAsmParserSemaCallback *SI); void eatToEndOfLine(); - bool parseCppHashLineFilenameComment(const SMLoc &L); + bool parseCppHashLineFilenameComment(SMLoc L); void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, ArrayRef<MCAsmMacroParameter> Parameters); bool expandMacro(raw_svector_ostream &OS, StringRef Body, ArrayRef<MCAsmMacroParameter> Parameters, ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, - const SMLoc &L); + SMLoc L); /// \brief Are macros enabled in the parser? bool areMacrosEnabled() {return MacrosEnabledFlag;} @@ -342,6 +343,7 @@ private: enum DirectiveKind { DK_NO_DIRECTIVE, // Placeholder DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT, + DK_RELOC, DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA, DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW, DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR, @@ -374,6 +376,7 @@ private: // ".ascii", ".asciz", ".string" bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); + bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ... bool parseDirectiveOctaValue(); // ".octa" bool parseDirectiveRealValue(const fltSemantics &); // ".single", ... @@ -553,6 +556,8 @@ void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { } bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { + if(getTargetParser().getTargetOptions().MCNoWarn) + return false; if (getTargetParser().getTargetOptions().MCFatalWarnings) return Error(L, Msg, Ranges); printMessage(L, SourceMgr::DK_Warning, Msg, Ranges); @@ -679,11 +684,8 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { // so conservatively exclude them. Only do this if we're finalizing, though, // as otherwise we won't necessarilly have seen everything yet. if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { - const MCContext::SymbolTable &Symbols = getContext().getSymbols(); - for (MCContext::SymbolTable::const_iterator i = Symbols.begin(), - e = Symbols.end(); - i != e; ++i) { - MCSymbol *Sym = i->getValue(); + for (const auto &TableEntry : getContext().getSymbols()) { + MCSymbol *Sym = TableEntry.getValue(); // Variable symbols may not be marked as defined, so check those // explicitly. If we know it's a variable, we have a definition for // the purposes of this check. @@ -691,9 +693,8 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { // FIXME: We would really like to refer back to where the symbol was // first referenced for a source location. We need to add something // to track that. Currently, we just point to the end of the file. - printMessage( - getLexer().getLoc(), SourceMgr::DK_Error, - "assembler local symbol '" + Sym->getName() + "' not defined"); + return Error(getLexer().getLoc(), "assembler local symbol '" + + Sym->getName() + "' not defined"); } } @@ -702,7 +703,7 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { if (!HadError && !NoFinalize) Out.Finish(); - return HadError; + return HadError || getContext().hadError(); } void AsmParser::checkForValidSection() { @@ -865,11 +866,12 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { // If this is an absolute variable reference, substitute it now to preserve // semantics in the face of reassignment. - if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { + if (Sym->isVariable() && + isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) { if (Variant) return Error(EndLoc, "unexpected modifier on variable reference"); - Res = Sym->getVariableValue(); + Res = Sym->getVariableValue(/*SetUsed*/ false); return false; } @@ -1102,8 +1104,9 @@ bool AsmParser::parseAbsoluteExpression(int64_t &Res) { return false; } -unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, - MCBinaryExpr::Opcode &Kind) { +static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, + MCBinaryExpr::Opcode &Kind, + bool ShouldUseLogicalShr) { switch (K) { default: return 0; // not a binop. @@ -1155,7 +1158,7 @@ unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, Kind = MCBinaryExpr::Shl; return 4; case AsmToken::GreaterGreater: - Kind = MAI.shouldUseLogicalShr() ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; + Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; return 4; // High Intermediate Precedence: +, - @@ -1179,6 +1182,89 @@ unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, } } +static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, + MCBinaryExpr::Opcode &Kind, + bool ShouldUseLogicalShr) { + switch (K) { + default: + return 0; // not a binop. + + // Lowest Precedence: &&, || + case AsmToken::AmpAmp: + Kind = MCBinaryExpr::LAnd; + return 2; + case AsmToken::PipePipe: + Kind = MCBinaryExpr::LOr; + return 1; + + // Low Precedence: ==, !=, <>, <, <=, >, >= + case AsmToken::EqualEqual: + Kind = MCBinaryExpr::EQ; + return 3; + case AsmToken::ExclaimEqual: + case AsmToken::LessGreater: + Kind = MCBinaryExpr::NE; + return 3; + case AsmToken::Less: + Kind = MCBinaryExpr::LT; + return 3; + case AsmToken::LessEqual: + Kind = MCBinaryExpr::LTE; + return 3; + case AsmToken::Greater: + Kind = MCBinaryExpr::GT; + return 3; + case AsmToken::GreaterEqual: + Kind = MCBinaryExpr::GTE; + return 3; + + // Low Intermediate Precedence: +, - + case AsmToken::Plus: + Kind = MCBinaryExpr::Add; + return 4; + case AsmToken::Minus: + Kind = MCBinaryExpr::Sub; + return 4; + + // High Intermediate Precedence: |, &, ^ + // + // FIXME: gas seems to support '!' as an infix operator? + case AsmToken::Pipe: + Kind = MCBinaryExpr::Or; + return 5; + case AsmToken::Caret: + Kind = MCBinaryExpr::Xor; + return 5; + case AsmToken::Amp: + Kind = MCBinaryExpr::And; + return 5; + + // Highest Precedence: *, /, %, <<, >> + case AsmToken::Star: + Kind = MCBinaryExpr::Mul; + return 6; + case AsmToken::Slash: + Kind = MCBinaryExpr::Div; + return 6; + case AsmToken::Percent: + Kind = MCBinaryExpr::Mod; + return 6; + case AsmToken::LessLess: + Kind = MCBinaryExpr::Shl; + return 6; + case AsmToken::GreaterGreater: + Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; + return 6; + } +} + +unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, + MCBinaryExpr::Opcode &Kind) { + bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); + return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) + : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); +} + /// \brief Parse all binary operators with precedence >= 'Precedence'. /// Res contains the LHS of the expression on input. bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, @@ -1251,6 +1337,15 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, // Treat '.' as a valid identifier in this context. Lex(); IDVal = "."; + } else if (Lexer.is(AsmToken::LCurly)) { + // Treat '{' as a valid identifier in this context. + Lex(); + IDVal = "{"; + + } else if (Lexer.is(AsmToken::RCurly)) { + // Treat '}' as a valid identifier in this context. + Lex(); + IDVal = "}"; } else if (parseIdentifier(IDVal)) { if (!TheCondState.Ignore) return TokError("unexpected token at start of statement"); @@ -1313,6 +1408,8 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, // See what kind of statement we have. switch (Lexer.getKind()) { case AsmToken::Colon: { + if (!getTargetParser().isLabel(ID)) + break; checkForValidSection(); // identifier ':' -> Label. @@ -1334,8 +1431,8 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); assert(RewrittenLabel.size() && "We should have an internal name here."); - Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc, - IDVal.size(), RewrittenLabel)); + Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), + RewrittenLabel); IDVal = RewrittenLabel; } Sym = getContext().getOrCreateSymbol(IDVal); @@ -1371,6 +1468,8 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, } case AsmToken::Equal: + if (!getTargetParser().equalIsAsmAssignment()) + break; // identifier '=' ... -> assignment statement Lex(); @@ -1599,6 +1698,8 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, return parseDirectiveError(IDLoc, true); case DK_WARNING: return parseDirectiveWarning(IDLoc); + case DK_RELOC: + return parseDirectiveReloc(IDLoc); } return Error(IDLoc, "unknown directive"); @@ -1613,12 +1714,14 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) return parseDirectiveMSAlign(IDLoc, Info); + if (ParsingInlineAsm && (IDVal == "even")) + Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4); checkForValidSection(); // Canonicalize the opcode to lower case. std::string OpcodeStr = IDVal.lower(); ParseInstructionInfo IInfo(Info.AsmRewrites); - bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc, + bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, Info.ParsedOperands); Info.ParseError = HadError; @@ -1703,7 +1806,7 @@ void AsmParser::eatToEndOfLine() { /// parseCppHashLineFilenameComment as this: /// ::= # number "filename" /// or just as a full line comment if it doesn't have a number and a string. -bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) { +bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { Lex(); // Eat the hash token. if (getLexer().isNot(AsmToken::Integer)) { @@ -1743,7 +1846,7 @@ void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { raw_ostream &OS = errs(); const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); - const SMLoc &DiagLoc = Diag.getLoc(); + SMLoc DiagLoc = Diag.getLoc(); unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); unsigned CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); @@ -1802,7 +1905,7 @@ static bool isIdentifierChar(char c) { bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, ArrayRef<MCAsmMacroParameter> Parameters, ArrayRef<MCAsmMacroArgument> A, - bool EnableAtPseudoVariable, const SMLoc &L) { + bool EnableAtPseudoVariable, SMLoc L) { unsigned NParameters = Parameters.size(); bool HasVararg = NParameters ? Parameters.back().Vararg : false; if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) @@ -1858,10 +1961,8 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, break; // Otherwise substitute with the token values, with spaces eliminated. - for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), - ie = A[Index].end(); - it != ie; ++it) - OS << it->getString(); + for (const AsmToken &Token : A[Index]) + OS << Token.getString(); break; } } @@ -1897,15 +1998,13 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, } } else { bool VarargParameter = HasVararg && Index == (NParameters - 1); - for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), - ie = A[Index].end(); - it != ie; ++it) + for (const AsmToken &Token : A[Index]) // We expect no quotes around the string's contents when // parsing for varargs. - if (it->getKind() != AsmToken::String || VarargParameter) - OS << it->getString(); + if (Token.getKind() != AsmToken::String || VarargParameter) + OS << Token.getString(); else - OS << it->getStringContents(); + OS << Token.getStringContents(); Pos += 1 + Argument.size(); } @@ -2371,6 +2470,51 @@ bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { return false; } +/// parseDirectiveReloc +/// ::= .reloc expression , identifier [ , expression ] +bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { + const MCExpr *Offset; + const MCExpr *Expr = nullptr; + + SMLoc OffsetLoc = Lexer.getTok().getLoc(); + if (parseExpression(Offset)) + return true; + + // We can only deal with constant expressions at the moment. + int64_t OffsetValue; + if (!Offset->evaluateAsAbsolute(OffsetValue)) + return Error(OffsetLoc, "expression is not a constant value"); + + if (Lexer.isNot(AsmToken::Comma)) + return TokError("expected comma"); + Lexer.Lex(); + + if (Lexer.isNot(AsmToken::Identifier)) + return TokError("expected relocation name"); + SMLoc NameLoc = Lexer.getTok().getLoc(); + StringRef Name = Lexer.getTok().getIdentifier(); + Lexer.Lex(); + + if (Lexer.is(AsmToken::Comma)) { + Lexer.Lex(); + SMLoc ExprLoc = Lexer.getLoc(); + if (parseExpression(Expr)) + return true; + + MCValue Value; + if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) + return Error(ExprLoc, "expression must be relocatable"); + } + + if (Lexer.isNot(AsmToken::EndOfStatement)) + return TokError("unexpected token in .reloc directive"); + + if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc)) + return Error(NameLoc, "unknown relocation name"); + + return false; +} + /// parseDirectiveValue /// ::= (.byte | .short | ... ) [ expression (, expression)* ] bool AsmParser::parseDirectiveValue(unsigned Size) { @@ -2617,7 +2761,6 @@ bool AsmParser::parseDirectiveOrg() { checkForValidSection(); const MCExpr *Offset; - SMLoc Loc = getTok().getLoc(); if (parseExpression(Offset)) return true; @@ -2636,13 +2779,7 @@ bool AsmParser::parseDirectiveOrg() { } Lex(); - - // Only limited forms of relocatable expressions are accepted here, it - // has to be relative to the current section. The streamer will return - // 'true' if the expression wasn't evaluatable. - if (getStreamer().EmitValueToOffset(Offset, FillExpr)) - return Error(Loc, "expected assembly-time absolute expression"); - + getStreamer().emitValueToOffset(Offset, FillExpr); return false; } @@ -2703,7 +2840,11 @@ bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { Alignment = 1ULL << Alignment; } else { - // Reject alignments that aren't a power of two, for gas compatibility. + // Reject alignments that aren't either a power of two or zero, + // for gas compatibility. Alignment of zero is silently rounded + // up to one. + if (Alignment == 0) + Alignment = 1; if (!isPowerOf2_64(Alignment)) Error(AlignmentLoc, "alignment must be a power of 2"); } @@ -4269,6 +4410,7 @@ void AsmParser::initializeDirectiveKindMap() { DirectiveKindMap[".err"] = DK_ERR; DirectiveKindMap[".error"] = DK_ERROR; DirectiveKindMap[".warning"] = DK_WARNING; + DirectiveKindMap[".reloc"] = DK_RELOC; } MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { @@ -4405,10 +4547,10 @@ bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { SmallString<256> Buf; raw_svector_ostream OS(Buf); - for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { + for (const MCAsmMacroArgument &Arg : A) { // Note that the AtPseudoVariable is enabled for instantiations of .irp. // This is undocumented, but GAS seems to support it. - if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc())) + if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) return true; } @@ -4488,10 +4630,10 @@ bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, if (!MCE) return Error(ExprLoc, "unexpected expression in _emit"); uint64_t IntValue = MCE->getValue(); - if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) + if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) return Error(ExprLoc, "literal value out of range for directive"); - Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len)); + Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); return false; } @@ -4507,8 +4649,7 @@ bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { if (!isPowerOf2_64(IntValue)) return Error(ExprLoc, "literal value not a power of two greater then zero"); - Info.AsmRewrites->push_back( - AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue))); + Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); return false; } @@ -4604,18 +4745,18 @@ bool AsmParser::parseMSInlineAsm( OutputDecls.push_back(OpDecl); OutputDeclsAddressOf.push_back(Operand.needAddressOf()); OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); - AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size())); + AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); } else { InputDecls.push_back(OpDecl); InputDeclsAddressOf.push_back(Operand.needAddressOf()); InputConstraints.push_back(Operand.getConstraint().str()); - AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size())); + AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); } } // Consider implicit defs to be clobbers. Think of cpuid and push. - ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(), - Desc.getNumImplicitDefs()); + ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), + Desc.getNumImplicitDefs()); ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); } @@ -4710,14 +4851,23 @@ bool AsmParser::parseMSInlineAsm( OS << ".byte"; break; case AOK_Align: { - unsigned Val = AR.Val; - OS << ".align " << Val; + // MS alignment directives are measured in bytes. If the native assembler + // measures alignment in bytes, we can pass it straight through. + OS << ".align"; + if (getContext().getAsmInfo()->getAlignmentIsInBytes()) + break; - // Skip the original immediate. + // Alignment is in log2 form, so print that instead and skip the original + // immediate. + unsigned Val = AR.Val; + OS << ' ' << Val; assert(Val < 10 && "Expected alignment less then 2^10."); AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; break; } + case AOK_EVEN: + OS << ".even"; + break; case AOK_DotOperator: // Insert the dot if the user omitted it. OS.flush(); @@ -4803,7 +4953,8 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef, // FIXME: Diagnose assignment to protected identifier (e.g., register name). if (isSymbolUsedInExpression(Sym, Value)) return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); - else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable()) + else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && + !Sym->isVariable()) ; // Allow redefinitions of undefined symbols only used in directives. else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) ; // Allow redefinitions of variables that haven't yet been used. @@ -4815,15 +4966,8 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef, return Parser.Error(EqualLoc, "invalid reassignment of non-absolute variable '" + Name + "'"); - - // Don't count these checks as uses. - Sym->setUsed(false); } else if (Name == ".") { - if (Parser.getStreamer().EmitValueToOffset(Value, 0)) { - Parser.Error(EqualLoc, "expected absolute expression"); - Parser.eatToEndOfStatement(); - return true; - } + Parser.getStreamer().emitValueToOffset(Value, 0); return false; } else Sym = Parser.getContext().getOrCreateSymbol(Name); diff --git a/lib/MC/MCParser/COFFAsmParser.cpp b/lib/MC/MCParser/COFFAsmParser.cpp index f09bce005d6a..a4b2b195f710 100644 --- a/lib/MC/MCParser/COFFAsmParser.cpp +++ b/lib/MC/MCParser/COFFAsmParser.cpp @@ -98,11 +98,10 @@ class COFFAsmParser : public MCAsmParserExtension { SectionKind::getText()); } bool ParseSectionDirectiveData(StringRef, SMLoc) { - return ParseSectionSwitch(".data", - COFF::IMAGE_SCN_CNT_INITIALIZED_DATA - | COFF::IMAGE_SCN_MEM_READ - | COFF::IMAGE_SCN_MEM_WRITE, - SectionKind::getDataRel()); + return ParseSectionSwitch(".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ | + COFF::IMAGE_SCN_MEM_WRITE, + SectionKind::getData()); } bool ParseSectionDirectiveBSS(StringRef, SMLoc) { return ParseSectionSwitch(".bss", @@ -153,7 +152,7 @@ static SectionKind computeSectionKind(unsigned Flags) { if (Flags & COFF::IMAGE_SCN_MEM_READ && (Flags & COFF::IMAGE_SCN_MEM_WRITE) == 0) return SectionKind::getReadOnly(); - return SectionKind::getDataRel(); + return SectionKind::getData(); } bool COFFAsmParser::ParseSectionFlags(StringRef FlagsString, unsigned* Flags) { diff --git a/lib/MC/MCParser/DarwinAsmParser.cpp b/lib/MC/MCParser/DarwinAsmParser.cpp index dc664e8a8f61..73e068a34391 100644 --- a/lib/MC/MCParser/DarwinAsmParser.cpp +++ b/lib/MC/MCParser/DarwinAsmParser.cpp @@ -8,10 +8,13 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/MCParser/MCAsmParserExtension.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCContext.h" +#include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCParser/MCAsmLexer.h" #include "llvm/MC/MCParser/MCAsmParser.h" #include "llvm/MC/MCSectionMachO.h" @@ -38,6 +41,8 @@ class DarwinAsmParser : public MCAsmParserExtension { unsigned TAA = 0, unsigned ImplicitAlign = 0, unsigned StubSize = 0); + SMLoc LastVersionMinDirective; + public: DarwinAsmParser() {} @@ -164,9 +169,14 @@ public: addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveTLV>(".tlv"); addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveIdent>(".ident"); + addDirectiveHandler<&DarwinAsmParser::parseVersionMin>( + ".watchos_version_min"); + addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".tvos_version_min"); addDirectiveHandler<&DarwinAsmParser::parseVersionMin>(".ios_version_min"); addDirectiveHandler<&DarwinAsmParser::parseVersionMin>( ".macosx_version_min"); + + LastVersionMinDirective = SMLoc(); } bool parseDirectiveDesc(StringRef, SMLoc); @@ -381,9 +391,8 @@ bool DarwinAsmParser::parseSectionSwitch(const char *Segment, // FIXME: Arch specific. bool isText = TAA & MachO::S_ATTR_PURE_INSTRUCTIONS; getStreamer().SwitchSection(getContext().getMachOSection( - Segment, Section, TAA, StubSize, - isText ? SectionKind::getText() - : SectionKind::getDataRel())); + Segment, Section, TAA, StubSize, + isText ? SectionKind::getText() : SectionKind::getData())); // Set the implicit alignment, if any. // @@ -579,12 +588,34 @@ bool DarwinAsmParser::parseDirectiveSection(StringRef, SMLoc) { if (!ErrorStr.empty()) return Error(Loc, ErrorStr.c_str()); + // Issue a warning if the target is not powerpc and Section is a *coal* section. + Triple TT = getParser().getContext().getObjectFileInfo()->getTargetTriple(); + Triple::ArchType ArchTy = TT.getArch(); + + if (ArchTy != Triple::ppc && ArchTy != Triple::ppc64) { + StringRef NonCoalSection = StringSwitch<StringRef>(Section) + .Case("__textcoal_nt", "__text") + .Case("__const_coal", "__const") + .Case("__datacoal_nt", "__data") + .Default(Section); + + if (!Section.equals(NonCoalSection)) { + StringRef SectionVal(Loc.getPointer()); + size_t B = SectionVal.find(',') + 1, E = SectionVal.find(',', B); + SMLoc BLoc = SMLoc::getFromPointer(SectionVal.data() + B); + SMLoc ELoc = SMLoc::getFromPointer(SectionVal.data() + E); + getParser().Warning(Loc, "section \"" + Section + "\" is deprecated", + SMRange(BLoc, ELoc)); + getParser().Note(Loc, "change section name to \"" + NonCoalSection + + "\"", SMRange(BLoc, ELoc)); + } + } + // FIXME: Arch specific. bool isText = Segment == "__TEXT"; // FIXME: Hack. getStreamer().SwitchSection(getContext().getMachOSection( - Segment, Section, TAA, StubSize, - isText ? SectionKind::getText() - : SectionKind::getDataRel())); + Segment, Section, TAA, StubSize, + isText ? SectionKind::getText() : SectionKind::getData())); return false; } @@ -636,17 +667,16 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) { "environment variable unset."); // Open the secure log file if we haven't already. - raw_ostream *OS = getContext().getSecureLog(); + raw_fd_ostream *OS = getContext().getSecureLog(); if (!OS) { std::error_code EC; - OS = new raw_fd_ostream(SecureLogFile, EC, - sys::fs::F_Append | sys::fs::F_Text); - if (EC) { - delete OS; + auto NewOS = llvm::make_unique<raw_fd_ostream>( + SecureLogFile, EC, sys::fs::F_Append | sys::fs::F_Text); + if (EC) return Error(IDLoc, Twine("can't open secure log file: ") + SecureLogFile + " (" + EC.message() + ")"); - } - getContext().setSecureLog(OS); + OS = NewOS.get(); + getContext().setSecureLog(std::move(NewOS)); } // Write the message. @@ -867,9 +897,11 @@ bool DarwinAsmParser::parseDirectiveDataRegionEnd(StringRef, SMLoc) { /// parseVersionMin /// ::= .ios_version_min major,minor[,update] /// ::= .macosx_version_min major,minor[,update] -bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc) { +bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc Loc) { int64_t Major = 0, Minor = 0, Update = 0; int Kind = StringSwitch<int>(Directive) + .Case(".watchos_version_min", MCVM_WatchOSVersionMin) + .Case(".tvos_version_min", MCVM_TvOSVersionMin) .Case(".ios_version_min", MCVM_IOSVersionMin) .Case(".macosx_version_min", MCVM_OSXVersionMin); // Get the major version number. @@ -902,6 +934,24 @@ bool DarwinAsmParser::parseVersionMin(StringRef Directive, SMLoc) { Lex(); } + const Triple &T = getContext().getObjectFileInfo()->getTargetTriple(); + Triple::OSType ExpectedOS = Triple::UnknownOS; + switch ((MCVersionMinType)Kind) { + case MCVM_WatchOSVersionMin: ExpectedOS = Triple::WatchOS; break; + case MCVM_TvOSVersionMin: ExpectedOS = Triple::TvOS; break; + case MCVM_IOSVersionMin: ExpectedOS = Triple::IOS; break; + case MCVM_OSXVersionMin: ExpectedOS = Triple::MacOSX; break; + } + if (T.getOS() != ExpectedOS) + Warning(Loc, Directive + " should only be used for " + + Triple::getOSTypeName(ExpectedOS) + " targets"); + + if (LastVersionMinDirective.isValid()) { + Warning(Loc, "overriding previous version_min directive"); + Note(LastVersionMinDirective, "previous definition is here"); + } + LastVersionMinDirective = Loc; + // We've parsed a correct version specifier, so send it to the streamer. getStreamer().EmitVersionMin((MCVersionMinType)Kind, Major, Minor, Update); diff --git a/lib/MC/MCParser/ELFAsmParser.cpp b/lib/MC/MCParser/ELFAsmParser.cpp index 5f8a6039afd0..6cbcdec5e275 100644 --- a/lib/MC/MCParser/ELFAsmParser.cpp +++ b/lib/MC/MCParser/ELFAsmParser.cpp @@ -52,8 +52,6 @@ public: addDirectiveHandler< &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro"); addDirectiveHandler< - &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local"); - addDirectiveHandler< &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame"); addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section"); addDirectiveHandler< @@ -81,8 +79,8 @@ public: // the best way for us to get access to it? bool ParseSectionDirectiveData(StringRef, SMLoc) { return ParseSectionSwitch(".data", ELF::SHT_PROGBITS, - ELF::SHF_WRITE |ELF::SHF_ALLOC, - SectionKind::getDataRel()); + ELF::SHF_WRITE | ELF::SHF_ALLOC, + SectionKind::getData()); } bool ParseSectionDirectiveText(StringRef, SMLoc) { return ParseSectionSwitch(".text", ELF::SHT_PROGBITS, @@ -113,9 +111,8 @@ public: } bool ParseSectionDirectiveDataRel(StringRef, SMLoc) { return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC | - ELF::SHF_WRITE, - SectionKind::getDataRel()); + ELF::SHF_ALLOC | ELF::SHF_WRITE, + SectionKind::getData()); } bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) { return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS, @@ -123,17 +120,10 @@ public: ELF::SHF_WRITE, SectionKind::getReadOnlyWithRel()); } - bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) { - return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC | - ELF::SHF_WRITE, - SectionKind::getReadOnlyWithRelLocal()); - } bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) { return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS, - ELF::SHF_ALLOC | - ELF::SHF_WRITE, - SectionKind::getDataRel()); + ELF::SHF_ALLOC | ELF::SHF_WRITE, + SectionKind::getData()); } bool ParseDirectivePushSection(StringRef, SMLoc); bool ParseDirectivePopSection(StringRef, SMLoc); diff --git a/lib/MC/MCParser/MCAsmLexer.cpp b/lib/MC/MCParser/MCAsmLexer.cpp index 795cc85ef547..e891bd2c6240 100644 --- a/lib/MC/MCParser/MCAsmLexer.cpp +++ b/lib/MC/MCParser/MCAsmLexer.cpp @@ -12,8 +12,8 @@ using namespace llvm; -MCAsmLexer::MCAsmLexer() : CurTok(AsmToken::Error, StringRef()), - TokStart(nullptr), SkipSpace(true) { +MCAsmLexer::MCAsmLexer() : TokStart(nullptr), SkipSpace(true) { + CurTok.emplace_back(AsmToken::Error, StringRef()); } MCAsmLexer::~MCAsmLexer() { diff --git a/lib/MC/MCParser/MCTargetAsmParser.cpp b/lib/MC/MCParser/MCTargetAsmParser.cpp index 60a3a3b59a3d..4e4b47805cd8 100644 --- a/lib/MC/MCParser/MCTargetAsmParser.cpp +++ b/lib/MC/MCParser/MCTargetAsmParser.cpp @@ -7,13 +7,26 @@ // //===----------------------------------------------------------------------===// +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCTargetAsmParser.h" using namespace llvm; -MCTargetAsmParser::MCTargetAsmParser() - : AvailableFeatures(0), ParsingInlineAsm(false) +MCTargetAsmParser::MCTargetAsmParser(MCTargetOptions const &MCOptions, + const MCSubtargetInfo &STI) + : AvailableFeatures(0), ParsingInlineAsm(false), MCOptions(MCOptions), + STI(&STI) { } MCTargetAsmParser::~MCTargetAsmParser() { } + +MCSubtargetInfo &MCTargetAsmParser::copySTI() { + MCSubtargetInfo &STICopy = getContext().getSubtargetCopy(getSTI()); + STI = &STICopy; + return STICopy; +} + +const MCSubtargetInfo &MCTargetAsmParser::getSTI() const { + return *STI; +} diff --git a/lib/MC/MCSection.cpp b/lib/MC/MCSection.cpp index 9152f2b42a48..dbd544a44ce3 100644 --- a/lib/MC/MCSection.cpp +++ b/lib/MC/MCSection.cpp @@ -21,7 +21,7 @@ using namespace llvm; MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false), - IsRegistered(false), Variant(V), Kind(K) {} + IsRegistered(false), DummyFragment(this), Variant(V), Kind(K) {} MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { if (!End) @@ -72,7 +72,7 @@ MCSection::getSubsectionInsertionPoint(unsigned Subsection) { if (MI == SubsectionFragmentMap.end()) IP = end(); else - IP = MI->second; + IP = MI->second->getIterator(); if (!ExactMatch && Subsection != 0) { // The GNU as documentation claims that subsections have an alignment of 4, // although this appears not to be the case. diff --git a/lib/MC/MCSectionCOFF.cpp b/lib/MC/MCSectionCOFF.cpp index ce0b4f5fb411..b8373f40b8be 100644 --- a/lib/MC/MCSectionCOFF.cpp +++ b/lib/MC/MCSectionCOFF.cpp @@ -11,6 +11,7 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCSymbol.h" +#include "llvm/Support/COFF.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; diff --git a/lib/MC/MCSectionELF.cpp b/lib/MC/MCSectionELF.cpp index b4448d79a2d5..5a0bb7fe986f 100644 --- a/lib/MC/MCSectionELF.cpp +++ b/lib/MC/MCSectionELF.cpp @@ -27,12 +27,7 @@ bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name, if (isUnique()) return false; - // FIXME: Does .section .bss/.data/.text work everywhere?? - if (Name == ".text" || Name == ".data" || - (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS())) - return true; - - return false; + return MAI.shouldOmitSectionDirective(Name); } static void printName(raw_ostream &OS, StringRef Name) { @@ -138,6 +133,8 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, OS << "note"; else if (Type == ELF::SHT_PROGBITS) OS << "progbits"; + else if (Type == ELF::SHT_X86_64_UNWIND) + OS << "unwind"; if (EntrySize) { assert(Flags & ELF::SHF_MERGE); diff --git a/lib/MC/MCSectionMachO.cpp b/lib/MC/MCSectionMachO.cpp index c9f15914e4b1..879c6e5ff932 100644 --- a/lib/MC/MCSectionMachO.cpp +++ b/lib/MC/MCSectionMachO.cpp @@ -177,7 +177,7 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. TAAParsed = false; SmallVector<StringRef, 5> SplitSpec; - Spec.split(SplitSpec, ","); + Spec.split(SplitSpec, ','); // Remove leading and trailing whitespace. auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef { return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef(); @@ -235,7 +235,7 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In. // The attribute list is a '+' separated list of attributes. SmallVector<StringRef, 1> SectionAttrs; - Attrs.split(SectionAttrs, "+", /*MaxSplit=*/-1, /*KeepEmpty=*/false); + Attrs.split(SectionAttrs, '+', /*MaxSplit=*/-1, /*KeepEmpty=*/false); for (StringRef &SectionAttr : SectionAttrs) { auto AttrDescriptorI = std::find_if( diff --git a/lib/MC/MCStreamer.cpp b/lib/MC/MCStreamer.cpp index 7fbbbd95b560..836b40544642 100644 --- a/lib/MC/MCStreamer.cpp +++ b/lib/MC/MCStreamer.cpp @@ -107,8 +107,7 @@ void MCStreamer::EmitSLEB128IntValue(int64_t Value) { EmitBytes(OSE.str()); } -void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, - const SMLoc &Loc) { +void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc) { EmitValueImpl(Value, Size, Loc); } @@ -189,11 +188,9 @@ void MCStreamer::InitSections(bool NoExecStack) { SwitchSection(getContext().getObjectFileInfo()->getTextSection()); } -void MCStreamer::AssignSection(MCSymbol *Symbol, MCSection *Section) { - if (Section) - Symbol->setSection(*Section); - else - Symbol->setUndefined(); +void MCStreamer::AssignFragment(MCSymbol *Symbol, MCFragment *Fragment) { + assert(Fragment); + Symbol->setFragment(Fragment); // As we emit symbols into a section, track the order so that they can // be sorted upon later. Zero is reserved to mean 'unemitted'. @@ -203,7 +200,8 @@ void MCStreamer::AssignSection(MCSymbol *Symbol, MCSection *Section) { void MCStreamer::EmitLabel(MCSymbol *Symbol) { assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); assert(getCurrentSection().first && "Cannot emit before setting section!"); - AssignSection(Symbol, getCurrentSection().first); + assert(!Symbol->getFragment() && "Unexpected fragment on symbol data!"); + Symbol->setFragment(&getCurrentSectionOnly()->getDummyFragment()); MCTargetStreamer *TS = getTargetStreamer(); if (TS) @@ -361,6 +359,14 @@ void MCStreamer::EmitCFIEscape(StringRef Values) { CurFrame->Instructions.push_back(Instruction); } +void MCStreamer::EmitCFIGnuArgsSize(int64_t Size) { + MCSymbol *Label = EmitCFICommon(); + MCCFIInstruction Instruction = + MCCFIInstruction::createGnuArgsSize(Label, Size); + MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); + CurFrame->Instructions.push_back(Instruction); +} + void MCStreamer::EmitCFISignalFrame() { EnsureValidDwarfFrame(); MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo(); @@ -467,6 +473,8 @@ void MCStreamer::EmitWinEHHandlerData() { report_fatal_error("Chained unwind areas can't have handlers!"); } +void MCStreamer::EmitSyntaxDirective() {} + void MCStreamer::EmitWinCFIPushReg(unsigned Register) { EnsureValidWinFrameInfo(); @@ -679,8 +687,7 @@ void MCStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, void MCStreamer::ChangeSection(MCSection *, const MCExpr *) {} void MCStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {} void MCStreamer::EmitBytes(StringRef Data) {} -void MCStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, - const SMLoc &Loc) { +void MCStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) { visitUsedExpr(*Value); } void MCStreamer::EmitULEB128Value(const MCExpr *Value) {} @@ -690,9 +697,7 @@ void MCStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, unsigned MaxBytesToEmit) {} void MCStreamer::EmitCodeAlignment(unsigned ByteAlignment, unsigned MaxBytesToEmit) {} -bool MCStreamer::EmitValueToOffset(const MCExpr *Offset, unsigned char Value) { - return false; -} +void MCStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value) {} void MCStreamer::EmitBundleAlignMode(unsigned AlignPow2) {} void MCStreamer::EmitBundleLock(bool AlignToEnd) {} void MCStreamer::FinishImpl() {} diff --git a/lib/MC/MCSubtargetInfo.cpp b/lib/MC/MCSubtargetInfo.cpp index 9210cf544b16..dc864d3a17f8 100644 --- a/lib/MC/MCSubtargetInfo.cpp +++ b/lib/MC/MCSubtargetInfo.cpp @@ -32,8 +32,8 @@ void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef FS) { CPUSchedModel = &MCSchedModel::GetDefaultSchedModel(); } -void MCSubtargetInfo::setDefaultFeatures(StringRef CPU) { - FeatureBits = getFeatures(CPU, "", ProcDesc, ProcFeatures); +void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef FS) { + FeatureBits = getFeatures(CPU, FS, ProcDesc, ProcFeatures); } MCSubtargetInfo::MCSubtargetInfo( @@ -77,13 +77,12 @@ FeatureBitset MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) { const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const { assert(ProcSchedModels && "Processor machine model not available!"); - unsigned NumProcs = ProcDesc.size(); -#ifndef NDEBUG - for (size_t i = 1; i < NumProcs; i++) { - assert(strcmp(ProcSchedModels[i - 1].Key, ProcSchedModels[i].Key) < 0 && - "Processor machine model table is not sorted"); - } -#endif + size_t NumProcs = ProcDesc.size(); + assert(std::is_sorted(ProcSchedModels, ProcSchedModels+NumProcs, + [](const SubtargetInfoKV &LHS, const SubtargetInfoKV &RHS) { + return strcmp(LHS.Key, RHS.Key) < 0; + }) && + "Processor machine model table is not sorted"); // Find entry const SubtargetInfoKV *Found = diff --git a/lib/MC/MCSymbol.cpp b/lib/MC/MCSymbol.cpp index 125380a9d140..ab3b8eb68322 100644 --- a/lib/MC/MCSymbol.cpp +++ b/lib/MC/MCSymbol.cpp @@ -16,8 +16,11 @@ #include "llvm/Support/raw_ostream.h" using namespace llvm; -// Sentinel value for the absolute pseudo section. -MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1); +// Only the address of this fragment is ever actually used. +static MCDummyFragment SentinelFragment(nullptr); + +// Sentinel value for the absolute pseudo fragment. +MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment; void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name, MCContext &Ctx) { diff --git a/lib/MC/MCTargetOptions.cpp b/lib/MC/MCTargetOptions.cpp index 1258d9e29f2e..465622715526 100644 --- a/lib/MC/MCTargetOptions.cpp +++ b/lib/MC/MCTargetOptions.cpp @@ -14,9 +14,10 @@ namespace llvm { MCTargetOptions::MCTargetOptions() : SanitizeAddress(false), MCRelaxAll(false), MCNoExecStack(false), - MCFatalWarnings(false), MCSaveTempLabels(false), - MCUseDwarfDirectory(false), ShowMCEncoding(false), ShowMCInst(false), - AsmVerbose(false), DwarfVersion(0), ABIName() {} + MCFatalWarnings(false), MCNoWarn(false), MCSaveTempLabels(false), + MCUseDwarfDirectory(false), MCIncrementalLinkerCompatible(false), + ShowMCEncoding(false), ShowMCInst(false), AsmVerbose(false), + DwarfVersion(0), ABIName() {} StringRef MCTargetOptions::getABIName() const { return ABIName; diff --git a/lib/MC/MCWinEH.cpp b/lib/MC/MCWinEH.cpp index d5d9eadf39a0..83af203c7acb 100644 --- a/lib/MC/MCWinEH.cpp +++ b/lib/MC/MCWinEH.cpp @@ -49,10 +49,10 @@ static MCSection *getUnwindInfoSection(StringRef SecName, if (CodeSecName.startswith(".text$")) CodeSecName = CodeSecName.substr(6); - return Context.getCOFFSection( - (SecName + Twine('$') + CodeSecName).str(), - COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, - SectionKind::getDataRel()); + return Context.getCOFFSection((SecName + Twine('$') + CodeSecName).str(), + COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getData()); } } diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index 8ce6127e3866..324385fa132a 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -78,7 +78,6 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S, dyn_cast<const MCConstantExpr>(S.getVariableValue())) return C->getValue(); - MCValue Target; if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr)) report_fatal_error("unable to evaluate offset for variable '" + @@ -117,7 +116,8 @@ uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec, return OffsetToAlignment(EndAddr, NextSec.getAlignment()); } -void MachObjectWriter::writeHeader(unsigned NumLoadCommands, +void MachObjectWriter::writeHeader(MachO::HeaderFileType Type, + unsigned NumLoadCommands, unsigned LoadCommandsSize, bool SubsectionsViaSymbols) { uint32_t Flags = 0; @@ -128,7 +128,7 @@ void MachObjectWriter::writeHeader(unsigned NumLoadCommands, // struct mach_header (28 bytes) or // struct mach_header_64 (32 bytes) - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC); @@ -136,29 +136,30 @@ void MachObjectWriter::writeHeader(unsigned NumLoadCommands, write32(TargetObjectWriter->getCPUType()); write32(TargetObjectWriter->getCPUSubtype()); - write32(MachO::MH_OBJECT); + write32(Type); write32(NumLoadCommands); write32(LoadCommandsSize); write32(Flags); if (is64Bit()) write32(0); // reserved - assert(OS.tell() - Start == - (is64Bit()?sizeof(MachO::mach_header_64): sizeof(MachO::mach_header))); + assert( + getStream().tell() - Start == + (is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header))); } /// writeSegmentLoadCommand - Write a segment load command. /// /// \param NumSections The number of sections in this segment. /// \param SectionDataSize The total size of the sections. -void MachObjectWriter::writeSegmentLoadCommand(unsigned NumSections, - uint64_t VMSize, - uint64_t SectionDataStartOffset, - uint64_t SectionDataSize) { +void MachObjectWriter::writeSegmentLoadCommand( + StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, + uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, + uint32_t InitProt) { // struct segment_command (56 bytes) or // struct segment_command_64 (72 bytes) - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; unsigned SegmentLoadCommandSize = @@ -169,31 +170,32 @@ void MachObjectWriter::writeSegmentLoadCommand(unsigned NumSections, NumSections * (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section))); - writeBytes("", 16); + assert(Name.size() <= 16); + writeBytes(Name, 16); if (is64Bit()) { - write64(0); // vmaddr + write64(VMAddr); // vmaddr write64(VMSize); // vmsize write64(SectionDataStartOffset); // file offset write64(SectionDataSize); // file size } else { - write32(0); // vmaddr + write32(VMAddr); // vmaddr write32(VMSize); // vmsize write32(SectionDataStartOffset); // file offset write32(SectionDataSize); // file size } // maxprot - write32(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE); + write32(MaxProt); // initprot - write32(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE); + write32(InitProt); write32(NumSections); write32(0); // flags - assert(OS.tell() - Start == SegmentLoadCommandSize); + assert(getStream().tell() - Start == SegmentLoadCommandSize); } -void MachObjectWriter::writeSection(const MCAssembler &Asm, - const MCAsmLayout &Layout, - const MCSection &Sec, uint64_t FileOffset, +void MachObjectWriter::writeSection(const MCAsmLayout &Layout, + const MCSection &Sec, uint64_t VMAddr, + uint64_t FileOffset, unsigned Flags, uint64_t RelocationsStart, unsigned NumRelocations) { uint64_t SectionSize = Layout.getSectionAddressSize(&Sec); @@ -208,24 +210,20 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm, // struct section (68 bytes) or // struct section_64 (80 bytes) - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; writeBytes(Section.getSectionName(), 16); writeBytes(Section.getSegmentName(), 16); if (is64Bit()) { - write64(getSectionAddress(&Sec)); // address + write64(VMAddr); // address write64(SectionSize); // size } else { - write32(getSectionAddress(&Sec)); // address + write32(VMAddr); // address write32(SectionSize); // size } write32(FileOffset); - unsigned Flags = Section.getTypeAndAttributes(); - if (Section.hasInstructions()) - Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS; - assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!"); write32(Log2_32(Section.getAlignment())); write32(NumRelocations ? RelocationsStart : 0); @@ -236,8 +234,8 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm, if (is64Bit()) write32(0); // reserved3 - assert(OS.tell() - Start == (is64Bit() ? sizeof(MachO::section_64) : - sizeof(MachO::section))); + assert(getStream().tell() - Start == + (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section))); } void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset, @@ -246,7 +244,7 @@ void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset, uint32_t StringTableSize) { // struct symtab_command (24 bytes) - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; write32(MachO::LC_SYMTAB); @@ -256,7 +254,7 @@ void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset, write32(StringTableOffset); write32(StringTableSize); - assert(OS.tell() - Start == sizeof(MachO::symtab_command)); + assert(getStream().tell() - Start == sizeof(MachO::symtab_command)); } void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, @@ -269,7 +267,7 @@ void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, uint32_t NumIndirectSymbols) { // struct dysymtab_command (80 bytes) - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; write32(MachO::LC_DYSYMTAB); @@ -293,7 +291,7 @@ void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, write32(0); // locreloff write32(0); // nlocrel - assert(OS.tell() - Start == sizeof(MachO::dysymtab_command)); + assert(getStream().tell() - Start == sizeof(MachO::dysymtab_command)); } MachObjectWriter::MachSymbolData * @@ -389,7 +387,7 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type, uint32_t DataOffset, uint32_t DataSize) { - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; write32(Type); @@ -397,7 +395,7 @@ void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type, write32(DataOffset); write32(DataSize); - assert(OS.tell() - Start == sizeof(MachO::linkedit_data_command)); + assert(getStream().tell() - Start == sizeof(MachO::linkedit_data_command)); } static unsigned ComputeLinkerOptionsLoadCommandSize( @@ -413,7 +411,7 @@ void MachObjectWriter::writeLinkerOptionsLoadCommand( const std::vector<std::string> &Options) { unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit()); - uint64_t Start = OS.tell(); + uint64_t Start = getStream().tell(); (void) Start; write32(MachO::LC_LINKER_OPTION); @@ -429,7 +427,7 @@ void MachObjectWriter::writeLinkerOptionsLoadCommand( // Pad to a multiple of the pointer size. writeBytes("", OffsetToAlignment(BytesWritten, is64Bit() ? 8 : 4)); - assert(OS.tell() - Start == Size); + assert(getStream().tell() - Start == Size); } void MachObjectWriter::recordRelocation(MCAssembler &Asm, @@ -458,9 +456,9 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) { if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && Section.getType() != MachO::S_SYMBOL_STUBS) { - MCSymbol &Symbol = *it->Symbol; - report_fatal_error("indirect symbol '" + Symbol.getName() + - "' not in a symbol pointer or stub section"); + MCSymbol &Symbol = *it->Symbol; + report_fatal_error("indirect symbol '" + Symbol.getName() + + "' not in a symbol pointer or stub section"); } } @@ -522,7 +520,7 @@ void MachObjectWriter::computeSymbolTable( StringTable.add(Symbol.getName()); } - StringTable.finalize(StringTableBuilder::MachO); + StringTable.finalize(); // Build the symbol arrays but only for non-local symbols. // @@ -628,6 +626,18 @@ void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm, } bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( + const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, + bool InSet) const { + // FIXME: We don't handle things like + // foo = . + // creating atoms. + if (A.isVariable() || B.isVariable()) + return false; + return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, A, B, + InSet); +} + +bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const { if (InSet) @@ -746,7 +756,7 @@ void MachObjectWriter::writeObject(MCAssembler &Asm, ++NumLoadCommands; LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(Option, is64Bit()); } - + // Compute the total size of the section data, as well as its file size and vm // size. uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) : @@ -776,18 +786,25 @@ void MachObjectWriter::writeObject(MCAssembler &Asm, SectionDataFileSize += SectionDataPadding; // Write the prolog, starting with the header and load command... - writeHeader(NumLoadCommands, LoadCommandsSize, + writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize, Asm.getSubsectionsViaSymbols()); - writeSegmentLoadCommand(NumSections, VMSize, - SectionDataStart, SectionDataSize); + uint32_t Prot = + MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE; + writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart, + SectionDataSize, Prot, Prot); // ... and then the section headers. uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; - for (const MCSection &Sec : Asm) { + for (const MCSection &Section : Asm) { + const auto &Sec = cast<MCSectionMachO>(Section); std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; unsigned NumRelocs = Relocs.size(); uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec); - writeSection(Asm, Layout, Sec, SectionStart, RelocTableEnd, NumRelocs); + unsigned Flags = Sec.getTypeAndAttributes(); + if (Sec.hasInstructions()) + Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS; + writeSection(Layout, Sec, getSectionAddress(&Sec), SectionStart, Flags, + RelocTableEnd, NumRelocs); RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info); } @@ -798,8 +815,22 @@ void MachObjectWriter::writeObject(MCAssembler &Asm, assert(VersionInfo.Major < 65536 && "unencodable major target version"); uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) | (VersionInfo.Major << 16); - write32(VersionInfo.Kind == MCVM_OSXVersionMin ? MachO::LC_VERSION_MIN_MACOSX : - MachO::LC_VERSION_MIN_IPHONEOS); + MachO::LoadCommandType LCType; + switch (VersionInfo.Kind) { + case MCVM_OSXVersionMin: + LCType = MachO::LC_VERSION_MIN_MACOSX; + break; + case MCVM_IOSVersionMin: + LCType = MachO::LC_VERSION_MIN_IPHONEOS; + break; + case MCVM_TvOSVersionMin: + LCType = MachO::LC_VERSION_MIN_TVOS; + break; + case MCVM_WatchOSVersionMin: + LCType = MachO::LC_VERSION_MIN_WATCHOS; + break; + } + write32(LCType); write32(sizeof(MachO::version_min_command)); write32(EncodedVersion); write32(0); // reserved. @@ -901,12 +932,12 @@ void MachObjectWriter::writeObject(MCAssembler &Asm, // Write out the loh commands, if there is one. if (LOHSize) { #ifndef NDEBUG - unsigned Start = OS.tell(); + unsigned Start = getStream().tell(); #endif Asm.getLOHContainer().emit(*this, Layout); // Pad to a multiple of the pointer size. writeBytes("", OffsetToAlignment(LOHRawSize, is64Bit() ? 8 : 4)); - assert(OS.tell() - Start == LOHSize); + assert(getStream().tell() - Start == LOHSize); } // Write the symbol table data, if used. @@ -942,7 +973,7 @@ void MachObjectWriter::writeObject(MCAssembler &Asm, writeNlist(Entry, Layout); // Write the string table. - OS << StringTable.data(); + getStream() << StringTable.data(); } } diff --git a/lib/MC/StringTableBuilder.cpp b/lib/MC/StringTableBuilder.cpp index 9de9363611e6..80e552287b3d 100644 --- a/lib/MC/StringTableBuilder.cpp +++ b/lib/MC/StringTableBuilder.cpp @@ -8,35 +8,71 @@ //===----------------------------------------------------------------------===// #include "llvm/MC/StringTableBuilder.h" -#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/Support/COFF.h" #include "llvm/Support/Endian.h" +#include <vector> + using namespace llvm; -static bool compareBySuffix(StringRef a, StringRef b) { - size_t sizeA = a.size(); - size_t sizeB = b.size(); - size_t len = std::min(sizeA, sizeB); - for (size_t i = 0; i < len; ++i) { - char ca = a[sizeA - i - 1]; - char cb = b[sizeB - i - 1]; - if (ca != cb) - return ca > cb; +StringTableBuilder::StringTableBuilder(Kind K) : K(K) {} + +typedef std::pair<StringRef, size_t> StringPair; + +// Returns the character at Pos from end of a string. +static int charTailAt(StringPair *P, size_t Pos) { + StringRef S = P->first; + if (Pos >= S.size()) + return -1; + return (unsigned char)S[S.size() - Pos - 1]; +} + +// Three-way radix quicksort. This is much faster than std::sort with strcmp +// because it does not compare characters that we already know the same. +static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) { +tailcall: + if (End - Begin <= 1) + return; + + // Partition items. Items in [Begin, P) are greater than the pivot, + // [P, Q) are the same as the pivot, and [Q, End) are less than the pivot. + int Pivot = charTailAt(*Begin, Pos); + StringPair **P = Begin; + StringPair **Q = End; + for (StringPair **R = Begin + 1; R < Q;) { + int C = charTailAt(*R, Pos); + if (C > Pivot) + std::swap(*P++, *R++); + else if (C < Pivot) + std::swap(*--Q, *R); + else + R++; + } + + multikey_qsort(Begin, P, Pos); + multikey_qsort(Q, End, Pos); + if (Pivot != -1) { + // qsort(P, Q, Pos + 1), but with tail call optimization. + Begin = P; + End = Q; + ++Pos; + goto tailcall; } - return sizeA > sizeB; } -void StringTableBuilder::finalize(Kind kind) { - SmallVector<StringRef, 8> Strings; +void StringTableBuilder::finalize() { + std::vector<std::pair<StringRef, size_t> *> Strings; Strings.reserve(StringIndexMap.size()); + for (std::pair<StringRef, size_t> &P : StringIndexMap) + Strings.push_back(&P); - for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i) - Strings.push_back(i->getKey()); - - std::sort(Strings.begin(), Strings.end(), compareBySuffix); + if (!Strings.empty()) + multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0); - switch (kind) { + switch (K) { + case RAW: + break; case ELF: case MachO: // Start the table with a NUL byte. @@ -49,22 +85,25 @@ void StringTableBuilder::finalize(Kind kind) { } StringRef Previous; - for (StringRef s : Strings) { - if (kind == WinCOFF) - assert(s.size() > COFF::NameSize && "Short string in COFF string table!"); + for (std::pair<StringRef, size_t> *P : Strings) { + StringRef S = P->first; + if (K == WinCOFF) + assert(S.size() > COFF::NameSize && "Short string in COFF string table!"); - if (Previous.endswith(s)) { - StringIndexMap[s] = StringTable.size() - 1 - s.size(); + if (Previous.endswith(S)) { + P->second = StringTable.size() - S.size() - (K != RAW); continue; } - StringIndexMap[s] = StringTable.size(); - StringTable += s; - StringTable += '\x00'; - Previous = s; + P->second = StringTable.size(); + StringTable += S; + if (K != RAW) + StringTable += '\x00'; + Previous = S; } - switch (kind) { + switch (K) { + case RAW: case ELF: break; case MachO: @@ -75,14 +114,31 @@ void StringTableBuilder::finalize(Kind kind) { case WinCOFF: // Write the table size in the first word. assert(StringTable.size() <= std::numeric_limits<uint32_t>::max()); - uint32_t size = static_cast<uint32_t>(StringTable.size()); + uint32_t Size = static_cast<uint32_t>(StringTable.size()); support::endian::write<uint32_t, support::little, support::unaligned>( - StringTable.data(), size); + StringTable.data(), Size); break; } + + Size = StringTable.size(); } void StringTableBuilder::clear() { StringTable.clear(); StringIndexMap.clear(); } + +size_t StringTableBuilder::getOffset(StringRef S) const { + assert(isFinalized()); + auto I = StringIndexMap.find(S); + assert(I != StringIndexMap.end() && "String is not in table!"); + return I->second; +} + +size_t StringTableBuilder::add(StringRef S) { + assert(!isFinalized()); + auto P = StringIndexMap.insert(std::make_pair(S, Size)); + if (P.second) + Size += S.size() + (K != RAW); + return P.first->second; +} diff --git a/lib/MC/SubtargetFeature.cpp b/lib/MC/SubtargetFeature.cpp index 76574e987cb1..b642f17f0e79 100644 --- a/lib/MC/SubtargetFeature.cpp +++ b/lib/MC/SubtargetFeature.cpp @@ -56,7 +56,7 @@ static inline bool isEnabled(StringRef Feature) { /// static void Split(std::vector<std::string> &V, StringRef S) { SmallVector<StringRef, 3> Tmp; - S.split(Tmp, ",", -1, false /* KeepEmpty */); + S.split(Tmp, ',', -1, false /* KeepEmpty */); V.assign(Tmp.begin(), Tmp.end()); } diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index 56ef1c7a2735..a3820906b76b 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -17,6 +17,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" +#include "llvm/Config/config.h" #include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCContext.h" @@ -32,8 +33,10 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/JamCRC.h" #include "llvm/Support/TimeValue.h" #include <cstdio> +#include <ctime> using namespace llvm; @@ -76,8 +79,6 @@ public: COFFSymbol(StringRef name); void set_name_offset(uint32_t Offset); - bool should_keep() const; - int64_t getIndex() const { return Index; } void setIndex(int Value) { Index = Value; @@ -125,7 +126,7 @@ public: COFF::header Header; sections Sections; symbols Symbols; - StringTableBuilder Strings; + StringTableBuilder Strings{StringTableBuilder::WinCOFF}; // Maps used during object file creation. section_map SectionMap; @@ -160,8 +161,6 @@ public: void SetSymbolName(COFFSymbol &S); void SetSectionName(COFFSection &S); - bool ExportSymbol(const MCSymbol &Symbol, MCAssembler &Asm); - bool IsPhysicalSection(COFFSection *S); // Entity writing methods. @@ -215,38 +214,6 @@ void COFFSymbol::set_name_offset(uint32_t Offset) { write_uint32_le(Data.Name + 4, Offset); } -/// logic to decide if the symbol should be reported in the symbol table -bool COFFSymbol::should_keep() const { - // no section means its external, keep it - if (!Section) - return true; - - // if it has relocations pointing at it, keep it - if (Relocations > 0) { - assert(Section->Number != -1 && "Sections with relocations must be real!"); - return true; - } - - // if this is a safeseh handler, keep it - if (MC && (cast<MCSymbolCOFF>(MC)->isSafeSEH())) - return true; - - // if the section its in is being droped, drop it - if (Section->Number == -1) - return false; - - // if it is the section symbol, keep it - if (Section->Symbol == this) - return true; - - // if its temporary, drop it - if (MC && MC->isTemporary()) - return false; - - // otherwise, keep it - return true; -} - //------------------------------------------------------------------------------ // Section class implementation @@ -392,7 +359,6 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler, const MCAsmLayout &Layout) { COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol); - SymbolMap[&Symbol] = coff_symbol; if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) { coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL; @@ -515,25 +481,6 @@ void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) { std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size()); } -bool WinCOFFObjectWriter::ExportSymbol(const MCSymbol &Symbol, - MCAssembler &Asm) { - // This doesn't seem to be right. Strings referred to from the .data section - // need symbols so they can be linked to code in the .text section right? - - // return Asm.isSymbolLinkerVisible(Symbol); - - // Non-temporary labels should always be visible to the linker. - if (!Symbol.isTemporary()) - return true; - - // Temporary variable symbols are invisible. - if (Symbol.isVariable()) - return false; - - // Absolute temporary labels are never visible. - return !Symbol.isAbsolute(); -} - bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) { return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0; @@ -663,7 +610,7 @@ void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm, defineSection(static_cast<const MCSectionCOFF &>(Section)); for (const MCSymbol &Symbol : Asm.symbols()) - if (ExportSymbol(Symbol, Asm)) + if (!Symbol.isTemporary()) DefineSymbol(Symbol, Asm, Layout); } @@ -674,7 +621,8 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( // thunk to implement their /INCREMENTAL feature. Make sure we don't optimize // away any relocations to functions. uint16_t Type = cast<MCSymbolCOFF>(SymA).getType(); - if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION) + if (Asm.isIncrementalLinkerCompatible() && + (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION) return false; return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, InSet, IsPCRel); @@ -702,41 +650,49 @@ void WinCOFFObjectWriter::recordRelocation( const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) { assert(Target.getSymA() && "Relocation must reference a symbol!"); - const MCSymbol &Symbol = Target.getSymA()->getSymbol(); - const MCSymbol &A = Symbol; - if (!A.isRegistered()) - Asm.getContext().reportFatalError(Fixup.getLoc(), + const MCSymbol &A = Target.getSymA()->getSymbol(); + if (!A.isRegistered()) { + Asm.getContext().reportError(Fixup.getLoc(), Twine("symbol '") + A.getName() + "' can not be undefined"); + return; + } + if (A.isTemporary() && A.isUndefined()) { + Asm.getContext().reportError(Fixup.getLoc(), + Twine("assembler label '") + A.getName() + + "' can not be undefined"); + return; + } MCSection *Section = Fragment->getParent(); // Mark this symbol as requiring an entry in the symbol table. assert(SectionMap.find(Section) != SectionMap.end() && "Section must already have been defined in executePostLayoutBinding!"); - assert(SymbolMap.find(&A) != SymbolMap.end() && - "Symbol must already have been defined in executePostLayoutBinding!"); COFFSection *coff_section = SectionMap[Section]; - COFFSymbol *coff_symbol = SymbolMap[&A]; const MCSymbolRefExpr *SymB = Target.getSymB(); bool CrossSection = false; if (SymB) { const MCSymbol *B = &SymB->getSymbol(); - if (!B->getFragment()) - Asm.getContext().reportFatalError( + if (!B->getFragment()) { + Asm.getContext().reportError( Fixup.getLoc(), Twine("symbol '") + B->getName() + "' can not be undefined in a subtraction expression"); + return; + } - if (!A.getFragment()) - Asm.getContext().reportFatalError( + if (!A.getFragment()) { + Asm.getContext().reportError( Fixup.getLoc(), - Twine("symbol '") + Symbol.getName() + + Twine("symbol '") + A.getName() + "' can not be undefined in a subtraction expression"); + return; + } - CrossSection = &Symbol.getSection() != &B->getSection(); + CrossSection = &A.getSection() != &B->getSection(); // Offset of the symbol in the section int64_t OffsetOfB = Layout.getSymbolOffset(*B); @@ -765,12 +721,19 @@ void WinCOFFObjectWriter::recordRelocation( Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment); // Turn relocations for temporary symbols into section relocations. - if (coff_symbol->MC->isTemporary() || CrossSection) { - Reloc.Symb = coff_symbol->Section->Symbol; - FixedValue += Layout.getFragmentOffset(coff_symbol->MC->getFragment()) + - coff_symbol->MC->getOffset(); - } else - Reloc.Symb = coff_symbol; + if (A.isTemporary() || CrossSection) { + MCSection *TargetSection = &A.getSection(); + assert( + SectionMap.find(TargetSection) != SectionMap.end() && + "Section must already have been defined in executePostLayoutBinding!"); + Reloc.Symb = SectionMap[TargetSection]->Symbol; + FixedValue += Layout.getSymbolOffset(A); + } else { + assert( + SymbolMap.find(&A) != SymbolMap.end() && + "Symbol must already have been defined in executePostLayoutBinding!"); + Reloc.Symb = SymbolMap[&A]; + } ++Reloc.Symb->Relocations; @@ -884,14 +847,10 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, // Update section number & offset for symbols that have them. if (Symbol->Section) Symbol->Data.SectionNumber = Symbol->Section->Number; - if (Symbol->should_keep()) { - Symbol->setIndex(Header.NumberOfSymbols++); - // Update auxiliary symbol info. - Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size(); - Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols; - } else { - Symbol->setIndex(-1); - } + Symbol->setIndex(Header.NumberOfSymbols++); + // Update auxiliary symbol info. + Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size(); + Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols; } // Build string table. @@ -899,16 +858,15 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, if (S->Name.size() > COFF::NameSize) Strings.add(S->Name); for (const auto &S : Symbols) - if (S->should_keep() && S->Name.size() > COFF::NameSize) + if (S->Name.size() > COFF::NameSize) Strings.add(S->Name); - Strings.finalize(StringTableBuilder::WinCOFF); + Strings.finalize(); // Set names. for (const auto &S : Sections) SetSectionName(*S); for (auto &S : Symbols) - if (S->should_keep()) - SetSymbolName(*S); + SetSymbolName(*S); // Fixup weak external references. for (auto &Symbol : Symbols) { @@ -948,7 +906,7 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, // Assign file offsets to COFF object file structures. - unsigned offset = 0; + unsigned offset = getInitialOffset(); if (UseBigObj) offset += COFF::Header32Size; @@ -1011,8 +969,23 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, Header.PointerToSymbolTable = offset; + // FIXME: Remove the #else branch and make the #if branch unconditional once + // LLVM's self host configuration is aware of /Brepro. +#if (ENABLE_TIMESTAMPS == 1) + // MS LINK expects to be able to use this timestamp to implement their + // /INCREMENTAL feature. + if (Asm.isIncrementalLinkerCompatible()) { + std::time_t Now = time(nullptr); + if (Now < 0 || !isUInt<32>(Now)) + Now = UINT32_MAX; + Header.TimeDateStamp = Now; + } else { + Header.TimeDateStamp = 0; + } +#else // We want a deterministic output. It looks like GNU as also writes 0 in here. Header.TimeDateStamp = 0; +#endif // Write it all to disk... WriteFileHeader(Header); @@ -1029,6 +1002,7 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, } } + SmallVector<char, 128> SectionContents; for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(), je = Asm.end(); (i != ie) && (j != je); ++i, ++j) { @@ -1037,20 +1011,47 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, continue; if ((*i)->Header.PointerToRawData != 0) { - assert(OS.tell() <= (*i)->Header.PointerToRawData && + assert(getStream().tell() <= (*i)->Header.PointerToRawData && "Section::PointerToRawData is insane!"); - unsigned SectionDataPadding = (*i)->Header.PointerToRawData - OS.tell(); + unsigned SectionDataPadding = + (*i)->Header.PointerToRawData - getStream().tell(); assert(SectionDataPadding < 4 && "Should only need at most three bytes of padding!"); WriteZeros(SectionDataPadding); + // Save the contents of the section to a temporary buffer, we need this + // to CRC the data before we dump it into the object file. + SectionContents.clear(); + raw_svector_ostream VecOS(SectionContents); + raw_pwrite_stream &OldStream = getStream(); + // Redirect the output stream to our buffer. + setStream(VecOS); + // Fill our buffer with the section data. Asm.writeSectionData(&*j, Layout); + // Reset the stream back to what it was before. + setStream(OldStream); + + // Calculate our CRC with an initial value of '0', this is not how + // JamCRC is specified but it aligns with the expected output. + JamCRC JC(/*Init=*/0x00000000U); + JC.update(SectionContents); + + // Write the section contents to the object file. + getStream() << SectionContents; + + // Update the section definition auxiliary symbol to record the CRC. + COFFSection *Sec = SectionMap[&*j]; + COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux; + assert(AuxSyms.size() == 1 && + AuxSyms[0].AuxType == ATSectionDefinition); + AuxSymbol &SecDef = AuxSyms[0]; + SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC(); } if ((*i)->Relocations.size() > 0) { - assert(OS.tell() == (*i)->Header.PointerToRelocations && + assert(getStream().tell() == (*i)->Header.PointerToRelocations && "Section::PointerToRelocations is insane!"); if ((*i)->Relocations.size() >= 0xffff) { @@ -1071,14 +1072,14 @@ void WinCOFFObjectWriter::writeObject(MCAssembler &Asm, } } - assert(OS.tell() == Header.PointerToSymbolTable && + assert(getStream().tell() == Header.PointerToSymbolTable && "Header::PointerToSymbolTable is insane!"); for (auto &Symbol : Symbols) if (Symbol->getIndex() != -1) WriteSymbol(*Symbol); - OS.write(Strings.data().data(), Strings.data().size()); + getStream().write(Strings.data().data(), Strings.data().size()); } MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_) diff --git a/lib/MC/WinCOFFStreamer.cpp b/lib/MC/WinCOFFStreamer.cpp index 36dd691f07b8..a38b1a41a9b0 100644 --- a/lib/MC/WinCOFFStreamer.cpp +++ b/lib/MC/WinCOFFStreamer.cpp @@ -49,7 +49,6 @@ void MCWinCOFFStreamer::EmitInstToData(const MCInst &Inst, SmallString<256> Code; raw_svector_ostream VecOS(Code); getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI); - VecOS.flush(); // Add the fixups and data. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { @@ -123,29 +122,37 @@ void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) { "Got non-COFF section in the COFF backend!"); if (CurSymbol) - FatalError("starting a new symbol definition without completing the " - "previous one"); + Error("starting a new symbol definition without completing the " + "previous one"); CurSymbol = Symbol; } void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { - if (!CurSymbol) - FatalError("storage class specified outside of symbol definition"); + if (!CurSymbol) { + Error("storage class specified outside of symbol definition"); + return; + } - if (StorageClass & ~COFF::SSC_Invalid) - FatalError("storage class value '" + Twine(StorageClass) + + if (StorageClass & ~COFF::SSC_Invalid) { + Error("storage class value '" + Twine(StorageClass) + "' out of range"); + return; + } getAssembler().registerSymbol(*CurSymbol); cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass); } void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) { - if (!CurSymbol) - FatalError("symbol type specified outside of a symbol definition"); + if (!CurSymbol) { + Error("symbol type specified outside of a symbol definition"); + return; + } - if (Type & ~0xffff) - FatalError("type value '" + Twine(Type) + "' out of range"); + if (Type & ~0xffff) { + Error("type value '" + Twine(Type) + "' out of range"); + return; + } getAssembler().registerSymbol(*CurSymbol); cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type); @@ -153,7 +160,7 @@ void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) { void MCWinCOFFStreamer::EndCOFFSymbolDef() { if (!CurSymbol) - FatalError("ending symbol definition without starting one"); + Error("ending symbol definition without starting one"); CurSymbol = nullptr; } @@ -215,8 +222,6 @@ void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Size = std::max(Size, static_cast<uint64_t>(ByteAlignment)); } - AssignSection(Symbol, nullptr); - getAssembler().registerSymbol(*Symbol); Symbol->setExternal(true); Symbol->setCommon(Size, ByteAlignment); @@ -228,7 +233,6 @@ void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, OS << " -aligncomm:\"" << Symbol->getName() << "\"," << Log2_32_Ceil(ByteAlignment); - OS.flush(); PushSection(); SwitchSection(MFI->getDrectveSection()); @@ -249,8 +253,6 @@ void MCWinCOFFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, getAssembler().registerSymbol(*Symbol); Symbol->setExternal(false); - AssignSection(Symbol, Section); - if (ByteAlignment != 1) new MCAlignFragment(ByteAlignment, /*Value=*/0, /*ValueSize=*/0, ByteAlignment, Section); @@ -287,9 +289,8 @@ void MCWinCOFFStreamer::FinishImpl() { MCObjectStreamer::FinishImpl(); } -LLVM_ATTRIBUTE_NORETURN -void MCWinCOFFStreamer::FatalError(const Twine &Msg) const { - getContext().reportFatalError(SMLoc(), Msg); +void MCWinCOFFStreamer::Error(const Twine &Msg) const { + getContext().reportError(SMLoc(), Msg); } } |