diff --git a/include/llvm/MC/MCAsmLayout.h b/include/llvm/MC/MCAsmLayout.h index 27bdbe9cbf5..2c26ff02964 100644 --- a/include/llvm/MC/MCAsmLayout.h +++ b/include/llvm/MC/MCAsmLayout.h @@ -12,6 +12,9 @@ namespace llvm { class MCAssembler; +class MCFragment; +class MCSectionData; +class MCSymbolData; /// Encapsulates the layout of an assembly file at a particular point in time. /// @@ -29,6 +32,14 @@ public: /// Get the assembler object this is a layout for. MCAssembler &getAssembler() const { return Assembler; } + + uint64_t getFragmentAddress(const MCFragment *F) const; + + uint64_t getSectionAddress(const MCSectionData *SD) const; + + uint64_t getSymbolAddress(const MCSymbolData *SD) const; + + void setSectionAddress(MCSectionData *SD, uint64_t Value); }; } // end namespace llvm diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h index d14122bf74c..54c91a2cb53 100644 --- a/include/llvm/MC/MCAssembler.h +++ b/include/llvm/MC/MCAssembler.h @@ -699,15 +699,17 @@ public: /// Find the symbol which defines the atom containing given address, inside /// the given section, or null if there is no such symbol. // - // FIXME: Eliminate this, it is very slow. - const MCSymbolData *getAtomForAddress(const MCSectionData *Section, + // FIXME-PERF: Eliminate this, it is very slow. + const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout, + const MCSectionData *Section, uint64_t Address) const; /// Find the symbol which defines the atom containing the given symbol, or /// null if there is no such symbol. // - // FIXME: Eliminate this, it is very slow. - const MCSymbolData *getAtom(const MCSymbolData *Symbol) const; + // FIXME-PERF: Eliminate this, it is very slow. + const MCSymbolData *getAtom(const MCAsmLayout &Layout, + const MCSymbolData *Symbol) const; /// Check whether a particular symbol is visible to the linker and is required /// in the symbol table, or whether it can be discarded by the assembler. This diff --git a/include/llvm/MC/MCObjectWriter.h b/include/llvm/MC/MCObjectWriter.h index 0316f51049c..f70a3d1851e 100644 --- a/include/llvm/MC/MCObjectWriter.h +++ b/include/llvm/MC/MCObjectWriter.h @@ -16,6 +16,7 @@ namespace llvm { class MCAsmFixup; +class MCAsmLayout; class MCAssembler; class MCFragment; class MCValue; @@ -69,6 +70,7 @@ public: /// information about the relocation so that it can be emitted during /// WriteObject(). virtual void RecordRelocation(const MCAssembler &Asm, + const MCAsmLayout &Layout, const MCFragment *Fragment, const MCAsmFixup &Fixup, MCValue Target, uint64_t &FixedValue) = 0; @@ -78,7 +80,8 @@ public: /// This routine is called by the assembler after layout and relaxation is /// complete, fixups have been evaluate and applied, and relocations /// generated. - virtual void WriteObject(const MCAssembler &Asm) = 0; + virtual void WriteObject(const MCAssembler &Asm, + const MCAsmLayout &Layout) = 0; /// @} /// @name Binary Output diff --git a/include/llvm/MC/MachObjectWriter.h b/include/llvm/MC/MachObjectWriter.h index c2edf638636..844025d15c4 100644 --- a/include/llvm/MC/MachObjectWriter.h +++ b/include/llvm/MC/MachObjectWriter.h @@ -31,11 +31,12 @@ public: virtual void ExecutePostLayoutBinding(MCAssembler &Asm); virtual void RecordRelocation(const MCAssembler &Asm, + const MCAsmLayout &Layout, const MCFragment *Fragment, const MCAsmFixup &Fixup, MCValue Target, uint64_t &FixedValue); - virtual void WriteObject(const MCAssembler &Asm); + virtual void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout); }; } // End llvm namespace diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index a168abc02ec..4a5db8ce222 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -45,6 +45,24 @@ STATISTIC(ObjectBytes, "Number of emitted object file bytes"); /* *** */ +uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const { + return F->getAddress(); +} + +uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const { + return SD->getAddress(); +} + +uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const { + return SD->getAddress(); +} + +void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) { + SD->setAddress(Value); +} + +/* *** */ + MCFragment::MCFragment() : Kind(FragmentType(~0)) { } @@ -145,6 +163,7 @@ static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm, } static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, + const MCAsmLayout &Layout, const MCAsmFixup &Fixup, const MCValue Target, const MCSymbolData *BaseSymbol) { @@ -165,7 +184,7 @@ static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, if (A->getKind() != MCSymbolRefExpr::VK_None) return false; - A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol())); + A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol())); if (!A_Base) return false; } @@ -175,7 +194,7 @@ static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, if (B->getKind() != MCSymbolRefExpr::VK_None) return false; - B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol())); + B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol())); if (!B_Base) return false; } @@ -203,9 +222,13 @@ bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const { SD->getFragment()->getParent()->getSection()); } -const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section, +// FIXME-PERF: This routine is really slow. +const MCSymbolData *MCAssembler::getAtomForAddress(const MCAsmLayout &Layout, + const MCSectionData *Section, uint64_t Address) const { const MCSymbolData *Best = 0; + uint64_t BestAddress = 0; + for (MCAssembler::const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { // Ignore non-linker visible symbols. @@ -218,15 +241,19 @@ const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section, // Otherwise, find the closest symbol preceding this address (ties are // resolved in favor of the last defined symbol). - if (it->getAddress() <= Address && - (!Best || it->getAddress() >= Best->getAddress())) + uint64_t SymbolAddress = Layout.getSymbolAddress(it); + if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) { Best = it; + BestAddress = SymbolAddress; + } } return Best; } -const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { +// FIXME-PERF: This routine is really slow. +const MCSymbolData *MCAssembler::getAtom(const MCAsmLayout &Layout, + const MCSymbolData *SD) const { // Linker visible symbols define atoms. if (isSymbolLinkerVisible(SD)) return SD; @@ -236,7 +263,8 @@ const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const { return 0; // Otherwise, search by address. - return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress()); + return getAtomForAddress(Layout, SD->getFragment()->getParent(), + Layout.getSymbolAddress(SD)); } bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, @@ -258,13 +286,13 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, bool IsResolved = true; if (const MCSymbolRefExpr *A = Target.getSymA()) { if (A->getSymbol().isDefined()) - Value += getSymbolData(A->getSymbol()).getAddress(); + Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol())); else IsResolved = false; } if (const MCSymbolRefExpr *B = Target.getSymB()) { if (B->getSymbol().isDefined()) - Value -= getSymbolData(B->getSymbol()).getAddress(); + Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol())); else IsResolved = false; } @@ -278,13 +306,13 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, const MCSymbolData *BaseSymbol = 0; if (IsPCRel) { BaseSymbol = getAtomForAddress( - DF->getParent(), DF->getAddress() + Fixup.Offset); + Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset); if (!BaseSymbol) IsResolved = false; } if (IsResolved) - IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target, + IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target, BaseSymbol); } else { const MCSection *BaseSection = 0; @@ -297,19 +325,19 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout, } if (IsPCRel) - Value -= DF->getAddress() + Fixup.Offset; + Value -= Layout.getFragmentAddress(DF) + Fixup.Offset; return IsResolved; } void MCAssembler::LayoutSection(MCSectionData &SD, MCAsmLayout &Layout) { - uint64_t Address = SD.getAddress(); + uint64_t Address, StartAddress = Address = Layout.getSectionAddress(&SD); for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) { MCFragment &F = *it; - F.setOffset(Address - SD.getAddress()); + F.setOffset(Address - StartAddress); // Evaluate fragment size. switch (F.getKind()) { @@ -361,7 +389,7 @@ void MCAssembler::LayoutSection(MCSectionData &SD, // Align the fragment offset; it is safe to adjust the offset freely since // this is only in virtual sections. Address = RoundUpToAlignment(Address, ZFF.getAlignment()); - F.setOffset(Address - SD.getAddress()); + F.setOffset(Address - StartAddress); // FIXME: This is misnamed. F.setFileSize(ZFF.getSize()); @@ -373,11 +401,11 @@ void MCAssembler::LayoutSection(MCSectionData &SD, } // Set the section sizes. - SD.setSize(Address - SD.getAddress()); + SD.setSize(Address - StartAddress); if (getBackend().isVirtualSection(SD.getSection())) SD.setFileSize(0); else - SD.setFileSize(Address - SD.getAddress()); + SD.setFileSize(Address - StartAddress); } /// WriteFragmentData - Write the \arg F data to the output file. @@ -543,7 +571,7 @@ void MCAssembler::Finish() { // The fixup was unresolved, we need a relocation. Inform the object // writer of the relocation, and give it an opportunity to adjust the // fixup value if need be. - Writer->RecordRelocation(*this, DF, Fixup, Target, FixedValue); + Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue); } getBackend().ApplyFixup(Fixup, *DF, FixedValue); @@ -552,7 +580,7 @@ void MCAssembler::Finish() { } // Write the object file. - Writer->WriteObject(*this); + Writer->WriteObject(*this, Layout); OS.flush(); stats::ObjectBytes += OS.tell() - StartOffset; @@ -609,7 +637,7 @@ bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) { } // Layout the section fragments and its size. - SD.setAddress(Address); + Layout.setSectionAddress(&SD, Address); LayoutSection(SD, Layout); Address += SD.getFileSize(); @@ -628,7 +656,7 @@ bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) { if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) Address += Pad; - SD.setAddress(Address); + Layout.setSectionAddress(&SD, Address); LayoutSection(SD, Layout); Address += SD.getSize(); } diff --git a/lib/MC/MCExpr.cpp b/lib/MC/MCExpr.cpp index a9256b632e2..1d6a316363f 100644 --- a/lib/MC/MCExpr.cpp +++ b/lib/MC/MCExpr.cpp @@ -268,8 +268,8 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res, Layout->getAssembler().getSymbolData(Res.getSymA()->getSymbol()); MCSymbolData &B = Layout->getAssembler().getSymbolData(Res.getSymB()->getSymbol()); - Res = MCValue::get(+ A.getFragment()->getAddress() + A.getOffset() - - B.getFragment()->getAddress() - B.getOffset() + Res = MCValue::get(+ Layout->getSymbolAddress(&A) + A.getOffset() + - Layout->getSymbolAddress(&B) - B.getOffset() + Res.getConstant()); } diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index e6232acbf0d..39dfac158a5 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -11,6 +11,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCAssembler.h" +#include "llvm/MC/MCAsmLayout.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSectionMachO.h" @@ -271,9 +272,9 @@ public: assert(OS.tell() - Start == SegmentLoadCommandSize); } - void WriteSection(const MCAssembler &Asm, const MCSectionData &SD, - uint64_t FileOffset, uint64_t RelocationsStart, - unsigned NumRelocations) { + void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout, + const MCSectionData &SD, uint64_t FileOffset, + uint64_t RelocationsStart, unsigned NumRelocations) { // The offset is unused for virtual sections. if (Asm.getBackend().isVirtualSection(SD.getSection())) { assert(SD.getFileSize() == 0 && "Invalid file size!"); @@ -292,10 +293,10 @@ public: WriteBytes(Section.getSectionName(), 16); WriteBytes(Section.getSegmentName(), 16); if (Is64Bit) { - Write64(SD.getAddress()); // address + Write64(Layout.getSectionAddress(&SD)); // address Write64(SD.getSize()); // size } else { - Write32(SD.getAddress()); // address + Write32(Layout.getSectionAddress(&SD)); // address Write32(SD.getSize()); // size } Write32(FileOffset); @@ -372,7 +373,7 @@ public: assert(OS.tell() - Start == DysymtabLoadCommandSize); } - void WriteNlist(MachSymbolData &MSD) { + void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) { MCSymbolData &Data = *MSD.SymbolData; const MCSymbol &Symbol = Data.getSymbol(); uint8_t Type = 0; @@ -403,7 +404,7 @@ public: if (Symbol.isAbsolute()) { llvm_unreachable("FIXME: Not yet implemented!"); } else { - Address = Data.getAddress(); + Address = Layout.getSymbolAddress(&Data); } } else if (Data.isCommon()) { // Common symbols are encoded with the size in the address @@ -451,7 +452,7 @@ public: // - Input errors, where something cannot be correctly encoded. 'as' allows // these through in many cases. - void RecordX86_64Relocation(const MCAssembler &Asm, + void RecordX86_64Relocation(const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCAsmFixup &Fixup, MCValue Target, uint64_t &FixedValue) { @@ -494,11 +495,11 @@ public: } else if (Target.getSymB()) { // A - B + constant const MCSymbol *A = &Target.getSymA()->getSymbol(); MCSymbolData &A_SD = Asm.getSymbolData(*A); - const MCSymbolData *A_Base = Asm.getAtom(&A_SD); + const MCSymbolData *A_Base = Asm.getAtom(Layout, &A_SD); const MCSymbol *B = &Target.getSymB()->getSymbol(); MCSymbolData &B_SD = Asm.getSymbolData(*B); - const MCSymbolData *B_Base = Asm.getAtom(&B_SD); + const MCSymbolData *B_Base = Asm.getAtom(Layout, &B_SD); // Neither symbol can be modified. if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || @@ -521,8 +522,8 @@ public: if (A_Base == B_Base) llvm_report_error("unsupported relocation with identical base"); - Value += A_SD.getAddress() - A_Base->getAddress(); - Value -= B_SD.getAddress() - B_Base->getAddress(); + Value += Layout.getSymbolAddress(&A_SD) - Layout.getSymbolAddress(A_Base); + Value -= Layout.getSymbolAddress(&B_SD) - Layout.getSymbolAddress(B_Base); Index = A_Base->getIndex(); IsExtern = 1; @@ -543,7 +544,7 @@ public: } else { const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); MCSymbolData &SD = Asm.getSymbolData(*Symbol); - const MCSymbolData *Base = Asm.getAtom(&SD); + const MCSymbolData *Base = Asm.getAtom(Layout, &SD); // x86_64 almost always uses external relocations, except when there is no // symbol to use as a base address (a local symbol with no preceeding @@ -554,7 +555,7 @@ public: // Add the local offset, if needed. if (Base != &SD) - Value += SD.getAddress() - Base->getAddress(); + Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base); } else { // The index is the section ordinal. // @@ -566,7 +567,7 @@ public: break; assert(it != ie && "Unable to find section index!"); IsExtern = 0; - Value += SD.getAddress(); + Value += Layout.getSymbolAddress(&SD); if (IsPCRel) Value -= Address + (1 << Log2Size); @@ -640,6 +641,7 @@ public: } void RecordScatteredRelocation(const MCAssembler &Asm, + const MCAsmLayout &Layout, const MCFragment *Fragment, const MCAsmFixup &Fixup, MCValue Target, uint64_t &FixedValue) { @@ -656,7 +658,7 @@ public: llvm_report_error("symbol '" + A->getName() + "' can not be undefined in a subtraction expression"); - uint32_t Value = A_SD->getAddress(); + uint32_t Value = Layout.getSymbolAddress(A_SD); uint32_t Value2 = 0; if (const MCSymbolRefExpr *B = Target.getSymB()) { @@ -672,7 +674,7 @@ public: // relocation types from the linkers point of view, this is done solely // for pedantic compatibility with 'as'. Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference; - Value2 = B_SD->getAddress(); + Value2 = Layout.getSymbolAddress(B_SD); } // Relocations are written out in reverse order, so the PAIR comes first. @@ -697,11 +699,11 @@ public: Relocations[Fragment->getParent()].push_back(MRE); } - void RecordRelocation(const MCAssembler &Asm, const MCFragment *Fragment, - const MCAsmFixup &Fixup, MCValue Target, - uint64_t &FixedValue) { + void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, + const MCFragment *Fragment, const MCAsmFixup &Fixup, + MCValue Target, uint64_t &FixedValue) { if (Is64Bit) { - RecordX86_64Relocation(Asm, Fragment, Fixup, Target, FixedValue); + RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue); return; } @@ -716,7 +718,7 @@ public: if (Target.getSymB() || (Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() && Offset)) { - RecordScatteredRelocation(Asm, Fragment, Fixup, Target, FixedValue); + RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,Target,FixedValue); return; } @@ -752,7 +754,7 @@ public: if (&*it == SD->getFragment()->getParent()) break; assert(it != ie && "Unable to find section index!"); - Value = SD->getAddress(); + Value = Layout.getSymbolAddress(SD); } Type = RIT_Vanilla; @@ -934,7 +936,7 @@ public: UndefinedSymbolData); } - void WriteObject(const MCAssembler &Asm) { + void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout) { unsigned NumSections = Asm.size(); // The section data starts after the header, the segment load command (and @@ -962,16 +964,16 @@ public: for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) { const MCSectionData &SD = *it; + uint64_t Address = Layout.getSectionAddress(&SD); - VMSize = std::max(VMSize, SD.getAddress() + SD.getSize()); + VMSize = std::max(VMSize, Address + SD.getSize()); if (Asm.getBackend().isVirtualSection(SD.getSection())) continue; - SectionDataSize = std::max(SectionDataSize, - SD.getAddress() + SD.getSize()); + SectionDataSize = std::max(SectionDataSize, Address + SD.getSize()); SectionDataFileSize = std::max(SectionDataFileSize, - SD.getAddress() + SD.getFileSize()); + Address + SD.getFileSize()); } // The section data is padded to 4 bytes. @@ -992,8 +994,8 @@ public: ie = Asm.end(); it != ie; ++it) { std::vector &Relocs = Relocations[it]; unsigned NumRelocs = Relocs.size(); - uint64_t SectionStart = SectionDataStart + it->getAddress(); - WriteSection(Asm, *it, SectionStart, RelocTableEnd, NumRelocs); + uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it); + WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs); RelocTableEnd += NumRelocs * RelocationInfoSize; } @@ -1080,11 +1082,11 @@ public: // Write the symbol table entries. for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) - WriteNlist(LocalSymbolData[i]); + WriteNlist(LocalSymbolData[i], Layout); for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) - WriteNlist(ExternalSymbolData[i]); + WriteNlist(ExternalSymbolData[i], Layout); for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) - WriteNlist(UndefinedSymbolData[i]); + WriteNlist(UndefinedSymbolData[i], Layout); // Write the string table. OS << StringTable.str(); @@ -1111,13 +1113,15 @@ void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) { } void MachObjectWriter::RecordRelocation(const MCAssembler &Asm, + const MCAsmLayout &Layout, const MCFragment *Fragment, const MCAsmFixup &Fixup, MCValue Target, uint64_t &FixedValue) { - ((MachObjectWriterImpl*) Impl)->RecordRelocation(Asm, Fragment, Fixup, + ((MachObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue); } -void MachObjectWriter::WriteObject(const MCAssembler &Asm) { - ((MachObjectWriterImpl*) Impl)->WriteObject(Asm); +void MachObjectWriter::WriteObject(const MCAssembler &Asm, + const MCAsmLayout &Layout) { + ((MachObjectWriterImpl*) Impl)->WriteObject(Asm, Layout); }