home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / osfsolib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-03  |  24.4 KB  |  931 lines

  1. /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
  2.    Copyright 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. /* FIXME: Most of this code could be merged with solib.c by using
  21.    next_link_map_member and xfer_link_map_member in solib.c.  */
  22.  
  23. #include "defs.h"
  24.  
  25. #include <sys/types.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29.  
  30. #include "symtab.h"
  31. #include "bfd.h"
  32. #include "symfile.h"
  33. #include "objfiles.h"
  34. #include "gdbcore.h"
  35. #include "command.h"
  36. #include "target.h"
  37. #include "frame.h"
  38. #include "regex.h"
  39. #include "inferior.h"
  40. #include "language.h"
  41.  
  42. #define MAX_PATH_SIZE 1024        /* FIXME: Should be dynamic */
  43.  
  44. /* When handling shared libraries, GDB has to find out the pathnames
  45.    of all shared libraries that are currently loaded (to read in their
  46.    symbols) and where the shared libraries are loaded in memory
  47.    (to relocate them properly from their prelinked addresses to the
  48.    current load address).
  49.  
  50.    Under OSF/1 there are two possibilities to get at this information:
  51.    1) Peek around in the runtime loader structures.
  52.       These are not documented, and they are not defined in the system
  53.       header files. The definitions below were obtained by experimentation,
  54.       but they seem stable enough.
  55.    2) Use the undocumented libxproc.a library, which contains the
  56.       equivalent ldr_* routines.
  57.       This approach is somewhat cleaner, but it requires that the GDB
  58.       executable is dynamically linked. In addition it requires a
  59.       NAT_CLIBS= -lxproc -Wl,-expect_unresolved,ldr_process_context
  60.       linker specification for GDB and all applications that are using
  61.       libgdb.
  62.    We will use the peeking approach until it becomes unwieldy.  */
  63.  
  64. #ifndef USE_LDR_ROUTINES
  65.  
  66. /* Definition of runtime loader structures, found by experimentation.  */
  67. #define RLD_CONTEXT_ADDRESS    0x3ffc0000000
  68.  
  69. typedef struct
  70. {
  71.     CORE_ADDR next;
  72.     CORE_ADDR previous;
  73.     CORE_ADDR unknown1;
  74.     char *module_name;
  75.     CORE_ADDR modinfo_addr;
  76.     long module_id;
  77.     CORE_ADDR unknown2;
  78.     CORE_ADDR unknown3;
  79.     long region_count;
  80.     CORE_ADDR regioninfo_addr;
  81. } ldr_module_info_t;
  82.  
  83. typedef struct
  84. {
  85.     long unknown1;
  86.     CORE_ADDR regionname_addr;
  87.     long protection;
  88.     CORE_ADDR vaddr;
  89.     CORE_ADDR mapaddr;
  90.     long size;
  91.     long unknown2[5];
  92. } ldr_region_info_t;
  93.  
  94. typedef struct
  95. {
  96.     CORE_ADDR unknown1;
  97.     CORE_ADDR unknown2;
  98.     CORE_ADDR head;
  99.     CORE_ADDR tail;
  100. } ldr_context_t;
  101.  
  102. static ldr_context_t ldr_context;
  103.  
  104. #else
  105.  
  106. #include <loader.h>
  107. static ldr_process_t fake_ldr_process;
  108.  
  109. /* Called by ldr_* routines to read memory from the current target.  */
  110.  
  111. static int ldr_read_memory PARAMS ((CORE_ADDR, char *, int, int));
  112.  
  113. static int
  114. ldr_read_memory (memaddr, myaddr, len, readstring)
  115.      CORE_ADDR memaddr;
  116.      char *myaddr;
  117.      int len;
  118.      int readstring;
  119. {
  120.   int result;
  121.   char *buffer;
  122.  
  123.   if (readstring)
  124.     {
  125.       target_read_string (memaddr, &buffer, len, &result);
  126.       if (result == 0)
  127.         strcpy (myaddr, buffer);
  128.       free (buffer);
  129.     }
  130.   else
  131.     result = target_read_memory (memaddr, myaddr, len);
  132.  
  133.   if (result != 0)
  134.     result = -result;
  135.   return result;
  136. }
  137.  
  138. #endif
  139.  
  140. /* Define our own link_map structure.
  141.    This will help to share code with solib.c.  */
  142.  
  143. struct link_map {
  144.   CORE_ADDR l_offset;            /* prelink to load address offset */
  145.   char *l_name;                /* full name of loaded object */
  146.   ldr_module_info_t module_info;    /* corresponding module info */
  147. };
  148.  
  149. #define LM_OFFSET(so) ((so) -> lm.l_offset)
  150. #define LM_NAME(so) ((so) -> lm.l_name)
  151.  
  152. struct so_list {
  153.   struct so_list *next;            /* next structure in linked list */
  154.   struct link_map lm;            /* copy of link map from inferior */
  155.   struct link_map *lmaddr;        /* addr in inferior lm was read from */
  156.   CORE_ADDR lmend;            /* upper addr bound of mapped object */
  157.   char so_name[MAX_PATH_SIZE];        /* shared object lib name (FIXME) */
  158.   char symbols_loaded;            /* flag: symbols read in yet? */
  159.   char from_tty;            /* flag: print msgs? */
  160.   struct objfile *objfile;        /* objfile for loaded lib */
  161.   struct section_table *sections;
  162.   struct section_table *sections_end;
  163.   struct section_table *textsection;
  164.   bfd *abfd;
  165. };
  166.  
  167. static struct so_list *so_list_head;    /* List of known shared objects */
  168.  
  169. extern int
  170. fdmatch PARAMS ((int, int));        /* In libiberty */
  171.  
  172. /* Local function prototypes */
  173.  
  174. static void
  175. sharedlibrary_command PARAMS ((char *, int));
  176.  
  177. static void
  178. info_sharedlibrary_command PARAMS ((char *, int));
  179.  
  180. static int
  181. symbol_add_stub PARAMS ((char *));
  182.  
  183. static struct so_list *
  184. find_solib PARAMS ((struct so_list *));
  185.  
  186. static struct link_map *
  187. first_link_map_member PARAMS ((void));
  188.  
  189. static struct link_map *
  190. next_link_map_member PARAMS ((struct so_list *));
  191.  
  192. static void
  193. xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
  194.  
  195. static void
  196. solib_map_sections PARAMS ((struct so_list *));
  197.  
  198. /*
  199.  
  200. LOCAL FUNCTION
  201.  
  202.     solib_map_sections -- open bfd and build sections for shared lib
  203.  
  204. SYNOPSIS
  205.  
  206.     static void solib_map_sections (struct so_list *so)
  207.  
  208. DESCRIPTION
  209.  
  210.     Given a pointer to one of the shared objects in our list
  211.     of mapped objects, use the recorded name to open a bfd
  212.     descriptor for the object, build a section table, and then
  213.     relocate all the section addresses by the base address at
  214.     which the shared object was mapped.
  215.  
  216. FIXMES
  217.  
  218.     In most (all?) cases the shared object file name recorded in the
  219.     dynamic linkage tables will be a fully qualified pathname.  For
  220.     cases where it isn't, do we really mimic the systems search
  221.     mechanism correctly in the below code (particularly the tilde
  222.     expansion stuff?).
  223.  */
  224.  
  225. static void
  226. solib_map_sections (so)
  227.      struct so_list *so;
  228. {
  229.   char *filename;
  230.   char *scratch_pathname;
  231.   int scratch_chan;
  232.   struct section_table *p;
  233.   struct cleanup *old_chain;
  234.   bfd *abfd;
  235.   
  236.   filename = tilde_expand (so -> so_name);
  237.   old_chain = make_cleanup (free, filename);
  238.   
  239.   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  240.             &scratch_pathname);
  241.   if (scratch_chan < 0)
  242.     {
  243.       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
  244.                 O_RDONLY, 0, &scratch_pathname);
  245.     }
  246.   if (scratch_chan < 0)
  247.     {
  248.       perror_with_name (filename);
  249.     }
  250.   /* Leave scratch_pathname allocated.  bfd->name will point to it.  */
  251.  
  252.   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
  253.   if (!abfd)
  254.     {
  255.       close (scratch_chan);
  256.       error ("Could not open `%s' as an executable file: %s",
  257.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  258.     }
  259.   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
  260.   so -> abfd = abfd;
  261.   abfd -> cacheable = true;
  262.  
  263.   if (!bfd_check_format (abfd, bfd_object))
  264.     {
  265.       error ("\"%s\": not in executable format: %s.",
  266.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  267.     }
  268.   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
  269.     {
  270.       error ("Can't find the file sections in `%s': %s", 
  271.          bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
  272.     }
  273.  
  274.   for (p = so -> sections; p < so -> sections_end; p++)
  275.     {
  276.       /* Relocate the section binding addresses as recorded in the shared
  277.      object's file by the offset to get the address to which the
  278.      object was actually mapped.  */
  279.       p -> addr += LM_OFFSET (so);
  280.       p -> endaddr += LM_OFFSET (so);
  281.       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
  282.       if (STREQ (p -> the_bfd_section -> name, ".text"))
  283.     {
  284.       so -> textsection = p;
  285.     }
  286.     }
  287.  
  288.   /* Free the file names, close the file now.  */
  289.   do_cleanups (old_chain);
  290. }
  291.  
  292. /*
  293.  
  294. LOCAL FUNCTION
  295.  
  296.     first_link_map_member -- locate first member in dynamic linker's map
  297.  
  298. SYNOPSIS
  299.  
  300.     static struct link_map *first_link_map_member (void)
  301.  
  302. DESCRIPTION
  303.  
  304.     Read in a copy of the first member in the inferior's dynamic
  305.     link map from the inferior's dynamic linker structures, and return
  306.     a pointer to the copy in our address space.
  307. */
  308.  
  309. static struct link_map *
  310. first_link_map_member ()
  311. {
  312.   struct link_map *lm = NULL;
  313.   static struct link_map first_lm;
  314.  
  315. #ifdef USE_LDR_ROUTINES
  316.   ldr_module_t mod_id = LDR_NULL_MODULE;
  317.   size_t retsize;
  318.  
  319.   fake_ldr_process = ldr_core_process ();
  320.   ldr_set_core_reader (ldr_read_memory);
  321.   ldr_xdetach (fake_ldr_process);
  322.   if (ldr_xattach (fake_ldr_process) != 0
  323.       || ldr_next_module(fake_ldr_process, &mod_id) != 0
  324.       || mod_id == LDR_NULL_MODULE
  325.       || ldr_inq_module(fake_ldr_process, mod_id,
  326.             &first_lm.module_info, sizeof(ldr_module_info_t),
  327.             &retsize) != 0)
  328.     return lm;
  329. #else
  330.   CORE_ADDR ldr_context_addr;
  331.  
  332.   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
  333.               (char *) &ldr_context_addr,
  334.               sizeof (CORE_ADDR)) != 0
  335.       || target_read_memory (ldr_context_addr,
  336.                  (char *) &ldr_context,
  337.                  sizeof (ldr_context_t)) != 0
  338.       || target_read_memory ((CORE_ADDR) ldr_context.head,
  339.                  (char *) &first_lm.module_info,
  340.                  sizeof (ldr_module_info_t)) != 0)
  341.     return lm;
  342. #endif
  343.  
  344.   lm = &first_lm;
  345.  
  346.   /* The first entry is for the main program and should be skipped.  */
  347.   lm->l_name = NULL;
  348.  
  349.   return lm;
  350. }
  351.  
  352. static struct link_map *
  353. next_link_map_member (so_list_ptr)
  354.      struct so_list *so_list_ptr;
  355. {
  356.   struct link_map *lm = NULL;
  357.   static struct link_map next_lm;
  358. #ifdef USE_LDR_ROUTINES
  359.   ldr_module_t mod_id = so_list_ptr->lm.module_info.lmi_modid;
  360.   size_t retsize;
  361.  
  362.   if (ldr_next_module(fake_ldr_process, &mod_id) != 0
  363.       || mod_id == LDR_NULL_MODULE
  364.       || ldr_inq_module(fake_ldr_process, mod_id,
  365.             &next_lm.module_info, sizeof(ldr_module_info_t),
  366.             &retsize) != 0)
  367.     return lm;
  368.  
  369.   lm = &next_lm;
  370.   lm->l_name = lm->module_info.lmi_name;
  371. #else
  372.   CORE_ADDR ldr_context_addr;
  373.  
  374.   /* Reread context in case ldr_context.tail was updated.  */
  375.  
  376.   if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
  377.               (char *) &ldr_context_addr,
  378.               sizeof (CORE_ADDR)) != 0
  379.       || target_read_memory (ldr_context_addr,
  380.                  (char *) &ldr_context,
  381.                  sizeof (ldr_context_t)) != 0
  382.       || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
  383.       || target_read_memory (so_list_ptr->lm.module_info.next,
  384.                  (char *) &next_lm.module_info,
  385.                  sizeof (ldr_module_info_t)) != 0)
  386.     return lm;
  387.  
  388.   lm = &next_lm;
  389.   lm->l_name = lm->module_info.module_name;
  390. #endif
  391.   return lm;
  392. }
  393.  
  394. static void
  395. xfer_link_map_member (so_list_ptr, lm)
  396.      struct so_list *so_list_ptr;
  397.      struct link_map *lm;
  398. {
  399.   int i;
  400.   so_list_ptr->lm = *lm;
  401.  
  402.   /* OSF/1 shared libraries are pre-linked to particular addresses,
  403.      but the runtime loader may have to relocate them if the
  404.      address ranges of the libraries used by the target executable clash,
  405.      or if the target executable is linked with the -taso option.
  406.      The offset is the difference between the address where the shared
  407.      library is mapped and the pre-linked address of the shared library.
  408.  
  409.      FIXME:  GDB is currently unable to relocate the shared library
  410.      sections by different offsets. If sections are relocated by
  411.      different offsets, put out a warning and use the offset of the
  412.      first section for all remaining sections.  */
  413.   LM_OFFSET (so_list_ptr) = 0;
  414.  
  415.   /* There is one entry that has no name (for the inferior executable)
  416.      since it is not a shared object. */
  417.   if (LM_NAME (so_list_ptr) != 0)
  418.     {
  419.  
  420. #ifdef USE_LDR_ROUTINES
  421.       int len = strlen (LM_NAME (so_list_ptr) + 1);
  422.  
  423.       if (len > MAX_PATH_SIZE)
  424.     len = MAX_PATH_SIZE;
  425.       strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
  426.       so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
  427.  
  428.       for (i = 0; i < lm->module_info.lmi_nregion; i++)
  429.     {
  430.       ldr_region_info_t region_info;
  431.       size_t retsize;
  432.       CORE_ADDR region_offset;
  433.  
  434.       if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
  435.                   i, ®ion_info, sizeof (region_info),
  436.                   &retsize) != 0)
  437.         break;
  438.       region_offset = (CORE_ADDR) region_info.lri_mapaddr
  439.               - (CORE_ADDR) region_info.lri_vaddr;
  440.       if (i == 0)
  441.         LM_OFFSET (so_list_ptr) = region_offset;
  442.       else if (LM_OFFSET (so_list_ptr) != region_offset)
  443.         warning ("cannot handle shared library relocation for %s (%s)",
  444.              so_list_ptr->so_name, region_info.lri_name);
  445.     }
  446. #else
  447.       int errcode;
  448.       char *buffer;
  449.       target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
  450.               MAX_PATH_SIZE - 1, &errcode);
  451.       if (errcode != 0)
  452.     error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
  453.            safe_strerror (errcode));
  454.       strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
  455.       free (buffer);
  456.       so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
  457.  
  458.       for (i = 0; i < lm->module_info.region_count; i++)
  459.     {
  460.       ldr_region_info_t region_info;
  461.       CORE_ADDR region_offset;
  462.  
  463.       if (target_read_memory (lm->module_info.regioninfo_addr
  464.                    + i * sizeof (region_info),
  465.                   (char *) ®ion_info,
  466.                   sizeof (region_info)) != 0)
  467.         break;
  468.       region_offset = region_info.mapaddr - region_info.vaddr;
  469.       if (i == 0)
  470.         LM_OFFSET (so_list_ptr) = region_offset;
  471.       else if (LM_OFFSET (so_list_ptr) != region_offset)
  472.         {
  473.           char *region_name;
  474.           target_read_string (region_info.regionname_addr, &buffer,
  475.                   MAX_PATH_SIZE - 1, &errcode);
  476.           if (errcode == 0)
  477.         region_name = buffer;
  478.           else
  479.         region_name = "??";
  480.           warning ("cannot handle shared library relocation for %s (%s)",
  481.                 so_list_ptr->so_name, region_name);
  482.           free (buffer);
  483.         }
  484.     }
  485. #endif
  486.  
  487.       solib_map_sections (so_list_ptr);
  488.     }
  489. }
  490.  
  491. /*
  492.  
  493. LOCAL FUNCTION
  494.  
  495.     find_solib -- step through list of shared objects
  496.  
  497. SYNOPSIS
  498.  
  499.     struct so_list *find_solib (struct so_list *so_list_ptr)
  500.  
  501. DESCRIPTION
  502.  
  503.     This module contains the routine which finds the names of any
  504.     loaded "images" in the current process. The argument in must be
  505.     NULL on the first call, and then the returned value must be passed
  506.     in on subsequent calls. This provides the capability to "step" down
  507.     the list of loaded objects. On the last object, a NULL value is
  508.     returned.
  509.  
  510.     The arg and return value are "struct link_map" pointers, as defined
  511.     in <link.h>.
  512.  */
  513.  
  514. static struct so_list *
  515. find_solib (so_list_ptr)
  516.      struct so_list *so_list_ptr;    /* Last lm or NULL for first one */
  517. {
  518.   struct so_list *so_list_next = NULL;
  519.   struct link_map *lm = NULL;
  520.   struct so_list *new;
  521.   
  522.   if (so_list_ptr == NULL)
  523.     {
  524.       /* We are setting up for a new scan through the loaded images. */
  525.       if ((so_list_next = so_list_head) == NULL)
  526.     {
  527.       /* Find the first link map list member. */
  528.       lm = first_link_map_member ();
  529.     }
  530.     }
  531.   else
  532.     {
  533.       /* We have been called before, and are in the process of walking
  534.      the shared library list.  Advance to the next shared object. */
  535.       lm = next_link_map_member (so_list_ptr);
  536.       so_list_next = so_list_ptr -> next;
  537.     }
  538.   if ((so_list_next == NULL) && (lm != NULL))
  539.     {
  540.       /* Get next link map structure from inferior image and build a local
  541.      abbreviated load_map structure */
  542.       new = (struct so_list *) xmalloc (sizeof (struct so_list));
  543.       memset ((char *) new, 0, sizeof (struct so_list));
  544.       new -> lmaddr = lm;
  545.       /* Add the new node as the next node in the list, or as the root
  546.      node if this is the first one. */
  547.       if (so_list_ptr != NULL)
  548.     {
  549.       so_list_ptr -> next = new;
  550.     }
  551.       else
  552.     {
  553.       so_list_head = new;
  554.     }      
  555.       so_list_next = new;
  556.       xfer_link_map_member (new, lm);
  557.     }
  558.   return (so_list_next);
  559. }
  560.  
  561. /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
  562.  
  563. static int
  564. symbol_add_stub (arg)
  565.      char *arg;
  566. {
  567.   register struct so_list *so = (struct so_list *) arg;    /* catch_errs bogon */
  568.   
  569.   so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
  570.                    so -> textsection -> addr,
  571.                    0, 0, 0);
  572.   return (1);
  573. }
  574.  
  575. /*
  576.  
  577. GLOBAL FUNCTION
  578.  
  579.     solib_add -- add a shared library file to the symtab and section list
  580.  
  581. SYNOPSIS
  582.  
  583.     void solib_add (char *arg_string, int from_tty,
  584.             struct target_ops *target)
  585.  
  586. DESCRIPTION
  587.  
  588. */
  589.  
  590. void
  591. solib_add (arg_string, from_tty, target)
  592.      char *arg_string;
  593.      int from_tty;
  594.      struct target_ops *target;
  595. {    
  596.   register struct so_list *so = NULL;       /* link map state variable */
  597.  
  598.   /* Last shared library that we read.  */
  599.   struct so_list *so_last = NULL;
  600.  
  601.   char *re_err;
  602.   int count;
  603.   int old;
  604.   
  605.   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
  606.     {
  607.       error ("Invalid regexp: %s", re_err);
  608.     }
  609.   
  610.   
  611.   /* Add the shared library sections to the section table of the
  612.      specified target, if any.  */
  613.   if (target)
  614.     {
  615.       /* Count how many new section_table entries there are.  */
  616.       so = NULL;
  617.       count = 0;
  618.       while ((so = find_solib (so)) != NULL)
  619.     {
  620.       if (so -> so_name[0])
  621.         {
  622.           count += so -> sections_end - so -> sections;
  623.         }
  624.     }
  625.       
  626.       if (count)
  627.     {
  628.       /* Reallocate the target's section table including the new size.  */
  629.       if (target -> to_sections)
  630.         {
  631.           old = target -> to_sections_end - target -> to_sections;
  632.           target -> to_sections = (struct section_table *)
  633.         xrealloc ((char *)target -> to_sections,
  634.              (sizeof (struct section_table)) * (count + old));
  635.         }
  636.       else
  637.         {
  638.           old = 0;
  639.           target -> to_sections = (struct section_table *)
  640.         xmalloc ((sizeof (struct section_table)) * count);
  641.         }
  642.       target -> to_sections_end = target -> to_sections + (count + old);
  643.       
  644.       /* Add these section table entries to the target's table.  */
  645.       while ((so = find_solib (so)) != NULL)
  646.         {
  647.           if (so -> so_name[0])
  648.         {
  649.           count = so -> sections_end - so -> sections;
  650.           memcpy ((char *) (target -> to_sections + old),
  651.               so -> sections, 
  652.               (sizeof (struct section_table)) * count);
  653.           old += count;
  654.         }
  655.         }
  656.     }
  657.     }
  658.   
  659.   /* Now add the symbol files.  */
  660.   so = NULL;
  661.   while ((so = find_solib (so)) != NULL)
  662.     {
  663.       if (so -> so_name[0] && re_exec (so -> so_name))
  664.     {
  665.       so -> from_tty = from_tty;
  666.       if (so -> symbols_loaded)
  667.         {
  668.           if (from_tty)
  669.         {
  670.           printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
  671.         }
  672.         }
  673.       else if (catch_errors
  674.            (symbol_add_stub, (char *) so,
  675.             "Error while reading shared library symbols:\n",
  676.             RETURN_MASK_ALL))
  677.         {
  678.           so_last = so;
  679.           so -> symbols_loaded = 1;
  680.         }
  681.     }
  682.     }
  683.  
  684.   /* Getting new symbols may change our opinion about what is
  685.      frameless.  */
  686.   if (so_last)
  687.     reinit_frame_cache ();
  688. }
  689.  
  690. /*
  691.  
  692. LOCAL FUNCTION
  693.  
  694.     info_sharedlibrary_command -- code for "info sharedlibrary"
  695.  
  696. SYNOPSIS
  697.  
  698.     static void info_sharedlibrary_command ()
  699.  
  700. DESCRIPTION
  701.  
  702.     Walk through the shared library list and print information
  703.     about each attached library.
  704. */
  705.  
  706. static void
  707. info_sharedlibrary_command (ignore, from_tty)
  708.      char *ignore;
  709.      int from_tty;
  710. {
  711.   register struct so_list *so = NULL;      /* link map state variable */
  712.   int header_done = 0;
  713.   
  714.   if (exec_bfd == NULL)
  715.     {
  716.       printf_unfiltered ("No exec file.\n");
  717.       return;
  718.     }
  719.   while ((so = find_solib (so)) != NULL)
  720.     {
  721.       if (so -> so_name[0])
  722.     {
  723.       unsigned long txt_start = 0;
  724.       unsigned long txt_end = 0;
  725.  
  726.       if (!header_done)
  727.         {
  728.           printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
  729.              "Shared Object Library");
  730.           header_done++;
  731.         }
  732.       if (so -> textsection)
  733.         {
  734.           txt_start = (unsigned long) so -> textsection -> addr;
  735.           txt_end = (unsigned long) so -> textsection -> endaddr;
  736.         }
  737.       printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
  738.       printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
  739.       printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
  740.       printf_unfiltered ("%s\n",  so -> so_name);
  741.     }
  742.     }
  743.   if (so_list_head == NULL)
  744.     {
  745.       printf_unfiltered ("No shared libraries loaded at this time.\n");    
  746.     }
  747. }
  748.  
  749. /*
  750.  
  751. GLOBAL FUNCTION
  752.  
  753.     solib_address -- check to see if an address is in a shared lib
  754.  
  755. SYNOPSIS
  756.  
  757.     int solib_address (CORE_ADDR address)
  758.  
  759. DESCRIPTION
  760.  
  761.     Provides a hook for other gdb routines to discover whether or
  762.     not a particular address is within the mapped address space of
  763.     a shared library.  Any address between the base mapping address
  764.     and the first address beyond the end of the last mapping, is
  765.     considered to be within the shared library address space, for
  766.     our purposes.
  767.  
  768.     For example, this routine is called at one point to disable
  769.     breakpoints which are in shared libraries that are not currently
  770.     mapped in.
  771.  */
  772.  
  773. int
  774. solib_address (address)
  775.      CORE_ADDR address;
  776. {
  777.   register struct so_list *so = 0;       /* link map state variable */
  778.   
  779.   while ((so = find_solib (so)) != NULL)
  780.     {
  781.       if (so -> so_name[0] && so -> textsection)
  782.     {
  783.       if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
  784.           (address < (CORE_ADDR) so -> textsection -> endaddr))
  785.         {
  786.           return (1);
  787.         }
  788.     }
  789.     }
  790.   return (0);
  791. }
  792.  
  793. /* Called by free_all_symtabs */
  794.  
  795. void 
  796. clear_solib()
  797. {
  798.   struct so_list *next;
  799.   char *bfd_filename;
  800.   
  801.   while (so_list_head)
  802.     {
  803.       if (so_list_head -> sections)
  804.     {
  805.       free ((PTR)so_list_head -> sections);
  806.     }
  807.       if (so_list_head -> abfd)
  808.     {
  809.       bfd_filename = bfd_get_filename (so_list_head -> abfd);
  810.       if (!bfd_close (so_list_head -> abfd))
  811.         warning ("cannot close \"%s\": %s",
  812.              bfd_filename, bfd_errmsg (bfd_get_error ()));
  813.     }
  814.       else
  815.     /* This happens for the executable on SVR4.  */
  816.     bfd_filename = NULL;
  817.       
  818.       next = so_list_head -> next;
  819.       if (bfd_filename)
  820.     free ((PTR)bfd_filename);
  821.       free ((PTR)so_list_head);
  822.       so_list_head = next;
  823.     }
  824. }
  825.   
  826. /*
  827.   
  828. GLOBAL FUNCTION
  829.   
  830.     solib_create_inferior_hook -- shared library startup support
  831.   
  832. SYNOPSIS
  833.   
  834.     void solib_create_inferior_hook()
  835.   
  836. DESCRIPTION
  837.  
  838.     When gdb starts up the inferior, it nurses it along (through the
  839.     shell) until it is ready to execute it's first instruction.  At this
  840.     point, this function gets called via expansion of the macro
  841.     SOLIB_CREATE_INFERIOR_HOOK.
  842.     For a statically bound executable, this first instruction is the
  843.     one at "_start", or a similar text label. No further processing is
  844.     needed in that case.
  845.     For a dynamically bound executable, this first instruction is somewhere
  846.     in the rld, and the actual user executable is not yet mapped in.
  847.     We continue the inferior again, rld then maps in the actual user
  848.     executable and any needed shared libraries and then sends
  849.     itself a SIGTRAP.
  850.     At that point we discover the names of all shared libraries and
  851.     read their symbols in.
  852.  
  853. FIXME
  854.  
  855.     This code does not properly handle hitting breakpoints which the
  856.     user might have set in the rld itself.  Proper handling would have
  857.     to check if the SIGTRAP happened due to a kill call.
  858.  
  859.     Also, what if child has exit()ed?  Must exit loop somehow.
  860.   */
  861.  
  862. void
  863. solib_create_inferior_hook()
  864. {
  865.  
  866.   /* Nothing to do for statically bound executables.  */
  867.  
  868.   if (symfile_objfile == NULL
  869.       || symfile_objfile->obfd == NULL
  870.       || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
  871.     return;
  872.  
  873.   /* Now run the target.  It will eventually get a SIGTRAP, at
  874.      which point all of the libraries will have been mapped in and we
  875.      can go groveling around in the rld structures to find
  876.      out what we need to know about them. */
  877.  
  878.   clear_proceed_status ();
  879.   stop_soon_quietly = 1;
  880.   stop_signal = TARGET_SIGNAL_0;
  881.   do
  882.     {
  883.       target_resume (-1, 0, stop_signal);
  884.       wait_for_inferior ();
  885.     }
  886.   while (stop_signal != TARGET_SIGNAL_TRAP);
  887.  
  888.   /*  solib_add will call reinit_frame_cache.
  889.       But we are stopped in the runtime loader and we do not have symbols
  890.       for the runtime loader. So heuristic_proc_start will be called
  891.       and will put out an annoying warning.
  892.       Delaying the resetting of stop_soon_quietly until after symbol loading
  893.       suppresses the warning.  */
  894.   solib_add ((char *) 0, 0, (struct target_ops *) 0);
  895.   stop_soon_quietly = 0;
  896. }
  897.  
  898.  
  899. /*
  900.  
  901. LOCAL FUNCTION
  902.  
  903.     sharedlibrary_command -- handle command to explicitly add library
  904.  
  905. SYNOPSIS
  906.  
  907.     static void sharedlibrary_command (char *args, int from_tty)
  908.  
  909. DESCRIPTION
  910.  
  911. */
  912.  
  913. static void
  914. sharedlibrary_command (args, from_tty)
  915. char *args;
  916. int from_tty;
  917. {
  918.   dont_repeat ();
  919.   solib_add (args, from_tty, (struct target_ops *) 0);
  920. }
  921.  
  922. void
  923. _initialize_solib()
  924. {
  925.   
  926.   add_com ("sharedlibrary", class_files, sharedlibrary_command,
  927.        "Load shared object library symbols for files matching REGEXP.");
  928.   add_info ("sharedlibrary", info_sharedlibrary_command, 
  929.         "Status of loaded shared object libraries.");
  930. }
  931.