home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / solib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-10  |  43.3 KB  |  1,595 lines

  1. /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
  2.    Copyright 1990, 1991, 1992, 1993, 1994, 1995
  3.    Free Software Foundation, Inc.
  4.    
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include "defs.h"
  23.  
  24. #include <sys/types.h>
  25. #include <signal.h>
  26. #include <string.h>
  27. #include <sys/param.h>
  28. #include <fcntl.h>
  29.  
  30. #ifndef SVR4_SHARED_LIBS
  31.  /* SunOS shared libs need the nlist structure.  */
  32. #include <a.out.h> 
  33. #else
  34. #include "elf/external.h"
  35. #endif
  36.  
  37. #include <link.h>
  38.  
  39. #include "symtab.h"
  40. #include "bfd.h"
  41. #include "symfile.h"
  42. #include "objfiles.h"
  43. #include "gdbcore.h"
  44. #include "command.h"
  45. #include "target.h"
  46. #include "frame.h"
  47. #include "regex.h"
  48. #include "inferior.h"
  49. #include "language.h"
  50. #include "gdbcmd.h"
  51.  
  52. #define MAX_PATH_SIZE 256        /* FIXME: Should be dynamic */
  53.  
  54. /* On SVR4 systems, for the initial implementation, use some runtime startup
  55.    symbol as the "startup mapping complete" breakpoint address.  The models
  56.    for SunOS and SVR4 dynamic linking debugger support are different in that
  57.    SunOS hits one breakpoint when all mapping is complete while using the SVR4
  58.    debugger support takes two breakpoint hits for each file mapped, and
  59.    there is no way to know when the "last" one is hit.  Both these
  60.    mechanisms should be tied to a "breakpoint service routine" that
  61.    gets automatically executed whenever one of the breakpoints indicating
  62.    a change in mapping is hit.  This is a future enhancement.  (FIXME) */
  63.  
  64. #define BKPT_AT_SYMBOL 1
  65.  
  66. #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
  67. static char *bkpt_names[] = {
  68. #ifdef SOLIB_BKPT_NAME
  69.   SOLIB_BKPT_NAME,        /* Prefer configured name if it exists. */
  70. #endif
  71.   "_start",
  72.   "main",
  73.   NULL
  74. };
  75. #endif
  76.  
  77. /* Symbols which are used to locate the base of the link map structures. */
  78.  
  79. #ifndef SVR4_SHARED_LIBS
  80. static char *debug_base_symbols[] = {
  81.   "_DYNAMIC",
  82.   NULL
  83. };
  84. #endif
  85.  
  86. /* local data declarations */
  87.  
  88. /* If true, then shared library symbols will be added automatically
  89.    when the inferior is created.  This is almost always what users
  90.    will want to have happen; but for very large programs, the startup
  91.    time will be excessive, and so if this is a problem, the user can
  92.    clear this flag and then add the shared library symbols as needed.
  93.    Note that there is a potential for confusion, since if the shared
  94.    library symbols are not loaded, commands like "info fun" will *not*
  95.    report all the functions that are actually present.  */
  96.    
  97. int auto_solib_add_at_startup = 1;
  98.  
  99. #ifndef SVR4_SHARED_LIBS
  100.  
  101. #define LM_ADDR(so) ((so) -> lm.lm_addr)
  102. #define LM_NEXT(so) ((so) -> lm.lm_next)
  103. #define LM_NAME(so) ((so) -> lm.lm_name)
  104. /* Test for first link map entry; first entry is a shared library. */
  105. #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
  106. static struct link_dynamic dynamic_copy;
  107. static struct link_dynamic_2 ld_2_copy;
  108. static struct ld_debug debug_copy;
  109. static CORE_ADDR debug_addr;
  110. static CORE_ADDR flag_addr;
  111.  
  112. #else    /* SVR4_SHARED_LIBS */
  113.  
  114. #define LM_ADDR(so) ((so) -> lm.l_addr)
  115. #define LM_NEXT(so) ((so) -> lm.l_next)
  116. #define LM_NAME(so) ((so) -> lm.l_name)
  117. /* Test for first link map entry; first entry is the exec-file. */
  118. #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
  119. static struct r_debug debug_copy;
  120. char shadow_contents[BREAKPOINT_MAX];    /* Stash old bkpt addr contents */
  121.  
  122. #endif    /* !SVR4_SHARED_LIBS */
  123.  
  124. struct so_list {
  125.   struct so_list *next;            /* next structure in linked list */
  126.   struct link_map lm;            /* copy of link map from inferior */
  127.   struct link_map *lmaddr;        /* addr in inferior lm was read from */
  128.   CORE_ADDR lmend;            /* upper addr bound of mapped object */
  129.   char so_name[MAX_PATH_SIZE];        /* shared object lib name (FIXME) */
  130.   char symbols_loaded;            /* flag: symbols read in yet? */
  131.   char from_tty;            /* flag: print msgs? */
  132.   struct objfile *objfile;        /* objfile for loaded lib */
  133.   struct section_table *sections;
  134.   struct section_table *sections_end;
  135.   struct section_table *textsection;
  136.   bfd *abfd;
  137. };
  138.  
  139. static struct so_list *so_list_head;    /* List of known shared objects */
  140. static CORE_ADDR debug_base;        /* Base of dynamic linker structures */
  141. static CORE_ADDR breakpoint_addr;    /* Address where end bkpt is set */
  142.  
  143. extern int
  144. fdmatch PARAMS ((int, int));        /* In libiberty */
  145.  
  146. /* Local function prototypes */
  147.  
  148. static void
  149. special_symbol_handling PARAMS ((struct so_list *));
  150.  
  151. static void
  152. sharedlibrary_command PARAMS ((char *, int));
  153.  
  154. static int
  155. enable_break PARAMS ((void));
  156.  
  157. static int
  158. disable_break PARAMS ((void));
  159.  
  160. static void
  161. info_sharedlibrary_command PARAMS ((char *, int));
  162.  
  163. static int
  164. symbol_add_stub PARAMS ((char *));
  165.  
  166. static struct so_list *
  167. find_solib PARAMS ((struct so_list *));
  168.  
  169. static struct link_map *
  170. first_link_map_member PARAMS ((void));
  171.  
  172. static CORE_ADDR
  173. locate_base PARAMS ((void));
  174.  
  175. static void
  176. solib_map_sections PARAMS ((struct so_list *));
  177.  
  178. #ifdef SVR4_SHARED_LIBS
  179.  
  180. static CORE_ADDR
  181. elf_locate_base PARAMS ((void));
  182.  
  183. #else
  184.  
  185. static void
  186. allocate_rt_common_objfile PARAMS ((void));
  187.  
  188. static void
  189. solib_add_common_symbols PARAMS ((struct rtc_symb *));
  190.  
  191. #endif
  192.  
  193. /*
  194.  
  195. LOCAL FUNCTION
  196.  
  197.     solib_map_sections -- open bfd and build sections for shared lib
  198.  
  199. SYNOPSIS
  200.  
  201.     static void solib_map_sections (struct so_list *so)
  202.  
  203. DESCRIPTION
  204.  
  205.     Given a pointer to one of the shared objects in our list
  206.     of mapped objects, use the recorded name to open a bfd
  207.     descriptor for the object, build a section table, and then
  208.     relocate all the section addresses by the base address at
  209.     which the shared object was mapped.
  210.  
  211. FIXMES
  212.  
  213.     In most (all?) cases the shared object file name recorded in the
  214.     dynamic linkage tables will be a fully qualified pathname.  For
  215.     cases where it isn't, do we really mimic the systems search
  216.     mechanism correctly in the below code (particularly the tilde
  217.     expansion stuff?).
  218.  */
  219.  
  220. static void
  221. solib_map_sections (so)
  222.      struct so_list *so;
  223. {
  224.   char *filename;
  225.   char *scratch_pathname;
  226.   int scratch_chan;
  227.   struct section_table *p;
  228.   struct cleanup *old_chain;
  229.   bfd *abfd;
  230.   
  231.   filename = tilde_expand (so -> so_name);
  232.   old_chain = make_cleanup (free, filename);
  233.   
  234.   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  235.             &scratch_pathname);
  236.   if (scratch_chan < 0)
  237.     {
  238.       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
  239.                 O_RDONLY, 0, &scratch_pathname);
  240.     }
  241.   if (scratch_chan < 0)
  242.     {
  243.       perror_with_name (filename);
  244.     }
  245.   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
  246.  
  247.   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
  248.   if (!abfd)
  249.     {
  250.       close (scratch_chan);
  251.       error ("Could not open `%s' as an executable file: %s",
  252.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  253.     }
  254.   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
  255.   so -> abfd = abfd;
  256.   abfd -> cacheable = true;
  257.  
  258.   if (!bfd_check_format (abfd, bfd_object))
  259.     {
  260.       error ("\"%s\": not in executable format: %s.",
  261.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  262.     }
  263.   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
  264.     {
  265.       error ("Can't find the file sections in `%s': %s", 
  266.          bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  267.     }
  268.  
  269.   for (p = so -> sections; p < so -> sections_end; p++)
  270.     {
  271.       /* Relocate the section binding addresses as recorded in the shared
  272.      object's file by the base address to which the object was actually
  273.      mapped. */
  274.       p -> addr += (CORE_ADDR) LM_ADDR (so);
  275.       p -> endaddr += (CORE_ADDR) LM_ADDR (so);
  276.       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
  277.       if (STREQ (p -> the_bfd_section -> name, ".text"))
  278.     {
  279.       so -> textsection = p;
  280.     }
  281.     }
  282.  
  283.   /* Free the file names, close the file now.  */
  284.   do_cleanups (old_chain);
  285. }
  286.  
  287. #ifndef SVR4_SHARED_LIBS
  288.  
  289. /* Allocate the runtime common object file.  */
  290.  
  291. static void
  292. allocate_rt_common_objfile ()
  293. {
  294.   struct objfile *objfile;
  295.   struct objfile *last_one;
  296.  
  297.   objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
  298.   memset (objfile, 0, sizeof (struct objfile));
  299.   objfile -> md = NULL;
  300.   obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
  301.                   free);
  302.   obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
  303.                   free);
  304.   obstack_specify_allocation (&objfile -> type_obstack, 0, 0, xmalloc,
  305.                   free);
  306.   objfile -> name = mstrsave (objfile -> md, "rt_common");
  307.  
  308.   /* Add this file onto the tail of the linked list of other such files. */
  309.  
  310.   objfile -> next = NULL;
  311.   if (object_files == NULL)
  312.     object_files = objfile;
  313.   else
  314.     {
  315.       for (last_one = object_files;
  316.        last_one -> next;
  317.        last_one = last_one -> next);
  318.       last_one -> next = objfile;
  319.     }
  320.  
  321.   rt_common_objfile = objfile;
  322. }
  323.  
  324. /* Read all dynamically loaded common symbol definitions from the inferior
  325.    and put them into the minimal symbol table for the runtime common
  326.    objfile.  */
  327.  
  328. static void
  329. solib_add_common_symbols (rtc_symp)
  330.     struct rtc_symb *rtc_symp;
  331. {
  332.   struct rtc_symb inferior_rtc_symb;
  333.   struct nlist inferior_rtc_nlist;
  334.   int len;
  335.   char *name;
  336.   char *origname;
  337.  
  338.   /* Remove any runtime common symbols from previous runs.  */
  339.  
  340.   if (rt_common_objfile != NULL && rt_common_objfile -> minimal_symbol_count)
  341.     {
  342.       obstack_free (&rt_common_objfile -> symbol_obstack, 0);
  343.       obstack_specify_allocation (&rt_common_objfile -> symbol_obstack, 0, 0,
  344.                   xmalloc, free);
  345.       rt_common_objfile -> minimal_symbol_count = 0;
  346.       rt_common_objfile -> msymbols = NULL;
  347.     }
  348.  
  349.   init_minimal_symbol_collection ();
  350.   make_cleanup (discard_minimal_symbols, 0);
  351.  
  352.   while (rtc_symp)
  353.     {
  354.       read_memory ((CORE_ADDR) rtc_symp,
  355.            (char *) &inferior_rtc_symb,
  356.            sizeof (inferior_rtc_symb));
  357.       read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
  358.            (char *) &inferior_rtc_nlist,
  359.            sizeof(inferior_rtc_nlist));
  360.       if (inferior_rtc_nlist.n_type == N_COMM)
  361.     {
  362.       /* FIXME: The length of the symbol name is not available, but in the
  363.          current implementation the common symbol is allocated immediately
  364.          behind the name of the symbol. */
  365.       len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
  366.  
  367.       origname = name = xmalloc (len);
  368.       read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
  369.  
  370.       /* Allocate the runtime common objfile if necessary. */
  371.       if (rt_common_objfile == NULL)
  372.         allocate_rt_common_objfile ();
  373.  
  374.       name = obsavestring (name, strlen (name),
  375.                    &rt_common_objfile -> symbol_obstack);
  376.       prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
  377.                       mst_bss, rt_common_objfile);
  378.       free (origname);
  379.     }
  380.       rtc_symp = inferior_rtc_symb.rtc_next;
  381.     }
  382.  
  383.   /* Install any minimal symbols that have been collected as the current
  384.      minimal symbols for the runtime common objfile.  */
  385.  
  386.   install_minimal_symbols (rt_common_objfile);
  387. }
  388.  
  389. #endif    /* SVR4_SHARED_LIBS */
  390.  
  391.  
  392. #ifdef SVR4_SHARED_LIBS
  393.  
  394. #ifdef HANDLE_SVR4_EXEC_EMULATORS
  395.  
  396. /*
  397.     Solaris BCP (the part of Solaris which allows it to run SunOS4
  398.     a.out files) throws in another wrinkle. Solaris does not fill
  399.     in the usual a.out link map structures when running BCP programs,
  400.     the only way to get at them is via groping around in the dynamic
  401.     linker.
  402.     The dynamic linker and it's structures are located in the shared
  403.     C library, which gets run as the executable's "interpreter" by
  404.     the kernel.
  405.  
  406.     Note that we can assume nothing about the process state at the time
  407.     we need to find these structures.  We may be stopped on the first
  408.     instruction of the interpreter (C shared library), the first
  409.     instruction of the executable itself, or somewhere else entirely
  410.     (if we attached to the process for example).
  411. */
  412.  
  413. static char *debug_base_symbols[] = {
  414.   "r_debug",    /* Solaris 2.3 */
  415.   "_r_debug",    /* Solaris 2.1, 2.2 */
  416.   NULL
  417. };
  418.  
  419. static int
  420. look_for_base PARAMS ((int, CORE_ADDR));
  421.  
  422. static CORE_ADDR
  423. bfd_lookup_symbol PARAMS ((bfd *, char *));
  424.  
  425. /*
  426.  
  427. LOCAL FUNCTION
  428.  
  429.     bfd_lookup_symbol -- lookup the value for a specific symbol
  430.  
  431. SYNOPSIS
  432.  
  433.     CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
  434.  
  435. DESCRIPTION
  436.  
  437.     An expensive way to lookup the value of a single symbol for
  438.     bfd's that are only temporary anyway.  This is used by the
  439.     shared library support to find the address of the debugger
  440.     interface structures in the shared library.
  441.  
  442.     Note that 0 is specifically allowed as an error return (no
  443.     such symbol).
  444. */
  445.  
  446. static CORE_ADDR
  447. bfd_lookup_symbol (abfd, symname)
  448.      bfd *abfd;
  449.      char *symname;
  450. {
  451.   unsigned int storage_needed;
  452.   asymbol *sym;
  453.   asymbol **symbol_table;
  454.   unsigned int number_of_symbols;
  455.   unsigned int i;
  456.   struct cleanup *back_to;
  457.   CORE_ADDR symaddr = 0;
  458.   
  459.   storage_needed = bfd_get_symtab_upper_bound (abfd);
  460.  
  461.   if (storage_needed > 0)
  462.     {
  463.       symbol_table = (asymbol **) xmalloc (storage_needed);
  464.       back_to = make_cleanup (free, (PTR)symbol_table);
  465.       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
  466.   
  467.       for (i = 0; i < number_of_symbols; i++)
  468.     {
  469.       sym = *symbol_table++;
  470.       if (STREQ (sym -> name, symname))
  471.         {
  472.           /* Bfd symbols are section relative. */
  473.           symaddr = sym -> value + sym -> section -> vma;
  474.           break;
  475.         }
  476.     }
  477.       do_cleanups (back_to);
  478.     }
  479.   return (symaddr);
  480. }
  481.  
  482. /*
  483.  
  484. LOCAL FUNCTION
  485.  
  486.     look_for_base -- examine file for each mapped address segment
  487.  
  488. SYNOPSYS
  489.  
  490.     static int look_for_base (int fd, CORE_ADDR baseaddr)
  491.  
  492. DESCRIPTION
  493.  
  494.     This function is passed to proc_iterate_over_mappings, which
  495.     causes it to get called once for each mapped address space, with
  496.     an open file descriptor for the file mapped to that space, and the
  497.     base address of that mapped space.
  498.  
  499.     Our job is to find the debug base symbol in the file that this
  500.     fd is open on, if it exists, and if so, initialize the dynamic
  501.     linker structure base address debug_base.
  502.  
  503.     Note that this is a computationally expensive proposition, since
  504.     we basically have to open a bfd on every call, so we specifically
  505.     avoid opening the exec file.
  506.  */
  507.  
  508. static int
  509. look_for_base (fd, baseaddr)
  510.      int fd;
  511.      CORE_ADDR baseaddr;
  512. {
  513.   bfd *interp_bfd;
  514.   CORE_ADDR address = 0;
  515.   char **symbolp;
  516.  
  517.   /* If the fd is -1, then there is no file that corresponds to this
  518.      mapped memory segment, so skip it.  Also, if the fd corresponds
  519.      to the exec file, skip it as well. */
  520.  
  521.   if (fd == -1
  522.       || (exec_bfd != NULL
  523.       && fdmatch (fileno ((GDB_FILE *)(exec_bfd -> iostream)), fd)))
  524.     {
  525.       return (0);
  526.     }
  527.  
  528.   /* Try to open whatever random file this fd corresponds to.  Note that
  529.      we have no way currently to find the filename.  Don't gripe about
  530.      any problems we might have, just fail. */
  531.  
  532.   if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
  533.     {
  534.       return (0);
  535.     }
  536.   if (!bfd_check_format (interp_bfd, bfd_object))
  537.     {
  538.       /* FIXME-leak: on failure, might not free all memory associated with
  539.      interp_bfd.  */
  540.       bfd_close (interp_bfd);
  541.       return (0);
  542.     }
  543.  
  544.   /* Now try to find our debug base symbol in this file, which we at
  545.      least know to be a valid ELF executable or shared library. */
  546.  
  547.   for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
  548.     {
  549.       address = bfd_lookup_symbol (interp_bfd, *symbolp);
  550.       if (address != 0)
  551.     {
  552.       break;
  553.     }
  554.     }
  555.   if (address == 0)
  556.     {
  557.       /* FIXME-leak: on failure, might not free all memory associated with
  558.      interp_bfd.  */
  559.       bfd_close (interp_bfd);
  560.       return (0);
  561.     }
  562.  
  563.   /* Eureka!  We found the symbol.  But now we may need to relocate it
  564.      by the base address.  If the symbol's value is less than the base
  565.      address of the shared library, then it hasn't yet been relocated
  566.      by the dynamic linker, and we have to do it ourself.  FIXME: Note
  567.      that we make the assumption that the first segment that corresponds
  568.      to the shared library has the base address to which the library
  569.      was relocated. */
  570.  
  571.   if (address < baseaddr)
  572.     {
  573.       address += baseaddr;
  574.     }
  575.   debug_base = address;
  576.   /* FIXME-leak: on failure, might not free all memory associated with
  577.      interp_bfd.  */
  578.   bfd_close (interp_bfd);
  579.   return (1);
  580. }
  581. #endif /* HANDLE_SVR4_EXEC_EMULATORS */
  582.  
  583. /*
  584.  
  585. LOCAL FUNCTION
  586.  
  587.     elf_locate_base -- locate the base address of dynamic linker structs
  588.     for SVR4 elf targets.
  589.  
  590. SYNOPSIS
  591.  
  592.     CORE_ADDR elf_locate_base (void)
  593.  
  594. DESCRIPTION
  595.  
  596.     For SVR4 elf targets the address of the dynamic linker's runtime
  597.     structure is contained within the dynamic info section in the
  598.     executable file.  The dynamic section is also mapped into the
  599.     inferior address space.  Because the runtime loader fills in the
  600.     real address before starting the inferior, we have to read in the
  601.     dynamic info section from the inferior address space.
  602.     If there are any errors while trying to find the address, we
  603.     silently return 0, otherwise the found address is returned.
  604.  
  605.  */
  606.  
  607. static CORE_ADDR
  608. elf_locate_base ()
  609. {
  610.   sec_ptr dyninfo_sect;
  611.   int dyninfo_sect_size;
  612.   CORE_ADDR dyninfo_addr;
  613.   char *buf;
  614.   char *bufend;
  615.  
  616.   /* Find the start address of the .dynamic section.  */
  617.   dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
  618.   if (dyninfo_sect == NULL)
  619.     return 0;
  620.   dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
  621.  
  622.   /* Read in .dynamic section, silently ignore errors.  */
  623.   dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
  624.   buf = alloca (dyninfo_sect_size);
  625.   if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
  626.     return 0;
  627.  
  628.   /* Find the DT_DEBUG entry in the the .dynamic section.
  629.      For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
  630.      no DT_DEBUG entries.  */
  631.   /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
  632.      a 32 bit ELF ABI target.  */
  633.   for (bufend = buf + dyninfo_sect_size;
  634.        buf < bufend;
  635.        buf += sizeof (Elf32_External_Dyn))
  636.     {
  637.       Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *)buf;
  638.       long dyn_tag;
  639.       CORE_ADDR dyn_ptr;
  640.  
  641.       dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
  642.       if (dyn_tag == DT_NULL)
  643.     break;
  644.       else if (dyn_tag == DT_DEBUG)
  645.     {
  646.       dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
  647.       return dyn_ptr;
  648.     }
  649. #ifdef DT_MIPS_RLD_MAP
  650.       else if (dyn_tag == DT_MIPS_RLD_MAP)
  651.     {
  652.       char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
  653.  
  654.       /* DT_MIPS_RLD_MAP contains a pointer to the address
  655.          of the dynamic link structure.  */
  656.       dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
  657.       if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
  658.         return 0;
  659.       return extract_unsigned_integer (pbuf, sizeof (pbuf));
  660.     }
  661. #endif
  662.     }
  663.  
  664.   /* DT_DEBUG entry not found.  */
  665.   return 0;
  666. }
  667.  
  668. #endif    /* SVR4_SHARED_LIBS */
  669.  
  670. /*
  671.  
  672. LOCAL FUNCTION
  673.  
  674.     locate_base -- locate the base address of dynamic linker structs
  675.  
  676. SYNOPSIS
  677.  
  678.     CORE_ADDR locate_base (void)
  679.  
  680. DESCRIPTION
  681.  
  682.     For both the SunOS and SVR4 shared library implementations, if the
  683.     inferior executable has been linked dynamically, there is a single
  684.     address somewhere in the inferior's data space which is the key to
  685.     locating all of the dynamic linker's runtime structures.  This
  686.     address is the value of the debug base symbol.  The job of this
  687.     function is to find and return that address, or to return 0 if there
  688.     is no such address (the executable is statically linked for example).
  689.  
  690.     For SunOS, the job is almost trivial, since the dynamic linker and
  691.     all of it's structures are statically linked to the executable at
  692.     link time.  Thus the symbol for the address we are looking for has
  693.     already been added to the minimal symbol table for the executable's
  694.     objfile at the time the symbol file's symbols were read, and all we
  695.     have to do is look it up there.  Note that we explicitly do NOT want
  696.     to find the copies in the shared library.
  697.  
  698.     The SVR4 version is a bit more complicated because the address
  699.     is contained somewhere in the dynamic info section.  We have to go
  700.     to a lot more work to discover the address of the debug base symbol.
  701.     Because of this complexity, we cache the value we find and return that
  702.     value on subsequent invocations.  Note there is no copy in the
  703.     executable symbol tables.
  704.  
  705.  */
  706.  
  707. static CORE_ADDR
  708. locate_base ()
  709. {
  710.  
  711. #ifndef SVR4_SHARED_LIBS
  712.  
  713.   struct minimal_symbol *msymbol;
  714.   CORE_ADDR address = 0;
  715.   char **symbolp;
  716.  
  717.   /* For SunOS, we want to limit the search for the debug base symbol to the
  718.      executable being debugged, since there is a duplicate named symbol in the
  719.      shared library.  We don't want the shared library versions. */
  720.  
  721.   for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
  722.     {
  723.       msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
  724.       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
  725.     {
  726.       address = SYMBOL_VALUE_ADDRESS (msymbol);
  727.       return (address);
  728.     }
  729.     }
  730.   return (0);
  731.  
  732. #else    /* SVR4_SHARED_LIBS */
  733.  
  734.   /* Check to see if we have a currently valid address, and if so, avoid
  735.      doing all this work again and just return the cached address.  If
  736.      we have no cached address, try to locate it in the dynamic info
  737.      section for ELF executables.  */
  738.  
  739.   if (debug_base == 0)
  740.     {
  741.       if (exec_bfd != NULL
  742.       && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
  743.     debug_base = elf_locate_base ();
  744. #ifdef HANDLE_SVR4_EXEC_EMULATORS
  745.       /* Try it the hard way for emulated executables.  */
  746.       else if (inferior_pid != 0)
  747.     proc_iterate_over_mappings (look_for_base);
  748. #endif
  749.     }
  750.   return (debug_base);
  751.  
  752. #endif    /* !SVR4_SHARED_LIBS */
  753.  
  754. }
  755.  
  756. /*
  757.  
  758. LOCAL FUNCTION
  759.  
  760.     first_link_map_member -- locate first member in dynamic linker's map
  761.  
  762. SYNOPSIS
  763.  
  764.     static struct link_map *first_link_map_member (void)
  765.  
  766. DESCRIPTION
  767.  
  768.     Read in a copy of the first member in the inferior's dynamic
  769.     link map from the inferior's dynamic linker structures, and return
  770.     a pointer to the copy in our address space.
  771. */
  772.  
  773. static struct link_map *
  774. first_link_map_member ()
  775. {
  776.   struct link_map *lm = NULL;
  777.  
  778. #ifndef SVR4_SHARED_LIBS
  779.  
  780.   read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
  781.   if (dynamic_copy.ld_version >= 2)
  782.     {
  783.       /* It is a version that we can deal with, so read in the secondary
  784.      structure and find the address of the link map list from it. */
  785.       read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
  786.            sizeof (struct link_dynamic_2));
  787.       lm = ld_2_copy.ld_loaded;
  788.     }
  789.  
  790. #else    /* SVR4_SHARED_LIBS */
  791.  
  792.   read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
  793.   /* FIXME:  Perhaps we should validate the info somehow, perhaps by
  794.      checking r_version for a known version number, or r_state for
  795.      RT_CONSISTENT. */
  796.   lm = debug_copy.r_map;
  797.  
  798. #endif    /* !SVR4_SHARED_LIBS */
  799.  
  800.   return (lm);
  801. }
  802.  
  803. /*
  804.  
  805. LOCAL FUNCTION
  806.  
  807.     find_solib -- step through list of shared objects
  808.  
  809. SYNOPSIS
  810.  
  811.     struct so_list *find_solib (struct so_list *so_list_ptr)
  812.  
  813. DESCRIPTION
  814.  
  815.     This module contains the routine which finds the names of any
  816.     loaded "images" in the current process. The argument in must be
  817.     NULL on the first call, and then the returned value must be passed
  818.     in on subsequent calls. This provides the capability to "step" down
  819.     the list of loaded objects. On the last object, a NULL value is
  820.     returned.
  821.  
  822.     The arg and return value are "struct link_map" pointers, as defined
  823.     in <link.h>.
  824.  */
  825.  
  826. static struct so_list *
  827. find_solib (so_list_ptr)
  828.      struct so_list *so_list_ptr;    /* Last lm or NULL for first one */
  829. {
  830.   struct so_list *so_list_next = NULL;
  831.   struct link_map *lm = NULL;
  832.   struct so_list *new;
  833.   
  834.   if (so_list_ptr == NULL)
  835.     {
  836.       /* We are setting up for a new scan through the loaded images. */
  837.       if ((so_list_next = so_list_head) == NULL)
  838.     {
  839.       /* We have not already read in the dynamic linking structures
  840.          from the inferior, lookup the address of the base structure. */
  841.       debug_base = locate_base ();
  842.       if (debug_base != 0)
  843.         {
  844.           /* Read the base structure in and find the address of the first
  845.          link map list member. */
  846.           lm = first_link_map_member ();
  847.         }
  848.     }
  849.     }
  850.   else
  851.     {
  852.       /* We have been called before, and are in the process of walking
  853.      the shared library list.  Advance to the next shared object. */
  854.       if ((lm = LM_NEXT (so_list_ptr)) == NULL)
  855.     {
  856.       /* We have hit the end of the list, so check to see if any were
  857.          added, but be quiet if we can't read from the target any more. */
  858.       int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
  859.                        (char *) &(so_list_ptr -> lm),
  860.                        sizeof (struct link_map));
  861.       if (status == 0)
  862.         {
  863.           lm = LM_NEXT (so_list_ptr);
  864.         }
  865.       else
  866.         {
  867.           lm = NULL;
  868.         }
  869.     }
  870.       so_list_next = so_list_ptr -> next;
  871.     }
  872.   if ((so_list_next == NULL) && (lm != NULL))
  873.     {
  874.       /* Get next link map structure from inferior image and build a local
  875.      abbreviated load_map structure */
  876.       new = (struct so_list *) xmalloc (sizeof (struct so_list));
  877.       memset ((char *) new, 0, sizeof (struct so_list));
  878.       new -> lmaddr = lm;
  879.       /* Add the new node as the next node in the list, or as the root
  880.      node if this is the first one. */
  881.       if (so_list_ptr != NULL)
  882.     {
  883.       so_list_ptr -> next = new;
  884.     }
  885.       else
  886.     {
  887.       so_list_head = new;
  888.     }      
  889.       so_list_next = new;
  890.       read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
  891.            sizeof (struct link_map));
  892.       /* For SVR4 versions, the first entry in the link map is for the
  893.      inferior executable, so we must ignore it.  For some versions of
  894.      SVR4, it has no name.  For others (Solaris 2.3 for example), it
  895.      does have a name, so we can no longer use a missing name to
  896.      decide when to ignore it. */
  897.       if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
  898.     {
  899.       int errcode;
  900.       char *buffer;
  901.       target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
  902.                   MAX_PATH_SIZE - 1, &errcode);
  903.       if (errcode != 0)
  904.         error ("find_solib: Can't read pathname for load map: %s\n",
  905.            safe_strerror (errcode));
  906.       strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
  907.       new -> so_name[MAX_PATH_SIZE - 1] = '\0';
  908.       free (buffer);
  909.       solib_map_sections (new);
  910.     }      
  911.     }
  912.   return (so_list_next);
  913. }
  914.  
  915. /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
  916.  
  917. static int
  918. symbol_add_stub (arg)
  919.      char *arg;
  920. {
  921.   register struct so_list *so = (struct so_list *) arg;    /* catch_errs bogon */
  922.   
  923.   so -> objfile =
  924.     symbol_file_add (so -> so_name, so -> from_tty,
  925.              (so->textsection == NULL
  926.               ? 0
  927.               : (unsigned int) so -> textsection -> addr),
  928.              0, 0, 0);
  929.   return (1);
  930. }
  931.  
  932. /*
  933.  
  934. GLOBAL FUNCTION
  935.  
  936.     solib_add -- add a shared library file to the symtab and section list
  937.  
  938. SYNOPSIS
  939.  
  940.     void solib_add (char *arg_string, int from_tty,
  941.             struct target_ops *target)
  942.  
  943. DESCRIPTION
  944.  
  945. */
  946.  
  947. void
  948. solib_add (arg_string, from_tty, target)
  949.      char *arg_string;
  950.      int from_tty;
  951.      struct target_ops *target;
  952. {    
  953.   register struct so_list *so = NULL;       /* link map state variable */
  954.  
  955.   /* Last shared library that we read.  */
  956.   struct so_list *so_last = NULL;
  957.  
  958.   char *re_err;
  959.   int count;
  960.   int old;
  961.   
  962.   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
  963.     {
  964.       error ("Invalid regexp: %s", re_err);
  965.     }
  966.   
  967.   /* Add the shared library sections to the section table of the
  968.      specified target, if any.  */
  969.   if (target)
  970.     {
  971.       /* Count how many new section_table entries there are.  */
  972.       so = NULL;
  973.       count = 0;
  974.       while ((so = find_solib (so)) != NULL)
  975.     {
  976.       if (so -> so_name[0])
  977.         {
  978.           count += so -> sections_end - so -> sections;
  979.         }
  980.     }
  981.       
  982.       if (count)
  983.     {
  984.       /* Reallocate the target's section table including the new size.  */
  985.       if (target -> to_sections)
  986.         {
  987.           old = target -> to_sections_end - target -> to_sections;
  988.           target -> to_sections = (struct section_table *)
  989.         xrealloc ((char *)target -> to_sections,
  990.              (sizeof (struct section_table)) * (count + old));
  991.         }
  992.       else
  993.         {
  994.           old = 0;
  995.           target -> to_sections = (struct section_table *)
  996.         xmalloc ((sizeof (struct section_table)) * count);
  997.         }
  998.       target -> to_sections_end = target -> to_sections + (count + old);
  999.       
  1000.       /* Add these section table entries to the target's table.  */
  1001.       while ((so = find_solib (so)) != NULL)
  1002.         {
  1003.           if (so -> so_name[0])
  1004.         {
  1005.           count = so -> sections_end - so -> sections;
  1006.           memcpy ((char *) (target -> to_sections + old),
  1007.               so -> sections, 
  1008.               (sizeof (struct section_table)) * count);
  1009.           old += count;
  1010.         }
  1011.         }
  1012.     }
  1013.     }
  1014.   
  1015.   /* Now add the symbol files.  */
  1016.   while ((so = find_solib (so)) != NULL)
  1017.     {
  1018.       if (so -> so_name[0] && re_exec (so -> so_name))
  1019.     {
  1020.       so -> from_tty = from_tty;
  1021.       if (so -> symbols_loaded)
  1022.         {
  1023.           if (from_tty)
  1024.         {
  1025.           printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
  1026.         }
  1027.         }
  1028.       else if (catch_errors
  1029.            (symbol_add_stub, (char *) so,
  1030.             "Error while reading shared library symbols:\n",
  1031.             RETURN_MASK_ALL))
  1032.         {
  1033.           so_last = so;
  1034.           so -> symbols_loaded = 1;
  1035.         }
  1036.     }
  1037.     }
  1038.  
  1039.   /* Getting new symbols may change our opinion about what is
  1040.      frameless.  */
  1041.   if (so_last)
  1042.     reinit_frame_cache ();
  1043.  
  1044.   if (so_last)
  1045.     special_symbol_handling (so_last);
  1046. }
  1047.  
  1048. /*
  1049.  
  1050. LOCAL FUNCTION
  1051.  
  1052.     info_sharedlibrary_command -- code for "info sharedlibrary"
  1053.  
  1054. SYNOPSIS
  1055.  
  1056.     static void info_sharedlibrary_command ()
  1057.  
  1058. DESCRIPTION
  1059.  
  1060.     Walk through the shared library list and print information
  1061.     about each attached library.
  1062. */
  1063.  
  1064. static void
  1065. info_sharedlibrary_command (ignore, from_tty)
  1066.      char *ignore;
  1067.      int from_tty;
  1068. {
  1069.   register struct so_list *so = NULL;      /* link map state variable */
  1070.   int header_done = 0;
  1071.   
  1072.   if (exec_bfd == NULL)
  1073.     {
  1074.       printf_unfiltered ("No exec file.\n");
  1075.       return;
  1076.     }
  1077.   while ((so = find_solib (so)) != NULL)
  1078.     {
  1079.       if (so -> so_name[0])
  1080.     {
  1081.       if (!header_done)
  1082.         {
  1083.           printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
  1084.              "Shared Object Library");
  1085.           header_done++;
  1086.         }
  1087.       /* FIXME-32x64: need print_address_numeric with field width or
  1088.          some such.  */
  1089.       printf_unfiltered ("%-12s",
  1090.           local_hex_string_custom ((unsigned long) LM_ADDR (so),
  1091.                        "08l"));
  1092.       printf_unfiltered ("%-12s",
  1093.           local_hex_string_custom ((unsigned long) so -> lmend,
  1094.                        "08l"));
  1095.       printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
  1096.       printf_unfiltered ("%s\n",  so -> so_name);
  1097.     }
  1098.     }
  1099.   if (so_list_head == NULL)
  1100.     {
  1101.       printf_unfiltered ("No shared libraries loaded at this time.\n");    
  1102.     }
  1103. }
  1104.  
  1105. /*
  1106.  
  1107. GLOBAL FUNCTION
  1108.  
  1109.     solib_address -- check to see if an address is in a shared lib
  1110.  
  1111. SYNOPSIS
  1112.  
  1113.     int solib_address (CORE_ADDR address)
  1114.  
  1115. DESCRIPTION
  1116.  
  1117.     Provides a hook for other gdb routines to discover whether or
  1118.     not a particular address is within the mapped address space of
  1119.     a shared library.  Any address between the base mapping address
  1120.     and the first address beyond the end of the last mapping, is
  1121.     considered to be within the shared library address space, for
  1122.     our purposes.
  1123.  
  1124.     For example, this routine is called at one point to disable
  1125.     breakpoints which are in shared libraries that are not currently
  1126.     mapped in.
  1127.  */
  1128.  
  1129. int
  1130. solib_address (address)
  1131.      CORE_ADDR address;
  1132. {
  1133.   register struct so_list *so = 0;       /* link map state variable */
  1134.   
  1135.   while ((so = find_solib (so)) != NULL)
  1136.     {
  1137.       if (so -> so_name[0])
  1138.     {
  1139.       if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
  1140.           (address < (CORE_ADDR) so -> lmend))
  1141.         {
  1142.           return (1);
  1143.         }
  1144.     }
  1145.     }
  1146.   return (0);
  1147. }
  1148.  
  1149. /* Called by free_all_symtabs */
  1150.  
  1151. void 
  1152. clear_solib()
  1153. {
  1154.   struct so_list *next;
  1155.   char *bfd_filename;
  1156.   
  1157.   while (so_list_head)
  1158.     {
  1159.       if (so_list_head -> sections)
  1160.     {
  1161.       free ((PTR)so_list_head -> sections);
  1162.     }
  1163.       if (so_list_head -> abfd)
  1164.     {
  1165.       bfd_filename = bfd_get_filename (so_list_head -> abfd);
  1166.       if (!bfd_close (so_list_head -> abfd))
  1167.         warning ("cannot close \"%s\": %s",
  1168.              bfd_filename, bfd_errmsg (bfd_get_error ()));
  1169.     }
  1170.       else
  1171.     /* This happens for the executable on SVR4.  */
  1172.     bfd_filename = NULL;
  1173.       
  1174.       next = so_list_head -> next;
  1175.       if (bfd_filename)
  1176.     free ((PTR)bfd_filename);
  1177.       free ((PTR)so_list_head);
  1178.       so_list_head = next;
  1179.     }
  1180.   debug_base = 0;
  1181. }
  1182.  
  1183. /*
  1184.  
  1185. LOCAL FUNCTION
  1186.  
  1187.     disable_break -- remove the "mapping changed" breakpoint
  1188.  
  1189. SYNOPSIS
  1190.  
  1191.     static int disable_break ()
  1192.  
  1193. DESCRIPTION
  1194.  
  1195.     Removes the breakpoint that gets hit when the dynamic linker
  1196.     completes a mapping change.
  1197.  
  1198. */
  1199.  
  1200. static int
  1201. disable_break ()
  1202. {
  1203.   int status = 1;
  1204.  
  1205. #ifndef SVR4_SHARED_LIBS
  1206.  
  1207.   int in_debugger = 0;
  1208.   
  1209.   /* Read the debugger structure from the inferior to retrieve the
  1210.      address of the breakpoint and the original contents of the
  1211.      breakpoint address.  Remove the breakpoint by writing the original
  1212.      contents back. */
  1213.  
  1214.   read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
  1215.  
  1216.   /* Set `in_debugger' to zero now. */
  1217.  
  1218.   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
  1219.  
  1220.   breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
  1221.   write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
  1222.         sizeof (debug_copy.ldd_bp_inst));
  1223.  
  1224. #else    /* SVR4_SHARED_LIBS */
  1225.  
  1226.   /* Note that breakpoint address and original contents are in our address
  1227.      space, so we just need to write the original contents back. */
  1228.  
  1229.   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
  1230.     {
  1231.       status = 0;
  1232.     }
  1233.  
  1234. #endif    /* !SVR4_SHARED_LIBS */
  1235.  
  1236.   /* For the SVR4 version, we always know the breakpoint address.  For the
  1237.      SunOS version we don't know it until the above code is executed.
  1238.      Grumble if we are stopped anywhere besides the breakpoint address. */
  1239.  
  1240.   if (stop_pc != breakpoint_addr)
  1241.     {
  1242.       warning ("stopped at unknown breakpoint while handling shared libraries");
  1243.     }
  1244.  
  1245.   return (status);
  1246. }
  1247.  
  1248. /*
  1249.  
  1250. LOCAL FUNCTION
  1251.  
  1252.     enable_break -- arrange for dynamic linker to hit breakpoint
  1253.  
  1254. SYNOPSIS
  1255.  
  1256.     int enable_break (void)
  1257.  
  1258. DESCRIPTION
  1259.  
  1260.     Both the SunOS and the SVR4 dynamic linkers have, as part of their
  1261.     debugger interface, support for arranging for the inferior to hit
  1262.     a breakpoint after mapping in the shared libraries.  This function
  1263.     enables that breakpoint.
  1264.  
  1265.     For SunOS, there is a special flag location (in_debugger) which we
  1266.     set to 1.  When the dynamic linker sees this flag set, it will set
  1267.     a breakpoint at a location known only to itself, after saving the
  1268.     original contents of that place and the breakpoint address itself,
  1269.     in it's own internal structures.  When we resume the inferior, it
  1270.     will eventually take a SIGTRAP when it runs into the breakpoint.
  1271.     We handle this (in a different place) by restoring the contents of
  1272.     the breakpointed location (which is only known after it stops),
  1273.     chasing around to locate the shared libraries that have been
  1274.     loaded, then resuming.
  1275.  
  1276.     For SVR4, the debugger interface structure contains a member (r_brk)
  1277.     which is statically initialized at the time the shared library is
  1278.     built, to the offset of a function (_r_debug_state) which is guaran-
  1279.     teed to be called once before mapping in a library, and again when
  1280.     the mapping is complete.  At the time we are examining this member,
  1281.     it contains only the unrelocated offset of the function, so we have
  1282.     to do our own relocation.  Later, when the dynamic linker actually
  1283.     runs, it relocates r_brk to be the actual address of _r_debug_state().
  1284.  
  1285.     The debugger interface structure also contains an enumeration which
  1286.     is set to either RT_ADD or RT_DELETE prior to changing the mapping,
  1287.     depending upon whether or not the library is being mapped or unmapped,
  1288.     and then set to RT_CONSISTENT after the library is mapped/unmapped.
  1289. */
  1290.  
  1291. static int
  1292. enable_break ()
  1293. {
  1294.   int success = 0;
  1295.  
  1296. #ifndef SVR4_SHARED_LIBS
  1297.  
  1298.   int j;
  1299.   int in_debugger;
  1300.  
  1301.   /* Get link_dynamic structure */
  1302.  
  1303.   j = target_read_memory (debug_base, (char *) &dynamic_copy,
  1304.               sizeof (dynamic_copy));
  1305.   if (j)
  1306.     {
  1307.       /* unreadable */
  1308.       return (0);
  1309.     }
  1310.  
  1311.   /* Calc address of debugger interface structure */
  1312.  
  1313.   debug_addr = (CORE_ADDR) dynamic_copy.ldd;
  1314.  
  1315.   /* Calc address of `in_debugger' member of debugger interface structure */
  1316.  
  1317.   flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
  1318.                     (char *) &debug_copy);
  1319.  
  1320.   /* Write a value of 1 to this member.  */
  1321.  
  1322.   in_debugger = 1;
  1323.   write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
  1324.   success = 1;
  1325.  
  1326. #else    /* SVR4_SHARED_LIBS */
  1327.  
  1328. #ifdef BKPT_AT_SYMBOL
  1329.  
  1330.   struct minimal_symbol *msymbol;
  1331.   char **bkpt_namep;
  1332.   CORE_ADDR bkpt_addr;
  1333.  
  1334.   /* Scan through the list of symbols, trying to look up the symbol and
  1335.      set a breakpoint there.  Terminate loop when we/if we succeed. */
  1336.  
  1337.   breakpoint_addr = 0;
  1338.   for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
  1339.     {
  1340.       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
  1341.       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
  1342.     {
  1343.       bkpt_addr = SYMBOL_VALUE_ADDRESS (msymbol);
  1344.       if (target_insert_breakpoint (bkpt_addr, shadow_contents) == 0)
  1345.         {
  1346.           breakpoint_addr = bkpt_addr;
  1347.           success = 1;
  1348.           break;
  1349.         }
  1350.     }
  1351.     }
  1352.  
  1353. #else    /* !BKPT_AT_SYMBOL */
  1354.  
  1355.   struct symtab_and_line sal;
  1356.  
  1357.   /* Read the debugger interface structure directly. */
  1358.  
  1359.   read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
  1360.  
  1361.   /* Set breakpoint at the debugger interface stub routine that will
  1362.      be called just prior to each mapping change and again after the
  1363.      mapping change is complete.  Set up the (nonexistent) handler to
  1364.      deal with hitting these breakpoints.  (FIXME). */
  1365.  
  1366.   warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
  1367.   success = 1;
  1368.  
  1369. #endif    /* BKPT_AT_SYMBOL */
  1370.  
  1371. #endif    /* !SVR4_SHARED_LIBS */
  1372.  
  1373.   return (success);
  1374. }
  1375.   
  1376. /*
  1377.   
  1378. GLOBAL FUNCTION
  1379.   
  1380.     solib_create_inferior_hook -- shared library startup support
  1381.   
  1382. SYNOPSIS
  1383.   
  1384.     void solib_create_inferior_hook()
  1385.   
  1386. DESCRIPTION
  1387.   
  1388.     When gdb starts up the inferior, it nurses it along (through the
  1389.     shell) until it is ready to execute it's first instruction.  At this
  1390.     point, this function gets called via expansion of the macro
  1391.     SOLIB_CREATE_INFERIOR_HOOK.
  1392.  
  1393.     For SunOS executables, this first instruction is typically the
  1394.     one at "_start", or a similar text label, regardless of whether
  1395.     the executable is statically or dynamically linked.  The runtime
  1396.     startup code takes care of dynamically linking in any shared
  1397.     libraries, once gdb allows the inferior to continue.
  1398.  
  1399.     For SVR4 executables, this first instruction is either the first
  1400.     instruction in the dynamic linker (for dynamically linked
  1401.     executables) or the instruction at "start" for statically linked
  1402.     executables.  For dynamically linked executables, the system
  1403.     first exec's /lib/libc.so.N, which contains the dynamic linker,
  1404.     and starts it running.  The dynamic linker maps in any needed
  1405.     shared libraries, maps in the actual user executable, and then
  1406.     jumps to "start" in the user executable.
  1407.  
  1408.     For both SunOS shared libraries, and SVR4 shared libraries, we
  1409.     can arrange to cooperate with the dynamic linker to discover the
  1410.     names of shared libraries that are dynamically linked, and the
  1411.     base addresses to which they are linked.
  1412.  
  1413.     This function is responsible for discovering those names and
  1414.     addresses, and saving sufficient information about them to allow
  1415.     their symbols to be read at a later time.
  1416.  
  1417. FIXME
  1418.  
  1419.     Between enable_break() and disable_break(), this code does not
  1420.     properly handle hitting breakpoints which the user might have
  1421.     set in the startup code or in the dynamic linker itself.  Proper
  1422.     handling will probably have to wait until the implementation is
  1423.     changed to use the "breakpoint handler function" method.
  1424.  
  1425.     Also, what if child has exit()ed?  Must exit loop somehow.
  1426.   */
  1427.  
  1428. void 
  1429. solib_create_inferior_hook()
  1430. {
  1431.   /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
  1432.      yet.  In fact, in the case of a SunOS4 executable being run on
  1433.      Solaris, we can't get it yet.  find_solib will get it when it needs
  1434.      it.  */
  1435. #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
  1436.   if ((debug_base = locate_base ()) == 0)
  1437.     {
  1438.       /* Can't find the symbol or the executable is statically linked. */
  1439.       return;
  1440.     }
  1441. #endif
  1442.  
  1443.   if (!enable_break ())
  1444.     {
  1445.       warning ("shared library handler failed to enable breakpoint");
  1446.       return;
  1447.     }
  1448.  
  1449.   /* Now run the target.  It will eventually hit the breakpoint, at
  1450.      which point all of the libraries will have been mapped in and we
  1451.      can go groveling around in the dynamic linker structures to find
  1452.      out what we need to know about them. */
  1453.  
  1454.   clear_proceed_status ();
  1455.   stop_soon_quietly = 1;
  1456.   stop_signal = TARGET_SIGNAL_0;
  1457.   do
  1458.     {
  1459.       target_resume (-1, 0, stop_signal);
  1460.       wait_for_inferior ();
  1461.     }
  1462.   while (stop_signal != TARGET_SIGNAL_TRAP);
  1463.   stop_soon_quietly = 0;
  1464.   
  1465.   /* We are now either at the "mapping complete" breakpoint (or somewhere
  1466.      else, a condition we aren't prepared to deal with anyway), so adjust
  1467.      the PC as necessary after a breakpoint, disable the breakpoint, and
  1468.      add any shared libraries that were mapped in. */
  1469.  
  1470.   if (DECR_PC_AFTER_BREAK)
  1471.     {
  1472.       stop_pc -= DECR_PC_AFTER_BREAK;
  1473.       write_register (PC_REGNUM, stop_pc);
  1474.     }
  1475.  
  1476.   if (!disable_break ())
  1477.     {
  1478.       warning ("shared library handler failed to disable breakpoint");
  1479.     }
  1480.  
  1481.   if (auto_solib_add_at_startup)
  1482.     solib_add ((char *) 0, 0, (struct target_ops *) 0);
  1483. }
  1484.  
  1485. /*
  1486.  
  1487. LOCAL FUNCTION
  1488.  
  1489.     special_symbol_handling -- additional shared library symbol handling
  1490.  
  1491. SYNOPSIS
  1492.  
  1493.     void special_symbol_handling (struct so_list *so)
  1494.  
  1495. DESCRIPTION
  1496.  
  1497.     Once the symbols from a shared object have been loaded in the usual
  1498.     way, we are called to do any system specific symbol handling that 
  1499.     is needed.
  1500.  
  1501.     For SunOS4, this consists of grunging around in the dynamic
  1502.     linkers structures to find symbol definitions for "common" symbols
  1503.     and adding them to the minimal symbol table for the runtime common
  1504.     objfile.
  1505.  
  1506. */
  1507.  
  1508. static void
  1509. special_symbol_handling (so)
  1510. struct so_list *so;
  1511. {
  1512. #ifndef SVR4_SHARED_LIBS
  1513.   int j;
  1514.  
  1515.   if (debug_addr == 0)
  1516.     {
  1517.       /* Get link_dynamic structure */
  1518.  
  1519.       j = target_read_memory (debug_base, (char *) &dynamic_copy,
  1520.                   sizeof (dynamic_copy));
  1521.       if (j)
  1522.     {
  1523.       /* unreadable */
  1524.       return;
  1525.     }
  1526.  
  1527.       /* Calc address of debugger interface structure */
  1528.       /* FIXME, this needs work for cross-debugging of core files
  1529.      (byteorder, size, alignment, etc).  */
  1530.  
  1531.       debug_addr = (CORE_ADDR) dynamic_copy.ldd;
  1532.     }
  1533.  
  1534.   /* Read the debugger structure from the inferior, just to make sure
  1535.      we have a current copy. */
  1536.  
  1537.   j = target_read_memory (debug_addr, (char *) &debug_copy,
  1538.               sizeof (debug_copy));
  1539.   if (j)
  1540.     return;        /* unreadable */
  1541.  
  1542.   /* Get common symbol definitions for the loaded object. */
  1543.  
  1544.   if (debug_copy.ldd_cp)
  1545.     {
  1546.       solib_add_common_symbols (debug_copy.ldd_cp);
  1547.     }
  1548.  
  1549. #endif    /* !SVR4_SHARED_LIBS */
  1550. }
  1551.  
  1552.  
  1553. /*
  1554.  
  1555. LOCAL FUNCTION
  1556.  
  1557.     sharedlibrary_command -- handle command to explicitly add library
  1558.  
  1559. SYNOPSIS
  1560.  
  1561.     static void sharedlibrary_command (char *args, int from_tty)
  1562.  
  1563. DESCRIPTION
  1564.  
  1565. */
  1566.  
  1567. static void
  1568. sharedlibrary_command (args, from_tty)
  1569. char *args;
  1570. int from_tty;
  1571. {
  1572.   dont_repeat ();
  1573.   solib_add (args, from_tty, (struct target_ops *) 0);
  1574. }
  1575.  
  1576. void
  1577. _initialize_solib()
  1578. {
  1579.   
  1580.   add_com ("sharedlibrary", class_files, sharedlibrary_command,
  1581.        "Load shared object library symbols for files matching REGEXP.");
  1582.   add_info ("sharedlibrary", info_sharedlibrary_command, 
  1583.         "Status of loaded shared object libraries.");
  1584.  
  1585.   add_show_from_set
  1586.     (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
  1587.           (char *) &auto_solib_add_at_startup,
  1588.           "Set autoloading of shared library symbols at startup.\n\
  1589. If nonzero, symbols from all shared object libraries will be loaded\n\
  1590. automatically when the inferior begins execution.  Otherwise, symbols\n\
  1591. must be loaded manually, using `sharedlibrary'.",
  1592.           &setlist),
  1593.      &showlist);
  1594. }
  1595.