MC: Direct all {fragment,section,symbol} address access through the MCAsmLayout object.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99380 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-03-24 03:43:40 +00:00
parent 2250425d6e
commit 207e06ea04
7 changed files with 114 additions and 65 deletions

View File

@ -12,6 +12,9 @@
namespace llvm { namespace llvm {
class MCAssembler; class MCAssembler;
class MCFragment;
class MCSectionData;
class MCSymbolData;
/// Encapsulates the layout of an assembly file at a particular point in time. /// 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. /// Get the assembler object this is a layout for.
MCAssembler &getAssembler() const { return Assembler; } 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 } // end namespace llvm

View File

@ -699,15 +699,17 @@ public:
/// Find the symbol which defines the atom containing given address, inside /// Find the symbol which defines the atom containing given address, inside
/// the given section, or null if there is no such symbol. /// the given section, or null if there is no such symbol.
// //
// FIXME: Eliminate this, it is very slow. // FIXME-PERF: Eliminate this, it is very slow.
const MCSymbolData *getAtomForAddress(const MCSectionData *Section, const MCSymbolData *getAtomForAddress(const MCAsmLayout &Layout,
const MCSectionData *Section,
uint64_t Address) const; uint64_t Address) const;
/// Find the symbol which defines the atom containing the given symbol, or /// Find the symbol which defines the atom containing the given symbol, or
/// null if there is no such symbol. /// null if there is no such symbol.
// //
// FIXME: Eliminate this, it is very slow. // FIXME-PERF: Eliminate this, it is very slow.
const MCSymbolData *getAtom(const MCSymbolData *Symbol) const; const MCSymbolData *getAtom(const MCAsmLayout &Layout,
const MCSymbolData *Symbol) const;
/// Check whether a particular symbol is visible to the linker and is required /// 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 /// in the symbol table, or whether it can be discarded by the assembler. This

View File

@ -16,6 +16,7 @@
namespace llvm { namespace llvm {
class MCAsmFixup; class MCAsmFixup;
class MCAsmLayout;
class MCAssembler; class MCAssembler;
class MCFragment; class MCFragment;
class MCValue; class MCValue;
@ -69,6 +70,7 @@ public:
/// information about the relocation so that it can be emitted during /// information about the relocation so that it can be emitted during
/// WriteObject(). /// WriteObject().
virtual void RecordRelocation(const MCAssembler &Asm, virtual void RecordRelocation(const MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFragment *Fragment,
const MCAsmFixup &Fixup, MCValue Target, const MCAsmFixup &Fixup, MCValue Target,
uint64_t &FixedValue) = 0; uint64_t &FixedValue) = 0;
@ -78,7 +80,8 @@ public:
/// This routine is called by the assembler after layout and relaxation is /// This routine is called by the assembler after layout and relaxation is
/// complete, fixups have been evaluate and applied, and relocations /// complete, fixups have been evaluate and applied, and relocations
/// generated. /// generated.
virtual void WriteObject(const MCAssembler &Asm) = 0; virtual void WriteObject(const MCAssembler &Asm,
const MCAsmLayout &Layout) = 0;
/// @} /// @}
/// @name Binary Output /// @name Binary Output

View File

@ -31,11 +31,12 @@ public:
virtual void ExecutePostLayoutBinding(MCAssembler &Asm); virtual void ExecutePostLayoutBinding(MCAssembler &Asm);
virtual void RecordRelocation(const MCAssembler &Asm, virtual void RecordRelocation(const MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFragment *Fragment,
const MCAsmFixup &Fixup, MCValue Target, const MCAsmFixup &Fixup, MCValue Target,
uint64_t &FixedValue); uint64_t &FixedValue);
virtual void WriteObject(const MCAssembler &Asm); virtual void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout);
}; };
} // End llvm namespace } // End llvm namespace

View File

