MC: Route access to SectionData offset and file size through MCAsmLayout.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99474 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar 2010-03-25 02:00:07 +00:00
parent 432cd5fd9b
commit 5d428511ca
4 changed files with 86 additions and 42 deletions

View File

@ -33,19 +33,58 @@ public:
/// Get the assembler object this is a layout for.
MCAssembler &getAssembler() const { return Assembler; }
uint64_t getFragmentAddress(const MCFragment *F) const;
/// @name Fragment Layout Data
/// @{
/// \brief Get the effective size of the given fragment, as computed in the
/// current layout.
uint64_t getFragmentEffectiveSize(const MCFragment *F) const;
/// \brief Set the effective size of the given fragment.
void setFragmentEffectiveSize(MCFragment *F, uint64_t Value);
/// \brief Get the offset of the given fragment inside its containing section.
uint64_t getFragmentOffset(const MCFragment *F) const;
/// \brief Set the offset of the given fragment inside its containing section.
void setFragmentOffset(MCFragment *F, uint64_t Value);
/// @}
/// @name Section Layout Data
/// @{
/// \brief Get the computed address of the given section.
uint64_t getSectionAddress(const MCSectionData *SD) const;
/// \brief Set the computed address of the given section.
void setSectionAddress(MCSectionData *SD, uint64_t Value);
/// \brief Get the data size of the given section, as emitted to the object
/// file. This may include additional padding, or be 0 for virtual sections.
uint64_t getSectionFileSize(const MCSectionData *SD) const;
/// \brief Set the data size of the given section.
void setSectionFileSize(MCSectionData *SD, uint64_t Value);
/// \brief Get the actual data size of the given section.
uint64_t getSectionSize(const MCSectionData *SD) const;
/// \brief Set the actual data size of the given section.
void setSectionSize(MCSectionData *SD, uint64_t Value);
/// @}
/// @name Utility Functions
/// @{
/// \brief Get the address of the given fragment, as computed in the current
/// layout.
uint64_t getFragmentAddress(const MCFragment *F) const;
/// \brief Get the address of the given symbol, as computed in the current
/// layout.
uint64_t getSymbolAddress(const MCSymbolData *SD) const;
void setSectionAddress(MCSectionData *SD, uint64_t Value);
/// @}
};
} // end namespace llvm

View File

@ -425,6 +425,9 @@ public:
unsigned getAlignment() const { return Alignment; }
void setAlignment(unsigned Value) { Alignment = Value; }
bool hasInstructions() const { return HasInstructions; }
void setHasInstructions(bool Value) { HasInstructions = Value; }
/// @name Fragment Access
/// @{
@ -447,29 +450,6 @@ public:
bool empty() const { return Fragments.empty(); }
/// @}
/// @name Assembler Backend Support
/// @{
//
// FIXME: This could all be kept private to the assembler implementation.
uint64_t getSize() const {
assert(Size != ~UINT64_C(0) && "File size not set!");
return Size;
}
void setSize(uint64_t Value) { Size = Value; }
uint64_t getFileSize() const {
assert(FileSize != ~UINT64_C(0) && "File size not set!");
return FileSize;
}
void setFileSize(uint64_t Value) { FileSize = Value; }
bool hasInstructions() const { return HasInstructions; }
void setHasInstructions(bool Value) { HasInstructions = Value; }
/// @}
void dump();
};

View File

