diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -518,6 +518,31 @@ this); } + +/// For DWARF4 the Type Units are in their own seperate .debug_types section. +/// For DWARF5 they are part of the .debug_info section. +/// Returns true if link group contains either of those sections. +template +static bool isDebugGroupSection(ObjFile &objFile, + ArrayRef &entries, + StringRef &shstrtab) { + ArrayRef ndxArray = entries.slice(1); + object::ELFFile obj = objFile.getObj(); + for (uint32_t ndx : ndxArray) { + Expected grpSection = obj.getSection(ndx); + if (!grpSection) { + warn(toString(&objFile) + " : " + + toString(std::move(grpSection.takeError()))); + break; + } + StringRef name = check(obj.getSectionName(**grpSection, shstrtab)); + if ((name.find("debug_types") != StringRef::npos) || + (name.find("debug_info") != StringRef::npos)) + return true; + } + return false; +} + template void ObjFile::parse(bool ignoreComdats) { object::ELFFile obj = this->getObj(); // Read a section table. justSymbols is usually false. @@ -588,13 +613,12 @@ CHECK(obj.template getSectionContentsAsArray(sec), this); if (entries.empty()) fatal(toString(this) + ": empty SHT_GROUP"); - Elf_Word flag = entries[0]; if (flag && flag != GRP_COMDAT) fatal(toString(this) + ": unsupported SHT_GROUP format"); - + bool isDebugSection = isDebugGroupSection(*this, entries, shstrtab); bool keepGroup = - (flag & GRP_COMDAT) == 0 || ignoreComdats || + (flag & GRP_COMDAT) == 0 || (ignoreComdats && !isDebugSection) || symtab.comdatGroups.try_emplace(CachedHashStringRef(signature), this) .second; if (keepGroup) {