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::getSectionAddressSize(const MCSectionData *SD) const {
+ // Empty sections have no size.
+ if (SD->getFragmentList().empty())
+ return 0;
+
+ // Otherwise, the size is the last fragment's end offset.
+ const MCFragment &F = SD->getFragmentList().back();
+ return getFragmentOffset(&F) + getFragmentEffectiveSize(&F);
}
uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
- assert(SD->FileSize != ~UINT64_C(0) && "File size not set!");
- return SD->FileSize;
+ // Virtual sections have no file size.
+ if (getAssembler().getBackend().isVirtualSection(SD->getSection()))
+ return 0;
+
+ // Otherwise, the file size is the same as the address space size.
+ return getSectionAddressSize(SD);
}
-void MCAsmLayout::setSectionFileSize(MCSectionData *SD, uint64_t Value) {
- SD->FileSize = Value;
+
+uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
+ // Empty sections have no size.
+ if (SD->getFragmentList().empty())
+ return 0;
+
+ // The logical size is the address space size minus any tail padding.
+ uint64_t Size = getSectionAddressSize(SD);
+ const MCAlignFragment *AF =
+ dyn_cast<MCAlignFragment>(&(SD->getFragmentList().back()));
+ if (AF && AF->hasOnlyAlignAddress())
+ Size -= getFragmentEffectiveSize(AF);
+
+ return Size;
}
/* *** */
: Section(&_Section),
Alignment(1),
Address(~UINT64_C(0)),
- Size(~UINT64_C(0)),
- FileSize(~UINT64_C(0)),
HasInstructions(false)
{
if (A)
return IsResolved;
}
-void MCAssembler::LayoutSection(MCAsmLayout &Layout,
- unsigned SectionOrderIndex) {
- MCSectionData &SD = *Layout.getSectionOrder()[SectionOrderIndex];
- bool IsVirtual = getBackend().isVirtualSection(SD.getSection());
-
- ++stats::SectionLayouts;
-
- // Get the section start address.
- uint64_t StartAddress = 0;
- if (SectionOrderIndex) {
- MCSectionData *Prev = Layout.getSectionOrder()[SectionOrderIndex - 1];
- StartAddress = Layout.getSectionAddress(Prev) + Layout.getSectionSize(Prev);
- }
-
- // Align this section if necessary by adding padding bytes to the previous
- // section. It is safe to adjust this out-of-band, because no symbol or
- // fragment is allowed to point past the end of the section at any time.
- if (uint64_t Pad = OffsetToAlignment(StartAddress, SD.getAlignment())) {
- // Unless this section is virtual (where we are allowed to adjust the offset
- // freely), the padding goes in the previous section.
- if (!IsVirtual) {
- // Find the previous non-virtual section.
- iterator it = &SD;
- assert(it != begin() && "Invalid initial section address!");
- for (--it; getBackend().isVirtualSection(it->getSection()); --it) ;
- Layout.setSectionFileSize(&*it, Layout.getSectionFileSize(&*it) + Pad);
- }
+void MCAssembler::LayoutFragment(MCAsmLayout &Layout, MCFragment &F) {
+ uint64_t StartAddress = Layout.getSectionAddress(F.getParent());
- StartAddress += Pad;
- }
+ // Get the fragment start address.
+ uint64_t Address = StartAddress;
+ MCSectionData::iterator it = &F;
+ if (MCFragment *Prev = F.getPrevNode())
+ Address = (StartAddress + Layout.getFragmentOffset(Prev) +
+ Layout.getFragmentEffectiveSize(Prev));
- // Set the aligned section address.
- Layout.setSectionAddress(&SD, StartAddress);
+ ++stats::FragmentLayouts;
- for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
- MCFragment &F = *it;
+ uint64_t FragmentOffset = Address - StartAddress;
+ Layout.setFragmentOffset(&F, FragmentOffset);
- // Compute the fragment start address.
- uint64_t Address = StartAddress;
- if (MCFragment *Prev = F.getPrevNode())
- Address = (Layout.getFragmentAddress(Prev) +
- Layout.getFragmentEffectiveSize(Prev));
+ // Evaluate fragment size.
+ uint64_t EffectiveSize = 0;
+ switch (F.getKind()) {
+ case MCFragment::FT_Align: {
+ MCAlignFragment &AF = cast<MCAlignFragment>(F);
- ++stats::FragmentLayouts;
+ assert((!AF.hasOnlyAlignAddress() || !AF.getNextNode()) &&
+ "Invalid OnlyAlignAddress bit, not the last fragment!");
- uint64_t FragmentOffset = Address - StartAddress;
- Layout.setFragmentOffset(&F, FragmentOffset);
+ EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
+ if (EffectiveSize > AF.getMaxBytesToEmit())
+ EffectiveSize = 0;
+ break;
+ }
- // Evaluate fragment size.
- uint64_t EffectiveSize = 0;
- switch (F.getKind()) {
- case MCFragment::FT_Align: {
- MCAlignFragment &AF = cast<MCAlignFragment>(F);
+ case MCFragment::FT_Data:
+ EffectiveSize = cast<MCDataFragment>(F).getContents().size();
+ break;
- EffectiveSize = OffsetToAlignment(Address, AF.getAlignment());
- if (EffectiveSize > AF.getMaxBytesToEmit())
- EffectiveSize = 0;
- break;
- }
+ case MCFragment::FT_Fill: {
+ EffectiveSize = cast<MCFillFragment>(F).getSize();
+ break;
+ }
- case MCFragment::FT_Data:
- EffectiveSize = cast<MCDataFragment>(F).getContents().size();
- break;
+ case MCFragment::FT_Inst:
+ EffectiveSize = cast<MCInstFragment>(F).getInstSize();
+ break;
- case MCFragment::FT_Fill: {
- MCFillFragment &FF = cast<MCFillFragment>(F);
- EffectiveSize = FF.getValueSize() * FF.getCount();
- break;
- }
+ case MCFragment::FT_Org: {
+ MCOrgFragment &OF = cast<MCOrgFragment>(F);
- case MCFragment::FT_Inst:
- EffectiveSize = cast<MCInstFragment>(F).getInstSize();
- break;
+ int64_t TargetLocation;
+ if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
+ report_fatal_error("expected assembly-time absolute expression");
- case MCFragment::FT_Org: {
- MCOrgFragment &OF = cast<MCOrgFragment>(F);
+ // FIXME: We need a way to communicate this error.
+ int64_t Offset = TargetLocation - FragmentOffset;
+ if (Offset < 0)
+ report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
+ "' (at offset '" + Twine(FragmentOffset) + "'");
- int64_t TargetLocation;
- if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
- report_fatal_error("expected assembly-time absolute expression");
+ EffectiveSize = Offset;
+ break;
+ }
+ }
- // FIXME: We need a way to communicate this error.
- int64_t Offset = TargetLocation - FragmentOffset;
- if (Offset < 0)
- report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
- "' (at offset '" + Twine(FragmentOffset) + "'");
+ Layout.setFragmentEffectiveSize(&F, EffectiveSize);
+}
- EffectiveSize = Offset;
- break;
- }
+void MCAssembler::LayoutSection(MCAsmLayout &Layout,
+ unsigned SectionOrderIndex) {
+ MCSectionData &SD = *Layout.getSectionOrder()[SectionOrderIndex];
- case MCFragment::FT_ZeroFill: {
- MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
+ ++stats::SectionLayouts;
- // Align the fragment offset; it is safe to adjust the offset freely since
- // this is only in virtual sections.
- //
- // FIXME: We shouldn't be doing this here.
- Address = RoundUpToAlignment(Address, ZFF.getAlignment());
- Layout.setFragmentOffset(&F, Address - StartAddress);
+ // Compute the section start address.
+ uint64_t StartAddress = 0;
+ if (SectionOrderIndex) {
+ MCSectionData *Prev = Layout.getSectionOrder()[SectionOrderIndex - 1];
+ StartAddress = (Layout.getSectionAddress(Prev) +
+ Layout.getSectionAddressSize(Prev));
+ }
- EffectiveSize = ZFF.getSize();
- break;
- }
- }
+ // Honor the section alignment requirements.
+ StartAddress = RoundUpToAlignment(StartAddress, SD.getAlignment());
- Layout.setFragmentEffectiveSize(&F, EffectiveSize);
- }
+ // Set the section address.
+ Layout.setSectionAddress(&SD, StartAddress);
- // Set the section sizes.
- uint64_t Size = 0;
- if (!SD.getFragmentList().empty()) {
- MCFragment *F = &SD.getFragmentList().back();
- Size = Layout.getFragmentOffset(F) + Layout.getFragmentEffectiveSize(F);
- }
- Layout.setSectionSize(&SD, Size);
- Layout.setSectionFileSize(&SD, IsVirtual ? 0 : Size);
+ for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it)
+ LayoutFragment(Layout, *it);
}
/// WriteFragmentData - Write the \arg F data to the output file.
MCAlignFragment &AF = cast<MCAlignFragment>(F);
uint64_t Count = FragmentSize / AF.getValueSize();
+ assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
+
// FIXME: This error shouldn't actually occur (the front end should emit
// multiple .align directives to enforce the semantics it wants), but is
// severe enough that we want to report it. How to handle this?
// the Count bytes. Then if that did not fill any bytes or there are any
// bytes left to fill use the the Value and ValueSize to fill the rest.
// If we are aligning with nops, ask that target to emit the right data.
- if (AF.getEmitNops()) {
+ if (AF.hasEmitNops()) {
if (!Asm.getBackend().WriteNopData(Count, OW))
report_fatal_error("unable to write nop sequence of " +
Twine(Count) + " bytes");
case MCFragment::FT_Fill: {
MCFillFragment &FF = cast<MCFillFragment>(F);
- for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
+
+ assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
+
+ for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
switch (FF.getValueSize()) {
default:
assert(0 && "Invalid size!");
break;
}
-
- case MCFragment::FT_ZeroFill: {
- assert(0 && "Invalid zero fill fragment in concrete section!");
- break;
- }
}
assert(OW->getStream().tell() - Start == FragmentSize);
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(SectionFileSize == 0 && "Invalid size for section!");
+ assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
+
+ // Check that contents are only things legal inside a virtual section.
+ for (MCSectionData::const_iterator it = SD->begin(),
+ ie = SD->end(); it != ie; ++it) {
+ switch (it->getKind()) {
+ default:
+ assert(0 && "Invalid fragment in virtual section!");
+ case MCFragment::FT_Align:
+ assert(!cast<MCAlignFragment>(it)->getValueSize() &&
+ "Invalid align in virtual section!");
+ break;
+ case MCFragment::FT_Fill:
+ assert(!cast<MCFillFragment>(it)->getValueSize() &&
+ "Invalid fill in virtual section!");
+ break;
+ }
+ }
+
return;
}
ie = SD->end(); it != ie; ++it)
WriteFragmentData(*this, Layout, *it, OW);
- // Add section padding.
- assert(SectionFileSize >= SectionSize && "Invalid section sizes!");
- OW->WriteZeros(SectionFileSize - SectionSize);
-
- assert(OW->getStream().tell() - Start == SectionFileSize);
+ assert(OW->getStream().tell() - Start == Layout.getSectionFileSize(SD));
}
void MCAssembler::Finish() {
it2->setOrdinal(FragmentIndex++);
}
- // Layout until everything fits.
+ // Create the layout object.
MCAsmLayout Layout(*this);
+
+ // Insert additional align fragments for concrete sections to explicitly pad
+ // the previous section to match their alignment requirements. This is for
+ // 'gas' compatibility, it shouldn't strictly be necessary.
+ //
+ // FIXME: This may be Mach-O specific.
+ for (unsigned i = 1, e = Layout.getSectionOrder().size(); i < e; ++i) {
+ MCSectionData *SD = Layout.getSectionOrder()[i];
+
+ // Ignore sections without alignment requirements.
+ unsigned Align = SD->getAlignment();
+ if (Align <= 1)
+ continue;
+
+ // Ignore virtual sections, they don't cause file size modifications.
+ if (getBackend().isVirtualSection(SD->getSection()))
+ continue;
+
+ // Otherwise, create a new align fragment at the end of the previous
+ // section.
+ MCAlignFragment *AF = new MCAlignFragment(Align, 0, 1, Align,
+ Layout.getSectionOrder()[i - 1]);
+ AF->setOnlyAlignAddress(true);
+ }
+
+ // Layout until everything fits.
while (LayoutOnce(Layout))
continue;
raw_ostream &OS = llvm::errs();
OS << "<MCFragment " << (void*) this << " Offset:" << Offset
- << " EffectiveSize:" << EffectiveSize;
-
- OS << ">";
+ << " EffectiveSize:" << EffectiveSize << ">";
}
void MCAlignFragment::dump() {
OS << "<MCAlignFragment ";
this->MCFragment::dump();
+ if (hasEmitNops())
+ OS << " (emit nops)";
+ if (hasOnlyAlignAddress())
+ OS << " (only align section)";
OS << "\n ";
OS << " Alignment:" << getAlignment()
<< " Value:" << getValue() << " ValueSize:" << getValueSize()
this->MCFragment::dump();
OS << "\n ";
OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
- << " Count:" << getCount() << ">";
+ << " Size:" << getSize() << ">";
}
void MCInstFragment::dump() {
OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
}
-void MCZeroFillFragment::dump() {
- raw_ostream &OS = llvm::errs();
-
- OS << "<MCZeroFillFragment ";
- this->MCFragment::dump();
- OS << "\n ";
- OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
-}
-
void MCSectionData::dump() {
raw_ostream &OS = llvm::errs();
OS << "<MCSectionData";
OS << " Alignment:" << getAlignment() << " Address:" << Address
- << " Size:" << Size << " FileSize:" << FileSize
<< " Fragments:[\n ";
for (iterator it = begin(), ie = end(); it != ie; ++it) {
if (it != begin()) OS << ",\n ";