home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / solib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  42.2 KB  |  1,547 lines

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