From c8d166a4373812212ed489c41760bac3b8c043ff Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Wed, 20 May 2015 15:16:14 +0000 Subject: [PATCH] MC: Use MCSymbol in MachObjectWriter, NFC Replace uses of `MCSymbolData` with `MCSymbol` where both are needed, so we can remove the backpointer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237799 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCMachObjectWriter.h | 4 +-- lib/MC/MachObjectWriter.cpp | 30 ++++++++-------- .../ARM/MCTargetDesc/ARMMachObjectWriter.cpp | 35 +++++++++---------- .../MCTargetDesc/PPCMachObjectWriter.cpp | 19 +++++----- .../X86/MCTargetDesc/X86MachObjectWriter.cpp | 27 +++++++------- 5 files changed, 54 insertions(+), 61 deletions(-) diff --git a/include/llvm/MC/MCMachObjectWriter.h b/include/llvm/MC/MCMachObjectWriter.h index cee3924a850..cb73791eb22 100644 --- a/include/llvm/MC/MCMachObjectWriter.h +++ b/include/llvm/MC/MCMachObjectWriter.h @@ -81,7 +81,7 @@ class MachObjectWriter : public MCObjectWriter { /// MachSymbolData - Helper struct for containing some precomputed information /// on symbols. struct MachSymbolData { - MCSymbolData *SymbolData; + const MCSymbol *Symbol; uint64_t StringIndex; uint8_t SectionIndex; @@ -152,7 +152,7 @@ public: uint64_t getPaddingSize(const MCSectionData *SD, const MCAsmLayout &Layout) const; - bool doesSymbolRequireExternRelocation(const MCSymbolData *SD); + bool doesSymbolRequireExternRelocation(const MCSymbol &S); /// @} diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index 7d6ed41b5b4..b4fd55e8e3d 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -39,15 +39,14 @@ void MachObjectWriter::reset() { MCObjectWriter::reset(); } -bool MachObjectWriter:: -doesSymbolRequireExternRelocation(const MCSymbolData *SD) { +bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) { // Undefined symbols are always extern. - if (SD->getSymbol().isUndefined()) + if (S.isUndefined()) return true; // References to weak definitions require external relocation entries; the // definition may not always be the one in the same object file. - if (SD->getFlags() & SF_WeakDefinition) + if (S.getData().getFlags() & SF_WeakDefinition) return true; // Otherwise, we can use an internal relocation. @@ -56,8 +55,7 @@ doesSymbolRequireExternRelocation(const MCSymbolData *SD) { bool MachObjectWriter:: MachSymbolData::operator<(const MachSymbolData &RHS) const { - return SymbolData->getSymbol().getName() < - RHS.SymbolData->getSymbol().getName(); + return Symbol->getName() < RHS.Symbol->getName(); } bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { @@ -303,15 +301,15 @@ void MachObjectWriter::WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol, MachObjectWriter::MachSymbolData * MachObjectWriter::findSymbolData(const MCSymbol &Sym) { for (auto &Entry : LocalSymbolData) - if (&Entry.SymbolData->getSymbol() == &Sym) + if (Entry.Symbol == &Sym) return &Entry; for (auto &Entry : ExternalSymbolData) - if (&Entry.SymbolData->getSymbol() == &Sym) + if (Entry.Symbol == &Sym) return &Entry; for (auto &Entry : UndefinedSymbolData) - if (&Entry.SymbolData->getSymbol() == &Sym) + if (Entry.Symbol == &Sym) return &Entry; return nullptr; @@ -331,8 +329,8 @@ const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const { void MachObjectWriter::WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) { - MCSymbolData &Data = *MSD.SymbolData; - const MCSymbol *Symbol = &Data.getSymbol(); + const MCSymbol *Symbol = MSD.Symbol; + MCSymbolData &Data = Symbol->getData(); const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol); uint8_t SectionIndex = MSD.SectionIndex; uint8_t Type = 0; @@ -570,7 +568,7 @@ void MachObjectWriter::ComputeSymbolTable( continue; MachSymbolData MSD; - MSD.SymbolData = &SD; + MSD.Symbol = &Symbol; MSD.StringIndex = StringTable.getOffset(Symbol.getName()); if (Symbol.isUndefined()) { @@ -598,7 +596,7 @@ void MachObjectWriter::ComputeSymbolTable( continue; MachSymbolData MSD; - MSD.SymbolData = &SD; + MSD.Symbol = &Symbol; MSD.StringIndex = StringTable.getOffset(Symbol.getName()); if (Symbol.isAbsolute()) { @@ -618,11 +616,11 @@ void MachObjectWriter::ComputeSymbolTable( // Set the symbol indices. Index = 0; for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) - LocalSymbolData[i].SymbolData->setIndex(Index++); + LocalSymbolData[i].Symbol->getData().setIndex(Index++); for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) - ExternalSymbolData[i].SymbolData->setIndex(Index++); + ExternalSymbolData[i].Symbol->getData().setIndex(Index++); for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) - UndefinedSymbolData[i].SymbolData->setIndex(Index++); + UndefinedSymbolData[i].Symbol->getData().setIndex(Index++); for (const MCSectionData &SD : Asm) { std::vector &Relocs = Relocations[&SD]; diff --git a/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp b/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp index 2b38da1d988..d5f5f9a005f 100644 --- a/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp +++ b/lib/Target/ARM/MCTargetDesc/ARMMachObjectWriter.cpp @@ -44,9 +44,8 @@ class ARMMachObjectWriter : public MCMachObjectTargetWriter { bool requiresExternRelocation(MachObjectWriter *Writer, const MCAssembler &Asm, - const MCFragment &Fragment, - unsigned RelocType, const MCSymbolData *SD, - uint64_t FixedValue); + const MCFragment &Fragment, unsigned RelocType, + const MCSymbol &S, uint64_t FixedValue); public: ARMMachObjectWriter(bool Is64Bit, uint32_t CPUType, @@ -309,10 +308,10 @@ bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer, const MCAssembler &Asm, const MCFragment &Fragment, unsigned RelocType, - const MCSymbolData *SD, + const MCSymbol &S, uint64_t FixedValue) { // Most cases can be identified purely from the symbol. - if (Writer->doesSymbolRequireExternRelocation(SD)) + if (Writer->doesSymbolRequireExternRelocation(S)) return true; int64_t Value = (int64_t)FixedValue; // The displacement is signed. int64_t Range; @@ -334,8 +333,7 @@ bool ARMMachObjectWriter::requiresExternRelocation(MachObjectWriter *Writer, // BL/BLX also use external relocations when an internal relocation // would result in the target being out of range. This gives the linker // enough information to generate a branch island. - const MCSectionData &SymSD = Asm.getSectionData( - SD->getSymbol().getSection()); + const MCSectionData &SymSD = Asm.getSectionData(S.getSection()); Value += Writer->getSectionAddress(&SymSD); Value -= Writer->getSectionAddress(Fragment.getParent()); // If the resultant value would be out of range for an internal relocation, @@ -375,9 +373,9 @@ void ARMMachObjectWriter::RecordRelocation(MachObjectWriter *Writer, } // Get the symbol data, if any. - const MCSymbolData *SD = nullptr; + const MCSymbol *A = nullptr; if (Target.getSymA()) - SD = &Asm.getSymbolData(Target.getSymA()->getSymbol()); + A = &Target.getSymA()->getSymbol(); // FIXME: For other platforms, we need to use scattered relocations for // internal relocations with offsets. If this is an internal relocation with @@ -387,7 +385,7 @@ void ARMMachObjectWriter::RecordRelocation(MachObjectWriter *Writer, uint32_t Offset = Target.getConstant(); if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA) Offset += 1 << Log2Size; - if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD)) + if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A)) return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, RelocType, Log2Size, FixedValue); @@ -404,29 +402,28 @@ void ARMMachObjectWriter::RecordRelocation(MachObjectWriter *Writer, "not yet implemented"); } else { // Resolve constant variables. - if (SD->getSymbol().isVariable()) { + if (A->isVariable()) { int64_t Res; - if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute( - Res, Layout, Writer->getSectionAddressMap())) { + if (A->getVariableValue()->EvaluateAsAbsolute( + Res, Layout, Writer->getSectionAddressMap())) { FixedValue = Res; return; } } // Check whether we need an external or internal relocation. - if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, SD, + if (requiresExternRelocation(Writer, Asm, *Fragment, RelocType, *A, FixedValue)) { - RelSymbol = &SD->getSymbol(); + RelSymbol = A; // For external relocations, make sure to offset the fixup value to // compensate for the addend of the symbol address, if it was // undefined. This occurs with weak definitions, for example. - if (!SD->getSymbol().isUndefined()) - FixedValue -= Layout.getSymbolOffset(SD->getSymbol()); + if (!A->isUndefined()) + FixedValue -= Layout.getSymbolOffset(*A); } else { // The index is the section ordinal (1-based). - const MCSectionData &SymSD = Asm.getSectionData( - SD->getSymbol().getSection()); + const MCSectionData &SymSD = Asm.getSectionData(A->getSection()); Index = SymSD.getOrdinal() + 1; FixedValue += Writer->getSectionAddress(&SymSD); } diff --git a/lib/Target/PowerPC/MCTargetDesc/PPCMachObjectWriter.cpp b/lib/Target/PowerPC/MCTargetDesc/PPCMachObjectWriter.cpp index 99e40f71d13..99bc56aeb48 100644 --- a/lib/Target/PowerPC/MCTargetDesc/PPCMachObjectWriter.cpp +++ b/lib/Target/PowerPC/MCTargetDesc/PPCMachObjectWriter.cpp @@ -324,9 +324,9 @@ void PPCMachObjectWriter::RecordPPCRelocation( // this doesn't seem right for RIT_PPC_BR24 // Get the symbol data, if any. - const MCSymbolData *SD = nullptr; + const MCSymbol *A = nullptr; if (Target.getSymA()) - SD = &Asm.getSymbolData(Target.getSymA()->getSymbol()); + A = &Target.getSymA()->getSymbol(); // See . const uint32_t FixupOffset = getFixupOffset(Layout, Fragment, Fixup); @@ -344,9 +344,9 @@ void PPCMachObjectWriter::RecordPPCRelocation( // the above line stolen from ARM, not sure } else { // Resolve constant variables. - if (SD->getSymbol().isVariable()) { + if (A->isVariable()) { int64_t Res; - if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute( + if (A->getVariableValue()->EvaluateAsAbsolute( Res, Layout, Writer->getSectionAddressMap())) { FixedValue = Res; return; @@ -354,17 +354,16 @@ void PPCMachObjectWriter::RecordPPCRelocation( } // Check whether we need an external or internal relocation. - if (Writer->doesSymbolRequireExternRelocation(SD)) { - RelSymbol = &SD->getSymbol(); + if (Writer->doesSymbolRequireExternRelocation(*A)) { + RelSymbol = A; // For external relocations, make sure to offset the fixup value to // compensate for the addend of the symbol address, if it was // undefined. This occurs with weak definitions, for example. - if (!SD->getSymbol().isUndefined()) - FixedValue -= Layout.getSymbolOffset(SD->getSymbol()); + if (!A->isUndefined()) + FixedValue -= Layout.getSymbolOffset(*A); } else { // The index is the section ordinal (1-based). - const MCSectionData &SymSD = - Asm.getSectionData(SD->getSymbol().getSection()); + const MCSectionData &SymSD = Asm.getSectionData(A->getSection()); Index = SymSD.getOrdinal() + 1; FixedValue += Writer->getSectionAddress(&SymSD); } diff --git a/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp b/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp index efad2d92770..f189b424757 100644 --- a/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp +++ b/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp @@ -504,9 +504,9 @@ void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer, } // Get the symbol data, if any. - const MCSymbolData *SD = nullptr; + const MCSymbol *A = nullptr; if (Target.getSymA()) - SD = &Asm.getSymbolData(Target.getSymA()->getSymbol()); + A = &Target.getSymA()->getSymbol(); // If this is an internal relocation with an offset, it also needs a scattered // relocation entry. @@ -516,9 +516,9 @@ void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer, // Try to record the scattered relocation if needed. Fall back to non // scattered if necessary (see comments in RecordScatteredRelocation() // for details). - if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD) && - RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, - Target, Log2Size, FixedValue)) + if (Offset && A && !Writer->doesSymbolRequireExternRelocation(*A) && + RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target, + Log2Size, FixedValue)) return; // See . @@ -535,27 +535,26 @@ void X86MachObjectWriter::RecordX86Relocation(MachObjectWriter *Writer, Type = MachO::GENERIC_RELOC_VANILLA; } else { // Resolve constant variables. - if (SD->getSymbol().isVariable()) { + if (A->isVariable()) { int64_t Res; - if (SD->getSymbol().getVariableValue()->EvaluateAsAbsolute( - Res, Layout, Writer->getSectionAddressMap())) { + if (A->getVariableValue()->EvaluateAsAbsolute( + Res, Layout, Writer->getSectionAddressMap())) { FixedValue = Res; return; } } // Check whether we need an external or internal relocation. - if (Writer->doesSymbolRequireExternRelocation(SD)) { - RelSymbol = &SD->getSymbol(); + if (Writer->doesSymbolRequireExternRelocation(*A)) { + RelSymbol = A; // For external relocations, make sure to offset the fixup value to // compensate for the addend of the symbol address, if it was // undefined. This occurs with weak definitions, for example. - if (!SD->getSymbol().isUndefined()) - FixedValue -= Layout.getSymbolOffset(SD->getSymbol()); + if (!A->isUndefined()) + FixedValue -= Layout.getSymbolOffset(*A); } else { // The index is the section ordinal (1-based). - const MCSectionData &SymSD = Asm.getSectionData( - SD->getSymbol().getSection()); + const MCSectionData &SymSD = Asm.getSectionData(A->getSection()); Index = SymSD.getOrdinal() + 1; FixedValue += Writer->getSectionAddress(&SymSD); } -- 2.34.1