diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -855,22 +855,63 @@ } } -template static void readCallGraphsFromObjectFiles() { - auto getIndex = [&](ObjFile *obj, uint32_t index) { - const Elf_Rel_Impl &rel = obj->cgProfileRel[index]; - return rel.getSymbol(config->isMips64EL); - }; +/// If SHT_LLVM_CALL_GRAPH_PROFILE and it's relocation section exists returns +/// true, and populates cgProfile and symbolIndices. +template +static bool +processCallGraphRelocations(SmallVector &symbolIndices, + ArrayRef &cgProfile, + ObjFile *inputObj) { + symbolIndices.clear(); + const ELFFile &obj = inputObj->getObj(); + ArrayRef> objSections = + CHECK(obj.sections(), "could not retrieve object sections"); + + if (inputObj->cgProfileSectionIndex == SHN_UNDEF) + return false; + + cgProfile = + check(obj.template getSectionContentsAsArray( + objSections[inputObj->cgProfileSectionIndex])); + + for (size_t i = 0, e = objSections.size(); i < e; ++i) { + const Elf_Shdr_Impl &sec = objSections[i]; + if (sec.sh_info == inputObj->cgProfileSectionIndex) { + if (sec.sh_type == SHT_RELA) { + ArrayRef relas = + CHECK(obj.relas(sec), "could not retrieve cg profile rela section"); + for (const typename ELFT::Rela &rel : relas) + symbolIndices.push_back(rel.getSymbol(config->isMips64EL)); + break; + } else if (sec.sh_type == SHT_REL) { + ArrayRef rels = + CHECK(obj.rels(sec), "could not retrieve cg profile rel section"); + for (const typename ELFT::Rel &rel : rels) + symbolIndices.push_back(rel.getSymbol(config->isMips64EL)); + break; + } + } + } + if (symbolIndices.empty()) + warn("SHT_LLVM_CALL_GRAPH_PROFILE exists, but relocation section doesn't"); + return !symbolIndices.empty(); +} +template static void readCallGraphsFromObjectFiles() { + SmallVector symbolIndices; + ArrayRef cgProfile; for (auto file : objectFiles) { auto *obj = cast>(file); - if (obj->cgProfileRel.empty()) + if (!processCallGraphRelocations(symbolIndices, cgProfile, obj)) continue; - if (obj->cgProfileRel.size() != obj->cgProfile.size() * 2) + + if (symbolIndices.size() != cgProfile.size() * 2) fatal("number of relocations doesn't match Weights"); - for (uint32_t i = 0, size = obj->cgProfile.size(); i < size; ++i) { - const Elf_CGProfile_Impl &cgpe = obj->cgProfile[i]; - uint32_t fromIndex = getIndex(obj, i * 2); - uint32_t toIndex = getIndex(obj, i * 2 + 1); + + for (uint32_t i = 0, size = cgProfile.size(); i < size; ++i) { + const Elf_CGProfile_Impl &cgpe = cgProfile[i]; + uint32_t fromIndex = symbolIndices[i * 2]; + uint32_t toIndex = symbolIndices[i * 2 + 1]; auto *fromSym = dyn_cast(&obj->getSymbol(fromIndex)); auto *toSym = dyn_cast(&obj->getSymbol(toIndex)); if (!fromSym || !toSym) diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -21,6 +21,7 @@ #include "llvm/Object/ELF.h" #include "llvm/Object/IRObjectFile.h" #include "llvm/Support/Threading.h" +#include #include namespace llvm { @@ -249,10 +250,8 @@ // Pointer to this input file's .llvm_addrsig section, if it has one. const Elf_Shdr *addrsigSec = nullptr; - // SHT_LLVM_CALL_GRAPH_PROFILE table. - ArrayRef cgProfile; - // SHT_LLVM_CALL_GRAPH_PROFILE relocations, always in the REL format. - ArrayRef cgProfileRel; + // SHT_LLVM_CALL_GRAPH_PROFILE section index. + uint32_t cgProfileSectionIndex = 0; // Get cached DWARF information. DWARFCache *getDwarf(); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -571,19 +571,14 @@ CHECK(obj.getSectionStringTable(objSections), this); std::vector> selectedGroups; - // SHT_LLVM_CALL_GRAPH_PROFILE Section Index. - size_t cgProfileSectionIndex = 0; for (size_t i = 0, e = objSections.size(); i < e; ++i) { if (this->sections[i] == &InputSection::discarded) continue; const Elf_Shdr &sec = objSections[i]; - if (sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE) { - cgProfile = - check(obj.template getSectionContentsAsArray(sec)); + if (sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE) cgProfileSectionIndex = i; - } // SHF_EXCLUDE'ed sections are discarded by the linker. However, // if -r is given, we'll let the final link discard such sections. @@ -669,13 +664,8 @@ continue; const Elf_Shdr &sec = objSections[i]; - if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) { + if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) this->sections[i] = createInputSection(sec); - if (cgProfileSectionIndex && sec.sh_info == cgProfileSectionIndex) { - if (sec.sh_type == SHT_REL) - cgProfileRel = CHECK(getObj().rels(sec), this); - } - } // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have // the flag. diff --git a/llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test b/llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test --- a/llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test +++ b/llvm/test/tools/llvm-readobj/ELF/call-graph-profile.test @@ -40,13 +40,13 @@ Relocations: - Symbol: foo Type: R_X86_64_NONE - - Offset: 0x1 + - Offset: 0x0 Symbol: bar Type: R_X86_64_NONE - - Offset: 0x2 + - Offset: 0x8 Symbol: bar Type: R_X86_64_NONE - - Offset: 0x3 + - Offset: 0x8 Symbol: foo Type: R_X86_64_NONE Symbols: @@ -104,19 +104,19 @@ Relocations: - Symbol: 1 Type: R_X86_64_NONE - - Offset: 0x1 + - Offset: 0x0 Symbol: 2 Type: R_X86_64_NONE - - Offset: 0x2 + - Offset: 0x8 Symbol: 2 Type: R_X86_64_NONE - - Offset: 0x3 + - Offset: 0x8 Symbol: 3 Type: R_X86_64_NONE - - Offset: 0x4 + - Offset: 0x10 Symbol: 0x0 ## Null symbol. Type: R_X86_64_NONE - - Offset: 0x5 + - Offset: 0x10 Symbol: 0x4 ## This index goes past the end of the symbol table. Type: R_X86_64_NONE - Name: .strtab @@ -153,7 +153,6 @@ Entries: - Weight: 89 - Weight: 98 - EntSize: [[ENTSIZE=]] Symbols: - Name: foo - Name: bar @@ -185,23 +184,22 @@ Entries: - Weight: 89 - Weight: 98 - EntSize: [[ENTSIZE=]] - Name: .rel.llvm.call-graph-profile Type: SHT_REL Info: .llvm.call-graph-profile Relocations: - Symbol: foo Type: R_X86_64_NONE - - Offset: 0x1 + - Offset: 0x0 Symbol: bar Type: R_X86_64_NONE - - Offset: 0x2 + - Offset: 0x8 Symbol: bar Type: R_X86_64_NONE - - Offset: 0x3 + - Offset: 0x8 Symbol: foo Type: R_X86_64_NONE - - Offset: 0x4 + - Offset: 0x10 Symbol: foo Type: R_X86_64_NONE Symbols: @@ -235,23 +233,73 @@ Entries: - Weight: 89 - Weight: 98 - EntSize: [[ENTSIZE=]] - Name: .rel.llvm.call-graph-profile Type: SHT_REL Info: .llvm.call-graph-profile Relocations: - Symbol: foo Type: R_X86_64_NONE - - Offset: 0x1 + - Offset: 0x0 Symbol: bar Type: R_X86_64_NONE - - Offset: 0x2 + - Offset: 0x8 Symbol: bar Type: R_X86_64_NONE - - Offset: 0x3 + - Offset: 0x8 Symbol: foo Type: R_X86_64_NONE EntSize: 24 Symbols: - Name: foo - Name: bar + +## GNU strip may convert SHT_REL to SHT_RELA. Test we can handle SHT_RELA. +# RUN: yaml2obj %s --docnum=6 -o %t7.o +# RUN: llvm-readobj %t7.o --cg-profile | FileCheck %s --check-prefix=LLVM-RELA +# RUN: llvm-readelf %t7.o --cg-profile | FileCheck %s --check-prefix=GNU-RELA + +# LLVM-RELA: CGProfile [ +# LLVM-RELA-NEXT: CGProfileEntry { +# LLVM-RELA-NEXT: From: foo (1) +# LLVM-RELA-NEXT: To: bar (2) +# LLVM-RELA-NEXT: Weight: 89 +# LLVM-RELA-NEXT: } +# LLVM-RELA-NEXT: CGProfileEntry { +# LLVM-RELA-NEXT: From: bar (2) +# LLVM-RELA-NEXT: To: foo (1) +# LLVM-RELA-NEXT: Weight: 98 +# LLVM-RELA-NEXT: } +# LLVM-RELA-NEXT: ] + +# GNU-RELA: GNUStyle::printCGProfile not implemented + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_X86_64 +Sections: + - Name: .llvm.call-graph-profile + Type: SHT_LLVM_CALL_GRAPH_PROFILE + Entries: + - Weight: 89 + - Weight: 98 + - Name: .rela.llvm.call-graph-profile + Type: SHT_RELA + Info: .llvm.call-graph-profile + Relocations: + - Symbol: foo + Type: R_X86_64_NONE + - Offset: 0x0 + Symbol: bar + Type: R_X86_64_NONE + - Offset: 0x8 + Symbol: bar + Type: R_X86_64_NONE + - Offset: 0x8 + Symbol: foo + Type: R_X86_64_NONE +Symbols: + - Name: foo + - Name: bar diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -6702,6 +6702,54 @@ W.startLine() << "Hash Histogram not implemented!\n"; } +/// Returns true if rel/rela section exists, and populates SymbolIndices. +/// Otherwise returns false. +template +static bool getSymbolIndices(const typename ELFT::Shdr *CGRelSection, + const ELFFile &Obj, + const LLVMELFDumper *Dumper, + SmallVector &SymbolIndices) { + if (!CGRelSection) { + Dumper->reportUniqueWarning( + "relocation section for a call graph section doesn't exist"); + return false; + } + + if (CGRelSection->sh_type == SHT_REL) { + typename ELFT::RelRange CGProfileRel; + Expected CGProfileRelOrError = + Obj.rels(*CGRelSection); + if (!CGProfileRelOrError) { + Dumper->reportUniqueWarning("unable to load relocations for " + "SHT_LLVM_CALL_GRAPH_PROFILE section: " + + toString(CGProfileRelOrError.takeError())); + return false; + } + + CGProfileRel = *CGProfileRelOrError; + for (const typename ELFT::Rel &Rel : CGProfileRel) + SymbolIndices.push_back(Rel.getSymbol(Obj.isMips64EL())); + } else { + // MC unconditionally produces SHT_REL, but GNU strip/objcopy may convert the format + // to SHT_RELA (https://sourceware.org/bugzilla/show_bug.cgi?id=28035) + typename ELFT::RelaRange CGProfileRela; + Expected CGProfileRelaOrError = + Obj.relas(*CGRelSection); + if (!CGProfileRelaOrError) { + Dumper->reportUniqueWarning("unable to load relocations for " + "SHT_LLVM_CALL_GRAPH_PROFILE section: " + + toString(CGProfileRelaOrError.takeError())); + return false; + } + + CGProfileRela = *CGProfileRelaOrError; + for (const typename ELFT::Rela &Rela : CGProfileRela) + SymbolIndices.push_back(Rela.getSymbol(Obj.isMips64EL())); + } + + return true; +} + template void LLVMELFDumper::printCGProfile() { llvm::MapVector SecToRelocMap; @@ -6723,40 +6771,22 @@ return; } - Elf_Rel_Range CGProfileRel; - bool UseReloc = (CGRelSection != nullptr); - if (UseReloc) { - Expected CGProfileRelaOrError = - this->Obj.rels(*CGRelSection); - if (!CGProfileRelaOrError) { - this->reportUniqueWarning("unable to load relocations for " - "SHT_LLVM_CALL_GRAPH_PROFILE section: " + - toString(CGProfileRelaOrError.takeError())); - UseReloc = false; - } else - CGProfileRel = *CGProfileRelaOrError; - - if (UseReloc && CGProfileRel.size() != (CGProfileOrErr->size() * 2)) { - this->reportUniqueWarning( - "number of from/to pairs does not match number of frequencies"); - UseReloc = false; - } - } else + SmallVector SymbolIndices; + bool UseReloc = + getSymbolIndices(CGRelSection, this->Obj, this, SymbolIndices); + if (UseReloc && SymbolIndices.size() != CGProfileOrErr->size() * 2) { this->reportUniqueWarning( - "relocation section for a call graph section doesn't exist"); - - auto GetIndex = [&](uint32_t Index) { - const Elf_Rel_Impl &Rel = CGProfileRel[Index]; - return Rel.getSymbol(this->Obj.isMips64EL()); - }; + "number of from/to pairs does not match number of frequencies"); + UseReloc = false; + } ListScope L(W, "CGProfile"); for (uint32_t I = 0, Size = CGProfileOrErr->size(); I != Size; ++I) { const Elf_CGProfile &CGPE = (*CGProfileOrErr)[I]; DictScope D(W, "CGProfileEntry"); if (UseReloc) { - uint32_t From = GetIndex(I * 2); - uint32_t To = GetIndex(I * 2 + 1); + uint32_t From = SymbolIndices[I * 2]; + uint32_t To = SymbolIndices[I * 2 + 1]; W.printNumber("From", this->getStaticSymbolName(From), From); W.printNumber("To", this->getStaticSymbolName(To), To); }