@ -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)) { MCFragment::MCFragment() : Kind(FragmentType(~0)) {
} }
@ -145,6 +163,7 @@ static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
} }
static bool isScatteredFixupFullyResolved(const MCAssembler &Asm, static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCAsmFixup &Fixup, const MCAsmFixup &Fixup,
const MCValue Target, const MCValue Target,
const MCSymbolData *BaseSymbol) { const MCSymbolData *BaseSymbol) {
@ -165,7 +184,7 @@ static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
if (A->getKind() != MCSymbolRefExpr::VK_None) if (A->getKind() != MCSymbolRefExpr::VK_None)
return false; return false;
A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol())); A_Base = Asm.getAtom(Layout, &Asm.getSymbolData(A->getSymbol()));
if (!A_Base) if (!A_Base)
return false; return false;
} }
@ -175,7 +194,7 @@ static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
if (B->getKind() != MCSymbolRefExpr::VK_None) if (B->getKind() != MCSymbolRefExpr::VK_None)
return false; return false;
B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol())); B_Base = Asm.getAtom(Layout, &Asm.getSymbolData(B->getSymbol()));
if (!B_Base) if (!B_Base)
return false; return false;
} }
@ -203,9 +222,13 @@ bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
SD->getFragment()->getParent()->getSection()); 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 { uint64_t Address) const {
const MCSymbolData *Best = 0; const MCSymbolData *Best = 0;
uint64_t BestAddress = 0;
for (MCAssembler::const_symbol_iterator it = symbol_begin(), for (MCAssembler::const_symbol_iterator it = symbol_begin(),
ie = symbol_end(); it != ie; ++it) { ie = symbol_end(); it != ie; ++it) {
// Ignore non-linker visible symbols. // 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 // Otherwise, find the closest symbol preceding this address (ties are
// resolved in favor of the last defined symbol). // resolved in favor of the last defined symbol).
if (it->getAddress() <= Address && uint64_t SymbolAddress = Layout.getSymbolAddress(it);
(!Best || it->getAddress() >= Best->getAddress())) if (SymbolAddress <= Address && (!Best || SymbolAddress >= BestAddress)) {
Best = it; Best = it;
BestAddress = SymbolAddress;
}
} }
return Best; 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. // Linker visible symbols define atoms.
if (isSymbolLinkerVisible(SD)) if (isSymbolLinkerVisible(SD))
return SD; return SD;
@ -236,7 +263,8 @@ const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
return 0; return 0;
// Otherwise, search by address. // 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, bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
@ -258,13 +286,13 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
bool IsResolved = true; bool IsResolved = true;
if (const MCSymbolRefExpr *A = Target.getSymA()) { if (const MCSymbolRefExpr *A = Target.getSymA()) {
if (A->getSymbol().isDefined()) if (A->getSymbol().isDefined())
Value += getSymbolData(A->getSymbol()).getAddress(); Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
else else
IsResolved = false; IsResolved = false;
} }
if (const MCSymbolRefExpr *B = Target.getSymB()) { if (const MCSymbolRefExpr *B = Target.getSymB()) {
if (B->getSymbol().isDefined()) if (B->getSymbol().isDefined())
Value -= getSymbolData(B->getSymbol()).getAddress(); Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
else else
IsResolved = false; IsResolved = false;
} }
@ -278,13 +306,13 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
const MCSymbolData *BaseSymbol = 0; const MCSymbolData *BaseSymbol = 0;
if (IsPCRel) { if (IsPCRel) {
BaseSymbol = getAtomForAddress( BaseSymbol = getAtomForAddress(
DF->getParent(), DF->getAddress() + Fixup.Offset); Layout, DF->getParent(), Layout.getFragmentAddress(DF)+Fixup.Offset);
if (!BaseSymbol) if (!BaseSymbol)
IsResolved = false; IsResolved = false;
} }
if (IsResolved) if (IsResolved)
IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target, IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
BaseSymbol); BaseSymbol);
} else { } else {
const MCSection *BaseSection = 0; const MCSection *BaseSection = 0;
@ -297,19 +325,19 @@ bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
} }
if (IsPCRel) if (IsPCRel)
Value -= DF->getAddress() + Fixup.Offset; Value -= Layout.getFragmentAddress(DF) + Fixup.Offset;
return IsResolved; return IsResolved;
} }
void MCAssembler::LayoutSection(MCSectionData &SD, void MCAssembler::LayoutSection(MCSectionData &SD,
MCAsmLayout &Layout) { 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) { for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
MCFragment &F = *it; MCFragment &F = *it;
F.setOffset(Address - SD.getAddress()); F.setOffset(Address - StartAddress);
// Evaluate fragment size. // Evaluate fragment size.
switch (F.getKind()) { 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 // Align the fragment offset; it is safe to adjust the offset freely since
// this is only in virtual sections. // this is only in virtual sections.
Address = RoundUpToAlignment(Address, ZFF.getAlignment()); Address = RoundUpToAlignment(Address, ZFF.getAlignment());
F.setOffset(Address - SD.getAddress()); F.setOffset(Address - StartAddress);
// FIXME: This is misnamed. // FIXME: This is misnamed.
F.setFileSize(ZFF.getSize()); F.setFileSize(ZFF.getSize());
@ -373,11 +401,11 @@ void MCAssembler::LayoutSection(MCSectionData &SD,
} }
// Set the section sizes. // Set the section sizes.
SD.setSize(Address - SD.getAddress()); SD.setSize(Address - StartAddress);
if (getBackend().isVirtualSection(SD.getSection())) if (getBackend().isVirtualSection(SD.getSection()))
SD.setFileSize(0); SD.setFileSize(0);
else else
SD.setFileSize(Address - SD.getAddress()); SD.setFileSize(Address - StartAddress);
} }
/// WriteFragmentData - Write the \arg F data to the output file. /// 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 // The fixup was unresolved, we need a relocation. Inform the object
// writer of the relocation, and give it an opportunity to adjust the // writer of the relocation, and give it an opportunity to adjust the
// fixup value if need be. // 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); getBackend().ApplyFixup(Fixup, *DF, FixedValue);
@ -552,7 +580,7 @@ void MCAssembler::Finish() {
} }
// Write the object file. // Write the object file.
Writer->WriteObject(*this); Writer->WriteObject(*this, Layout);
OS.flush(); OS.flush();
stats::ObjectBytes += OS.tell() - StartOffset; stats::ObjectBytes += OS.tell() - StartOffset;
@ -609,7 +637,7 @@ bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
} }
// Layout the section fragments and its size. // Layout the section fragments and its size.
SD.setAddress(Address); Layout.setSectionAddress(&SD, Address);
LayoutSection(SD, Layout); LayoutSection(SD, Layout);
Address += SD.getFileSize(); Address += SD.getFileSize();
@ -628,7 +656,7 @@ bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Address += Pad; Address += Pad;
SD.setAddress(Address); Layout.setSectionAddress(&SD, Address);
LayoutSection(SD, Layout); LayoutSection(SD, Layout);
Address += SD.getSize(); Address += SD.getSize();
} }

