binutils-gdb/gdb/solib.h
Andrew Burgess fa826a4bbe gdb: improve shared library build-id check for core-files
When GDB opens a core file, in 'core_target::build_file_mappings ()',
we collection information about the files that are mapped into the
core file, specifically, the build-id and the DT_SONAME attribute for
the file, which will be set for some shared libraries.

We then cache the DT_SONAME to build-id information on the core file
bfd object in the function set_cbfd_soname_build_id.

Later, when we are loading the shared libraries for the core file, we
can use the library's file name to look in the DT_SONAME to build-id
map, and, if we find a matching entry, we can use the build-id to
validate that we are loading the correct shared library.

This works OK, but has some limitations: not every shared library will
have a DT_SONAME attribute.  Though it is good practice to add such an
attribute, it's not required.  A library without this attribute will
not have its build-id checked, which can lead to GDB loading the wrong
shared library.

What I want to do in this commit is to improve GDB's ability to use
the build-ids extracted in core_target::build_file_mappings to both
validate the shared libraries being loaded, and then to use these
build-ids to potentially find (via debuginfod) the shared library.

To do this I propose making the following changes to GDB:

(1) Rather than just recording the DT_SONAME to build-id mapping in
set_cbfd_soname_build_id, we should also record, the full filename to
build-id mapping, and also the memory ranges to build-id mapping for
every memory range covered by every mapped file.

(2) Add a new callback solib_ops::find_solib_addr.  This callback
takes a solib object and returns an (optional) address within the
inferior that is part of this library.  We can use this address to
find a mapped file using the stored memory ranges which will increase
the cases in which a match can be found.

(3) Move the mapped file record keeping out of solib.c and into
corelow.c.  Future commits will make use of this information from
other parts of GDB.  This information was never solib specific, it
lived in the solib.c file because that was the only user of the data,
but really, the data is all about the core file, and should be stored
in core_target, other parts of GDB can then query this data as needed.

Now, when we load a shared library for a core file, we do the
following lookups:

  1. Is the exact filename of the shared library found in the filename
  to build-id map?  If so then use this build-id for validation.

  2. Find an address within the shared library using ::find_solib_addr
  and then look for an entry in the mapped address to build-id map.
  If an entry is found then use this build-id.

  3. Finally, look in the soname to build-id map.  If an entry is
  found then use this build-id.

The addition of step #2 here means that GDB is now far more likely to
find a suitable build-id for a shared library.  Having acquired a
build-id the existing code for using debuginfod to lookup a shared
library object can trigger more often.

On top of this, we also create a build-id to filename map.  This is
useful as often a shared library is implemented as a symbolic link to
the actual shared library file.  The mapped file information is stored
based on the actual, real file name, while the shared library
information holds the original symbolic link file name.

If when loading the shared library, we find the symbolic link has
disappeared, we can use the build-id to file name map to check if the
actual file is still around, if it is (and if the build-id matches)
then we can fall back to use that file.  This is another way in which
we can slightly increase the chances that GDB will find the required
files when loading a core file.

Adding all of the above required pretty much a full rewrite of the
existing set_cbfd_soname_build_id function and the corresponding
get_cbfd_soname_build_id function, so I have taken the opportunity to
move the information caching out of solib.c and into corelow.c where
it is now accessed through the function core_target_find_mapped_file.

At this point the benefit of this move is not entirely obvious, though
I don't think the new location is significantly worse than where it
was originally.  The benefit though is that the cached information is
no longer tied to the shared library loading code.

I already have a second set of patches (not in this series) that make
use of this caching from elsewhere in GDB.  I've not included those
patches in this series as this series is already pretty big, but even
if those follow up patches don't arrive, I think the new location is
just as good as the original location.

Rather that caching the information within the core file BFD via the
registry mechanism, the information used for the mapped file lookup is
now stored within the core_file target directly.
2024-09-07 20:28:57 +01:00

140 lines
4.8 KiB
C

