Home | History | Annotate | Line # | Download | only in gdb
solib-svr4.c revision 1.1.1.9
      1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
      2 
      3    Copyright (C) 1990-2024 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 
     21 #include "elf/external.h"
     22 #include "elf/common.h"
     23 #include "elf/mips.h"
     24 
     25 #include "extract-store-integer.h"
     26 #include "symtab.h"
     27 #include "bfd.h"
     28 #include "symfile.h"
     29 #include "objfiles.h"
     30 #include "gdbcore.h"
     31 #include "target.h"
     32 #include "inferior.h"
     33 #include "infrun.h"
     34 #include "regcache.h"
     35 #include "observable.h"
     36 
     37 #include "solist.h"
     38 #include "solib.h"
     39 #include "solib-svr4.h"
     40 
     41 #include "bfd-target.h"
     42 #include "elf-bfd.h"
     43 #include "exec.h"
     44 #include "auxv.h"
     45 #include "gdb_bfd.h"
     46 #include "probe.h"
     47 
     48 #include <map>
     49 
     50 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
     51 static int svr4_have_link_map_offsets (void);
     52 static void svr4_relocate_main_executable (void);
     53 static void probes_table_remove_objfile_probes (struct objfile *objfile);
     54 static void svr4_iterate_over_objfiles_in_search_order
     55   (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
     56    objfile *current_objfile);
     57 
     58 
     59 /* On SVR4 systems, a list of symbols in the dynamic linker where
     60    GDB can try to place a breakpoint to monitor shared library
     61    events.
     62 
     63    If none of these symbols are found, or other errors occur, then
     64    SVR4 systems will fall back to using a symbol as the "startup
     65    mapping complete" breakpoint address.  */
     66 
     67 static const char * const solib_break_names[] =
     68 {
     69   "r_debug_state",
     70   "_r_debug_state",
     71   "_dl_debug_state",
     72   "rtld_db_dlactivity",
     73   "__dl_rtld_db_dlactivity",
     74   "_rtld_debug_state",
     75 
     76   NULL
     77 };
     78 
     79 static const char * const bkpt_names[] =
     80 {
     81   "_start",
     82   "__start",
     83   "main",
     84   NULL
     85 };
     86 
     87 static const  char * const main_name_list[] =
     88 {
     89   "main_$main",
     90   NULL
     91 };
     92 
     93 /* What to do when a probe stop occurs.  */
     94 
     95 enum probe_action
     96 {
     97   /* Something went seriously wrong.  Stop using probes and
     98      revert to using the older interface.  */
     99   PROBES_INTERFACE_FAILED,
    100 
    101   /* No action is required.  The shared object list is still
    102      valid.  */
    103   DO_NOTHING,
    104 
    105   /* The shared object list should be reloaded entirely.  */
    106   FULL_RELOAD,
    107 
    108   /* Attempt to incrementally update the shared object list. If
    109      the update fails or is not possible, fall back to reloading
    110      the list in full.  */
    111   UPDATE_OR_RELOAD,
    112 };
    113 
    114 /* A probe's name and its associated action.  */
    115 
    116 struct probe_info
    117 {
    118   /* The name of the probe.  */
    119   const char *name;
    120 
    121   /* What to do when a probe stop occurs.  */
    122   enum probe_action action;
    123 };
    124 
    125 /* A list of named probes and their associated actions.  If all
    126    probes are present in the dynamic linker then the probes-based
    127    interface will be used.  */
    128 
    129 static const struct probe_info probe_info[] =
    130 {
    131   { "init_start", DO_NOTHING },
    132   { "init_complete", FULL_RELOAD },
    133   { "map_start", DO_NOTHING },
    134   { "map_failed", DO_NOTHING },
    135   { "reloc_complete", UPDATE_OR_RELOAD },
    136   { "unmap_start", DO_NOTHING },
    137   { "unmap_complete", FULL_RELOAD },
    138 };
    139 
    140 #define NUM_PROBES ARRAY_SIZE (probe_info)
    141 
    142 /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
    143    the same shared library.  */
    144 
    145 static int
    146 svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
    147 {
    148   if (strcmp (gdb_so_name, inferior_so_name) == 0)
    149     return 1;
    150 
    151   /* On Solaris, when starting inferior we think that dynamic linker is
    152      /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
    153      contains /lib/ld.so.1.  Sometimes one file is a link to another, but
    154      sometimes they have identical content, but are not linked to each
    155      other.  We don't restrict this check for Solaris, but the chances
    156      of running into this situation elsewhere are very low.  */
    157   if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
    158       && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
    159     return 1;
    160 
    161   /* Similarly, we observed the same issue with amd64 and sparcv9, but with
    162      different locations.  */
    163   if (strcmp (gdb_so_name, "/usr/lib/amd64/ld.so.1") == 0
    164       && strcmp (inferior_so_name, "/lib/amd64/ld.so.1") == 0)
    165     return 1;
    166 
    167   if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
    168       && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
    169     return 1;
    170 
    171   return 0;
    172 }
    173 
    174 static bool
    175 svr4_same (const char *gdb_name, const char *inferior_name,
    176 	   const lm_info_svr4 &gdb_lm_info,
    177 	   const lm_info_svr4 &inferior_lm_info)
    178 {
    179   if (!svr4_same_1 (gdb_name, inferior_name))
    180     return false;
    181 
    182   /* There may be different instances of the same library, in different
    183      namespaces.  Each instance, however, must have been loaded at a
    184      different address so its relocation offset would be different.  */
    185   return gdb_lm_info.l_addr_inferior == inferior_lm_info.l_addr_inferior;
    186 }
    187 
    188 static int
    189 svr4_same (const solib &gdb, const solib &inferior)
    190 {
    191   auto *lmg
    192     = gdb::checked_static_cast<const lm_info_svr4 *> (gdb.lm_info.get ());
    193   auto *lmi
    194     = gdb::checked_static_cast<const lm_info_svr4 *> (inferior.lm_info.get ());
    195 
    196   return svr4_same (gdb.so_original_name.c_str (),
    197 		    inferior.so_original_name.c_str (), *lmg, *lmi);
    198 }
    199 
    200 static lm_info_svr4_up
    201 lm_info_read (CORE_ADDR lm_addr)
    202 {
    203   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    204   lm_info_svr4_up lm_info;
    205 
    206   gdb::byte_vector lm (lmo->link_map_size);
    207 
    208   if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
    209     warning (_("Error reading shared library list entry at %s"),
    210 	     paddress (current_inferior ()->arch (), lm_addr));
    211   else
    212     {
    213       type *ptr_type
    214 	= builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    215 
    216       lm_info = std::make_unique<lm_info_svr4> ();
    217       lm_info->lm_addr = lm_addr;
    218 
    219       lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
    220 							ptr_type);
    221       lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
    222       lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
    223 					       ptr_type);
    224       lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
    225 					       ptr_type);
    226       lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
    227 					       ptr_type);
    228     }
    229 
    230   return lm_info;
    231 }
    232 
    233 static int
    234 has_lm_dynamic_from_link_map (void)
    235 {
    236   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    237 
    238   return lmo->l_ld_offset >= 0;
    239 }
    240 
    241 static CORE_ADDR
    242 lm_addr_check (const solib &so, bfd *abfd)
    243 {
    244   auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
    245 
    246   if (!li->l_addr_p)
    247     {
    248       struct bfd_section *dyninfo_sect;
    249       CORE_ADDR l_addr, l_dynaddr, dynaddr;
    250 
    251       l_addr = li->l_addr_inferior;
    252 
    253       if (! abfd || ! has_lm_dynamic_from_link_map ())
    254 	goto set_addr;
    255 
    256       l_dynaddr = li->l_ld;
    257 
    258       dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
    259       if (dyninfo_sect == NULL)
    260 	goto set_addr;
    261 
    262       dynaddr = bfd_section_vma (dyninfo_sect);
    263 
    264       if (dynaddr + l_addr != l_dynaddr)
    265 	{
    266 	  CORE_ADDR align = 0x1000;
    267 	  CORE_ADDR minpagesize = align;
    268 
    269 	  if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
    270 	    {
    271 	      Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
    272 	      Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
    273 	      int i;
    274 
    275 	      align = 1;
    276 
    277 	      for (i = 0; i < ehdr->e_phnum; i++)
    278 		if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
    279 		  align = phdr[i].p_align;
    280 
    281 	      minpagesize = get_elf_backend_data (abfd)->minpagesize;
    282 	    }
    283 
    284 	  /* Turn it into a mask.  */
    285 	  align--;
    286 
    287 	  /* If the changes match the alignment requirements, we
    288 	     assume we're using a core file that was generated by the
    289 	     same binary, just prelinked with a different base offset.
    290 	     If it doesn't match, we may have a different binary, the
    291 	     same binary with the dynamic table loaded at an unrelated
    292 	     location, or anything, really.  To avoid regressions,
    293 	     don't adjust the base offset in the latter case, although
    294 	     odds are that, if things really changed, debugging won't
    295 	     quite work.
    296 
    297 	     One could expect more the condition
    298 	       ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
    299 	     but the one below is relaxed for PPC.  The PPC kernel supports
    300 	     either 4k or 64k page sizes.  To be prepared for 64k pages,
    301 	     PPC ELF files are built using an alignment requirement of 64k.
    302 	     However, when running on a kernel supporting 4k pages, the memory
    303 	     mapping of the library may not actually happen on a 64k boundary!
    304 
    305 	     (In the usual case where (l_addr & align) == 0, this check is
    306 	     equivalent to the possibly expected check above.)
    307 
    308 	     Even on PPC it must be zero-aligned at least for MINPAGESIZE.  */
    309 
    310 	  l_addr = l_dynaddr - dynaddr;
    311 
    312 	  if ((l_addr & (minpagesize - 1)) == 0
    313 	      && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
    314 	    {
    315 	      if (info_verbose)
    316 		gdb_printf (_("Using PIC (Position Independent Code) "
    317 			      "prelink displacement %s for \"%s\".\n"),
    318 			    paddress (current_inferior ()->arch (), l_addr),
    319 			    so.so_name.c_str ());
    320 	    }
    321 	  else
    322 	    {
    323 	      /* There is no way to verify the library file matches.  prelink
    324 		 can during prelinking of an unprelinked file (or unprelinking
    325 		 of a prelinked file) shift the DYNAMIC segment by arbitrary
    326 		 offset without any page size alignment.  There is no way to
    327 		 find out the ELF header and/or Program Headers for a limited
    328 		 verification if it they match.  One could do a verification
    329 		 of the DYNAMIC segment.  Still the found address is the best
    330 		 one GDB could find.  */
    331 
    332 	      warning (_(".dynamic section for \"%s\" "
    333 			 "is not at the expected address "
    334 			 "(wrong library or version mismatch?)"),
    335 			 so.so_name.c_str ());
    336 	    }
    337 	}
    338 
    339     set_addr:
    340       li->l_addr = l_addr;
    341       li->l_addr_p = 1;
    342     }
    343 
    344   return li->l_addr;
    345 }
    346 
    347 struct svr4_so
    348 {
    349   svr4_so (const char *name, lm_info_svr4_up lm_info)
    350     : name (name), lm_info (std::move (lm_info))
    351   {}
    352 
    353   std::string name;
    354   lm_info_svr4_up lm_info;
    355 };
    356 
    357 /* Per pspace SVR4 specific data.  */
    358 
    359 struct svr4_info
    360 {
    361   /* Base of dynamic linker structures in default namespace.  */
    362   CORE_ADDR debug_base = 0;
    363 
    364   /* Validity flag for debug_loader_offset.  */
    365   int debug_loader_offset_p = 0;
    366 
    367   /* Load address for the dynamic linker, inferred.  */
    368   CORE_ADDR debug_loader_offset = 0;
    369 
    370   /* Name of the dynamic linker, valid if debug_loader_offset_p.  */
    371   char *debug_loader_name = nullptr;
    372 
    373   /* Load map address for the main executable in default namespace.  */
    374   CORE_ADDR main_lm_addr = 0;
    375 
    376   CORE_ADDR interp_text_sect_low = 0;
    377   CORE_ADDR interp_text_sect_high = 0;
    378   CORE_ADDR interp_plt_sect_low = 0;
    379   CORE_ADDR interp_plt_sect_high = 0;
    380 
    381   /* True if the list of objects was last obtained from the target
    382      via qXfer:libraries-svr4:read.  */
    383   bool using_xfer = false;
    384 
    385   /* Table of struct probe_and_action instances, used by the
    386      probes-based interface to map breakpoint addresses to probes
    387      and their associated actions.  Lookup is performed using
    388      probe_and_action->prob->address.  */
    389   htab_up probes_table;
    390 
    391   /* List of objects loaded into the inferior per namespace, used by the
    392      probes-based interface.
    393 
    394      The namespace is represented by the address of its corresponding
    395      r_debug[_ext] object.  We get the namespace id as argument to the
    396      'reloc_complete' probe but we don't get it when scanning the load map
    397      on attach.
    398 
    399      The r_debug[_ext] objects may move when ld.so itself moves.  In that
    400      case, we expect also the global _r_debug to move so we can detect
    401      this and reload everything.  The r_debug[_ext] objects are not
    402      expected to move individually.
    403 
    404      The special entry zero is reserved for a linear list to support
    405      gdbstubs that do not support namespaces.  */
    406   std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists;
    407 };
    408 
    409 /* Per-program-space data key.  */
    410 static const registry<program_space>::key<svr4_info> solib_svr4_pspace_data;
    411 
    412 /* Return whether DEBUG_BASE is the default namespace of INFO.  */
    413 
    414 static bool
    415 svr4_is_default_namespace (const svr4_info *info, CORE_ADDR debug_base)
    416 {
    417   return (debug_base == info->debug_base);
    418 }
    419 
    420 /* Free the probes table.  */
    421 
    422 static void
    423 free_probes_table (struct svr4_info *info)
    424 {
    425   info->probes_table.reset (nullptr);
    426 }
    427 
    428 /* Get the svr4 data for program space PSPACE.  If none is found yet, add it now.
    429    This function always returns a valid object.  */
    430 
    431 static struct svr4_info *
    432 get_svr4_info (program_space *pspace)
    433 {
    434   struct svr4_info *info = solib_svr4_pspace_data.get (pspace);
    435 
    436   if (info == NULL)
    437     info = solib_svr4_pspace_data.emplace (pspace);
    438 
    439   return info;
    440 }
    441 
    442 /* Local function prototypes */
    443 
    444 static int match_main (const char *);
    445 
    446 /* Read program header TYPE from inferior memory.  The header is found
    447    by scanning the OS auxiliary vector.
    448 
    449    If TYPE == -1, return the program headers instead of the contents of
    450    one program header.
    451 
    452    Return vector of bytes holding the program header contents, or an empty
    453    optional on failure.  If successful and P_ARCH_SIZE is non-NULL, the target
    454    architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE.  Likewise,
    455    the base address of the section is returned in *BASE_ADDR.  */
    456 
    457 static std::optional<gdb::byte_vector>
    458 read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
    459 {
    460   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
    461   CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
    462   int arch_size, sect_size;
    463   CORE_ADDR sect_addr;
    464   int pt_phdr_p = 0;
    465 
    466   /* Get required auxv elements from target.  */
    467   if (target_auxv_search (AT_PHDR, &at_phdr) <= 0)
    468     return {};
    469   if (target_auxv_search (AT_PHENT, &at_phent) <= 0)
    470     return {};
    471   if (target_auxv_search (AT_PHNUM, &at_phnum) <= 0)
    472     return {};
    473   if (!at_phdr || !at_phnum)
    474     return {};
    475 
    476   /* Determine ELF architecture type.  */
    477   if (at_phent == sizeof (Elf32_External_Phdr))
    478     arch_size = 32;
    479   else if (at_phent == sizeof (Elf64_External_Phdr))
    480     arch_size = 64;
    481   else
    482     return {};
    483 
    484   /* Find the requested segment.  */
    485   if (type == -1)
    486     {
    487       sect_addr = at_phdr;
    488       sect_size = at_phent * at_phnum;
    489     }
    490   else if (arch_size == 32)
    491     {
    492       Elf32_External_Phdr phdr;
    493       int i;
    494 
    495       /* Search for requested PHDR.  */
    496       for (i = 0; i < at_phnum; i++)
    497 	{
    498 	  int p_type;
    499 
    500 	  if (target_read_memory (at_phdr + i * sizeof (phdr),
    501 				  (gdb_byte *)&phdr, sizeof (phdr)))
    502 	    return {};
    503 
    504 	  p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
    505 					     4, byte_order);
    506 
    507 	  if (p_type == PT_PHDR)
    508 	    {
    509 	      pt_phdr_p = 1;
    510 	      pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
    511 						  4, byte_order);
    512 	    }
    513 
    514 	  if (p_type == type)
    515 	    break;
    516 	}
    517 
    518       if (i == at_phnum)
    519 	return {};
    520 
    521       /* Retrieve address and size.  */
    522       sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
    523 					    4, byte_order);
    524       sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
    525 					    4, byte_order);
    526     }
    527   else
    528     {
    529       Elf64_External_Phdr phdr;
    530       int i;
    531 
    532       /* Search for requested PHDR.  */
    533       for (i = 0; i < at_phnum; i++)
    534 	{
    535 	  int p_type;
    536 
    537 	  if (target_read_memory (at_phdr + i * sizeof (phdr),
    538 				  (gdb_byte *)&phdr, sizeof (phdr)))
    539 	    return {};
    540 
    541 	  p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
    542 					     4, byte_order);
    543 
    544 	  if (p_type == PT_PHDR)
    545 	    {
    546 	      pt_phdr_p = 1;
    547 	      pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
    548 						  8, byte_order);
    549 	    }
    550 
    551 	  if (p_type == type)
    552 	    break;
    553 	}
    554 
    555       if (i == at_phnum)
    556 	return {};
    557 
    558       /* Retrieve address and size.  */
    559       sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
    560 					    8, byte_order);
    561       sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
    562 					    8, byte_order);
    563     }
    564 
    565   /* PT_PHDR is optional, but we really need it
    566      for PIE to make this work in general.  */
    567 
    568   if (pt_phdr_p)
    569     {
    570       /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
    571 	 Relocation offset is the difference between the two. */
    572       sect_addr = sect_addr + (at_phdr - pt_phdr);
    573     }
    574 
    575   /* Read in requested program header.  */
    576   gdb::byte_vector buf (sect_size);
    577   if (target_read_memory (sect_addr, buf.data (), sect_size))
    578     return {};
    579 
    580 #if defined(__NetBSD__) && defined(__m68k__)
    581   /*
    582    * XXX PR toolchain/56268
    583    *
    584    * For NetBSD/m68k, program header is erroneously readable from core dump,
    585    * although a page containing it is missing. This spoils relocation for
    586    * the main executable, and debugging with core dumps becomes impossible,
    587    * as described in toolchain/56268.
    588    *
    589    * In order to avoid this failure, we carry out consistency check for
    590    * program header; for NetBSD, 1st entry of program header refers program
    591    * header itself. If this is not the case, we should be reading random
    592    * garbage from core dump.
    593    */
    594   if (type == -1 && arch_size == 32)
    595     {
    596       Elf32_External_Phdr phdr;
    597       int p_type, p_filesz, p_memsz;
    598 
    599       if (target_read_memory (at_phdr, (gdb_byte *)&phdr, sizeof (phdr)))
    600         return {};
    601 
    602       p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type, 4,
    603 					 byte_order);
    604       p_filesz = extract_unsigned_integer ((gdb_byte *)phdr.p_filesz, 4,
    605 					   byte_order);
    606       p_memsz = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz, 4,
    607 					  byte_order);
    608 
    609       if (p_type != PT_PHDR || p_filesz != sect_size || p_memsz != sect_size)
    610 	return {};
    611     }
    612 #endif
    613 
    614   if (p_arch_size)
    615     *p_arch_size = arch_size;
    616   if (base_addr)
    617     *base_addr = sect_addr;
    618 
    619   return buf;
    620 }
    621 
    622 
    623 /* Return program interpreter string.  */
    624 static std::optional<gdb::byte_vector>
    625 find_program_interpreter (void)
    626 {
    627   /* If we have a current exec_bfd, use its section table.  */
    628   if (current_program_space->exec_bfd ()
    629       && (bfd_get_flavour (current_program_space->exec_bfd ())
    630 	  == bfd_target_elf_flavour))
    631    {
    632      struct bfd_section *interp_sect;
    633 
    634      interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
    635 					    ".interp");
    636      if (interp_sect != NULL)
    637       {
    638 	int sect_size = bfd_section_size (interp_sect);
    639 
    640 	gdb::byte_vector buf (sect_size);
    641 	bool res
    642 	  = bfd_get_section_contents (current_program_space->exec_bfd (),
    643 				      interp_sect, buf.data (), 0, sect_size);
    644 	if (res)
    645 	  return buf;
    646       }
    647    }
    648 
    649   /* If we didn't find it, use the target auxiliary vector.  */
    650   return read_program_header (PT_INTERP, NULL, NULL);
    651 }
    652 
    653 
    654 /* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
    655    found by consulting the OS auxillary vector.  If DESIRED_DYNTAG is found, 1
    656    is returned and the corresponding PTR is set.  */
    657 
    658 static int
    659 scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr,
    660 		  CORE_ADDR *ptr_addr)
    661 {
    662   bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
    663   int arch_size, step;
    664   long current_dyntag;
    665   CORE_ADDR dyn_ptr;
    666   CORE_ADDR base_addr;
    667 
    668   /* Read in .dynamic section.  */
    669   std::optional<gdb::byte_vector> ph_data
    670     = read_program_header (PT_DYNAMIC, &arch_size, &base_addr);
    671   if (!ph_data)
    672     return 0;
    673 
    674   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
    675   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
    676 			   : sizeof (Elf64_External_Dyn);
    677   for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size ();
    678        buf < bufend; buf += step)
    679   {
    680     if (arch_size == 32)
    681       {
    682 	Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
    683 
    684 	current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
    685 					    4, byte_order);
    686 	dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
    687 					    4, byte_order);
    688       }
    689     else
    690       {
    691 	Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
    692 
    693 	current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
    694 					    8, byte_order);
    695 	dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
    696 					    8, byte_order);
    697       }
    698     if (current_dyntag == DT_NULL)
    699       break;
    700 
    701     if (current_dyntag == desired_dyntag)
    702       {
    703 	if (ptr)
    704 	  *ptr = dyn_ptr;
    705 
    706 	if (ptr_addr)
    707 	  *ptr_addr = base_addr + buf - ph_data->data ();
    708 
    709 	return 1;
    710       }
    711   }
    712 
    713   return 0;
    714 }
    715 
    716 /* Locate the base address of dynamic linker structs for SVR4 elf
    717    targets.
    718 
    719    For SVR4 elf targets the address of the dynamic linker's runtime
    720    structure is contained within the dynamic info section in the
    721    executable file.  The dynamic section is also mapped into the
    722    inferior address space.  Because the runtime loader fills in the
    723    real address before starting the inferior, we have to read in the
    724    dynamic info section from the inferior address space.
    725    If there are any errors while trying to find the address, we
    726    silently return 0, otherwise the found address is returned.  */
    727 
    728 static CORE_ADDR
    729 elf_locate_base (void)
    730 {
    731   struct bound_minimal_symbol msymbol;
    732   CORE_ADDR dyn_ptr, dyn_ptr_addr;
    733 
    734   if (!svr4_have_link_map_offsets ())
    735     return 0;
    736 
    737   /* Look for DT_MIPS_RLD_MAP first.  MIPS executables use this
    738      instead of DT_DEBUG, although they sometimes contain an unused
    739      DT_DEBUG.  */
    740   if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP,
    741 			       current_program_space->exec_bfd (),
    742 			       &dyn_ptr, NULL)
    743       || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
    744     {
    745       type *ptr_type
    746 	= builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    747       gdb_byte *pbuf;
    748       int pbuf_size = ptr_type->length ();
    749 
    750       pbuf = (gdb_byte *) alloca (pbuf_size);
    751       /* DT_MIPS_RLD_MAP contains a pointer to the address
    752 	 of the dynamic link structure.  */
    753       if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
    754 	return 0;
    755       return extract_typed_address (pbuf, ptr_type);
    756     }
    757 
    758   /* Then check DT_MIPS_RLD_MAP_REL.  MIPS executables now use this form
    759      because of needing to support PIE.  DT_MIPS_RLD_MAP will also exist
    760      in non-PIE.  */
    761   if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP_REL,
    762 			       current_program_space->exec_bfd (),
    763 			       &dyn_ptr, &dyn_ptr_addr)
    764       || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
    765     {
    766       type *ptr_type
    767 	= builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    768       gdb_byte *pbuf;
    769       int pbuf_size = ptr_type->length ();
    770 
    771       pbuf = (gdb_byte *) alloca (pbuf_size);
    772       /* DT_MIPS_RLD_MAP_REL contains an offset from the address of the
    773 	 DT slot to the address of the dynamic link structure.  */
    774       if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size))
    775 	return 0;
    776       return extract_typed_address (pbuf, ptr_type);
    777     }
    778 
    779   /* Find DT_DEBUG.  */
    780   if (gdb_bfd_scan_elf_dyntag (DT_DEBUG, current_program_space->exec_bfd (),
    781 			       &dyn_ptr, NULL)
    782       || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
    783     return dyn_ptr;
    784 
    785   /* This may be a static executable.  Look for the symbol
    786      conventionally named _r_debug, as a last resort.  */
    787   msymbol = lookup_minimal_symbol ("_r_debug", NULL,
    788 				   current_program_space->symfile_object_file);
    789   if (msymbol.minsym != NULL)
    790     return msymbol.value_address ();
    791 
    792   /* DT_DEBUG entry not found.  */
    793   return 0;
    794 }
    795 
    796 /* Find the first element in the inferior's dynamic link map, and
    797    return its address in the inferior.  Return zero if the address
    798    could not be determined.
    799 
    800    FIXME: Perhaps we should validate the info somehow, perhaps by
    801    checking r_version for a known version number, or r_state for
    802    RT_CONSISTENT.  */
    803 
    804 static CORE_ADDR
    805 solib_svr4_r_map (CORE_ADDR debug_base)
    806 {
    807   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    808   type *ptr_type
    809     = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    810   CORE_ADDR addr = 0;
    811 
    812   try
    813     {
    814       addr = read_memory_typed_address (debug_base + lmo->r_map_offset,
    815 					ptr_type);
    816     }
    817   catch (const gdb_exception_error &ex)
    818     {
    819       exception_print (gdb_stderr, ex);
    820     }
    821 
    822   return addr;
    823 }
    824 
    825 /* Find r_brk from the inferior's debug base.  */
    826 
    827 static CORE_ADDR
    828 solib_svr4_r_brk (struct svr4_info *info)
    829 {
    830   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    831   type *ptr_type
    832     = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    833 
    834   return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
    835 				    ptr_type);
    836 }
    837 
    838 /* Find the link map for the dynamic linker (if it is not in the
    839    normal list of loaded shared objects).  */
    840 
    841 static CORE_ADDR
    842 solib_svr4_r_ldsomap (struct svr4_info *info)
    843 {
    844   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    845   type *ptr_type
    846     = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    847   enum bfd_endian byte_order = type_byte_order (ptr_type);
    848   ULONGEST version = 0;
    849 
    850   try
    851     {
    852       /* Check version, and return zero if `struct r_debug' doesn't have
    853 	 the r_ldsomap member.  */
    854       version
    855 	= read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
    856 					lmo->r_version_size, byte_order);
    857     }
    858   catch (const gdb_exception_error &ex)
    859     {
    860       exception_print (gdb_stderr, ex);
    861     }
    862 
    863   if (version < 2 || lmo->r_ldsomap_offset == -1)
    864     return 0;
    865 
    866   return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
    867 				    ptr_type);
    868 }
    869 
    870 /* Find the next namespace from the r_next field.  */
    871 
    872 static CORE_ADDR
    873 solib_svr4_r_next (CORE_ADDR debug_base)
    874 {
    875   link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    876   type *ptr_type
    877     = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    878   bfd_endian byte_order = type_byte_order (ptr_type);
    879   ULONGEST version = 0;
    880 
    881   try
    882     {
    883       version
    884 	= read_memory_unsigned_integer (debug_base + lmo->r_version_offset,
    885 					lmo->r_version_size, byte_order);
    886     }
    887   catch (const gdb_exception_error &ex)
    888     {
    889       exception_print (gdb_stderr, ex);
    890     }
    891 
    892   /* The r_next field is added with r_version == 2.  */
    893   if (version < 2 || lmo->r_next_offset == -1)
    894     return 0;
    895 
    896   return read_memory_typed_address (debug_base + lmo->r_next_offset,
    897 				    ptr_type);
    898 }
    899 
    900 /* On Solaris systems with some versions of the dynamic linker,
    901    ld.so's l_name pointer points to the SONAME in the string table
    902    rather than into writable memory.  So that GDB can find shared
    903    libraries when loading a core file generated by gcore, ensure that
    904    memory areas containing the l_name string are saved in the core
    905    file.  */
    906 
    907 static int
    908 svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
    909 {
    910   struct svr4_info *info;
    911   CORE_ADDR ldsomap;
    912   CORE_ADDR name_lm;
    913 
    914   info = get_svr4_info (current_program_space);
    915 
    916   info->debug_base = elf_locate_base ();
    917   if (info->debug_base == 0)
    918     return 0;
    919 
    920   ldsomap = solib_svr4_r_ldsomap (info);
    921   if (!ldsomap)
    922     return 0;
    923 
    924   std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
    925   name_lm = li != NULL ? li->l_name : 0;
    926 
    927   return (name_lm >= vaddr && name_lm < vaddr + size);
    928 }
    929 
    930 /* See solist.h.  */
    931 
    932 static int
    933 open_symbol_file_object (int from_tty)
    934 {
    935   CORE_ADDR lm, l_name;
    936   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
    937   type *ptr_type
    938     = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
    939   int l_name_size = ptr_type->length ();
    940   gdb::byte_vector l_name_buf (l_name_size);
    941   struct svr4_info *info = get_svr4_info (current_program_space);
    942   symfile_add_flags add_flags = 0;
    943 
    944   if (from_tty)
    945     add_flags |= SYMFILE_VERBOSE;
    946 
    947   if (current_program_space->symfile_object_file)
    948     if (!query (_("Attempt to reload symbols from process? ")))
    949       return 0;
    950 
    951   /* Always locate the debug struct, in case it has moved.  */
    952   info->debug_base = elf_locate_base ();
    953   if (info->debug_base == 0)
    954     return 0;	/* failed somehow...  */
    955 
    956   /* First link map member should be the executable.  */
    957   lm = solib_svr4_r_map (info->debug_base);
    958   if (lm == 0)
    959     return 0;	/* failed somehow...  */
    960 
    961   /* Read address of name from target memory to GDB.  */
    962   read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
    963 
    964   /* Convert the address to host format.  */
    965   l_name = extract_typed_address (l_name_buf.data (), ptr_type);
    966 
    967   if (l_name == 0)
    968     return 0;		/* No filename.  */
    969 
    970   /* Now fetch the filename from target memory.  */
    971   gdb::unique_xmalloc_ptr<char> filename
    972     = target_read_string (l_name, SO_NAME_MAX_PATH_SIZE - 1);
    973 
    974   if (filename == nullptr)
    975     {
    976       warning (_("failed to read exec filename from attached file"));
    977       return 0;
    978     }
    979 
    980   /* Have a pathname: read the symbol file.  */
    981   symbol_file_add_main (filename.get (), add_flags);
    982 
    983   return 1;
    984 }
    985 
    986 /* Data exchange structure for the XML parser as returned by
    987    svr4_current_sos_via_xfer_libraries.  */
    988 
    989 struct svr4_library_list
    990 {
    991   /* The so list for the current namespace.  This is internal to XML
    992      parsing.  */
    993   std::vector<svr4_so> *cur_list;
    994 
    995   /* Inferior address of struct link_map used for the main executable.  It is
    996      NULL if not known.  */
    997   CORE_ADDR main_lm;
    998 
    999   /* List of objects loaded into the inferior per namespace.  This does
   1000      not include any default sos.
   1001 
   1002      See comment on struct svr4_info.solib_lists.  */
   1003   std::map<CORE_ADDR, std::vector<svr4_so>> solib_lists;
   1004 };
   1005 
   1006 /* This module's 'free_objfile' observer.  */
   1007 
   1008 static void
   1009 svr4_free_objfile_observer (struct objfile *objfile)
   1010 {
   1011   probes_table_remove_objfile_probes (objfile);
   1012 }
   1013 
   1014 /* Implement solib_ops.clear_so.  */
   1015 
   1016 static void
   1017 svr4_clear_so (const solib &so)
   1018 {
   1019   auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
   1020 
   1021   if (li != NULL)
   1022     li->l_addr_p = 0;
   1023 }
   1024 
   1025 /* Create the so_list objects equivalent to the svr4_sos in SOS.  */
   1026 
   1027 static intrusive_list<solib>
   1028 so_list_from_svr4_sos (const std::vector<svr4_so> &sos)
   1029 {
   1030   intrusive_list<solib> dst;
   1031 
   1032   for (const svr4_so &so : sos)
   1033     {
   1034       struct solib *newobj = new struct solib;
   1035 
   1036       newobj->so_name = so.name;
   1037       newobj->so_original_name = so.name;
   1038       newobj->lm_info = std::make_unique<lm_info_svr4> (*so.lm_info);
   1039 
   1040       dst.push_back (*newobj);
   1041     }
   1042 
   1043   return dst;
   1044 }
   1045 
   1046 #ifdef HAVE_LIBEXPAT
   1047 
   1048 #include "xml-support.h"
   1049 
   1050 /* Handle the start of a <library> element.  Note: new elements are added
   1051    at the tail of the list, keeping the list in order.  */
   1052 
   1053 static void
   1054 library_list_start_library (struct gdb_xml_parser *parser,
   1055 			    const struct gdb_xml_element *element,
   1056 			    void *user_data,
   1057 			    std::vector<gdb_xml_value> &attributes)
   1058 {
   1059   struct svr4_library_list *list = (struct svr4_library_list *) user_data;
   1060   const char *name
   1061     = (const char *) xml_find_attribute (attributes, "name")->value.get ();
   1062   ULONGEST *lmp
   1063     = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get ();
   1064   ULONGEST *l_addrp
   1065     = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
   1066   ULONGEST *l_ldp
   1067     = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
   1068 
   1069   lm_info_svr4_up li = std::make_unique<lm_info_svr4> ();
   1070   li->lm_addr = *lmp;
   1071   li->l_addr_inferior = *l_addrp;
   1072   li->l_ld = *l_ldp;
   1073 
   1074   std::vector<svr4_so> *solist;
   1075 
   1076   /* Older versions did not supply lmid.  Put the element into the flat
   1077      list of the special namespace zero in that case.  */
   1078   gdb_xml_value *at_lmid = xml_find_attribute (attributes, "lmid");
   1079   if (at_lmid == nullptr)
   1080     solist = list->cur_list;
   1081   else
   1082     {
   1083       ULONGEST lmid = *(ULONGEST *) at_lmid->value.get ();
   1084       solist = &list->solib_lists[lmid];
   1085     }
   1086 
   1087   solist->emplace_back (name, std::move (li));
   1088 }
   1089 
   1090 /* Handle the start of a <library-list-svr4> element.  */
   1091 
   1092 static void
   1093 svr4_library_list_start_list (struct gdb_xml_parser *parser,
   1094 			      const struct gdb_xml_element *element,
   1095 			      void *user_data,
   1096 			      std::vector<gdb_xml_value> &attributes)
   1097 {
   1098   struct svr4_library_list *list = (struct svr4_library_list *) user_data;
   1099   const char *version
   1100     = (const char *) xml_find_attribute (attributes, "version")->value.get ();
   1101   struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
   1102 
   1103   if (strcmp (version, "1.0") != 0)
   1104     gdb_xml_error (parser,
   1105 		   _("SVR4 Library list has unsupported version \"%s\""),
   1106 		   version);
   1107 
   1108   if (main_lm)
   1109     list->main_lm = *(ULONGEST *) main_lm->value.get ();
   1110 
   1111   /* Older gdbserver do not support namespaces.  We use the special
   1112      namespace zero for a linear list of libraries.  */
   1113   list->cur_list = &list->solib_lists[0];
   1114 }
   1115 
   1116 /* The allowed elements and attributes for an XML library list.
   1117    The root element is a <library-list>.  */
   1118 
   1119 static const struct gdb_xml_attribute svr4_library_attributes[] =
   1120 {
   1121   { "name", GDB_XML_AF_NONE, NULL, NULL },
   1122   { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   1123   { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   1124   { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   1125   { "lmid", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
   1126   { NULL, GDB_XML_AF_NONE, NULL, NULL }
   1127 };
   1128 
   1129 static const struct gdb_xml_element svr4_library_list_children[] =
   1130 {
   1131   {
   1132     "library", svr4_library_attributes, NULL,
   1133     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
   1134     library_list_start_library, NULL
   1135   },
   1136   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
   1137 };
   1138 
   1139 static const struct gdb_xml_attribute svr4_library_list_attributes[] =
   1140 {
   1141   { "version", GDB_XML_AF_NONE, NULL, NULL },
   1142   { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
   1143   { NULL, GDB_XML_AF_NONE, NULL, NULL }
   1144 };
   1145 
   1146 static const struct gdb_xml_element svr4_library_list_elements[] =
   1147 {
   1148   { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
   1149     GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
   1150   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
   1151 };
   1152 
   1153 /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN.  Return 1 if
   1154 
   1155    Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
   1156    case.  Return 1 if *SO_LIST_RETURN contains the library list, it may be
   1157    empty, caller is responsible for freeing all its entries.  */
   1158 
   1159 static int
   1160 svr4_parse_libraries (const char *document, struct svr4_library_list *list)
   1161 {
   1162   auto cleanup = make_scope_exit ([list] ()
   1163     {  list->solib_lists.clear (); });
   1164 
   1165   list->cur_list = nullptr;
   1166   list->main_lm = 0;
   1167   list->solib_lists.clear ();
   1168   if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
   1169 			   svr4_library_list_elements, document, list) == 0)
   1170     {
   1171       /* Parsed successfully, keep the result.  */
   1172       cleanup.release ();
   1173       return 1;
   1174     }
   1175 
   1176   return 0;
   1177 }
   1178 
   1179 /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
   1180 
   1181    Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
   1182    case.  Return 1 if *SO_LIST_RETURN contains the library list, it may be
   1183    empty, caller is responsible for freeing all its entries.
   1184 
   1185    Note that ANNEX must be NULL if the remote does not explicitly allow
   1186    qXfer:libraries-svr4:read packets with non-empty annexes.  Support for
   1187    this can be checked using target_augmented_libraries_svr4_read ().  */
   1188 
   1189 static int
   1190 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
   1191 				     const char *annex)
   1192 {
   1193   gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
   1194 
   1195   /* Fetch the list of shared libraries.  */
   1196   std::optional<gdb::char_vector> svr4_library_document
   1197     = target_read_stralloc (current_inferior ()->top_target (),
   1198 			    TARGET_OBJECT_LIBRARIES_SVR4,
   1199 			    annex);
   1200   if (!svr4_library_document)
   1201     return 0;
   1202 
   1203   return svr4_parse_libraries (svr4_library_document->data (), list);
   1204 }
   1205 
   1206 #else
   1207 
   1208 static int
   1209 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
   1210 				     const char *annex)
   1211 {
   1212   return 0;
   1213 }
   1214 
   1215 #endif
   1216 
   1217 /* If no shared library information is available from the dynamic
   1218    linker, build a fallback list from other sources.  */
   1219 
   1220 static intrusive_list<solib>
   1221 svr4_default_sos (svr4_info *info)
   1222 {
   1223   if (!info->debug_loader_offset_p)
   1224     return {};
   1225 
   1226   solib *newobj = new solib;
   1227   auto li = std::make_unique<lm_info_svr4> ();
   1228 
   1229   /* Nothing will ever check the other fields if we set l_addr_p.  */
   1230   li->l_addr = li->l_addr_inferior = info->debug_loader_offset;
   1231   li->l_addr_p = 1;
   1232 
   1233   newobj->lm_info = std::move (li);
   1234   newobj->so_name = info->debug_loader_name;
   1235   newobj->so_original_name = newobj->so_name;
   1236 
   1237   intrusive_list<solib> sos;
   1238   sos.push_back (*newobj);
   1239 
   1240   return sos;
   1241 }
   1242 
   1243 /* Read the whole inferior libraries chain starting at address LM.
   1244    Expect the first entry in the chain's previous entry to be PREV_LM.
   1245    Add the entries to SOS.  Ignore the first entry if IGNORE_FIRST and set
   1246    global MAIN_LM_ADDR according to it.  Returns nonzero upon success.  If zero
   1247    is returned the entries stored to LINK_PTR_PTR are still valid although they may
   1248    represent only part of the inferior library list.  */
   1249 
   1250 static int
   1251 svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
   1252 		   std::vector<svr4_so> &sos, int ignore_first)
   1253 {
   1254   CORE_ADDR first_l_name = 0;
   1255   CORE_ADDR next_lm;
   1256 
   1257   for (; lm != 0; prev_lm = lm, lm = next_lm)
   1258     {
   1259       lm_info_svr4_up li = lm_info_read (lm);
   1260       if (li == NULL)
   1261 	return 0;
   1262 
   1263       next_lm = li->l_next;
   1264 
   1265       if (li->l_prev != prev_lm)
   1266 	{
   1267 	  warning (_("Corrupted shared library list: %s != %s"),
   1268 		   paddress (current_inferior ()->arch (), prev_lm),
   1269 		   paddress (current_inferior ()->arch (), li->l_prev));
   1270 	  return 0;
   1271 	}
   1272 
   1273       /* For SVR4 versions, the first entry in the link map is for the
   1274 	 inferior executable, so we must ignore it.  For some versions of
   1275 	 SVR4, it has no name.  For others (Solaris 2.3 for example), it
   1276 	 does have a name, so we can no longer use a missing name to
   1277 	 decide when to ignore it.  */
   1278       if (ignore_first && li->l_prev == 0)
   1279 	{
   1280 	  first_l_name = li->l_name;
   1281 	  info->main_lm_addr = li->lm_addr;
   1282 	  continue;
   1283 	}
   1284 
   1285       /* Extract this shared object's name.  */
   1286       gdb::unique_xmalloc_ptr<char> name
   1287 	= target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1);
   1288       if (name == nullptr)
   1289 	{
   1290 	  /* If this entry's l_name address matches that of the
   1291 	     inferior executable, then this is not a normal shared
   1292 	     object, but (most likely) a vDSO.  In this case, silently
   1293 	     skip it; otherwise emit a warning. */
   1294 	  if (first_l_name == 0 || li->l_name != first_l_name)
   1295 	    warning (_("Can't read pathname for load map."));
   1296 	  continue;
   1297 	}
   1298 
   1299       /* If this entry has no name, or its name matches the name
   1300 	 for the main executable, don't include it in the list.  */
   1301       if (*name == '\0' || match_main (name.get ()))
   1302 	continue;
   1303 
   1304       sos.emplace_back (name.get (), std::move (li));
   1305     }
   1306 
   1307   return 1;
   1308 }
   1309 
   1310 /* Read the full list of currently loaded shared objects directly
   1311    from the inferior, without referring to any libraries read and
   1312    stored by the probes interface.  Handle special cases relating
   1313    to the first elements of the list in default namespace.  */
   1314 
   1315 static void
   1316 svr4_current_sos_direct (struct svr4_info *info)
   1317 {
   1318   CORE_ADDR lm;
   1319   bool ignore_first;
   1320   struct svr4_library_list library_list;
   1321 
   1322   /* Remove any old libraries.  We're going to read them back in again.  */
   1323   info->solib_lists.clear ();
   1324 
   1325   /* Fall back to manual examination of the target if the packet is not
   1326      supported or gdbserver failed to find DT_DEBUG.  gdb.server/solib-list.exp
   1327      tests a case where gdbserver cannot find the shared libraries list while
   1328      GDB itself is able to find it via SYMFILE_OBJFILE.
   1329 
   1330      Unfortunately statically linked inferiors will also fall back through this
   1331      suboptimal code path.  */
   1332 
   1333   info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
   1334 							  NULL);
   1335   if (info->using_xfer)
   1336     {
   1337       if (library_list.main_lm)
   1338 	info->main_lm_addr = library_list.main_lm;
   1339 
   1340       /* Remove an empty special zero namespace so we know that when there
   1341 	 is one, it is actually used, and we have a flat list without
   1342 	 namespace information.  */
   1343       auto it_0 = library_list.solib_lists.find (0);
   1344       if (it_0 != library_list.solib_lists.end ()
   1345 	  && it_0->second.empty ())
   1346 	library_list.solib_lists.erase (it_0);
   1347 
   1348       /* Replace the (empty) solib_lists in INFO with the one generated
   1349 	 from the target.  We don't want to copy it on assignment and then
   1350 	 delete the original afterwards, so let's just swap the
   1351 	 internals.  */
   1352       std::swap (info->solib_lists, library_list.solib_lists);
   1353       return;
   1354     }
   1355 
   1356   /* If we can't find the dynamic linker's base structure, this
   1357      must not be a dynamically linked executable.  Hmm.  */
   1358   info->debug_base = elf_locate_base ();
   1359   if (info->debug_base == 0)
   1360     return;
   1361 
   1362   /* Assume that everything is a library if the dynamic loader was loaded
   1363      late by a static executable.  */
   1364   if (current_program_space->exec_bfd ()
   1365       && bfd_get_section_by_name (current_program_space->exec_bfd (),
   1366 				  ".dynamic") == NULL)
   1367     ignore_first = false;
   1368   else
   1369     ignore_first = true;
   1370 
   1371   auto cleanup = make_scope_exit ([info] ()
   1372     { info->solib_lists.clear (); });
   1373 
   1374   /* Collect the sos in each namespace.  */
   1375   CORE_ADDR debug_base = info->debug_base;
   1376   for (; debug_base != 0;
   1377        ignore_first = false, debug_base = solib_svr4_r_next (debug_base))
   1378     {
   1379       /* Walk the inferior's link map list, and build our so_list list.  */
   1380       lm = solib_svr4_r_map (debug_base);
   1381       if (lm != 0)
   1382 	svr4_read_so_list (info, lm, 0, info->solib_lists[debug_base],
   1383 			   ignore_first);
   1384     }
   1385 
   1386   /* On Solaris, the dynamic linker is not in the normal list of
   1387      shared objects, so make sure we pick it up too.  Having
   1388      symbol information for the dynamic linker is quite crucial
   1389      for skipping dynamic linker resolver code.
   1390 
   1391      Note that we interpret the ldsomap load map address as 'virtual'
   1392      r_debug object.  If we added it to the default namespace (as it was),
   1393      we would probably run into inconsistencies with the load map's
   1394      prev/next links (I wonder if we did).  */
   1395   debug_base = solib_svr4_r_ldsomap (info);
   1396   if (debug_base != 0)
   1397     {
   1398       /* Add the dynamic linker's namespace unless we already did.  */
   1399       if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
   1400 	svr4_read_so_list (info, debug_base, 0, info->solib_lists[debug_base],
   1401 			   0);
   1402     }
   1403 
   1404   cleanup.release ();
   1405 }
   1406 
   1407 /* Collect sos read and stored by the probes interface.  */
   1408 
   1409 static intrusive_list<solib>
   1410 svr4_collect_probes_sos (svr4_info *info)
   1411 {
   1412   intrusive_list<solib> res;
   1413 
   1414   for (const auto &tuple : info->solib_lists)
   1415     {
   1416       const std::vector<svr4_so> &sos = tuple.second;
   1417       res.splice (so_list_from_svr4_sos (sos));
   1418     }
   1419 
   1420   return res;
   1421 }
   1422 
   1423 /* Implement the main part of the "current_sos" solib_ops
   1424    method.  */
   1425 
   1426 static intrusive_list<solib>
   1427 svr4_current_sos_1 (svr4_info *info)
   1428 {
   1429   intrusive_list<solib> sos;
   1430 
   1431   /* If we're using the probes interface, we can use the cache as it will
   1432      be maintained by probe update/reload actions.  */
   1433   if (info->probes_table != nullptr)
   1434     sos = svr4_collect_probes_sos (info);
   1435 
   1436   /* If we're not using the probes interface or if we didn't cache
   1437      anything, read the sos to fill the cache, then collect them from the
   1438      cache.  */
   1439   if (sos.empty ())
   1440     {
   1441       svr4_current_sos_direct (info);
   1442 
   1443       sos = svr4_collect_probes_sos (info);
   1444       if (sos.empty ())
   1445 	sos = svr4_default_sos (info);
   1446     }
   1447 
   1448   return sos;
   1449 }
   1450 
   1451 /* Implement the "current_sos" solib_ops method.  */
   1452 
   1453 static intrusive_list<solib>
   1454 svr4_current_sos ()
   1455 {
   1456   svr4_info *info = get_svr4_info (current_program_space);
   1457   intrusive_list<solib> sos = svr4_current_sos_1 (info);
   1458   struct mem_range vsyscall_range;
   1459 
   1460   /* Filter out the vDSO module, if present.  Its symbol file would
   1461      not be found on disk.  The vDSO/vsyscall's OBJFILE is instead
   1462      managed by symfile-mem.c:add_vsyscall_page.  */
   1463   if (gdbarch_vsyscall_range (current_inferior ()->arch (), &vsyscall_range)
   1464       && vsyscall_range.length != 0)
   1465     {
   1466       for (auto so = sos.begin (); so != sos.end (); )
   1467 	{
   1468 	  /* We can't simply match the vDSO by starting address alone,
   1469 	     because lm_info->l_addr_inferior (and also l_addr) do not
   1470 	     necessarily represent the real starting address of the
   1471 	     ELF if the vDSO's ELF itself is "prelinked".  The l_ld
   1472 	     field (the ".dynamic" section of the shared object)
   1473 	     always points at the absolute/resolved address though.
   1474 	     So check whether that address is inside the vDSO's
   1475 	     mapping instead.
   1476 
   1477 	     E.g., on Linux 3.16 (x86_64) the vDSO is a regular
   1478 	     0-based ELF, and we see:
   1479 
   1480 	      (gdb) info auxv
   1481 	      33  AT_SYSINFO_EHDR  System-supplied DSO's ELF header 0x7ffff7ffb000
   1482 	      (gdb)  p/x *_r_debug.r_map.l_next
   1483 	      $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...}
   1484 
   1485 	     And on Linux 2.6.32 (x86_64) we see:
   1486 
   1487 	      (gdb) info auxv
   1488 	      33  AT_SYSINFO_EHDR  System-supplied DSO's ELF header 0x7ffff7ffe000
   1489 	      (gdb) p/x *_r_debug.r_map.l_next
   1490 	      $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... }
   1491 
   1492 	     Dumping that vDSO shows:
   1493 
   1494 	      (gdb) info proc mappings
   1495 	      0x7ffff7ffe000  0x7ffff7fff000  0x1000  0  [vdso]
   1496 	      (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
   1497 	      # readelf -Wa vdso.bin
   1498 	      [...]
   1499 		Entry point address: 0xffffffffff700700
   1500 	      [...]
   1501 	      Section Headers:
   1502 		[Nr] Name     Type    Address	       Off    Size
   1503 		[ 0]	      NULL    0000000000000000 000000 000000
   1504 		[ 1] .hash    HASH    ffffffffff700120 000120 000038
   1505 		[ 2] .dynsym  DYNSYM  ffffffffff700158 000158 0000d8
   1506 	      [...]
   1507 		[ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
   1508 	  */
   1509 
   1510 	  auto *li = gdb::checked_static_cast<lm_info_svr4 *> (so->lm_info.get ());
   1511 
   1512 	  if (vsyscall_range.contains (li->l_ld))
   1513 	    {
   1514 	      auto next = sos.erase (so);
   1515 	      delete &*so;
   1516 	      so = next;
   1517 	      break;
   1518 	    }
   1519 
   1520 	  ++so;
   1521 	}
   1522     }
   1523 
   1524   return sos;
   1525 }
   1526 
   1527 /* Get the address of the link_map for a given OBJFILE.  */
   1528 
   1529 CORE_ADDR
   1530 svr4_fetch_objfile_link_map (struct objfile *objfile)
   1531 {
   1532   struct svr4_info *info = get_svr4_info (objfile->pspace);
   1533 
   1534   /* Cause svr4_current_sos() to be run if it hasn't been already.  */
   1535   if (info->main_lm_addr == 0)
   1536     solib_add (NULL, 0, auto_solib_add);
   1537 
   1538   /* svr4_current_sos() will set main_lm_addr for the main executable.  */
   1539   if (objfile == current_program_space->symfile_object_file)
   1540     return info->main_lm_addr;
   1541 
   1542   /* The other link map addresses may be found by examining the list
   1543      of shared libraries.  */
   1544   for (const solib &so : current_program_space->solibs ())
   1545     if (so.objfile == objfile)
   1546       {
   1547 	auto *li
   1548 	  = gdb::checked_static_cast<lm_info_svr4 *> (so.lm_info.get ());
   1549 
   1550 	return li->lm_addr;
   1551       }
   1552 
   1553   /* Not found!  */
   1554   return 0;
   1555 }
   1556 
   1557 /* On some systems, the only way to recognize the link map entry for
   1558    the main executable file is by looking at its name.  Return
   1559    non-zero iff SONAME matches one of the known main executable names.  */
   1560 
   1561 static int
   1562 match_main (const char *soname)
   1563 {
   1564   const char * const *mainp;
   1565 
   1566   for (mainp = main_name_list; *mainp != NULL; mainp++)
   1567     {
   1568       if (strcmp (soname, *mainp) == 0)
   1569 	return (1);
   1570     }
   1571 
   1572   return (0);
   1573 }
   1574 
   1575 /* Return 1 if PC lies in the dynamic symbol resolution code of the
   1576    SVR4 run time loader.  */
   1577 
   1578 int
   1579 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
   1580 {
   1581   struct svr4_info *info = get_svr4_info (current_program_space);
   1582 
   1583   return ((pc >= info->interp_text_sect_low
   1584 	   && pc < info->interp_text_sect_high)
   1585 	  || (pc >= info->interp_plt_sect_low
   1586 	      && pc < info->interp_plt_sect_high)
   1587 	  || in_plt_section (pc)
   1588 	  || in_gnu_ifunc_stub (pc));
   1589 }
   1590 
   1591 /* Given an executable's ABFD and target, compute the entry-point
   1592    address.  */
   1593 
   1594 static CORE_ADDR
   1595 exec_entry_point (struct bfd *abfd, struct target_ops *targ)
   1596 {
   1597   CORE_ADDR addr;
   1598 
   1599   /* KevinB wrote ... for most targets, the address returned by
   1600      bfd_get_start_address() is the entry point for the start
   1601      function.  But, for some targets, bfd_get_start_address() returns
   1602      the address of a function descriptor from which the entry point
   1603      address may be extracted.  This address is extracted by
   1604      gdbarch_convert_from_func_ptr_addr().  The method
   1605      gdbarch_convert_from_func_ptr_addr() is the merely the identify
   1606      function for targets which don't use function descriptors.  */
   1607   addr = gdbarch_convert_from_func_ptr_addr (current_inferior ()->arch (),
   1608 					     bfd_get_start_address (abfd),
   1609 					     targ);
   1610   return gdbarch_addr_bits_remove (current_inferior ()->arch (), addr);
   1611 }
   1612 
   1613 /* A probe and its associated action.  */
   1614 
   1615 struct probe_and_action
   1616 {
   1617   /* The probe.  */
   1618   probe *prob;
   1619 
   1620   /* The relocated address of the probe.  */
   1621   CORE_ADDR address;
   1622 
   1623   /* The action.  */
   1624   enum probe_action action;
   1625 
   1626   /* The objfile where this probe was found.  */
   1627   struct objfile *objfile;
   1628 };
   1629 
   1630 /* Returns a hash code for the probe_and_action referenced by p.  */
   1631 
   1632 static hashval_t
   1633 hash_probe_and_action (const void *p)
   1634 {
   1635   const struct probe_and_action *pa = (const struct probe_and_action *) p;
   1636 
   1637   return (hashval_t) pa->address;
   1638 }
   1639 
   1640 /* Returns non-zero if the probe_and_actions referenced by p1 and p2
   1641    are equal.  */
   1642 
   1643 static int
   1644 equal_probe_and_action (const void *p1, const void *p2)
   1645 {
   1646   const struct probe_and_action *pa1 = (const struct probe_and_action *) p1;
   1647   const struct probe_and_action *pa2 = (const struct probe_and_action *) p2;
   1648 
   1649   return pa1->address == pa2->address;
   1650 }
   1651 
   1652 /* Traversal function for probes_table_remove_objfile_probes.  */
   1653 
   1654 static int
   1655 probes_table_htab_remove_objfile_probes (void **slot, void *info)
   1656 {
   1657   probe_and_action *pa = (probe_and_action *) *slot;
   1658   struct objfile *objfile = (struct objfile *) info;
   1659 
   1660   if (pa->objfile == objfile)
   1661     htab_clear_slot (get_svr4_info (objfile->pspace)->probes_table.get (),
   1662 		     slot);
   1663 
   1664   return 1;
   1665 }
   1666 
   1667 /* Remove all probes that belong to OBJFILE from the probes table.  */
   1668 
   1669 static void
   1670 probes_table_remove_objfile_probes (struct objfile *objfile)
   1671 {
   1672   svr4_info *info = get_svr4_info (objfile->pspace);
   1673   if (info->probes_table != nullptr)
   1674     htab_traverse_noresize (info->probes_table.get (),
   1675 			    probes_table_htab_remove_objfile_probes, objfile);
   1676 }
   1677 
   1678 /* Register a solib event probe and its associated action in the
   1679    probes table.  */
   1680 
   1681 static void
   1682 register_solib_event_probe (svr4_info *info, struct objfile *objfile,
   1683 			    probe *prob, CORE_ADDR address,
   1684 			    enum probe_action action)
   1685 {
   1686   struct probe_and_action lookup, *pa;
   1687   void **slot;
   1688 
   1689   /* Create the probes table, if necessary.  */
   1690   if (info->probes_table == NULL)
   1691     info->probes_table.reset (htab_create_alloc (1, hash_probe_and_action,
   1692 						 equal_probe_and_action,
   1693 						 xfree, xcalloc, xfree));
   1694 
   1695   lookup.address = address;
   1696   slot = htab_find_slot (info->probes_table.get (), &lookup, INSERT);
   1697   gdb_assert (*slot == HTAB_EMPTY_ENTRY);
   1698 
   1699   pa = XCNEW (struct probe_and_action);
   1700   pa->prob = prob;
   1701   pa->address = address;
   1702   pa->action = action;
   1703   pa->objfile = objfile;
   1704 
   1705   *slot = pa;
   1706 }
   1707 
   1708 /* Get the solib event probe at the specified location, and the
   1709    action associated with it.  Returns NULL if no solib event probe
   1710    was found.  */
   1711 
   1712 static struct probe_and_action *
   1713 solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
   1714 {
   1715   struct probe_and_action lookup;
   1716   void **slot;
   1717 
   1718   lookup.address = address;
   1719   slot = htab_find_slot (info->probes_table.get (), &lookup, NO_INSERT);
   1720 
   1721   if (slot == NULL)
   1722     return NULL;
   1723 
   1724   return (struct probe_and_action *) *slot;
   1725 }
   1726 
   1727 /* Decide what action to take when the specified solib event probe is
   1728    hit.  */
   1729 
   1730 static enum probe_action
   1731 solib_event_probe_action (struct probe_and_action *pa)
   1732 {
   1733   enum probe_action action;
   1734   unsigned probe_argc = 0;
   1735   frame_info_ptr frame = get_current_frame ();
   1736 
   1737   action = pa->action;
   1738   if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
   1739     return action;
   1740 
   1741   gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
   1742 
   1743   /* Check that an appropriate number of arguments has been supplied.
   1744      We expect:
   1745        arg0: Lmid_t lmid (mandatory)
   1746        arg1: struct r_debug *debug_base (mandatory)
   1747        arg2: struct link_map *new (optional, for incremental updates)  */
   1748   try
   1749     {
   1750       probe_argc = pa->prob->get_argument_count (get_frame_arch (frame));
   1751     }
   1752   catch (const gdb_exception_error &ex)
   1753     {
   1754       exception_print (gdb_stderr, ex);
   1755       probe_argc = 0;
   1756     }
   1757 
   1758   /* If get_argument_count throws an exception, probe_argc will be set
   1759      to zero.  However, if pa->prob does not have arguments, then
   1760      get_argument_count will succeed but probe_argc will also be zero.
   1761      Both cases happen because of different things, but they are
   1762      treated equally here: action will be set to
   1763      PROBES_INTERFACE_FAILED.  */
   1764   if (probe_argc == 2)
   1765     action = FULL_RELOAD;
   1766   else if (probe_argc < 2)
   1767     action = PROBES_INTERFACE_FAILED;
   1768 
   1769   return action;
   1770 }
   1771 
   1772 /* Populate the shared object list by reading the entire list of
   1773    shared objects from the inferior.  Handle special cases relating
   1774    to the first elements of the list.  Returns nonzero on success.  */
   1775 
   1776 static int
   1777 solist_update_full (struct svr4_info *info)
   1778 {
   1779   svr4_current_sos_direct (info);
   1780 
   1781   return 1;
   1782 }
   1783 
   1784 /* Update the shared object list starting from the link-map entry
   1785    passed by the linker in the probe's third argument.  Returns
   1786    nonzero if the list was successfully updated, or zero to indicate
   1787    failure.  */
   1788 
   1789 static int
   1790 solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
   1791 			   CORE_ADDR lm)
   1792 {
   1793   /* Fall back to a full update if we are using a remote target
   1794      that does not support incremental transfers.  */
   1795   if (info->using_xfer && !target_augmented_libraries_svr4_read ())
   1796     return 0;
   1797 
   1798   /* Fall back to a full update if we used the special namespace zero.  We
   1799      wouldn't be able to find the last item in the DEBUG_BASE namespace
   1800      and hence get the prev link wrong.  */
   1801   if (info->solib_lists.find (0) != info->solib_lists.end ())
   1802     return 0;
   1803 
   1804   std::vector<svr4_so> &solist = info->solib_lists[debug_base];
   1805   CORE_ADDR prev_lm;
   1806 
   1807   if (solist.empty ())
   1808     {
   1809       /* svr4_current_sos_direct contains logic to handle a number of
   1810 	 special cases relating to the first elements of the list in
   1811 	 default namespace.  To avoid duplicating this logic we defer to
   1812 	 solist_update_full in this case.  */
   1813       if (svr4_is_default_namespace (info, debug_base))
   1814 	return 0;
   1815 
   1816       prev_lm = 0;
   1817     }
   1818   else
   1819     prev_lm = solist.back ().lm_info->lm_addr;
   1820 
   1821   /* Read the new objects.  */
   1822   if (info->using_xfer)
   1823     {
   1824       struct svr4_library_list library_list;
   1825       char annex[64];
   1826 
   1827       /* Unknown key=value pairs are ignored by the gdbstub.  */
   1828       xsnprintf (annex, sizeof (annex), "lmid=%s;start=%s;prev=%s",
   1829 		 phex_nz (debug_base, sizeof (debug_base)),
   1830 		 phex_nz (lm, sizeof (lm)),
   1831 		 phex_nz (prev_lm, sizeof (prev_lm)));
   1832       if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
   1833 	return 0;
   1834 
   1835       /* Get the so list from the target.  We replace the list in the
   1836 	 target response so we can easily check that the response only
   1837 	 covers one namespace.
   1838 
   1839 	 We expect gdbserver to provide updates for the namespace that
   1840 	 contains LM, which would be this namespace...  */
   1841       std::vector<svr4_so> sos;
   1842       auto it_debug_base = library_list.solib_lists.find (debug_base);
   1843       if (it_debug_base != library_list.solib_lists.end ())
   1844 	std::swap (sos, it_debug_base->second);
   1845       else
   1846 	{
   1847 	  /* ...or for the special zero namespace for earlier versions...  */
   1848 	  auto it_0 = library_list.solib_lists.find (0);
   1849 	  if (it_0 != library_list.solib_lists.end ())
   1850 	    std::swap (sos, it_0->second);
   1851 	}
   1852 
   1853       /* ...but nothing else.  */
   1854       for (const auto &tuple : library_list.solib_lists)
   1855 	gdb_assert (tuple.second.empty ());
   1856 
   1857       std::move (sos.begin (), sos.end (), std::back_inserter (solist));
   1858     }
   1859   else
   1860     {
   1861       /* IGNORE_FIRST may safely be set to zero here because the
   1862 	 above check and deferral to solist_update_full ensures
   1863 	 that this call to svr4_read_so_list will never see the
   1864 	 first element.  */
   1865       if (!svr4_read_so_list (info, lm, prev_lm, solist, 0))
   1866 	return 0;
   1867     }
   1868 
   1869   return 1;
   1870 }
   1871 
   1872 /* Disable the probes-based linker interface and revert to the
   1873    original interface.  We don't reset the breakpoints as the
   1874    ones set up for the probes-based interface are adequate.  */
   1875 
   1876 static void
   1877 disable_probes_interface (svr4_info *info)
   1878 {
   1879   warning (_("Probes-based dynamic linker interface failed.\n"
   1880 	     "Reverting to original interface."));
   1881 
   1882   free_probes_table (info);
   1883   info->solib_lists.clear ();
   1884 }
   1885 
   1886 /* Update the solib list as appropriate when using the
   1887    probes-based linker interface.  Do nothing if using the
   1888    standard interface.  */
   1889 
   1890 static void
   1891 svr4_handle_solib_event (void)
   1892 {
   1893   struct svr4_info *info = get_svr4_info (current_program_space);
   1894   struct probe_and_action *pa;
   1895   enum probe_action action;
   1896   struct value *val = NULL;
   1897   CORE_ADDR pc, debug_base, lm = 0;
   1898   frame_info_ptr frame = get_current_frame ();
   1899 
   1900   /* Do nothing if not using the probes interface.  */
   1901   if (info->probes_table == NULL)
   1902     return;
   1903 
   1904   pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
   1905   pa = solib_event_probe_at (info, pc);
   1906   if (pa == nullptr)
   1907     {
   1908       /* When some solib ops sits above us, it can respond to a solib event
   1909 	 by calling in here.  This is done assuming that if the current event
   1910 	 is not an SVR4 solib event, calling here should be a no-op.  */
   1911       return;
   1912     }
   1913 
   1914   /* If anything goes wrong we revert to the original linker
   1915      interface.  */
   1916   auto cleanup = make_scope_exit ([info] ()
   1917     {
   1918       disable_probes_interface (info);
   1919     });
   1920 
   1921   action = solib_event_probe_action (pa);
   1922   if (action == PROBES_INTERFACE_FAILED)
   1923     return;
   1924 
   1925   if (action == DO_NOTHING)
   1926     {
   1927       cleanup.release ();
   1928       return;
   1929     }
   1930 
   1931   /* evaluate_argument looks up symbols in the dynamic linker
   1932      using find_pc_section.  find_pc_section is accelerated by a cache
   1933      called the section map.  The section map is invalidated every
   1934      time a shared library is loaded or unloaded, and if the inferior
   1935      is generating a lot of shared library events then the section map
   1936      will be updated every time svr4_handle_solib_event is called.
   1937      We called find_pc_section in svr4_create_solib_event_breakpoints,
   1938      so we can guarantee that the dynamic linker's sections are in the
   1939      section map.  We can therefore inhibit section map updates across
   1940      these calls to evaluate_argument and save a lot of time.  */
   1941   {
   1942     scoped_restore inhibit_updates
   1943       = inhibit_section_map_updates (current_program_space);
   1944 
   1945     try
   1946       {
   1947 	val = pa->prob->evaluate_argument (1, frame);
   1948       }
   1949     catch (const gdb_exception_error &ex)
   1950       {
   1951 	exception_print (gdb_stderr, ex);
   1952 	val = NULL;
   1953       }
   1954 
   1955     if (val == NULL)
   1956       return;
   1957 
   1958     debug_base = value_as_address (val);
   1959     if (debug_base == 0)
   1960       return;
   1961 
   1962     /* If the global _r_debug object moved, we need to reload everything
   1963        since we cannot identify namespaces (by the location of their
   1964        r_debug_ext object) anymore.  */
   1965     CORE_ADDR global_debug_base = elf_locate_base ();
   1966     if (global_debug_base != info->debug_base)
   1967       {
   1968 	info->debug_base = global_debug_base;
   1969 	action = FULL_RELOAD;
   1970       }
   1971 
   1972     if (info->debug_base == 0)
   1973       {
   1974 	/* It's possible for the reloc_complete probe to be triggered before
   1975 	   the linker has set the DT_DEBUG pointer (for example, when the
   1976 	   linker has finished relocating an LD_AUDIT library or its
   1977 	   dependencies).  Since we can't yet handle libraries from other link
   1978 	   namespaces, we don't lose anything by ignoring them here.  */
   1979 	struct value *link_map_id_val;
   1980 	try
   1981 	  {
   1982 	    link_map_id_val = pa->prob->evaluate_argument (0, frame);
   1983 	  }
   1984 	catch (const gdb_exception_error)
   1985 	  {
   1986 	    link_map_id_val = NULL;
   1987 	  }
   1988 	/* glibc and illumos' libc both define LM_ID_BASE as zero.  */
   1989 	if (link_map_id_val != NULL && value_as_long (link_map_id_val) != 0)
   1990 	  action = DO_NOTHING;
   1991 	else
   1992 	  return;
   1993       }
   1994 
   1995     if (action == UPDATE_OR_RELOAD)
   1996       {
   1997 	try
   1998 	  {
   1999 	    val = pa->prob->evaluate_argument (2, frame);
   2000 	  }
   2001 	catch (const gdb_exception_error &ex)
   2002 	  {
   2003 	    exception_print (gdb_stderr, ex);
   2004 	    return;
   2005 	  }
   2006 
   2007 	if (val != NULL)
   2008 	  lm = value_as_address (val);
   2009 
   2010 	if (lm == 0)
   2011 	  action = FULL_RELOAD;
   2012       }
   2013 
   2014     /* Resume section map updates.  Closing the scope is
   2015        sufficient.  */
   2016   }
   2017 
   2018   if (action == UPDATE_OR_RELOAD)
   2019     {
   2020       if (!solist_update_incremental (info, debug_base, lm))
   2021 	action = FULL_RELOAD;
   2022     }
   2023 
   2024   if (action == FULL_RELOAD)
   2025     {
   2026       if (!solist_update_full (info))
   2027 	return;
   2028     }
   2029 
   2030   cleanup.release ();
   2031 }
   2032 
   2033 /* Helper function for svr4_update_solib_event_breakpoints.  */
   2034 
   2035 static bool
   2036 svr4_update_solib_event_breakpoint (struct breakpoint *b)
   2037 {
   2038   if (b->type != bp_shlib_event)
   2039     {
   2040       /* Continue iterating.  */
   2041       return false;
   2042     }
   2043 
   2044   for (bp_location &loc : b->locations ())
   2045     {
   2046       struct svr4_info *info;
   2047       struct probe_and_action *pa;
   2048 
   2049       info = solib_svr4_pspace_data.get (loc.pspace);
   2050       if (info == NULL || info->probes_table == NULL)
   2051 	continue;
   2052 
   2053       pa = solib_event_probe_at (info, loc.address);
   2054       if (pa == NULL)
   2055 	continue;
   2056 
   2057       if (pa->action == DO_NOTHING)
   2058 	{
   2059 	  if (b->enable_state == bp_disabled && stop_on_solib_events)
   2060 	    enable_breakpoint (b);
   2061 	  else if (b->enable_state == bp_enabled && !stop_on_solib_events)
   2062 	    disable_breakpoint (b);
   2063 	}
   2064 
   2065       break;
   2066     }
   2067 
   2068   /* Continue iterating.  */
   2069   return false;
   2070 }
   2071 
   2072 /* Enable or disable optional solib event breakpoints as appropriate.
   2073    Called whenever stop_on_solib_events is changed.  */
   2074 
   2075 static void
   2076 svr4_update_solib_event_breakpoints (void)
   2077 {
   2078   for (breakpoint &bp : all_breakpoints_safe ())
   2079     svr4_update_solib_event_breakpoint (&bp);
   2080 }
   2081 
   2082 /* Create and register solib event breakpoints.  PROBES is an array
   2083    of NUM_PROBES elements, each of which is vector of probes.  A
   2084    solib event breakpoint will be created and registered for each
   2085    probe.  */
   2086 
   2087 static void
   2088 svr4_create_probe_breakpoints (svr4_info *info, struct gdbarch *gdbarch,
   2089 			       const std::vector<probe *> *probes,
   2090 			       struct objfile *objfile)
   2091 {
   2092   for (int i = 0; i < NUM_PROBES; i++)
   2093     {
   2094       enum probe_action action = probe_info[i].action;
   2095 
   2096       for (probe *p : probes[i])
   2097 	{
   2098 	  CORE_ADDR address = p->get_relocated_address (objfile);
   2099 
   2100 	  solib_debug_printf ("name=%s, addr=%s", probe_info[i].name,
   2101 			      paddress (gdbarch, address));
   2102 
   2103 	  create_solib_event_breakpoint (gdbarch, address);
   2104 	  register_solib_event_probe (info, objfile, p, address, action);
   2105 	}
   2106     }
   2107 
   2108   svr4_update_solib_event_breakpoints ();
   2109 }
   2110 
   2111 /* Find all the glibc named probes.  Only if all of the probes are found, then
   2112    create them and return true.  Otherwise return false.  If WITH_PREFIX is set
   2113    then add "rtld" to the front of the probe names.  */
   2114 static bool
   2115 svr4_find_and_create_probe_breakpoints (svr4_info *info,
   2116 					struct gdbarch *gdbarch,
   2117 					struct obj_section *os,
   2118 					bool with_prefix)
   2119 {
   2120   SOLIB_SCOPED_DEBUG_START_END ("objfile=%s, with_prefix=%d",
   2121 				os->objfile->original_name, with_prefix);
   2122 
   2123   std::vector<probe *> probes[NUM_PROBES];
   2124 
   2125   for (int i = 0; i < NUM_PROBES; i++)
   2126     {
   2127       const char *name = probe_info[i].name;
   2128       char buf[32];
   2129 
   2130       /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4 shipped with an early
   2131 	 version of the probes code in which the probes' names were prefixed
   2132 	 with "rtld_" and the "map_failed" probe did not exist.  The locations
   2133 	 of the probes are otherwise the same, so we check for probes with
   2134 	 prefixed names if probes with unprefixed names are not present.  */
   2135       if (with_prefix)
   2136 	{
   2137 	  xsnprintf (buf, sizeof (buf), "rtld_%s", name);
   2138 	  name = buf;
   2139 	}
   2140 
   2141       probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
   2142       solib_debug_printf ("probe=%s, num found=%zu", name, probes[i].size ());
   2143 
   2144       /* Ensure at least one probe for the current name was found.  */
   2145       if (probes[i].empty ())
   2146 	{
   2147 	  /* The "map_failed" probe did not exist in early versions of the
   2148 	     probes code in which the probes' names were prefixed with
   2149 	     "rtld_".
   2150 
   2151 	     Additionally, the "map_failed" probe was accidentally removed
   2152 	     from glibc 2.35 and 2.36, when changes in glibc meant the
   2153 	     probe could no longer be reached, and the compiler optimized
   2154 	     the probe away.  In this case the probe name doesn't have the
   2155 	     "rtld_" prefix.
   2156 
   2157 	     To handle this, and give GDB as much flexibility as possible,
   2158 	     we make the rule that, if a probe isn't required for the
   2159 	     correct operation of GDB (i.e. its action is DO_NOTHING), then
   2160 	     we will still use the probes interface, even if that probe is
   2161 	     missing.
   2162 
   2163 	     The only (possible) downside of this is that, if the user has
   2164 	     'set stop-on-solib-events on' in effect, then they might get
   2165 	     fewer events using the probes interface than with the classic
   2166 	     non-probes interface.  */
   2167 	  if (probe_info[i].action == DO_NOTHING)
   2168 	    continue;
   2169 	  else
   2170 	    return false;
   2171 	}
   2172 
   2173       /* Ensure probe arguments can be evaluated.  */
   2174       for (probe *p : probes[i])
   2175 	{
   2176 	  if (!p->can_evaluate_arguments ())
   2177 	    return false;
   2178 	  /* This will fail if the probe is invalid.  This has been seen on Arm
   2179 	     due to references to symbols that have been resolved away.  */
   2180 	  try
   2181 	    {
   2182 	      p->get_argument_count (gdbarch);
   2183 	    }
   2184 	  catch (const gdb_exception_error &ex)
   2185 	    {
   2186 	      exception_print (gdb_stderr, ex);
   2187 	      warning (_("Initializing probes-based dynamic linker interface "
   2188 			 "failed.\nReverting to original interface."));
   2189 	      return false;
   2190 	    }
   2191 	}
   2192     }
   2193 
   2194   /* All probes found.  Now create them.  */
   2195   solib_debug_printf ("using probes interface");
   2196   svr4_create_probe_breakpoints (info, gdbarch, probes, os->objfile);
   2197   return true;
   2198 }
   2199 
   2200 /* Both the SunOS and the SVR4 dynamic linkers call a marker function
   2201    before and after mapping and unmapping shared libraries.  The sole
   2202    purpose of this method is to allow debuggers to set a breakpoint so
   2203    they can track these changes.
   2204 
   2205    Some versions of the glibc dynamic linker contain named probes
   2206    to allow more fine grained stopping.  Given the address of the
   2207    original marker function, this function attempts to find these
   2208    probes, and if found, sets breakpoints on those instead.  If the
   2209    probes aren't found, a single breakpoint is set on the original
   2210    marker function.  */
   2211 
   2212 static void
   2213 svr4_create_solib_event_breakpoints (svr4_info *info, struct gdbarch *gdbarch,
   2214 				     CORE_ADDR address)
   2215 {
   2216   struct obj_section *os = find_pc_section (address);
   2217 
   2218   if (os == nullptr
   2219       || (!svr4_find_and_create_probe_breakpoints (info, gdbarch, os, false)
   2220 	  && !svr4_find_and_create_probe_breakpoints (info, gdbarch, os, true)))
   2221     {
   2222       solib_debug_printf ("falling back to r_brk breakpoint: addr=%s",
   2223 			  paddress (gdbarch, address));
   2224       create_solib_event_breakpoint (gdbarch, address);
   2225     }
   2226 }
   2227 
   2228 /* Arrange for dynamic linker to hit breakpoint.
   2229 
   2230    Both the SunOS and the SVR4 dynamic linkers have, as part of their
   2231    debugger interface, support for arranging for the inferior to hit
   2232    a breakpoint after mapping in the shared libraries.  This function
   2233    enables that breakpoint.
   2234 
   2235    For SunOS, there is a special flag location (in_debugger) which we
   2236    set to 1.  When the dynamic linker sees this flag set, it will set
   2237    a breakpoint at a location known only to itself, after saving the
   2238    original contents of that place and the breakpoint address itself,
   2239    in its own internal structures.  When we resume the inferior, it
   2240    will eventually take a SIGTRAP when it runs into the breakpoint.
   2241    We handle this (in a different place) by restoring the contents of
   2242    the breakpointed location (which is only known after it stops),
   2243    chasing around to locate the shared libraries that have been
   2244    loaded, then resuming.
   2245 
   2246    For SVR4, the debugger interface structure contains a member (r_brk)
   2247    which is statically initialized at the time the shared library is
   2248    built, to the offset of a function (_r_debug_state) which is guaran-
   2249    teed to be called once before mapping in a library, and again when
   2250    the mapping is complete.  At the time we are examining this member,
   2251    it contains only the unrelocated offset of the function, so we have
   2252    to do our own relocation.  Later, when the dynamic linker actually
   2253    runs, it relocates r_brk to be the actual address of _r_debug_state().
   2254 
   2255    The debugger interface structure also contains an enumeration which
   2256    is set to either RT_ADD or RT_DELETE prior to changing the mapping,
   2257    depending upon whether or not the library is being mapped or unmapped,
   2258    and then set to RT_CONSISTENT after the library is mapped/unmapped.  */
   2259 
   2260 static int
   2261 enable_break (struct svr4_info *info, int from_tty)
   2262 {
   2263   struct bound_minimal_symbol msymbol;
   2264   const char * const *bkpt_namep;
   2265   asection *interp_sect;
   2266   CORE_ADDR sym_addr;
   2267 
   2268   info->interp_text_sect_low = info->interp_text_sect_high = 0;
   2269   info->interp_plt_sect_low = info->interp_plt_sect_high = 0;
   2270 
   2271   /* If we already have a shared library list in the target, and
   2272      r_debug contains r_brk, set the breakpoint there - this should
   2273      mean r_brk has already been relocated.  Assume the dynamic linker
   2274      is the object containing r_brk.  */
   2275 
   2276   solib_add (NULL, from_tty, auto_solib_add);
   2277   sym_addr = 0;
   2278   if (info->debug_base && solib_svr4_r_map (info->debug_base) != 0)
   2279     sym_addr = solib_svr4_r_brk (info);
   2280 
   2281   if (sym_addr != 0)
   2282     {
   2283       struct obj_section *os;
   2284 
   2285       sym_addr = gdbarch_addr_bits_remove
   2286 	(current_inferior ()->arch (),
   2287 	 gdbarch_convert_from_func_ptr_addr
   2288 	   (current_inferior ()->arch (), sym_addr,
   2289 	    current_inferior ()->top_target ()));
   2290 
   2291       /* On at least some versions of Solaris there's a dynamic relocation
   2292 	 on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
   2293 	 we get control before the dynamic linker has self-relocated.
   2294 	 Check if SYM_ADDR is in a known section, if it is assume we can
   2295 	 trust its value.  This is just a heuristic though, it could go away
   2296 	 or be replaced if it's getting in the way.
   2297 
   2298 	 On ARM we need to know whether the ISA of rtld_db_dlactivity (or
   2299 	 however it's spelled in your particular system) is ARM or Thumb.
   2300 	 That knowledge is encoded in the address, if it's Thumb the low bit
   2301 	 is 1.  However, we've stripped that info above and it's not clear
   2302 	 what all the consequences are of passing a non-addr_bits_remove'd
   2303 	 address to svr4_create_solib_event_breakpoints.  The call to
   2304 	 find_pc_section verifies we know about the address and have some
   2305 	 hope of computing the right kind of breakpoint to use (via
   2306 	 symbol info).  It does mean that GDB needs to be pointed at a
   2307 	 non-stripped version of the dynamic linker in order to obtain
   2308 	 information it already knows about.  Sigh.  */
   2309 
   2310       os = find_pc_section (sym_addr);
   2311       if (os != NULL)
   2312 	{
   2313 	  /* Record the relocated start and end address of the dynamic linker
   2314 	     text and plt section for svr4_in_dynsym_resolve_code.  */
   2315 	  bfd *tmp_bfd;
   2316 	  CORE_ADDR load_addr;
   2317 
   2318 	  tmp_bfd = os->objfile->obfd.get ();
   2319 	  load_addr = os->objfile->text_section_offset ();
   2320 
   2321 	  interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
   2322 	  if (interp_sect)
   2323 	    {
   2324 	      info->interp_text_sect_low
   2325 		= bfd_section_vma (interp_sect) + load_addr;
   2326 	      info->interp_text_sect_high
   2327 		= info->interp_text_sect_low + bfd_section_size (interp_sect);
   2328 	    }
   2329 	  interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
   2330 	  if (interp_sect)
   2331 	    {
   2332 	      info->interp_plt_sect_low
   2333 		= bfd_section_vma (interp_sect) + load_addr;
   2334 	      info->interp_plt_sect_high
   2335 		= info->interp_plt_sect_low + bfd_section_size (interp_sect);
   2336 	    }
   2337 
   2338 	  svr4_create_solib_event_breakpoints
   2339 	    (info, current_inferior ()->arch (), sym_addr);
   2340 	  return 1;
   2341 	}
   2342     }
   2343 
   2344   /* Find the program interpreter; if not found, warn the user and drop
   2345      into the old breakpoint at symbol code.  */
   2346   std::optional<gdb::byte_vector> interp_name_holder
   2347     = find_program_interpreter ();
   2348   if (interp_name_holder)
   2349     {
   2350       const char *interp_name = (const char *) interp_name_holder->data ();
   2351       CORE_ADDR load_addr = 0;
   2352       int load_addr_found = 0;
   2353       int loader_found_in_list = 0;
   2354       target_ops_up tmp_bfd_target;
   2355 
   2356       sym_addr = 0;
   2357 
   2358       /* Now we need to figure out where the dynamic linker was
   2359 	 loaded so that we can load its symbols and place a breakpoint
   2360 	 in the dynamic linker itself.
   2361 
   2362 	 This address is stored on the stack.  However, I've been unable
   2363 	 to find any magic formula to find it for Solaris (appears to
   2364 	 be trivial on GNU/Linux).  Therefore, we have to try an alternate
   2365 	 mechanism to find the dynamic linker's base address.  */
   2366 
   2367       gdb_bfd_ref_ptr tmp_bfd;
   2368       try
   2369 	{
   2370 	  tmp_bfd = solib_bfd_open (interp_name);
   2371 	}
   2372       catch (const gdb_exception &ex)
   2373 	{
   2374 	}
   2375 
   2376       if (tmp_bfd == NULL)
   2377 	goto bkpt_at_symbol;
   2378 
   2379       /* Now convert the TMP_BFD into a target.  That way target, as
   2380 	 well as BFD operations can be used.  */
   2381       tmp_bfd_target = target_bfd_reopen (tmp_bfd);
   2382 
   2383       /* On a running target, we can get the dynamic linker's base
   2384 	 address from the shared library table.  */
   2385       for (const solib &so : current_program_space->solibs ())
   2386 	{
   2387 	  if (svr4_same_1 (interp_name, so.so_original_name.c_str ()))
   2388 	    {
   2389 	      load_addr_found = 1;
   2390 	      loader_found_in_list = 1;
   2391 	      load_addr = lm_addr_check (so, tmp_bfd.get ());
   2392 	      break;
   2393 	    }
   2394 	}
   2395 
   2396       /* If we were not able to find the base address of the loader
   2397 	 from our so_list, then try using the AT_BASE auxilliary entry.  */
   2398       if (!load_addr_found)
   2399 	if (target_auxv_search (AT_BASE, &load_addr) > 0)
   2400 	  {
   2401 	    int addr_bit = gdbarch_addr_bit (current_inferior ()->arch ());
   2402 
   2403 	    /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
   2404 	       that `+ load_addr' will overflow CORE_ADDR width not creating
   2405 	       invalid addresses like 0x101234567 for 32bit inferiors on 64bit
   2406 	       GDB.  */
   2407 
   2408 	    if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
   2409 	      {
   2410 		CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
   2411 		CORE_ADDR tmp_entry_point
   2412 		  = exec_entry_point (tmp_bfd.get (), tmp_bfd_target.get ());
   2413 
   2414 		gdb_assert (load_addr < space_size);
   2415 
   2416 		/* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
   2417 		   64bit ld.so with 32bit executable, it should not happen.  */
   2418 
   2419 		if (tmp_entry_point < space_size
   2420 		    && tmp_entry_point + load_addr >= space_size)
   2421 		  load_addr -= space_size;
   2422 	      }
   2423 
   2424 	    load_addr_found = 1;
   2425 	  }
   2426 
   2427       /* Otherwise we find the dynamic linker's base address by examining
   2428 	 the current pc (which should point at the entry point for the
   2429 	 dynamic linker) and subtracting the offset of the entry point.
   2430 
   2431 	 This is more fragile than the previous approaches, but is a good
   2432 	 fallback method because it has actually been working well in
   2433 	 most cases.  */
   2434       if (!load_addr_found)
   2435 	{
   2436 	  regcache *regcache
   2437 	    = get_thread_arch_regcache (current_inferior (), inferior_ptid,
   2438 					current_inferior ()->arch ());
   2439 
   2440 	  load_addr = (regcache_read_pc (regcache)
   2441 		       - exec_entry_point (tmp_bfd.get (),
   2442 					   tmp_bfd_target.get ()));
   2443 	}
   2444 
   2445       if (!loader_found_in_list)
   2446 	{
   2447 	  info->debug_loader_name = xstrdup (interp_name);
   2448 	  info->debug_loader_offset_p = 1;
   2449 	  info->debug_loader_offset = load_addr;
   2450 	  solib_add (NULL, from_tty, auto_solib_add);
   2451 	}
   2452 
   2453       /* Record the relocated start and end address of the dynamic linker
   2454 	 text and plt section for svr4_in_dynsym_resolve_code.  */
   2455       interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
   2456       if (interp_sect)
   2457 	{
   2458 	  info->interp_text_sect_low
   2459 	    = bfd_section_vma (interp_sect) + load_addr;
   2460 	  info->interp_text_sect_high
   2461 	    = info->interp_text_sect_low + bfd_section_size (interp_sect);
   2462 	}
   2463       interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
   2464       if (interp_sect)
   2465 	{
   2466 	  info->interp_plt_sect_low
   2467 	    = bfd_section_vma (interp_sect) + load_addr;
   2468 	  info->interp_plt_sect_high
   2469 	    = info->interp_plt_sect_low + bfd_section_size (interp_sect);
   2470 	}
   2471 
   2472       /* Now try to set a breakpoint in the dynamic linker.  */
   2473       for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
   2474 	{
   2475 	  sym_addr
   2476 	    = (gdb_bfd_lookup_symbol
   2477 	       (tmp_bfd.get (),
   2478 		[=] (const asymbol *sym)
   2479 		{
   2480 		  return (strcmp (sym->name, *bkpt_namep) == 0
   2481 			  && ((sym->section->flags & (SEC_CODE | SEC_DATA))
   2482 			      != 0));
   2483 		}));
   2484 	  if (sym_addr != 0)
   2485 	    break;
   2486 	}
   2487 
   2488       if (sym_addr != 0)
   2489 	/* Convert 'sym_addr' from a function pointer to an address.
   2490 	   Because we pass tmp_bfd_target instead of the current
   2491 	   target, this will always produce an unrelocated value.  */
   2492 	sym_addr = gdbarch_convert_from_func_ptr_addr
   2493 		     (current_inferior ()->arch (), sym_addr,
   2494 		      tmp_bfd_target.get ());
   2495 
   2496       if (sym_addr != 0)
   2497 	{
   2498 	  svr4_create_solib_event_breakpoints (info,
   2499 					       current_inferior ()->arch (),
   2500 					       load_addr + sym_addr);
   2501 	  return 1;
   2502 	}
   2503 
   2504       /* For whatever reason we couldn't set a breakpoint in the dynamic
   2505 	 linker.  Warn and drop into the old code.  */
   2506     bkpt_at_symbol:
   2507       warning (_("Unable to find dynamic linker breakpoint function.\n"
   2508 	       "GDB will be unable to debug shared library initializers\n"
   2509 	       "and track explicitly loaded dynamic code."));
   2510     }
   2511 
   2512   /* Scan through the lists of symbols, trying to look up the symbol and
   2513      set a breakpoint there.  Terminate loop when we/if we succeed.  */
   2514 
   2515   objfile *objf = current_program_space->symfile_object_file;
   2516   for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
   2517     {
   2518       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
   2519       if ((msymbol.minsym != NULL)
   2520 	  && (msymbol.value_address () != 0))
   2521 	{
   2522 	  sym_addr = msymbol.value_address ();
   2523 	  sym_addr = gdbarch_convert_from_func_ptr_addr
   2524 	    (current_inferior ()->arch (), sym_addr,
   2525 	     current_inferior ()->top_target ());
   2526 	  svr4_create_solib_event_breakpoints (info,
   2527 					       current_inferior ()->arch (),
   2528 					       sym_addr);
   2529 	  return 1;
   2530 	}
   2531     }
   2532 
   2533   if (interp_name_holder && !current_inferior ()->attach_flag)
   2534     {
   2535       for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
   2536 	{
   2537 	  msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
   2538 	  if ((msymbol.minsym != NULL)
   2539 	      && (msymbol.value_address () != 0))
   2540 	    {
   2541 	      sym_addr = msymbol.value_address ();
   2542 	      sym_addr = gdbarch_convert_from_func_ptr_addr
   2543 		(current_inferior ()->arch (), sym_addr,
   2544 		 current_inferior ()->top_target ());
   2545 	      svr4_create_solib_event_breakpoints
   2546 		(info, current_inferior ()->arch (), sym_addr);
   2547 	      return 1;
   2548 	    }
   2549 	}
   2550     }
   2551   return 0;
   2552 }
   2553 
   2554 /* Read the ELF program headers from ABFD.  */
   2555 
   2556 static std::optional<gdb::byte_vector>
   2557 read_program_headers_from_bfd (bfd *abfd)
   2558 {
   2559   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
   2560   int phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
   2561   if (phdrs_size == 0)
   2562     return {};
   2563 
   2564   gdb::byte_vector buf (phdrs_size);
   2565   if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
   2566       || bfd_read (buf.data (), phdrs_size, abfd) != phdrs_size)
   2567     return {};
   2568 
   2569   return buf;
   2570 }
   2571 
   2572 /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
   2573    exec_bfd.  Otherwise return 0.
   2574 
   2575    We relocate all of the sections by the same amount.  This
   2576    behavior is mandated by recent editions of the System V ABI.
   2577    According to the System V Application Binary Interface,
   2578    Edition 4.1, page 5-5:
   2579 
   2580      ...  Though the system chooses virtual addresses for
   2581      individual processes, it maintains the segments' relative
   2582      positions.  Because position-independent code uses relative
   2583      addressing between segments, the difference between
   2584      virtual addresses in memory must match the difference
   2585      between virtual addresses in the file.  The difference
   2586      between the virtual address of any segment in memory and
   2587      the corresponding virtual address in the file is thus a
   2588      single constant value for any one executable or shared
   2589      object in a given process.  This difference is the base
   2590      address.  One use of the base address is to relocate the
   2591      memory image of the program during dynamic linking.
   2592 
   2593    The same language also appears in Edition 4.0 of the System V
   2594    ABI and is left unspecified in some of the earlier editions.
   2595 
   2596    Decide if the objfile needs to be relocated.  As indicated above, we will
   2597    only be here when execution is stopped.  But during attachment PC can be at
   2598    arbitrary address therefore regcache_read_pc can be misleading (contrary to
   2599    the auxv AT_ENTRY value).  Moreover for executable with interpreter section
   2600    regcache_read_pc would point to the interpreter and not the main executable.
   2601 
   2602    So, to summarize, relocations are necessary when the start address obtained
   2603    from the executable is different from the address in auxv AT_ENTRY entry.
   2604 
   2605    [ The astute reader will note that we also test to make sure that
   2606      the executable in question has the DYNAMIC flag set.  It is my
   2607      opinion that this test is unnecessary (undesirable even).  It
   2608      was added to avoid inadvertent relocation of an executable
   2609      whose e_type member in the ELF header is not ET_DYN.  There may
   2610      be a time in the future when it is desirable to do relocations
   2611      on other types of files as well in which case this condition
   2612      should either be removed or modified to accommodate the new file
   2613      type.  - Kevin, Nov 2000. ]  */
   2614 
   2615 static int
   2616 svr4_exec_displacement (CORE_ADDR *displacementp)
   2617 {
   2618   /* ENTRY_POINT is a possible function descriptor - before
   2619      a call to gdbarch_convert_from_func_ptr_addr.  */
   2620   CORE_ADDR entry_point, exec_displacement;
   2621 
   2622   if (current_program_space->exec_bfd () == NULL)
   2623     return 0;
   2624 
   2625   /* Therefore for ELF it is ET_EXEC and not ET_DYN.  Both shared libraries
   2626      being executed themselves and PIE (Position Independent Executable)
   2627      executables are ET_DYN.  */
   2628 
   2629   if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0)
   2630     return 0;
   2631 
   2632   if (target_auxv_search (AT_ENTRY, &entry_point) <= 0)
   2633     return 0;
   2634 
   2635   exec_displacement
   2636     = entry_point - bfd_get_start_address (current_program_space->exec_bfd ());
   2637 
   2638   /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
   2639      alignment.  It is cheaper than the program headers comparison below.  */
   2640 
   2641   if (bfd_get_flavour (current_program_space->exec_bfd ())
   2642       == bfd_target_elf_flavour)
   2643     {
   2644       const struct elf_backend_data *elf
   2645 	= get_elf_backend_data (current_program_space->exec_bfd ());
   2646 
   2647       /* p_align of PT_LOAD segments does not specify any alignment but
   2648 	 only congruency of addresses:
   2649 	   p_offset % p_align == p_vaddr % p_align
   2650 	 Kernel is free to load the executable with lower alignment.  */
   2651 
   2652       if ((exec_displacement & (elf->minpagesize - 1)) != 0)
   2653 	return 0;
   2654     }
   2655 
   2656   /* Verify that the auxilliary vector describes the same file as exec_bfd, by
   2657      comparing their program headers.  If the program headers in the auxilliary
   2658      vector do not match the program headers in the executable, then we are
   2659      looking at a different file than the one used by the kernel - for
   2660      instance, "gdb program" connected to "gdbserver :PORT ld.so program".  */
   2661 
   2662   if (bfd_get_flavour (current_program_space->exec_bfd ())
   2663       == bfd_target_elf_flavour)
   2664     {
   2665       /* Be optimistic and return 0 only if GDB was able to verify the headers
   2666 	 really do not match.  */
   2667       int arch_size;
   2668 
   2669       std::optional<gdb::byte_vector> phdrs_target
   2670 	= read_program_header (-1, &arch_size, NULL);
   2671       std::optional<gdb::byte_vector> phdrs_binary
   2672 	= read_program_headers_from_bfd (current_program_space->exec_bfd ());
   2673       if (phdrs_target && phdrs_binary)
   2674 	{
   2675 	  bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
   2676 
   2677 	  /* We are dealing with three different addresses.  EXEC_BFD
   2678 	     represents current address in on-disk file.  target memory content
   2679 	     may be different from EXEC_BFD as the file may have been prelinked
   2680 	     to a different address after the executable has been loaded.
   2681 	     Moreover the address of placement in target memory can be
   2682 	     different from what the program headers in target memory say -
   2683 	     this is the goal of PIE.
   2684 
   2685 	     Detected DISPLACEMENT covers both the offsets of PIE placement and
   2686 	     possible new prelink performed after start of the program.  Here
   2687 	     relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
   2688 	     content offset for the verification purpose.  */
   2689 
   2690 	  if (phdrs_target->size () != phdrs_binary->size ()
   2691 	      || bfd_get_arch_size (current_program_space->exec_bfd ()) != arch_size)
   2692 	    return 0;
   2693 	  else if (arch_size == 32
   2694 		   && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
   2695 		   && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
   2696 	    {
   2697 	      Elf_Internal_Ehdr *ehdr2
   2698 		= elf_tdata (current_program_space->exec_bfd ())->elf_header;
   2699 	      Elf_Internal_Phdr *phdr2
   2700 		= elf_tdata (current_program_space->exec_bfd ())->phdr;
   2701 	      CORE_ADDR displacement = 0;
   2702 	      int i;
   2703 
   2704 	      /* DISPLACEMENT could be found more easily by the difference of
   2705 		 ehdr2->e_entry.  But we haven't read the ehdr yet, and we
   2706 		 already have enough information to compute that displacement
   2707 		 with what we've read.  */
   2708 
   2709 	      for (i = 0; i < ehdr2->e_phnum; i++)
   2710 		if (phdr2[i].p_type == PT_LOAD)
   2711 		  {
   2712 		    Elf32_External_Phdr *phdrp;
   2713 		    gdb_byte *buf_vaddr_p, *buf_paddr_p;
   2714 		    CORE_ADDR vaddr, paddr;
   2715 		    CORE_ADDR displacement_vaddr = 0;
   2716 		    CORE_ADDR displacement_paddr = 0;
   2717 
   2718 		    phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
   2719 		    buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
   2720 		    buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
   2721 
   2722 		    vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
   2723 						      byte_order);
   2724 		    displacement_vaddr = vaddr - phdr2[i].p_vaddr;
   2725 
   2726 		    paddr = extract_unsigned_integer (buf_paddr_p, 4,
   2727 						      byte_order);
   2728 		    displacement_paddr = paddr - phdr2[i].p_paddr;
   2729 
   2730 		    if (displacement_vaddr == displacement_paddr)
   2731 		      displacement = displacement_vaddr;
   2732 
   2733 		    break;
   2734 		  }
   2735 
   2736 	      /* Now compare program headers from the target and the binary
   2737 		 with optional DISPLACEMENT.  */
   2738 
   2739 	      for (i = 0;
   2740 		   i < phdrs_target->size () / sizeof (Elf32_External_Phdr);
   2741 		   i++)
   2742 		{
   2743 		  Elf32_External_Phdr *phdrp;
   2744 		  Elf32_External_Phdr *phdr2p;
   2745 		  gdb_byte *buf_vaddr_p, *buf_paddr_p;
   2746 		  CORE_ADDR vaddr, paddr;
   2747 		  asection *plt2_asect;
   2748 
   2749 		  phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
   2750 		  buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
   2751 		  buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
   2752 		  phdr2p = &((Elf32_External_Phdr *) phdrs_binary->data ())[i];
   2753 
   2754 		  /* PT_GNU_STACK is an exception by being never relocated by
   2755 		     prelink as its addresses are always zero.  */
   2756 
   2757 		  if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
   2758 		    continue;
   2759 
   2760 		  /* Check also other adjustment combinations - PR 11786.  */
   2761 
   2762 		  vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
   2763 						    byte_order);
   2764 		  vaddr -= displacement;
   2765 		  store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);
   2766 
   2767 		  paddr = extract_unsigned_integer (buf_paddr_p, 4,
   2768 						    byte_order);
   2769 		  paddr -= displacement;
   2770 		  store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);
   2771 
   2772 		  if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
   2773 		    continue;
   2774 
   2775 		  /* Strip modifies the flags and alignment of PT_GNU_RELRO.
   2776 		     CentOS-5 has problems with filesz, memsz as well.
   2777 		     Strip also modifies memsz of PT_TLS.
   2778 		     See PR 11786.  */
   2779 		  if (phdr2[i].p_type == PT_GNU_RELRO
   2780 		      || phdr2[i].p_type == PT_TLS)
   2781 		    {
   2782 		      Elf32_External_Phdr tmp_phdr = *phdrp;
   2783 		      Elf32_External_Phdr tmp_phdr2 = *phdr2p;
   2784 
   2785 		      memset (tmp_phdr.p_filesz, 0, 4);
   2786 		      memset (tmp_phdr.p_memsz, 0, 4);
   2787 		      memset (tmp_phdr.p_flags, 0, 4);
   2788 		      memset (tmp_phdr.p_align, 0, 4);
   2789 		      memset (tmp_phdr2.p_filesz, 0, 4);
   2790 		      memset (tmp_phdr2.p_memsz, 0, 4);
   2791 		      memset (tmp_phdr2.p_flags, 0, 4);
   2792 		      memset (tmp_phdr2.p_align, 0, 4);
   2793 
   2794 		      if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
   2795 			  == 0)
   2796 			continue;
   2797 		    }
   2798 
   2799 		  /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
   2800 		  bfd *exec_bfd = current_program_space->exec_bfd ();
   2801 		  plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
   2802 		  if (plt2_asect)
   2803 		    {
   2804 		      int content2;
   2805 		      gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
   2806 		      CORE_ADDR filesz;
   2807 
   2808 		      content2 = (bfd_section_flags (plt2_asect)
   2809 				  & SEC_HAS_CONTENTS) != 0;
   2810 
   2811 		      filesz = extract_unsigned_integer (buf_filesz_p, 4,
   2812 							 byte_order);
   2813 
   2814 		      /* PLT2_ASECT is from on-disk file (exec_bfd) while
   2815 			 FILESZ is from the in-memory image.  */
   2816 		      if (content2)
   2817 			filesz += bfd_section_size (plt2_asect);
   2818 		      else
   2819 			filesz -= bfd_section_size (plt2_asect);
   2820 
   2821 		      store_unsigned_integer (buf_filesz_p, 4, byte_order,
   2822 					      filesz);
   2823 
   2824 		      if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
   2825 			continue;
   2826 		    }
   2827 
   2828 		  return 0;
   2829 		}
   2830 	    }
   2831 	  else if (arch_size == 64
   2832 		   && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
   2833 		   && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
   2834 	    {
   2835 	      Elf_Internal_Ehdr *ehdr2
   2836 		= elf_tdata (current_program_space->exec_bfd ())->elf_header;
   2837 	      Elf_Internal_Phdr *phdr2
   2838 		= elf_tdata (current_program_space->exec_bfd ())->phdr;
   2839 	      CORE_ADDR displacement = 0;
   2840 	      int i;
   2841 
   2842 	      /* DISPLACEMENT could be found more easily by the difference of
   2843 		 ehdr2->e_entry.  But we haven't read the ehdr yet, and we
   2844 		 already have enough information to compute that displacement
   2845 		 with what we've read.  */
   2846 
   2847 	      for (i = 0; i < ehdr2->e_phnum; i++)
   2848 		if (phdr2[i].p_type == PT_LOAD)
   2849 		  {
   2850 		    Elf64_External_Phdr *phdrp;
   2851 		    gdb_byte *buf_vaddr_p, *buf_paddr_p;
   2852 		    CORE_ADDR vaddr, paddr;
   2853 		    CORE_ADDR displacement_vaddr = 0;
   2854 		    CORE_ADDR displacement_paddr = 0;
   2855 
   2856 		    phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
   2857 		    buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
   2858 		    buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
   2859 
   2860 		    vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
   2861 						      byte_order);
   2862 		    displacement_vaddr = vaddr - phdr2[i].p_vaddr;
   2863 
   2864 		    paddr = extract_unsigned_integer (buf_paddr_p, 8,
   2865 						      byte_order);
   2866 		    displacement_paddr = paddr - phdr2[i].p_paddr;
   2867 
   2868 		    if (displacement_vaddr == displacement_paddr)
   2869 		      displacement = displacement_vaddr;
   2870 
   2871 		    break;
   2872 		  }
   2873 
   2874 	      /* Now compare BUF and BUF2 with optional DISPLACEMENT.  */
   2875 
   2876 	      for (i = 0;
   2877 		   i < phdrs_target->size () / sizeof (Elf64_External_Phdr);
   2878 		   i++)
   2879 		{
   2880 		  Elf64_External_Phdr *phdrp;
   2881 		  Elf64_External_Phdr *phdr2p;
   2882 		  gdb_byte *buf_vaddr_p, *buf_paddr_p;
   2883 		  CORE_ADDR vaddr, paddr;
   2884 		  asection *plt2_asect;
   2885 
   2886 		  phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
   2887 		  buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
   2888 		  buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
   2889 		  phdr2p = &((Elf64_External_Phdr *) phdrs_binary->data ())[i];
   2890 
   2891 		  /* PT_GNU_STACK is an exception by being never relocated by
   2892 		     prelink as its addresses are always zero.  */
   2893 
   2894 		  if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
   2895 		    continue;
   2896 
   2897 		  /* Check also other adjustment combinations - PR 11786.  */
   2898 
   2899 		  vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
   2900 						    byte_order);
   2901 		  vaddr -= displacement;
   2902 		  store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);
   2903 
   2904 		  paddr = extract_unsigned_integer (buf_paddr_p, 8,
   2905 						    byte_order);
   2906 		  paddr -= displacement;
   2907 		  store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);
   2908 
   2909 		  if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
   2910 		    continue;
   2911 
   2912 		  /* Strip modifies the flags and alignment of PT_GNU_RELRO.
   2913 		     CentOS-5 has problems with filesz, memsz as well.
   2914 		     Strip also modifies memsz of PT_TLS.
   2915 		     See PR 11786.  */
   2916 		  if (phdr2[i].p_type == PT_GNU_RELRO
   2917 		      || phdr2[i].p_type == PT_TLS)
   2918 		    {
   2919 		      Elf64_External_Phdr tmp_phdr = *phdrp;
   2920 		      Elf64_External_Phdr tmp_phdr2 = *phdr2p;
   2921 
   2922 		      memset (tmp_phdr.p_filesz, 0, 8);
   2923 		      memset (tmp_phdr.p_memsz, 0, 8);
   2924 		      memset (tmp_phdr.p_flags, 0, 4);
   2925 		      memset (tmp_phdr.p_align, 0, 8);
   2926 		      memset (tmp_phdr2.p_filesz, 0, 8);
   2927 		      memset (tmp_phdr2.p_memsz, 0, 8);
   2928 		      memset (tmp_phdr2.p_flags, 0, 4);
   2929 		      memset (tmp_phdr2.p_align, 0, 8);
   2930 
   2931 		      if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
   2932 			  == 0)
   2933 			continue;
   2934 		    }
   2935 
   2936 		  /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
   2937 		  plt2_asect
   2938 		    = bfd_get_section_by_name (current_program_space->exec_bfd (),
   2939 					       ".plt");
   2940 		  if (plt2_asect)
   2941 		    {
   2942 		      int content2;
   2943 		      gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
   2944 		      CORE_ADDR filesz;
   2945 
   2946 		      content2 = (bfd_section_flags (plt2_asect)
   2947 				  & SEC_HAS_CONTENTS) != 0;
   2948 
   2949 		      filesz = extract_unsigned_integer (buf_filesz_p, 8,
   2950 							 byte_order);
   2951 
   2952 		      /* PLT2_ASECT is from on-disk file (current
   2953 			 exec_bfd) while FILESZ is from the in-memory
   2954 			 image.  */
   2955 		      if (content2)
   2956 			filesz += bfd_section_size (plt2_asect);
   2957 		      else
   2958 			filesz -= bfd_section_size (plt2_asect);
   2959 
   2960 		      store_unsigned_integer (buf_filesz_p, 8, byte_order,
   2961 					      filesz);
   2962 
   2963 		      if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
   2964 			continue;
   2965 		    }
   2966 
   2967 		  return 0;
   2968 		}
   2969 	    }
   2970 	  else
   2971 	    return 0;
   2972 	}
   2973     }
   2974 
   2975   if (info_verbose)
   2976     {
   2977       /* It can be printed repeatedly as there is no easy way to check
   2978 	 the executable symbols/file has been already relocated to
   2979 	 displacement.  */
   2980 
   2981       gdb_printf (_("Using PIE (Position Independent Executable) "
   2982 		    "displacement %s for \"%s\".\n"),
   2983 		  paddress (current_inferior ()->arch (), exec_displacement),
   2984 		  bfd_get_filename (current_program_space->exec_bfd ()));
   2985     }
   2986 
   2987   *displacementp = exec_displacement;
   2988   return 1;
   2989 }
   2990 
   2991 /* Relocate the main executable.  This function should be called upon
   2992    stopping the inferior process at the entry point to the program.
   2993    The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
   2994    different, the main executable is relocated by the proper amount.  */
   2995 
   2996 static void
   2997 svr4_relocate_main_executable (void)
   2998 {
   2999   CORE_ADDR displacement;
   3000 
   3001   /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
   3002      probably contains the offsets computed using the PIE displacement
   3003      from the previous run, which of course are irrelevant for this run.
   3004      So we need to determine the new PIE displacement and recompute the
   3005      section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
   3006      already contains pre-computed offsets.
   3007 
   3008      If we cannot compute the PIE displacement, either:
   3009 
   3010        - The executable is not PIE.
   3011 
   3012        - SYMFILE_OBJFILE does not match the executable started in the target.
   3013 	 This can happen for main executable symbols loaded at the host while
   3014 	 `ld.so --ld-args main-executable' is loaded in the target.
   3015 
   3016      Then we leave the section offsets untouched and use them as is for
   3017      this run.  Either:
   3018 
   3019        - These section offsets were properly reset earlier, and thus
   3020 	 already contain the correct values.  This can happen for instance
   3021 	 when reconnecting via the remote protocol to a target that supports
   3022 	 the `qOffsets' packet.
   3023 
   3024        - The section offsets were not reset earlier, and the best we can
   3025 	 hope is that the old offsets are still applicable to the new run.  */
   3026 
   3027   if (! svr4_exec_displacement (&displacement))
   3028     return;
   3029 
   3030   /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
   3031      addresses.  */
   3032 
   3033   objfile *objf = current_program_space->symfile_object_file;
   3034   if (objf)
   3035     {
   3036       section_offsets new_offsets (objf->section_offsets.size (),
   3037 				   displacement);
   3038       objfile_relocate (objf, new_offsets);
   3039     }
   3040   else if (current_program_space->exec_bfd ())
   3041     {
   3042       asection *asect;
   3043 
   3044       bfd *exec_bfd = current_program_space->exec_bfd ();
   3045       for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
   3046 	exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
   3047 				  bfd_section_vma (asect) + displacement);
   3048     }
   3049 }
   3050 
   3051 /* Implement the "create_inferior_hook" target_solib_ops method.
   3052 
   3053    For SVR4 executables, this first instruction is either the first
   3054    instruction in the dynamic linker (for dynamically linked
   3055    executables) or the instruction at "start" for statically linked
   3056    executables.  For dynamically linked executables, the system
   3057    first exec's /lib/libc.so.N, which contains the dynamic linker,
   3058    and starts it running.  The dynamic linker maps in any needed
   3059    shared libraries, maps in the actual user executable, and then
   3060    jumps to "start" in the user executable.
   3061 
   3062    We can arrange to cooperate with the dynamic linker to discover the
   3063    names of shared libraries that are dynamically linked, and the base
   3064    addresses to which they are linked.
   3065 
   3066    This function is responsible for discovering those names and
   3067    addresses, and saving sufficient information about them to allow
   3068    their symbols to be read at a later time.  */
   3069 
   3070 static void
   3071 svr4_solib_create_inferior_hook (int from_tty)
   3072 {
   3073   struct svr4_info *info;
   3074 
   3075   info = get_svr4_info (current_program_space);
   3076 
   3077   /* Clear the probes-based interface's state.  */
   3078   free_probes_table (info);
   3079   info->solib_lists.clear ();
   3080 
   3081   /* Relocate the main executable if necessary.  */
   3082   svr4_relocate_main_executable ();
   3083 
   3084   /* No point setting a breakpoint in the dynamic linker if we can't
   3085      hit it (e.g., a core file, or a trace file).  */
   3086   if (!target_has_execution ())
   3087     return;
   3088 
   3089   if (!svr4_have_link_map_offsets ())
   3090     return;
   3091 
   3092   if (!enable_break (info, from_tty))
   3093     return;
   3094 }
   3095 
   3096 static void
   3097 svr4_clear_solib (program_space *pspace)
   3098 {
   3099   svr4_info *info = get_svr4_info (pspace);
   3100   info->debug_base = 0;
   3101   info->debug_loader_offset_p = 0;
   3102   info->debug_loader_offset = 0;
   3103   xfree (info->debug_loader_name);
   3104   info->debug_loader_name = NULL;
   3105 }
   3106 
   3107 /* Clear any bits of ADDR that wouldn't fit in a target-format
   3108    data pointer.  "Data pointer" here refers to whatever sort of
   3109    address the dynamic linker uses to manage its sections.  At the
   3110    moment, we don't support shared libraries on any processors where
   3111    code and data pointers are different sizes.
   3112 
   3113    This isn't really the right solution.  What we really need here is
   3114    a way to do arithmetic on CORE_ADDR values that respects the
   3115    natural pointer/address correspondence.  (For example, on the MIPS,
   3116    converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
   3117    sign-extend the value.  There, simply truncating the bits above
   3118    gdbarch_ptr_bit, as we do below, is no good.)  This should probably
   3119    be a new gdbarch method or something.  */
   3120 static CORE_ADDR
   3121 svr4_truncate_ptr (CORE_ADDR addr)
   3122 {
   3123   if (gdbarch_ptr_bit (current_inferior ()->arch ()) == sizeof (CORE_ADDR) * 8)
   3124     /* We don't need to truncate anything, and the bit twiddling below
   3125        will fail due to overflow problems.  */
   3126     return addr;
   3127   else
   3128     return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (current_inferior ()->arch ())) - 1);
   3129 }
   3130 
   3131 
   3132 static void
   3133 svr4_relocate_section_addresses (solib &so, target_section *sec)
   3134 {
   3135   bfd *abfd = sec->the_bfd_section->owner;
   3136 
   3137   sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
   3138   sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
   3139 }
   3140 
   3141 
   3143 /* Architecture-specific operations.  */
   3144 
   3145 struct solib_svr4_ops
   3146 {
   3147   /* Return a description of the layout of `struct link_map'.  */
   3148   struct link_map_offsets *(*fetch_link_map_offsets)(void) = nullptr;
   3149 };
   3150 
   3151 /* Per-architecture data key.  */
   3152 static const registry<gdbarch>::key<struct solib_svr4_ops> solib_svr4_data;
   3153 
   3154 /* Return a default for the architecture-specific operations.  */
   3155 
   3156 static struct solib_svr4_ops *
   3157 get_ops (struct gdbarch *gdbarch)
   3158 {
   3159   struct solib_svr4_ops *ops = solib_svr4_data.get (gdbarch);
   3160   if (ops == nullptr)
   3161     ops = solib_svr4_data.emplace (gdbarch);
   3162   return ops;
   3163 }
   3164 
   3165 /* Set the architecture-specific `struct link_map_offsets' fetcher for
   3166    GDBARCH to FLMO.  Also, install SVR4 solib_ops into GDBARCH.  */
   3167 
   3168 void
   3169 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
   3170 				       struct link_map_offsets *(*flmo) (void))
   3171 {
   3172   struct solib_svr4_ops *ops = get_ops (gdbarch);
   3173 
   3174   ops->fetch_link_map_offsets = flmo;
   3175 
   3176   set_gdbarch_so_ops (gdbarch, &svr4_so_ops);
   3177   set_gdbarch_iterate_over_objfiles_in_search_order
   3178     (gdbarch, svr4_iterate_over_objfiles_in_search_order);
   3179 }
   3180 
   3181 /* Fetch a link_map_offsets structure using the architecture-specific
   3182    `struct link_map_offsets' fetcher.  */
   3183 
   3184 static struct link_map_offsets *
   3185 svr4_fetch_link_map_offsets (void)
   3186 {
   3187   struct solib_svr4_ops *ops = get_ops (current_inferior ()->arch ());
   3188 
   3189   gdb_assert (ops->fetch_link_map_offsets);
   3190   return ops->fetch_link_map_offsets ();
   3191 }
   3192 
   3193 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise.  */
   3194 
   3195 static int
   3196 svr4_have_link_map_offsets (void)
   3197 {
   3198   struct solib_svr4_ops *ops = get_ops (current_inferior ()->arch ());
   3199 
   3200   return (ops->fetch_link_map_offsets != NULL);
   3201 }
   3202 
   3203 
   3205 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
   3206    `struct r_debug' and a `struct link_map' that are binary compatible
   3207    with the original SVR4 implementation.  */
   3208 
   3209 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
   3210    for an ILP32 SVR4 system.  */
   3211 
   3212 struct link_map_offsets *
   3213 svr4_ilp32_fetch_link_map_offsets (void)
   3214 {
   3215   static struct link_map_offsets lmo;
   3216   static struct link_map_offsets *lmp = NULL;
   3217 
   3218   if (lmp == NULL)
   3219     {
   3220       lmp = &lmo;
   3221 
   3222       lmo.r_version_offset = 0;
   3223       lmo.r_version_size = 4;
   3224       lmo.r_map_offset = 4;
   3225       lmo.r_brk_offset = 8;
   3226       lmo.r_ldsomap_offset = 20;
   3227       lmo.r_next_offset = -1;
   3228 
   3229       /* Everything we need is in the first 20 bytes.  */
   3230       lmo.link_map_size = 20;
   3231       lmo.l_addr_offset = 0;
   3232       lmo.l_name_offset = 4;
   3233       lmo.l_ld_offset = 8;
   3234       lmo.l_next_offset = 12;
   3235       lmo.l_prev_offset = 16;
   3236     }
   3237 
   3238   return lmp;
   3239 }
   3240 
   3241 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
   3242    for an LP64 SVR4 system.  */
   3243 
   3244 struct link_map_offsets *
   3245 svr4_lp64_fetch_link_map_offsets (void)
   3246 {
   3247   static struct link_map_offsets lmo;
   3248   static struct link_map_offsets *lmp = NULL;
   3249 
   3250   if (lmp == NULL)
   3251     {
   3252       lmp = &lmo;
   3253 
   3254       lmo.r_version_offset = 0;
   3255       lmo.r_version_size = 4;
   3256       lmo.r_map_offset = 8;
   3257       lmo.r_brk_offset = 16;
   3258       lmo.r_ldsomap_offset = 40;
   3259       lmo.r_next_offset = -1;
   3260 
   3261       /* Everything we need is in the first 40 bytes.  */
   3262       lmo.link_map_size = 40;
   3263       lmo.l_addr_offset = 0;
   3264       lmo.l_name_offset = 8;
   3265       lmo.l_ld_offset = 16;
   3266       lmo.l_next_offset = 24;
   3267       lmo.l_prev_offset = 32;
   3268     }
   3269 
   3270   return lmp;
   3271 }
   3272 
   3273 
   3275 /* Return the DSO matching OBJFILE or nullptr if none can be found.  */
   3276 
   3277 static const solib *
   3278 find_solib_for_objfile (struct objfile *objfile)
   3279 {
   3280   if (objfile == nullptr)
   3281     return nullptr;
   3282 
   3283   /* If OBJFILE is a separate debug object file, look for the original
   3284      object file.  */
   3285   if (objfile->separate_debug_objfile_backlink != nullptr)
   3286     objfile = objfile->separate_debug_objfile_backlink;
   3287 
   3288   for (const solib &so : current_program_space->solibs ())
   3289     if (so.objfile == objfile)
   3290       return &so;
   3291 
   3292   return nullptr;
   3293 }
   3294 
   3295 /* Return the address of the r_debug object for the namespace containing
   3296    SOLIB or zero if it cannot be found.  This may happen when symbol files
   3297    are added manually, for example, or with the main executable.
   3298 
   3299    Current callers treat zero as initial namespace so they are doing the
   3300    right thing for the main executable.  */
   3301 
   3302 static CORE_ADDR
   3303 find_debug_base_for_solib (const solib *solib)
   3304 {
   3305   if (solib == nullptr)
   3306     return 0;
   3307 
   3308   svr4_info *info = get_svr4_info (current_program_space);
   3309   gdb_assert (info != nullptr);
   3310 
   3311   auto *lm_info
   3312     = gdb::checked_static_cast<const lm_info_svr4 *> (solib->lm_info.get ());
   3313 
   3314   for (const auto &tuple : info->solib_lists)
   3315     {
   3316       CORE_ADDR debug_base = tuple.first;
   3317       const std::vector<svr4_so> &sos = tuple.second;
   3318 
   3319       for (const svr4_so &so : sos)
   3320 	if (svr4_same (solib->so_original_name.c_str (), so.name.c_str (),
   3321 		       *lm_info, *so.lm_info))
   3322 	  return debug_base;
   3323     }
   3324 
   3325   return 0;
   3326 }
   3327 
   3328 /* Search order for ELF DSOs linked with -Bsymbolic.  Those DSOs have a
   3329    different rule for symbol lookup.  The lookup begins here in the DSO,
   3330    not in the main executable.  When starting from CURRENT_OBJFILE, we
   3331    stay in the same namespace as that file.  Otherwise, we only consider
   3332    the initial namespace.  */
   3333 
   3334 static void
   3335 svr4_iterate_over_objfiles_in_search_order
   3336   (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
   3337    objfile *current_objfile)
   3338 {
   3339   bool checked_current_objfile = false;
   3340   if (current_objfile != nullptr)
   3341     {
   3342       bfd *abfd;
   3343 
   3344       if (current_objfile->separate_debug_objfile_backlink != nullptr)
   3345 	current_objfile = current_objfile->separate_debug_objfile_backlink;
   3346 
   3347       if (current_objfile == current_program_space->symfile_object_file)
   3348 	abfd = current_program_space->exec_bfd ();
   3349       else
   3350 	abfd = current_objfile->obfd.get ();
   3351 
   3352       if (abfd != nullptr
   3353 	  && gdb_bfd_scan_elf_dyntag (DT_SYMBOLIC, abfd, nullptr, nullptr) == 1)
   3354 	{
   3355 	  checked_current_objfile = true;
   3356 	  if (cb (current_objfile))
   3357 	    return;
   3358 	}
   3359     }
   3360 
   3361   /* The linker namespace to iterate identified by the address of its
   3362      r_debug object, defaulting to the initial namespace.  */
   3363   CORE_ADDR initial = elf_locate_base ();
   3364   const solib *curr_solib = find_solib_for_objfile (current_objfile);
   3365   CORE_ADDR debug_base = find_debug_base_for_solib (curr_solib);
   3366   if (debug_base == 0)
   3367     debug_base = initial;
   3368 
   3369   for (objfile *objfile : current_program_space->objfiles ())
   3370     {
   3371       if (checked_current_objfile && objfile == current_objfile)
   3372 	continue;
   3373 
   3374       /* Try to determine the namespace into which objfile was loaded.
   3375 
   3376 	 If we fail, e.g. for manually added symbol files or for the main
   3377 	 executable, we assume that they were added to the initial
   3378 	 namespace.  */
   3379       const solib *solib = find_solib_for_objfile (objfile);
   3380       CORE_ADDR solib_base = find_debug_base_for_solib (solib);
   3381       if (solib_base == 0)
   3382 	solib_base = initial;
   3383 
   3384       /* Ignore objfiles that were added to a different namespace.  */
   3385       if (solib_base != debug_base)
   3386 	continue;
   3387 
   3388       if (cb (objfile))
   3389 	return;
   3390     }
   3391 }
   3392 
   3393 const struct solib_ops svr4_so_ops =
   3394 {
   3395   svr4_relocate_section_addresses,
   3396   svr4_clear_so,
   3397   svr4_clear_solib,
   3398   svr4_solib_create_inferior_hook,
   3399   svr4_current_sos,
   3400   open_symbol_file_object,
   3401   svr4_in_dynsym_resolve_code,
   3402   solib_bfd_open,
   3403   nullptr,
   3404   svr4_same,
   3405   svr4_keep_data_in_core,
   3406   svr4_update_solib_event_breakpoints,
   3407   svr4_handle_solib_event,
   3408 };
   3409 
   3410 void _initialize_svr4_solib ();
   3411 void
   3412 _initialize_svr4_solib ()
   3413 {
   3414   gdb::observers::free_objfile.attach (svr4_free_objfile_observer,
   3415 				       "solib-svr4");
   3416 }
   3417