if (!isFragmentUpToDate(F))
return;
- // Otherwise, reset the last valid fragment to the predecessor of the
- // invalidated fragment.
+ // Otherwise, reset the last valid fragment to this fragment.
const MCSectionData &SD = *F->getParent();
- LastValidFragment[&SD] = F->getPrevNode();
+ LastValidFragment[&SD] = F;
}
void MCAsmLayout::EnsureValid(const MCFragment *F) const {
}
}
-uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
- EnsureValid(F);
- assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
- return F->EffectiveSize;
-}
-
uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
EnsureValid(F);
assert(F->Offset != ~UINT64_C(0) && "Address not set!");
uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
// The size is the last fragment's end offset.
const MCFragment &F = SD->getFragmentList().back();
- return getFragmentOffset(&F) + getFragmentEffectiveSize(&F);
+ return getFragmentOffset(&F) + getAssembler().ComputeFragmentSize(F);
}
uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
}
MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
- : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0)),
- EffectiveSize(~UINT64_C(0))
+ : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
{
if (Parent)
Parent->getFragmentList().push_back(this);
Value = Target.getConstant();
- bool IsPCRel = Emitter.getFixupKindInfo(
+ bool IsPCRel = Backend.getFixupKindInfo(
Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
bool IsResolved = true;
+ bool IsThumb = false;
if (const MCSymbolRefExpr *A = Target.getSymA()) {
const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
if (Sym.isDefined())
Value += Layout.getSymbolOffset(&getSymbolData(Sym));
else
IsResolved = false;
+ if (isThumbFunc(&Sym))
+ IsThumb = true;
}
if (const MCSymbolRefExpr *B = Target.getSymB()) {
const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
if (IsResolved)
IsResolved = Writer.IsFixupFullyResolved(*this, Target, IsPCRel, DF);
- if (IsPCRel)
- Value -= Layout.getFragmentOffset(DF) + Fixup.getOffset();
+ bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
+ MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
+ assert((ShouldAlignPC ? IsPCRel : true) &&
+ "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
+
+ if (IsPCRel) {
+ uint32_t Offset = Fixup.getOffset();
+
+ // A number of ARM fixups in Thumb mode require that the effective PC
+ // address be determined as the 32-bit aligned version of the actual offset.
+ if (ShouldAlignPC) Offset &= ~0x3;
+ Value -= Layout.getFragmentOffset(DF) + Offset;
+ }
+
+ // ARM fixups based from a thumb function address need to have the low
+ // bit set. The actual value is always at least 16-bit aligned, so the
+ // low bit is normally clear and available for use as an ISA flag for
+ // interworking.
+ if (IsThumb)
+ Value |= 1;
return IsResolved;
}
-uint64_t MCAssembler::ComputeFragmentSize(const MCFragment &F,
- uint64_t FragmentOffset) const {
+uint64_t MCAssembler::ComputeFragmentSize(const MCFragment &F) const {
switch (F.getKind()) {
case MCFragment::FT_Data:
return cast<MCDataFragment>(F).getContents().size();
case MCFragment::FT_LEB:
return cast<MCLEBFragment>(F).getContents().size();
- case MCFragment::FT_Align: {
- const MCAlignFragment &AF = cast<MCAlignFragment>(F);
-
- uint64_t Size = OffsetToAlignment(FragmentOffset, AF.getAlignment());
-
- // Honor MaxBytesToEmit.
- if (Size > AF.getMaxBytesToEmit())
- return 0;
-
- return Size;
- }
+ case MCFragment::FT_Align:
+ return cast<MCAlignFragment>(F).getSize();
case MCFragment::FT_Org:
return cast<MCOrgFragment>(F).getSize();
// Compute fragment offset and size.
uint64_t Offset = 0;
if (Prev)
- Offset += Prev->Offset + Prev->EffectiveSize;
+ Offset += Prev->Offset + getAssembler().ComputeFragmentSize(*Prev);
F->Offset = Offset;
- F->EffectiveSize = getAssembler().ComputeFragmentSize(*F, F->Offset);
LastValidFragment[F->getParent()] = F;
}
++stats::EmittedFragments;
// FIXME: Embed in fragments instead?
- uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
+ uint64_t FragmentSize = Asm.ComputeFragmentSize(F);
switch (F.getKind()) {
case MCFragment::FT_Align: {
MCAlignFragment &AF = cast<MCAlignFragment>(F);
return OldSize != Data.size();
}
+bool MCAssembler::RelaxAlignment(const MCObjectWriter &Writer,
+ MCAsmLayout &Layout,
+ MCAlignFragment &AF) {
+ unsigned Offset = Layout.getFragmentOffset(&AF);
+ unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
+ if (Size > AF.getMaxBytesToEmit())
+ Size = 0;
+ unsigned OldSize = AF.getSize();
+ AF.setSize(Size);
+ return OldSize != Size;
+}
+
bool MCAssembler::LayoutOnce(const MCObjectWriter &Writer,
MCAsmLayout &Layout) {
++stats::RelaxationSteps;
bool WasRelaxed = false;
for (iterator it = begin(), ie = end(); it != ie; ++it) {
MCSectionData &SD = *it;
+ MCFragment *FirstInvalidFragment = NULL;
for (MCSectionData::iterator it2 = SD.begin(),
ie2 = SD.end(); it2 != ie2; ++it2) {
switch(it2->getKind()) {
default:
break;
+ case MCFragment::FT_Align:
+ relaxedFrag = RelaxAlignment(Writer, Layout,
+ *cast<MCAlignFragment>(it2));
+ break;
case MCFragment::FT_Inst:
relaxedFrag = RelaxInstruction(Writer, Layout,
*cast<MCInstFragment>(it2));
break;
}
// Update the layout, and remember that we relaxed.
- if (relaxedFrag)
- Layout.Invalidate(it2);
+ if (relaxedFrag && !FirstInvalidFragment)
+ FirstInvalidFragment = it2;
WasRelaxed |= relaxedFrag;
}
+ if (FirstInvalidFragment)
+ Layout.Invalidate(FirstInvalidFragment);
}
return WasRelaxed;
}
OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
- << " Offset:" << Offset << " EffectiveSize:" << EffectiveSize << ">";
+ << " Offset:" << Offset << ">";
switch (getKind()) {
case MCFragment::FT_Align: {