mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-19 02:25:01 +00:00
Simplify the creation of compressed debug sections.
This is actually fairly simple in the current code layout: Check if we should compress just before writing out and everything else just works. This removes the last case in which the object writer was creating a fragment. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236267 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -182,8 +182,6 @@ class ELFObjectWriter : public MCObjectWriter {
|
|||||||
support::endian::Writer<support::big>(OS).write(Val);
|
support::endian::Writer<support::big>(OS).write(Val);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void write(MCDataFragment &F, T Value);
|
|
||||||
|
|
||||||
void writeHeader(const MCAssembler &Asm);
|
void writeHeader(const MCAssembler &Asm);
|
||||||
|
|
||||||
void WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
|
void WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
|
||||||
@@ -224,8 +222,6 @@ class ELFObjectWriter : public MCObjectWriter {
|
|||||||
const MCSectionELF *createRelocationSection(MCAssembler &Asm,
|
const MCSectionELF *createRelocationSection(MCAssembler &Asm,
|
||||||
const MCSectionELF &Sec);
|
const MCSectionELF &Sec);
|
||||||
|
|
||||||
void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
|
|
||||||
|
|
||||||
const MCSectionELF *createSectionHeaderStringTable();
|
const MCSectionELF *createSectionHeaderStringTable();
|
||||||
const MCSectionELF *createStringTable(MCContext &Ctx);
|
const MCSectionELF *createStringTable(MCContext &Ctx);
|
||||||
|
|
||||||
@@ -236,10 +232,13 @@ class ELFObjectWriter : public MCObjectWriter {
|
|||||||
const SectionIndexMapTy &SectionIndexMap,
|
const SectionIndexMapTy &SectionIndexMap,
|
||||||
const SectionOffsetsTy &SectionOffsets);
|
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,
|
void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
|
||||||
uint64_t Address, uint64_t Offset,
|
uint64_t Address, uint64_t Offset, uint64_t Size,
|
||||||
uint64_t Size, uint32_t Link, uint32_t Info,
|
uint32_t Link, uint32_t Info, uint64_t Alignment,
|
||||||
uint64_t Alignment, uint64_t EntrySize);
|
uint64_t EntrySize);
|
||||||
|
|
||||||
void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
|
void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
|
||||||
|
|
||||||
@@ -267,15 +266,6 @@ unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
|
|||||||
return SectionTable.size();
|
return SectionTable.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
void SymbolTableWriter::createSymtabShndx() {
|
void SymbolTableWriter::createSymtabShndx() {
|
||||||
if (!ShndxIndexes.empty())
|
if (!ShndxIndexes.empty())
|
||||||
return;
|
return;
|
||||||
@@ -1105,7 +1095,7 @@ ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
|
|||||||
|
|
||||||
static SmallVector<char, 128>
|
static SmallVector<char, 128>
|
||||||
getUncompressedData(const MCAsmLayout &Layout,
|
getUncompressedData(const MCAsmLayout &Layout,
|
||||||
MCSectionData::FragmentListType &Fragments) {
|
const MCSectionData::FragmentListType &Fragments) {
|
||||||
SmallVector<char, 128> UncompressedData;
|
SmallVector<char, 128> UncompressedData;
|
||||||
for (const MCFragment &F : Fragments) {
|
for (const MCFragment &F : Fragments) {
|
||||||
const SmallVectorImpl<char> *Contents;
|
const SmallVectorImpl<char> *Contents;
|
||||||
@@ -1148,92 +1138,9 @@ prependCompressionHeader(uint64_t Size,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return a single fragment containing the compressed contents of the whole
|
void ELFObjectWriter::writeSectionData(const MCAssembler &Asm,
|
||||||
// section. Null if the section was not compressed for any reason.
|
const MCSectionData &SD,
|
||||||
static std::unique_ptr<MCDataFragment>
|
const MCAsmLayout &Layout) {
|
||||||
getCompressedFragment(const MCAsmLayout &Layout,
|
|
||||||
MCSectionData::FragmentListType &Fragments) {
|
|
||||||
std::unique_ptr<MCDataFragment> CompressedFragment(new MCDataFragment());
|
|
||||||
|
|
||||||
// Gather the uncompressed data from all the fragments, recording the
|
|
||||||
// alignment fragment, if seen, and any fixups.
|
|
||||||
SmallVector<char, 128> UncompressedData =
|
|
||||||
getUncompressedData(Layout, Fragments);
|
|
||||||
|
|
||||||
SmallVectorImpl<char> &CompressedContents = CompressedFragment->getContents();
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
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 =
|
const MCSectionELF &Section =
|
||||||
static_cast<const MCSectionELF &>(SD.getSection());
|
static_cast<const MCSectionELF &>(SD.getSection());
|
||||||
StringRef SectionName = Section.getSectionName();
|
StringRef SectionName = Section.getSectionName();
|
||||||
@@ -1241,11 +1148,33 @@ void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm,
|
|||||||
// Compressing debug_frame requires handling alignment fragments which is
|
// Compressing debug_frame requires handling alignment fragments which is
|
||||||
// more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
|
// more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
|
||||||
// for writing to arbitrary buffers) for little benefit.
|
// for writing to arbitrary buffers) for little benefit.
|
||||||
if (!SectionName.startswith(".debug_") || SectionName == ".debug_frame")
|
if (!Asm.getContext().getAsmInfo()->compressDebugSections() ||
|
||||||
continue;
|
!SectionName.startswith(".debug_") || SectionName == ".debug_frame") {
|
||||||
|
Asm.writeSectionData(&SD, Layout);
|
||||||
CompressDebugSection(Asm, Layout, DefiningSymbols, Section, SD);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gather the uncompressed data from all the fragments.
|
||||||
|
const MCSectionData::FragmentListType &Fragments = SD.getFragmentList();
|
||||||
|
SmallVector<char, 128> UncompressedData =
|
||||||
|
getUncompressedData(Layout, Fragments);
|
||||||
|
|
||||||
|
SmallVector<char, 128> CompressedContents;
|
||||||
|
zlib::Status Success = zlib::compress(
|
||||||
|
StringRef(UncompressedData.data(), UncompressedData.size()),
|
||||||
|
CompressedContents);
|
||||||
|
if (Success != zlib::StatusOK) {
|
||||||
|
Asm.writeSectionData(&SD, Layout);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) {
|
||||||
|
Asm.writeSectionData(&SD, Layout);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Asm.getContext().renameELFSection(&Section,
|
||||||
|
(".z" + SectionName.drop_front(1)).str());
|
||||||
|
OS << CompressedContents;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
|
void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
|
||||||
@@ -1408,8 +1337,6 @@ void ELFObjectWriter::writeSectionHeader(
|
|||||||
|
|
||||||
void ELFObjectWriter::WriteObject(MCAssembler &Asm,
|
void ELFObjectWriter::WriteObject(MCAssembler &Asm,
|
||||||
const MCAsmLayout &Layout) {
|
const MCAsmLayout &Layout) {
|
||||||
CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout));
|
|
||||||
|
|
||||||
MCContext &Ctx = Asm.getContext();
|
MCContext &Ctx = Asm.getContext();
|
||||||
const MCSectionELF *ShstrtabSection =
|
const MCSectionELF *ShstrtabSection =
|
||||||
Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);
|
Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);
|
||||||
@@ -1453,7 +1380,7 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm,
|
|||||||
}
|
}
|
||||||
writeRelocations(Asm, *Section.getAssociatedSection());
|
writeRelocations(Asm, *Section.getAssociatedSection());
|
||||||
} else {
|
} else {
|
||||||
Asm.writeSectionData(&SD, Layout);
|
writeSectionData(Asm, SD, Layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t SecEnd = OS.tell();
|
uint64_t SecEnd = OS.tell();
|
||||||
|
Reference in New Issue
Block a user