unsigned ShstrtabIndex;
+ // Sections in the order they are to be output in the section table.
+ std::vector<const MCSectionELF *> SectionTable;
+ unsigned addToSectionTable(const MCSectionELF *Sec);
// TargetObjectWriter wrappers.
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
LocalSymbolData.clear();
ExternalSymbolData.clear();
UndefinedSymbolData.clear();
+ NeedsGOT = false;
+ SectionTable.clear();
MCObjectWriter::reset();
}
support::endian::Writer<support::big>(OS).write(Val);
}
- template <typename T> void write(MCDataFragment &F, T Value);
-
void writeHeader(const MCAssembler &Asm);
void WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
SectionOffsetsTy;
void WriteSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
- std::vector<const MCSectionELF *> &Sections,
SectionOffsetsTy &SectionOffsets);
bool shouldRelocateWithSymbol(const MCAssembler &Asm,
const MCSectionELF *createRelocationSection(MCAssembler &Asm,
const MCSectionELF &Sec);
- void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
-
- const MCSectionELF *
- createSectionHeaderStringTable(MCAssembler &Asm,
- std::vector<const MCSectionELF *> &Sections);
- const MCSectionELF *
- createStringTable(MCAssembler &Asm,
- std::vector<const MCSectionELF *> &Sections);
+ const MCSectionELF *createSectionHeaderStringTable();
+ const MCSectionELF *createStringTable(MCContext &Ctx);
void ExecutePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override;
- void writeSectionHeader(ArrayRef<const MCSectionELF *> Sections,
- MCAssembler &Asm, const MCAsmLayout &Layout,
+ void writeSectionHeader(MCAssembler &Asm, const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap,
const SectionOffsetsTy &SectionOffsets);
+ void writeSectionData(const MCAssembler &Asm, const MCSectionData &SD,
+ const MCAsmLayout &Layout);
+
void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
- uint64_t Address, uint64_t Offset,
- uint64_t Size, uint32_t Link, uint32_t Info,
- uint64_t Alignment, uint64_t EntrySize);
+ uint64_t Address, uint64_t Offset, uint64_t Size,
+ uint32_t Link, uint32_t Info, uint64_t Alignment,
+ uint64_t EntrySize);
void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
- bool
- IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
- const MCSymbolData &DataA,
- const MCFragment &FB,
- bool InSet,
- bool IsPCRel) const override;
+ bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
+ const MCSymbol &SymA,
+ const MCFragment &FB,
+ bool InSet,
+ bool IsPCRel) const override;
bool isWeak(const MCSymbolData &SD) const override;
};
}
-template <typename T> void ELFObjectWriter::write(MCDataFragment &F, T Val) {
- if (IsLittleEndian)
- Val = support::endian::byte_swap<T, support::little>(Val);
- else
- Val = support::endian::byte_swap<T, support::big>(Val);
- const char *Start = (const char *)&Val;
- F.getContents().append(Start, Start + sizeof(T));
+unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
+ SectionTable.push_back(Sec);
+ ShStrTabBuilder.add(Sec->getSectionName());
+ return SectionTable.size();
}
void SymbolTableWriter::createSymtabShndx() {
// The presence of symbol versions causes undefined symbols and
// versions declared with @@@ to be renamed.
- for (MCSymbolData &OriginalData : Asm.symbols()) {
- const MCSymbol &Alias = OriginalData.getSymbol();
+ for (const MCSymbol &Alias : Asm.symbols()) {
+ MCSymbolData &OriginalData = Alias.getData();
// Not an alias.
if (!Alias.isVariable())
MSD.SectionIndex, IsReserved);
}
-void ELFObjectWriter::WriteSymbolTable(
- MCAssembler &Asm, const MCAsmLayout &Layout,
- std::vector<const MCSectionELF *> &Sections,
- SectionOffsetsTy &SectionOffsets) {
+void ELFObjectWriter::WriteSymbolTable(MCAssembler &Asm,
+ const MCAsmLayout &Layout,
+ SectionOffsetsTy &SectionOffsets) {
MCContext &Ctx = Asm.getContext();
Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
SymtabSD.setAlignment(is64Bit() ? 8 : 4);
- SymbolTableIndex = Sections.size() + 1;
- Sections.push_back(SymtabSection);
+ SymbolTableIndex = addToSectionTable(SymtabSection);
// The string table must be emitted first because we need the index
// into the string table for all the symbol names.
SecStart = OS.tell();
const MCSectionELF *SymtabShndxSection =
Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
- Sections.push_back(SymtabShndxSection);
+ addToSectionTable(SymtabShndxSection);
MCSectionData *SymtabShndxSD =
&Asm.getOrCreateSectionData(*SymtabShndxSection);
SymtabShndxSD->setAlignment(4);
// or (A + C - R). If B = R + K and the relocation is not pcrel, we can
// replace B to implement it: (A - R - K + C)
if (IsPCRel)
- Asm.getContext().FatalError(
+ Asm.getContext().reportFatalError(
Fixup.getLoc(),
"No relocation available to represent this relative expression");
const MCSymbol &SymB = RefB->getSymbol();
if (SymB.isUndefined())
- Asm.getContext().FatalError(
+ Asm.getContext().reportFatalError(
Fixup.getLoc(),
Twine("symbol '") + SymB.getName() +
"' can not be undefined in a subtraction expression");
assert(!SymB.isAbsolute() && "Should have been folded");
const MCSection &SecB = SymB.getSection();
if (&SecB != &FixupSection)
- Asm.getContext().FatalError(
+ Asm.getContext().reportFatalError(
Fixup.getLoc(), "Cannot represent a difference across sections");
const MCSymbolData &SymBD = Asm.getSymbolData(SymB);
if (::isWeak(SymBD))
- Asm.getContext().FatalError(
+ Asm.getContext().reportFatalError(
Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
uint64_t SymBOffset = Layout.getSymbolOffset(&SymBD);
// FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
if (NeedsGOT) {
StringRef Name = "_GLOBAL_OFFSET_TABLE_";
- MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
+ MCSymbol *Sym = Asm.getContext().getOrCreateSymbol(Name);
MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
Data.setExternal(true);
MCELF::SetBinding(Data, ELF::STB_GLOBAL);
}
// Add the data for the symbols.
- for (MCSymbolData &SD : Asm.symbols()) {
- const MCSymbol &Symbol = SD.getSymbol();
+ for (const MCSymbol &Symbol : Asm.symbols()) {
+ MCSymbolData &SD = Symbol.getData();
bool Used = UsedInReloc.count(&Symbol);
bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
static SmallVector<char, 128>
getUncompressedData(const MCAsmLayout &Layout,
- MCSectionData::FragmentListType &Fragments) {
+ const MCSectionData::FragmentListType &Fragments) {
SmallVector<char, 128> UncompressedData;
for (const MCFragment &F : Fragments) {
const SmallVectorImpl<char> *Contents;
return true;
}
-// Return a single fragment containing the compressed contents of the whole
-// section. Null if the section was not compressed for any reason.
-static std::unique_ptr<MCDataFragment>
-getCompressedFragment(const MCAsmLayout &Layout,
- MCSectionData::FragmentListType &Fragments) {
- std::unique_ptr<MCDataFragment> CompressedFragment(new MCDataFragment());
+void ELFObjectWriter::writeSectionData(const MCAssembler &Asm,
+ const MCSectionData &SD,
+ const MCAsmLayout &Layout) {
+ const MCSectionELF &Section =
+ static_cast<const MCSectionELF &>(SD.getSection());
+ StringRef SectionName = Section.getSectionName();
- // Gather the uncompressed data from all the fragments, recording the
- // alignment fragment, if seen, and any fixups.
+ // Compressing debug_frame requires handling alignment fragments which is
+ // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
+ // for writing to arbitrary buffers) for little benefit.
+ if (!Asm.getContext().getAsmInfo()->compressDebugSections() ||
+ !SectionName.startswith(".debug_") || SectionName == ".debug_frame") {
+ Asm.writeSectionData(&SD, Layout);
+ return;
+ }
+
+ // Gather the uncompressed data from all the fragments.
+ const MCSectionData::FragmentListType &Fragments = SD.getFragmentList();
SmallVector<char, 128> UncompressedData =
getUncompressedData(Layout, Fragments);
- SmallVectorImpl<char> &CompressedContents = CompressedFragment->getContents();
-
+ SmallVector<char, 128> CompressedContents;
zlib::Status Success = zlib::compress(
StringRef(UncompressedData.data(), UncompressedData.size()),
CompressedContents);
- if (Success != zlib::StatusOK)
- return nullptr;
-
- if (!prependCompressionHeader(UncompressedData.size(), CompressedContents))
- return nullptr;
-
- return CompressedFragment;
-}
-
-typedef DenseMap<const MCSectionData *, std::vector<MCSymbolData *>>
-DefiningSymbolMap;
-
-static void UpdateSymbols(const MCAsmLayout &Layout,
- const std::vector<MCSymbolData *> &Symbols,
- MCFragment &NewFragment) {
- for (MCSymbolData *Sym : Symbols) {
- Sym->setOffset(Sym->getOffset() +
- Layout.getFragmentOffset(Sym->getFragment()));
- Sym->setFragment(&NewFragment);
+ if (Success != zlib::StatusOK) {
+ Asm.writeSectionData(&SD, Layout);
+ return;
}
-}
-static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout,
- const DefiningSymbolMap &DefiningSymbols,
- const MCSectionELF &Section,
- MCSectionData &SD) {
- StringRef SectionName = Section.getSectionName();
- MCSectionData::FragmentListType &Fragments = SD.getFragmentList();
-
- std::unique_ptr<MCDataFragment> CompressedFragment =
- getCompressedFragment(Layout, Fragments);
-
- // Leave the section as-is if the fragments could not be compressed.
- if (!CompressedFragment)
+ if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) {
+ Asm.writeSectionData(&SD, Layout);
return;
-
- // Update the fragment+offsets of any symbols referring to fragments in this
- // section to refer to the new fragment.
- auto I = DefiningSymbols.find(&SD);
- if (I != DefiningSymbols.end())
- UpdateSymbols(Layout, I->second, *CompressedFragment);
-
- // Invalidate the layout for the whole section since it will have new and
- // different fragments now.
- Layout.invalidateFragmentsFrom(&Fragments.front());
- Fragments.clear();
-
- // Complete the initialization of the new fragment
- CompressedFragment->setParent(&SD);
- CompressedFragment->setLayoutOrder(0);
- Fragments.push_back(CompressedFragment.release());
-
- // Rename from .debug_* to .zdebug_*
+ }
Asm.getContext().renameELFSection(&Section,
(".z" + SectionName.drop_front(1)).str());
-}
-
-void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm,
- MCAsmLayout &Layout) {
- if (!Asm.getContext().getAsmInfo()->compressDebugSections())
- return;
-
- DefiningSymbolMap DefiningSymbols;
-
- for (MCSymbolData &SD : Asm.symbols())
- if (MCFragment *F = SD.getFragment())
- DefiningSymbols[F->getParent()].push_back(&SD);
-
- for (MCSectionData &SD : Asm) {
- const MCSectionELF &Section =
- static_cast<const MCSectionELF &>(SD.getSection());
- StringRef SectionName = Section.getSectionName();
-
- // Compressing debug_frame requires handling alignment fragments which is
- // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
- // for writing to arbitrary buffers) for little benefit.
- if (!SectionName.startswith(".debug_") || SectionName == ".debug_frame")
- continue;
-
- CompressDebugSection(Asm, Layout, DefiningSymbols, Section, SD);
- }
+ OS << CompressedContents;
}
void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
}
}
-const MCSectionELF *ELFObjectWriter::createSectionHeaderStringTable(
- MCAssembler &Asm, std::vector<const MCSectionELF *> &Sections) {
- const MCSectionELF *ShstrtabSection = Sections[ShstrtabIndex - 1];
-
- Asm.getOrCreateSectionData(*ShstrtabSection);
-
- for (MCSectionData &SD : Asm) {
- const MCSectionELF &Section =
- static_cast<const MCSectionELF &>(SD.getSection());
- ShStrTabBuilder.add(Section.getSectionName());
- }
+const MCSectionELF *ELFObjectWriter::createSectionHeaderStringTable() {
+ const MCSectionELF *ShstrtabSection = SectionTable[ShstrtabIndex - 1];
ShStrTabBuilder.finalize(StringTableBuilder::ELF);
OS << ShStrTabBuilder.data();
return ShstrtabSection;
}
-const MCSectionELF *ELFObjectWriter::createStringTable(
- MCAssembler &Asm, std::vector<const MCSectionELF *> &Sections) {
- MCContext &Ctx = Asm.getContext();
+const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {
const MCSectionELF *StrtabSection =
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
- Asm.getOrCreateSectionData(*StrtabSection);
- Sections.push_back(StrtabSection);
- StringTableIndex = Sections.size();
+ StringTableIndex = addToSectionTable(StrtabSection);
OS << StrTabBuilder.data();
return StrtabSection;
}
break;
case ELF::SHT_DYNAMIC:
- sh_link = ShStrTabBuilder.getOffset(Section.getSectionName());
- break;
+ llvm_unreachable("SHT_DYNAMIC in a relocatable object");
case ELF::SHT_REL:
case ELF::SHT_RELA: {
}
void ELFObjectWriter::writeSectionHeader(
- ArrayRef<const MCSectionELF *> Sections, MCAssembler &Asm,
- const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
+ MCAssembler &Asm, const MCAsmLayout &Layout,
+ const SectionIndexMapTy &SectionIndexMap,
const SectionOffsetsTy &SectionOffsets) {
- const unsigned NumSections = Asm.size();
+ const unsigned NumSections = SectionTable.size();
// Null section first.
uint64_t FirstSectionSize =
(NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
- for (unsigned i = 0; i < NumSections; ++i) {
- const MCSectionELF &Section = *Sections[i];
- const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
+ for (const MCSectionELF *Section : SectionTable) {
+ const MCSectionData &SD = Asm.getOrCreateSectionData(*Section);
uint32_t GroupSymbolIndex;
- if (Section.getType() != ELF::SHT_GROUP)
+ unsigned Type = Section->getType();
+ if (Type != ELF::SHT_GROUP)
GroupSymbolIndex = 0;
else
- GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, Section.getGroup());
+ GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, Section->getGroup());
const std::pair<uint64_t, uint64_t> &Offsets =
- SectionOffsets.find(&Section)->second;
- uint64_t Size = Section.getType() == ELF::SHT_NOBITS
- ? Layout.getSectionAddressSize(&SD)
- : Offsets.second - Offsets.first;
+ SectionOffsets.find(Section)->second;
+ uint64_t Size = Type == ELF::SHT_NOBITS ? Layout.getSectionAddressSize(&SD)
+ : Offsets.second - Offsets.first;
writeSection(Asm, SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
- SD.getAlignment(), Section);
+ SD.getAlignment(), *Section);
}
}
void ELFObjectWriter::WriteObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
- CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout));
-
- std::vector<const MCSectionELF *> Sections;
MCContext &Ctx = Asm.getContext();
const MCSectionELF *ShstrtabSection =
Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);
- Sections.push_back(ShstrtabSection);
- ShstrtabIndex = Sections.size();
+ ShstrtabIndex = addToSectionTable(ShstrtabSection);
RevGroupMapTy RevGroupMap;
SectionIndexMapTy SectionIndexMap;
}
writeRelocations(Asm, *Section.getAssociatedSection());
} else {
- Asm.writeSectionData(&SD, Layout);
+ writeSectionData(Asm, SD, Layout);
}
uint64_t SecEnd = OS.tell();
unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
if (!GroupIdx) {
const MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
- Sections.push_back(Group);
- GroupIdx = Sections.size();
+ GroupIdx = addToSectionTable(Group);
MCSectionData *GroupD = &Asm.getOrCreateSectionData(*Group);
GroupD->setAlignment(4);
}
GroupMembers[SignatureSymbol].push_back(RelSection);
}
- Sections.push_back(&Section);
- SectionIndexMap[&Section] = Sections.size();
- if (RelSection) {
- Sections.push_back(RelSection);
- SectionIndexMap[RelSection] = Sections.size();
- }
+ SectionIndexMap[&Section] = addToSectionTable(&Section);
+ if (RelSection)
+ SectionIndexMap[RelSection] = addToSectionTable(RelSection);
}
if (!ComputedSymtab) {
ComputedSymtab = true;
}
- WriteSymbolTable(Asm, Layout, Sections, SectionOffsets);
+ WriteSymbolTable(Asm, Layout, SectionOffsets);
{
uint64_t SecStart = OS.tell();
- const MCSectionELF *Sec = createStringTable(Asm, Sections);
+ const MCSectionELF *Sec = createStringTable(Ctx);
uint64_t SecEnd = OS.tell();
SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
}
{
uint64_t SecStart = OS.tell();
- const MCSectionELF *Sec = createSectionHeaderStringTable(Asm, Sections);
+ const MCSectionELF *Sec = createSectionHeaderStringTable();
uint64_t SecEnd = OS.tell();
SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
}
const unsigned SectionHeaderOffset = OS.tell();
// ... then the section header table ...
- writeSectionHeader(Sections, Asm, Layout, SectionIndexMap, SectionOffsets);
+ writeSectionHeader(Asm, Layout, SectionIndexMap, SectionOffsets);
- uint16_t NumSections = (Sections.size() + 1 >= ELF::SHN_LORESERVE)
+ uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE)
? (uint16_t)ELF::SHN_UNDEF
- : Sections.size() + 1;
+ : SectionTable.size() + 1;
if (sys::IsLittleEndianHost != IsLittleEndian)
sys::swapByteOrder(NumSections);
unsigned NumSectionsOffset;
}
bool ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
- const MCAssembler &Asm, const MCSymbolData &DataA, const MCFragment &FB,
+ const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
bool InSet, bool IsPCRel) const {
if (IsPCRel) {
assert(!InSet);
- if (::isWeak(DataA))
+ if (::isWeak(SymA.getData()))
return false;
}
- return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(Asm, DataA, FB,
+ return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
InSet, IsPCRel);
}