else { // EndOfFragment > BundleSize
return 2 * BundleSize - EndOfFragment;
}
- } else if (EndOfFragment > BundleSize)
+ } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
return BundleSize - OffsetInBundle;
else
return 0;
/* *** */
-MCFragment::MCFragment() : Kind(FragmentType(~0)) {
+void ilist_node_traits<MCFragment>::deleteNode(MCFragment *V) {
+ V->destroy();
}
-MCFragment::~MCFragment() {
+MCFragment::MCFragment() : Kind(FragmentType(~0)), HasInstructions(false),
+ AlignToBundleEnd(false), BundlePadding(0) {
}
-MCFragment::MCFragment(FragmentType Kind, MCSection *Parent)
- : Kind(Kind), Parent(Parent), Atom(nullptr), Offset(~UINT64_C(0)) {
+MCFragment::~MCFragment() { }
+
+MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
+ uint8_t BundlePadding, MCSection *Parent)
+ : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false),
+ BundlePadding(BundlePadding), Parent(Parent), Atom(nullptr),
+ Offset(~UINT64_C(0)) {
if (Parent)
Parent->getFragmentList().push_back(this);
}
-/* *** */
-
-MCEncodedFragment::~MCEncodedFragment() {
-}
-
-/* *** */
+void MCFragment::destroy() {
+ // First check if we are the sentinal.
+ if (Kind == FragmentType(~0)) {
+ delete this;
+ return;
+ }
-MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() {
+ switch (Kind) {
+ case FT_Align:
+ delete cast<MCAlignFragment>(this);
+ return;
+ case FT_Data:
+ delete cast<MCDataFragment>(this);
+ return;
+ case FT_CompactEncodedInst:
+ delete cast<MCCompactEncodedInstFragment>(this);
+ return;
+ case FT_Fill:
+ delete cast<MCFillFragment>(this);
+ return;
+ case FT_Relaxable:
+ delete cast<MCRelaxableFragment>(this);
+ return;
+ case FT_Org:
+ delete cast<MCOrgFragment>(this);
+ return;
+ case FT_Dwarf:
+ delete cast<MCDwarfLineAddrFragment>(this);
+ return;
+ case FT_DwarfFrame:
+ delete cast<MCDwarfCallFrameFragment>(this);
+ return;
+ case FT_LEB:
+ delete cast<MCLEBFragment>(this);
+ return;
+ case FT_SafeSEH:
+ delete cast<MCSafeSEHFragment>(this);
+ return;
+ }
}
/* *** */
return true;
}
-void MCAssembler::addLocalUsedInReloc(const MCSymbol &Sym) {
- assert(Sym.isTemporary());
- LocalsUsedInReloc.insert(&Sym);
-}
-
-bool MCAssembler::isLocalUsedInReloc(const MCSymbol &Sym) const {
- assert(Sym.isTemporary());
- return LocalsUsedInReloc.count(&Sym);
-}
-
bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
// Non-temporary labels should always be visible to the linker.
if (!Symbol.isTemporary())
if (!Symbol.isInSection())
return false;
- if (isLocalUsedInReloc(Symbol))
+ if (Symbol.isUsedInReloc())
return true;
return false;
MCValue &Target, uint64_t &Value) const {
++stats::evaluateFixup;
- // FIXME: This code has some duplication with RecordRelocation. We should
+ // FIXME: This code has some duplication with recordRelocation. We should
// probably merge the two into a single callback that tries to evaluate a
// fixup and records a relocation if one is needed.
const MCExpr *Expr = Fixup.getValue();
if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
IsResolved = false;
} else {
- IsResolved = getWriter().IsSymbolRefDifferenceFullyResolvedImpl(
+ IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl(
*this, SA, *DF, false, true);
}
}
const MCFragment &F) const {
switch (F.getKind()) {
case MCFragment::FT_Data:
+ return cast<MCDataFragment>(F).getContents().size();
case MCFragment::FT_Relaxable:
+ return cast<MCRelaxableFragment>(F).getContents().size();
case MCFragment::FT_CompactEncodedInst:
- return cast<MCEncodedFragment>(F).getContents().size();
+ return cast<MCCompactEncodedInstFragment>(F).getContents().size();
case MCFragment::FT_Fill:
return cast<MCFillFragment>(F).getSize();
// size won't include the padding.
//
// When the -mc-relax-all flag is used, we optimize bundling by writting the
- // bundle padding directly into fragments when the instructions are emitted
- // inside the streamer.
+ // padding directly into fragments when the instructions are emitted inside
+ // the streamer. When the fragment is larger than the bundle size, we need to
+ // ensure that it's bundle aligned. This means that if we end up with
+ // multiple fragments, we must emit bundle padding between fragments.
+ //
+ // ".align N" is an example of a directive that introduces multiple
+ // fragments. We could add a special case to handle ".align N" by emitting
+ // within-fragment padding (which would produce less padding when N is less
+ // than the bundle size), but for now we don't.
//
- if (Assembler.isBundlingEnabled() && !Assembler.getRelaxAll() &&
- F->hasInstructions()) {
+ if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
assert(isa<MCEncodedFragment>(F) &&
"Only MCEncodedFragment implementations have instructions");
uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
- if (FSize > Assembler.getBundleAlignSize())
+ if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
report_fatal_error("Fragment can't be larger than a bundle size");
uint64_t RequiredBundlePadding = computeBundlePadding(Assembler, F,
}
}
-/// \brief Write the contents of a fragment to the given object writer. Expects
-/// a MCEncodedFragment.
-static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
- const MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
- OW->WriteBytes(EF.getContents());
+void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
+ bool New = !Symbol.isRegistered();
+ if (Created)
+ *Created = New;
+ if (New) {
+ Symbol.setIsRegistered(true);
+ Symbols.push_back(&Symbol);
+ }
}
void MCAssembler::writeFragmentPadding(const MCFragment &F, uint64_t FSize,
for (uint64_t i = 0; i != Count; ++i) {
switch (AF.getValueSize()) {
default: llvm_unreachable("Invalid size!");
- case 1: OW->Write8 (uint8_t (AF.getValue())); break;
- case 2: OW->Write16(uint16_t(AF.getValue())); break;
- case 4: OW->Write32(uint32_t(AF.getValue())); break;
- case 8: OW->Write64(uint64_t(AF.getValue())); break;
+ case 1: OW->write8 (uint8_t (AF.getValue())); break;
+ case 2: OW->write16(uint16_t(AF.getValue())); break;
+ case 4: OW->write32(uint32_t(AF.getValue())); break;
+ case 8: OW->write64(uint64_t(AF.getValue())); break;
}
}
break;
case MCFragment::FT_Data:
++stats::EmittedDataFragments;
- writeFragmentContents(F, OW);
+ OW->writeBytes(cast<MCDataFragment>(F).getContents());
break;
case MCFragment::FT_Relaxable:
++stats::EmittedRelaxableFragments;
- writeFragmentContents(F, OW);
+ OW->writeBytes(cast<MCRelaxableFragment>(F).getContents());
break;
case MCFragment::FT_CompactEncodedInst:
++stats::EmittedCompactEncodedInstFragments;
- writeFragmentContents(F, OW);
+ OW->writeBytes(cast<MCCompactEncodedInstFragment>(F).getContents());
break;
case MCFragment::FT_Fill: {
for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
switch (FF.getValueSize()) {
default: llvm_unreachable("Invalid size!");
- case 1: OW->Write8 (uint8_t (FF.getValue())); break;
- case 2: OW->Write16(uint16_t(FF.getValue())); break;
- case 4: OW->Write32(uint32_t(FF.getValue())); break;
- case 8: OW->Write64(uint64_t(FF.getValue())); break;
+ case 1: OW->write8 (uint8_t (FF.getValue())); break;
+ case 2: OW->write16(uint16_t(FF.getValue())); break;
+ case 4: OW->write32(uint32_t(FF.getValue())); break;
+ case 8: OW->write64(uint64_t(FF.getValue())); break;
}
}
break;
case MCFragment::FT_LEB: {
const MCLEBFragment &LF = cast<MCLEBFragment>(F);
- OW->WriteBytes(LF.getContents());
+ OW->writeBytes(LF.getContents());
break;
}
case MCFragment::FT_SafeSEH: {
const MCSafeSEHFragment &SF = cast<MCSafeSEHFragment>(F);
- OW->Write32(SF.getSymbol()->getIndex());
+ OW->write32(SF.getSymbol()->getIndex());
break;
}
const MCOrgFragment &OF = cast<MCOrgFragment>(F);
for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
- OW->Write8(uint8_t(OF.getValue()));
+ OW->write8(uint8_t(OF.getValue()));
break;
}
case MCFragment::FT_Dwarf: {
const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
- OW->WriteBytes(OF.getContents());
+ OW->writeBytes(OF.getContents());
break;
}
case MCFragment::FT_DwarfFrame: {
const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
- OW->WriteBytes(CF.getContents());
+ OW->writeBytes(CF.getContents());
break;
}
}
// 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.
- getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, IsPCRel,
+ getWriter().recordRelocation(*this, Layout, &F, Fixup, Target, IsPCRel,
FixedValue);
}
return std::make_pair(FixedValue, IsPCRel);
// Allow the object writer a chance to perform post-layout binding (for
// example, to set the index fields in the symbol data).
- getWriter().ExecutePostLayoutBinding(*this, Layout);
+ getWriter().executePostLayoutBinding(*this, Layout);
// Evaluate and apply the fixups, generating relocation entries as necessary.
for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
for (MCSection::iterator it2 = it->begin(), ie2 = it->end(); it2 != ie2;
++it2) {
- MCEncodedFragmentWithFixups *F =
- dyn_cast<MCEncodedFragmentWithFixups>(it2);
- if (F) {
- for (MCEncodedFragmentWithFixups::fixup_iterator it3 = F->fixup_begin(),
- ie3 = F->fixup_end(); it3 != ie3; ++it3) {
- MCFixup &Fixup = *it3;
- uint64_t FixedValue;
- bool IsPCRel;
- std::tie(FixedValue, IsPCRel) = handleFixup(Layout, *F, Fixup);
- getBackend().applyFixup(Fixup, F->getContents().data(),
- F->getContents().size(), FixedValue, IsPCRel);
- }
+ MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(it2);
+ // Data and relaxable fragments both have fixups. So only process
+ // those here.
+ // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups
+ // being templated makes this tricky.
+ if (!F || isa<MCCompactEncodedInstFragment>(F))
+ continue;
+ ArrayRef<MCFixup> Fixups;
+ MutableArrayRef<char> Contents;
+ if (auto *FragWithFixups = dyn_cast<MCDataFragment>(F)) {
+ Fixups = FragWithFixups->getFixups();
+ Contents = FragWithFixups->getContents();
+ } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(F)) {
+ Fixups = FragWithFixups->getFixups();
+ Contents = FragWithFixups->getContents();
+ } else
+ llvm_unreachable("Unknown fragment with fixups!");
+ for (const MCFixup &Fixup : Fixups) {
+ uint64_t FixedValue;
+ bool IsPCRel;
+ std::tie(FixedValue, IsPCRel) = handleFixup(Layout, *F, Fixup);
+ getBackend().applyFixup(Fixup, Contents.data(),
+ Contents.size(), FixedValue, IsPCRel);
}
}
}
// Write the object file.
- getWriter().WriteObject(*this, Layout);
+ getWriter().writeObject(*this, Layout);
stats::ObjectBytes += OS.tell() - StartOffset;
}
bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
const MCRelaxableFragment *DF,
const MCAsmLayout &Layout) const {
- // If we cannot resolve the fixup value, it requires relaxation.
MCValue Target;
uint64_t Value;
- if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
- return true;
-
- return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
+ bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value);
+ return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF,
+ Layout);
}
bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
case MCFragment::FT_SafeSEH: {
const MCSafeSEHFragment *F = cast<MCSafeSEHFragment>(this);
OS << "\n ";
- OS << " Sym:";
- F->getSymbol()->print(OS);
+ OS << " Sym:" << F->getSymbol();
break;
}
}
OS << "]>\n";
}
#endif
-
-// anchors for MC*Fragment vtables
-void MCEncodedFragment::anchor() { }
-void MCEncodedFragmentWithFixups::anchor() { }
-void MCDataFragment::anchor() { }
-void MCCompactEncodedInstFragment::anchor() { }
-void MCRelaxableFragment::anchor() { }
-void MCAlignFragment::anchor() { }
-void MCFillFragment::anchor() { }
-void MCOrgFragment::anchor() { }
-void MCLEBFragment::anchor() { }
-void MCSafeSEHFragment::anchor() { }
-void MCDwarfLineAddrFragment::anchor() { }
-void MCDwarfCallFrameFragment::anchor() { }