@ -82,6 +82,24 @@ void MCAsmLayout::setSectionAddress(MCSectionData *SD, uint64_t Value) {
SD->Address = Value;
}
uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
assert(SD->Size != ~UINT64_C(0) && "File size not set!");
return SD->Size;
}
void MCAsmLayout::setSectionSize(MCSectionData *SD, uint64_t Value) {
SD->Size = Value;
}
uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
assert(SD->FileSize != ~UINT64_C(0) && "File size not set!");
return SD->FileSize;
}
void MCAsmLayout::setSectionFileSize(MCSectionData *SD, uint64_t Value) {
SD->FileSize = Value;
}
/// @}
/* *** */
MCFragment::MCFragment() : Kind(FragmentType(~0)) {
@ -419,11 +437,11 @@ void MCAssembler::LayoutSection(MCSectionData &SD,
}
// Set the section sizes.
SD.setSize(Address - StartAddress);
Layout.setSectionSize(&SD, Address - StartAddress);
if (getBackend().isVirtualSection(SD.getSection()))
SD.setFileSize(0);
Layout.setSectionFileSize(&SD, 0);
else
SD.setFileSize(Address - StartAddress);
Layout.setSectionFileSize(&SD, Address - StartAddress);
}
/// WriteFragmentData - Write the \arg F data to the output file.
@ -522,9 +540,12 @@ static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
void MCAssembler::WriteSectionData(const MCSectionData *SD,
const MCAsmLayout &Layout,
MCObjectWriter *OW) const {
uint64_t SectionSize = Layout.getSectionSize(SD);
uint64_t SectionFileSize = Layout.getSectionFileSize(SD);
// Ignore virtual sections.
if (getBackend().isVirtualSection(SD->getSection())) {
assert(SD->getFileSize() == 0);
assert(SectionFileSize == 0 && "Invalid size for section!");
return;
}
@ -536,10 +557,10 @@ void MCAssembler::WriteSectionData(const MCSectionData *SD,
WriteFragmentData(*this, Layout, *it, OW);
// Add section padding.
assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
OW->WriteZeros(SD->getFileSize() - SD->getSize());
assert(SectionFileSize >= SectionSize && "Invalid section sizes!");
OW->WriteZeros(SectionFileSize - SectionSize);
assert(OW->getStream().tell() - Start == SD->getFileSize());
assert(OW->getStream().tell() - Start == SectionFileSize);
}
void MCAssembler::Finish() {
@ -652,14 +673,14 @@ bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
// section.
if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
assert(Prev && "Missing prev section!");
Prev->setFileSize(Prev->getFileSize() + Pad);
Layout.setSectionFileSize(Prev, Layout.getSectionFileSize(Prev) + Pad);
Address += Pad;
}
// Layout the section fragments and its size.
Layout.setSectionAddress(&SD, Address);
LayoutSection(SD, Layout);
Address += SD.getFileSize();
Address += Layout.getSectionFileSize(&SD);
Prev = &SD;
}
@ -678,7 +699,7 @@ bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Layout.setSectionAddress(&SD, Address);
LayoutSection(SD, Layout);
Address += SD.getSize();
Address += Layout.getSectionSize(&SD);
}
// Scan for fragments that need relaxation.

View File

@ -275,9 +275,12 @@ public:
void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
const MCSectionData &SD, uint64_t FileOffset,
uint64_t RelocationsStart, unsigned NumRelocations) {
uint64_t SectionSize = Layout.getSectionSize(&SD);
uint64_t SectionFileSize = Layout.getSectionFileSize(&SD);
// The offset is unused for virtual sections.
if (Asm.getBackend().isVirtualSection(SD.getSection())) {
assert(SD.getFileSize() == 0 && "Invalid file size!");
assert(SectionFileSize == 0 && "Invalid file size!");
FileOffset = 0;
}
@ -294,10 +297,10 @@ public:
WriteBytes(Section.getSegmentName(), 16);
if (Is64Bit) {
Write64(Layout.getSectionAddress(&SD)); // address
Write64(SD.getSize()); // size
Write64(SectionSize); // size
} else {
Write32(Layout.getSectionAddress(&SD)); // address
Write32(SD.getSize()); // size
Write32(SectionSize); // size
}
Write32(FileOffset);
@ -965,15 +968,16 @@ public:
ie = Asm.end(); it != ie; ++it) {
const MCSectionData &SD = *it;
uint64_t Address = Layout.getSectionAddress(&SD);
uint64_t Size = Layout.getSectionSize(&SD);
uint64_t FileSize = Layout.getSectionFileSize(&SD);
VMSize = std::max(VMSize, Address + SD.getSize());
VMSize = std::max(VMSize, Address + Size);
if (Asm.getBackend().isVirtualSection(SD.getSection()))
continue;
SectionDataSize = std::max(SectionDataSize, Address + SD.getSize());
SectionDataFileSize = std::max(SectionDataFileSize,
Address + SD.getFileSize());
SectionDataSize = std::max(SectionDataSize, Address + Size);
SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
}
// The section data is padded to 4 bytes.