View File

@ -268,8 +268,8 @@ bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
Layout->getAssembler().getSymbolData(Res.getSymA()->getSymbol()); Layout->getAssembler().getSymbolData(Res.getSymA()->getSymbol());
MCSymbolData &B = MCSymbolData &B =
Layout->getAssembler().getSymbolData(Res.getSymB()->getSymbol()); Layout->getAssembler().getSymbolData(Res.getSymB()->getSymbol());
Res = MCValue::get(+ A.getFragment()->getAddress() + A.getOffset() Res = MCValue::get(+ Layout->getSymbolAddress(&A) + A.getOffset()
- B.getFragment()->getAddress() - B.getOffset() - Layout->getSymbolAddress(&B) - B.getOffset()
+ Res.getConstant()); + Res.getConstant());
} }

View File

@ -11,6 +11,7 @@
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h" #include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAssembler.h" #include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionMachO.h" #include "llvm/MC/MCSectionMachO.h"
@ -271,9 +272,9 @@ public:
assert(OS.tell() - Start == SegmentLoadCommandSize); assert(OS.tell() - Start == SegmentLoadCommandSize);
} }
void WriteSection(const MCAssembler &Asm, const MCSectionData &SD, void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
uint64_t FileOffset, uint64_t RelocationsStart, const MCSectionData &SD, uint64_t FileOffset,
unsigned NumRelocations) { uint64_t RelocationsStart, unsigned NumRelocations) {
// The offset is unused for virtual sections. // The offset is unused for virtual sections.
if (Asm.getBackend().isVirtualSection(SD.getSection())) { if (Asm.getBackend().isVirtualSection(SD.getSection())) {
assert(SD.getFileSize() == 0 && "Invalid file size!"); assert(SD.getFileSize() == 0 && "Invalid file size!");
@ -292,10 +293,10 @@ public:
WriteBytes(Section.getSectionName(), 16); WriteBytes(Section.getSectionName(), 16);
WriteBytes(Section.getSegmentName(), 16); WriteBytes(Section.getSegmentName(), 16);
if (Is64Bit) { if (Is64Bit) {
Write64(SD.getAddress()); // address Write64(Layout.getSectionAddress(&SD)); // address
Write64(SD.getSize()); // size Write64(SD.getSize()); // size
} else { } else {
Write32(SD.getAddress()); // address Write32(Layout.getSectionAddress(&SD)); // address
Write32(SD.getSize()); // size Write32(SD.getSize()); // size
} }
Write32(FileOffset); Write32(FileOffset);
@ -372,7 +373,7 @@ public:
assert(OS.tell() - Start == DysymtabLoadCommandSize); assert(OS.tell() - Start == DysymtabLoadCommandSize);
} }
void WriteNlist(MachSymbolData &MSD) { void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) {
MCSymbolData &Data = *MSD.SymbolData; MCSymbolData &Data = *MSD.SymbolData;
const MCSymbol &Symbol = Data.getSymbol(); const MCSymbol &Symbol = Data.getSymbol();
uint8_t Type = 0; uint8_t Type = 0;
@ -403,7 +404,7 @@ public:
if (Symbol.isAbsolute()) { if (Symbol.isAbsolute()) {
llvm_unreachable("FIXME: Not yet implemented!"); llvm_unreachable("FIXME: Not yet implemented!");
} else { } else {
Address = Data.getAddress(); Address = Layout.getSymbolAddress(&Data);
} }
} else if (Data.isCommon()) { } else if (Data.isCommon()) {
// Common symbols are encoded with the size in the address // 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 // - Input errors, where something cannot be correctly encoded. 'as' allows
// these through in many cases. // these through in many cases.
void RecordX86_64Relocation(const MCAssembler &Asm, void RecordX86_64Relocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFragment *Fragment,
const MCAsmFixup &Fixup, MCValue Target, const MCAsmFixup &Fixup, MCValue Target,
uint64_t &FixedValue) { uint64_t &FixedValue) {
@ -494,11 +495,11 @@ public:
} else if (Target.getSymB()) { // A - B + constant } else if (Target.getSymB()) { // A - B + constant
const MCSymbol *A = &Target.getSymA()->getSymbol(); const MCSymbol *A = &Target.getSymA()->getSymbol();
MCSymbolData &A_SD = Asm.getSymbolData(*A); 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(); const MCSymbol *B = &Target.getSymB()->getSymbol();
MCSymbolData &B_SD = Asm.getSymbolData(*B); 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. // Neither symbol can be modified.
if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
@ -521,8 +522,8 @@ public:
if (A_Base == B_Base) if (A_Base == B_Base)
llvm_report_error("unsupported relocation with identical base"); llvm_report_error("unsupported relocation with identical base");
Value += A_SD.getAddress() - A_Base->getAddress(); Value += Layout.getSymbolAddress(&A_SD) - Layout.getSymbolAddress(A_Base);
Value -= B_SD.getAddress() - B_Base->getAddress(); Value -= Layout.getSymbolAddress(&B_SD) - Layout.getSymbolAddress(B_Base);
Index = A_Base->getIndex(); Index = A_Base->getIndex();
IsExtern = 1; IsExtern = 1;
@ -543,7 +544,7 @@ public:
} else { } else {
const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
MCSymbolData &SD = Asm.getSymbolData(*Symbol); 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 // 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 // symbol to use as a base address (a local symbol with no preceeding
@ -554,7 +555,7 @@ public:
// Add the local offset, if needed. // Add the local offset, if needed.
if (Base != &SD) if (Base != &SD)
Value += SD.getAddress() - Base->getAddress(); Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
} else { } else {
// The index is the section ordinal. // The index is the section ordinal.
// //
@ -566,7 +567,7 @@ public:
break; break;
assert(it != ie && "Unable to find section index!"); assert(it != ie && "Unable to find section index!");
IsExtern = 0; IsExtern = 0;
Value += SD.getAddress(); Value += Layout.getSymbolAddress(&SD);
if (IsPCRel) if (IsPCRel)
Value -= Address + (1 << Log2Size); Value -= Address + (1 << Log2Size);
@ -640,6 +641,7 @@ public:
} }
void RecordScatteredRelocation(const MCAssembler &Asm, void RecordScatteredRelocation(const MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFragment *Fragment,
const MCAsmFixup &Fixup, MCValue Target, const MCAsmFixup &Fixup, MCValue Target,
uint64_t &FixedValue) { uint64_t &FixedValue) {
@ -656,7 +658,7 @@ public:
llvm_report_error("symbol '" + A->getName() + llvm_report_error("symbol '" + A->getName() +
"' can not be undefined in a subtraction expression"); "' 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; uint32_t Value2 = 0;
if (const MCSymbolRefExpr *B = Target.getSymB()) { if (const MCSymbolRefExpr *B = Target.getSymB()) {
@ -672,7 +674,7 @@ public:
// relocation types from the linkers point of view, this is done solely // relocation types from the linkers point of view, this is done solely
// for pedantic compatibility with 'as'. // for pedantic compatibility with 'as'.
Type = A_SD->isExternal() ? RIT_Difference : RIT_LocalDifference; 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. // Relocations are written out in reverse order, so the PAIR comes first.
@ -697,11 +699,11 @@ public:
Relocations[Fragment->getParent()].push_back(MRE); Relocations[Fragment->getParent()].push_back(MRE);
} }
void RecordRelocation(const MCAssembler &Asm, const MCFragment *Fragment, void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCAsmFixup &Fixup, MCValue Target, const MCFragment *Fragment, const MCAsmFixup &Fixup,
uint64_t &FixedValue) { MCValue Target, uint64_t &FixedValue) {
if (Is64Bit) { if (Is64Bit) {
RecordX86_64Relocation(Asm, Fragment, Fixup, Target, FixedValue); RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
return; return;
} }
@ -716,7 +718,7 @@ public:
if (Target.getSymB() || if (Target.getSymB() ||
(Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() && (Target.getSymA() && !Target.getSymA()->getSymbol().isUndefined() &&
Offset)) { Offset)) {
RecordScatteredRelocation(Asm, Fragment, Fixup, Target, FixedValue); RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,Target,FixedValue);
return; return;
} }
@ -752,7 +754,7 @@ public:
if (&*it == SD->getFragment()->getParent()) if (&*it == SD->getFragment()->getParent())
break; break;
assert(it != ie && "Unable to find section index!"); assert(it != ie && "Unable to find section index!");
Value = SD->getAddress(); Value = Layout.getSymbolAddress(SD);
} }
Type = RIT_Vanilla; Type = RIT_Vanilla;
@ -934,7 +936,7 @@ public:
UndefinedSymbolData); UndefinedSymbolData);
} }
void WriteObject(const MCAssembler &Asm) { void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout) {
unsigned NumSections = Asm.size(); unsigned NumSections = Asm.size();
// The section data starts after the header, the segment load command (and // The section data starts after the header, the segment load command (and
@ -962,16 +964,16 @@ public:
for (MCAssembler::const_iterator it = Asm.begin(), for (MCAssembler::const_iterator it = Asm.begin(),
ie = Asm.end(); it != ie; ++it) { ie = Asm.end(); it != ie; ++it) {
const MCSectionData &SD = *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())) if (Asm.getBackend().isVirtualSection(SD.getSection()))
continue; continue;
SectionDataSize = std::max(SectionDataSize, SectionDataSize = std::max(SectionDataSize, Address + SD.getSize());
SD.getAddress() + SD.getSize());
SectionDataFileSize = std::max(SectionDataFileSize, SectionDataFileSize = std::max(SectionDataFileSize,
SD.getAddress() + SD.getFileSize()); Address + SD.getFileSize());
} }
// The section data is padded to 4 bytes. // The section data is padded to 4 bytes.
@ -992,8 +994,8 @@ public:
ie = Asm.end(); it != ie; ++it) { ie = Asm.end(); it != ie; ++it) {
std::vector<MachRelocationEntry> &Relocs = Relocations[it]; std::vector<MachRelocationEntry> &Relocs = Relocations[it];
unsigned NumRelocs = Relocs.size(); unsigned NumRelocs = Relocs.size();
uint64_t SectionStart = SectionDataStart + it->getAddress(); uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it);
WriteSection(Asm, *it, SectionStart, RelocTableEnd, NumRelocs); WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
RelocTableEnd += NumRelocs * RelocationInfoSize; RelocTableEnd += NumRelocs * RelocationInfoSize;
} }
@ -1080,11 +1082,11 @@ public:
// Write the symbol table entries. // Write the symbol table entries.
for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) 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) 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) for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
WriteNlist(UndefinedSymbolData[i]); WriteNlist(UndefinedSymbolData[i], Layout);
// Write the string table. // Write the string table.
OS << StringTable.str(); OS << StringTable.str();
@ -1111,13 +1113,15 @@ void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
} }
void MachObjectWriter::RecordRelocation(const MCAssembler &Asm, void MachObjectWriter::RecordRelocation(const MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFragment *Fragment,
const MCAsmFixup &Fixup, MCValue Target, const MCAsmFixup &Fixup, MCValue Target,
uint64_t &FixedValue) { uint64_t &FixedValue) {
((MachObjectWriterImpl*) Impl)->RecordRelocation(Asm, Fragment, Fixup, ((MachObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
Target, FixedValue); Target, FixedValue);
} }
void MachObjectWriter::WriteObject(const MCAssembler &Asm) { void MachObjectWriter::WriteObject(const MCAssembler &Asm,
((MachObjectWriterImpl*) Impl)->WriteObject(Asm); const MCAsmLayout &Layout) {
((MachObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
} }