/* Shared library declarations for GDB, the GNU Debugger.
Copyright (C) 1992-2024 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef SOLIB_H
#define SOLIB_H
/* Forward decl's for prototypes */
struct solib;
struct target_ops;
struct solib_ops;
struct program_space;
#include "gdb_bfd.h"
#include "symfile-add-flags.h"
#include "gdbsupport/function-view.h"
/* Value of the 'set debug solib' configuration variable. */
extern bool debug_solib;
/* Print an "solib" debug statement. */
#define solib_debug_printf(fmt, ...) \
debug_prefixed_printf_cond (debug_solib, "solib", fmt, ##__VA_ARGS__)
#define SOLIB_SCOPED_DEBUG_START_END(fmt, ...) \
scoped_debug_start_end (debug_solib, "solib", fmt, ##__VA_ARGS__)
/* Called when we free all symtabs of PSPACE, to free the shared library
information as well. */
extern void clear_solib (program_space *pspace);
/* Called to add symbols from a shared library to gdb's symbol table. */
extern void solib_add (const char *, int, int);
extern bool solib_read_symbols (solib &, symfile_add_flags);
/* Function to be called when the inferior starts up, to discover the
names of shared libraries that are dynamically linked, the base
addresses to which they are linked, and sufficient information to
read in their symbols at a later time. */
extern void solib_create_inferior_hook (int from_tty);
/* If ADDR lies in a shared library, return its name. */
extern const char *solib_name_from_address (struct program_space *, CORE_ADDR);
/* Return true if ADDR lies within SOLIB. */
extern bool solib_contains_address_p (const solib &, CORE_ADDR);
/* Return whether the data starting at VADDR, size SIZE, must be kept
in a core file for shared libraries loaded before "gcore" is used
to be handled correctly when the core file is loaded. This only
applies when the section would otherwise not be kept in the core
file (in particular, for readonly sections). */
extern bool solib_keep_data_in_core (CORE_ADDR vaddr, unsigned long size);
/* Return true if PC lies in the dynamic symbol resolution code of the
run time loader. */
extern bool in_solib_dynsym_resolve_code (CORE_ADDR);
/* Discard symbols that were auto-loaded from shared libraries in PSPACE. */
extern void no_shared_libraries (program_space *pspace);
/* Synchronize GDB's shared object list with inferior's.
Extract the list of currently loaded shared objects from the
inferior, and compare it with the list of shared objects in the
current program space's list of shared libraries. Edit
so_list_head to bring it in sync with the inferior's new list.
If we notice that the inferior has unloaded some shared objects,
free any symbolic info GDB had read about those shared objects.
Don't load symbolic info for any new shared objects; just add them
to the list, and leave their symbols_loaded flag clear.
If FROM_TTY is non-null, feel free to print messages about what
we're doing. */
extern void update_solib_list (int from_tty);
/* Return true if NAME is the libpthread shared library. */
extern bool libpthread_name_p (const char *name);
/* Look up symbol from both symbol table and dynamic string table. */
extern CORE_ADDR gdb_bfd_lookup_symbol
(bfd *abfd, gdb::function_view<bool (const asymbol *)> match_sym);
/* Look up symbol from symbol table. */
extern CORE_ADDR gdb_bfd_lookup_symbol_from_symtab
(bfd *abfd, gdb::function_view<bool (const asymbol *)> match_sym);
/* Scan for DESIRED_DYNTAG in .dynamic section of ABFD. If DESIRED_DYNTAG is
found, 1 is returned and the corresponding PTR and PTR_ADDR are set. */
extern int gdb_bfd_scan_elf_dyntag (const int desired_dyntag, bfd *abfd,
CORE_ADDR *ptr, CORE_ADDR *ptr_addr);
/* If FILENAME refers to an ELF shared object then attempt to return the
string referred to by its DT_SONAME tag. */
extern gdb::unique_xmalloc_ptr<char> gdb_bfd_read_elf_soname
(const char *filename);
/* Enable or disable optional solib event breakpoints as appropriate. */
extern void update_solib_breakpoints (void);
/* Handle an solib event by calling solib_add. */
extern void handle_solib_event (void);
#endif /* SOLIB_H */