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