GDB (API)
/home/stan/gdb/src/gdb/solib-svr4.c
Go to the documentation of this file.
00001 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
00002 
00003    Copyright (C) 1990-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include "elf/external.h"
00023 #include "elf/common.h"
00024 #include "elf/mips.h"
00025 
00026 #include "symtab.h"
00027 #include "bfd.h"
00028 #include "symfile.h"
00029 #include "objfiles.h"
00030 #include "gdbcore.h"
00031 #include "target.h"
00032 #include "inferior.h"
00033 #include "regcache.h"
00034 #include "gdbthread.h"
00035 #include "observer.h"
00036 
00037 #include "gdb_assert.h"
00038 
00039 #include "solist.h"
00040 #include "solib.h"
00041 #include "solib-svr4.h"
00042 
00043 #include "bfd-target.h"
00044 #include "elf-bfd.h"
00045 #include "exec.h"
00046 #include "auxv.h"
00047 #include "exceptions.h"
00048 #include "gdb_bfd.h"
00049 #include "probe.h"
00050 
00051 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
00052 static int svr4_have_link_map_offsets (void);
00053 static void svr4_relocate_main_executable (void);
00054 static void svr4_free_library_list (void *p_list);
00055 
00056 /* Link map info to include in an allocated so_list entry.  */
00057 
00058 struct lm_info
00059   {
00060     /* Amount by which addresses in the binary should be relocated to
00061        match the inferior.  The direct inferior value is L_ADDR_INFERIOR.
00062        When prelinking is involved and the prelink base address changes,
00063        we may need a different offset - the recomputed offset is in L_ADDR.
00064        It is commonly the same value.  It is cached as we want to warn about
00065        the difference and compute it only once.  L_ADDR is valid
00066        iff L_ADDR_P.  */
00067     CORE_ADDR l_addr, l_addr_inferior;
00068     unsigned int l_addr_p : 1;
00069 
00070     /* The target location of lm.  */
00071     CORE_ADDR lm_addr;
00072 
00073     /* Values read in from inferior's fields of the same name.  */
00074     CORE_ADDR l_ld, l_next, l_prev, l_name;
00075   };
00076 
00077 /* On SVR4 systems, a list of symbols in the dynamic linker where
00078    GDB can try to place a breakpoint to monitor shared library
00079    events.
00080 
00081    If none of these symbols are found, or other errors occur, then
00082    SVR4 systems will fall back to using a symbol as the "startup
00083    mapping complete" breakpoint address.  */
00084 
00085 static const char * const solib_break_names[] =
00086 {
00087   "r_debug_state",
00088   "_r_debug_state",
00089   "_dl_debug_state",
00090   "rtld_db_dlactivity",
00091   "__dl_rtld_db_dlactivity",
00092   "_rtld_debug_state",
00093 
00094   NULL
00095 };
00096 
00097 static const char * const bkpt_names[] =
00098 {
00099   "_start",
00100   "__start",
00101   "main",
00102   NULL
00103 };
00104 
00105 static const  char * const main_name_list[] =
00106 {
00107   "main_$main",
00108   NULL
00109 };
00110 
00111 /* What to do when a probe stop occurs.  */
00112 
00113 enum probe_action
00114 {
00115   /* Something went seriously wrong.  Stop using probes and
00116      revert to using the older interface.  */
00117   PROBES_INTERFACE_FAILED,
00118 
00119   /* No action is required.  The shared object list is still
00120      valid.  */
00121   DO_NOTHING,
00122 
00123   /* The shared object list should be reloaded entirely.  */
00124   FULL_RELOAD,
00125 
00126   /* Attempt to incrementally update the shared object list. If
00127      the update fails or is not possible, fall back to reloading
00128      the list in full.  */
00129   UPDATE_OR_RELOAD,
00130 };
00131 
00132 /* A probe's name and its associated action.  */
00133 
00134 struct probe_info
00135 {
00136   /* The name of the probe.  */
00137   const char *name;
00138 
00139   /* What to do when a probe stop occurs.  */
00140   enum probe_action action;
00141 };
00142 
00143 /* A list of named probes and their associated actions.  If all
00144    probes are present in the dynamic linker then the probes-based
00145    interface will be used.  */
00146 
00147 static const struct probe_info probe_info[] =
00148 {
00149   { "init_start", DO_NOTHING },
00150   { "init_complete", FULL_RELOAD },
00151   { "map_start", DO_NOTHING },
00152   { "map_failed", DO_NOTHING },
00153   { "reloc_complete", UPDATE_OR_RELOAD },
00154   { "unmap_start", DO_NOTHING },
00155   { "unmap_complete", FULL_RELOAD },
00156 };
00157 
00158 #define NUM_PROBES ARRAY_SIZE (probe_info)
00159 
00160 /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
00161    the same shared library.  */
00162 
00163 static int
00164 svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
00165 {
00166   if (strcmp (gdb_so_name, inferior_so_name) == 0)
00167     return 1;
00168 
00169   /* On Solaris, when starting inferior we think that dynamic linker is
00170      /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
00171      contains /lib/ld.so.1.  Sometimes one file is a link to another, but
00172      sometimes they have identical content, but are not linked to each
00173      other.  We don't restrict this check for Solaris, but the chances
00174      of running into this situation elsewhere are very low.  */
00175   if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
00176       && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
00177     return 1;
00178 
00179   /* Similarly, we observed the same issue with sparc64, but with
00180      different locations.  */
00181   if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
00182       && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
00183     return 1;
00184 
00185   return 0;
00186 }
00187 
00188 static int
00189 svr4_same (struct so_list *gdb, struct so_list *inferior)
00190 {
00191   return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
00192 }
00193 
00194 static struct lm_info *
00195 lm_info_read (CORE_ADDR lm_addr)
00196 {
00197   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
00198   gdb_byte *lm;
00199   struct lm_info *lm_info;
00200   struct cleanup *back_to;
00201 
00202   lm = xmalloc (lmo->link_map_size);
00203   back_to = make_cleanup (xfree, lm);
00204 
00205   if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)
00206     {
00207       warning (_("Error reading shared library list entry at %s"),
00208                paddress (target_gdbarch (), lm_addr)),
00209       lm_info = NULL;
00210     }
00211   else
00212     {
00213       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00214 
00215       lm_info = xzalloc (sizeof (*lm_info));
00216       lm_info->lm_addr = lm_addr;
00217 
00218       lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
00219                                                         ptr_type);
00220       lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
00221       lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
00222                                                ptr_type);
00223       lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
00224                                                ptr_type);
00225       lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
00226                                                ptr_type);
00227     }
00228 
00229   do_cleanups (back_to);
00230 
00231   return lm_info;
00232 }
00233 
00234 static int
00235 has_lm_dynamic_from_link_map (void)
00236 {
00237   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
00238 
00239   return lmo->l_ld_offset >= 0;
00240 }
00241 
00242 static CORE_ADDR
00243 lm_addr_check (const struct so_list *so, bfd *abfd)
00244 {
00245   if (!so->lm_info->l_addr_p)
00246     {
00247       struct bfd_section *dyninfo_sect;
00248       CORE_ADDR l_addr, l_dynaddr, dynaddr;
00249 
00250       l_addr = so->lm_info->l_addr_inferior;
00251 
00252       if (! abfd || ! has_lm_dynamic_from_link_map ())
00253         goto set_addr;
00254 
00255       l_dynaddr = so->lm_info->l_ld;
00256 
00257       dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
00258       if (dyninfo_sect == NULL)
00259         goto set_addr;
00260 
00261       dynaddr = bfd_section_vma (abfd, dyninfo_sect);
00262 
00263       if (dynaddr + l_addr != l_dynaddr)
00264         {
00265           CORE_ADDR align = 0x1000;
00266           CORE_ADDR minpagesize = align;
00267 
00268           if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
00269             {
00270               Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
00271               Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
00272               int i;
00273 
00274               align = 1;
00275 
00276               for (i = 0; i < ehdr->e_phnum; i++)
00277                 if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
00278                   align = phdr[i].p_align;
00279 
00280               minpagesize = get_elf_backend_data (abfd)->minpagesize;
00281             }
00282 
00283           /* Turn it into a mask.  */
00284           align--;
00285 
00286           /* If the changes match the alignment requirements, we
00287              assume we're using a core file that was generated by the
00288              same binary, just prelinked with a different base offset.
00289              If it doesn't match, we may have a different binary, the
00290              same binary with the dynamic table loaded at an unrelated
00291              location, or anything, really.  To avoid regressions,
00292              don't adjust the base offset in the latter case, although
00293              odds are that, if things really changed, debugging won't
00294              quite work.
00295 
00296              One could expect more the condition
00297                ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
00298              but the one below is relaxed for PPC.  The PPC kernel supports
00299              either 4k or 64k page sizes.  To be prepared for 64k pages,
00300              PPC ELF files are built using an alignment requirement of 64k.
00301              However, when running on a kernel supporting 4k pages, the memory
00302              mapping of the library may not actually happen on a 64k boundary!
00303 
00304              (In the usual case where (l_addr & align) == 0, this check is
00305              equivalent to the possibly expected check above.)
00306 
00307              Even on PPC it must be zero-aligned at least for MINPAGESIZE.  */
00308 
00309           l_addr = l_dynaddr - dynaddr;
00310 
00311           if ((l_addr & (minpagesize - 1)) == 0
00312               && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
00313             {
00314               if (info_verbose)
00315                 printf_unfiltered (_("Using PIC (Position Independent Code) "
00316                                      "prelink displacement %s for \"%s\".\n"),
00317                                    paddress (target_gdbarch (), l_addr),
00318                                    so->so_name);
00319             }
00320           else
00321             {
00322               /* There is no way to verify the library file matches.  prelink
00323                  can during prelinking of an unprelinked file (or unprelinking
00324                  of a prelinked file) shift the DYNAMIC segment by arbitrary
00325                  offset without any page size alignment.  There is no way to
00326                  find out the ELF header and/or Program Headers for a limited
00327                  verification if it they match.  One could do a verification
00328                  of the DYNAMIC segment.  Still the found address is the best
00329                  one GDB could find.  */
00330 
00331               warning (_(".dynamic section for \"%s\" "
00332                          "is not at the expected address "
00333                          "(wrong library or version mismatch?)"), so->so_name);
00334             }
00335         }
00336 
00337     set_addr:
00338       so->lm_info->l_addr = l_addr;
00339       so->lm_info->l_addr_p = 1;
00340     }
00341 
00342   return so->lm_info->l_addr;
00343 }
00344 
00345 /* Per pspace SVR4 specific data.  */
00346 
00347 struct svr4_info
00348 {
00349   CORE_ADDR debug_base; /* Base of dynamic linker structures.  */
00350 
00351   /* Validity flag for debug_loader_offset.  */
00352   int debug_loader_offset_p;
00353 
00354   /* Load address for the dynamic linker, inferred.  */
00355   CORE_ADDR debug_loader_offset;
00356 
00357   /* Name of the dynamic linker, valid if debug_loader_offset_p.  */
00358   char *debug_loader_name;
00359 
00360   /* Load map address for the main executable.  */
00361   CORE_ADDR main_lm_addr;
00362 
00363   CORE_ADDR interp_text_sect_low;
00364   CORE_ADDR interp_text_sect_high;
00365   CORE_ADDR interp_plt_sect_low;
00366   CORE_ADDR interp_plt_sect_high;
00367 
00368   /* Nonzero if the list of objects was last obtained from the target
00369      via qXfer:libraries-svr4:read.  */
00370   int using_xfer;
00371 
00372   /* Table of struct probe_and_action instances, used by the
00373      probes-based interface to map breakpoint addresses to probes
00374      and their associated actions.  Lookup is performed using
00375      probe_and_action->probe->address.  */
00376   htab_t probes_table;
00377 
00378   /* List of objects loaded into the inferior, used by the probes-
00379      based interface.  */
00380   struct so_list *solib_list;
00381 };
00382 
00383 /* Per-program-space data key.  */
00384 static const struct program_space_data *solib_svr4_pspace_data;
00385 
00386 /* Free the probes table.  */
00387 
00388 static void
00389 free_probes_table (struct svr4_info *info)
00390 {
00391   if (info->probes_table == NULL)
00392     return;
00393 
00394   htab_delete (info->probes_table);
00395   info->probes_table = NULL;
00396 }
00397 
00398 /* Free the solib list.  */
00399 
00400 static void
00401 free_solib_list (struct svr4_info *info)
00402 {
00403   svr4_free_library_list (&info->solib_list);
00404   info->solib_list = NULL;
00405 }
00406 
00407 static void
00408 svr4_pspace_data_cleanup (struct program_space *pspace, void *arg)
00409 {
00410   struct svr4_info *info;
00411 
00412   info = program_space_data (pspace, solib_svr4_pspace_data);
00413   if (info == NULL)
00414     return;
00415 
00416   free_probes_table (info);
00417   free_solib_list (info);
00418 
00419   xfree (info);
00420 }
00421 
00422 /* Get the current svr4 data.  If none is found yet, add it now.  This
00423    function always returns a valid object.  */
00424 
00425 static struct svr4_info *
00426 get_svr4_info (void)
00427 {
00428   struct svr4_info *info;
00429 
00430   info = program_space_data (current_program_space, solib_svr4_pspace_data);
00431   if (info != NULL)
00432     return info;
00433 
00434   info = XZALLOC (struct svr4_info);
00435   set_program_space_data (current_program_space, solib_svr4_pspace_data, info);
00436   return info;
00437 }
00438 
00439 /* Local function prototypes */
00440 
00441 static int match_main (const char *);
00442 
00443 /* Read program header TYPE from inferior memory.  The header is found
00444    by scanning the OS auxillary vector.
00445 
00446    If TYPE == -1, return the program headers instead of the contents of
00447    one program header.
00448 
00449    Return a pointer to allocated memory holding the program header contents,
00450    or NULL on failure.  If sucessful, and unless P_SECT_SIZE is NULL, the
00451    size of those contents is returned to P_SECT_SIZE.  Likewise, the target
00452    architecture size (32-bit or 64-bit) is returned to P_ARCH_SIZE.  */
00453 
00454 static gdb_byte *
00455 read_program_header (int type, int *p_sect_size, int *p_arch_size)
00456 {
00457   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00458   CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
00459   int arch_size, sect_size;
00460   CORE_ADDR sect_addr;
00461   gdb_byte *buf;
00462   int pt_phdr_p = 0;
00463 
00464   /* Get required auxv elements from target.  */
00465   if (target_auxv_search (&current_target, AT_PHDR, &at_phdr) <= 0)
00466     return 0;
00467   if (target_auxv_search (&current_target, AT_PHENT, &at_phent) <= 0)
00468     return 0;
00469   if (target_auxv_search (&current_target, AT_PHNUM, &at_phnum) <= 0)
00470     return 0;
00471   if (!at_phdr || !at_phnum)
00472     return 0;
00473 
00474   /* Determine ELF architecture type.  */
00475   if (at_phent == sizeof (Elf32_External_Phdr))
00476     arch_size = 32;
00477   else if (at_phent == sizeof (Elf64_External_Phdr))
00478     arch_size = 64;
00479   else
00480     return 0;
00481 
00482   /* Find the requested segment.  */
00483   if (type == -1)
00484     {
00485       sect_addr = at_phdr;
00486       sect_size = at_phent * at_phnum;
00487     }
00488   else if (arch_size == 32)
00489     {
00490       Elf32_External_Phdr phdr;
00491       int i;
00492 
00493       /* Search for requested PHDR.  */
00494       for (i = 0; i < at_phnum; i++)
00495         {
00496           int p_type;
00497 
00498           if (target_read_memory (at_phdr + i * sizeof (phdr),
00499                                   (gdb_byte *)&phdr, sizeof (phdr)))
00500             return 0;
00501 
00502           p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
00503                                              4, byte_order);
00504 
00505           if (p_type == PT_PHDR)
00506             {
00507               pt_phdr_p = 1;
00508               pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
00509                                                   4, byte_order);
00510             }
00511 
00512           if (p_type == type)
00513             break;
00514         }
00515 
00516       if (i == at_phnum)
00517         return 0;
00518 
00519       /* Retrieve address and size.  */
00520       sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
00521                                             4, byte_order);
00522       sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
00523                                             4, byte_order);
00524     }
00525   else
00526     {
00527       Elf64_External_Phdr phdr;
00528       int i;
00529 
00530       /* Search for requested PHDR.  */
00531       for (i = 0; i < at_phnum; i++)
00532         {
00533           int p_type;
00534 
00535           if (target_read_memory (at_phdr + i * sizeof (phdr),
00536                                   (gdb_byte *)&phdr, sizeof (phdr)))
00537             return 0;
00538 
00539           p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
00540                                              4, byte_order);
00541 
00542           if (p_type == PT_PHDR)
00543             {
00544               pt_phdr_p = 1;
00545               pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
00546                                                   8, byte_order);
00547             }
00548 
00549           if (p_type == type)
00550             break;
00551         }
00552 
00553       if (i == at_phnum)
00554         return 0;
00555 
00556       /* Retrieve address and size.  */
00557       sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
00558                                             8, byte_order);
00559       sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
00560                                             8, byte_order);
00561     }
00562 
00563   /* PT_PHDR is optional, but we really need it
00564      for PIE to make this work in general.  */
00565 
00566   if (pt_phdr_p)
00567     {
00568       /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
00569          Relocation offset is the difference between the two. */
00570       sect_addr = sect_addr + (at_phdr - pt_phdr);
00571     }
00572 
00573   /* Read in requested program header.  */
00574   buf = xmalloc (sect_size);
00575   if (target_read_memory (sect_addr, buf, sect_size))
00576     {
00577       xfree (buf);
00578       return NULL;
00579     }
00580 
00581   if (p_arch_size)
00582     *p_arch_size = arch_size;
00583   if (p_sect_size)
00584     *p_sect_size = sect_size;
00585 
00586   return buf;
00587 }
00588 
00589 
00590 /* Return program interpreter string.  */
00591 static char *
00592 find_program_interpreter (void)
00593 {
00594   gdb_byte *buf = NULL;
00595 
00596   /* If we have an exec_bfd, use its section table.  */
00597   if (exec_bfd
00598       && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
00599    {
00600      struct bfd_section *interp_sect;
00601 
00602      interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
00603      if (interp_sect != NULL)
00604       {
00605         int sect_size = bfd_section_size (exec_bfd, interp_sect);
00606 
00607         buf = xmalloc (sect_size);
00608         bfd_get_section_contents (exec_bfd, interp_sect, buf, 0, sect_size);
00609       }
00610    }
00611 
00612   /* If we didn't find it, use the target auxillary vector.  */
00613   if (!buf)
00614     buf = read_program_header (PT_INTERP, NULL, NULL);
00615 
00616   return (char *) buf;
00617 }
00618 
00619 
00620 /* Scan for DYNTAG in .dynamic section of ABFD.  If DYNTAG is found 1 is
00621    returned and the corresponding PTR is set.  */
00622 
00623 static int
00624 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
00625 {
00626   int arch_size, step, sect_size;
00627   long dyn_tag;
00628   CORE_ADDR dyn_ptr, dyn_addr;
00629   gdb_byte *bufend, *bufstart, *buf;
00630   Elf32_External_Dyn *x_dynp_32;
00631   Elf64_External_Dyn *x_dynp_64;
00632   struct bfd_section *sect;
00633   struct target_section *target_section;
00634 
00635   if (abfd == NULL)
00636     return 0;
00637 
00638   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
00639     return 0;
00640 
00641   arch_size = bfd_get_arch_size (abfd);
00642   if (arch_size == -1)
00643     return 0;
00644 
00645   /* Find the start address of the .dynamic section.  */
00646   sect = bfd_get_section_by_name (abfd, ".dynamic");
00647   if (sect == NULL)
00648     return 0;
00649 
00650   for (target_section = current_target_sections->sections;
00651        target_section < current_target_sections->sections_end;
00652        target_section++)
00653     if (sect == target_section->the_bfd_section)
00654       break;
00655   if (target_section < current_target_sections->sections_end)
00656     dyn_addr = target_section->addr;
00657   else
00658     {
00659       /* ABFD may come from OBJFILE acting only as a symbol file without being
00660          loaded into the target (see add_symbol_file_command).  This case is
00661          such fallback to the file VMA address without the possibility of
00662          having the section relocated to its actual in-memory address.  */
00663 
00664       dyn_addr = bfd_section_vma (abfd, sect);
00665     }
00666 
00667   /* Read in .dynamic from the BFD.  We will get the actual value
00668      from memory later.  */
00669   sect_size = bfd_section_size (abfd, sect);
00670   buf = bufstart = alloca (sect_size);
00671   if (!bfd_get_section_contents (abfd, sect,
00672                                  buf, 0, sect_size))
00673     return 0;
00674 
00675   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
00676   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
00677                            : sizeof (Elf64_External_Dyn);
00678   for (bufend = buf + sect_size;
00679        buf < bufend;
00680        buf += step)
00681   {
00682     if (arch_size == 32)
00683       {
00684         x_dynp_32 = (Elf32_External_Dyn *) buf;
00685         dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
00686         dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
00687       }
00688     else
00689       {
00690         x_dynp_64 = (Elf64_External_Dyn *) buf;
00691         dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
00692         dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
00693       }
00694      if (dyn_tag == DT_NULL)
00695        return 0;
00696      if (dyn_tag == dyntag)
00697        {
00698          /* If requested, try to read the runtime value of this .dynamic
00699             entry.  */
00700          if (ptr)
00701            {
00702              struct type *ptr_type;
00703              gdb_byte ptr_buf[8];
00704              CORE_ADDR ptr_addr;
00705 
00706              ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00707              ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
00708              if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
00709                dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
00710              *ptr = dyn_ptr;
00711            }
00712          return 1;
00713        }
00714   }
00715 
00716   return 0;
00717 }
00718 
00719 /* Scan for DYNTAG in .dynamic section of the target's main executable,
00720    found by consulting the OS auxillary vector.  If DYNTAG is found 1 is
00721    returned and the corresponding PTR is set.  */
00722 
00723 static int
00724 scan_dyntag_auxv (int dyntag, CORE_ADDR *ptr)
00725 {
00726   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00727   int sect_size, arch_size, step;
00728   long dyn_tag;
00729   CORE_ADDR dyn_ptr;
00730   gdb_byte *bufend, *bufstart, *buf;
00731 
00732   /* Read in .dynamic section.  */
00733   buf = bufstart = read_program_header (PT_DYNAMIC, &sect_size, &arch_size);
00734   if (!buf)
00735     return 0;
00736 
00737   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
00738   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
00739                            : sizeof (Elf64_External_Dyn);
00740   for (bufend = buf + sect_size;
00741        buf < bufend;
00742        buf += step)
00743   {
00744     if (arch_size == 32)
00745       {
00746         Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
00747 
00748         dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
00749                                             4, byte_order);
00750         dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
00751                                             4, byte_order);
00752       }
00753     else
00754       {
00755         Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
00756 
00757         dyn_tag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
00758                                             8, byte_order);
00759         dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
00760                                             8, byte_order);
00761       }
00762     if (dyn_tag == DT_NULL)
00763       break;
00764 
00765     if (dyn_tag == dyntag)
00766       {
00767         if (ptr)
00768           *ptr = dyn_ptr;
00769 
00770         xfree (bufstart);
00771         return 1;
00772       }
00773   }
00774 
00775   xfree (bufstart);
00776   return 0;
00777 }
00778 
00779 /* Locate the base address of dynamic linker structs for SVR4 elf
00780    targets.
00781 
00782    For SVR4 elf targets the address of the dynamic linker's runtime
00783    structure is contained within the dynamic info section in the
00784    executable file.  The dynamic section is also mapped into the
00785    inferior address space.  Because the runtime loader fills in the
00786    real address before starting the inferior, we have to read in the
00787    dynamic info section from the inferior address space.
00788    If there are any errors while trying to find the address, we
00789    silently return 0, otherwise the found address is returned.  */
00790 
00791 static CORE_ADDR
00792 elf_locate_base (void)
00793 {
00794   struct minimal_symbol *msymbol;
00795   CORE_ADDR dyn_ptr;
00796 
00797   /* Look for DT_MIPS_RLD_MAP first.  MIPS executables use this
00798      instead of DT_DEBUG, although they sometimes contain an unused
00799      DT_DEBUG.  */
00800   if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr)
00801       || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr))
00802     {
00803       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00804       gdb_byte *pbuf;
00805       int pbuf_size = TYPE_LENGTH (ptr_type);
00806 
00807       pbuf = alloca (pbuf_size);
00808       /* DT_MIPS_RLD_MAP contains a pointer to the address
00809          of the dynamic link structure.  */
00810       if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
00811         return 0;
00812       return extract_typed_address (pbuf, ptr_type);
00813     }
00814 
00815   /* Find DT_DEBUG.  */
00816   if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr)
00817       || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr))
00818     return dyn_ptr;
00819 
00820   /* This may be a static executable.  Look for the symbol
00821      conventionally named _r_debug, as a last resort.  */
00822   msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
00823   if (msymbol != NULL)
00824     return SYMBOL_VALUE_ADDRESS (msymbol);
00825 
00826   /* DT_DEBUG entry not found.  */
00827   return 0;
00828 }
00829 
00830 /* Locate the base address of dynamic linker structs.
00831 
00832    For both the SunOS and SVR4 shared library implementations, if the
00833    inferior executable has been linked dynamically, there is a single
00834    address somewhere in the inferior's data space which is the key to
00835    locating all of the dynamic linker's runtime structures.  This
00836    address is the value of the debug base symbol.  The job of this
00837    function is to find and return that address, or to return 0 if there
00838    is no such address (the executable is statically linked for example).
00839 
00840    For SunOS, the job is almost trivial, since the dynamic linker and
00841    all of it's structures are statically linked to the executable at
00842    link time.  Thus the symbol for the address we are looking for has
00843    already been added to the minimal symbol table for the executable's
00844    objfile at the time the symbol file's symbols were read, and all we
00845    have to do is look it up there.  Note that we explicitly do NOT want
00846    to find the copies in the shared library.
00847 
00848    The SVR4 version is a bit more complicated because the address
00849    is contained somewhere in the dynamic info section.  We have to go
00850    to a lot more work to discover the address of the debug base symbol.
00851    Because of this complexity, we cache the value we find and return that
00852    value on subsequent invocations.  Note there is no copy in the
00853    executable symbol tables.  */
00854 
00855 static CORE_ADDR
00856 locate_base (struct svr4_info *info)
00857 {
00858   /* Check to see if we have a currently valid address, and if so, avoid
00859      doing all this work again and just return the cached address.  If
00860      we have no cached address, try to locate it in the dynamic info
00861      section for ELF executables.  There's no point in doing any of this
00862      though if we don't have some link map offsets to work with.  */
00863 
00864   if (info->debug_base == 0 && svr4_have_link_map_offsets ())
00865     info->debug_base = elf_locate_base ();
00866   return info->debug_base;
00867 }
00868 
00869 /* Find the first element in the inferior's dynamic link map, and
00870    return its address in the inferior.  Return zero if the address
00871    could not be determined.
00872 
00873    FIXME: Perhaps we should validate the info somehow, perhaps by
00874    checking r_version for a known version number, or r_state for
00875    RT_CONSISTENT.  */
00876 
00877 static CORE_ADDR
00878 solib_svr4_r_map (struct svr4_info *info)
00879 {
00880   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
00881   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00882   CORE_ADDR addr = 0;
00883   volatile struct gdb_exception ex;
00884 
00885   TRY_CATCH (ex, RETURN_MASK_ERROR)
00886     {
00887       addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
00888                                         ptr_type);
00889     }
00890   exception_print (gdb_stderr, ex);
00891   return addr;
00892 }
00893 
00894 /* Find r_brk from the inferior's debug base.  */
00895 
00896 static CORE_ADDR
00897 solib_svr4_r_brk (struct svr4_info *info)
00898 {
00899   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
00900   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00901 
00902   return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
00903                                     ptr_type);
00904 }
00905 
00906 /* Find the link map for the dynamic linker (if it is not in the
00907    normal list of loaded shared objects).  */
00908 
00909 static CORE_ADDR
00910 solib_svr4_r_ldsomap (struct svr4_info *info)
00911 {
00912   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
00913   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00914   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00915   ULONGEST version;
00916 
00917   /* Check version, and return zero if `struct r_debug' doesn't have
00918      the r_ldsomap member.  */
00919   version
00920     = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
00921                                     lmo->r_version_size, byte_order);
00922   if (version < 2 || lmo->r_ldsomap_offset == -1)
00923     return 0;
00924 
00925   return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
00926                                     ptr_type);
00927 }
00928 
00929 /* On Solaris systems with some versions of the dynamic linker,
00930    ld.so's l_name pointer points to the SONAME in the string table
00931    rather than into writable memory.  So that GDB can find shared
00932    libraries when loading a core file generated by gcore, ensure that
00933    memory areas containing the l_name string are saved in the core
00934    file.  */
00935 
00936 static int
00937 svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
00938 {
00939   struct svr4_info *info;
00940   CORE_ADDR ldsomap;
00941   struct so_list *new;
00942   struct cleanup *old_chain;
00943   CORE_ADDR name_lm;
00944 
00945   info = get_svr4_info ();
00946 
00947   info->debug_base = 0;
00948   locate_base (info);
00949   if (!info->debug_base)
00950     return 0;
00951 
00952   ldsomap = solib_svr4_r_ldsomap (info);
00953   if (!ldsomap)
00954     return 0;
00955 
00956   new = XZALLOC (struct so_list);
00957   old_chain = make_cleanup (xfree, new);
00958   new->lm_info = lm_info_read (ldsomap);
00959   make_cleanup (xfree, new->lm_info);
00960   name_lm = new->lm_info ? new->lm_info->l_name : 0;
00961   do_cleanups (old_chain);
00962 
00963   return (name_lm >= vaddr && name_lm < vaddr + size);
00964 }
00965 
00966 /* Implement the "open_symbol_file_object" target_so_ops method.
00967 
00968    If no open symbol file, attempt to locate and open the main symbol
00969    file.  On SVR4 systems, this is the first link map entry.  If its
00970    name is here, we can open it.  Useful when attaching to a process
00971    without first loading its symbol file.  */
00972 
00973 static int
00974 open_symbol_file_object (void *from_ttyp)
00975 {
00976   CORE_ADDR lm, l_name;
00977   char *filename;
00978   int errcode;
00979   int from_tty = *(int *)from_ttyp;
00980   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
00981   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00982   int l_name_size = TYPE_LENGTH (ptr_type);
00983   gdb_byte *l_name_buf = xmalloc (l_name_size);
00984   struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
00985   struct svr4_info *info = get_svr4_info ();
00986 
00987   if (symfile_objfile)
00988     if (!query (_("Attempt to reload symbols from process? ")))
00989       {
00990         do_cleanups (cleanups);
00991         return 0;
00992       }
00993 
00994   /* Always locate the debug struct, in case it has moved.  */
00995   info->debug_base = 0;
00996   if (locate_base (info) == 0)
00997     {
00998       do_cleanups (cleanups);
00999       return 0; /* failed somehow...  */
01000     }
01001 
01002   /* First link map member should be the executable.  */
01003   lm = solib_svr4_r_map (info);
01004   if (lm == 0)
01005     {
01006       do_cleanups (cleanups);
01007       return 0; /* failed somehow...  */
01008     }
01009 
01010   /* Read address of name from target memory to GDB.  */
01011   read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size);
01012 
01013   /* Convert the address to host format.  */
01014   l_name = extract_typed_address (l_name_buf, ptr_type);
01015 
01016   if (l_name == 0)
01017     {
01018       do_cleanups (cleanups);
01019       return 0;         /* No filename.  */
01020     }
01021 
01022   /* Now fetch the filename from target memory.  */
01023   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
01024   make_cleanup (xfree, filename);
01025 
01026   if (errcode)
01027     {
01028       warning (_("failed to read exec filename from attached file: %s"),
01029                safe_strerror (errcode));
01030       do_cleanups (cleanups);
01031       return 0;
01032     }
01033 
01034   /* Have a pathname: read the symbol file.  */
01035   symbol_file_add_main (filename, from_tty);
01036 
01037   do_cleanups (cleanups);
01038   return 1;
01039 }
01040 
01041 /* Data exchange structure for the XML parser as returned by
01042    svr4_current_sos_via_xfer_libraries.  */
01043 
01044 struct svr4_library_list
01045 {
01046   struct so_list *head, **tailp;
01047 
01048   /* Inferior address of struct link_map used for the main executable.  It is
01049      NULL if not known.  */
01050   CORE_ADDR main_lm;
01051 };
01052 
01053 /* Implementation for target_so_ops.free_so.  */
01054 
01055 static void
01056 svr4_free_so (struct so_list *so)
01057 {
01058   xfree (so->lm_info);
01059 }
01060 
01061 /* Implement target_so_ops.clear_so.  */
01062 
01063 static void
01064 svr4_clear_so (struct so_list *so)
01065 {
01066   if (so->lm_info != NULL)
01067     so->lm_info->l_addr_p = 0;
01068 }
01069 
01070 /* Free so_list built so far (called via cleanup).  */
01071 
01072 static void
01073 svr4_free_library_list (void *p_list)
01074 {
01075   struct so_list *list = *(struct so_list **) p_list;
01076 
01077   while (list != NULL)
01078     {
01079       struct so_list *next = list->next;
01080 
01081       free_so (list);
01082       list = next;
01083     }
01084 }
01085 
01086 /* Copy library list.  */
01087 
01088 static struct so_list *
01089 svr4_copy_library_list (struct so_list *src)
01090 {
01091   struct so_list *dst = NULL;
01092   struct so_list **link = &dst;
01093 
01094   while (src != NULL)
01095     {
01096       struct so_list *new;
01097 
01098       new = xmalloc (sizeof (struct so_list));
01099       memcpy (new, src, sizeof (struct so_list));
01100 
01101       new->lm_info = xmalloc (sizeof (struct lm_info));
01102       memcpy (new->lm_info, src->lm_info, sizeof (struct lm_info));
01103 
01104       new->next = NULL;
01105       *link = new;
01106       link = &new->next;
01107 
01108       src = src->next;
01109     }
01110 
01111   return dst;
01112 }
01113 
01114 #ifdef HAVE_LIBEXPAT
01115 
01116 #include "xml-support.h"
01117 
01118 /* Handle the start of a <library> element.  Note: new elements are added
01119    at the tail of the list, keeping the list in order.  */
01120 
01121 static void
01122 library_list_start_library (struct gdb_xml_parser *parser,
01123                             const struct gdb_xml_element *element,
01124                             void *user_data, VEC(gdb_xml_value_s) *attributes)
01125 {
01126   struct svr4_library_list *list = user_data;
01127   const char *name = xml_find_attribute (attributes, "name")->value;
01128   ULONGEST *lmp = xml_find_attribute (attributes, "lm")->value;
01129   ULONGEST *l_addrp = xml_find_attribute (attributes, "l_addr")->value;
01130   ULONGEST *l_ldp = xml_find_attribute (attributes, "l_ld")->value;
01131   struct so_list *new_elem;
01132 
01133   new_elem = XZALLOC (struct so_list);
01134   new_elem->lm_info = XZALLOC (struct lm_info);
01135   new_elem->lm_info->lm_addr = *lmp;
01136   new_elem->lm_info->l_addr_inferior = *l_addrp;
01137   new_elem->lm_info->l_ld = *l_ldp;
01138 
01139   strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
01140   new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
01141   strcpy (new_elem->so_original_name, new_elem->so_name);
01142 
01143   *list->tailp = new_elem;
01144   list->tailp = &new_elem->next;
01145 }
01146 
01147 /* Handle the start of a <library-list-svr4> element.  */
01148 
01149 static void
01150 svr4_library_list_start_list (struct gdb_xml_parser *parser,
01151                               const struct gdb_xml_element *element,
01152                               void *user_data, VEC(gdb_xml_value_s) *attributes)
01153 {
01154   struct svr4_library_list *list = user_data;
01155   const char *version = xml_find_attribute (attributes, "version")->value;
01156   struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
01157 
01158   if (strcmp (version, "1.0") != 0)
01159     gdb_xml_error (parser,
01160                    _("SVR4 Library list has unsupported version \"%s\""),
01161                    version);
01162 
01163   if (main_lm)
01164     list->main_lm = *(ULONGEST *) main_lm->value;
01165 }
01166 
01167 /* The allowed elements and attributes for an XML library list.
01168    The root element is a <library-list>.  */
01169 
01170 static const struct gdb_xml_attribute svr4_library_attributes[] =
01171 {
01172   { "name", GDB_XML_AF_NONE, NULL, NULL },
01173   { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
01174   { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
01175   { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
01176   { NULL, GDB_XML_AF_NONE, NULL, NULL }
01177 };
01178 
01179 static const struct gdb_xml_element svr4_library_list_children[] =
01180 {
01181   {
01182     "library", svr4_library_attributes, NULL,
01183     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
01184     library_list_start_library, NULL
01185   },
01186   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
01187 };
01188 
01189 static const struct gdb_xml_attribute svr4_library_list_attributes[] =
01190 {
01191   { "version", GDB_XML_AF_NONE, NULL, NULL },
01192   { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
01193   { NULL, GDB_XML_AF_NONE, NULL, NULL }
01194 };
01195 
01196 static const struct gdb_xml_element svr4_library_list_elements[] =
01197 {
01198   { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
01199     GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
01200   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
01201 };
01202 
01203 /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN.  Return 1 if
01204 
01205    Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
01206    case.  Return 1 if *SO_LIST_RETURN contains the library list, it may be
01207    empty, caller is responsible for freeing all its entries.  */
01208 
01209 static int
01210 svr4_parse_libraries (const char *document, struct svr4_library_list *list)
01211 {
01212   struct cleanup *back_to = make_cleanup (svr4_free_library_list,
01213                                           &list->head);
01214 
01215   memset (list, 0, sizeof (*list));
01216   list->tailp = &list->head;
01217   if (gdb_xml_parse_quick (_("target library list"), "library-list.dtd",
01218                            svr4_library_list_elements, document, list) == 0)
01219     {
01220       /* Parsed successfully, keep the result.  */
01221       discard_cleanups (back_to);
01222       return 1;
01223     }
01224 
01225   do_cleanups (back_to);
01226   return 0;
01227 }
01228 
01229 /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
01230 
01231    Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
01232    case.  Return 1 if *SO_LIST_RETURN contains the library list, it may be
01233    empty, caller is responsible for freeing all its entries.
01234 
01235    Note that ANNEX must be NULL if the remote does not explicitly allow
01236    qXfer:libraries-svr4:read packets with non-empty annexes.  Support for
01237    this can be checked using target_augmented_libraries_svr4_read ().  */
01238 
01239 static int
01240 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
01241                                      const char *annex)
01242 {
01243   char *svr4_library_document;
01244   int result;
01245   struct cleanup *back_to;
01246 
01247   gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
01248 
01249   /* Fetch the list of shared libraries.  */
01250   svr4_library_document = target_read_stralloc (&current_target,
01251                                                 TARGET_OBJECT_LIBRARIES_SVR4,
01252                                                 annex);
01253   if (svr4_library_document == NULL)
01254     return 0;
01255 
01256   back_to = make_cleanup (xfree, svr4_library_document);
01257   result = svr4_parse_libraries (svr4_library_document, list);
01258   do_cleanups (back_to);
01259 
01260   return result;
01261 }
01262 
01263 #else
01264 
01265 static int
01266 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
01267                                      const char *annex)
01268 {
01269   return 0;
01270 }
01271 
01272 #endif
01273 
01274 /* If no shared library information is available from the dynamic
01275    linker, build a fallback list from other sources.  */
01276 
01277 static struct so_list *
01278 svr4_default_sos (void)
01279 {
01280   struct svr4_info *info = get_svr4_info ();
01281   struct so_list *new;
01282 
01283   if (!info->debug_loader_offset_p)
01284     return NULL;
01285 
01286   new = XZALLOC (struct so_list);
01287 
01288   new->lm_info = xzalloc (sizeof (struct lm_info));
01289 
01290   /* Nothing will ever check the other fields if we set l_addr_p.  */
01291   new->lm_info->l_addr = info->debug_loader_offset;
01292   new->lm_info->l_addr_p = 1;
01293 
01294   strncpy (new->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
01295   new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
01296   strcpy (new->so_original_name, new->so_name);
01297 
01298   return new;
01299 }
01300 
01301 /* Read the whole inferior libraries chain starting at address LM.
01302    Expect the first entry in the chain's previous entry to be PREV_LM.
01303    Add the entries to the tail referenced by LINK_PTR_PTR.  Ignore the
01304    first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
01305    to it.  Returns nonzero upon success.  If zero is returned the
01306    entries stored to LINK_PTR_PTR are still valid although they may
01307    represent only part of the inferior library list.  */
01308 
01309 static int
01310 svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
01311                    struct so_list ***link_ptr_ptr, int ignore_first)
01312 {
01313   struct so_list *first = NULL;
01314   CORE_ADDR next_lm;
01315 
01316   for (; lm != 0; prev_lm = lm, lm = next_lm)
01317     {
01318       struct so_list *new;
01319       struct cleanup *old_chain;
01320       int errcode;
01321       char *buffer;
01322 
01323       new = XZALLOC (struct so_list);
01324       old_chain = make_cleanup_free_so (new);
01325 
01326       new->lm_info = lm_info_read (lm);
01327       if (new->lm_info == NULL)
01328         {
01329           do_cleanups (old_chain);
01330           return 0;
01331         }
01332 
01333       next_lm = new->lm_info->l_next;
01334 
01335       if (new->lm_info->l_prev != prev_lm)
01336         {
01337           warning (_("Corrupted shared library list: %s != %s"),
01338                    paddress (target_gdbarch (), prev_lm),
01339                    paddress (target_gdbarch (), new->lm_info->l_prev));
01340           do_cleanups (old_chain);
01341           return 0;
01342         }
01343 
01344       /* For SVR4 versions, the first entry in the link map is for the
01345          inferior executable, so we must ignore it.  For some versions of
01346          SVR4, it has no name.  For others (Solaris 2.3 for example), it
01347          does have a name, so we can no longer use a missing name to
01348          decide when to ignore it.  */
01349       if (ignore_first && new->lm_info->l_prev == 0)
01350         {
01351           struct svr4_info *info = get_svr4_info ();
01352 
01353           first = new;
01354           info->main_lm_addr = new->lm_info->lm_addr;
01355           do_cleanups (old_chain);
01356           continue;
01357         }
01358 
01359       /* Extract this shared object's name.  */
01360       target_read_string (new->lm_info->l_name, &buffer,
01361                           SO_NAME_MAX_PATH_SIZE - 1, &errcode);
01362       if (errcode != 0)
01363         {
01364           /* If this entry's l_name address matches that of the
01365              inferior executable, then this is not a normal shared
01366              object, but (most likely) a vDSO.  In this case, silently
01367              skip it; otherwise emit a warning. */
01368           if (first == NULL
01369               || new->lm_info->l_name != first->lm_info->l_name)
01370             warning (_("Can't read pathname for load map: %s."),
01371                      safe_strerror (errcode));
01372           do_cleanups (old_chain);
01373           continue;
01374         }
01375 
01376       strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
01377       new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
01378       strcpy (new->so_original_name, new->so_name);
01379       xfree (buffer);
01380 
01381       /* If this entry has no name, or its name matches the name
01382          for the main executable, don't include it in the list.  */
01383       if (! new->so_name[0] || match_main (new->so_name))
01384         {
01385           do_cleanups (old_chain);
01386           continue;
01387         }
01388 
01389       discard_cleanups (old_chain);
01390       new->next = 0;
01391       **link_ptr_ptr = new;
01392       *link_ptr_ptr = &new->next;
01393     }
01394 
01395   return 1;
01396 }
01397 
01398 /* Read the full list of currently loaded shared objects directly
01399    from the inferior, without referring to any libraries read and
01400    stored by the probes interface.  Handle special cases relating
01401    to the first elements of the list.  */
01402 
01403 static struct so_list *
01404 svr4_current_sos_direct (struct svr4_info *info)
01405 {
01406   CORE_ADDR lm;
01407   struct so_list *head = NULL;
01408   struct so_list **link_ptr = &head;
01409   struct cleanup *back_to;
01410   int ignore_first;
01411   struct svr4_library_list library_list;
01412 
01413   /* Fall back to manual examination of the target if the packet is not
01414      supported or gdbserver failed to find DT_DEBUG.  gdb.server/solib-list.exp
01415      tests a case where gdbserver cannot find the shared libraries list while
01416      GDB itself is able to find it via SYMFILE_OBJFILE.
01417 
01418      Unfortunately statically linked inferiors will also fall back through this
01419      suboptimal code path.  */
01420 
01421   info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
01422                                                           NULL);
01423   if (info->using_xfer)
01424     {
01425       if (library_list.main_lm)
01426         info->main_lm_addr = library_list.main_lm;
01427 
01428       return library_list.head ? library_list.head : svr4_default_sos ();
01429     }
01430 
01431   /* Always locate the debug struct, in case it has moved.  */
01432   info->debug_base = 0;
01433   locate_base (info);
01434 
01435   /* If we can't find the dynamic linker's base structure, this
01436      must not be a dynamically linked executable.  Hmm.  */
01437   if (! info->debug_base)
01438     return svr4_default_sos ();
01439 
01440   /* Assume that everything is a library if the dynamic loader was loaded
01441      late by a static executable.  */
01442   if (exec_bfd && bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL)
01443     ignore_first = 0;
01444   else
01445     ignore_first = 1;
01446 
01447   back_to = make_cleanup (svr4_free_library_list, &head);
01448 
01449   /* Walk the inferior's link map list, and build our list of
01450      `struct so_list' nodes.  */
01451   lm = solib_svr4_r_map (info);
01452   if (lm)
01453     svr4_read_so_list (lm, 0, &link_ptr, ignore_first);
01454 
01455   /* On Solaris, the dynamic linker is not in the normal list of
01456      shared objects, so make sure we pick it up too.  Having
01457      symbol information for the dynamic linker is quite crucial
01458      for skipping dynamic linker resolver code.  */
01459   lm = solib_svr4_r_ldsomap (info);
01460   if (lm)
01461     svr4_read_so_list (lm, 0, &link_ptr, 0);
01462 
01463   discard_cleanups (back_to);
01464 
01465   if (head == NULL)
01466     return svr4_default_sos ();
01467 
01468   return head;
01469 }
01470 
01471 /* Implement the "current_sos" target_so_ops method.  */
01472 
01473 static struct so_list *
01474 svr4_current_sos (void)
01475 {
01476   struct svr4_info *info = get_svr4_info ();
01477 
01478   /* If the solib list has been read and stored by the probes
01479      interface then we return a copy of the stored list.  */
01480   if (info->solib_list != NULL)
01481     return svr4_copy_library_list (info->solib_list);
01482 
01483   /* Otherwise obtain the solib list directly from the inferior.  */
01484   return svr4_current_sos_direct (info);
01485 }
01486 
01487 /* Get the address of the link_map for a given OBJFILE.  */
01488 
01489 CORE_ADDR
01490 svr4_fetch_objfile_link_map (struct objfile *objfile)
01491 {
01492   struct so_list *so;
01493   struct svr4_info *info = get_svr4_info ();
01494 
01495   /* Cause svr4_current_sos() to be run if it hasn't been already.  */
01496   if (info->main_lm_addr == 0)
01497     solib_add (NULL, 0, &current_target, auto_solib_add);
01498 
01499   /* svr4_current_sos() will set main_lm_addr for the main executable.  */
01500   if (objfile == symfile_objfile)
01501     return info->main_lm_addr;
01502 
01503   /* The other link map addresses may be found by examining the list
01504      of shared libraries.  */
01505   for (so = master_so_list (); so; so = so->next)
01506     if (so->objfile == objfile)
01507       return so->lm_info->lm_addr;
01508 
01509   /* Not found!  */
01510   return 0;
01511 }
01512 
01513 /* On some systems, the only way to recognize the link map entry for
01514    the main executable file is by looking at its name.  Return
01515    non-zero iff SONAME matches one of the known main executable names.  */
01516 
01517 static int
01518 match_main (const char *soname)
01519 {
01520   const char * const *mainp;
01521 
01522   for (mainp = main_name_list; *mainp != NULL; mainp++)
01523     {
01524       if (strcmp (soname, *mainp) == 0)
01525         return (1);
01526     }
01527 
01528   return (0);
01529 }
01530 
01531 /* Return 1 if PC lies in the dynamic symbol resolution code of the
01532    SVR4 run time loader.  */
01533 
01534 int
01535 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
01536 {
01537   struct svr4_info *info = get_svr4_info ();
01538 
01539   return ((pc >= info->interp_text_sect_low
01540            && pc < info->interp_text_sect_high)
01541           || (pc >= info->interp_plt_sect_low
01542               && pc < info->interp_plt_sect_high)
01543           || in_plt_section (pc)
01544           || in_gnu_ifunc_stub (pc));
01545 }
01546 
01547 /* Given an executable's ABFD and target, compute the entry-point
01548    address.  */
01549 
01550 static CORE_ADDR
01551 exec_entry_point (struct bfd *abfd, struct target_ops *targ)
01552 {
01553   CORE_ADDR addr;
01554 
01555   /* KevinB wrote ... for most targets, the address returned by
01556      bfd_get_start_address() is the entry point for the start
01557      function.  But, for some targets, bfd_get_start_address() returns
01558      the address of a function descriptor from which the entry point
01559      address may be extracted.  This address is extracted by
01560      gdbarch_convert_from_func_ptr_addr().  The method
01561      gdbarch_convert_from_func_ptr_addr() is the merely the identify
01562      function for targets which don't use function descriptors.  */
01563   addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
01564                                              bfd_get_start_address (abfd),
01565                                              targ);
01566   return gdbarch_addr_bits_remove (target_gdbarch (), addr);
01567 }
01568 
01569 /* A probe and its associated action.  */
01570 
01571 struct probe_and_action
01572 {
01573   /* The probe.  */
01574   struct probe *probe;
01575 
01576   /* The action.  */
01577   enum probe_action action;
01578 };
01579 
01580 /* Returns a hash code for the probe_and_action referenced by p.  */
01581 
01582 static hashval_t
01583 hash_probe_and_action (const void *p)
01584 {
01585   const struct probe_and_action *pa = p;
01586 
01587   return (hashval_t) pa->probe->address;
01588 }
01589 
01590 /* Returns non-zero if the probe_and_actions referenced by p1 and p2
01591    are equal.  */
01592 
01593 static int
01594 equal_probe_and_action (const void *p1, const void *p2)
01595 {
01596   const struct probe_and_action *pa1 = p1;
01597   const struct probe_and_action *pa2 = p2;
01598 
01599   return pa1->probe->address == pa2->probe->address;
01600 }
01601 
01602 /* Register a solib event probe and its associated action in the
01603    probes table.  */
01604 
01605 static void
01606 register_solib_event_probe (struct probe *probe, enum probe_action action)
01607 {
01608   struct svr4_info *info = get_svr4_info ();
01609   struct probe_and_action lookup, *pa;
01610   void **slot;
01611 
01612   /* Create the probes table, if necessary.  */
01613   if (info->probes_table == NULL)
01614     info->probes_table = htab_create_alloc (1, hash_probe_and_action,
01615                                             equal_probe_and_action,
01616                                             xfree, xcalloc, xfree);
01617 
01618   lookup.probe = probe;
01619   slot = htab_find_slot (info->probes_table, &lookup, INSERT);
01620   gdb_assert (*slot == HTAB_EMPTY_ENTRY);
01621 
01622   pa = XCNEW (struct probe_and_action);
01623   pa->probe = probe;
01624   pa->action = action;
01625 
01626   *slot = pa;
01627 }
01628 
01629 /* Get the solib event probe at the specified location, and the
01630    action associated with it.  Returns NULL if no solib event probe
01631    was found.  */
01632 
01633 static struct probe_and_action *
01634 solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
01635 {
01636   struct probe lookup_probe;
01637   struct probe_and_action lookup;
01638   void **slot;
01639 
01640   lookup_probe.address = address;
01641   lookup.probe = &lookup_probe;
01642   slot = htab_find_slot (info->probes_table, &lookup, NO_INSERT);
01643 
01644   if (slot == NULL)
01645     return NULL;
01646 
01647   return (struct probe_and_action *) *slot;
01648 }
01649 
01650 /* Decide what action to take when the specified solib event probe is
01651    hit.  */
01652 
01653 static enum probe_action
01654 solib_event_probe_action (struct probe_and_action *pa)
01655 {
01656   enum probe_action action;
01657   unsigned probe_argc;
01658 
01659   action = pa->action;
01660   if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
01661     return action;
01662 
01663   gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
01664 
01665   /* Check that an appropriate number of arguments has been supplied.
01666      We expect:
01667        arg0: Lmid_t lmid (mandatory)
01668        arg1: struct r_debug *debug_base (mandatory)
01669        arg2: struct link_map *new (optional, for incremental updates)  */
01670   probe_argc = get_probe_argument_count (pa->probe);
01671   if (probe_argc == 2)
01672     action = FULL_RELOAD;
01673   else if (probe_argc < 2)
01674     action = PROBES_INTERFACE_FAILED;
01675 
01676   return action;
01677 }
01678 
01679 /* Populate the shared object list by reading the entire list of
01680    shared objects from the inferior.  Handle special cases relating
01681    to the first elements of the list.  Returns nonzero on success.  */
01682 
01683 static int
01684 solist_update_full (struct svr4_info *info)
01685 {
01686   free_solib_list (info);
01687   info->solib_list = svr4_current_sos_direct (info);
01688 
01689   return 1;
01690 }
01691 
01692 /* Update the shared object list starting from the link-map entry
01693    passed by the linker in the probe's third argument.  Returns
01694    nonzero if the list was successfully updated, or zero to indicate
01695    failure.  */
01696 
01697 static int
01698 solist_update_incremental (struct svr4_info *info, CORE_ADDR lm)
01699 {
01700   struct so_list *tail;
01701   CORE_ADDR prev_lm;
01702 
01703   /* svr4_current_sos_direct contains logic to handle a number of
01704      special cases relating to the first elements of the list.  To
01705      avoid duplicating this logic we defer to solist_update_full
01706      if the list is empty.  */
01707   if (info->solib_list == NULL)
01708     return 0;
01709 
01710   /* Fall back to a full update if we are using a remote target
01711      that does not support incremental transfers.  */
01712   if (info->using_xfer && !target_augmented_libraries_svr4_read ())
01713     return 0;
01714 
01715   /* Walk to the end of the list.  */
01716   for (tail = info->solib_list; tail->next != NULL; tail = tail->next)
01717     /* Nothing.  */;
01718   prev_lm = tail->lm_info->lm_addr;
01719 
01720   /* Read the new objects.  */
01721   if (info->using_xfer)
01722     {
01723       struct svr4_library_list library_list;
01724       char annex[64];
01725 
01726       xsnprintf (annex, sizeof (annex), "start=%s;prev=%s",
01727                  phex_nz (lm, sizeof (lm)),
01728                  phex_nz (prev_lm, sizeof (prev_lm)));
01729       if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
01730         return 0;
01731 
01732       tail->next = library_list.head;
01733     }
01734   else
01735     {
01736       struct so_list **link = &tail->next;
01737 
01738       /* IGNORE_FIRST may safely be set to zero here because the
01739          above check and deferral to solist_update_full ensures
01740          that this call to svr4_read_so_list will never see the
01741          first element.  */
01742       if (!svr4_read_so_list (lm, prev_lm, &link, 0))
01743         return 0;
01744     }
01745 
01746   return 1;
01747 }
01748 
01749 /* Disable the probes-based linker interface and revert to the
01750    original interface.  We don't reset the breakpoints as the
01751    ones set up for the probes-based interface are adequate.  */
01752 
01753 static void
01754 disable_probes_interface_cleanup (void *arg)
01755 {
01756   struct svr4_info *info = get_svr4_info ();
01757 
01758   warning (_("Probes-based dynamic linker interface failed.\n"
01759              "Reverting to original interface.\n"));
01760 
01761   free_probes_table (info);
01762   free_solib_list (info);
01763 }
01764 
01765 /* Update the solib list as appropriate when using the
01766    probes-based linker interface.  Do nothing if using the
01767    standard interface.  */
01768 
01769 static void
01770 svr4_handle_solib_event (void)
01771 {
01772   struct svr4_info *info = get_svr4_info ();
01773   struct probe_and_action *pa;
01774   enum probe_action action;
01775   struct cleanup *old_chain, *usm_chain;
01776   struct value *val;
01777   CORE_ADDR pc, debug_base, lm = 0;
01778   int is_initial_ns;
01779 
01780   /* Do nothing if not using the probes interface.  */
01781   if (info->probes_table == NULL)
01782     return;
01783 
01784   /* If anything goes wrong we revert to the original linker
01785      interface.  */
01786   old_chain = make_cleanup (disable_probes_interface_cleanup, NULL);
01787 
01788   pc = regcache_read_pc (get_current_regcache ());
01789   pa = solib_event_probe_at (info, pc);
01790   if (pa == NULL)
01791     {
01792       do_cleanups (old_chain);
01793       return;
01794     }
01795 
01796   action = solib_event_probe_action (pa);
01797   if (action == PROBES_INTERFACE_FAILED)
01798     {
01799       do_cleanups (old_chain);
01800       return;
01801     }
01802 
01803   if (action == DO_NOTHING)
01804     {
01805       discard_cleanups (old_chain);
01806       return;
01807     }
01808 
01809   /* evaluate_probe_argument looks up symbols in the dynamic linker
01810      using find_pc_section.  find_pc_section is accelerated by a cache
01811      called the section map.  The section map is invalidated every
01812      time a shared library is loaded or unloaded, and if the inferior
01813      is generating a lot of shared library events then the section map
01814      will be updated every time svr4_handle_solib_event is called.
01815      We called find_pc_section in svr4_create_solib_event_breakpoints,
01816      so we can guarantee that the dynamic linker's sections are in the
01817      section map.  We can therefore inhibit section map updates across
01818      these calls to evaluate_probe_argument and save a lot of time.  */
01819   inhibit_section_map_updates (current_program_space);
01820   usm_chain = make_cleanup (resume_section_map_updates_cleanup,
01821                             current_program_space);
01822 
01823   val = evaluate_probe_argument (pa->probe, 1);
01824   if (val == NULL)
01825     {
01826       do_cleanups (old_chain);
01827       return;
01828     }
01829 
01830   debug_base = value_as_address (val);
01831   if (debug_base == 0)
01832     {
01833       do_cleanups (old_chain);
01834       return;
01835     }
01836 
01837   /* Always locate the debug struct, in case it moved.  */
01838   info->debug_base = 0;
01839   if (locate_base (info) == 0)
01840     {
01841       do_cleanups (old_chain);
01842       return;
01843     }
01844 
01845   /* GDB does not currently support libraries loaded via dlmopen
01846      into namespaces other than the initial one.  We must ignore
01847      any namespace other than the initial namespace here until
01848      support for this is added to GDB.  */
01849   if (debug_base != info->debug_base)
01850     action = DO_NOTHING;
01851 
01852   if (action == UPDATE_OR_RELOAD)
01853     {
01854       val = evaluate_probe_argument (pa->probe, 2);
01855       if (val != NULL)
01856         lm = value_as_address (val);
01857 
01858       if (lm == 0)
01859         action = FULL_RELOAD;
01860     }
01861 
01862   /* Resume section map updates.  */
01863   do_cleanups (usm_chain);
01864 
01865   if (action == UPDATE_OR_RELOAD)
01866     {
01867       if (!solist_update_incremental (info, lm))
01868         action = FULL_RELOAD;
01869     }
01870 
01871   if (action == FULL_RELOAD)
01872     {
01873       if (!solist_update_full (info))
01874         {
01875           do_cleanups (old_chain);
01876           return;
01877         }
01878     }
01879 
01880   discard_cleanups (old_chain);
01881 }
01882 
01883 /* Helper function for svr4_update_solib_event_breakpoints.  */
01884 
01885 static int
01886 svr4_update_solib_event_breakpoint (struct breakpoint *b, void *arg)
01887 {
01888   struct bp_location *loc;
01889 
01890   if (b->type != bp_shlib_event)
01891     {
01892       /* Continue iterating.  */
01893       return 0;
01894     }
01895 
01896   for (loc = b->loc; loc != NULL; loc = loc->next)
01897     {
01898       struct svr4_info *info;
01899       struct probe_and_action *pa;
01900 
01901       info = program_space_data (loc->pspace, solib_svr4_pspace_data);
01902       if (info == NULL || info->probes_table == NULL)
01903         continue;
01904 
01905       pa = solib_event_probe_at (info, loc->address);
01906       if (pa == NULL)
01907         continue;
01908 
01909       if (pa->action == DO_NOTHING)
01910         {
01911           if (b->enable_state == bp_disabled && stop_on_solib_events)
01912             enable_breakpoint (b);
01913           else if (b->enable_state == bp_enabled && !stop_on_solib_events)
01914             disable_breakpoint (b);
01915         }
01916 
01917       break;
01918     }
01919 
01920   /* Continue iterating.  */
01921   return 0;
01922 }
01923 
01924 /* Enable or disable optional solib event breakpoints as appropriate.
01925    Called whenever stop_on_solib_events is changed.  */
01926 
01927 static void
01928 svr4_update_solib_event_breakpoints (void)
01929 {
01930   iterate_over_breakpoints (svr4_update_solib_event_breakpoint, NULL);
01931 }
01932 
01933 /* Create and register solib event breakpoints.  PROBES is an array
01934    of NUM_PROBES elements, each of which is vector of probes.  A
01935    solib event breakpoint will be created and registered for each
01936    probe.  */
01937 
01938 static void
01939 svr4_create_probe_breakpoints (struct gdbarch *gdbarch,
01940                                VEC (probe_p) **probes)
01941 {
01942   int i;
01943 
01944   for (i = 0; i < NUM_PROBES; i++)
01945     {
01946       enum probe_action action = probe_info[i].action;
01947       struct probe *probe;
01948       int ix;
01949 
01950       for (ix = 0;
01951            VEC_iterate (probe_p, probes[i], ix, probe);
01952            ++ix)
01953         {
01954           create_solib_event_breakpoint (gdbarch, probe->address);
01955           register_solib_event_probe (probe, action);
01956         }
01957     }
01958 
01959   svr4_update_solib_event_breakpoints ();
01960 }
01961 
01962 /* Both the SunOS and the SVR4 dynamic linkers call a marker function
01963    before and after mapping and unmapping shared libraries.  The sole
01964    purpose of this method is to allow debuggers to set a breakpoint so
01965    they can track these changes.
01966 
01967    Some versions of the glibc dynamic linker contain named probes
01968    to allow more fine grained stopping.  Given the address of the
01969    original marker function, this function attempts to find these
01970    probes, and if found, sets breakpoints on those instead.  If the
01971    probes aren't found, a single breakpoint is set on the original
01972    marker function.  */
01973 
01974 static void
01975 svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
01976                                      CORE_ADDR address)
01977 {
01978   struct obj_section *os;
01979 
01980   os = find_pc_section (address);
01981   if (os != NULL)
01982     {
01983       int with_prefix;
01984 
01985       for (with_prefix = 0; with_prefix <= 1; with_prefix++)
01986         {
01987           VEC (probe_p) *probes[NUM_PROBES];
01988           int all_probes_found = 1;
01989           int checked_can_use_probe_arguments = 0;
01990           int i;
01991 
01992           memset (probes, 0, sizeof (probes));
01993           for (i = 0; i < NUM_PROBES; i++)
01994             {
01995               const char *name = probe_info[i].name;
01996               struct probe *p;
01997               char buf[32];
01998 
01999               /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4
02000                  shipped with an early version of the probes code in
02001                  which the probes' names were prefixed with "rtld_"
02002                  and the "map_failed" probe did not exist.  The
02003                  locations of the probes are otherwise the same, so
02004                  we check for probes with prefixed names if probes
02005                  with unprefixed names are not present.  */
02006               if (with_prefix)
02007                 {
02008                   xsnprintf (buf, sizeof (buf), "rtld_%s", name);
02009                   name = buf;
02010                 }
02011 
02012               probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
02013 
02014               /* The "map_failed" probe did not exist in early
02015                  versions of the probes code in which the probes'
02016                  names were prefixed with "rtld_".  */
02017               if (strcmp (name, "rtld_map_failed") == 0)
02018                 continue;
02019 
02020               if (VEC_empty (probe_p, probes[i]))
02021                 {
02022                   all_probes_found = 0;
02023                   break;
02024                 }
02025 
02026               /* Ensure probe arguments can be evaluated.  */
02027               if (!checked_can_use_probe_arguments)
02028                 {
02029                   p = VEC_index (probe_p, probes[i], 0);
02030                   if (!can_evaluate_probe_arguments (p))
02031                     {
02032                       all_probes_found = 0;
02033                       break;
02034                     }
02035                   checked_can_use_probe_arguments = 1;
02036                 }
02037             }
02038 
02039           if (all_probes_found)
02040             svr4_create_probe_breakpoints (gdbarch, probes);
02041 
02042           for (i = 0; i < NUM_PROBES; i++)
02043             VEC_free (probe_p, probes[i]);
02044 
02045           if (all_probes_found)
02046             return;
02047         }
02048     }
02049 
02050   create_solib_event_breakpoint (gdbarch, address);
02051 }
02052 
02053 /* Helper function for gdb_bfd_lookup_symbol.  */
02054 
02055 static int
02056 cmp_name_and_sec_flags (asymbol *sym, void *data)
02057 {
02058   return (strcmp (sym->name, (const char *) data) == 0
02059           && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0);
02060 }
02061 /* Arrange for dynamic linker to hit breakpoint.
02062 
02063    Both the SunOS and the SVR4 dynamic linkers have, as part of their
02064    debugger interface, support for arranging for the inferior to hit
02065    a breakpoint after mapping in the shared libraries.  This function
02066    enables that breakpoint.
02067 
02068    For SunOS, there is a special flag location (in_debugger) which we
02069    set to 1.  When the dynamic linker sees this flag set, it will set
02070    a breakpoint at a location known only to itself, after saving the
02071    original contents of that place and the breakpoint address itself,
02072    in it's own internal structures.  When we resume the inferior, it
02073    will eventually take a SIGTRAP when it runs into the breakpoint.
02074    We handle this (in a different place) by restoring the contents of
02075    the breakpointed location (which is only known after it stops),
02076    chasing around to locate the shared libraries that have been
02077    loaded, then resuming.
02078 
02079    For SVR4, the debugger interface structure contains a member (r_brk)
02080    which is statically initialized at the time the shared library is
02081    built, to the offset of a function (_r_debug_state) which is guaran-
02082    teed to be called once before mapping in a library, and again when
02083    the mapping is complete.  At the time we are examining this member,
02084    it contains only the unrelocated offset of the function, so we have
02085    to do our own relocation.  Later, when the dynamic linker actually
02086    runs, it relocates r_brk to be the actual address of _r_debug_state().
02087 
02088    The debugger interface structure also contains an enumeration which
02089    is set to either RT_ADD or RT_DELETE prior to changing the mapping,
02090    depending upon whether or not the library is being mapped or unmapped,
02091    and then set to RT_CONSISTENT after the library is mapped/unmapped.  */
02092 
02093 static int
02094 enable_break (struct svr4_info *info, int from_tty)
02095 {
02096   struct minimal_symbol *msymbol;
02097   const char * const *bkpt_namep;
02098   asection *interp_sect;
02099   char *interp_name;
02100   CORE_ADDR sym_addr;
02101 
02102   info->interp_text_sect_low = info->interp_text_sect_high = 0;
02103   info->interp_plt_sect_low = info->interp_plt_sect_high = 0;
02104 
02105   /* If we already have a shared library list in the target, and
02106      r_debug contains r_brk, set the breakpoint there - this should
02107      mean r_brk has already been relocated.  Assume the dynamic linker
02108      is the object containing r_brk.  */
02109 
02110   solib_add (NULL, from_tty, &current_target, auto_solib_add);
02111   sym_addr = 0;
02112   if (info->debug_base && solib_svr4_r_map (info) != 0)
02113     sym_addr = solib_svr4_r_brk (info);
02114 
02115   if (sym_addr != 0)
02116     {
02117       struct obj_section *os;
02118 
02119       sym_addr = gdbarch_addr_bits_remove
02120         (target_gdbarch (), gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
02121                                                              sym_addr,
02122                                                              &current_target));
02123 
02124       /* On at least some versions of Solaris there's a dynamic relocation
02125          on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
02126          we get control before the dynamic linker has self-relocated.
02127          Check if SYM_ADDR is in a known section, if it is assume we can
02128          trust its value.  This is just a heuristic though, it could go away
02129          or be replaced if it's getting in the way.
02130 
02131          On ARM we need to know whether the ISA of rtld_db_dlactivity (or
02132          however it's spelled in your particular system) is ARM or Thumb.
02133          That knowledge is encoded in the address, if it's Thumb the low bit
02134          is 1.  However, we've stripped that info above and it's not clear
02135          what all the consequences are of passing a non-addr_bits_remove'd
02136          address to svr4_create_solib_event_breakpoints.  The call to
02137          find_pc_section verifies we know about the address and have some
02138          hope of computing the right kind of breakpoint to use (via
02139          symbol info).  It does mean that GDB needs to be pointed at a
02140          non-stripped version of the dynamic linker in order to obtain
02141          information it already knows about.  Sigh.  */
02142 
02143       os = find_pc_section (sym_addr);
02144       if (os != NULL)
02145         {
02146           /* Record the relocated start and end address of the dynamic linker
02147              text and plt section for svr4_in_dynsym_resolve_code.  */
02148           bfd *tmp_bfd;
02149           CORE_ADDR load_addr;
02150 
02151           tmp_bfd = os->objfile->obfd;
02152           load_addr = ANOFFSET (os->objfile->section_offsets,
02153                                 SECT_OFF_TEXT (os->objfile));
02154 
02155           interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
02156           if (interp_sect)
02157             {
02158               info->interp_text_sect_low =
02159                 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
02160               info->interp_text_sect_high =
02161                 info->interp_text_sect_low
02162                 + bfd_section_size (tmp_bfd, interp_sect);
02163             }
02164           interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
02165           if (interp_sect)
02166             {
02167               info->interp_plt_sect_low =
02168                 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
02169               info->interp_plt_sect_high =
02170                 info->interp_plt_sect_low
02171                 + bfd_section_size (tmp_bfd, interp_sect);
02172             }
02173 
02174           svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
02175           return 1;
02176         }
02177     }
02178 
02179   /* Find the program interpreter; if not found, warn the user and drop
02180      into the old breakpoint at symbol code.  */
02181   interp_name = find_program_interpreter ();
02182   if (interp_name)
02183     {
02184       CORE_ADDR load_addr = 0;
02185       int load_addr_found = 0;
02186       int loader_found_in_list = 0;
02187       struct so_list *so;
02188       bfd *tmp_bfd = NULL;
02189       struct target_ops *tmp_bfd_target;
02190       volatile struct gdb_exception ex;
02191 
02192       sym_addr = 0;
02193 
02194       /* Now we need to figure out where the dynamic linker was
02195          loaded so that we can load its symbols and place a breakpoint
02196          in the dynamic linker itself.
02197 
02198          This address is stored on the stack.  However, I've been unable
02199          to find any magic formula to find it for Solaris (appears to
02200          be trivial on GNU/Linux).  Therefore, we have to try an alternate
02201          mechanism to find the dynamic linker's base address.  */
02202 
02203       TRY_CATCH (ex, RETURN_MASK_ALL)
02204         {
02205           tmp_bfd = solib_bfd_open (interp_name);
02206         }
02207       if (tmp_bfd == NULL)
02208         goto bkpt_at_symbol;
02209 
02210       /* Now convert the TMP_BFD into a target.  That way target, as
02211          well as BFD operations can be used.  */
02212       tmp_bfd_target = target_bfd_reopen (tmp_bfd);
02213       /* target_bfd_reopen acquired its own reference, so we can
02214          release ours now.  */
02215       gdb_bfd_unref (tmp_bfd);
02216 
02217       /* On a running target, we can get the dynamic linker's base
02218          address from the shared library table.  */
02219       so = master_so_list ();
02220       while (so)
02221         {
02222           if (svr4_same_1 (interp_name, so->so_original_name))
02223             {
02224               load_addr_found = 1;
02225               loader_found_in_list = 1;
02226               load_addr = lm_addr_check (so, tmp_bfd);
02227               break;
02228             }
02229           so = so->next;
02230         }
02231 
02232       /* If we were not able to find the base address of the loader
02233          from our so_list, then try using the AT_BASE auxilliary entry.  */
02234       if (!load_addr_found)
02235         if (target_auxv_search (&current_target, AT_BASE, &load_addr) > 0)
02236           {
02237             int addr_bit = gdbarch_addr_bit (target_gdbarch ());
02238 
02239             /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
02240                that `+ load_addr' will overflow CORE_ADDR width not creating
02241                invalid addresses like 0x101234567 for 32bit inferiors on 64bit
02242                GDB.  */
02243 
02244             if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
02245               {
02246                 CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
02247                 CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd,
02248                                                               tmp_bfd_target);
02249 
02250                 gdb_assert (load_addr < space_size);
02251 
02252                 /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
02253                    64bit ld.so with 32bit executable, it should not happen.  */
02254 
02255                 if (tmp_entry_point < space_size
02256                     && tmp_entry_point + load_addr >= space_size)
02257                   load_addr -= space_size;
02258               }
02259 
02260             load_addr_found = 1;
02261           }
02262 
02263       /* Otherwise we find the dynamic linker's base address by examining
02264          the current pc (which should point at the entry point for the
02265          dynamic linker) and subtracting the offset of the entry point.
02266 
02267          This is more fragile than the previous approaches, but is a good
02268          fallback method because it has actually been working well in
02269          most cases.  */
02270       if (!load_addr_found)
02271         {
02272           struct regcache *regcache
02273             = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());
02274 
02275           load_addr = (regcache_read_pc (regcache)
02276                        - exec_entry_point (tmp_bfd, tmp_bfd_target));
02277         }
02278 
02279       if (!loader_found_in_list)
02280         {
02281           info->debug_loader_name = xstrdup (interp_name);
02282           info->debug_loader_offset_p = 1;
02283           info->debug_loader_offset = load_addr;
02284           solib_add (NULL, from_tty, &current_target, auto_solib_add);
02285         }
02286 
02287       /* Record the relocated start and end address of the dynamic linker
02288          text and plt section for svr4_in_dynsym_resolve_code.  */
02289       interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
02290       if (interp_sect)
02291         {
02292           info->interp_text_sect_low =
02293             bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
02294           info->interp_text_sect_high =
02295             info->interp_text_sect_low
02296             + bfd_section_size (tmp_bfd, interp_sect);
02297         }
02298       interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
02299       if (interp_sect)
02300         {
02301           info->interp_plt_sect_low =
02302             bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
02303           info->interp_plt_sect_high =
02304             info->interp_plt_sect_low
02305             + bfd_section_size (tmp_bfd, interp_sect);
02306         }
02307 
02308       /* Now try to set a breakpoint in the dynamic linker.  */
02309       for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
02310         {
02311           sym_addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name_and_sec_flags,
02312                                             (void *) *bkpt_namep);
02313           if (sym_addr != 0)
02314             break;
02315         }
02316 
02317       if (sym_addr != 0)
02318         /* Convert 'sym_addr' from a function pointer to an address.
02319            Because we pass tmp_bfd_target instead of the current
02320            target, this will always produce an unrelocated value.  */
02321         sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
02322                                                        sym_addr,
02323                                                        tmp_bfd_target);
02324 
02325       /* We're done with both the temporary bfd and target.  Closing
02326          the target closes the underlying bfd, because it holds the
02327          only remaining reference.  */
02328       target_close (tmp_bfd_target);
02329 
02330       if (sym_addr != 0)
02331         {
02332           svr4_create_solib_event_breakpoints (target_gdbarch (),
02333                                                load_addr + sym_addr);
02334           xfree (interp_name);
02335           return 1;
02336         }
02337 
02338       /* For whatever reason we couldn't set a breakpoint in the dynamic
02339          linker.  Warn and drop into the old code.  */
02340     bkpt_at_symbol:
02341       xfree (interp_name);
02342       warning (_("Unable to find dynamic linker breakpoint function.\n"
02343                "GDB will be unable to debug shared library initializers\n"
02344                "and track explicitly loaded dynamic code."));
02345     }
02346 
02347   /* Scan through the lists of symbols, trying to look up the symbol and
02348      set a breakpoint there.  Terminate loop when we/if we succeed.  */
02349 
02350   for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
02351     {
02352       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
02353       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
02354         {
02355           sym_addr = SYMBOL_VALUE_ADDRESS (msymbol);
02356           sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
02357                                                          sym_addr,
02358                                                          &current_target);
02359           svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
02360           return 1;
02361         }
02362     }
02363 
02364   if (interp_name != NULL && !current_inferior ()->attach_flag)
02365     {
02366       for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
02367         {
02368           msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
02369           if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
02370             {
02371               sym_addr = SYMBOL_VALUE_ADDRESS (msymbol);
02372               sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
02373                                                              sym_addr,
02374                                                              &current_target);
02375               svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
02376               return 1;
02377             }
02378         }
02379     }
02380   return 0;
02381 }
02382 
02383 /* Implement the "special_symbol_handling" target_so_ops method.  */
02384 
02385 static void
02386 svr4_special_symbol_handling (void)
02387 {
02388   /* Nothing to do.  */
02389 }
02390 
02391 /* Read the ELF program headers from ABFD.  Return the contents and
02392    set *PHDRS_SIZE to the size of the program headers.  */
02393 
02394 static gdb_byte *
02395 read_program_headers_from_bfd (bfd *abfd, int *phdrs_size)
02396 {
02397   Elf_Internal_Ehdr *ehdr;
02398   gdb_byte *buf;
02399 
02400   ehdr = elf_elfheader (abfd);
02401 
02402   *phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
02403   if (*phdrs_size == 0)
02404     return NULL;
02405 
02406   buf = xmalloc (*phdrs_size);
02407   if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
02408       || bfd_bread (buf, *phdrs_size, abfd) != *phdrs_size)
02409     {
02410       xfree (buf);
02411       return NULL;
02412     }
02413 
02414   return buf;
02415 }
02416 
02417 /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
02418    exec_bfd.  Otherwise return 0.
02419 
02420    We relocate all of the sections by the same amount.  This
02421    behavior is mandated by recent editions of the System V ABI.
02422    According to the System V Application Binary Interface,
02423    Edition 4.1, page 5-5:
02424 
02425      ...  Though the system chooses virtual addresses for
02426      individual processes, it maintains the segments' relative
02427      positions.  Because position-independent code uses relative
02428      addressesing between segments, the difference between
02429      virtual addresses in memory must match the difference
02430      between virtual addresses in the file.  The difference
02431      between the virtual address of any segment in memory and
02432      the corresponding virtual address in the file is thus a
02433      single constant value for any one executable or shared
02434      object in a given process.  This difference is the base
02435      address.  One use of the base address is to relocate the
02436      memory image of the program during dynamic linking.
02437 
02438    The same language also appears in Edition 4.0 of the System V
02439    ABI and is left unspecified in some of the earlier editions.
02440 
02441    Decide if the objfile needs to be relocated.  As indicated above, we will
02442    only be here when execution is stopped.  But during attachment PC can be at
02443    arbitrary address therefore regcache_read_pc can be misleading (contrary to
02444    the auxv AT_ENTRY value).  Moreover for executable with interpreter section
02445    regcache_read_pc would point to the interpreter and not the main executable.
02446 
02447    So, to summarize, relocations are necessary when the start address obtained
02448    from the executable is different from the address in auxv AT_ENTRY entry.
02449 
02450    [ The astute reader will note that we also test to make sure that
02451      the executable in question has the DYNAMIC flag set.  It is my
02452      opinion that this test is unnecessary (undesirable even).  It
02453      was added to avoid inadvertent relocation of an executable
02454      whose e_type member in the ELF header is not ET_DYN.  There may
02455      be a time in the future when it is desirable to do relocations
02456      on other types of files as well in which case this condition
02457      should either be removed or modified to accomodate the new file
02458      type.  - Kevin, Nov 2000. ]  */
02459 
02460 static int
02461 svr4_exec_displacement (CORE_ADDR *displacementp)
02462 {
02463   /* ENTRY_POINT is a possible function descriptor - before
02464      a call to gdbarch_convert_from_func_ptr_addr.  */
02465   CORE_ADDR entry_point, displacement;
02466 
02467   if (exec_bfd == NULL)
02468     return 0;
02469 
02470   /* Therefore for ELF it is ET_EXEC and not ET_DYN.  Both shared libraries
02471      being executed themselves and PIE (Position Independent Executable)
02472      executables are ET_DYN.  */
02473 
02474   if ((bfd_get_file_flags (exec_bfd) & DYNAMIC) == 0)
02475     return 0;
02476 
02477   if (target_auxv_search (&current_target, AT_ENTRY, &entry_point) <= 0)
02478     return 0;
02479 
02480   displacement = entry_point - bfd_get_start_address (exec_bfd);
02481 
02482   /* Verify the DISPLACEMENT candidate complies with the required page
02483      alignment.  It is cheaper than the program headers comparison below.  */
02484 
02485   if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
02486     {
02487       const struct elf_backend_data *elf = get_elf_backend_data (exec_bfd);
02488 
02489       /* p_align of PT_LOAD segments does not specify any alignment but
02490          only congruency of addresses:
02491            p_offset % p_align == p_vaddr % p_align
02492          Kernel is free to load the executable with lower alignment.  */
02493 
02494       if ((displacement & (elf->minpagesize - 1)) != 0)
02495         return 0;
02496     }
02497 
02498   /* Verify that the auxilliary vector describes the same file as exec_bfd, by
02499      comparing their program headers.  If the program headers in the auxilliary
02500      vector do not match the program headers in the executable, then we are
02501      looking at a different file than the one used by the kernel - for
02502      instance, "gdb program" connected to "gdbserver :PORT ld.so program".  */
02503 
02504   if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
02505     {
02506       /* Be optimistic and clear OK only if GDB was able to verify the headers
02507          really do not match.  */
02508       int phdrs_size, phdrs2_size, ok = 1;
02509       gdb_byte *buf, *buf2;
02510       int arch_size;
02511 
02512       buf = read_program_header (-1, &phdrs_size, &arch_size);
02513       buf2 = read_program_headers_from_bfd (exec_bfd, &phdrs2_size);
02514       if (buf != NULL && buf2 != NULL)
02515         {
02516           enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
02517 
02518           /* We are dealing with three different addresses.  EXEC_BFD
02519              represents current address in on-disk file.  target memory content
02520              may be different from EXEC_BFD as the file may have been prelinked
02521              to a different address after the executable has been loaded.
02522              Moreover the address of placement in target memory can be
02523              different from what the program headers in target memory say -
02524              this is the goal of PIE.
02525 
02526              Detected DISPLACEMENT covers both the offsets of PIE placement and
02527              possible new prelink performed after start of the program.  Here
02528              relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
02529              content offset for the verification purpose.  */
02530 
02531           if (phdrs_size != phdrs2_size
02532               || bfd_get_arch_size (exec_bfd) != arch_size)
02533             ok = 0;
02534           else if (arch_size == 32
02535                    && phdrs_size >= sizeof (Elf32_External_Phdr)
02536                    && phdrs_size % sizeof (Elf32_External_Phdr) == 0)
02537             {
02538               Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
02539               Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
02540               CORE_ADDR displacement = 0;
02541               int i;
02542 
02543               /* DISPLACEMENT could be found more easily by the difference of
02544                  ehdr2->e_entry.  But we haven't read the ehdr yet, and we
02545                  already have enough information to compute that displacement
02546                  with what we've read.  */
02547 
02548               for (i = 0; i < ehdr2->e_phnum; i++)
02549                 if (phdr2[i].p_type == PT_LOAD)
02550                   {
02551                     Elf32_External_Phdr *phdrp;
02552                     gdb_byte *buf_vaddr_p, *buf_paddr_p;
02553                     CORE_ADDR vaddr, paddr;
02554                     CORE_ADDR displacement_vaddr = 0;
02555                     CORE_ADDR displacement_paddr = 0;
02556 
02557                     phdrp = &((Elf32_External_Phdr *) buf)[i];
02558                     buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
02559                     buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
02560 
02561                     vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
02562                                                       byte_order);
02563                     displacement_vaddr = vaddr - phdr2[i].p_vaddr;
02564 
02565                     paddr = extract_unsigned_integer (buf_paddr_p, 4,
02566                                                       byte_order);
02567                     displacement_paddr = paddr - phdr2[i].p_paddr;
02568 
02569                     if (displacement_vaddr == displacement_paddr)
02570                       displacement = displacement_vaddr;
02571 
02572                     break;
02573                   }
02574 
02575               /* Now compare BUF and BUF2 with optional DISPLACEMENT.  */
02576 
02577               for (i = 0; i < phdrs_size / sizeof (Elf32_External_Phdr); i++)
02578                 {
02579                   Elf32_External_Phdr *phdrp;
02580                   Elf32_External_Phdr *phdr2p;
02581                   gdb_byte *buf_vaddr_p, *buf_paddr_p;
02582                   CORE_ADDR vaddr, paddr;
02583                   asection *plt2_asect;
02584 
02585                   phdrp = &((Elf32_External_Phdr *) buf)[i];
02586                   buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
02587                   buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
02588                   phdr2p = &((Elf32_External_Phdr *) buf2)[i];
02589 
02590                   /* PT_GNU_STACK is an exception by being never relocated by
02591                      prelink as its addresses are always zero.  */
02592 
02593                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
02594                     continue;
02595 
02596                   /* Check also other adjustment combinations - PR 11786.  */
02597 
02598                   vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
02599                                                     byte_order);
02600                   vaddr -= displacement;
02601                   store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);
02602 
02603                   paddr = extract_unsigned_integer (buf_paddr_p, 4,
02604                                                     byte_order);
02605                   paddr -= displacement;
02606                   store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);
02607 
02608                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
02609                     continue;
02610 
02611                   /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
02612                   plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
02613                   if (plt2_asect)
02614                     {
02615                       int content2;
02616                       gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
02617                       CORE_ADDR filesz;
02618 
02619                       content2 = (bfd_get_section_flags (exec_bfd, plt2_asect)
02620                                   & SEC_HAS_CONTENTS) != 0;
02621 
02622                       filesz = extract_unsigned_integer (buf_filesz_p, 4,
02623                                                          byte_order);
02624 
02625                       /* PLT2_ASECT is from on-disk file (exec_bfd) while
02626                          FILESZ is from the in-memory image.  */
02627                       if (content2)
02628                         filesz += bfd_get_section_size (plt2_asect);
02629                       else
02630                         filesz -= bfd_get_section_size (plt2_asect);
02631 
02632                       store_unsigned_integer (buf_filesz_p, 4, byte_order,
02633                                               filesz);
02634 
02635                       if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
02636                         continue;
02637                     }
02638 
02639                   ok = 0;
02640                   break;
02641                 }
02642             }
02643           else if (arch_size == 64
02644                    && phdrs_size >= sizeof (Elf64_External_Phdr)
02645                    && phdrs_size % sizeof (Elf64_External_Phdr) == 0)
02646             {
02647               Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
02648               Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
02649               CORE_ADDR displacement = 0;
02650               int i;
02651 
02652               /* DISPLACEMENT could be found more easily by the difference of
02653                  ehdr2->e_entry.  But we haven't read the ehdr yet, and we
02654                  already have enough information to compute that displacement
02655                  with what we've read.  */
02656 
02657               for (i = 0; i < ehdr2->e_phnum; i++)
02658                 if (phdr2[i].p_type == PT_LOAD)
02659                   {
02660                     Elf64_External_Phdr *phdrp;
02661                     gdb_byte *buf_vaddr_p, *buf_paddr_p;
02662                     CORE_ADDR vaddr, paddr;
02663                     CORE_ADDR displacement_vaddr = 0;
02664                     CORE_ADDR displacement_paddr = 0;
02665 
02666                     phdrp = &((Elf64_External_Phdr *) buf)[i];
02667                     buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
02668                     buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
02669 
02670                     vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
02671                                                       byte_order);
02672                     displacement_vaddr = vaddr - phdr2[i].p_vaddr;
02673 
02674                     paddr = extract_unsigned_integer (buf_paddr_p, 8,
02675                                                       byte_order);
02676                     displacement_paddr = paddr - phdr2[i].p_paddr;
02677 
02678                     if (displacement_vaddr == displacement_paddr)
02679                       displacement = displacement_vaddr;
02680 
02681                     break;
02682                   }
02683 
02684               /* Now compare BUF and BUF2 with optional DISPLACEMENT.  */
02685 
02686               for (i = 0; i < phdrs_size / sizeof (Elf64_External_Phdr); i++)
02687                 {
02688                   Elf64_External_Phdr *phdrp;
02689                   Elf64_External_Phdr *phdr2p;
02690                   gdb_byte *buf_vaddr_p, *buf_paddr_p;
02691                   CORE_ADDR vaddr, paddr;
02692                   asection *plt2_asect;
02693 
02694                   phdrp = &((Elf64_External_Phdr *) buf)[i];
02695                   buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
02696                   buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
02697                   phdr2p = &((Elf64_External_Phdr *) buf2)[i];
02698 
02699                   /* PT_GNU_STACK is an exception by being never relocated by
02700                      prelink as its addresses are always zero.  */
02701 
02702                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
02703                     continue;
02704 
02705                   /* Check also other adjustment combinations - PR 11786.  */
02706 
02707                   vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
02708                                                     byte_order);
02709                   vaddr -= displacement;
02710                   store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);
02711 
02712                   paddr = extract_unsigned_integer (buf_paddr_p, 8,
02713                                                     byte_order);
02714                   paddr -= displacement;
02715                   store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);
02716 
02717                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
02718                     continue;
02719 
02720                   /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
02721                   plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
02722                   if (plt2_asect)
02723                     {
02724                       int content2;
02725                       gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
02726                       CORE_ADDR filesz;
02727 
02728                       content2 = (bfd_get_section_flags (exec_bfd, plt2_asect)
02729                                   & SEC_HAS_CONTENTS) != 0;
02730 
02731                       filesz = extract_unsigned_integer (buf_filesz_p, 8,
02732                                                          byte_order);
02733 
02734                       /* PLT2_ASECT is from on-disk file (exec_bfd) while
02735                          FILESZ is from the in-memory image.  */
02736                       if (content2)
02737                         filesz += bfd_get_section_size (plt2_asect);
02738                       else
02739                         filesz -= bfd_get_section_size (plt2_asect);
02740 
02741                       store_unsigned_integer (buf_filesz_p, 8, byte_order,
02742                                               filesz);
02743 
02744                       if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
02745                         continue;
02746                     }
02747 
02748                   ok = 0;
02749                   break;
02750                 }
02751             }
02752           else
02753             ok = 0;
02754         }
02755 
02756       xfree (buf);
02757       xfree (buf2);
02758 
02759       if (!ok)
02760         return 0;
02761     }
02762 
02763   if (info_verbose)
02764     {
02765       /* It can be printed repeatedly as there is no easy way to check
02766          the executable symbols/file has been already relocated to
02767          displacement.  */
02768 
02769       printf_unfiltered (_("Using PIE (Position Independent Executable) "
02770                            "displacement %s for \"%s\".\n"),
02771                          paddress (target_gdbarch (), displacement),
02772                          bfd_get_filename (exec_bfd));
02773     }
02774 
02775   *displacementp = displacement;
02776   return 1;
02777 }
02778 
02779 /* Relocate the main executable.  This function should be called upon
02780    stopping the inferior process at the entry point to the program.
02781    The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
02782    different, the main executable is relocated by the proper amount.  */
02783 
02784 static void
02785 svr4_relocate_main_executable (void)
02786 {
02787   CORE_ADDR displacement;
02788 
02789   /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
02790      probably contains the offsets computed using the PIE displacement
02791      from the previous run, which of course are irrelevant for this run.
02792      So we need to determine the new PIE displacement and recompute the
02793      section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
02794      already contains pre-computed offsets.
02795 
02796      If we cannot compute the PIE displacement, either:
02797 
02798        - The executable is not PIE.
02799 
02800        - SYMFILE_OBJFILE does not match the executable started in the target.
02801          This can happen for main executable symbols loaded at the host while
02802          `ld.so --ld-args main-executable' is loaded in the target.
02803 
02804      Then we leave the section offsets untouched and use them as is for
02805      this run.  Either:
02806 
02807        - These section offsets were properly reset earlier, and thus
02808          already contain the correct values.  This can happen for instance
02809          when reconnecting via the remote protocol to a target that supports
02810          the `qOffsets' packet.
02811 
02812        - The section offsets were not reset earlier, and the best we can
02813          hope is that the old offsets are still applicable to the new run.  */
02814 
02815   if (! svr4_exec_displacement (&displacement))
02816     return;
02817 
02818   /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
02819      addresses.  */
02820 
02821   if (symfile_objfile)
02822     {
02823       struct section_offsets *new_offsets;
02824       int i;
02825 
02826       new_offsets = alloca (symfile_objfile->num_sections
02827                             * sizeof (*new_offsets));
02828 
02829       for (i = 0; i < symfile_objfile->num_sections; i++)
02830         new_offsets->offsets[i] = displacement;
02831 
02832       objfile_relocate (symfile_objfile, new_offsets);
02833     }
02834   else if (exec_bfd)
02835     {
02836       asection *asect;
02837 
02838       for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
02839         exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
02840                                   (bfd_section_vma (exec_bfd, asect)
02841                                    + displacement));
02842     }
02843 }
02844 
02845 /* Implement the "create_inferior_hook" target_solib_ops method.
02846 
02847    For SVR4 executables, this first instruction is either the first
02848    instruction in the dynamic linker (for dynamically linked
02849    executables) or the instruction at "start" for statically linked
02850    executables.  For dynamically linked executables, the system
02851    first exec's /lib/libc.so.N, which contains the dynamic linker,
02852    and starts it running.  The dynamic linker maps in any needed
02853    shared libraries, maps in the actual user executable, and then
02854    jumps to "start" in the user executable.
02855 
02856    We can arrange to cooperate with the dynamic linker to discover the
02857    names of shared libraries that are dynamically linked, and the base
02858    addresses to which they are linked.
02859 
02860    This function is responsible for discovering those names and
02861    addresses, and saving sufficient information about them to allow
02862    their symbols to be read at a later time.  */
02863 
02864 static void
02865 svr4_solib_create_inferior_hook (int from_tty)
02866 {
02867   struct svr4_info *info;
02868 
02869   info = get_svr4_info ();
02870 
02871   /* Clear the probes-based interface's state.  */
02872   free_probes_table (info);
02873   free_solib_list (info);
02874 
02875   /* Relocate the main executable if necessary.  */
02876   svr4_relocate_main_executable ();
02877 
02878   /* No point setting a breakpoint in the dynamic linker if we can't
02879      hit it (e.g., a core file, or a trace file).  */
02880   if (!target_has_execution)
02881     return;
02882 
02883   if (!svr4_have_link_map_offsets ())
02884     return;
02885 
02886   if (!enable_break (info, from_tty))
02887     return;
02888 }
02889 
02890 static void
02891 svr4_clear_solib (void)
02892 {
02893   struct svr4_info *info;
02894 
02895   info = get_svr4_info ();
02896   info->debug_base = 0;
02897   info->debug_loader_offset_p = 0;
02898   info->debug_loader_offset = 0;
02899   xfree (info->debug_loader_name);
02900   info->debug_loader_name = NULL;
02901 }
02902 
02903 /* Clear any bits of ADDR that wouldn't fit in a target-format
02904    data pointer.  "Data pointer" here refers to whatever sort of
02905    address the dynamic linker uses to manage its sections.  At the
02906    moment, we don't support shared libraries on any processors where
02907    code and data pointers are different sizes.
02908 
02909    This isn't really the right solution.  What we really need here is
02910    a way to do arithmetic on CORE_ADDR values that respects the
02911    natural pointer/address correspondence.  (For example, on the MIPS,
02912    converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
02913    sign-extend the value.  There, simply truncating the bits above
02914    gdbarch_ptr_bit, as we do below, is no good.)  This should probably
02915    be a new gdbarch method or something.  */
02916 static CORE_ADDR
02917 svr4_truncate_ptr (CORE_ADDR addr)
02918 {
02919   if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
02920     /* We don't need to truncate anything, and the bit twiddling below
02921        will fail due to overflow problems.  */
02922     return addr;
02923   else
02924     return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
02925 }
02926 
02927 
02928 static void
02929 svr4_relocate_section_addresses (struct so_list *so,
02930                                  struct target_section *sec)
02931 {
02932   bfd *abfd = sec->the_bfd_section->owner;
02933 
02934   sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
02935   sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
02936 }
02937 
02938 
02939 /* Architecture-specific operations.  */
02940 
02941 /* Per-architecture data key.  */
02942 static struct gdbarch_data *solib_svr4_data;
02943 
02944 struct solib_svr4_ops
02945 {
02946   /* Return a description of the layout of `struct link_map'.  */
02947   struct link_map_offsets *(*fetch_link_map_offsets)(void);
02948 };
02949 
02950 /* Return a default for the architecture-specific operations.  */
02951 
02952 static void *
02953 solib_svr4_init (struct obstack *obstack)
02954 {
02955   struct solib_svr4_ops *ops;
02956 
02957   ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops);
02958   ops->fetch_link_map_offsets = NULL;
02959   return ops;
02960 }
02961 
02962 /* Set the architecture-specific `struct link_map_offsets' fetcher for
02963    GDBARCH to FLMO.  Also, install SVR4 solib_ops into GDBARCH.  */
02964 
02965 void
02966 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
02967                                        struct link_map_offsets *(*flmo) (void))
02968 {
02969   struct solib_svr4_ops *ops = gdbarch_data (gdbarch, solib_svr4_data);
02970 
02971   ops->fetch_link_map_offsets = flmo;
02972 
02973   set_solib_ops (gdbarch, &svr4_so_ops);
02974 }
02975 
02976 /* Fetch a link_map_offsets structure using the architecture-specific
02977    `struct link_map_offsets' fetcher.  */
02978 
02979 static struct link_map_offsets *
02980 svr4_fetch_link_map_offsets (void)
02981 {
02982   struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch (), solib_svr4_data);
02983 
02984   gdb_assert (ops->fetch_link_map_offsets);
02985   return ops->fetch_link_map_offsets ();
02986 }
02987 
02988 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise.  */
02989 
02990 static int
02991 svr4_have_link_map_offsets (void)
02992 {
02993   struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch (), solib_svr4_data);
02994 
02995   return (ops->fetch_link_map_offsets != NULL);
02996 }
02997 
02998 
02999 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
03000    `struct r_debug' and a `struct link_map' that are binary compatible
03001    with the origional SVR4 implementation.  */
03002 
03003 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
03004    for an ILP32 SVR4 system.  */
03005 
03006 struct link_map_offsets *
03007 svr4_ilp32_fetch_link_map_offsets (void)
03008 {
03009   static struct link_map_offsets lmo;
03010   static struct link_map_offsets *lmp = NULL;
03011 
03012   if (lmp == NULL)
03013     {
03014       lmp = &lmo;
03015 
03016       lmo.r_version_offset = 0;
03017       lmo.r_version_size = 4;
03018       lmo.r_map_offset = 4;
03019       lmo.r_brk_offset = 8;
03020       lmo.r_ldsomap_offset = 20;
03021 
03022       /* Everything we need is in the first 20 bytes.  */
03023       lmo.link_map_size = 20;
03024       lmo.l_addr_offset = 0;
03025       lmo.l_name_offset = 4;
03026       lmo.l_ld_offset = 8;
03027       lmo.l_next_offset = 12;
03028       lmo.l_prev_offset = 16;
03029     }
03030 
03031   return lmp;
03032 }
03033 
03034 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
03035    for an LP64 SVR4 system.  */
03036 
03037 struct link_map_offsets *
03038 svr4_lp64_fetch_link_map_offsets (void)
03039 {
03040   static struct link_map_offsets lmo;
03041   static struct link_map_offsets *lmp = NULL;
03042 
03043   if (lmp == NULL)
03044     {
03045       lmp = &lmo;
03046 
03047       lmo.r_version_offset = 0;
03048       lmo.r_version_size = 4;
03049       lmo.r_map_offset = 8;
03050       lmo.r_brk_offset = 16;
03051       lmo.r_ldsomap_offset = 40;
03052 
03053       /* Everything we need is in the first 40 bytes.  */
03054       lmo.link_map_size = 40;
03055       lmo.l_addr_offset = 0;
03056       lmo.l_name_offset = 8;
03057       lmo.l_ld_offset = 16;
03058       lmo.l_next_offset = 24;
03059       lmo.l_prev_offset = 32;
03060     }
03061 
03062   return lmp;
03063 }
03064 
03065 
03066 struct target_so_ops svr4_so_ops;
03067 
03068 /* Lookup global symbol for ELF DSOs linked with -Bsymbolic.  Those DSOs have a
03069    different rule for symbol lookup.  The lookup begins here in the DSO, not in
03070    the main executable.  */
03071 
03072 static struct symbol *
03073 elf_lookup_lib_symbol (const struct objfile *objfile,
03074                        const char *name,
03075                        const domain_enum domain)
03076 {
03077   bfd *abfd;
03078 
03079   if (objfile == symfile_objfile)
03080     abfd = exec_bfd;
03081   else
03082     {
03083       /* OBJFILE should have been passed as the non-debug one.  */
03084       gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
03085 
03086       abfd = objfile->obfd;
03087     }
03088 
03089   if (abfd == NULL || scan_dyntag (DT_SYMBOLIC, abfd, NULL) != 1)
03090     return NULL;
03091 
03092   return lookup_global_symbol_from_objfile (objfile, name, domain);
03093 }
03094 
03095 extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */
03096 
03097 void
03098 _initialize_svr4_solib (void)
03099 {
03100   solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init);
03101   solib_svr4_pspace_data
03102     = register_program_space_data_with_cleanup (NULL, svr4_pspace_data_cleanup);
03103 
03104   svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
03105   svr4_so_ops.free_so = svr4_free_so;
03106   svr4_so_ops.clear_so = svr4_clear_so;
03107   svr4_so_ops.clear_solib = svr4_clear_solib;
03108   svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
03109   svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
03110   svr4_so_ops.current_sos = svr4_current_sos;
03111   svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
03112   svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
03113   svr4_so_ops.bfd_open = solib_bfd_open;
03114   svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol;
03115   svr4_so_ops.same = svr4_same;
03116   svr4_so_ops.keep_data_in_core = svr4_keep_data_in_core;
03117   svr4_so_ops.update_breakpoints = svr4_update_solib_event_breakpoints;
03118   svr4_so_ops.handle_event = svr4_handle_solib_event;
03119 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines