gdb/solib-svr4.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Handle SVR4 shared libraries for GDB, the GNU Debugger.

  2.    Copyright (C) 1990-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"

  15. #include "elf/external.h"
  16. #include "elf/common.h"
  17. #include "elf/mips.h"

  18. #include "symtab.h"
  19. #include "bfd.h"
  20. #include "symfile.h"
  21. #include "objfiles.h"
  22. #include "gdbcore.h"
  23. #include "target.h"
  24. #include "inferior.h"
  25. #include "infrun.h"
  26. #include "regcache.h"
  27. #include "gdbthread.h"
  28. #include "observer.h"

  29. #include "solist.h"
  30. #include "solib.h"
  31. #include "solib-svr4.h"

  32. #include "bfd-target.h"
  33. #include "elf-bfd.h"
  34. #include "exec.h"
  35. #include "auxv.h"
  36. #include "gdb_bfd.h"
  37. #include "probe.h"

  38. static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
  39. static int svr4_have_link_map_offsets (void);
  40. static void svr4_relocate_main_executable (void);
  41. static void svr4_free_library_list (void *p_list);

  42. /* Link map info to include in an allocated so_list entry.  */

  43. struct lm_info
  44.   {
  45.     /* Amount by which addresses in the binary should be relocated to
  46.        match the inferior.  The direct inferior value is L_ADDR_INFERIOR.
  47.        When prelinking is involved and the prelink base address changes,
  48.        we may need a different offset - the recomputed offset is in L_ADDR.
  49.        It is commonly the same value.  It is cached as we want to warn about
  50.        the difference and compute it only once.  L_ADDR is valid
  51.        iff L_ADDR_P.  */
  52.     CORE_ADDR l_addr, l_addr_inferior;
  53.     unsigned int l_addr_p : 1;

  54.     /* The target location of lm.  */
  55.     CORE_ADDR lm_addr;

  56.     /* Values read in from inferior's fields of the same name.  */
  57.     CORE_ADDR l_ld, l_next, l_prev, l_name;
  58.   };

  59. /* On SVR4 systems, a list of symbols in the dynamic linker where
  60.    GDB can try to place a breakpoint to monitor shared library
  61.    events.

  62.    If none of these symbols are found, or other errors occur, then
  63.    SVR4 systems will fall back to using a symbol as the "startup
  64.    mapping complete" breakpoint address.  */

  65. static const char * const solib_break_names[] =
  66. {
  67.   "r_debug_state",
  68.   "_r_debug_state",
  69.   "_dl_debug_state",
  70.   "rtld_db_dlactivity",
  71.   "__dl_rtld_db_dlactivity",
  72.   "_rtld_debug_state",

  73.   NULL
  74. };

  75. static const char * const bkpt_names[] =
  76. {
  77.   "_start",
  78.   "__start",
  79.   "main",
  80.   NULL
  81. };

  82. static const  char * const main_name_list[] =
  83. {
  84.   "main_$main",
  85.   NULL
  86. };

  87. /* What to do when a probe stop occurs.  */

  88. enum probe_action
  89. {
  90.   /* Something went seriously wrong.  Stop using probes and
  91.      revert to using the older interface.  */
  92.   PROBES_INTERFACE_FAILED,

  93.   /* No action is required.  The shared object list is still
  94.      valid.  */
  95.   DO_NOTHING,

  96.   /* The shared object list should be reloaded entirely.  */
  97.   FULL_RELOAD,

  98.   /* Attempt to incrementally update the shared object list. If
  99.      the update fails or is not possible, fall back to reloading
  100.      the list in full.  */
  101.   UPDATE_OR_RELOAD,
  102. };

  103. /* A probe's name and its associated action.  */

  104. struct probe_info
  105. {
  106.   /* The name of the probe.  */
  107.   const char *name;

  108.   /* What to do when a probe stop occurs.  */
  109.   enum probe_action action;
  110. };

  111. /* A list of named probes and their associated actions.  If all
  112.    probes are present in the dynamic linker then the probes-based
  113.    interface will be used.  */

  114. static const struct probe_info probe_info[] =
  115. {
  116.   { "init_start", DO_NOTHING },
  117.   { "init_complete", FULL_RELOAD },
  118.   { "map_start", DO_NOTHING },
  119.   { "map_failed", DO_NOTHING },
  120.   { "reloc_complete", UPDATE_OR_RELOAD },
  121.   { "unmap_start", DO_NOTHING },
  122.   { "unmap_complete", FULL_RELOAD },
  123. };

  124. #define NUM_PROBES ARRAY_SIZE (probe_info)

  125. /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
  126.    the same shared library.  */

  127. static int
  128. svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
  129. {
  130.   if (strcmp (gdb_so_name, inferior_so_name) == 0)
  131.     return 1;

  132.   /* On Solaris, when starting inferior we think that dynamic linker is
  133.      /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
  134.      contains /lib/ld.so.1.  Sometimes one file is a link to another, but
  135.      sometimes they have identical content, but are not linked to each
  136.      other.  We don't restrict this check for Solaris, but the chances
  137.      of running into this situation elsewhere are very low.  */
  138.   if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
  139.       && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
  140.     return 1;

  141.   /* Similarly, we observed the same issue with sparc64, but with
  142.      different locations.  */
  143.   if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
  144.       && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
  145.     return 1;

  146.   return 0;
  147. }

  148. static int
  149. svr4_same (struct so_list *gdb, struct so_list *inferior)
  150. {
  151.   return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
  152. }

  153. static struct lm_info *
  154. lm_info_read (CORE_ADDR lm_addr)
  155. {
  156.   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  157.   gdb_byte *lm;
  158.   struct lm_info *lm_info;
  159.   struct cleanup *back_to;

  160.   lm = xmalloc (lmo->link_map_size);
  161.   back_to = make_cleanup (xfree, lm);

  162.   if (target_read_memory (lm_addr, lm, lmo->link_map_size) != 0)
  163.     {
  164.       warning (_("Error reading shared library list entry at %s"),
  165.                paddress (target_gdbarch (), lm_addr)),
  166.       lm_info = NULL;
  167.     }
  168.   else
  169.     {
  170.       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;

  171.       lm_info = xzalloc (sizeof (*lm_info));
  172.       lm_info->lm_addr = lm_addr;

  173.       lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
  174.                                                         ptr_type);
  175.       lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
  176.       lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
  177.                                                ptr_type);
  178.       lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
  179.                                                ptr_type);
  180.       lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
  181.                                                ptr_type);
  182.     }

  183.   do_cleanups (back_to);

  184.   return lm_info;
  185. }

  186. static int
  187. has_lm_dynamic_from_link_map (void)
  188. {
  189.   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();

  190.   return lmo->l_ld_offset >= 0;
  191. }

  192. static CORE_ADDR
  193. lm_addr_check (const struct so_list *so, bfd *abfd)
  194. {
  195.   if (!so->lm_info->l_addr_p)
  196.     {
  197.       struct bfd_section *dyninfo_sect;
  198.       CORE_ADDR l_addr, l_dynaddr, dynaddr;

  199.       l_addr = so->lm_info->l_addr_inferior;

  200.       if (! abfd || ! has_lm_dynamic_from_link_map ())
  201.         goto set_addr;

  202.       l_dynaddr = so->lm_info->l_ld;

  203.       dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
  204.       if (dyninfo_sect == NULL)
  205.         goto set_addr;

  206.       dynaddr = bfd_section_vma (abfd, dyninfo_sect);

  207.       if (dynaddr + l_addr != l_dynaddr)
  208.         {
  209.           CORE_ADDR align = 0x1000;
  210.           CORE_ADDR minpagesize = align;

  211.           if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  212.             {
  213.               Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
  214.               Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
  215.               int i;

  216.               align = 1;

  217.               for (i = 0; i < ehdr->e_phnum; i++)
  218.                 if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
  219.                   align = phdr[i].p_align;

  220.               minpagesize = get_elf_backend_data (abfd)->minpagesize;
  221.             }

  222.           /* Turn it into a mask.  */
  223.           align--;

  224.           /* If the changes match the alignment requirements, we
  225.              assume we're using a core file that was generated by the
  226.              same binary, just prelinked with a different base offset.
  227.              If it doesn't match, we may have a different binary, the
  228.              same binary with the dynamic table loaded at an unrelated
  229.              location, or anything, really.  To avoid regressions,
  230.              don't adjust the base offset in the latter case, although
  231.              odds are that, if things really changed, debugging won't
  232.              quite work.

  233.              One could expect more the condition
  234.                ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
  235.              but the one below is relaxed for PPC.  The PPC kernel supports
  236.              either 4k or 64k page sizes.  To be prepared for 64k pages,
  237.              PPC ELF files are built using an alignment requirement of 64k.
  238.              However, when running on a kernel supporting 4k pages, the memory
  239.              mapping of the library may not actually happen on a 64k boundary!

  240.              (In the usual case where (l_addr & align) == 0, this check is
  241.              equivalent to the possibly expected check above.)

  242.              Even on PPC it must be zero-aligned at least for MINPAGESIZE.  */

  243.           l_addr = l_dynaddr - dynaddr;

  244.           if ((l_addr & (minpagesize - 1)) == 0
  245.               && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
  246.             {
  247.               if (info_verbose)
  248.                 printf_unfiltered (_("Using PIC (Position Independent Code) "
  249.                                      "prelink displacement %s for \"%s\".\n"),
  250.                                    paddress (target_gdbarch (), l_addr),
  251.                                    so->so_name);
  252.             }
  253.           else
  254.             {
  255.               /* There is no way to verify the library file matches.  prelink
  256.                  can during prelinking of an unprelinked file (or unprelinking
  257.                  of a prelinked file) shift the DYNAMIC segment by arbitrary
  258.                  offset without any page size alignment.  There is no way to
  259.                  find out the ELF header and/or Program Headers for a limited
  260.                  verification if it they match.  One could do a verification
  261.                  of the DYNAMIC segment.  Still the found address is the best
  262.                  one GDB could find.  */

  263.               warning (_(".dynamic section for \"%s\" "
  264.                          "is not at the expected address "
  265.                          "(wrong library or version mismatch?)"), so->so_name);
  266.             }
  267.         }

  268.     set_addr:
  269.       so->lm_info->l_addr = l_addr;
  270.       so->lm_info->l_addr_p = 1;
  271.     }

  272.   return so->lm_info->l_addr;
  273. }

  274. /* Per pspace SVR4 specific data.  */

  275. struct svr4_info
  276. {
  277.   CORE_ADDR debug_base;        /* Base of dynamic linker structures.  */

  278.   /* Validity flag for debug_loader_offset.  */
  279.   int debug_loader_offset_p;

  280.   /* Load address for the dynamic linker, inferred.  */
  281.   CORE_ADDR debug_loader_offset;

  282.   /* Name of the dynamic linker, valid if debug_loader_offset_p.  */
  283.   char *debug_loader_name;

  284.   /* Load map address for the main executable.  */
  285.   CORE_ADDR main_lm_addr;

  286.   CORE_ADDR interp_text_sect_low;
  287.   CORE_ADDR interp_text_sect_high;
  288.   CORE_ADDR interp_plt_sect_low;
  289.   CORE_ADDR interp_plt_sect_high;

  290.   /* Nonzero if the list of objects was last obtained from the target
  291.      via qXfer:libraries-svr4:read.  */
  292.   int using_xfer;

  293.   /* Table of struct probe_and_action instances, used by the
  294.      probes-based interface to map breakpoint addresses to probes
  295.      and their associated actions.  Lookup is performed using
  296.      probe_and_action->probe->address.  */
  297.   htab_t probes_table;

  298.   /* List of objects loaded into the inferior, used by the probes-
  299.      based interface.  */
  300.   struct so_list *solib_list;
  301. };

  302. /* Per-program-space data key.  */
  303. static const struct program_space_data *solib_svr4_pspace_data;

  304. /* Free the probes table.  */

  305. static void
  306. free_probes_table (struct svr4_info *info)
  307. {
  308.   if (info->probes_table == NULL)
  309.     return;

  310.   htab_delete (info->probes_table);
  311.   info->probes_table = NULL;
  312. }

  313. /* Free the solib list.  */

  314. static void
  315. free_solib_list (struct svr4_info *info)
  316. {
  317.   svr4_free_library_list (&info->solib_list);
  318.   info->solib_list = NULL;
  319. }

  320. static void
  321. svr4_pspace_data_cleanup (struct program_space *pspace, void *arg)
  322. {
  323.   struct svr4_info *info = arg;

  324.   free_probes_table (info);
  325.   free_solib_list (info);

  326.   xfree (info);
  327. }

  328. /* Get the current svr4 data.  If none is found yet, add it now.  This
  329.    function always returns a valid object.  */

  330. static struct svr4_info *
  331. get_svr4_info (void)
  332. {
  333.   struct svr4_info *info;

  334.   info = program_space_data (current_program_space, solib_svr4_pspace_data);
  335.   if (info != NULL)
  336.     return info;

  337.   info = XCNEW (struct svr4_info);
  338.   set_program_space_data (current_program_space, solib_svr4_pspace_data, info);
  339.   return info;
  340. }

  341. /* Local function prototypes */

  342. static int match_main (const char *);

  343. /* Read program header TYPE from inferior memory.  The header is found
  344.    by scanning the OS auxillary vector.

  345.    If TYPE == -1, return the program headers instead of the contents of
  346.    one program header.

  347.    Return a pointer to allocated memory holding the program header contents,
  348.    or NULL on failure.  If sucessful, and unless P_SECT_SIZE is NULL, the
  349.    size of those contents is returned to P_SECT_SIZE.  Likewise, the target
  350.    architecture size (32-bit or 64-bit) is returned to P_ARCH_SIZE.  */

  351. static gdb_byte *
  352. read_program_header (int type, int *p_sect_size, int *p_arch_size)
  353. {
  354.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  355.   CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
  356.   int arch_size, sect_size;
  357.   CORE_ADDR sect_addr;
  358.   gdb_byte *buf;
  359.   int pt_phdr_p = 0;

  360.   /* Get required auxv elements from target.  */
  361.   if (target_auxv_search (&current_target, AT_PHDR, &at_phdr) <= 0)
  362.     return 0;
  363.   if (target_auxv_search (&current_target, AT_PHENT, &at_phent) <= 0)
  364.     return 0;
  365.   if (target_auxv_search (&current_target, AT_PHNUM, &at_phnum) <= 0)
  366.     return 0;
  367.   if (!at_phdr || !at_phnum)
  368.     return 0;

  369.   /* Determine ELF architecture type.  */
  370.   if (at_phent == sizeof (Elf32_External_Phdr))
  371.     arch_size = 32;
  372.   else if (at_phent == sizeof (Elf64_External_Phdr))
  373.     arch_size = 64;
  374.   else
  375.     return 0;

  376.   /* Find the requested segment.  */
  377.   if (type == -1)
  378.     {
  379.       sect_addr = at_phdr;
  380.       sect_size = at_phent * at_phnum;
  381.     }
  382.   else if (arch_size == 32)
  383.     {
  384.       Elf32_External_Phdr phdr;
  385.       int i;

  386.       /* Search for requested PHDR.  */
  387.       for (i = 0; i < at_phnum; i++)
  388.         {
  389.           int p_type;

  390.           if (target_read_memory (at_phdr + i * sizeof (phdr),
  391.                                   (gdb_byte *)&phdr, sizeof (phdr)))
  392.             return 0;

  393.           p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
  394.                                              4, byte_order);

  395.           if (p_type == PT_PHDR)
  396.             {
  397.               pt_phdr_p = 1;
  398.               pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
  399.                                                   4, byte_order);
  400.             }

  401.           if (p_type == type)
  402.             break;
  403.         }

  404.       if (i == at_phnum)
  405.         return 0;

  406.       /* Retrieve address and size.  */
  407.       sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
  408.                                             4, byte_order);
  409.       sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
  410.                                             4, byte_order);
  411.     }
  412.   else
  413.     {
  414.       Elf64_External_Phdr phdr;
  415.       int i;

  416.       /* Search for requested PHDR.  */
  417.       for (i = 0; i < at_phnum; i++)
  418.         {
  419.           int p_type;

  420.           if (target_read_memory (at_phdr + i * sizeof (phdr),
  421.                                   (gdb_byte *)&phdr, sizeof (phdr)))
  422.             return 0;

  423.           p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
  424.                                              4, byte_order);

  425.           if (p_type == PT_PHDR)
  426.             {
  427.               pt_phdr_p = 1;
  428.               pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
  429.                                                   8, byte_order);
  430.             }

  431.           if (p_type == type)
  432.             break;
  433.         }

  434.       if (i == at_phnum)
  435.         return 0;

  436.       /* Retrieve address and size.  */
  437.       sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
  438.                                             8, byte_order);
  439.       sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
  440.                                             8, byte_order);
  441.     }

  442.   /* PT_PHDR is optional, but we really need it
  443.      for PIE to make this work in general.  */

  444.   if (pt_phdr_p)
  445.     {
  446.       /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
  447.          Relocation offset is the difference between the two. */
  448.       sect_addr = sect_addr + (at_phdr - pt_phdr);
  449.     }

  450.   /* Read in requested program header.  */
  451.   buf = xmalloc (sect_size);
  452.   if (target_read_memory (sect_addr, buf, sect_size))
  453.     {
  454.       xfree (buf);
  455.       return NULL;
  456.     }

  457.   if (p_arch_size)
  458.     *p_arch_size = arch_size;
  459.   if (p_sect_size)
  460.     *p_sect_size = sect_size;

  461.   return buf;
  462. }


  463. /* Return program interpreter string.  */
  464. static char *
  465. find_program_interpreter (void)
  466. {
  467.   gdb_byte *buf = NULL;

  468.   /* If we have an exec_bfd, use its section table.  */
  469.   if (exec_bfd
  470.       && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
  471.    {
  472.      struct bfd_section *interp_sect;

  473.      interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
  474.      if (interp_sect != NULL)
  475.       {
  476.         int sect_size = bfd_section_size (exec_bfd, interp_sect);

  477.         buf = xmalloc (sect_size);
  478.         bfd_get_section_contents (exec_bfd, interp_sect, buf, 0, sect_size);
  479.       }
  480.    }

  481.   /* If we didn't find it, use the target auxillary vector.  */
  482.   if (!buf)
  483.     buf = read_program_header (PT_INTERP, NULL, NULL);

  484.   return (char *) buf;
  485. }


  486. /* Scan for DESIRED_DYNTAG in .dynamic section of ABFD.  If DESIRED_DYNTAG is
  487.    found, 1 is returned and the corresponding PTR is set.  */

  488. static int
  489. scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr)
  490. {
  491.   int arch_size, step, sect_size;
  492.   long current_dyntag;
  493.   CORE_ADDR dyn_ptr, dyn_addr;
  494.   gdb_byte *bufend, *bufstart, *buf;
  495.   Elf32_External_Dyn *x_dynp_32;
  496.   Elf64_External_Dyn *x_dynp_64;
  497.   struct bfd_section *sect;
  498.   struct target_section *target_section;

  499.   if (abfd == NULL)
  500.     return 0;

  501.   if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  502.     return 0;

  503.   arch_size = bfd_get_arch_size (abfd);
  504.   if (arch_size == -1)
  505.     return 0;

  506.   /* Find the start address of the .dynamic section.  */
  507.   sect = bfd_get_section_by_name (abfd, ".dynamic");
  508.   if (sect == NULL)
  509.     return 0;

  510.   for (target_section = current_target_sections->sections;
  511.        target_section < current_target_sections->sections_end;
  512.        target_section++)
  513.     if (sect == target_section->the_bfd_section)
  514.       break;
  515.   if (target_section < current_target_sections->sections_end)
  516.     dyn_addr = target_section->addr;
  517.   else
  518.     {
  519.       /* ABFD may come from OBJFILE acting only as a symbol file without being
  520.          loaded into the target (see add_symbol_file_command).  This case is
  521.          such fallback to the file VMA address without the possibility of
  522.          having the section relocated to its actual in-memory address.  */

  523.       dyn_addr = bfd_section_vma (abfd, sect);
  524.     }

  525.   /* Read in .dynamic from the BFD.  We will get the actual value
  526.      from memory later.  */
  527.   sect_size = bfd_section_size (abfd, sect);
  528.   buf = bufstart = alloca (sect_size);
  529.   if (!bfd_get_section_contents (abfd, sect,
  530.                                  buf, 0, sect_size))
  531.     return 0;

  532.   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
  533.   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
  534.                            : sizeof (Elf64_External_Dyn);
  535.   for (bufend = buf + sect_size;
  536.        buf < bufend;
  537.        buf += step)
  538.   {
  539.     if (arch_size == 32)
  540.       {
  541.         x_dynp_32 = (Elf32_External_Dyn *) buf;
  542.         current_dyntag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
  543.         dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
  544.       }
  545.     else
  546.       {
  547.         x_dynp_64 = (Elf64_External_Dyn *) buf;
  548.         current_dyntag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
  549.         dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
  550.       }
  551.      if (current_dyntag == DT_NULL)
  552.        return 0;
  553.      if (current_dyntag == desired_dyntag)
  554.        {
  555.          /* If requested, try to read the runtime value of this .dynamic
  556.             entry.  */
  557.          if (ptr)
  558.            {
  559.              struct type *ptr_type;
  560.              gdb_byte ptr_buf[8];
  561.              CORE_ADDR ptr_addr;

  562.              ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  563.              ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
  564.              if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
  565.                dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
  566.              *ptr = dyn_ptr;
  567.            }
  568.          return 1;
  569.        }
  570.   }

  571.   return 0;
  572. }

  573. /* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
  574.    found by consulting the OS auxillary vector.  If DESIRED_DYNTAG is found, 1
  575.    is returned and the corresponding PTR is set.  */

  576. static int
  577. scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr)
  578. {
  579.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  580.   int sect_size, arch_size, step;
  581.   long current_dyntag;
  582.   CORE_ADDR dyn_ptr;
  583.   gdb_byte *bufend, *bufstart, *buf;

  584.   /* Read in .dynamic section.  */
  585.   buf = bufstart = read_program_header (PT_DYNAMIC, &sect_size, &arch_size);
  586.   if (!buf)
  587.     return 0;

  588.   /* Iterate over BUF and scan for DYNTAG.  If found, set PTR and return.  */
  589.   step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
  590.                            : sizeof (Elf64_External_Dyn);
  591.   for (bufend = buf + sect_size;
  592.        buf < bufend;
  593.        buf += step)
  594.   {
  595.     if (arch_size == 32)
  596.       {
  597.         Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;

  598.         current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
  599.                                             4, byte_order);
  600.         dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
  601.                                             4, byte_order);
  602.       }
  603.     else
  604.       {
  605.         Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;

  606.         current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
  607.                                             8, byte_order);
  608.         dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
  609.                                             8, byte_order);
  610.       }
  611.     if (current_dyntag == DT_NULL)
  612.       break;

  613.     if (current_dyntag == desired_dyntag)
  614.       {
  615.         if (ptr)
  616.           *ptr = dyn_ptr;

  617.         xfree (bufstart);
  618.         return 1;
  619.       }
  620.   }

  621.   xfree (bufstart);
  622.   return 0;
  623. }

  624. /* Locate the base address of dynamic linker structs for SVR4 elf
  625.    targets.

  626.    For SVR4 elf targets the address of the dynamic linker's runtime
  627.    structure is contained within the dynamic info section in the
  628.    executable file.  The dynamic section is also mapped into the
  629.    inferior address space.  Because the runtime loader fills in the
  630.    real address before starting the inferior, we have to read in the
  631.    dynamic info section from the inferior address space.
  632.    If there are any errors while trying to find the address, we
  633.    silently return 0, otherwise the found address is returned.  */

  634. static CORE_ADDR
  635. elf_locate_base (void)
  636. {
  637.   struct bound_minimal_symbol msymbol;
  638.   CORE_ADDR dyn_ptr;

  639.   /* Look for DT_MIPS_RLD_MAP first.  MIPS executables use this
  640.      instead of DT_DEBUG, although they sometimes contain an unused
  641.      DT_DEBUG.  */
  642.   if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr)
  643.       || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr))
  644.     {
  645.       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  646.       gdb_byte *pbuf;
  647.       int pbuf_size = TYPE_LENGTH (ptr_type);

  648.       pbuf = alloca (pbuf_size);
  649.       /* DT_MIPS_RLD_MAP contains a pointer to the address
  650.          of the dynamic link structure.  */
  651.       if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
  652.         return 0;
  653.       return extract_typed_address (pbuf, ptr_type);
  654.     }

  655.   /* Find DT_DEBUG.  */
  656.   if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr)
  657.       || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr))
  658.     return dyn_ptr;

  659.   /* This may be a static executable.  Look for the symbol
  660.      conventionally named _r_debug, as a last resort.  */
  661.   msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
  662.   if (msymbol.minsym != NULL)
  663.     return BMSYMBOL_VALUE_ADDRESS (msymbol);

  664.   /* DT_DEBUG entry not found.  */
  665.   return 0;
  666. }

  667. /* Locate the base address of dynamic linker structs.

  668.    For both the SunOS and SVR4 shared library implementations, if the
  669.    inferior executable has been linked dynamically, there is a single
  670.    address somewhere in the inferior's data space which is the key to
  671.    locating all of the dynamic linker's runtime structures.  This
  672.    address is the value of the debug base symbol.  The job of this
  673.    function is to find and return that address, or to return 0 if there
  674.    is no such address (the executable is statically linked for example).

  675.    For SunOS, the job is almost trivial, since the dynamic linker and
  676.    all of it's structures are statically linked to the executable at
  677.    link time.  Thus the symbol for the address we are looking for has
  678.    already been added to the minimal symbol table for the executable's
  679.    objfile at the time the symbol file's symbols were read, and all we
  680.    have to do is look it up there.  Note that we explicitly do NOT want
  681.    to find the copies in the shared library.

  682.    The SVR4 version is a bit more complicated because the address
  683.    is contained somewhere in the dynamic info section.  We have to go
  684.    to a lot more work to discover the address of the debug base symbol.
  685.    Because of this complexity, we cache the value we find and return that
  686.    value on subsequent invocations.  Note there is no copy in the
  687.    executable symbol tables.  */

  688. static CORE_ADDR
  689. locate_base (struct svr4_info *info)
  690. {
  691.   /* Check to see if we have a currently valid address, and if so, avoid
  692.      doing all this work again and just return the cached address.  If
  693.      we have no cached address, try to locate it in the dynamic info
  694.      section for ELF executables.  There's no point in doing any of this
  695.      though if we don't have some link map offsets to work with.  */

  696.   if (info->debug_base == 0 && svr4_have_link_map_offsets ())
  697.     info->debug_base = elf_locate_base ();
  698.   return info->debug_base;
  699. }

  700. /* Find the first element in the inferior's dynamic link map, and
  701.    return its address in the inferior.  Return zero if the address
  702.    could not be determined.

  703.    FIXME: Perhaps we should validate the info somehow, perhaps by
  704.    checking r_version for a known version number, or r_state for
  705.    RT_CONSISTENT.  */

  706. static CORE_ADDR
  707. solib_svr4_r_map (struct svr4_info *info)
  708. {
  709.   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  710.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  711.   CORE_ADDR addr = 0;
  712.   volatile struct gdb_exception ex;

  713.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  714.     {
  715.       addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
  716.                                         ptr_type);
  717.     }
  718.   exception_print (gdb_stderr, ex);
  719.   return addr;
  720. }

  721. /* Find r_brk from the inferior's debug base.  */

  722. static CORE_ADDR
  723. solib_svr4_r_brk (struct svr4_info *info)
  724. {
  725.   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  726.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;

  727.   return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
  728.                                     ptr_type);
  729. }

  730. /* Find the link map for the dynamic linker (if it is not in the
  731.    normal list of loaded shared objects).  */

  732. static CORE_ADDR
  733. solib_svr4_r_ldsomap (struct svr4_info *info)
  734. {
  735.   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  736.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  737.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  738.   ULONGEST version;

  739.   /* Check version, and return zero if `struct r_debug' doesn't have
  740.      the r_ldsomap member.  */
  741.   version
  742.     = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
  743.                                     lmo->r_version_size, byte_order);
  744.   if (version < 2 || lmo->r_ldsomap_offset == -1)
  745.     return 0;

  746.   return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
  747.                                     ptr_type);
  748. }

  749. /* On Solaris systems with some versions of the dynamic linker,
  750.    ld.so's l_name pointer points to the SONAME in the string table
  751.    rather than into writable memory.  So that GDB can find shared
  752.    libraries when loading a core file generated by gcore, ensure that
  753.    memory areas containing the l_name string are saved in the core
  754.    file.  */

  755. static int
  756. svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
  757. {
  758.   struct svr4_info *info;
  759.   CORE_ADDR ldsomap;
  760.   struct so_list *new;
  761.   struct cleanup *old_chain;
  762.   CORE_ADDR name_lm;

  763.   info = get_svr4_info ();

  764.   info->debug_base = 0;
  765.   locate_base (info);
  766.   if (!info->debug_base)
  767.     return 0;

  768.   ldsomap = solib_svr4_r_ldsomap (info);
  769.   if (!ldsomap)
  770.     return 0;

  771.   new = XCNEW (struct so_list);
  772.   old_chain = make_cleanup (xfree, new);
  773.   new->lm_info = lm_info_read (ldsomap);
  774.   make_cleanup (xfree, new->lm_info);
  775.   name_lm = new->lm_info ? new->lm_info->l_name : 0;
  776.   do_cleanups (old_chain);

  777.   return (name_lm >= vaddr && name_lm < vaddr + size);
  778. }

  779. /* Implement the "open_symbol_file_object" target_so_ops method.

  780.    If no open symbol file, attempt to locate and open the main symbol
  781.    file.  On SVR4 systems, this is the first link map entry.  If its
  782.    name is here, we can open it.  Useful when attaching to a process
  783.    without first loading its symbol file.  */

  784. static int
  785. open_symbol_file_object (void *from_ttyp)
  786. {
  787.   CORE_ADDR lm, l_name;
  788.   char *filename;
  789.   int errcode;
  790.   int from_tty = *(int *)from_ttyp;
  791.   struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
  792.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  793.   int l_name_size = TYPE_LENGTH (ptr_type);
  794.   gdb_byte *l_name_buf = xmalloc (l_name_size);
  795.   struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
  796.   struct svr4_info *info = get_svr4_info ();

  797.   if (symfile_objfile)
  798.     if (!query (_("Attempt to reload symbols from process? ")))
  799.       {
  800.         do_cleanups (cleanups);
  801.         return 0;
  802.       }

  803.   /* Always locate the debug struct, in case it has moved.  */
  804.   info->debug_base = 0;
  805.   if (locate_base (info) == 0)
  806.     {
  807.       do_cleanups (cleanups);
  808.       return 0;        /* failed somehow...  */
  809.     }

  810.   /* First link map member should be the executable.  */
  811.   lm = solib_svr4_r_map (info);
  812.   if (lm == 0)
  813.     {
  814.       do_cleanups (cleanups);
  815.       return 0;        /* failed somehow...  */
  816.     }

  817.   /* Read address of name from target memory to GDB.  */
  818.   read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size);

  819.   /* Convert the address to host format.  */
  820.   l_name = extract_typed_address (l_name_buf, ptr_type);

  821.   if (l_name == 0)
  822.     {
  823.       do_cleanups (cleanups);
  824.       return 0;                /* No filename.  */
  825.     }

  826.   /* Now fetch the filename from target memory.  */
  827.   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
  828.   make_cleanup (xfree, filename);

  829.   if (errcode)
  830.     {
  831.       warning (_("failed to read exec filename from attached file: %s"),
  832.                safe_strerror (errcode));
  833.       do_cleanups (cleanups);
  834.       return 0;
  835.     }

  836.   /* Have a pathname: read the symbol file.  */
  837.   symbol_file_add_main (filename, from_tty);

  838.   do_cleanups (cleanups);
  839.   return 1;
  840. }

  841. /* Data exchange structure for the XML parser as returned by
  842.    svr4_current_sos_via_xfer_libraries.  */

  843. struct svr4_library_list
  844. {
  845.   struct so_list *head, **tailp;

  846.   /* Inferior address of struct link_map used for the main executable.  It is
  847.      NULL if not known.  */
  848.   CORE_ADDR main_lm;
  849. };

  850. /* Implementation for target_so_ops.free_so.  */

  851. static void
  852. svr4_free_so (struct so_list *so)
  853. {
  854.   xfree (so->lm_info);
  855. }

  856. /* Implement target_so_ops.clear_so.  */

  857. static void
  858. svr4_clear_so (struct so_list *so)
  859. {
  860.   if (so->lm_info != NULL)
  861.     so->lm_info->l_addr_p = 0;
  862. }

  863. /* Free so_list built so far (called via cleanup).  */

  864. static void
  865. svr4_free_library_list (void *p_list)
  866. {
  867.   struct so_list *list = *(struct so_list **) p_list;

  868.   while (list != NULL)
  869.     {
  870.       struct so_list *next = list->next;

  871.       free_so (list);
  872.       list = next;
  873.     }
  874. }

  875. /* Copy library list.  */

  876. static struct so_list *
  877. svr4_copy_library_list (struct so_list *src)
  878. {
  879.   struct so_list *dst = NULL;
  880.   struct so_list **link = &dst;

  881.   while (src != NULL)
  882.     {
  883.       struct so_list *new;

  884.       new = xmalloc (sizeof (struct so_list));
  885.       memcpy (new, src, sizeof (struct so_list));

  886.       new->lm_info = xmalloc (sizeof (struct lm_info));
  887.       memcpy (new->lm_info, src->lm_info, sizeof (struct lm_info));

  888.       new->next = NULL;
  889.       *link = new;
  890.       link = &new->next;

  891.       src = src->next;
  892.     }

  893.   return dst;
  894. }

  895. #ifdef HAVE_LIBEXPAT

  896. #include "xml-support.h"

  897. /* Handle the start of a <library> element.  Note: new elements are added
  898.    at the tail of the list, keeping the list in order.  */

  899. static void
  900. library_list_start_library (struct gdb_xml_parser *parser,
  901.                             const struct gdb_xml_element *element,
  902.                             void *user_data, VEC(gdb_xml_value_s) *attributes)
  903. {
  904.   struct svr4_library_list *list = user_data;
  905.   const char *name = xml_find_attribute (attributes, "name")->value;
  906.   ULONGEST *lmp = xml_find_attribute (attributes, "lm")->value;
  907.   ULONGEST *l_addrp = xml_find_attribute (attributes, "l_addr")->value;
  908.   ULONGEST *l_ldp = xml_find_attribute (attributes, "l_ld")->value;
  909.   struct so_list *new_elem;

  910.   new_elem = XCNEW (struct so_list);
  911.   new_elem->lm_info = XCNEW (struct lm_info);
  912.   new_elem->lm_info->lm_addr = *lmp;
  913.   new_elem->lm_info->l_addr_inferior = *l_addrp;
  914.   new_elem->lm_info->l_ld = *l_ldp;

  915.   strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
  916.   new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
  917.   strcpy (new_elem->so_original_name, new_elem->so_name);

  918.   *list->tailp = new_elem;
  919.   list->tailp = &new_elem->next;
  920. }

  921. /* Handle the start of a <library-list-svr4> element.  */

  922. static void
  923. svr4_library_list_start_list (struct gdb_xml_parser *parser,
  924.                               const struct gdb_xml_element *element,
  925.                               void *user_data, VEC(gdb_xml_value_s) *attributes)
  926. {
  927.   struct svr4_library_list *list = user_data;
  928.   const char *version = xml_find_attribute (attributes, "version")->value;
  929.   struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");

  930.   if (strcmp (version, "1.0") != 0)
  931.     gdb_xml_error (parser,
  932.                    _("SVR4 Library list has unsupported version \"%s\""),
  933.                    version);

  934.   if (main_lm)
  935.     list->main_lm = *(ULONGEST *) main_lm->value;
  936. }

  937. /* The allowed elements and attributes for an XML library list.
  938.    The root element is a <library-list>.  */

  939. static const struct gdb_xml_attribute svr4_library_attributes[] =
  940. {
  941.   { "name", GDB_XML_AF_NONE, NULL, NULL },
  942.   { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  943.   { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  944.   { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
  945.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  946. };

  947. static const struct gdb_xml_element svr4_library_list_children[] =
  948. {
  949.   {
  950.     "library", svr4_library_attributes, NULL,
  951.     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
  952.     library_list_start_library, NULL
  953.   },
  954.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  955. };

  956. static const struct gdb_xml_attribute svr4_library_list_attributes[] =
  957. {
  958.   { "version", GDB_XML_AF_NONE, NULL, NULL },
  959.   { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
  960.   { NULL, GDB_XML_AF_NONE, NULL, NULL }
  961. };

  962. static const struct gdb_xml_element svr4_library_list_elements[] =
  963. {
  964.   { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
  965.     GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
  966.   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
  967. };

  968. /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN.  Return 1 if

  969.    Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
  970.    case.  Return 1 if *SO_LIST_RETURN contains the library list, it may be
  971.    empty, caller is responsible for freeing all its entries.  */

  972. static int
  973. svr4_parse_libraries (const char *document, struct svr4_library_list *list)
  974. {
  975.   struct cleanup *back_to = make_cleanup (svr4_free_library_list,
  976.                                           &list->head);

  977.   memset (list, 0, sizeof (*list));
  978.   list->tailp = &list->head;
  979.   if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
  980.                            svr4_library_list_elements, document, list) == 0)
  981.     {
  982.       /* Parsed successfully, keep the result.  */
  983.       discard_cleanups (back_to);
  984.       return 1;
  985.     }

  986.   do_cleanups (back_to);
  987.   return 0;
  988. }

  989. /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.

  990.    Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
  991.    case.  Return 1 if *SO_LIST_RETURN contains the library list, it may be
  992.    empty, caller is responsible for freeing all its entries.

  993.    Note that ANNEX must be NULL if the remote does not explicitly allow
  994.    qXfer:libraries-svr4:read packets with non-empty annexes.  Support for
  995.    this can be checked using target_augmented_libraries_svr4_read ().  */

  996. static int
  997. svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
  998.                                      const char *annex)
  999. {
  1000.   char *svr4_library_document;
  1001.   int result;
  1002.   struct cleanup *back_to;

  1003.   gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());

  1004.   /* Fetch the list of shared libraries.  */
  1005.   svr4_library_document = target_read_stralloc (&current_target,
  1006.                                                 TARGET_OBJECT_LIBRARIES_SVR4,
  1007.                                                 annex);
  1008.   if (svr4_library_document == NULL)
  1009.     return 0;

  1010.   back_to = make_cleanup (xfree, svr4_library_document);
  1011.   result = svr4_parse_libraries (svr4_library_document, list);
  1012.   do_cleanups (back_to);

  1013.   return result;
  1014. }

  1015. #else

  1016. static int
  1017. svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
  1018.                                      const char *annex)
  1019. {
  1020.   return 0;
  1021. }

  1022. #endif

  1023. /* If no shared library information is available from the dynamic
  1024.    linker, build a fallback list from other sources.  */

  1025. static struct so_list *
  1026. svr4_default_sos (void)
  1027. {
  1028.   struct svr4_info *info = get_svr4_info ();
  1029.   struct so_list *new;

  1030.   if (!info->debug_loader_offset_p)
  1031.     return NULL;

  1032.   new = XCNEW (struct so_list);

  1033.   new->lm_info = xzalloc (sizeof (struct lm_info));

  1034.   /* Nothing will ever check the other fields if we set l_addr_p.  */
  1035.   new->lm_info->l_addr = info->debug_loader_offset;
  1036.   new->lm_info->l_addr_p = 1;

  1037.   strncpy (new->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
  1038.   new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  1039.   strcpy (new->so_original_name, new->so_name);

  1040.   return new;
  1041. }

  1042. /* Read the whole inferior libraries chain starting at address LM.
  1043.    Expect the first entry in the chain's previous entry to be PREV_LM.
  1044.    Add the entries to the tail referenced by LINK_PTR_PTR.  Ignore the
  1045.    first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
  1046.    to it.  Returns nonzero upon success.  If zero is returned the
  1047.    entries stored to LINK_PTR_PTR are still valid although they may
  1048.    represent only part of the inferior library list.  */

  1049. static int
  1050. svr4_read_so_list (CORE_ADDR lm, CORE_ADDR prev_lm,
  1051.                    struct so_list ***link_ptr_ptr, int ignore_first)
  1052. {
  1053.   CORE_ADDR first_l_name = 0;
  1054.   CORE_ADDR next_lm;

  1055.   for (; lm != 0; prev_lm = lm, lm = next_lm)
  1056.     {
  1057.       struct so_list *new;
  1058.       struct cleanup *old_chain;
  1059.       int errcode;
  1060.       char *buffer;

  1061.       new = XCNEW (struct so_list);
  1062.       old_chain = make_cleanup_free_so (new);

  1063.       new->lm_info = lm_info_read (lm);
  1064.       if (new->lm_info == NULL)
  1065.         {
  1066.           do_cleanups (old_chain);
  1067.           return 0;
  1068.         }

  1069.       next_lm = new->lm_info->l_next;

  1070.       if (new->lm_info->l_prev != prev_lm)
  1071.         {
  1072.           warning (_("Corrupted shared library list: %s != %s"),
  1073.                    paddress (target_gdbarch (), prev_lm),
  1074.                    paddress (target_gdbarch (), new->lm_info->l_prev));
  1075.           do_cleanups (old_chain);
  1076.           return 0;
  1077.         }

  1078.       /* For SVR4 versions, the first entry in the link map is for the
  1079.          inferior executable, so we must ignore it.  For some versions of
  1080.          SVR4, it has no name.  For others (Solaris 2.3 for example), it
  1081.          does have a name, so we can no longer use a missing name to
  1082.          decide when to ignore it.  */
  1083.       if (ignore_first && new->lm_info->l_prev == 0)
  1084.         {
  1085.           struct svr4_info *info = get_svr4_info ();

  1086.           first_l_name = new->lm_info->l_name;
  1087.           info->main_lm_addr = new->lm_info->lm_addr;
  1088.           do_cleanups (old_chain);
  1089.           continue;
  1090.         }

  1091.       /* Extract this shared object's name.  */
  1092.       target_read_string (new->lm_info->l_name, &buffer,
  1093.                           SO_NAME_MAX_PATH_SIZE - 1, &errcode);
  1094.       if (errcode != 0)
  1095.         {
  1096.           /* If this entry's l_name address matches that of the
  1097.              inferior executable, then this is not a normal shared
  1098.              object, but (most likely) a vDSO.  In this case, silently
  1099.              skip it; otherwise emit a warning. */
  1100.           if (first_l_name == 0 || new->lm_info->l_name != first_l_name)
  1101.             warning (_("Can't read pathname for load map: %s."),
  1102.                      safe_strerror (errcode));
  1103.           do_cleanups (old_chain);
  1104.           continue;
  1105.         }

  1106.       strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
  1107.       new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  1108.       strcpy (new->so_original_name, new->so_name);
  1109.       xfree (buffer);

  1110.       /* If this entry has no name, or its name matches the name
  1111.          for the main executable, don't include it in the list.  */
  1112.       if (! new->so_name[0] || match_main (new->so_name))
  1113.         {
  1114.           do_cleanups (old_chain);
  1115.           continue;
  1116.         }

  1117.       discard_cleanups (old_chain);
  1118.       new->next = 0;
  1119.       **link_ptr_ptr = new;
  1120.       *link_ptr_ptr = &new->next;
  1121.     }

  1122.   return 1;
  1123. }

  1124. /* Read the full list of currently loaded shared objects directly
  1125.    from the inferior, without referring to any libraries read and
  1126.    stored by the probes interface.  Handle special cases relating
  1127.    to the first elements of the list.  */

  1128. static struct so_list *
  1129. svr4_current_sos_direct (struct svr4_info *info)
  1130. {
  1131.   CORE_ADDR lm;
  1132.   struct so_list *head = NULL;
  1133.   struct so_list **link_ptr = &head;
  1134.   struct cleanup *back_to;
  1135.   int ignore_first;
  1136.   struct svr4_library_list library_list;

  1137.   /* Fall back to manual examination of the target if the packet is not
  1138.      supported or gdbserver failed to find DT_DEBUG.  gdb.server/solib-list.exp
  1139.      tests a case where gdbserver cannot find the shared libraries list while
  1140.      GDB itself is able to find it via SYMFILE_OBJFILE.

  1141.      Unfortunately statically linked inferiors will also fall back through this
  1142.      suboptimal code path.  */

  1143.   info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
  1144.                                                           NULL);
  1145.   if (info->using_xfer)
  1146.     {
  1147.       if (library_list.main_lm)
  1148.         info->main_lm_addr = library_list.main_lm;

  1149.       return library_list.head ? library_list.head : svr4_default_sos ();
  1150.     }

  1151.   /* Always locate the debug struct, in case it has moved.  */
  1152.   info->debug_base = 0;
  1153.   locate_base (info);

  1154.   /* If we can't find the dynamic linker's base structure, this
  1155.      must not be a dynamically linked executable.  Hmm.  */
  1156.   if (! info->debug_base)
  1157.     return svr4_default_sos ();

  1158.   /* Assume that everything is a library if the dynamic loader was loaded
  1159.      late by a static executable.  */
  1160.   if (exec_bfd && bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL)
  1161.     ignore_first = 0;
  1162.   else
  1163.     ignore_first = 1;

  1164.   back_to = make_cleanup (svr4_free_library_list, &head);

  1165.   /* Walk the inferior's link map list, and build our list of
  1166.      `struct so_list' nodes.  */
  1167.   lm = solib_svr4_r_map (info);
  1168.   if (lm)
  1169.     svr4_read_so_list (lm, 0, &link_ptr, ignore_first);

  1170.   /* On Solaris, the dynamic linker is not in the normal list of
  1171.      shared objects, so make sure we pick it up too.  Having
  1172.      symbol information for the dynamic linker is quite crucial
  1173.      for skipping dynamic linker resolver code.  */
  1174.   lm = solib_svr4_r_ldsomap (info);
  1175.   if (lm)
  1176.     svr4_read_so_list (lm, 0, &link_ptr, 0);

  1177.   discard_cleanups (back_to);

  1178.   if (head == NULL)
  1179.     return svr4_default_sos ();

  1180.   return head;
  1181. }

  1182. /* Implement the main part of the "current_sos" target_so_ops
  1183.    method.  */

  1184. static struct so_list *
  1185. svr4_current_sos_1 (void)
  1186. {
  1187.   struct svr4_info *info = get_svr4_info ();

  1188.   /* If the solib list has been read and stored by the probes
  1189.      interface then we return a copy of the stored list.  */
  1190.   if (info->solib_list != NULL)
  1191.     return svr4_copy_library_list (info->solib_list);

  1192.   /* Otherwise obtain the solib list directly from the inferior.  */
  1193.   return svr4_current_sos_direct (info);
  1194. }

  1195. /* Implement the "current_sos" target_so_ops method.  */

  1196. static struct so_list *
  1197. svr4_current_sos (void)
  1198. {
  1199.   struct so_list *so_head = svr4_current_sos_1 ();
  1200.   struct mem_range vsyscall_range;

  1201.   /* Filter out the vDSO module, if present.  Its symbol file would
  1202.      not be found on disk.  The vDSO/vsyscall's OBJFILE is instead
  1203.      managed by symfile-mem.c:add_vsyscall_page.  */
  1204.   if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range)
  1205.       && vsyscall_range.length != 0)
  1206.     {
  1207.       struct so_list **sop;

  1208.       sop = &so_head;
  1209.       while (*sop != NULL)
  1210.         {
  1211.           struct so_list *so = *sop;

  1212.           /* We can't simply match the vDSO by starting address alone,
  1213.              because lm_info->l_addr_inferior (and also l_addr) do not
  1214.              necessarily represent the real starting address of the
  1215.              ELF if the vDSO's ELF itself is "prelinked".  The l_ld
  1216.              field (the ".dynamic" section of the shared object)
  1217.              always points at the absolute/resolved address though.
  1218.              So check whether that address is inside the vDSO's
  1219.              mapping instead.

  1220.              E.g., on Linux 3.16 (x86_64) the vDSO is a regular
  1221.              0-based ELF, and we see:

  1222.               (gdb) info auxv
  1223.               33  AT_SYSINFO_EHDR  System-supplied DSO's ELF header 0x7ffff7ffb000
  1224.               (gdb)  p/x *_r_debug.r_map.l_next
  1225.               $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...}

  1226.              And on Linux 2.6.32 (x86_64) we see:

  1227.               (gdb) info auxv
  1228.               33  AT_SYSINFO_EHDR  System-supplied DSO's ELF header 0x7ffff7ffe000
  1229.               (gdb) p/x *_r_debug.r_map.l_next
  1230.               $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... }

  1231.              Dumping that vDSO shows:

  1232.               (gdb) info proc mappings
  1233.               0x7ffff7ffe000  0x7ffff7fff000  0x1000  0  [vdso]
  1234.               (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
  1235.               # readelf -Wa vdso.bin
  1236.               [...]
  1237.                 Entry point address: 0xffffffffff700700
  1238.               [...]
  1239.               Section Headers:
  1240.                 [Nr] Name     Type    Address               Off    Size
  1241.                 [ 0]              NULL    0000000000000000 000000 000000
  1242.                 [ 1] .hash    HASH    ffffffffff700120 000120 000038
  1243.                 [ 2] .dynsym  DYNSYM  ffffffffff700158 000158 0000d8
  1244.               [...]
  1245.                 [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
  1246.           */
  1247.           if (address_in_mem_range (so->lm_info->l_ld, &vsyscall_range))
  1248.             {
  1249.               *sop = so->next;
  1250.               free_so (so);
  1251.               break;
  1252.             }

  1253.           sop = &so->next;
  1254.         }
  1255.     }

  1256.   return so_head;
  1257. }

  1258. /* Get the address of the link_map for a given OBJFILE.  */

  1259. CORE_ADDR
  1260. svr4_fetch_objfile_link_map (struct objfile *objfile)
  1261. {
  1262.   struct so_list *so;
  1263.   struct svr4_info *info = get_svr4_info ();

  1264.   /* Cause svr4_current_sos() to be run if it hasn't been already.  */
  1265.   if (info->main_lm_addr == 0)
  1266.     solib_add (NULL, 0, &current_target, auto_solib_add);

  1267.   /* svr4_current_sos() will set main_lm_addr for the main executable.  */
  1268.   if (objfile == symfile_objfile)
  1269.     return info->main_lm_addr;

  1270.   /* The other link map addresses may be found by examining the list
  1271.      of shared libraries.  */
  1272.   for (so = master_so_list (); so; so = so->next)
  1273.     if (so->objfile == objfile)
  1274.       return so->lm_info->lm_addr;

  1275.   /* Not found!  */
  1276.   return 0;
  1277. }

  1278. /* On some systems, the only way to recognize the link map entry for
  1279.    the main executable file is by looking at its name.  Return
  1280.    non-zero iff SONAME matches one of the known main executable names.  */

  1281. static int
  1282. match_main (const char *soname)
  1283. {
  1284.   const char * const *mainp;

  1285.   for (mainp = main_name_list; *mainp != NULL; mainp++)
  1286.     {
  1287.       if (strcmp (soname, *mainp) == 0)
  1288.         return (1);
  1289.     }

  1290.   return (0);
  1291. }

  1292. /* Return 1 if PC lies in the dynamic symbol resolution code of the
  1293.    SVR4 run time loader.  */

  1294. int
  1295. svr4_in_dynsym_resolve_code (CORE_ADDR pc)
  1296. {
  1297.   struct svr4_info *info = get_svr4_info ();

  1298.   return ((pc >= info->interp_text_sect_low
  1299.            && pc < info->interp_text_sect_high)
  1300.           || (pc >= info->interp_plt_sect_low
  1301.               && pc < info->interp_plt_sect_high)
  1302.           || in_plt_section (pc)
  1303.           || in_gnu_ifunc_stub (pc));
  1304. }

  1305. /* Given an executable's ABFD and target, compute the entry-point
  1306.    address.  */

  1307. static CORE_ADDR
  1308. exec_entry_point (struct bfd *abfd, struct target_ops *targ)
  1309. {
  1310.   CORE_ADDR addr;

  1311.   /* KevinB wrote ... for most targets, the address returned by
  1312.      bfd_get_start_address() is the entry point for the start
  1313.      function.  But, for some targets, bfd_get_start_address() returns
  1314.      the address of a function descriptor from which the entry point
  1315.      address may be extracted.  This address is extracted by
  1316.      gdbarch_convert_from_func_ptr_addr().  The method
  1317.      gdbarch_convert_from_func_ptr_addr() is the merely the identify
  1318.      function for targets which don't use function descriptors.  */
  1319.   addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  1320.                                              bfd_get_start_address (abfd),
  1321.                                              targ);
  1322.   return gdbarch_addr_bits_remove (target_gdbarch (), addr);
  1323. }

  1324. /* A probe and its associated action.  */

  1325. struct probe_and_action
  1326. {
  1327.   /* The probe.  */
  1328.   struct probe *probe;

  1329.   /* The relocated address of the probe.  */
  1330.   CORE_ADDR address;

  1331.   /* The action.  */
  1332.   enum probe_action action;
  1333. };

  1334. /* Returns a hash code for the probe_and_action referenced by p.  */

  1335. static hashval_t
  1336. hash_probe_and_action (const void *p)
  1337. {
  1338.   const struct probe_and_action *pa = p;

  1339.   return (hashval_t) pa->address;
  1340. }

  1341. /* Returns non-zero if the probe_and_actions referenced by p1 and p2
  1342.    are equal.  */

  1343. static int
  1344. equal_probe_and_action (const void *p1, const void *p2)
  1345. {
  1346.   const struct probe_and_action *pa1 = p1;
  1347.   const struct probe_and_action *pa2 = p2;

  1348.   return pa1->address == pa2->address;
  1349. }

  1350. /* Register a solib event probe and its associated action in the
  1351.    probes table.  */

  1352. static void
  1353. register_solib_event_probe (struct probe *probe, CORE_ADDR address,
  1354.                             enum probe_action action)
  1355. {
  1356.   struct svr4_info *info = get_svr4_info ();
  1357.   struct probe_and_action lookup, *pa;
  1358.   void **slot;

  1359.   /* Create the probes table, if necessary.  */
  1360.   if (info->probes_table == NULL)
  1361.     info->probes_table = htab_create_alloc (1, hash_probe_and_action,
  1362.                                             equal_probe_and_action,
  1363.                                             xfree, xcalloc, xfree);

  1364.   lookup.probe = probe;
  1365.   lookup.address = address;
  1366.   slot = htab_find_slot (info->probes_table, &lookup, INSERT);
  1367.   gdb_assert (*slot == HTAB_EMPTY_ENTRY);

  1368.   pa = XCNEW (struct probe_and_action);
  1369.   pa->probe = probe;
  1370.   pa->address = address;
  1371.   pa->action = action;

  1372.   *slot = pa;
  1373. }

  1374. /* Get the solib event probe at the specified location, and the
  1375.    action associated with it.  Returns NULL if no solib event probe
  1376.    was found.  */

  1377. static struct probe_and_action *
  1378. solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
  1379. {
  1380.   struct probe_and_action lookup;
  1381.   void **slot;

  1382.   lookup.address = address;
  1383.   slot = htab_find_slot (info->probes_table, &lookup, NO_INSERT);

  1384.   if (slot == NULL)
  1385.     return NULL;

  1386.   return (struct probe_and_action *) *slot;
  1387. }

  1388. /* Decide what action to take when the specified solib event probe is
  1389.    hit.  */

  1390. static enum probe_action
  1391. solib_event_probe_action (struct probe_and_action *pa)
  1392. {
  1393.   enum probe_action action;
  1394.   unsigned probe_argc;
  1395.   struct frame_info *frame = get_current_frame ();

  1396.   action = pa->action;
  1397.   if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
  1398.     return action;

  1399.   gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);

  1400.   /* Check that an appropriate number of arguments has been supplied.
  1401.      We expect:
  1402.        arg0: Lmid_t lmid (mandatory)
  1403.        arg1: struct r_debug *debug_base (mandatory)
  1404.        arg2: struct link_map *new (optional, for incremental updates)  */
  1405.   probe_argc = get_probe_argument_count (pa->probe, frame);
  1406.   if (probe_argc == 2)
  1407.     action = FULL_RELOAD;
  1408.   else if (probe_argc < 2)
  1409.     action = PROBES_INTERFACE_FAILED;

  1410.   return action;
  1411. }

  1412. /* Populate the shared object list by reading the entire list of
  1413.    shared objects from the inferior.  Handle special cases relating
  1414.    to the first elements of the list.  Returns nonzero on success.  */

  1415. static int
  1416. solist_update_full (struct svr4_info *info)
  1417. {
  1418.   free_solib_list (info);
  1419.   info->solib_list = svr4_current_sos_direct (info);

  1420.   return 1;
  1421. }

  1422. /* Update the shared object list starting from the link-map entry
  1423.    passed by the linker in the probe's third argument.  Returns
  1424.    nonzero if the list was successfully updated, or zero to indicate
  1425.    failure.  */

  1426. static int
  1427. solist_update_incremental (struct svr4_info *info, CORE_ADDR lm)
  1428. {
  1429.   struct so_list *tail;
  1430.   CORE_ADDR prev_lm;

  1431.   /* svr4_current_sos_direct contains logic to handle a number of
  1432.      special cases relating to the first elements of the list.  To
  1433.      avoid duplicating this logic we defer to solist_update_full
  1434.      if the list is empty.  */
  1435.   if (info->solib_list == NULL)
  1436.     return 0;

  1437.   /* Fall back to a full update if we are using a remote target
  1438.      that does not support incremental transfers.  */
  1439.   if (info->using_xfer && !target_augmented_libraries_svr4_read ())
  1440.     return 0;

  1441.   /* Walk to the end of the list.  */
  1442.   for (tail = info->solib_list; tail->next != NULL; tail = tail->next)
  1443.     /* Nothing.  */;
  1444.   prev_lm = tail->lm_info->lm_addr;

  1445.   /* Read the new objects.  */
  1446.   if (info->using_xfer)
  1447.     {
  1448.       struct svr4_library_list library_list;
  1449.       char annex[64];

  1450.       xsnprintf (annex, sizeof (annex), "start=%s;prev=%s",
  1451.                  phex_nz (lm, sizeof (lm)),
  1452.                  phex_nz (prev_lm, sizeof (prev_lm)));
  1453.       if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
  1454.         return 0;

  1455.       tail->next = library_list.head;
  1456.     }
  1457.   else
  1458.     {
  1459.       struct so_list **link = &tail->next;

  1460.       /* IGNORE_FIRST may safely be set to zero here because the
  1461.          above check and deferral to solist_update_full ensures
  1462.          that this call to svr4_read_so_list will never see the
  1463.          first element.  */
  1464.       if (!svr4_read_so_list (lm, prev_lm, &link, 0))
  1465.         return 0;
  1466.     }

  1467.   return 1;
  1468. }

  1469. /* Disable the probes-based linker interface and revert to the
  1470.    original interface.  We don't reset the breakpoints as the
  1471.    ones set up for the probes-based interface are adequate.  */

  1472. static void
  1473. disable_probes_interface_cleanup (void *arg)
  1474. {
  1475.   struct svr4_info *info = get_svr4_info ();

  1476.   warning (_("Probes-based dynamic linker interface failed.\n"
  1477.              "Reverting to original interface.\n"));

  1478.   free_probes_table (info);
  1479.   free_solib_list (info);
  1480. }

  1481. /* Update the solib list as appropriate when using the
  1482.    probes-based linker interface.  Do nothing if using the
  1483.    standard interface.  */

  1484. static void
  1485. svr4_handle_solib_event (void)
  1486. {
  1487.   struct svr4_info *info = get_svr4_info ();
  1488.   struct probe_and_action *pa;
  1489.   enum probe_action action;
  1490.   struct cleanup *old_chain, *usm_chain;
  1491.   struct value *val;
  1492.   CORE_ADDR pc, debug_base, lm = 0;
  1493.   int is_initial_ns;
  1494.   struct frame_info *frame = get_current_frame ();

  1495.   /* Do nothing if not using the probes interface.  */
  1496.   if (info->probes_table == NULL)
  1497.     return;

  1498.   /* If anything goes wrong we revert to the original linker
  1499.      interface.  */
  1500.   old_chain = make_cleanup (disable_probes_interface_cleanup, NULL);

  1501.   pc = regcache_read_pc (get_current_regcache ());
  1502.   pa = solib_event_probe_at (info, pc);
  1503.   if (pa == NULL)
  1504.     {
  1505.       do_cleanups (old_chain);
  1506.       return;
  1507.     }

  1508.   action = solib_event_probe_action (pa);
  1509.   if (action == PROBES_INTERFACE_FAILED)
  1510.     {
  1511.       do_cleanups (old_chain);
  1512.       return;
  1513.     }

  1514.   if (action == DO_NOTHING)
  1515.     {
  1516.       discard_cleanups (old_chain);
  1517.       return;
  1518.     }

  1519.   /* evaluate_probe_argument looks up symbols in the dynamic linker
  1520.      using find_pc_sectionfind_pc_section is accelerated by a cache
  1521.      called the section map.  The section map is invalidated every
  1522.      time a shared library is loaded or unloaded, and if the inferior
  1523.      is generating a lot of shared library events then the section map
  1524.      will be updated every time svr4_handle_solib_event is called.
  1525.      We called find_pc_section in svr4_create_solib_event_breakpoints,
  1526.      so we can guarantee that the dynamic linker's sections are in the
  1527.      section map.  We can therefore inhibit section map updates across
  1528.      these calls to evaluate_probe_argument and save a lot of time.  */
  1529.   inhibit_section_map_updates (current_program_space);
  1530.   usm_chain = make_cleanup (resume_section_map_updates_cleanup,
  1531.                             current_program_space);

  1532.   val = evaluate_probe_argument (pa->probe, 1, frame);
  1533.   if (val == NULL)
  1534.     {
  1535.       do_cleanups (old_chain);
  1536.       return;
  1537.     }

  1538.   debug_base = value_as_address (val);
  1539.   if (debug_base == 0)
  1540.     {
  1541.       do_cleanups (old_chain);
  1542.       return;
  1543.     }

  1544.   /* Always locate the debug struct, in case it moved.  */
  1545.   info->debug_base = 0;
  1546.   if (locate_base (info) == 0)
  1547.     {
  1548.       do_cleanups (old_chain);
  1549.       return;
  1550.     }

  1551.   /* GDB does not currently support libraries loaded via dlmopen
  1552.      into namespaces other than the initial one.  We must ignore
  1553.      any namespace other than the initial namespace here until
  1554.      support for this is added to GDB.  */
  1555.   if (debug_base != info->debug_base)
  1556.     action = DO_NOTHING;

  1557.   if (action == UPDATE_OR_RELOAD)
  1558.     {
  1559.       val = evaluate_probe_argument (pa->probe, 2, frame);
  1560.       if (val != NULL)
  1561.         lm = value_as_address (val);

  1562.       if (lm == 0)
  1563.         action = FULL_RELOAD;
  1564.     }

  1565.   /* Resume section map updates.  */
  1566.   do_cleanups (usm_chain);

  1567.   if (action == UPDATE_OR_RELOAD)
  1568.     {
  1569.       if (!solist_update_incremental (info, lm))
  1570.         action = FULL_RELOAD;
  1571.     }

  1572.   if (action == FULL_RELOAD)
  1573.     {
  1574.       if (!solist_update_full (info))
  1575.         {
  1576.           do_cleanups (old_chain);
  1577.           return;
  1578.         }
  1579.     }

  1580.   discard_cleanups (old_chain);
  1581. }

  1582. /* Helper function for svr4_update_solib_event_breakpoints.  */

  1583. static int
  1584. svr4_update_solib_event_breakpoint (struct breakpoint *b, void *arg)
  1585. {
  1586.   struct bp_location *loc;

  1587.   if (b->type != bp_shlib_event)
  1588.     {
  1589.       /* Continue iterating.  */
  1590.       return 0;
  1591.     }

  1592.   for (loc = b->loc; loc != NULL; loc = loc->next)
  1593.     {
  1594.       struct svr4_info *info;
  1595.       struct probe_and_action *pa;

  1596.       info = program_space_data (loc->pspace, solib_svr4_pspace_data);
  1597.       if (info == NULL || info->probes_table == NULL)
  1598.         continue;

  1599.       pa = solib_event_probe_at (info, loc->address);
  1600.       if (pa == NULL)
  1601.         continue;

  1602.       if (pa->action == DO_NOTHING)
  1603.         {
  1604.           if (b->enable_state == bp_disabled && stop_on_solib_events)
  1605.             enable_breakpoint (b);
  1606.           else if (b->enable_state == bp_enabled && !stop_on_solib_events)
  1607.             disable_breakpoint (b);
  1608.         }

  1609.       break;
  1610.     }

  1611.   /* Continue iterating.  */
  1612.   return 0;
  1613. }

  1614. /* Enable or disable optional solib event breakpoints as appropriate.
  1615.    Called whenever stop_on_solib_events is changed.  */

  1616. static void
  1617. svr4_update_solib_event_breakpoints (void)
  1618. {
  1619.   iterate_over_breakpoints (svr4_update_solib_event_breakpoint, NULL);
  1620. }

  1621. /* Create and register solib event breakpoints.  PROBES is an array
  1622.    of NUM_PROBES elements, each of which is vector of probes.  A
  1623.    solib event breakpoint will be created and registered for each
  1624.    probe.  */

  1625. static void
  1626. svr4_create_probe_breakpoints (struct gdbarch *gdbarch,
  1627.                                VEC (probe_p) **probes,
  1628.                                struct objfile *objfile)
  1629. {
  1630.   int i;

  1631.   for (i = 0; i < NUM_PROBES; i++)
  1632.     {
  1633.       enum probe_action action = probe_info[i].action;
  1634.       struct probe *probe;
  1635.       int ix;

  1636.       for (ix = 0;
  1637.            VEC_iterate (probe_p, probes[i], ix, probe);
  1638.            ++ix)
  1639.         {
  1640.           CORE_ADDR address = get_probe_address (probe, objfile);

  1641.           create_solib_event_breakpoint (gdbarch, address);
  1642.           register_solib_event_probe (probe, address, action);
  1643.         }
  1644.     }

  1645.   svr4_update_solib_event_breakpoints ();
  1646. }

  1647. /* Both the SunOS and the SVR4 dynamic linkers call a marker function
  1648.    before and after mapping and unmapping shared libraries.  The sole
  1649.    purpose of this method is to allow debuggers to set a breakpoint so
  1650.    they can track these changes.

  1651.    Some versions of the glibc dynamic linker contain named probes
  1652.    to allow more fine grained stopping.  Given the address of the
  1653.    original marker function, this function attempts to find these
  1654.    probes, and if found, sets breakpoints on those instead.  If the
  1655.    probes aren't found, a single breakpoint is set on the original
  1656.    marker function.  */

  1657. static void
  1658. svr4_create_solib_event_breakpoints (struct gdbarch *gdbarch,
  1659.                                      CORE_ADDR address)
  1660. {
  1661.   struct obj_section *os;

  1662.   os = find_pc_section (address);
  1663.   if (os != NULL)
  1664.     {
  1665.       int with_prefix;

  1666.       for (with_prefix = 0; with_prefix <= 1; with_prefix++)
  1667.         {
  1668.           VEC (probe_p) *probes[NUM_PROBES];
  1669.           int all_probes_found = 1;
  1670.           int checked_can_use_probe_arguments = 0;
  1671.           int i;

  1672.           memset (probes, 0, sizeof (probes));
  1673.           for (i = 0; i < NUM_PROBES; i++)
  1674.             {
  1675.               const char *name = probe_info[i].name;
  1676.               struct probe *p;
  1677.               char buf[32];

  1678.               /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4
  1679.                  shipped with an early version of the probes code in
  1680.                  which the probes' names were prefixed with "rtld_"
  1681.                  and the "map_failed" probe did not exist.  The
  1682.                  locations of the probes are otherwise the same, so
  1683.                  we check for probes with prefixed names if probes
  1684.                  with unprefixed names are not present.  */
  1685.               if (with_prefix)
  1686.                 {
  1687.                   xsnprintf (buf, sizeof (buf), "rtld_%s", name);
  1688.                   name = buf;
  1689.                 }

  1690.               probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);

  1691.               /* The "map_failed" probe did not exist in early
  1692.                  versions of the probes code in which the probes'
  1693.                  names were prefixed with "rtld_".  */
  1694.               if (strcmp (name, "rtld_map_failed") == 0)
  1695.                 continue;

  1696.               if (VEC_empty (probe_p, probes[i]))
  1697.                 {
  1698.                   all_probes_found = 0;
  1699.                   break;
  1700.                 }

  1701.               /* Ensure probe arguments can be evaluated.  */
  1702.               if (!checked_can_use_probe_arguments)
  1703.                 {
  1704.                   p = VEC_index (probe_p, probes[i], 0);
  1705.                   if (!can_evaluate_probe_arguments (p))
  1706.                     {
  1707.                       all_probes_found = 0;
  1708.                       break;
  1709.                     }
  1710.                   checked_can_use_probe_arguments = 1;
  1711.                 }
  1712.             }

  1713.           if (all_probes_found)
  1714.             svr4_create_probe_breakpoints (gdbarch, probes, os->objfile);

  1715.           for (i = 0; i < NUM_PROBES; i++)
  1716.             VEC_free (probe_p, probes[i]);

  1717.           if (all_probes_found)
  1718.             return;
  1719.         }
  1720.     }

  1721.   create_solib_event_breakpoint (gdbarch, address);
  1722. }

  1723. /* Helper function for gdb_bfd_lookup_symbol.  */

  1724. static int
  1725. cmp_name_and_sec_flags (asymbol *sym, void *data)
  1726. {
  1727.   return (strcmp (sym->name, (const char *) data) == 0
  1728.           && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0);
  1729. }
  1730. /* Arrange for dynamic linker to hit breakpoint.

  1731.    Both the SunOS and the SVR4 dynamic linkers have, as part of their
  1732.    debugger interface, support for arranging for the inferior to hit
  1733.    a breakpoint after mapping in the shared libraries.  This function
  1734.    enables that breakpoint.

  1735.    For SunOS, there is a special flag location (in_debugger) which we
  1736.    set to 1.  When the dynamic linker sees this flag set, it will set
  1737.    a breakpoint at a location known only to itself, after saving the
  1738.    original contents of that place and the breakpoint address itself,
  1739.    in it's own internal structures.  When we resume the inferior, it
  1740.    will eventually take a SIGTRAP when it runs into the breakpoint.
  1741.    We handle this (in a different place) by restoring the contents of
  1742.    the breakpointed location (which is only known after it stops),
  1743.    chasing around to locate the shared libraries that have been
  1744.    loaded, then resuming.

  1745.    For SVR4, the debugger interface structure contains a member (r_brk)
  1746.    which is statically initialized at the time the shared library is
  1747.    built, to the offset of a function (_r_debug_state) which is guaran-
  1748.    teed to be called once before mapping in a library, and again when
  1749.    the mapping is complete.  At the time we are examining this member,
  1750.    it contains only the unrelocated offset of the function, so we have
  1751.    to do our own relocation.  Later, when the dynamic linker actually
  1752.    runs, it relocates r_brk to be the actual address of _r_debug_state().

  1753.    The debugger interface structure also contains an enumeration which
  1754.    is set to either RT_ADD or RT_DELETE prior to changing the mapping,
  1755.    depending upon whether or not the library is being mapped or unmapped,
  1756.    and then set to RT_CONSISTENT after the library is mapped/unmapped.  */

  1757. static int
  1758. enable_break (struct svr4_info *info, int from_tty)
  1759. {
  1760.   struct bound_minimal_symbol msymbol;
  1761.   const char * const *bkpt_namep;
  1762.   asection *interp_sect;
  1763.   char *interp_name;
  1764.   CORE_ADDR sym_addr;

  1765.   info->interp_text_sect_low = info->interp_text_sect_high = 0;
  1766.   info->interp_plt_sect_low = info->interp_plt_sect_high = 0;

  1767.   /* If we already have a shared library list in the target, and
  1768.      r_debug contains r_brk, set the breakpoint there - this should
  1769.      mean r_brk has already been relocated.  Assume the dynamic linker
  1770.      is the object containing r_brk.  */

  1771.   solib_add (NULL, from_tty, &current_target, auto_solib_add);
  1772.   sym_addr = 0;
  1773.   if (info->debug_base && solib_svr4_r_map (info) != 0)
  1774.     sym_addr = solib_svr4_r_brk (info);

  1775.   if (sym_addr != 0)
  1776.     {
  1777.       struct obj_section *os;

  1778.       sym_addr = gdbarch_addr_bits_remove
  1779.         (target_gdbarch (), gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  1780.                                                              sym_addr,
  1781.                                                              &current_target));

  1782.       /* On at least some versions of Solaris there's a dynamic relocation
  1783.          on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
  1784.          we get control before the dynamic linker has self-relocated.
  1785.          Check if SYM_ADDR is in a known section, if it is assume we can
  1786.          trust its value.  This is just a heuristic though, it could go away
  1787.          or be replaced if it's getting in the way.

  1788.          On ARM we need to know whether the ISA of rtld_db_dlactivity (or
  1789.          however it's spelled in your particular system) is ARM or Thumb.
  1790.          That knowledge is encoded in the address, if it's Thumb the low bit
  1791.          is 1.  However, we've stripped that info above and it's not clear
  1792.          what all the consequences are of passing a non-addr_bits_remove'd
  1793.          address to svr4_create_solib_event_breakpoints.  The call to
  1794.          find_pc_section verifies we know about the address and have some
  1795.          hope of computing the right kind of breakpoint to use (via
  1796.          symbol info).  It does mean that GDB needs to be pointed at a
  1797.          non-stripped version of the dynamic linker in order to obtain
  1798.          information it already knows about.  Sigh.  */

  1799.       os = find_pc_section (sym_addr);
  1800.       if (os != NULL)
  1801.         {
  1802.           /* Record the relocated start and end address of the dynamic linker
  1803.              text and plt section for svr4_in_dynsym_resolve_code.  */
  1804.           bfd *tmp_bfd;
  1805.           CORE_ADDR load_addr;

  1806.           tmp_bfd = os->objfile->obfd;
  1807.           load_addr = ANOFFSET (os->objfile->section_offsets,
  1808.                                 SECT_OFF_TEXT (os->objfile));

  1809.           interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
  1810.           if (interp_sect)
  1811.             {
  1812.               info->interp_text_sect_low =
  1813.                 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
  1814.               info->interp_text_sect_high =
  1815.                 info->interp_text_sect_low
  1816.                 + bfd_section_size (tmp_bfd, interp_sect);
  1817.             }
  1818.           interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
  1819.           if (interp_sect)
  1820.             {
  1821.               info->interp_plt_sect_low =
  1822.                 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
  1823.               info->interp_plt_sect_high =
  1824.                 info->interp_plt_sect_low
  1825.                 + bfd_section_size (tmp_bfd, interp_sect);
  1826.             }

  1827.           svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
  1828.           return 1;
  1829.         }
  1830.     }

  1831.   /* Find the program interpreter; if not found, warn the user and drop
  1832.      into the old breakpoint at symbol code.  */
  1833.   interp_name = find_program_interpreter ();
  1834.   if (interp_name)
  1835.     {
  1836.       CORE_ADDR load_addr = 0;
  1837.       int load_addr_found = 0;
  1838.       int loader_found_in_list = 0;
  1839.       struct so_list *so;
  1840.       bfd *tmp_bfd = NULL;
  1841.       struct target_ops *tmp_bfd_target;
  1842.       volatile struct gdb_exception ex;

  1843.       sym_addr = 0;

  1844.       /* Now we need to figure out where the dynamic linker was
  1845.          loaded so that we can load its symbols and place a breakpoint
  1846.          in the dynamic linker itself.

  1847.          This address is stored on the stack.  However, I've been unable
  1848.          to find any magic formula to find it for Solaris (appears to
  1849.          be trivial on GNU/Linux).  Therefore, we have to try an alternate
  1850.          mechanism to find the dynamic linker's base address.  */

  1851.       TRY_CATCH (ex, RETURN_MASK_ALL)
  1852.         {
  1853.           tmp_bfd = solib_bfd_open (interp_name);
  1854.         }
  1855.       if (tmp_bfd == NULL)
  1856.         goto bkpt_at_symbol;

  1857.       /* Now convert the TMP_BFD into a target.  That way target, as
  1858.          well as BFD operations can be used.  */
  1859.       tmp_bfd_target = target_bfd_reopen (tmp_bfd);
  1860.       /* target_bfd_reopen acquired its own reference, so we can
  1861.          release ours now.  */
  1862.       gdb_bfd_unref (tmp_bfd);

  1863.       /* On a running target, we can get the dynamic linker's base
  1864.          address from the shared library table.  */
  1865.       so = master_so_list ();
  1866.       while (so)
  1867.         {
  1868.           if (svr4_same_1 (interp_name, so->so_original_name))
  1869.             {
  1870.               load_addr_found = 1;
  1871.               loader_found_in_list = 1;
  1872.               load_addr = lm_addr_check (so, tmp_bfd);
  1873.               break;
  1874.             }
  1875.           so = so->next;
  1876.         }

  1877.       /* If we were not able to find the base address of the loader
  1878.          from our so_list, then try using the AT_BASE auxilliary entry.  */
  1879.       if (!load_addr_found)
  1880.         if (target_auxv_search (&current_target, AT_BASE, &load_addr) > 0)
  1881.           {
  1882.             int addr_bit = gdbarch_addr_bit (target_gdbarch ());

  1883.             /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
  1884.                that `+ load_addr' will overflow CORE_ADDR width not creating
  1885.                invalid addresses like 0x101234567 for 32bit inferiors on 64bit
  1886.                GDB.  */

  1887.             if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
  1888.               {
  1889.                 CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
  1890.                 CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd,
  1891.                                                               tmp_bfd_target);

  1892.                 gdb_assert (load_addr < space_size);

  1893.                 /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
  1894.                    64bit ld.so with 32bit executable, it should not happen.  */

  1895.                 if (tmp_entry_point < space_size
  1896.                     && tmp_entry_point + load_addr >= space_size)
  1897.                   load_addr -= space_size;
  1898.               }

  1899.             load_addr_found = 1;
  1900.           }

  1901.       /* Otherwise we find the dynamic linker's base address by examining
  1902.          the current pc (which should point at the entry point for the
  1903.          dynamic linker) and subtracting the offset of the entry point.

  1904.          This is more fragile than the previous approaches, but is a good
  1905.          fallback method because it has actually been working well in
  1906.          most cases.  */
  1907.       if (!load_addr_found)
  1908.         {
  1909.           struct regcache *regcache
  1910.             = get_thread_arch_regcache (inferior_ptid, target_gdbarch ());

  1911.           load_addr = (regcache_read_pc (regcache)
  1912.                        - exec_entry_point (tmp_bfd, tmp_bfd_target));
  1913.         }

  1914.       if (!loader_found_in_list)
  1915.         {
  1916.           info->debug_loader_name = xstrdup (interp_name);
  1917.           info->debug_loader_offset_p = 1;
  1918.           info->debug_loader_offset = load_addr;
  1919.           solib_add (NULL, from_tty, &current_target, auto_solib_add);
  1920.         }

  1921.       /* Record the relocated start and end address of the dynamic linker
  1922.          text and plt section for svr4_in_dynsym_resolve_code.  */
  1923.       interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
  1924.       if (interp_sect)
  1925.         {
  1926.           info->interp_text_sect_low =
  1927.             bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
  1928.           info->interp_text_sect_high =
  1929.             info->interp_text_sect_low
  1930.             + bfd_section_size (tmp_bfd, interp_sect);
  1931.         }
  1932.       interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
  1933.       if (interp_sect)
  1934.         {
  1935.           info->interp_plt_sect_low =
  1936.             bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
  1937.           info->interp_plt_sect_high =
  1938.             info->interp_plt_sect_low
  1939.             + bfd_section_size (tmp_bfd, interp_sect);
  1940.         }

  1941.       /* Now try to set a breakpoint in the dynamic linker.  */
  1942.       for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
  1943.         {
  1944.           sym_addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name_and_sec_flags,
  1945.                                             (void *) *bkpt_namep);
  1946.           if (sym_addr != 0)
  1947.             break;
  1948.         }

  1949.       if (sym_addr != 0)
  1950.         /* Convert 'sym_addr' from a function pointer to an address.
  1951.            Because we pass tmp_bfd_target instead of the current
  1952.            target, this will always produce an unrelocated value.  */
  1953.         sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  1954.                                                        sym_addr,
  1955.                                                        tmp_bfd_target);

  1956.       /* We're done with both the temporary bfd and target.  Closing
  1957.          the target closes the underlying bfd, because it holds the
  1958.          only remaining reference.  */
  1959.       target_close (tmp_bfd_target);

  1960.       if (sym_addr != 0)
  1961.         {
  1962.           svr4_create_solib_event_breakpoints (target_gdbarch (),
  1963.                                                load_addr + sym_addr);
  1964.           xfree (interp_name);
  1965.           return 1;
  1966.         }

  1967.       /* For whatever reason we couldn't set a breakpoint in the dynamic
  1968.          linker.  Warn and drop into the old code.  */
  1969.     bkpt_at_symbol:
  1970.       xfree (interp_name);
  1971.       warning (_("Unable to find dynamic linker breakpoint function.\n"
  1972.                "GDB will be unable to debug shared library initializers\n"
  1973.                "and track explicitly loaded dynamic code."));
  1974.     }

  1975.   /* Scan through the lists of symbols, trying to look up the symbol and
  1976.      set a breakpoint there.  Terminate loop when we/if we succeed.  */

  1977.   for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
  1978.     {
  1979.       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
  1980.       if ((msymbol.minsym != NULL)
  1981.           && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
  1982.         {
  1983.           sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  1984.           sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  1985.                                                          sym_addr,
  1986.                                                          &current_target);
  1987.           svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
  1988.           return 1;
  1989.         }
  1990.     }

  1991.   if (interp_name != NULL && !current_inferior ()->attach_flag)
  1992.     {
  1993.       for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
  1994.         {
  1995.           msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
  1996.           if ((msymbol.minsym != NULL)
  1997.               && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
  1998.             {
  1999.               sym_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  2000.               sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
  2001.                                                              sym_addr,
  2002.                                                              &current_target);
  2003.               svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr);
  2004.               return 1;
  2005.             }
  2006.         }
  2007.     }
  2008.   return 0;
  2009. }

  2010. /* Implement the "special_symbol_handling" target_so_ops method.  */

  2011. static void
  2012. svr4_special_symbol_handling (void)
  2013. {
  2014.   /* Nothing to do.  */
  2015. }

  2016. /* Read the ELF program headers from ABFD.  Return the contents and
  2017.    set *PHDRS_SIZE to the size of the program headers.  */

  2018. static gdb_byte *
  2019. read_program_headers_from_bfd (bfd *abfd, int *phdrs_size)
  2020. {
  2021.   Elf_Internal_Ehdr *ehdr;
  2022.   gdb_byte *buf;

  2023.   ehdr = elf_elfheader (abfd);

  2024.   *phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
  2025.   if (*phdrs_size == 0)
  2026.     return NULL;

  2027.   buf = xmalloc (*phdrs_size);
  2028.   if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
  2029.       || bfd_bread (buf, *phdrs_size, abfd) != *phdrs_size)
  2030.     {
  2031.       xfree (buf);
  2032.       return NULL;
  2033.     }

  2034.   return buf;
  2035. }

  2036. /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
  2037.    exec_bfd.  Otherwise return 0.

  2038.    We relocate all of the sections by the same amount.  This
  2039.    behavior is mandated by recent editions of the System V ABI.
  2040.    According to the System V Application Binary Interface,
  2041.    Edition 4.1, page 5-5:

  2042.      ...  Though the system chooses virtual addresses for
  2043.      individual processes, it maintains the segments' relative
  2044.      positions.  Because position-independent code uses relative
  2045.      addressesing between segments, the difference between
  2046.      virtual addresses in memory must match the difference
  2047.      between virtual addresses in the file.  The difference
  2048.      between the virtual address of any segment in memory and
  2049.      the corresponding virtual address in the file is thus a
  2050.      single constant value for any one executable or shared
  2051.      object in a given process.  This difference is the base
  2052.      addressOne use of the base address is to relocate the
  2053.      memory image of the program during dynamic linking.

  2054.    The same language also appears in Edition 4.0 of the System V
  2055.    ABI and is left unspecified in some of the earlier editions.

  2056.    Decide if the objfile needs to be relocated.  As indicated above, we will
  2057.    only be here when execution is stopped.  But during attachment PC can be at
  2058.    arbitrary address therefore regcache_read_pc can be misleading (contrary to
  2059.    the auxv AT_ENTRY value).  Moreover for executable with interpreter section
  2060.    regcache_read_pc would point to the interpreter and not the main executable.

  2061.    So, to summarize, relocations are necessary when the start address obtained
  2062.    from the executable is different from the address in auxv AT_ENTRY entry.

  2063.    [ The astute reader will note that we also test to make sure that
  2064.      the executable in question has the DYNAMIC flag set.  It is my
  2065.      opinion that this test is unnecessary (undesirable even).  It
  2066.      was added to avoid inadvertent relocation of an executable
  2067.      whose e_type member in the ELF header is not ET_DYN.  There may
  2068.      be a time in the future when it is desirable to do relocations
  2069.      on other types of files as well in which case this condition
  2070.      should either be removed or modified to accomodate the new file
  2071.      type.  - Kevin, Nov 2000. ]  */

  2072. static int
  2073. svr4_exec_displacement (CORE_ADDR *displacementp)
  2074. {
  2075.   /* ENTRY_POINT is a possible function descriptor - before
  2076.      a call to gdbarch_convert_from_func_ptr_addr.  */
  2077.   CORE_ADDR entry_point, displacement;

  2078.   if (exec_bfd == NULL)
  2079.     return 0;

  2080.   /* Therefore for ELF it is ET_EXEC and not ET_DYN.  Both shared libraries
  2081.      being executed themselves and PIE (Position Independent Executable)
  2082.      executables are ET_DYN.  */

  2083.   if ((bfd_get_file_flags (exec_bfd) & DYNAMIC) == 0)
  2084.     return 0;

  2085.   if (target_auxv_search (&current_target, AT_ENTRY, &entry_point) <= 0)
  2086.     return 0;

  2087.   displacement = entry_point - bfd_get_start_address (exec_bfd);

  2088.   /* Verify the DISPLACEMENT candidate complies with the required page
  2089.      alignment.  It is cheaper than the program headers comparison below.  */

  2090.   if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
  2091.     {
  2092.       const struct elf_backend_data *elf = get_elf_backend_data (exec_bfd);

  2093.       /* p_align of PT_LOAD segments does not specify any alignment but
  2094.          only congruency of addresses:
  2095.            p_offset % p_align == p_vaddr % p_align
  2096.          Kernel is free to load the executable with lower alignment.  */

  2097.       if ((displacement & (elf->minpagesize - 1)) != 0)
  2098.         return 0;
  2099.     }

  2100.   /* Verify that the auxilliary vector describes the same file as exec_bfd, by
  2101.      comparing their program headers.  If the program headers in the auxilliary
  2102.      vector do not match the program headers in the executable, then we are
  2103.      looking at a different file than the one used by the kernel - for
  2104.      instance, "gdb program" connected to "gdbserver :PORT ld.so program".  */

  2105.   if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
  2106.     {
  2107.       /* Be optimistic and clear OK only if GDB was able to verify the headers
  2108.          really do not match.  */
  2109.       int phdrs_size, phdrs2_size, ok = 1;
  2110.       gdb_byte *buf, *buf2;
  2111.       int arch_size;

  2112.       buf = read_program_header (-1, &phdrs_size, &arch_size);
  2113.       buf2 = read_program_headers_from_bfd (exec_bfd, &phdrs2_size);
  2114.       if (buf != NULL && buf2 != NULL)
  2115.         {
  2116.           enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());

  2117.           /* We are dealing with three different addresses.  EXEC_BFD
  2118.              represents current address in on-disk file.  target memory content
  2119.              may be different from EXEC_BFD as the file may have been prelinked
  2120.              to a different address after the executable has been loaded.
  2121.              Moreover the address of placement in target memory can be
  2122.              different from what the program headers in target memory say -
  2123.              this is the goal of PIE.

  2124.              Detected DISPLACEMENT covers both the offsets of PIE placement and
  2125.              possible new prelink performed after start of the program.  Here
  2126.              relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
  2127.              content offset for the verification purpose.  */

  2128.           if (phdrs_size != phdrs2_size
  2129.               || bfd_get_arch_size (exec_bfd) != arch_size)
  2130.             ok = 0;
  2131.           else if (arch_size == 32
  2132.                    && phdrs_size >= sizeof (Elf32_External_Phdr)
  2133.                    && phdrs_size % sizeof (Elf32_External_Phdr) == 0)
  2134.             {
  2135.               Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
  2136.               Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
  2137.               CORE_ADDR displacement = 0;
  2138.               int i;

  2139.               /* DISPLACEMENT could be found more easily by the difference of
  2140.                  ehdr2->e_entry.  But we haven't read the ehdr yet, and we
  2141.                  already have enough information to compute that displacement
  2142.                  with what we've read.  */

  2143.               for (i = 0; i < ehdr2->e_phnum; i++)
  2144.                 if (phdr2[i].p_type == PT_LOAD)
  2145.                   {
  2146.                     Elf32_External_Phdr *phdrp;
  2147.                     gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2148.                     CORE_ADDR vaddr, paddr;
  2149.                     CORE_ADDR displacement_vaddr = 0;
  2150.                     CORE_ADDR displacement_paddr = 0;

  2151.                     phdrp = &((Elf32_External_Phdr *) buf)[i];
  2152.                     buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2153.                     buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;

  2154.                     vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
  2155.                                                       byte_order);
  2156.                     displacement_vaddr = vaddr - phdr2[i].p_vaddr;

  2157.                     paddr = extract_unsigned_integer (buf_paddr_p, 4,
  2158.                                                       byte_order);
  2159.                     displacement_paddr = paddr - phdr2[i].p_paddr;

  2160.                     if (displacement_vaddr == displacement_paddr)
  2161.                       displacement = displacement_vaddr;

  2162.                     break;
  2163.                   }

  2164.               /* Now compare BUF and BUF2 with optional DISPLACEMENT.  */

  2165.               for (i = 0; i < phdrs_size / sizeof (Elf32_External_Phdr); i++)
  2166.                 {
  2167.                   Elf32_External_Phdr *phdrp;
  2168.                   Elf32_External_Phdr *phdr2p;
  2169.                   gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2170.                   CORE_ADDR vaddr, paddr;
  2171.                   asection *plt2_asect;

  2172.                   phdrp = &((Elf32_External_Phdr *) buf)[i];
  2173.                   buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2174.                   buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
  2175.                   phdr2p = &((Elf32_External_Phdr *) buf2)[i];

  2176.                   /* PT_GNU_STACK is an exception by being never relocated by
  2177.                      prelink as its addresses are always zero.  */

  2178.                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2179.                     continue;

  2180.                   /* Check also other adjustment combinations - PR 11786.  */

  2181.                   vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
  2182.                                                     byte_order);
  2183.                   vaddr -= displacement;
  2184.                   store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);

  2185.                   paddr = extract_unsigned_integer (buf_paddr_p, 4,
  2186.                                                     byte_order);
  2187.                   paddr -= displacement;
  2188.                   store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);

  2189.                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2190.                     continue;

  2191.                   /* Strip modifies the flags and alignment of PT_GNU_RELRO.
  2192.                      CentOS-5 has problems with filesz, memsz as well.
  2193.                      See PR 11786.  */
  2194.                   if (phdr2[i].p_type == PT_GNU_RELRO)
  2195.                     {
  2196.                       Elf32_External_Phdr tmp_phdr = *phdrp;
  2197.                       Elf32_External_Phdr tmp_phdr2 = *phdr2p;

  2198.                       memset (tmp_phdr.p_filesz, 0, 4);
  2199.                       memset (tmp_phdr.p_memsz, 0, 4);
  2200.                       memset (tmp_phdr.p_flags, 0, 4);
  2201.                       memset (tmp_phdr.p_align, 0, 4);
  2202.                       memset (tmp_phdr2.p_filesz, 0, 4);
  2203.                       memset (tmp_phdr2.p_memsz, 0, 4);
  2204.                       memset (tmp_phdr2.p_flags, 0, 4);
  2205.                       memset (tmp_phdr2.p_align, 0, 4);

  2206.                       if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
  2207.                           == 0)
  2208.                         continue;
  2209.                     }

  2210.                   /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
  2211.                   plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
  2212.                   if (plt2_asect)
  2213.                     {
  2214.                       int content2;
  2215.                       gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
  2216.                       CORE_ADDR filesz;

  2217.                       content2 = (bfd_get_section_flags (exec_bfd, plt2_asect)
  2218.                                   & SEC_HAS_CONTENTS) != 0;

  2219.                       filesz = extract_unsigned_integer (buf_filesz_p, 4,
  2220.                                                          byte_order);

  2221.                       /* PLT2_ASECT is from on-disk file (exec_bfd) while
  2222.                          FILESZ is from the in-memory image.  */
  2223.                       if (content2)
  2224.                         filesz += bfd_get_section_size (plt2_asect);
  2225.                       else
  2226.                         filesz -= bfd_get_section_size (plt2_asect);

  2227.                       store_unsigned_integer (buf_filesz_p, 4, byte_order,
  2228.                                               filesz);

  2229.                       if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2230.                         continue;
  2231.                     }

  2232.                   ok = 0;
  2233.                   break;
  2234.                 }
  2235.             }
  2236.           else if (arch_size == 64
  2237.                    && phdrs_size >= sizeof (Elf64_External_Phdr)
  2238.                    && phdrs_size % sizeof (Elf64_External_Phdr) == 0)
  2239.             {
  2240.               Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
  2241.               Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
  2242.               CORE_ADDR displacement = 0;
  2243.               int i;

  2244.               /* DISPLACEMENT could be found more easily by the difference of
  2245.                  ehdr2->e_entry.  But we haven't read the ehdr yet, and we
  2246.                  already have enough information to compute that displacement
  2247.                  with what we've read.  */

  2248.               for (i = 0; i < ehdr2->e_phnum; i++)
  2249.                 if (phdr2[i].p_type == PT_LOAD)
  2250.                   {
  2251.                     Elf64_External_Phdr *phdrp;
  2252.                     gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2253.                     CORE_ADDR vaddr, paddr;
  2254.                     CORE_ADDR displacement_vaddr = 0;
  2255.                     CORE_ADDR displacement_paddr = 0;

  2256.                     phdrp = &((Elf64_External_Phdr *) buf)[i];
  2257.                     buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2258.                     buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;

  2259.                     vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
  2260.                                                       byte_order);
  2261.                     displacement_vaddr = vaddr - phdr2[i].p_vaddr;

  2262.                     paddr = extract_unsigned_integer (buf_paddr_p, 8,
  2263.                                                       byte_order);
  2264.                     displacement_paddr = paddr - phdr2[i].p_paddr;

  2265.                     if (displacement_vaddr == displacement_paddr)
  2266.                       displacement = displacement_vaddr;

  2267.                     break;
  2268.                   }

  2269.               /* Now compare BUF and BUF2 with optional DISPLACEMENT.  */

  2270.               for (i = 0; i < phdrs_size / sizeof (Elf64_External_Phdr); i++)
  2271.                 {
  2272.                   Elf64_External_Phdr *phdrp;
  2273.                   Elf64_External_Phdr *phdr2p;
  2274.                   gdb_byte *buf_vaddr_p, *buf_paddr_p;
  2275.                   CORE_ADDR vaddr, paddr;
  2276.                   asection *plt2_asect;

  2277.                   phdrp = &((Elf64_External_Phdr *) buf)[i];
  2278.                   buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
  2279.                   buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
  2280.                   phdr2p = &((Elf64_External_Phdr *) buf2)[i];

  2281.                   /* PT_GNU_STACK is an exception by being never relocated by
  2282.                      prelink as its addresses are always zero.  */

  2283.                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2284.                     continue;

  2285.                   /* Check also other adjustment combinations - PR 11786.  */

  2286.                   vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
  2287.                                                     byte_order);
  2288.                   vaddr -= displacement;
  2289.                   store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);

  2290.                   paddr = extract_unsigned_integer (buf_paddr_p, 8,
  2291.                                                     byte_order);
  2292.                   paddr -= displacement;
  2293.                   store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);

  2294.                   if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2295.                     continue;

  2296.                   /* Strip modifies the flags and alignment of PT_GNU_RELRO.
  2297.                      CentOS-5 has problems with filesz, memsz as well.
  2298.                      See PR 11786.  */
  2299.                   if (phdr2[i].p_type == PT_GNU_RELRO)
  2300.                     {
  2301.                       Elf64_External_Phdr tmp_phdr = *phdrp;
  2302.                       Elf64_External_Phdr tmp_phdr2 = *phdr2p;

  2303.                       memset (tmp_phdr.p_filesz, 0, 8);
  2304.                       memset (tmp_phdr.p_memsz, 0, 8);
  2305.                       memset (tmp_phdr.p_flags, 0, 4);
  2306.                       memset (tmp_phdr.p_align, 0, 8);
  2307.                       memset (tmp_phdr2.p_filesz, 0, 8);
  2308.                       memset (tmp_phdr2.p_memsz, 0, 8);
  2309.                       memset (tmp_phdr2.p_flags, 0, 4);
  2310.                       memset (tmp_phdr2.p_align, 0, 8);

  2311.                       if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
  2312.                           == 0)
  2313.                         continue;
  2314.                     }

  2315.                   /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
  2316.                   plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
  2317.                   if (plt2_asect)
  2318.                     {
  2319.                       int content2;
  2320.                       gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
  2321.                       CORE_ADDR filesz;

  2322.                       content2 = (bfd_get_section_flags (exec_bfd, plt2_asect)
  2323.                                   & SEC_HAS_CONTENTS) != 0;

  2324.                       filesz = extract_unsigned_integer (buf_filesz_p, 8,
  2325.                                                          byte_order);

  2326.                       /* PLT2_ASECT is from on-disk file (exec_bfd) while
  2327.                          FILESZ is from the in-memory image.  */
  2328.                       if (content2)
  2329.                         filesz += bfd_get_section_size (plt2_asect);
  2330.                       else
  2331.                         filesz -= bfd_get_section_size (plt2_asect);

  2332.                       store_unsigned_integer (buf_filesz_p, 8, byte_order,
  2333.                                               filesz);

  2334.                       if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
  2335.                         continue;
  2336.                     }

  2337.                   ok = 0;
  2338.                   break;
  2339.                 }
  2340.             }
  2341.           else
  2342.             ok = 0;
  2343.         }

  2344.       xfree (buf);
  2345.       xfree (buf2);

  2346.       if (!ok)
  2347.         return 0;
  2348.     }

  2349.   if (info_verbose)
  2350.     {
  2351.       /* It can be printed repeatedly as there is no easy way to check
  2352.          the executable symbols/file has been already relocated to
  2353.          displacement.  */

  2354.       printf_unfiltered (_("Using PIE (Position Independent Executable) "
  2355.                            "displacement %s for \"%s\".\n"),
  2356.                          paddress (target_gdbarch (), displacement),
  2357.                          bfd_get_filename (exec_bfd));
  2358.     }

  2359.   *displacementp = displacement;
  2360.   return 1;
  2361. }

  2362. /* Relocate the main executable.  This function should be called upon
  2363.    stopping the inferior process at the entry point to the program.
  2364.    The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
  2365.    different, the main executable is relocated by the proper amount.  */

  2366. static void
  2367. svr4_relocate_main_executable (void)
  2368. {
  2369.   CORE_ADDR displacement;

  2370.   /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
  2371.      probably contains the offsets computed using the PIE displacement
  2372.      from the previous run, which of course are irrelevant for this run.
  2373.      So we need to determine the new PIE displacement and recompute the
  2374.      section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
  2375.      already contains pre-computed offsets.

  2376.      If we cannot compute the PIE displacement, either:

  2377.        - The executable is not PIE.

  2378.        - SYMFILE_OBJFILE does not match the executable started in the target.
  2379.          This can happen for main executable symbols loaded at the host while
  2380.          `ld.so --ld-args main-executable' is loaded in the target.

  2381.      Then we leave the section offsets untouched and use them as is for
  2382.      this run.  Either:

  2383.        - These section offsets were properly reset earlier, and thus
  2384.          already contain the correct values.  This can happen for instance
  2385.          when reconnecting via the remote protocol to a target that supports
  2386.          the `qOffsets' packet.

  2387.        - The section offsets were not reset earlier, and the best we can
  2388.          hope is that the old offsets are still applicable to the new run.  */

  2389.   if (! svr4_exec_displacement (&displacement))
  2390.     return;

  2391.   /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
  2392.      addresses.  */

  2393.   if (symfile_objfile)
  2394.     {
  2395.       struct section_offsets *new_offsets;
  2396.       int i;

  2397.       new_offsets = alloca (symfile_objfile->num_sections
  2398.                             * sizeof (*new_offsets));

  2399.       for (i = 0; i < symfile_objfile->num_sections; i++)
  2400.         new_offsets->offsets[i] = displacement;

  2401.       objfile_relocate (symfile_objfile, new_offsets);
  2402.     }
  2403.   else if (exec_bfd)
  2404.     {
  2405.       asection *asect;

  2406.       for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
  2407.         exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
  2408.                                   (bfd_section_vma (exec_bfd, asect)
  2409.                                    + displacement));
  2410.     }
  2411. }

  2412. /* Implement the "create_inferior_hook" target_solib_ops method.

  2413.    For SVR4 executables, this first instruction is either the first
  2414.    instruction in the dynamic linker (for dynamically linked
  2415.    executables) or the instruction at "start" for statically linked
  2416.    executables.  For dynamically linked executables, the system
  2417.    first exec's /lib/libc.so.N, which contains the dynamic linker,
  2418.    and starts it running.  The dynamic linker maps in any needed
  2419.    shared libraries, maps in the actual user executable, and then
  2420.    jumps to "start" in the user executable.

  2421.    We can arrange to cooperate with the dynamic linker to discover the
  2422.    names of shared libraries that are dynamically linked, and the base
  2423.    addresses to which they are linked.

  2424.    This function is responsible for discovering those names and
  2425.    addresses, and saving sufficient information about them to allow
  2426.    their symbols to be read at a later time.  */

  2427. static void
  2428. svr4_solib_create_inferior_hook (int from_tty)
  2429. {
  2430.   struct svr4_info *info;

  2431.   info = get_svr4_info ();

  2432.   /* Clear the probes-based interface's state.  */
  2433.   free_probes_table (info);
  2434.   free_solib_list (info);

  2435.   /* Relocate the main executable if necessary.  */
  2436.   svr4_relocate_main_executable ();

  2437.   /* No point setting a breakpoint in the dynamic linker if we can't
  2438.      hit it (e.g., a core file, or a trace file).  */
  2439.   if (!target_has_execution)
  2440.     return;

  2441.   if (!svr4_have_link_map_offsets ())
  2442.     return;

  2443.   if (!enable_break (info, from_tty))
  2444.     return;
  2445. }

  2446. static void
  2447. svr4_clear_solib (void)
  2448. {
  2449.   struct svr4_info *info;

  2450.   info = get_svr4_info ();
  2451.   info->debug_base = 0;
  2452.   info->debug_loader_offset_p = 0;
  2453.   info->debug_loader_offset = 0;
  2454.   xfree (info->debug_loader_name);
  2455.   info->debug_loader_name = NULL;
  2456. }

  2457. /* Clear any bits of ADDR that wouldn't fit in a target-format
  2458.    data pointer.  "Data pointer" here refers to whatever sort of
  2459.    address the dynamic linker uses to manage its sections.  At the
  2460.    moment, we don't support shared libraries on any processors where
  2461.    code and data pointers are different sizes.

  2462.    This isn't really the right solution.  What we really need here is
  2463.    a way to do arithmetic on CORE_ADDR values that respects the
  2464.    natural pointer/address correspondence.  (For example, on the MIPS,
  2465.    converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
  2466.    sign-extend the value.  There, simply truncating the bits above
  2467.    gdbarch_ptr_bit, as we do below, is no good.)  This should probably
  2468.    be a new gdbarch method or something.  */
  2469. static CORE_ADDR
  2470. svr4_truncate_ptr (CORE_ADDR addr)
  2471. {
  2472.   if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
  2473.     /* We don't need to truncate anything, and the bit twiddling below
  2474.        will fail due to overflow problems.  */
  2475.     return addr;
  2476.   else
  2477.     return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
  2478. }


  2479. static void
  2480. svr4_relocate_section_addresses (struct so_list *so,
  2481.                                  struct target_section *sec)
  2482. {
  2483.   bfd *abfd = sec->the_bfd_section->owner;

  2484.   sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
  2485.   sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
  2486. }


  2487. /* Architecture-specific operations.  */

  2488. /* Per-architecture data key.  */
  2489. static struct gdbarch_data *solib_svr4_data;

  2490. struct solib_svr4_ops
  2491. {
  2492.   /* Return a description of the layout of `struct link_map'.  */
  2493.   struct link_map_offsets *(*fetch_link_map_offsets)(void);
  2494. };

  2495. /* Return a default for the architecture-specific operations.  */

  2496. static void *
  2497. solib_svr4_init (struct obstack *obstack)
  2498. {
  2499.   struct solib_svr4_ops *ops;

  2500.   ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops);
  2501.   ops->fetch_link_map_offsets = NULL;
  2502.   return ops;
  2503. }

  2504. /* Set the architecture-specific `struct link_map_offsets' fetcher for
  2505.    GDBARCH to FLMO.  Also, install SVR4 solib_ops into GDBARCH.  */

  2506. void
  2507. set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
  2508.                                        struct link_map_offsets *(*flmo) (void))
  2509. {
  2510.   struct solib_svr4_ops *ops = gdbarch_data (gdbarch, solib_svr4_data);

  2511.   ops->fetch_link_map_offsets = flmo;

  2512.   set_solib_ops (gdbarch, &svr4_so_ops);
  2513. }

  2514. /* Fetch a link_map_offsets structure using the architecture-specific
  2515.    `struct link_map_offsets' fetcher.  */

  2516. static struct link_map_offsets *
  2517. svr4_fetch_link_map_offsets (void)
  2518. {
  2519.   struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch (), solib_svr4_data);

  2520.   gdb_assert (ops->fetch_link_map_offsets);
  2521.   return ops->fetch_link_map_offsets ();
  2522. }

  2523. /* Return 1 if a link map offset fetcher has been defined, 0 otherwise.  */

  2524. static int
  2525. svr4_have_link_map_offsets (void)
  2526. {
  2527.   struct solib_svr4_ops *ops = gdbarch_data (target_gdbarch (), solib_svr4_data);

  2528.   return (ops->fetch_link_map_offsets != NULL);
  2529. }


  2530. /* Most OS'es that have SVR4-style ELF dynamic libraries define a
  2531.    `struct r_debug' and a `struct link_map' that are binary compatible
  2532.    with the origional SVR4 implementation.  */

  2533. /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
  2534.    for an ILP32 SVR4 system.  */

  2535. struct link_map_offsets *
  2536. svr4_ilp32_fetch_link_map_offsets (void)
  2537. {
  2538.   static struct link_map_offsets lmo;
  2539.   static struct link_map_offsets *lmp = NULL;

  2540.   if (lmp == NULL)
  2541.     {
  2542.       lmp = &lmo;

  2543.       lmo.r_version_offset = 0;
  2544.       lmo.r_version_size = 4;
  2545.       lmo.r_map_offset = 4;
  2546.       lmo.r_brk_offset = 8;
  2547.       lmo.r_ldsomap_offset = 20;

  2548.       /* Everything we need is in the first 20 bytes.  */
  2549.       lmo.link_map_size = 20;
  2550.       lmo.l_addr_offset = 0;
  2551.       lmo.l_name_offset = 4;
  2552.       lmo.l_ld_offset = 8;
  2553.       lmo.l_next_offset = 12;
  2554.       lmo.l_prev_offset = 16;
  2555.     }

  2556.   return lmp;
  2557. }

  2558. /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
  2559.    for an LP64 SVR4 system.  */

  2560. struct link_map_offsets *
  2561. svr4_lp64_fetch_link_map_offsets (void)
  2562. {
  2563.   static struct link_map_offsets lmo;
  2564.   static struct link_map_offsets *lmp = NULL;

  2565.   if (lmp == NULL)
  2566.     {
  2567.       lmp = &lmo;

  2568.       lmo.r_version_offset = 0;
  2569.       lmo.r_version_size = 4;
  2570.       lmo.r_map_offset = 8;
  2571.       lmo.r_brk_offset = 16;
  2572.       lmo.r_ldsomap_offset = 40;

  2573.       /* Everything we need is in the first 40 bytes.  */
  2574.       lmo.link_map_size = 40;
  2575.       lmo.l_addr_offset = 0;
  2576.       lmo.l_name_offset = 8;
  2577.       lmo.l_ld_offset = 16;
  2578.       lmo.l_next_offset = 24;
  2579.       lmo.l_prev_offset = 32;
  2580.     }

  2581.   return lmp;
  2582. }


  2583. struct target_so_ops svr4_so_ops;

  2584. /* Lookup global symbol for ELF DSOs linked with -Bsymbolic.  Those DSOs have a
  2585.    different rule for symbol lookup.  The lookup begins here in the DSO, not in
  2586.    the main executable.  */

  2587. static struct symbol *
  2588. elf_lookup_lib_symbol (struct objfile *objfile,
  2589.                        const char *name,
  2590.                        const domain_enum domain)
  2591. {
  2592.   bfd *abfd;

  2593.   if (objfile == symfile_objfile)
  2594.     abfd = exec_bfd;
  2595.   else
  2596.     {
  2597.       /* OBJFILE should have been passed as the non-debug one.  */
  2598.       gdb_assert (objfile->separate_debug_objfile_backlink == NULL);

  2599.       abfd = objfile->obfd;
  2600.     }

  2601.   if (abfd == NULL || scan_dyntag (DT_SYMBOLIC, abfd, NULL) != 1)
  2602.     return NULL;

  2603.   return lookup_global_symbol_from_objfile (objfile, name, domain);
  2604. }

  2605. extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */

  2606. void
  2607. _initialize_svr4_solib (void)
  2608. {
  2609.   solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init);
  2610.   solib_svr4_pspace_data
  2611.     = register_program_space_data_with_cleanup (NULL, svr4_pspace_data_cleanup);

  2612.   svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
  2613.   svr4_so_ops.free_so = svr4_free_so;
  2614.   svr4_so_ops.clear_so = svr4_clear_so;
  2615.   svr4_so_ops.clear_solib = svr4_clear_solib;
  2616.   svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
  2617.   svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
  2618.   svr4_so_ops.current_sos = svr4_current_sos;
  2619.   svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
  2620.   svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
  2621.   svr4_so_ops.bfd_open = solib_bfd_open;
  2622.   svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol;
  2623.   svr4_so_ops.same = svr4_same;
  2624.   svr4_so_ops.keep_data_in_core = svr4_keep_data_in_core;
  2625.   svr4_so_ops.update_breakpoints = svr4_update_solib_event_breakpoints;
  2626.   svr4_so_ops.handle_event = svr4_handle_solib_event;
  2627. }