diff options
Diffstat (limited to 'llvm/lib/MC')
24 files changed, 314 insertions, 206 deletions
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index ae8dffc..8f3814a 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -401,16 +401,15 @@ static bool isIFunc(const MCSymbolELF *Symbol) { mergeTypeForSet(Symbol->getType(), ELF::STT_GNU_IFUNC) != ELF::STT_GNU_IFUNC) return false; - Symbol = &cast<MCSymbolELF>(Value->getSymbol()); + Symbol = &static_cast<const MCSymbolELF &>(Value->getSymbol()); } return true; } void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, ELFSymbolData &MSD) { - const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol); - const MCSymbolELF *Base = - cast_or_null<MCSymbolELF>(Asm.getBaseSymbol(Symbol)); + auto &Symbol = static_cast<const MCSymbolELF &>(*MSD.Symbol); + auto *Base = static_cast<const MCSymbolELF *>(Asm.getBaseSymbol(Symbol)); // This has to be in sync with when computeSymbolTable uses SHN_ABS or // SHN_COMMON. @@ -446,7 +445,7 @@ void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, const MCSymbolELF *Sym = &Symbol; while (Sym->isVariable()) { if (auto *Expr = dyn_cast<MCSymbolRefExpr>(Sym->getVariableValue())) { - Sym = cast<MCSymbolELF>(&Expr->getSymbol()); + Sym = static_cast<const MCSymbolELF *>(&Expr->getSymbol()); if (!Sym->getSize()) continue; ESize = Sym->getSize(); @@ -523,7 +522,7 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) { // Add the data for the symbols. bool HasLargeSectionIndex = false; for (auto It : llvm::enumerate(Asm.symbols())) { - const auto &Symbol = cast<MCSymbolELF>(It.value()); + auto &Symbol = static_cast<const MCSymbolELF &>(It.value()); if (!isInSymtab(Symbol)) continue; @@ -533,7 +532,7 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) { } ELFSymbolData MSD; - MSD.Symbol = cast<MCSymbolELF>(&Symbol); + MSD.Symbol = static_cast<const MCSymbolELF *>(&Symbol); MSD.Order = It.index(); bool Local = Symbol.getBinding() == ELF::STB_LOCAL; @@ -1175,7 +1174,7 @@ void ELFObjectWriter::executePostLayoutBinding() { // versions declared with @@@ to be renamed. for (const Symver &S : Symvers) { StringRef AliasName = S.Name; - const auto &Symbol = cast<MCSymbolELF>(*S.Sym); + auto &Symbol = static_cast<const MCSymbolELF &>(*S.Sym); size_t Pos = AliasName.find('@'); assert(Pos != StringRef::npos); @@ -1185,8 +1184,8 @@ void ELFObjectWriter::executePostLayoutBinding() { if (Rest.starts_with("@@@")) Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1); - auto *Alias = - cast<MCSymbolELF>(Asm->getContext().getOrCreateSymbol(Prefix + Tail)); + auto *Alias = static_cast<MCSymbolELF *>( + Asm->getContext().getOrCreateSymbol(Prefix + Tail)); Asm->registerSymbol(*Alias); const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm->getContext()); Alias->setVariableValue(Value); @@ -1218,7 +1217,8 @@ void ELFObjectWriter::executePostLayoutBinding() { } for (const MCSymbol *&Sym : AddrsigSyms) { - if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym))) + if (const MCSymbol *R = + Renames.lookup(static_cast<const MCSymbolELF *>(Sym))) Sym = R; if (Sym->isInSection() && Sym->getName().starts_with(".L")) Sym = Sym->getSection().getBeginSymbol(); @@ -1234,7 +1234,7 @@ void ELFObjectWriter::executePostLayoutBinding() { continue; auto *Expr = Alias->getVariableValue(); if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) { - auto &Sym = cast<MCSymbolELF>(Inner->getSymbol()); + auto &Sym = static_cast<const MCSymbolELF &>(Inner->getSymbol()); if (Asm->registerSymbol(Sym)) Sym.setBinding(ELF::STB_WEAK); } @@ -1316,7 +1316,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F, auto &Section = static_cast<const MCSectionELF &>(*F.getParent()); MCContext &Ctx = getContext(); - const auto *SymA = cast_or_null<MCSymbolELF>(Target.getAddSym()); + auto *SymA = static_cast<const MCSymbolELF *>(Target.getAddSym()); const MCSectionELF *SecA = (SymA && SymA->isInSection()) ? static_cast<const MCSectionELF *>(&SymA->getSection()) @@ -1328,7 +1328,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F, uint64_t FixupOffset = Asm->getFragmentOffset(F) + Fixup.getOffset(); uint64_t Addend = Target.getConstant(); if (auto *RefB = Target.getSubSym()) { - const auto &SymB = cast<MCSymbolELF>(*RefB); + auto &SymB = static_cast<const MCSymbolELF &>(*RefB); if (SymB.isUndefined()) { Ctx.reportError(Fixup.getLoc(), Twine("symbol '") + SymB.getName() + @@ -1363,7 +1363,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F, !mc::isRelocRelocation(Fixup.getKind()); if (UseSectionSym && useSectionSymbol(Target, SymA, Addend, Type)) { Addend += Asm->getSymbolOffset(*SymA); - SymA = cast<MCSymbolELF>(SecA->getBeginSymbol()); + SymA = static_cast<const MCSymbolELF *>(SecA->getBeginSymbol()); } else if (const MCSymbolELF *R = Renames.lookup(SymA)) { SymA = R; } @@ -1383,7 +1383,7 @@ bool ELFObjectWriter::usesRela(const MCTargetOptions *TO, bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( const MCSymbol &SA, const MCFragment &FB, bool InSet, bool IsPCRel) const { - const auto &SymA = cast<MCSymbolELF>(SA); + auto &SymA = static_cast<const MCSymbolELF &>(SA); if (IsPCRel) { assert(!InSet); if (SymA.getBinding() != ELF::STB_LOCAL || diff --git a/llvm/lib/MC/GOFFObjectWriter.cpp b/llvm/lib/MC/GOFFObjectWriter.cpp index 88188f3..3b629cd 100644 --- a/llvm/lib/MC/GOFFObjectWriter.cpp +++ b/llvm/lib/MC/GOFFObjectWriter.cpp @@ -345,7 +345,7 @@ void GOFFWriter::defineSymbols() { for (const MCSymbol &Sym : Asm.symbols()) { if (Sym.isTemporary()) continue; - auto &Symbol = cast<MCSymbolGOFF>(Sym); + auto &Symbol = static_cast<const MCSymbolGOFF &>(Sym); if (Symbol.hasLDAttributes()) { Symbol.setIndex(++Ordinal); defineLabel(Symbol); diff --git a/llvm/lib/MC/MCAsmBackend.cpp b/llvm/lib/MC/MCAsmBackend.cpp index 828d9cf..55ec4a6 100644 --- a/llvm/lib/MC/MCAsmBackend.cpp +++ b/llvm/lib/MC/MCAsmBackend.cpp @@ -8,6 +8,7 @@ #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAssembler.h" +#include "llvm/MC/MCContext.h" #include "llvm/MC/MCDXContainerWriter.h" #include "llvm/MC/MCELFObjectWriter.h" #include "llvm/MC/MCGOFFObjectWriter.h" @@ -122,14 +123,12 @@ void MCAsmBackend::maybeAddReloc(const MCFragment &F, const MCFixup &Fixup, } bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const { + assert(getContext().isMachO()); // Consider a NULL personality (ie., no personality encoding) to be canonical // because it's always at 0. if (!Sym) return true; - if (!Sym->isMachO()) - llvm_unreachable("Expected MachO symbols only"); - StringRef name = Sym->getName(); // XXX: We intentionally leave out "___gcc_personality_v0" because, despite // being system-defined like these two, it is not very commonly-used. diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index da51da4..93614cd 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -897,14 +897,14 @@ void MCAsmStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, // Print symbol's rename (original name contains invalid character(s)) if // there is one. - MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(CsectSym); + auto *XSym = static_cast<MCSymbolXCOFF *>(CsectSym); if (XSym->hasRename()) emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName()); } void MCAsmStreamer::emitXCOFFSymbolLinkageWithVisibility( MCSymbol *Symbol, MCSymbolAttr Linkage, MCSymbolAttr Visibility) { - + auto &Sym = static_cast<MCSymbolXCOFF &>(*Symbol); switch (Linkage) { case MCSA_Global: OS << MAI->getGlobalDirective(); @@ -944,9 +944,8 @@ void MCAsmStreamer::emitXCOFFSymbolLinkageWithVisibility( // Print symbol's rename (original name contains invalid character(s)) if // there is one. - if (cast<MCSymbolXCOFF>(Symbol)->hasRename()) - emitXCOFFRenameDirective(Symbol, - cast<MCSymbolXCOFF>(Symbol)->getSymbolTableName()); + if (Sym.hasRename()) + emitXCOFFRenameDirective(&Sym, Sym.getSymbolTableName()); } void MCAsmStreamer::emitXCOFFRenameDirective(const MCSymbol *Name, @@ -1070,9 +1069,11 @@ void MCAsmStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, // Print symbol's rename (original name contains invalid character(s)) if // there is one. - MCSymbolXCOFF *XSym = dyn_cast<MCSymbolXCOFF>(Symbol); - if (XSym && XSym->hasRename()) - emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName()); + if (getContext().isXCOFF()) { + auto *XSym = static_cast<MCSymbolXCOFF *>(Symbol); + if (XSym && XSym->hasRename()) + emitXCOFFRenameDirective(XSym, XSym->getSymbolTableName()); + } } void MCAsmStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp index 8500fd1..d172ad1 100644 --- a/llvm/lib/MC/MCAssembler.cpp +++ b/llvm/lib/MC/MCAssembler.cpp @@ -59,7 +59,8 @@ STATISTIC(EmittedFillFragments, "Number of emitted assembler fragments - fill"); STATISTIC(EmittedNopsFragments, "Number of emitted assembler fragments - nops"); STATISTIC(EmittedOrgFragments, "Number of emitted assembler fragments - org"); -STATISTIC(evaluateFixup, "Number of evaluated fixups"); +STATISTIC(Fixups, "Number of fixups"); +STATISTIC(FixupEvalForRelax, "Number of fixup evaluations for relaxation"); STATISTIC(ObjectBytes, "Number of emitted object file bytes"); STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); @@ -140,9 +141,9 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target, uint64_t &Value, - bool RecordReloc, - MutableArrayRef<char> Contents) const { - ++stats::evaluateFixup; + bool RecordReloc, uint8_t *Data) const { + if (RecordReloc) + ++stats::Fixups; // FIXME: This code has some duplication with recordRelocation. We should // probably merge the two into a single callback that tries to evaluate a @@ -185,7 +186,7 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup, if (IsResolved && mc::isRelocRelocation(Fixup.getKind())) IsResolved = false; - getBackend().applyFixup(F, Fixup, Target, Contents, Value, IsResolved); + getBackend().applyFixup(F, Fixup, Target, Data, Value, IsResolved); return true; } @@ -703,21 +704,25 @@ void MCAssembler::layout() { for (MCFixup &Fixup : F.getFixups()) { uint64_t FixedValue; MCValue Target; + assert(mc::isRelocRelocation(Fixup.getKind()) || + Fixup.getOffset() <= F.getFixedSize()); + auto *Data = + reinterpret_cast<uint8_t *>(Contents.data() + Fixup.getOffset()); evaluateFixup(F, Fixup, Target, FixedValue, - /*RecordReloc=*/true, Contents); + /*RecordReloc=*/true, Data); } - if (F.getVarFixups().size()) { - // In the variable part, fixup offsets are relative to the fixed part's - // start. Extend the variable contents to the left to account for the - // fixed part size. - Contents = MutableArrayRef(F.getParent()->ContentStorage) - .slice(F.VarContentStart - Contents.size(), F.getSize()); - for (MCFixup &Fixup : F.getVarFixups()) { - uint64_t FixedValue; - MCValue Target; - evaluateFixup(F, Fixup, Target, FixedValue, - /*RecordReloc=*/true, Contents); - } + // In the variable part, fixup offsets are relative to the fixed part's + // start. + for (MCFixup &Fixup : F.getVarFixups()) { + uint64_t FixedValue; + MCValue Target; + assert(mc::isRelocRelocation(Fixup.getKind()) || + (Fixup.getOffset() >= F.getFixedSize() && + Fixup.getOffset() <= F.getSize())); + auto *Data = reinterpret_cast<uint8_t *>( + F.getVarContents().data() + (Fixup.getOffset() - F.getFixedSize())); + evaluateFixup(F, Fixup, Target, FixedValue, + /*RecordReloc=*/true, Data); } } } @@ -735,7 +740,7 @@ void MCAssembler::Finish() { bool MCAssembler::fixupNeedsRelaxation(const MCFragment &F, const MCFixup &Fixup) const { - assert(getBackendPtr() && "Expected assembler backend"); + ++stats::FixupEvalForRelax; MCValue Target; uint64_t Value; bool Resolved = evaluateFixup(F, const_cast<MCFixup &>(Fixup), Target, Value, @@ -940,6 +945,14 @@ bool MCAssembler::relaxFill(MCFillFragment &F) { return true; } +bool MCAssembler::relaxOrg(MCOrgFragment &F) { + uint64_t Size = computeFragmentSize(F); + if (F.getSize() == Size) + return false; + F.setSize(Size); + return true; +} + bool MCAssembler::relaxFragment(MCFragment &F) { switch(F.getKind()) { default: @@ -961,6 +974,8 @@ bool MCAssembler::relaxFragment(MCFragment &F) { return relaxCVDefRange(cast<MCCVDefRangeFragment>(F)); case MCFragment::FT_Fill: return relaxFill(cast<MCFillFragment>(F)); + case MCFragment::FT_Org: + return relaxOrg(static_cast<MCOrgFragment &>(F)); } } diff --git a/llvm/lib/MC/MCCodeView.cpp b/llvm/lib/MC/MCCodeView.cpp index 7d528a5..335934a7 100644 --- a/llvm/lib/MC/MCCodeView.cpp +++ b/llvm/lib/MC/MCCodeView.cpp @@ -436,12 +436,11 @@ void CodeViewContext::emitInlineLineTableForFunction(MCObjectStreamer &OS, const MCSymbol *FnEndSym) { // Create and insert a fragment into the current section that will be encoded // later. - auto *F = MCCtx->allocFragment<MCCVInlineLineTableFragment>( + OS.newSpecialFragment<MCCVInlineLineTableFragment>( PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym); - OS.insert(F); } -MCFragment *CodeViewContext::emitDefRange( +void CodeViewContext::emitDefRange( MCObjectStreamer &OS, ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges, StringRef FixedSizePortion) { @@ -451,9 +450,7 @@ MCFragment *CodeViewContext::emitDefRange( auto &Saved = DefRangeStorage.emplace_back(Ranges.begin(), Ranges.end()); // Create and insert a fragment into the current section that will be encoded // later. - auto *F = MCCtx->allocFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion); - OS.insert(F); - return F; + OS.newSpecialFragment<MCCVDefRangeFragment>(Saved, FixedSizePortion); } static unsigned computeLabelDiff(const MCAssembler &Asm, const MCSymbol *Begin, @@ -695,5 +692,7 @@ void CodeViewContext::encodeDefRange(const MCAssembler &Asm, } Frag.setVarContents(Contents); + assert(Fixups.size() < 256 && "Store fixups outside of MCFragment's VarFixup " + "storage if the number ever exceeds 256"); Frag.setVarFixups(Fixups); } diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index 39bf628..5e364e9 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -153,15 +153,12 @@ void MCContext::reset() { SPIRVAllocator.DestroyAll(); WasmSignatureAllocator.DestroyAll(); - // ~CodeViewContext may destroy a MCFragment outside of sections and need to - // be reset before FragmentAllocator. CVContext.reset(); MCSubtargetAllocator.DestroyAll(); InlineAsmUsedLabelNames.clear(); Symbols.clear(); Allocator.Reset(); - FragmentAllocator.Reset(); Instances.clear(); CompilationDir.clear(); MainFileName.clear(); @@ -297,11 +294,9 @@ MCSymbol *MCContext::createSymbolImpl(const MCSymbolTableEntry *Name, case MCContext::IsDXContainer: break; case MCContext::IsSPIRV: - return new (Name, *this) - MCSymbol(MCSymbol::SymbolKindUnset, Name, IsTemporary); + return new (Name, *this) MCSymbol(Name, IsTemporary); } - return new (Name, *this) - MCSymbol(MCSymbol::SymbolKindUnset, Name, IsTemporary); + return new (Name, *this) MCSymbol(Name, IsTemporary); } MCSymbol *MCContext::cloneSymbol(MCSymbol &Sym) { @@ -309,13 +304,16 @@ MCSymbol *MCContext::cloneSymbol(MCSymbol &Sym) { auto Name = Sym.getNameEntryPtr(); switch (getObjectFileType()) { case MCContext::IsCOFF: - NewSym = new (Name, *this) MCSymbolCOFF(cast<MCSymbolCOFF>(Sym)); + NewSym = + new (Name, *this) MCSymbolCOFF(static_cast<const MCSymbolCOFF &>(Sym)); break; case MCContext::IsELF: - NewSym = new (Name, *this) MCSymbolELF(cast<MCSymbolELF>(Sym)); + NewSym = + new (Name, *this) MCSymbolELF(static_cast<const MCSymbolELF &>(Sym)); break; case MCContext::IsMachO: - NewSym = new (Name, *this) MCSymbolMachO(cast<MCSymbolMachO>(Sym)); + NewSym = new (Name, *this) + MCSymbolMachO(static_cast<const MCSymbolMachO &>(Sym)); break; default: reportFatalUsageError(".set redefinition is not supported"); @@ -446,7 +444,7 @@ Symbol *MCContext::getOrCreateSectionSymbol(StringRef Section) { // Use the symbol's index to track if it has been used as a section symbol. // Set to -1 to catch potential bugs if misused as a symbol index. if (Sym && Sym->getIndex() != -1u) { - R = cast<Symbol>(Sym); + R = static_cast<Symbol *>(Sym); } else { SymEntry.second.Used = true; R = new (&SymEntry, *this) Symbol(&SymEntry, /*isTemporary=*/false); @@ -586,7 +584,7 @@ MCContext::createELFRelSection(const Twine &Name, unsigned Type, unsigned Flags, return createELFSectionImpl( I->getKey(), Type, Flags, EntrySize, Group, true, true, - cast<MCSymbolELF>(RelInfoSection->getBeginSymbol())); + static_cast<const MCSymbolELF *>(RelInfoSection->getBeginSymbol())); } MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix, @@ -604,7 +602,7 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, const MCSymbolELF *LinkedToSym) { MCSymbolELF *GroupSym = nullptr; if (!Group.isTriviallyEmpty() && !Group.str().empty()) - GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group)); + GroupSym = static_cast<MCSymbolELF *>(getOrCreateSymbol(Group)); return getELFSection(Section, Type, Flags, EntrySize, GroupSym, IsComdat, UniqueID, LinkedToSym); @@ -817,7 +815,7 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K, unsigned UniqueID) { MCSymbolWasm *GroupSym = nullptr; if (!Group.isTriviallyEmpty() && !Group.str().empty()) { - GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group)); + GroupSym = static_cast<MCSymbolWasm *>(getOrCreateSymbol(Group)); GroupSym->setComdat(true); if (K.isMetadata() && !GroupSym->getType().has_value()) { // Comdat group symbol associated with a custom section is a section @@ -848,7 +846,7 @@ MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind, MCSymbol *Begin = createRenamableSymbol(CachedName, true, false); // Begin always has a different name than CachedName... see #48596. getSymbolTableEntry(Begin->getName()).second.Symbol = Begin; - cast<MCSymbolWasm>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION); + static_cast<MCSymbolWasm *>(Begin)->setType(wasm::WASM_SYMBOL_TYPE_SECTION); MCSectionWasm *Result = new (WasmAllocator.Allocate()) MCSectionWasm(CachedName, Kind, Flags, GroupSym, UniqueID, Begin); @@ -889,9 +887,9 @@ MCSectionXCOFF *MCContext::getXCOFFSection( MCSymbolXCOFF *QualName = nullptr; // Debug section don't have storage class attribute. if (IsDwarfSec) - QualName = cast<MCSymbolXCOFF>(getOrCreateSymbol(CachedName)); + QualName = static_cast<MCSymbolXCOFF *>(getOrCreateSymbol(CachedName)); else - QualName = cast<MCSymbolXCOFF>(getOrCreateSymbol( + QualName = static_cast<MCSymbolXCOFF *>(getOrCreateSymbol( CachedName + "[" + XCOFF::getMappingClassString(CsectProp->MappingClass) + "]")); diff --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp index 38744a0..275e76e 100644 --- a/llvm/lib/MC/MCELFStreamer.cpp +++ b/llvm/lib/MC/MCELFStreamer.cpp @@ -59,7 +59,7 @@ void MCELFStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) { } void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { - auto *Symbol = cast<MCSymbolELF>(S); + auto *Symbol = static_cast<MCSymbolELF *>(S); MCObjectStreamer::emitLabel(Symbol, Loc); const MCSectionELF &Section = @@ -70,7 +70,7 @@ void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { void MCELFStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCFragment &F, uint64_t Offset) { - auto *Symbol = cast<MCSymbolELF>(S); + auto *Symbol = static_cast<MCSymbolELF *>(S); MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset); const MCSectionELF &Section = @@ -95,7 +95,7 @@ void MCELFStreamer::changeSection(MCSection *Section, uint32_t Subsection) { } void MCELFStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) { - auto *A = cast<MCSymbolELF>(Alias); + auto *A = static_cast<MCSymbolELF *>(Alias); if (A->isDefined()) { getContext().reportError(getStartTokLoc(), "symbol '" + A->getName() + "' is already defined"); @@ -126,7 +126,7 @@ static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) { } bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { - auto *Symbol = cast<MCSymbolELF>(S); + auto *Symbol = static_cast<MCSymbolELF *>(S); // Adding a symbol attribute always introduces the symbol, note that an // important side effect of calling registerSymbol here is to register @@ -247,7 +247,7 @@ bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { void MCELFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, Align ByteAlignment) { - auto *Symbol = cast<MCSymbolELF>(S); + auto *Symbol = static_cast<MCSymbolELF *>(S); getAssembler().registerSymbol(*Symbol); if (!Symbol->isBindingSet()) @@ -272,12 +272,12 @@ void MCELFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, " redeclared as different type"); } - cast<MCSymbolELF>(Symbol) - ->setSize(MCConstantExpr::create(Size, getContext())); + static_cast<MCSymbolELF *>(Symbol)->setSize( + MCConstantExpr::create(Size, getContext())); } void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) { - cast<MCSymbolELF>(Symbol)->setSize(Value); + static_cast<MCSymbolELF *>(Symbol)->setSize(Value); } void MCELFStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym, @@ -289,7 +289,7 @@ void MCELFStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym, void MCELFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size, Align ByteAlignment) { - auto *Symbol = cast<MCSymbolELF>(S); + auto *Symbol = static_cast<MCSymbolELF *>(S); // FIXME: Should this be caught and done earlier? getAssembler().registerSymbol(*Symbol); Symbol->setBinding(ELF::STB_LOCAL); diff --git a/llvm/lib/MC/MCMachOStreamer.cpp b/llvm/lib/MC/MCMachOStreamer.cpp index a214513..6226b02 100644 --- a/llvm/lib/MC/MCMachOStreamer.cpp +++ b/llvm/lib/MC/MCMachOStreamer.cpp @@ -147,7 +147,7 @@ void MCMachOStreamer::changeSection(MCSection *Section, uint32_t Subsection) { void MCMachOStreamer::emitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) { - auto *Sym = cast<MCSymbolMachO>(Symbol); + auto *Sym = static_cast<const MCSymbolMachO *>(Symbol); getAssembler().registerSymbol(*Symbol); if (Symbol->isExternal()) emitSymbolAttribute(EHSymbol, MCSA_Global); @@ -160,7 +160,7 @@ void MCMachOStreamer::emitEHSymAttributes(const MCSymbol *Symbol, void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { // We have to create a new fragment if this is an atom defining symbol, // fragments cannot span atoms. - if (cast<MCSymbolMachO>(Symbol)->isSymbolLinkerVisible()) + if (static_cast<MCSymbolMachO *>(Symbol)->isSymbolLinkerVisible()) newFragment(); MCObjectStreamer::emitLabel(Symbol, Loc); @@ -172,7 +172,7 @@ void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { // // FIXME: Cleanup this code, these bits should be emitted based on semantic // properties, not on the order of definition, etc. - cast<MCSymbolMachO>(Symbol)->clearReferenceType(); + static_cast<MCSymbolMachO *>(Symbol)->clearReferenceType(); } void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { @@ -182,7 +182,7 @@ void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { if (const auto *SymA = Res.getAddSym()) { if (!Res.getSubSym() && (SymA->getName().empty() || Res.getConstant() != 0)) - cast<MCSymbolMachO>(Symbol)->setAltEntry(); + static_cast<MCSymbolMachO *>(Symbol)->setAltEntry(); } } MCObjectStreamer::emitAssignment(Symbol, Value); @@ -256,7 +256,7 @@ void MCMachOStreamer::emitDarwinTargetVariantBuildVersion( bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym, MCSymbolAttr Attribute) { - MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym); + auto *Symbol = static_cast<MCSymbolMachO *>(Sym); // Indirect symbols are handled differently, to match how 'as' handles // them. This makes writing matching .o files easier. @@ -367,7 +367,7 @@ bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym, void MCMachOStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { // Encode the 'desc' value into the lowest implementation defined bits. getAssembler().registerSymbol(*Symbol); - cast<MCSymbolMachO>(Symbol)->setDesc(DescValue); + static_cast<MCSymbolMachO *>(Symbol)->setDesc(DescValue); } void MCMachOStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, @@ -430,7 +430,7 @@ void MCMachOStreamer::finishImpl() { // defining symbols. DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap; for (const MCSymbol &Symbol : getAssembler().symbols()) { - auto &Sym = cast<MCSymbolMachO>(Symbol); + auto &Sym = static_cast<const MCSymbolMachO &>(Symbol); if (Sym.isSymbolLinkerVisible() && Sym.isInSection() && !Sym.isVariable() && !Sym.isAltEntry()) { // An atom defining symbol should never be internal to a fragment. diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp index 393eed1..4ac73ab 100644 --- a/llvm/lib/MC/MCObjectFileInfo.cpp +++ b/llvm/lib/MC/MCObjectFileInfo.cpp @@ -1135,9 +1135,10 @@ MCObjectFileInfo::getCallGraphSection(const MCSection &TextSec) const { Flags |= ELF::SHF_GROUP; } - return Ctx->getELFSection(".callgraph", ELF::SHT_PROGBITS, Flags, 0, - GroupName, true, ElfSec.getUniqueID(), - cast<MCSymbolELF>(TextSec.getBeginSymbol())); + return Ctx->getELFSection( + ".callgraph", ELF::SHT_PROGBITS, Flags, 0, GroupName, true, + ElfSec.getUniqueID(), + static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol())); } MCSection * @@ -1154,9 +1155,10 @@ MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const { Flags |= ELF::SHF_GROUP; } - return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0, - GroupName, true, ElfSec.getUniqueID(), - cast<MCSymbolELF>(TextSec.getBeginSymbol())); + return Ctx->getELFSection( + ".stack_sizes", ELF::SHT_PROGBITS, Flags, 0, GroupName, true, + ElfSec.getUniqueID(), + static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol())); } MCSection * @@ -1174,9 +1176,10 @@ MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const { // Use the text section's begin symbol and unique ID to create a separate // .llvm_bb_addr_map section associated with every unique text section. - return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP, - Flags, 0, GroupName, true, ElfSec.getUniqueID(), - cast<MCSymbolELF>(TextSec.getBeginSymbol())); + return Ctx->getELFSection( + ".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP, Flags, 0, GroupName, true, + ElfSec.getUniqueID(), + static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol())); } MCSection * @@ -1192,10 +1195,10 @@ MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const { Flags |= ELF::SHF_GROUP; } - return Ctx->getELFSection(".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0, - GroupName, - /*IsComdat=*/true, ElfSec.getUniqueID(), - cast<MCSymbolELF>(TextSec.getBeginSymbol())); + return Ctx->getELFSection( + ".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0, GroupName, + /*IsComdat=*/true, ElfSec.getUniqueID(), + static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol())); } MCSection * @@ -1211,9 +1214,10 @@ MCObjectFileInfo::getPseudoProbeSection(const MCSection &TextSec) const { Flags |= ELF::SHF_GROUP; } - return Ctx->getELFSection(PseudoProbeSection->getName(), ELF::SHT_PROGBITS, - Flags, 0, GroupName, true, ElfSec.getUniqueID(), - cast<MCSymbolELF>(TextSec.getBeginSymbol())); + return Ctx->getELFSection( + PseudoProbeSection->getName(), ELF::SHT_PROGBITS, Flags, 0, GroupName, + true, ElfSec.getUniqueID(), + static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol())); } MCSection * @@ -1261,7 +1265,7 @@ MCSection *MCObjectFileInfo::getPCSection(StringRef Name, GroupName = Group->getName(); Flags |= ELF::SHF_GROUP; } - return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true, - ElfSec.getUniqueID(), - cast<MCSymbolELF>(TextSec->getBeginSymbol())); + return Ctx->getELFSection( + Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true, ElfSec.getUniqueID(), + static_cast<const MCSymbolELF *>(TextSec->getBeginSymbol())); } diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index e277143..8c27958 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -46,27 +46,83 @@ MCAssembler *MCObjectStreamer::getAssemblerPtr() { return nullptr; } +constexpr size_t FragBlockSize = 16384; +// Ensure the new fragment can at least store a few bytes. +constexpr size_t NewFragHeadroom = 8; + +static_assert(NewFragHeadroom >= alignof(MCFragment)); +static_assert(FragBlockSize >= sizeof(MCFragment) + NewFragHeadroom); + +MCFragment *MCObjectStreamer::allocFragSpace(size_t Headroom) { + auto Size = std::max(FragBlockSize, sizeof(MCFragment) + Headroom); + FragSpace = Size - sizeof(MCFragment); + auto Block = std::unique_ptr<uint8_t[]>(new uint8_t[Size]); + auto *F = reinterpret_cast<MCFragment *>(Block.get()); + FragStorage.push_back(std::move(Block)); + return F; +} + void MCObjectStreamer::newFragment() { - addFragment(getContext().allocFragment<MCFragment>()); + MCFragment *F; + if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) { + auto End = reinterpret_cast<size_t>(getCurFragEnd()); + F = reinterpret_cast<MCFragment *>( + alignToPowerOf2(End, alignof(MCFragment))); + FragSpace -= size_t(F) - End + sizeof(MCFragment); + } else { + F = allocFragSpace(0); + } + new (F) MCFragment(); + addFragment(F); } -void MCObjectStreamer::insert(MCFragment *F) { - assert(F->getKind() != MCFragment::FT_Data && - "F should have a variable-size tail"); +void MCObjectStreamer::ensureHeadroom(size_t Headroom) { + if (Headroom <= FragSpace) + return; + auto *F = allocFragSpace(Headroom); + new (F) MCFragment(); + addFragment(F); +} + +void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) { + assert(Frag->getKind() != MCFragment::FT_Data && + "Frag should have a variable-size tail"); + // Frag is not connected to FragSpace. Before modifying CurFrag with + // addFragment(Frag), allocate an empty fragment to maintain FragSpace + // connectivity, potentially reusing CurFrag's associated space. + MCFragment *F; + if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) { + auto End = reinterpret_cast<size_t>(getCurFragEnd()); + F = reinterpret_cast<MCFragment *>( + alignToPowerOf2(End, alignof(MCFragment))); + FragSpace -= size_t(F) - End + sizeof(MCFragment); + } else { + F = allocFragSpace(0); + } + new (F) MCFragment(); + + addFragment(Frag); addFragment(F); - newFragment(); } void MCObjectStreamer::appendContents(ArrayRef<char> Contents) { - CurFrag->appendContents(Contents); + ensureHeadroom(Contents.size()); + assert(FragSpace >= Contents.size()); + llvm::copy(Contents, getCurFragEnd()); + CurFrag->FixedSize += Contents.size(); + FragSpace -= Contents.size(); } -void MCObjectStreamer::appendContents(size_t Num, char Elt) { - CurFrag->appendContents(Num, Elt); +void MCObjectStreamer::appendContents(size_t Num, uint8_t Elt) { + ensureHeadroom(Num); + MutableArrayRef<uint8_t> Data(getCurFragEnd(), Num); + llvm::fill(Data, Elt); + CurFrag->FixedSize += Num; + FragSpace -= Num; } void MCObjectStreamer::addFixup(const MCExpr *Value, MCFixupKind Kind) { - CurFrag->addFixup(MCFixup::create(CurFrag->getFixedSize(), Value, Kind)); + CurFrag->addFixup(MCFixup::create(getCurFragSize(), Value, Kind)); } // As a compile-time optimization, avoid allocating and evaluating an MCExpr @@ -115,6 +171,9 @@ void MCObjectStreamer::reset() { } EmitEHFrame = true; EmitDebugFrame = false; + FragStorage.clear(); + FragSpace = 0; + SpecialFragAllocator.Reset(); MCStreamer::reset(); } @@ -143,7 +202,6 @@ void MCObjectStreamer::emitCFISections(bool EH, bool Debug, bool SFrame) { void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) { MCStreamer::emitValueImpl(Value, Size, Loc); - MCFragment *DF = getCurrentFragment(); MCDwarfLineEntry::make(this, getCurrentSectionOnly()); @@ -158,9 +216,9 @@ void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, emitIntValue(AbsValue, Size); return; } - DF->addFixup(MCFixup::create(DF->getContents().size(), Value, - MCFixup::getDataKindForSize(Size))); - DF->appendContents(Size, 0); + ensureHeadroom(Size); + addFixup(Value, MCFixup::getDataKindForSize(Size)); + appendContents(Size, 0); } MCSymbol *MCObjectStreamer::emitCFILabel() { @@ -194,7 +252,7 @@ void MCObjectStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { // section. MCFragment *F = CurFrag; Symbol->setFragment(F); - Symbol->setOffset(F->getContents().size()); + Symbol->setOffset(F->getFixedSize()); emitPendingAssignments(Symbol); } @@ -260,6 +318,21 @@ void MCObjectStreamer::changeSection(MCSection *Section, uint32_t Subsection) { F0 = CurFrag; } + // To maintain connectivity between CurFrag and FragSpace when CurFrag is + // modified, allocate an empty fragment and append it to the fragment list. + // (Subsections[I].second.Tail is not connected to FragSpace.) + MCFragment *F; + if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) { + auto End = reinterpret_cast<size_t>(getCurFragEnd()); + F = reinterpret_cast<MCFragment *>( + alignToPowerOf2(End, alignof(MCFragment))); + FragSpace -= size_t(F) - End + sizeof(MCFragment); + } else { + F = allocFragSpace(0); + } + new (F) MCFragment(); + F->setParent(Section); + auto &Subsections = Section->Subsections; size_t I = 0, E = Subsections.size(); while (I != E && Subsections[I].first < Subsection) @@ -267,13 +340,16 @@ void MCObjectStreamer::changeSection(MCSection *Section, uint32_t Subsection) { // If the subsection number is not in the sorted Subsections list, create a // new fragment list. if (I == E || Subsections[I].first != Subsection) { - auto *F = getContext().allocFragment<MCFragment>(); - F->setParent(Section); Subsections.insert(Subsections.begin() + I, {Subsection, MCSection::FragList{F, F}}); + Section->CurFragList = &Subsections[I].second; + CurFrag = F; + } else { + Section->CurFragList = &Subsections[I].second; + CurFrag = Subsections[I].second.Tail; + // Ensure CurFrag is associated with FragSpace. + addFragment(F); } - Section->CurFragList = &Subsections[I].second; - CurFrag = Section->CurFragList->Tail; // Define the section symbol at subsection 0's initial fragment if required. if (!NewSec) @@ -344,11 +420,15 @@ void MCObjectStreamer::emitInstToData(const MCInst &Inst, MCFragment *F = getCurrentFragment(); // Append the instruction to the data fragment. - size_t CodeOffset = F->getContents().size(); + size_t CodeOffset = getCurFragSize(); + SmallString<16> Content; SmallVector<MCFixup, 1> Fixups; - getAssembler().getEmitter().encodeInstruction( - Inst, F->getContentsForAppending(), Fixups, STI); - F->doneAppending(); + getAssembler().getEmitter().encodeInstruction(Inst, Content, Fixups, STI); + appendContents(Content); + if (CurFrag != F) { + F = CurFrag; + CodeOffset = 0; + } F->setHasInstructions(STI); if (Fixups.empty()) @@ -570,7 +650,7 @@ void MCObjectStreamer::emitCodeAlignment(Align Alignment, void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value, SMLoc Loc) { - insert(getContext().allocFragment<MCOrgFragment>(*Offset, Value, Loc)); + newSpecialFragment<MCOrgFragment>(*Offset, Value, Loc); } void MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name, @@ -602,8 +682,7 @@ void MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name, void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc) { assert(getCurrentSectionOnly() && "need a section"); - insert( - getContext().allocFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc)); + newSpecialFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc); } void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size, @@ -630,15 +709,13 @@ void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size, // Otherwise emit as fragment. assert(getCurrentSectionOnly() && "need a section"); - insert( - getContext().allocFragment<MCFillFragment>(Expr, Size, NumValues, Loc)); + newSpecialFragment<MCFillFragment>(Expr, Size, NumValues, Loc); } void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc, const MCSubtargetInfo &STI) { assert(getCurrentSectionOnly() && "need a section"); - insert(getContext().allocFragment<MCNopsFragment>( - NumBytes, ControlledNopLength, Loc, STI)); + newSpecialFragment<MCNopsFragment>(NumBytes, ControlledNopLength, Loc, STI); } void MCObjectStreamer::emitFileDirective(StringRef Filename) { diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 9f64a98..7782dc1 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -1865,7 +1865,7 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info, } if (MAI.hasSubsectionsViaSymbols() && CFIStartProcLoc && - Sym->isExternal() && !cast<MCSymbolMachO>(Sym)->isAltEntry()) + Sym->isExternal() && !static_cast<MCSymbolMachO *>(Sym)->isAltEntry()) return Error(StartTokLoc, "non-private labels cannot appear between " ".cfi_startproc / .cfi_endproc pairs") && Error(*CFIStartProcLoc, "previous .cfi_startproc was here"); @@ -6273,7 +6273,8 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef, // used as a symbol, or it is an absolute symbol). Sym = Parser.getContext().lookupSymbol(Name); if (Sym) { - if (!Sym->isUnset() && (!allow_redef || !Sym->isRedefinable())) + if ((Sym->isVariable() || Sym->isDefined()) && + (!allow_redef || !Sym->isRedefinable())) return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); // If the symbol is redefinable, clone it and update the symbol table // to the new symbol. Existing references to the original symbol remain diff --git a/llvm/lib/MC/MCParser/COFFMasmParser.cpp b/llvm/lib/MC/MCParser/COFFMasmParser.cpp index 282f22f..229b0b8 100644 --- a/llvm/lib/MC/MCParser/COFFMasmParser.cpp +++ b/llvm/lib/MC/MCParser/COFFMasmParser.cpp @@ -460,7 +460,8 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) { nextLoc = getTok().getLoc(); } } - MCSymbolCOFF *Sym = cast<MCSymbolCOFF>(getContext().getOrCreateSymbol(Label)); + auto *Sym = + static_cast<MCSymbolCOFF *>(getContext().getOrCreateSymbol(Label)); // Define symbol as simple external function Sym->setExternal(true); diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp index 2e251cc..6782c4b 100644 --- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp +++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp @@ -200,7 +200,7 @@ bool ELFAsmParser::parseDirectiveSize(StringRef, SMLoc) { StringRef Name; if (getParser().parseIdentifier(Name)) return TokError("expected identifier"); - MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name)); + auto *Sym = static_cast<MCSymbolELF *>(getContext().getOrCreateSymbol(Name)); if (getLexer().isNot(AsmToken::Comma)) return TokError("expected comma"); @@ -466,7 +466,7 @@ bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) { } return TokError("invalid linked-to symbol"); } - LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name)); + LinkedToSym = static_cast<MCSymbolELF *>(getContext().lookupSymbol(Name)); if (!LinkedToSym || !LinkedToSym->isInSection()) return Error(StartLoc, "linked-to symbol is not in a section: " + Name); return false; diff --git a/llvm/lib/MC/MCParser/WasmAsmParser.cpp b/llvm/lib/MC/MCParser/WasmAsmParser.cpp index d97f4f5..6c2d241 100644 --- a/llvm/lib/MC/MCParser/WasmAsmParser.cpp +++ b/llvm/lib/MC/MCParser/WasmAsmParser.cpp @@ -224,7 +224,7 @@ public: return true; if (expect(AsmToken::EndOfStatement, "eol")) return true; - auto WasmSym = cast<MCSymbolWasm>(Sym); + auto WasmSym = static_cast<const MCSymbolWasm *>(Sym); if (WasmSym->isFunction()) { // Ignore .size directives for function symbols. They get their size // set automatically based on their content. @@ -241,9 +241,9 @@ public: if (!Lexer->is(AsmToken::Identifier)) return error("Expected label after .type directive, got: ", Lexer->getTok()); - auto WasmSym = cast<MCSymbolWasm>( - getStreamer().getContext().getOrCreateSymbol( - Lexer->getTok().getString())); + auto *WasmSym = static_cast<MCSymbolWasm *>( + getStreamer().getContext().getOrCreateSymbol( + Lexer->getTok().getString())); Lex(); if (!(isNext(AsmToken::Comma) && isNext(AsmToken::At) && Lexer->is(AsmToken::Identifier))) diff --git a/llvm/lib/MC/MCSection.cpp b/llvm/lib/MC/MCSection.cpp index 4f28267..27ca131 100644 --- a/llvm/lib/MC/MCSection.cpp +++ b/llvm/lib/MC/MCSection.cpp @@ -83,12 +83,14 @@ void MCFragment::appendFixups(ArrayRef<MCFixup> Fixups) { } void MCFragment::setVarFixups(ArrayRef<MCFixup> Fixups) { + assert(Fixups.size() < 256 && + "variable-size tail cannot have more than 256 fixups"); auto &S = getParent()->FixupStorage; - if (VarFixupStart + Fixups.size() > VarFixupEnd) { + if (Fixups.size() > VarFixupSize) { VarFixupStart = S.size(); S.resize_for_overwrite(S.size() + Fixups.size()); } - VarFixupEnd = VarFixupStart + Fixups.size(); + VarFixupSize = Fixups.size(); // Source fixup offsets are relative to the variable part's start. Add the // fixed part size to make them relative to the fixed part's start. std::transform(Fixups.begin(), Fixups.end(), S.begin() + VarFixupStart, diff --git a/llvm/lib/MC/MCWasmStreamer.cpp b/llvm/lib/MC/MCWasmStreamer.cpp index e3ef111..9c8b224 100644 --- a/llvm/lib/MC/MCWasmStreamer.cpp +++ b/llvm/lib/MC/MCWasmStreamer.cpp @@ -36,7 +36,7 @@ using namespace llvm; MCWasmStreamer::~MCWasmStreamer() = default; // anchor. void MCWasmStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { - auto *Symbol = cast<MCSymbolWasm>(S); + auto *Symbol = static_cast<MCSymbolWasm *>(S); MCObjectStreamer::emitLabel(Symbol, Loc); const MCSectionWasm &Section = @@ -47,7 +47,7 @@ void MCWasmStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { void MCWasmStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCFragment &F, uint64_t Offset) { - auto *Symbol = cast<MCSymbolWasm>(S); + auto *Symbol = static_cast<MCSymbolWasm *>(S); MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset); const MCSectionWasm &Section = @@ -69,8 +69,7 @@ void MCWasmStreamer::changeSection(MCSection *Section, uint32_t Subsection) { bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported"); - - auto *Symbol = cast<MCSymbolWasm>(S); + auto *Symbol = static_cast<MCSymbolWasm *>(S); // Adding a symbol attribute always introduces the symbol; note that an // important side effect of calling registerSymbol here is to register the @@ -135,7 +134,7 @@ void MCWasmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, } void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) { - cast<MCSymbolWasm>(Symbol)->setSize(Value); + static_cast<MCSymbolWasm *>(Symbol)->setSize(Value); } void MCWasmStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size, diff --git a/llvm/lib/MC/MCWin64EH.cpp b/llvm/lib/MC/MCWin64EH.cpp index 72a8dd7..a87648a 100644 --- a/llvm/lib/MC/MCWin64EH.cpp +++ b/llvm/lib/MC/MCWin64EH.cpp @@ -318,6 +318,9 @@ static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) { // Emit the epilog instructions. if (EnableUnwindV2) { + // Ensure the fixups and appended content apply to the same fragment. + OS->ensureHeadroom(info->EpilogMap.size() * 2); + bool IsLast = true; for (const auto &Epilog : llvm::reverse(info->EpilogMap)) { if (IsLast) { diff --git a/llvm/lib/MC/MCWinCOFFStreamer.cpp b/llvm/lib/MC/MCWinCOFFStreamer.cpp index 1ffe25c..a45936b 100644 --- a/llvm/lib/MC/MCWinCOFFStreamer.cpp +++ b/llvm/lib/MC/MCWinCOFFStreamer.cpp @@ -163,13 +163,13 @@ void MCWinCOFFStreamer::changeSection(MCSection *Section, uint32_t Subsection) { } void MCWinCOFFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) { - auto *Symbol = cast<MCSymbolCOFF>(S); + auto *Symbol = static_cast<MCSymbolCOFF *>(S); MCObjectStreamer::emitLabel(Symbol, Loc); } bool MCWinCOFFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) { - auto *Symbol = cast<MCSymbolCOFF>(S); + auto *Symbol = static_cast<MCSymbolCOFF *>(S); getAssembler().registerSymbol(*Symbol); switch (Attribute) { @@ -199,11 +199,10 @@ void MCWinCOFFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { } void MCWinCOFFStreamer::beginCOFFSymbolDef(MCSymbol const *S) { - auto *Symbol = cast<MCSymbolCOFF>(S); if (CurSymbol) Error("starting a new symbol definition without completing the " "previous one"); - CurSymbol = Symbol; + CurSymbol = static_cast<MCSymbolCOFF *>(const_cast<MCSymbol *>(S)); } void MCWinCOFFStreamer::emitCOFFSymbolStorageClass(int StorageClass) { @@ -219,7 +218,7 @@ void MCWinCOFFStreamer::emitCOFFSymbolStorageClass(int StorageClass) { } getAssembler().registerSymbol(*CurSymbol); - cast<MCSymbolCOFF>(CurSymbol)->setClass((uint16_t)StorageClass); + static_cast<MCSymbolCOFF *>(CurSymbol)->setClass((uint16_t)StorageClass); } void MCWinCOFFStreamer::emitCOFFSymbolType(int Type) { @@ -234,7 +233,7 @@ void MCWinCOFFStreamer::emitCOFFSymbolType(int Type) { } getAssembler().registerSymbol(*CurSymbol); - cast<MCSymbolCOFF>(CurSymbol)->setType((uint16_t)Type); + static_cast<const MCSymbolCOFF *>(CurSymbol)->setType((uint16_t)Type); } void MCWinCOFFStreamer::endCOFFSymbolDef() { @@ -249,7 +248,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) { if (getContext().getTargetTriple().getArch() != Triple::x86) return; - const MCSymbolCOFF *CSymbol = cast<MCSymbolCOFF>(Symbol); + auto *CSymbol = static_cast<const MCSymbolCOFF *>(Symbol); if (CSymbol->isSafeSEH()) return; @@ -258,7 +257,7 @@ void MCWinCOFFStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) { switchSection(SXData); SXData->ensureMinAlignment(Align(4)); - insert(getContext().allocFragment<MCSymbolIdFragment>(Symbol)); + newSpecialFragment<MCSymbolIdFragment>(Symbol); getAssembler().registerSymbol(*Symbol); CSymbol->setIsSafeSEH(); @@ -273,13 +272,14 @@ void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) { MCSection *Sec = getCurrentSectionOnly(); Sec->ensureMinAlignment(Align(4)); - insert(getContext().allocFragment<MCSymbolIdFragment>(Symbol)); + newSpecialFragment<MCSymbolIdFragment>(Symbol); getAssembler().registerSymbol(*Symbol); } void MCWinCOFFStreamer::emitCOFFSectionIndex(const MCSymbol *Symbol) { visitUsedSymbol(*Symbol); const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext()); + ensureHeadroom(2); addFixup(SRE, FK_SecRel_2); appendContents(2, 0); } @@ -293,6 +293,7 @@ void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol, if (Offset) MCE = MCBinaryExpr::createAdd( MCE, MCConstantExpr::create(Offset, getContext()), getContext()); + ensureHeadroom(4); addFixup(MCE, FK_SecRel_4); // Emit 4 bytes (zeros) to the object file. appendContents(4, 0); @@ -308,6 +309,7 @@ void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol, if (Offset) MCE = MCBinaryExpr::createAdd( MCE, MCConstantExpr::create(Offset, getContext()), getContext()); + ensureHeadroom(4); addFixup(MCE, FK_Data_4); // Emit 4 bytes (zeros) to the object file. appendContents(4, 0); @@ -318,6 +320,7 @@ void MCWinCOFFStreamer::emitCOFFSecNumber(MCSymbol const *Symbol) { // Create Symbol for section number. const MCExpr *MCE = MCCOFFSectionNumberTargetExpr::create( *Symbol, this->getWriter(), getContext()); + ensureHeadroom(4); addFixup(MCE, FK_Data_4); // Emit 4 bytes (zeros) to the object file. appendContents(4, 0); @@ -328,6 +331,7 @@ void MCWinCOFFStreamer::emitCOFFSecOffset(MCSymbol const *Symbol) { // Create Symbol for section offset. const MCExpr *MCE = MCCOFFSectionOffsetTargetExpr::create(*Symbol, getContext()); + ensureHeadroom(4); addFixup(MCE, FK_Data_4); // Emit 4 bytes (zeros) to the object file. appendContents(4, 0); @@ -335,7 +339,7 @@ void MCWinCOFFStreamer::emitCOFFSecOffset(MCSymbol const *Symbol) { void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, Align ByteAlignment) { - auto *Symbol = cast<MCSymbolCOFF>(S); + auto *Symbol = static_cast<MCSymbolCOFF *>(S); const Triple &T = getContext().getTargetTriple(); if (T.isWindowsMSVCEnvironment()) { @@ -367,7 +371,7 @@ void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, void MCWinCOFFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size, Align ByteAlignment) { - auto *Symbol = cast<MCSymbolCOFF>(S); + auto *Symbol = static_cast<MCSymbolCOFF *>(S); MCSection *Section = getContext().getObjectFileInfo()->getBSSSection(); pushSection(); @@ -382,7 +386,7 @@ void MCWinCOFFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size, // Hack: Used by llvm-ml to implement the alias directive. void MCWinCOFFStreamer::emitWeakReference(MCSymbol *AliasS, const MCSymbol *Symbol) { - auto *Alias = cast<MCSymbolCOFF>(AliasS); + auto *Alias = static_cast<MCSymbolCOFF *>(AliasS); emitSymbolAttribute(Alias, MCSA_Weak); Alias->setIsWeakExternal(true); @@ -410,7 +414,7 @@ void MCWinCOFFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From, void MCWinCOFFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) { const MCSymbol *S = &SRE->getSymbol(); if (getAssembler().registerSymbol(*S)) - cast<MCSymbolCOFF>(S)->setExternal(true); + static_cast<const MCSymbolCOFF *>(S)->setExternal(true); } void MCWinCOFFStreamer::finishImpl() { diff --git a/llvm/lib/MC/MCXCOFFStreamer.cpp b/llvm/lib/MC/MCXCOFFStreamer.cpp index 26f45ce..a0e3dba 100644 --- a/llvm/lib/MC/MCXCOFFStreamer.cpp +++ b/llvm/lib/MC/MCXCOFFStreamer.cpp @@ -52,7 +52,7 @@ void MCXCOFFStreamer::changeSection(MCSection *Section, uint32_t Subsection) { bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym, MCSymbolAttr Attribute) { - auto *Symbol = cast<MCSymbolXCOFF>(Sym); + auto *Symbol = static_cast<MCSymbolXCOFF *>(Sym); getAssembler().registerSymbol(*Symbol); switch (Attribute) { @@ -109,7 +109,7 @@ void MCXCOFFStreamer::emitXCOFFRefDirective(const MCSymbol *Symbol) { void MCXCOFFStreamer::emitXCOFFRenameDirective(const MCSymbol *Name, StringRef Rename) { - const MCSymbolXCOFF *Symbol = cast<const MCSymbolXCOFF>(Name); + auto *Symbol = static_cast<const MCSymbolXCOFF *>(Name); if (!Symbol->hasRename()) report_fatal_error("Only explicit .rename is supported for XCOFF."); } @@ -129,15 +129,14 @@ void MCXCOFFStreamer::emitXCOFFCInfoSym(StringRef Name, StringRef Metadata) { void MCXCOFFStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) { + auto *Sym = static_cast<MCSymbolXCOFF *>(Symbol); getAssembler().registerSymbol(*Symbol); - Symbol->setExternal(cast<MCSymbolXCOFF>(Symbol)->getStorageClass() != - XCOFF::C_HIDEXT); + Symbol->setExternal(Sym->getStorageClass() != XCOFF::C_HIDEXT); Symbol->setCommon(Size, ByteAlignment); // Default csect align is 4, but common symbols have explicit alignment values // and we should honor it. - cast<MCSymbolXCOFF>(Symbol)->getRepresentedCsect()->setAlignment( - ByteAlignment); + Sym->getRepresentedCsect()->setAlignment(ByteAlignment); // Emit the alignment and storage for the variable to the section. emitValueToAlignment(ByteAlignment); diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp index e87696a..eb59e39 100644 --- a/llvm/lib/MC/MachObjectWriter.cpp +++ b/llvm/lib/MC/MachObjectWriter.cpp @@ -72,7 +72,7 @@ bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) { // References to weak definitions require external relocation entries; the // definition may not always be the one in the same object file. - if (cast<MCSymbolMachO>(S).isWeakDefinition()) + if (static_cast<const MCSymbolMachO &>(S).isWeakDefinition()) return true; // Otherwise, we can use an internal relocation. @@ -383,15 +383,16 @@ const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const { } void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCAssembler &Asm) { - const MCSymbol *Symbol = MSD.Symbol; - const auto &Data = cast<MCSymbolMachO>(*Symbol); - const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol); + auto *Symbol = static_cast<const MCSymbolMachO *>(MSD.Symbol); + const auto &Data = static_cast<const MCSymbolMachO &>(*Symbol); + auto *AliasedSymbol = + static_cast<const MCSymbolMachO *>(&findAliasedSymbol(*Symbol)); uint8_t SectionIndex = MSD.SectionIndex; uint8_t Type = 0; uint64_t Address = 0; bool IsAlias = Symbol != AliasedSymbol; - const MCSymbol &OrigSymbol = *Symbol; + const MCSymbolMachO &OrigSymbol = *Symbol; MachSymbolData *AliaseeInfo; if (IsAlias) { AliaseeInfo = findSymbolData(*AliasedSymbol); @@ -441,9 +442,8 @@ void MachObjectWriter::writeNlist(MachSymbolData &MSD, const MCAssembler &Asm) { // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc' // value. - bool EncodeAsAltEntry = - IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry(); - W.write<uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry)); + bool EncodeAsAltEntry = IsAlias && OrigSymbol.isAltEntry(); + W.write<uint16_t>(Symbol->getEncodedFlags(EncodeAsAltEntry)); if (is64Bit()) W.write<uint64_t>(Address); else @@ -570,7 +570,8 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) { // // FIXME: Do not hardcode. if (Asm.registerSymbol(*ISD.Symbol)) - cast<MCSymbolMachO>(ISD.Symbol)->setReferenceTypeUndefinedLazy(true); + static_cast<MCSymbolMachO *>(ISD.Symbol) + ->setReferenceTypeUndefinedLazy(true); } } @@ -588,7 +589,7 @@ void MachObjectWriter::computeSymbolTable( // Build the string table. for (const MCSymbol &Symbol : Asm.symbols()) { - if (!cast<MCSymbolMachO>(Symbol).isSymbolLinkerVisible()) + if (!static_cast<const MCSymbolMachO &>(Symbol).isSymbolLinkerVisible()) continue; StringTable.add(Symbol.getName()); @@ -602,7 +603,7 @@ void MachObjectWriter::computeSymbolTable( // important for letting us diff .o files. for (const MCSymbol &Symbol : Asm.symbols()) { // Ignore non-linker visible symbols. - if (!cast<MCSymbolMachO>(Symbol).isSymbolLinkerVisible()) + if (!static_cast<const MCSymbolMachO &>(Symbol).isSymbolLinkerVisible()) continue; if (!Symbol.isExternal() && !Symbol.isUndefined()) @@ -628,7 +629,7 @@ void MachObjectWriter::computeSymbolTable( // Now add the data for local symbols. for (const MCSymbol &Symbol : Asm.symbols()) { // Ignore non-linker visible symbols. - if (!cast<MCSymbolMachO>(Symbol).isSymbolLinkerVisible()) + if (!static_cast<const MCSymbolMachO &>(Symbol).isSymbolLinkerVisible()) continue; if (Symbol.isExternal() || Symbol.isUndefined()) diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index bfd6334..af009a4 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -487,7 +487,7 @@ void WasmObjectWriter::recordRelocation(const MCFragment &F, bool IsLocRel = false; if (const auto *RefB = Target.getSubSym()) { - const auto &SymB = cast<MCSymbolWasm>(*RefB); + auto &SymB = static_cast<const MCSymbolWasm &>(*RefB); if (FixupSection.isText()) { Ctx.reportError(Fixup.getLoc(), @@ -515,7 +515,7 @@ void WasmObjectWriter::recordRelocation(const MCFragment &F, } // We either rejected the fixup or folded B into C at this point. - const auto *SymA = cast<MCSymbolWasm>(Target.getAddSym()); + auto *SymA = static_cast<const MCSymbolWasm *>(Target.getAddSym()); // The .init_array isn't translated as data, so don't do relocations in it. if (FixupSection.getName().starts_with(".init_array")) { @@ -561,7 +561,7 @@ void WasmObjectWriter::recordRelocation(const MCFragment &F, report_fatal_error("section symbol is required for relocation"); C += Asm->getSymbolOffset(*SymA); - SymA = cast<MCSymbolWasm>(SectionSymbol); + SymA = static_cast<const MCSymbolWasm *>(SectionSymbol); } if (Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB || @@ -573,7 +573,7 @@ void WasmObjectWriter::recordRelocation(const MCFragment &F, // TABLE_INDEX relocs implicitly use the default indirect function table. // We require the function table to have already been defined. auto TableName = "__indirect_function_table"; - MCSymbolWasm *Sym = cast_or_null<MCSymbolWasm>(Ctx.lookupSymbol(TableName)); + auto *Sym = static_cast<MCSymbolWasm *>(Ctx.lookupSymbol(TableName)); if (!Sym) { report_fatal_error("missing indirect function table symbol"); } else { @@ -631,8 +631,8 @@ WasmObjectWriter::getProvisionalValue(const MCAssembler &Asm, case wasm::R_WASM_TABLE_INDEX_I32: case wasm::R_WASM_TABLE_INDEX_I64: { // Provisional value is table address of the resolved symbol itself - const MCSymbolWasm *Base = - cast<MCSymbolWasm>(Asm.getBaseSymbol(*RelEntry.Symbol)); + auto *Base = + static_cast<const MCSymbolWasm *>(Asm.getBaseSymbol(*RelEntry.Symbol)); assert(Base->isFunction()); if (RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB || RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB64) @@ -1342,11 +1342,11 @@ void WasmObjectWriter::prepareImports( // Register types for all functions, including those with private linkage // (because wasm always needs a type signature). if (WS.isFunction()) { - const auto *BS = Asm.getBaseSymbol(S); + auto *BS = static_cast<const MCSymbolWasm *>(Asm.getBaseSymbol(S)); if (!BS) report_fatal_error(Twine(S.getName()) + ": absolute addressing not supported!"); - registerFunctionType(*cast<MCSymbolWasm>(BS)); + registerFunctionType(*BS); } if (WS.isTag()) @@ -1516,10 +1516,10 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, // For user-defined custom sections, strip the prefix Name.consume_front(".custom_section."); - MCSymbol *Begin = Sec.getBeginSymbol(); + auto *Begin = static_cast<MCSymbolWasm *>(Sec.getBeginSymbol()); if (Begin) { - assert(WasmIndices.count(cast<MCSymbolWasm>(Begin)) == 0); - WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size(); + assert(WasmIndices.count(Begin) == 0); + WasmIndices[Begin] = CustomSections.size(); } // Separate out the producers and target features sections @@ -1719,7 +1719,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, if (!BS) report_fatal_error(Twine(S.getName()) + ": absolute addressing not supported!"); - const MCSymbolWasm *Base = cast<MCSymbolWasm>(BS); + const MCSymbolWasm *Base = static_cast<const MCSymbolWasm *>(BS); // Find the target symbol of this weak alias and export that index const auto &WS = static_cast<const MCSymbolWasm &>(S); @@ -1829,8 +1829,8 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB64) return; assert(Rel.Symbol->isFunction()); - const MCSymbolWasm *Base = - cast<MCSymbolWasm>(Asm.getBaseSymbol(*Rel.Symbol)); + auto *Base = + static_cast<const MCSymbolWasm *>(Asm.getBaseSymbol(*Rel.Symbol)); uint32_t FunctionIndex = WasmIndices.find(Base)->second; uint32_t TableIndex = TableElems.size() + InitialTableOffset; if (TableIndices.try_emplace(Base, TableIndex).second) { @@ -1880,7 +1880,8 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, if (!SymRef) report_fatal_error( "fixups in .init_array should be symbol references"); - const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol()); + auto &TargetSym = + static_cast<const MCSymbolWasm &>(SymRef->getSymbol()); if (TargetSym.getIndex() == InvalidIndex) report_fatal_error("symbols in .init_array should exist in symtab"); if (!TargetSym.isFunction()) @@ -1905,7 +1906,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, writeExportSection(Exports); const MCSymbol *IndirectFunctionTable = getContext().lookupSymbol("__indirect_function_table"); - writeElemSection(cast_or_null<const MCSymbolWasm>(IndirectFunctionTable), + writeElemSection(static_cast<const MCSymbolWasm *>(IndirectFunctionTable), TableElems); writeDataCountSection(); diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp index 856850d..0cc5ff5 100644 --- a/llvm/lib/MC/WinCOFFObjectWriter.cpp +++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp @@ -382,7 +382,8 @@ void WinCOFFWriter::defineSymbol(const MCSymbol &MCSym) { COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym); COFFSymbol *Local = nullptr; - if (cast<MCSymbolCOFF>(MCSym).getWeakExternalCharacteristics()) { + if (static_cast<const MCSymbolCOFF &>(MCSym) + .getWeakExternalCharacteristics()) { Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL; Sym->Section = nullptr; @@ -406,7 +407,8 @@ void WinCOFFWriter::defineSymbol(const MCSymbol &MCSym) { Sym->Aux[0].AuxType = ATWeakExternal; Sym->Aux[0].Aux.WeakExternal.TagIndex = 0; // Filled in later Sym->Aux[0].Aux.WeakExternal.Characteristics = - cast<MCSymbolCOFF>(MCSym).getWeakExternalCharacteristics(); + static_cast<const MCSymbolCOFF &>(MCSym) + .getWeakExternalCharacteristics(); } else { if (!Base) Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE; @@ -418,7 +420,7 @@ void WinCOFFWriter::defineSymbol(const MCSymbol &MCSym) { if (Local) { Local->Data.Value = getSymbolValue(MCSym, *Asm); - const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym); + auto &SymbolCOFF = static_cast<const MCSymbolCOFF &>(MCSym); Local->Data.Type = SymbolCOFF.getType(); Local->Data.StorageClass = SymbolCOFF.getClass(); @@ -821,7 +823,8 @@ void WinCOFFWriter::executePostLayoutBinding() { for (const MCSymbol &Symbol : Asm->symbols()) // Define non-temporary or temporary static (private-linkage) symbols if (!Symbol.isTemporary() || - cast<MCSymbolCOFF>(Symbol).getClass() == COFF::IMAGE_SYM_CLASS_STATIC) + static_cast<const MCSymbolCOFF &>(Symbol).getClass() == + COFF::IMAGE_SYM_CLASS_STATIC) defineSymbol(Symbol); UseBigObj = Sections.size() > COFF::MaxNumberOfSections16; @@ -1188,7 +1191,7 @@ bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( // point to thunks, and the /GUARD:CF flag assumes that it can use relocations // to approximate the set of all address taken functions. LLD's implementation // of /GUARD:CF also relies on the existance of these relocations. - uint16_t Type = cast<MCSymbolCOFF>(SymA).getType(); + uint16_t Type = static_cast<const MCSymbolCOFF &>(SymA).getType(); if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION) return false; return &SymA.getSection() == FB.getParent(); diff --git a/llvm/lib/MC/XCOFFObjectWriter.cpp b/llvm/lib/MC/XCOFFObjectWriter.cpp index 65f543b..13917ba 100644 --- a/llvm/lib/MC/XCOFFObjectWriter.cpp +++ b/llvm/lib/MC/XCOFFObjectWriter.cpp @@ -591,7 +591,7 @@ void XCOFFWriter::executePostLayoutBinding() { if (S.isTemporary()) continue; - const MCSymbolXCOFF *XSym = cast<MCSymbolXCOFF>(&S); + auto *XSym = static_cast<const MCSymbolXCOFF *>(&S); const MCSectionXCOFF *ContainingCsect = getContainingCsect(XSym); if (ContainingCsect->isDwarfSect()) @@ -690,7 +690,8 @@ void XCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup, std::tie(Type, SignAndSize) = TargetObjectWriter->getRelocTypeAndSignSize( Target, Fixup, Fixup.isPCRel()); - const MCSectionXCOFF *SymASec = getContainingCsect(cast<MCSymbolXCOFF>(SymA)); + const MCSectionXCOFF *SymASec = + getContainingCsect(static_cast<const MCSymbolXCOFF *>(SymA)); assert(SectionMap.contains(SymASec) && "Expected containing csect to exist in map."); @@ -773,13 +774,13 @@ void XCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup, "Expected containing csect to exist in map."); SectionMap[RelocationSec]->Relocations.push_back(Reloc); - const MCSymbol *const SymB = Target.getSubSym(); + auto SymB = static_cast<const MCSymbolXCOFF *>(Target.getSubSym()); if (!SymB) return; if (SymA == SymB) report_fatal_error("relocation for opposite term is not yet supported"); - const MCSectionXCOFF *SymBSec = getContainingCsect(cast<MCSymbolXCOFF>(SymB)); + const MCSectionXCOFF *SymBSec = getContainingCsect(SymB); assert(SectionMap.contains(SymBSec) && "Expected containing csect to exist in map."); if (SymASec == SymBSec) |