[llvm] 410c6ca - [llvm-objdump] [debuginfod] Fetch for very-stripped binaries.

Daniel Thornburgh via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 11:40:44 PDT 2022


Author: Daniel Thornburgh
Date: 2022-10-04T11:40:36-07:00
New Revision: 410c6ca9a4ed7090a3960d960cdfd09f7d08db68

URL: https://github.com/llvm/llvm-project/commit/410c6ca9a4ed7090a3960d960cdfd09f7d08db68
DIFF: https://github.com/llvm/llvm-project/commit/410c6ca9a4ed7090a3960d960cdfd09f7d08db68.diff

LOG: [llvm-objdump] [debuginfod] Fetch for very-stripped binaries.

When a binary is missing section headers or symbols, objdump can't
provide as good of a disassembly. This change makes objdump try to fetch
a better verion of the binary by its build ID.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D132887

Added: 
    

Modified: 
    llvm/test/tools/llvm-objdump/debuginfod.test
    llvm/tools/llvm-objdump/llvm-objdump.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/test/tools/llvm-objdump/debuginfod.test b/llvm/test/tools/llvm-objdump/debuginfod.test
index 2910c2068dd1..c8f871dc8cf2 100644
--- a/llvm/test/tools/llvm-objdump/debuginfod.test
+++ b/llvm/test/tools/llvm-objdump/debuginfod.test
@@ -61,5 +61,32 @@ RUN:   --debuginfod %t/stripped 2> %t.err | \
 RUN:   FileCheck %s --check-prefix=NOTFOUND
 RUN: count 0 < %t.err
 
+# Use debuginfod to recover symbols.
+RUN: llvm-strip --strip-sections %t/stripped
+RUN: env DEBUGINFOD_CACHE_PATH=%t llvm-objdump -d --debuginfod \
+RUN:   %t/stripped | \
+RUN:   FileCheck %s --check-prefix=SYMBOLS
+
+# Use debuginfod to recover section headers, but not symbols.
+RUN: mkdir %t/stripped-symbols
+RUN: cp %p/Inputs/embedded-source %t/stripped-symbols/llvmcache-7361776989772977641
+RUN: llvm-strip %t/stripped-symbols/llvmcache-7361776989772977641
+RUN: env DEBUGINFOD_CACHE_PATH=%t/stripped-symbols llvm-objdump -d \
+RUN:   --debuginfod %t/stripped | \
+RUN:   FileCheck %s --check-prefix=SECTIONS
+
+# Don't use debuginfod if neither section headers nor symbols can be recovered.
+RUN: mkdir %t/stripped-sections
+RUN: echo "" | llvm-mc -filetype=obj -triple x86_64 > \
+RUN:   %t/stripped-sections/llvmcache-7361776989772977641
+RUN: llvm-strip --strip-sections %t/stripped-sections/llvmcache-7361776989772977641
+RUN: env DEBUGINFOD_CACHE_PATH=%t/stripped-sections llvm-objdump -d \
+RUN:   --debuginfod %t/stripped | \
+RUN:   FileCheck %s --check-prefix=NOSECTIONS
+
 NOTFOUND-NOT: int main(int argc, char *argv[]) {
 FOUND: int main(int argc, char *argv[]) {
+
+SYMBOLS: <main>:
+SECTIONS: <.text>:
+NOSECTIONS: <PT_LOAD#{{[0-9]+}}>:

diff  --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 0c1bb0b29b87..689aa0ab7a82 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -1970,6 +1970,22 @@ static void disassembleObject(const Target *TheTarget, ObjectFile &Obj,
 }
 
 static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
+  // If information useful for showing the disassembly is missing, try to find a
+  // more complete binary and disassemble that instead.
+  OwningBinary<Binary> FetchedBinary;
+  if (Obj->symbols().empty()) {
+    if (Optional<OwningBinary<Binary>> FetchedBinaryOpt =
+            fetchBinaryByBuildID(*Obj)) {
+      if (auto *O = dyn_cast<ObjectFile>(FetchedBinaryOpt->getBinary())) {
+        if (!O->symbols().empty() ||
+            (!O->sections().empty() && Obj->sections().empty())) {
+          FetchedBinary = std::move(*FetchedBinaryOpt);
+          Obj = O;
+        }
+      }
+    }
+  }
+
   const Target *TheTarget = getTarget(Obj);
 
   // Package up features to be passed to target/subtarget
@@ -2074,14 +2090,13 @@ static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
   PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
 
   const ObjectFile *DbgObj = Obj;
-  OwningBinary<Binary> DebugBinary;
-  if (!DbgObj->hasDebugInfo()) {
+  if (!FetchedBinary.getBinary() && !Obj->hasDebugInfo()) {
     if (Optional<OwningBinary<Binary>> DebugBinaryOpt =
             fetchBinaryByBuildID(*Obj)) {
       if (auto *FetchedObj =
               dyn_cast<const ObjectFile>(DebugBinaryOpt->getBinary())) {
         if (FetchedObj->hasDebugInfo()) {
-          DebugBinary = std::move(*DebugBinaryOpt);
+          FetchedBinary = std::move(*DebugBinaryOpt);
           DbgObj = FetchedObj;
         }
       }


        


More information about the llvm-commits mailing list