]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/elfread.c
Add debuginfod support to GDB
[thirdparty/binutils-gdb.git] / gdb / elfread.c
index 453bca527e9b219aadab714c3faeff8cdcb121d9..d842d5b573dd5ed67634d8545f3bcf92002fc905 100644 (file)
@@ -49,6 +49,8 @@
 #include "mdebugread.h"
 #include "ctfread.h"
 #include "gdbsupport/gdb_string_view.h"
+#include "gdbsupport/scoped_fd.h"
+#include "debuginfod-support.h"
 
 /* Forward declarations.  */
 extern const struct sym_fns elf_sym_fns_gdb_index;
@@ -1316,8 +1318,36 @@ elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
          symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
                                    symfile_flags, objfile);
        }
-       else
+      else
+       {
          has_dwarf2 = false;
+         const struct bfd_build_id *build_id = build_id_bfd_get (objfile->obfd);
+
+         if (build_id != nullptr)
+           {
+             gdb::unique_xmalloc_ptr<char> symfile_path;
+             scoped_fd fd (debuginfod_debuginfo_query (build_id->data,
+                                                       build_id->size,
+                                                       objfile->original_name,
+                                                       &symfile_path));
+
+             if (fd.get () >= 0)
+               {
+                 /* File successfully retrieved from server.  */
+                 gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (symfile_path.get ()));
+
+                 if (debug_bfd == nullptr)
+                   warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
+                            objfile->original_name);
+                 else if (build_id_verify (debug_bfd.get (), build_id->size, build_id->data))
+                   {
+                     symbol_file_add_separate (debug_bfd.get (), symfile_path.get (),
+                                               symfile_flags, objfile);
+                     has_dwarf2 = true;
+                   }
+               }
+           }
+       }
     }
 
   /* Read the CTF section only if there is no DWARF info.  */