[lld] [lld][ELF] Add --debug-names to create merged .debug_names. (PR #86508)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 17 19:33:41 PDT 2024
================
@@ -2711,6 +2714,600 @@ static uint32_t computeGdbHash(StringRef s) {
return h;
}
+// 4-byte alignment ensures that values in the hash lookup table and the name
+// table are aligned.
+DebugNamesBaseSection::DebugNamesBaseSection()
+ : SyntheticSection(0, SHT_PROGBITS, 4, ".debug_names") {}
+
+// Get the size of the .debug_names section header in bytes for DWARF32:
+static uint32_t getDebugNamesHeaderSize(uint32_t augmentationStringSize) {
+ return /* unit length */ 4 +
+ /* version */ 2 +
+ /* padding */ 2 +
+ /* CU count */ 4 +
+ /* TU count */ 4 +
+ /* Foreign TU count */ 4 +
+ /* Bucket Count */ 4 +
+ /* Name Count */ 4 +
+ /* Abbrev table size */ 4 +
+ /* Augmentation string size */ 4 +
+ /* Augmentation string */ augmentationStringSize;
+}
+
+void DebugNamesBaseSection::parseDebugNames(
+ InputChunk &inputChunk, OutputChunk &chunk,
+ DWARFDataExtractor &namesExtractor, DataExtractor &strExtractor,
+ function_ref<SmallVector<uint32_t, 0>(
+ uint32_t numCus, const DWARFDebugNames::Header &,
+ const DWARFDebugNames::DWARFDebugNamesOffsets &)>
+ readOffsets) {
+ const LLDDWARFSection namesSec = inputChunk.section;
+ DenseMap<uint32_t, IndexEntry *> offsetMap;
+ // Number of CUs seen in previous NameIndex sections within current chunk.
+ uint32_t numCus = 0;
+ for (const DWARFDebugNames::NameIndex &ni : *inputChunk.llvmDebugNames) {
+ NameData &nd = inputChunk.nameData.emplace_back();
+ nd.hdr = ni.getHeader();
+ if (nd.hdr.Format != DwarfFormat::DWARF32) {
+ errorOrWarn(toString(namesSec.sec) +
+ Twine(": found DWARF64, which is currently unsupported"));
+ return;
+ }
+ if (nd.hdr.Version != 5) {
+ errorOrWarn(toString(namesSec.sec) + Twine(": unsupported version: ") +
+ Twine(nd.hdr.Version));
+ return;
+ }
+ const uint32_t dwarfSize =
+ dwarf::getDwarfOffsetByteSize(DwarfFormat::DWARF32);
+ DWARFDebugNames::DWARFDebugNamesOffsets locs = ni.getOffsets();
+ if (locs.EntriesBase > namesExtractor.getData().size()) {
+ errorOrWarn(toString(namesSec.sec) +
+ Twine(": entry pool start is beyond end of section"));
+ return;
+ }
+
+ SmallVector<uint32_t, 0> entryOffsets = readOffsets(numCus, nd.hdr, locs);
+
+ // Read the entry pool.
+ offsetMap.clear();
+ nd.nameEntries.resize(nd.hdr.NameCount);
+ for (auto i : seq(nd.hdr.NameCount)) {
+ NameEntry &ne = nd.nameEntries[i];
+ uint64_t strOffset = locs.StringOffsetsBase + i * dwarfSize;
+ ne.stringOffset = strOffset;
+ uint64_t strp = namesExtractor.getRelocatedValue(dwarfSize, &strOffset);
+ StringRef name = strExtractor.getCStrRef(&strp);
+ ne.name = name.data();
+ ne.hashValue = caseFoldingDjbHash(name);
+
+ // Read a series of index entries that end with abbreviation code 0.
+ const char *errMsg = nullptr;
+ uint64_t offset = locs.EntriesBase + entryOffsets[i];
+ while (offset < namesSec.Data.size() && namesSec.Data[offset] != 0) {
+ // Read & store all entries (for the same string).
+ auto ie = makeThreadLocal<IndexEntry>();
+ ie->poolOffset = offset;
+ Error err = Error::success();
+ ie->abbrevCode =
+ static_cast<uint32_t>(namesExtractor.getULEB128(&offset, &err));
+ if (err) {
+ consumeError(std::move(err));
+ errMsg = ": invalid abbrev code in entry";
+ break;
+ }
+ auto it = ni.getAbbrevs().find_as(ie->abbrevCode);
+ if (it == ni.getAbbrevs().end()) {
+ errMsg = ": invalid abbrev code in entry";
+ break;
+ }
+
+ AttrValue attr, cuAttr = {0, 0};
+ for (DWARFDebugNames::AttributeEncoding a : it->Attributes) {
+ if (a.Index == dwarf::DW_IDX_parent) {
+ if (a.Form == dwarf::DW_FORM_ref4) {
+ attr.attrValue = namesExtractor.getU32(&offset, &err);
+ attr.attrSize = 4;
+ ie->parentOffset = locs.EntriesBase + attr.attrValue;
+ } else if (a.Form != DW_FORM_flag_present) {
+ errMsg = ": invalid form for DW_IDX_parent";
+ }
+ } else {
+ switch (a.Form) {
+ case DW_FORM_data1:
+ case DW_FORM_ref1: {
+ attr.attrValue = namesExtractor.getU8(&offset, &err);
+ attr.attrSize = 1;
+ break;
+ }
+ case DW_FORM_data2:
+ case DW_FORM_ref2: {
+ attr.attrValue = namesExtractor.getU16(&offset, &err);
+ attr.attrSize = 2;
+ break;
+ }
+ case DW_FORM_data4:
+ case DW_FORM_ref4: {
+ attr.attrValue = namesExtractor.getU32(&offset, &err);
+ attr.attrSize = 4;
+ break;
+ }
+ default:
+ errorOrWarn(toString(namesSec.sec) +
+ Twine(": unrecognized form encoding ") +
+ Twine(a.Form) + Twine(" in abbrev table"));
+ return;
+ }
+ }
+ if (err) {
+ errorOrWarn(toString(namesSec.sec) +
+ Twine(": error while reading attributes: ") +
+ toString(std::move(err)));
+ return;
+ }
+ if (a.Index == DW_IDX_compile_unit)
+ cuAttr = attr;
+ else if (a.Form != DW_FORM_flag_present)
+ ie->attrValues.push_back(attr);
+ }
+
+ // Canonicalize abbrev by placing the CU/TU index at the end.
+ ie->attrValues.push_back(cuAttr);
+ ne.indexEntries.push_back(std::move(ie));
+ }
+ if (offset >= namesSec.Data.size())
+ errMsg = ": index entry is out of bounds";
+ if (errMsg)
+ errorOrWarn(toString(namesSec.sec) + Twine(errMsg));
+
+ for (IndexEntry &ie : ne.entries())
+ offsetMap[ie.poolOffset] = &ie;
+ }
+
+ // Assign parent pointers, which will be used to update DW_IDX_parent index
+ // attributes. Note: offsetMap[0] does not exist, so parentOffset == 0 will
+ // get parentEntry == null as well.
+ for (NameEntry &ne : nd.nameEntries)
+ for (IndexEntry &ie : ne.entries())
+ ie.parentEntry = offsetMap.lookup(ie.parentOffset);
+ numCus += nd.hdr.CompUnitCount;
+ }
+}
+
+// Compute the form for output DW_IDX_compile_unit attributes, similar to
+// DIEInteger::BestForm. The input form (often DW_FORM_data1) may not hold all
+// the merged CU indices.
+std::pair<uint8_t, dwarf::Form> static getMergedCuCountForm(
+ uint32_t compUnitCount) {
+ if (compUnitCount > UINT16_MAX)
+ return {4, DW_FORM_data4};
+ if (compUnitCount > UINT8_MAX)
+ return {2, DW_FORM_data2};
+ return {1, DW_FORM_data1};
+}
+
+void DebugNamesBaseSection::computeHdrAndAbbrevTable(
+ MutableArrayRef<InputChunk> inputChunks) {
+ TimeTraceScope timeScope("Merge .debug_names", "hdr and abbrev table");
+ size_t numCu = 0;
+ hdr.Format = DwarfFormat::DWARF32;
+ hdr.Version = 5;
+ hdr.CompUnitCount = 0;
+ hdr.LocalTypeUnitCount = 0;
+ hdr.ForeignTypeUnitCount = 0;
+ hdr.AugmentationStringSize = 0;
+
+ // Compute CU and TU counts.
+ for (auto i : seq(numChunks)) {
+ InputChunk &inputChunk = inputChunks[i];
+ inputChunk.baseCuIdx = numCu;
+ numCu += chunks[i].compUnits.size();
+ for (const NameData &nd : inputChunk.nameData) {
+ hdr.CompUnitCount += nd.hdr.CompUnitCount;
+ // We are not actually handling or emitting type units yet, so
----------------
MaskRay wrote:
typo: so so
I'd remove
```
//hdr.LocalTypeUnitCount += nd.hdr.LocalTypeUnitCount;
//hdr.ForeignTypeUnitCount += nd.hdr.ForeignTypeUnitCount;
```
and `non-zero type unit counts will crash LLD.` (it's an assertion failure which can be avoided if we write code slightly differently.
Just
```
// TODO: We don't handle type units yet and LocalTypeUnitCount/ForeignTypeUnitCount are left as 0.
```
is sufficient.
https://github.com/llvm/llvm-project/pull/86508
More information about the llvm-commits
mailing list