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 / irix5-nat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-06  |  29.4 KB  |  1,086 lines

  1. /* Native support for the SGI Iris running IRIX version 5, for GDB.
  2.    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.    Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
  5.    and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
  6.    Implemented for Irix 4.x by Garrett A. Wollman.
  7.    Modified for Irix 5.x by Ian Lance Taylor.
  8.  
  9. This file is part of GDB.
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  24.  
  25. #include "defs.h"
  26. #include "inferior.h"
  27. #include "gdbcore.h"
  28. #include "target.h"
  29.  
  30. #include <sys/time.h>
  31. #include <sys/procfs.h>
  32. #include <setjmp.h>        /* For JB_XXX.  */
  33.  
  34. /* Size of elements in jmpbuf */
  35.  
  36. #define JB_ELEMENT_SIZE 4
  37.  
  38. /*
  39.  * See the comment in m68k-tdep.c regarding the utility of these functions.
  40.  *
  41.  * These definitions are from the MIPS SVR4 ABI, so they may work for
  42.  * any MIPS SVR4 target.
  43.  */
  44.  
  45. void 
  46. supply_gregset (gregsetp)
  47.      gregset_t *gregsetp;
  48. {
  49.   register int regi;
  50.   register greg_t *regp = &(*gregsetp)[0];
  51.  
  52.   for(regi = 0; regi <= CTX_RA; regi++)
  53.     supply_register (regi, (char *)(regp + regi));
  54.  
  55.   supply_register (PC_REGNUM, (char *)(regp + CTX_EPC));
  56.   supply_register (HI_REGNUM, (char *)(regp + CTX_MDHI));
  57.   supply_register (LO_REGNUM, (char *)(regp + CTX_MDLO));
  58.   supply_register (CAUSE_REGNUM, (char *)(regp + CTX_CAUSE));
  59. }
  60.  
  61. void
  62. fill_gregset (gregsetp, regno)
  63.      gregset_t *gregsetp;
  64.      int regno;
  65. {
  66.   int regi;
  67.   register greg_t *regp = &(*gregsetp)[0];
  68.  
  69.   for (regi = 0; regi <= CTX_RA; regi++)
  70.     if ((regno == -1) || (regno == regi))
  71.       *(regp + regi) = *(greg_t *) ®isters[REGISTER_BYTE (regi)];
  72.  
  73.   if ((regno == -1) || (regno == PC_REGNUM))
  74.     *(regp + CTX_EPC) = *(greg_t *) ®isters[REGISTER_BYTE (PC_REGNUM)];
  75.  
  76.   if ((regno == -1) || (regno == CAUSE_REGNUM))
  77.     *(regp + CTX_CAUSE) = *(greg_t *) ®isters[REGISTER_BYTE (PS_REGNUM)];
  78.  
  79.   if ((regno == -1) || (regno == HI_REGNUM))
  80.     *(regp + CTX_MDHI) = *(greg_t *) ®isters[REGISTER_BYTE (HI_REGNUM)];
  81.  
  82.   if ((regno == -1) || (regno == LO_REGNUM))
  83.     *(regp + CTX_MDLO) = *(greg_t *) ®isters[REGISTER_BYTE (LO_REGNUM)];
  84. }
  85.  
  86. /*
  87.  * Now we do the same thing for floating-point registers.
  88.  * We don't bother to condition on FP0_REGNUM since any
  89.  * reasonable MIPS configuration has an R3010 in it.
  90.  *
  91.  * Again, see the comments in m68k-tdep.c.
  92.  */
  93.  
  94. void
  95. supply_fpregset (fpregsetp)
  96.      fpregset_t *fpregsetp;
  97. {
  98.   register int regi;
  99.  
  100.   for (regi = 0; regi < 32; regi++)
  101.     supply_register (FP0_REGNUM + regi,
  102.              (char *)&fpregsetp->fp_r.fp_regs[regi]);
  103.  
  104.   supply_register (FCRCS_REGNUM, (char *)&fpregsetp->fp_csr);
  105.  
  106.   /* FIXME: how can we supply FCRIR_REGNUM?  SGI doesn't tell us. */
  107. }
  108.  
  109. void
  110. fill_fpregset (fpregsetp, regno)
  111.      fpregset_t *fpregsetp;
  112.      int regno;
  113. {
  114.   int regi;
  115.   char *from, *to;
  116.  
  117.   for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
  118.     {
  119.       if ((regno == -1) || (regno == regi))
  120.     {
  121.       from = (char *) ®isters[REGISTER_BYTE (regi)];
  122.       to = (char *) &(fpregsetp->fp_r.fp_regs[regi - FP0_REGNUM]);
  123.       memcpy(to, from, REGISTER_RAW_SIZE (regi));
  124.     }
  125.     }
  126.  
  127.   if ((regno == -1) || (regno == FCRCS_REGNUM))
  128.     fpregsetp->fp_csr = *(unsigned *) ®isters[REGISTER_BYTE(FCRCS_REGNUM)];
  129. }
  130.  
  131.  
  132. /* Figure out where the longjmp will land.
  133.    We expect the first arg to be a pointer to the jmp_buf structure from which
  134.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  135.    This routine returns true on success. */
  136.  
  137. int
  138. get_longjmp_target (pc)
  139.      CORE_ADDR *pc;
  140. {
  141.   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
  142.   CORE_ADDR jb_addr;
  143.  
  144.   jb_addr = read_register (A0_REGNUM);
  145.  
  146.   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
  147.               TARGET_PTR_BIT / TARGET_CHAR_BIT))
  148.     return 0;
  149.  
  150.   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
  151.  
  152.   return 1;
  153. }
  154.  
  155. void
  156. fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
  157.      char *core_reg_sect;
  158.      unsigned core_reg_size;
  159.      int which;            /* Unused */
  160.      unsigned int reg_addr;    /* Unused */
  161. {
  162.   if (core_reg_size != REGISTER_BYTES)
  163.     {
  164.       warning ("wrong size gregset struct in core file");
  165.       return;
  166.     }
  167.  
  168.   memcpy ((char *)registers, core_reg_sect, core_reg_size);
  169. }
  170.  
  171. /* Irix 5 uses what appears to be a unique form of shared library
  172.    support.  This is a copy of solib.c modified for Irix 5.  */
  173.  
  174. #include <sys/types.h>
  175. #include <signal.h>
  176. #include <string.h>
  177. #include <sys/param.h>
  178. #include <fcntl.h>
  179.  
  180. /* <obj.h> includes <sym.h> and <symconst.h>, which causes conflicts
  181.    with our versions of those files included by tm-mips.h.  Prevent
  182.    <obj.h> from including them with some appropriate defines.  */
  183. #define __SYM_H__
  184. #define __SYMCONST_H__
  185. #include <obj.h>
  186.  
  187. #include "symtab.h"
  188. #include "bfd.h"
  189. #include "symfile.h"
  190. #include "objfiles.h"
  191. #include "command.h"
  192. #include "frame.h"
  193. #include "regex.h"
  194. #include "inferior.h"
  195. #include "language.h"
  196.  
  197. /* We need to set a breakpoint at a point when we know that the
  198.    mapping of shared libraries is complete.  dbx simply breaks at main
  199.    (or, for FORTRAN, MAIN__), so we do the same.  We can not break at
  200.    the very beginning of main, because the startup code will jump into
  201.    main after the GP initialization instructions.  SOLIB_BKPT_OFFSET
  202.    is used to skip those instructions.  */
  203.  
  204. #define SOLIB_BKPT_OFFSET 12
  205.  
  206. static char *bkpt_names[] = {
  207.   "main",
  208.   "MAIN__",
  209.   NULL
  210. };
  211.  
  212. /* The symbol which starts off the list of shared libraries.  */
  213. #define DEBUG_BASE "__rld_obj_head"
  214.  
  215. /* How to get the loaded address of a shared library.  */
  216. #define LM_ADDR(so) ((so)->lm.o_praw)
  217.  
  218. char shadow_contents[BREAKPOINT_MAX];    /* Stash old bkpt addr contents */
  219.  
  220. struct so_list {
  221.   struct so_list *next;            /* next structure in linked list */
  222.   struct obj_list ll;
  223.   struct obj lm;            /* copy of link map from inferior */
  224.   struct obj_list *lladdr;        /* addr in inferior lm was read from */
  225.   CORE_ADDR lmend;            /* upper addr bound of mapped object */
  226.   char symbols_loaded;            /* flag: symbols read in yet? */
  227.   char from_tty;            /* flag: print msgs? */
  228.   struct objfile *objfile;        /* objfile for loaded lib */
  229.   struct section_table *sections;
  230.   struct section_table *sections_end;
  231.   struct section_table *textsection;
  232.   bfd *abfd;
  233. };
  234.  
  235. static struct so_list *so_list_head;    /* List of known shared objects */
  236. static CORE_ADDR debug_base;        /* Base of dynamic linker structures */
  237. static CORE_ADDR breakpoint_addr;    /* Address where end bkpt is set */
  238.  
  239. /* Local function prototypes */
  240.  
  241. static void
  242. sharedlibrary_command PARAMS ((char *, int));
  243.  
  244. static int
  245. enable_break PARAMS ((void));
  246.  
  247. static int
  248. disable_break PARAMS ((void));
  249.  
  250. static void
  251. info_sharedlibrary_command PARAMS ((char *, int));
  252.  
  253. static int
  254. symbol_add_stub PARAMS ((char *));
  255.  
  256. static struct so_list *
  257. find_solib PARAMS ((struct so_list *));
  258.  
  259. static struct obj_list *
  260. first_link_map_member PARAMS ((void));
  261.  
  262. static CORE_ADDR
  263. locate_base PARAMS ((void));
  264.  
  265. static void
  266. solib_map_sections PARAMS ((struct so_list *));
  267.  
  268. /*
  269.  
  270. LOCAL FUNCTION
  271.  
  272.     solib_map_sections -- open bfd and build sections for shared lib
  273.  
  274. SYNOPSIS
  275.  
  276.     static void solib_map_sections (struct so_list *so)
  277.  
  278. DESCRIPTION
  279.  
  280.     Given a pointer to one of the shared objects in our list
  281.     of mapped objects, use the recorded name to open a bfd
  282.     descriptor for the object, build a section table, and then
  283.     relocate all the section addresses by the base address at
  284.     which the shared object was mapped.
  285.  
  286. FIXMES
  287.  
  288.     In most (all?) cases the shared object file name recorded in the
  289.     dynamic linkage tables will be a fully qualified pathname.  For
  290.     cases where it isn't, do we really mimic the systems search
  291.     mechanism correctly in the below code (particularly the tilde
  292.     expansion stuff?).
  293.  */
  294.  
  295. static void
  296. solib_map_sections (so)
  297.      struct so_list *so;
  298. {
  299.   char *filename;
  300.   char *scratch_pathname;
  301.   int scratch_chan;
  302.   struct section_table *p;
  303.   struct cleanup *old_chain;
  304.   bfd *abfd;
  305.   CORE_ADDR offset;
  306.   
  307.   filename = tilde_expand (so -> lm.o_path);
  308.   old_chain = make_cleanup (free, filename);
  309.   
  310.   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  311.             &scratch_pathname);
  312.   if (scratch_chan < 0)
  313.     {
  314.       scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
  315.                 O_RDONLY, 0, &scratch_pathname);
  316.     }
  317.   if (scratch_chan < 0)
  318.     {
  319.       perror_with_name (filename);
  320.     }
  321.   /* Leave scratch_pathname allocated.  abfd->name will point to it.  */
  322.  
  323.   abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
  324.   if (!abfd)
  325.     {
  326.       close (scratch_chan);
  327.       error ("Could not open `%s' as an executable file: %s",
  328.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  329.     }
  330.   /* Leave bfd open, core_xfer_memory and "info files" need it.  */
  331.   so -> abfd = abfd;
  332.   abfd -> cacheable = true;
  333.  
  334.   if (!bfd_check_format (abfd, bfd_object))
  335.     {
  336.       error ("\"%s\": not in executable format: %s.",
  337.          scratch_pathname, bfd_errmsg (bfd_get_error ()));
  338.     }
  339.   if (build_section_table (abfd, &so -> sections, &so -> sections_end))
  340.     {
  341.       error ("Can't find the file sections in `%s': %s", 
  342.          bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
  343.     }
  344.  
  345.   /* Irix 5 shared objects are pre-linked to particular addresses
  346.      although the dynamic linker may have to relocate them if the
  347.      address ranges of the libraries used by the main program clash.
  348.      The offset is the difference between the address where the object
  349.      is mapped and the binding address of the shared library.  */
  350.   offset = (CORE_ADDR) LM_ADDR (so) - so -> lm.o_base_address;
  351.  
  352.   for (p = so -> sections; p < so -> sections_end; p++)
  353.     {
  354.       /* Relocate the section binding addresses as recorded in the shared
  355.      object's file by the offset to get the address to which the
  356.      object was actually mapped.  */
  357.       p -> addr += offset;
  358.       p -> endaddr += offset;
  359.       so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
  360.       if (STREQ (p -> the_bfd_section -> name, ".text"))
  361.     {
  362.       so -> textsection = p;
  363.     }
  364.     }
  365.  
  366.   /* Free the file names, close the file now.  */
  367.   do_cleanups (old_chain);
  368. }
  369.  
  370. /*
  371.  
  372. LOCAL FUNCTION
  373.  
  374.     locate_base -- locate the base address of dynamic linker structs
  375.  
  376. SYNOPSIS
  377.  
  378.     CORE_ADDR locate_base (void)
  379.  
  380. DESCRIPTION
  381.  
  382.     For both the SunOS and SVR4 shared library implementations, if the
  383.     inferior executable has been linked dynamically, there is a single
  384.     address somewhere in the inferior's data space which is the key to
  385.     locating all of the dynamic linker's runtime structures.  This
  386.     address is the value of the symbol defined by the macro DEBUG_BASE.
  387.     The job of this function is to find and return that address, or to
  388.     return 0 if there is no such address (the executable is statically
  389.     linked for example).
  390.  
  391.     For SunOS, the job is almost trivial, since the dynamic linker and
  392.     all of it's structures are statically linked to the executable at
  393.     link time.  Thus the symbol for the address we are looking for has
  394.     already been added to the minimal symbol table for the executable's
  395.     objfile at the time the symbol file's symbols were read, and all we
  396.     have to do is look it up there.  Note that we explicitly do NOT want
  397.     to find the copies in the shared library.
  398.  
  399.     The SVR4 version is much more complicated because the dynamic linker
  400.     and it's structures are located in the shared C library, which gets
  401.     run as the executable's "interpreter" by the kernel.  We have to go
  402.     to a lot more work to discover the address of DEBUG_BASE.  Because
  403.     of this complexity, we cache the value we find and return that value
  404.     on subsequent invocations.  Note there is no copy in the executable
  405.     symbol tables.
  406.  
  407.     Irix 5 is basically like SunOS.
  408.  
  409.     Note that we can assume nothing about the process state at the time
  410.     we need to find this address.  We may be stopped on the first instruc-
  411.     tion of the interpreter (C shared library), the first instruction of
  412.     the executable itself, or somewhere else entirely (if we attached
  413.     to the process for example).
  414.  
  415.  */
  416.  
  417. static CORE_ADDR
  418. locate_base ()
  419. {
  420.   struct minimal_symbol *msymbol;
  421.   CORE_ADDR address = 0;
  422.  
  423.   msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
  424.   if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
  425.     {
  426.       address = SYMBOL_VALUE_ADDRESS (msymbol);
  427.     }
  428.   return (address);
  429. }
  430.  
  431. /*
  432.  
  433. LOCAL FUNCTION
  434.  
  435.     first_link_map_member -- locate first member in dynamic linker's map
  436.  
  437. SYNOPSIS
  438.  
  439.     static struct link_map *first_link_map_member (void)
  440.  
  441. DESCRIPTION
  442.  
  443.     Read in a copy of the first member in the inferior's dynamic
  444.     link map from the inferior's dynamic linker structures, and return
  445.     a pointer to the copy in our address space.
  446. */
  447.  
  448. static struct obj_list *
  449. first_link_map_member ()
  450. {
  451.   struct obj_list *lm;
  452.   struct obj_list s;
  453.  
  454.   read_memory (debug_base, (char *) &lm, sizeof (struct obj_list *));
  455.  
  456.   if (lm == NULL)
  457.     return NULL;
  458.  
  459.   /* The first entry in the list is the object file we are debugging,
  460.      so skip it.  */
  461.   read_memory ((CORE_ADDR) lm, (char *) &s, sizeof (struct obj_list));
  462.  
  463.   return s.next;
  464. }
  465.  
  466. /*
  467.  
  468. LOCAL FUNCTION
  469.  
  470.     find_solib -- step through list of shared objects
  471.  
  472. SYNOPSIS
  473.  
  474.     struct so_list *find_solib (struct so_list *so_list_ptr)
  475.  
  476. DESCRIPTION
  477.  
  478.     This module contains the routine which finds the names of any
  479.     loaded "images" in the current process. The argument in must be
  480.     NULL on the first call, and then the returned value must be passed
  481.     in on subsequent calls. This provides the capability to "step" down
  482.     the list of loaded objects. On the last object, a NULL value is
  483.     returned.
  484.  */
  485.  
  486. static struct so_list *
  487. find_solib (so_list_ptr)
  488.      struct so_list *so_list_ptr;    /* Last lm or NULL for first one */
  489. {
  490.   struct so_list *so_list_next = NULL;
  491.   struct obj_list *lm = NULL;
  492.   struct so_list *new;
  493.   
  494.   if (so_list_ptr == NULL)
  495.     {
  496.       /* We are setting up for a new scan through the loaded images. */
  497.       if ((so_list_next = so_list_head) == NULL)
  498.     {
  499.       /* We have not already read in the dynamic linking structures
  500.          from the inferior, lookup the address of the base structure. */
  501.       debug_base = locate_base ();
  502.       if (debug_base != 0)
  503.         {
  504.           /* Read the base structure in and find the address of the first
  505.          link map list member. */
  506.           lm = first_link_map_member ();
  507.         }
  508.     }
  509.     }
  510.   else
  511.     {
  512.       /* We have been called before, and are in the process of walking
  513.      the shared library list.  Advance to the next shared object. */
  514.       if ((lm = so_list_ptr->ll.next) == NULL)
  515.     {
  516.       /* We have hit the end of the list, so check to see if any were
  517.          added, but be quiet if we can't read from the target any more. */
  518.       int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lladdr,
  519.                        (char *) &(so_list_ptr -> ll),
  520.                        sizeof (struct obj_list));
  521.       if (status == 0)
  522.         {
  523.           lm = so_list_ptr->ll.next;
  524.         }
  525.       else
  526.         {
  527.           lm = NULL;
  528.         }
  529.     }
  530.       so_list_next = so_list_ptr -> next;
  531.     }
  532.   if ((so_list_next == NULL) && (lm != NULL))
  533.     {
  534.       int errcode;
  535.       char *buffer;
  536.  
  537.       /* Get next link map structure from inferior image and build a local
  538.      abbreviated load_map structure */
  539.       new = (struct so_list *) xmalloc (sizeof (struct so_list));
  540.       memset ((char *) new, 0, sizeof (struct so_list));
  541.       new -> lladdr = lm;
  542.       /* Add the new node as the next node in the list, or as the root
  543.      node if this is the first one. */
  544.       if (so_list_ptr != NULL)
  545.     {
  546.       so_list_ptr -> next = new;
  547.     }
  548.       else
  549.     {
  550.       so_list_head = new;
  551.     }      
  552.       so_list_next = new;
  553.       read_memory ((CORE_ADDR) lm, (char *) &(new -> ll),
  554.            sizeof (struct obj_list));
  555.       read_memory ((CORE_ADDR) new->ll.data, (char *) &(new -> lm),
  556.            sizeof (struct obj));
  557.       target_read_string ((CORE_ADDR)new->lm.o_path, &buffer,
  558.               INT_MAX, &errcode);
  559.       if (errcode != 0)
  560.     memory_error (errcode, (CORE_ADDR)new->lm.o_path);
  561.       new->lm.o_path = buffer;
  562.       solib_map_sections (new);
  563.     }
  564.   return (so_list_next);
  565. }
  566.  
  567. /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
  568.  
  569. static int
  570. symbol_add_stub (arg)
  571.      char *arg;
  572. {
  573.   register struct so_list *so = (struct so_list *) arg;    /* catch_errs bogon */
  574.   
  575.   so -> objfile = symbol_file_add (so -> lm.o_path, so -> from_tty,
  576.                    (unsigned int) so -> textsection -> addr,
  577.                    0, 0, 0);
  578.   return (1);
  579. }
  580.  
  581. /*
  582.  
  583. GLOBAL FUNCTION
  584.  
  585.     solib_add -- add a shared library file to the symtab and section list
  586.  
  587. SYNOPSIS
  588.  
  589.     void solib_add (char *arg_string, int from_tty,
  590.             struct target_ops *target)
  591.  
  592. DESCRIPTION
  593.  
  594. */
  595.  
  596. void
  597. solib_add (arg_string, from_tty, target)
  598.      char *arg_string;
  599.      int from_tty;
  600.      struct target_ops *target;
  601. {    
  602.   register struct so_list *so = NULL;       /* link map state variable */
  603.  
  604.   /* Last shared library that we read.  */
  605.   struct so_list *so_last = NULL;
  606.  
  607.   char *re_err;
  608.   int count;
  609.   int old;
  610.   
  611.   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
  612.     {
  613.       error ("Invalid regexp: %s", re_err);
  614.     }
  615.   
  616.   /* Add the shared library sections to the section table of the
  617.      specified target, if any. We have to do this before reading the
  618.      symbol files as symbol_file_add calls reinit_frame_cache and
  619.      creating a new frame might access memory in the shared library.  */
  620.   if (target)
  621.     {
  622.       /* Count how many new section_table entries there are.  */
  623.       so = NULL;
  624.       count = 0;
  625.       while ((so = find_solib (so)) != NULL)
  626.     {
  627.       if (so -> lm.o_path[0])
  628.         {
  629.           count += so -> sections_end - so -> sections;
  630.         }
  631.     }
  632.       
  633.       if (count)
  634.     {
  635.       /* Reallocate the target's section table including the new size.  */
  636.       if (target -> to_sections)
  637.         {
  638.           old = target -> to_sections_end - target -> to_sections;
  639.           target -> to_sections = (struct section_table *)
  640.         xrealloc ((char *)target -> to_sections,
  641.              (sizeof (struct section_table)) * (count + old));
  642.         }
  643.       else
  644.         {
  645.           old = 0;
  646.           target -> to_sections = (struct section_table *)
  647.         xmalloc ((sizeof (struct section_table)) * count);
  648.         }
  649.       target -> to_sections_end = target -> to_sections + (count + old);
  650.       
  651.       /* Add these section table entries to the target's table.  */
  652.       while ((so = find_solib (so)) != NULL)
  653.         {
  654.           if (so -> lm.o_path[0])
  655.         {
  656.           count = so -> sections_end - so -> sections;
  657.           memcpy ((char *) (target -> to_sections + old),
  658.               so -> sections, 
  659.               (sizeof (struct section_table)) * count);
  660.           old += count;
  661.         }
  662.         }
  663.     }
  664.     }
  665.   
  666.   /* Now add the symbol files.  */
  667.   while ((so = find_solib (so)) != NULL)
  668.     {
  669.       if (so -> lm.o_path[0] && re_exec (so -> lm.o_path))
  670.     {
  671.       so -> from_tty = from_tty;
  672.       if (so -> symbols_loaded)
  673.         {
  674.           if (from_tty)
  675.         {
  676.           printf_unfiltered ("Symbols already loaded for %s\n", so -> lm.o_path);
  677.         }
  678.         }
  679.       else if (catch_errors
  680.            (symbol_add_stub, (char *) so,
  681.             "Error while reading shared library symbols:\n",
  682.             RETURN_MASK_ALL))
  683.         {
  684.           so_last = so;
  685.           so -> symbols_loaded = 1;
  686.         }
  687.     }
  688.     }
  689. }
  690.  
  691. /*
  692.  
  693. LOCAL FUNCTION
  694.  
  695.     info_sharedlibrary_command -- code for "info sharedlibrary"
  696.  
  697. SYNOPSIS
  698.  
  699.     static void info_sharedlibrary_command ()
  700.  
  701. DESCRIPTION
  702.  
  703.     Walk through the shared library list and print information
  704.     about each attached library.
  705. */
  706.  
  707. static void
  708. info_sharedlibrary_command (ignore, from_tty)
  709.      char *ignore;
  710.      int from_tty;
  711. {
  712.   register struct so_list *so = NULL;      /* link map state variable */
  713.   int header_done = 0;
  714.   
  715.   if (exec_bfd == NULL)
  716.     {
  717.       printf_unfiltered ("No exec file.\n");
  718.       return;
  719.     }
  720.   while ((so = find_solib (so)) != NULL)
  721.     {
  722.       if (so -> lm.o_path[0])
  723.     {
  724.       if (!header_done)
  725.         {
  726.           printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
  727.              "Shared Object Library");
  728.           header_done++;
  729.         }
  730.       printf_unfiltered ("%-12s",
  731.           local_hex_string_custom ((unsigned long) LM_ADDR (so),
  732.                        "08l"));
  733.       printf_unfiltered ("%-12s",
  734.           local_hex_string_custom ((unsigned long) so -> lmend,
  735.                        "08l"));
  736.       printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
  737.       printf_unfiltered ("%s\n",  so -> lm.o_path);
  738.     }
  739.     }
  740.   if (so_list_head == NULL)
  741.     {
  742.       printf_unfiltered ("No shared libraries loaded at this time.\n");    
  743.     }
  744. }
  745.  
  746. /*
  747.  
  748. GLOBAL FUNCTION
  749.  
  750.     solib_address -- check to see if an address is in a shared lib
  751.  
  752. SYNOPSIS
  753.  
  754.     int solib_address (CORE_ADDR address)
  755.  
  756. DESCRIPTION
  757.  
  758.     Provides a hook for other gdb routines to discover whether or
  759.     not a particular address is within the mapped address space of
  760.     a shared library.  Any address between the base mapping address
  761.     and the first address beyond the end of the last mapping, is
  762.     considered to be within the shared library address space, for
  763.     our purposes.
  764.  
  765.     For example, this routine is called at one point to disable
  766.     breakpoints which are in shared libraries that are not currently
  767.     mapped in.
  768.  */
  769.  
  770. int
  771. solib_address (address)
  772.      CORE_ADDR address;
  773. {
  774.   register struct so_list *so = 0;       /* link map state variable */
  775.   
  776.   while ((so = find_solib (so)) != NULL)
  777.     {
  778.       if (so -> lm.o_path[0])
  779.     {
  780.       if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
  781.           (address < (CORE_ADDR) so -> lmend))
  782.         {
  783.           return (1);
  784.         }
  785.     }
  786.     }
  787.   return (0);
  788. }
  789.  
  790. /* Called by free_all_symtabs */
  791.  
  792. void 
  793. clear_solib()
  794. {
  795.   struct so_list *next;
  796.   char *bfd_filename;
  797.   
  798.   while (so_list_head)
  799.     {
  800.       if (so_list_head -> sections)
  801.     {
  802.       free ((PTR)so_list_head -> sections);
  803.     }
  804.       if (so_list_head -> abfd)
  805.     {
  806.       bfd_filename = bfd_get_filename (so_list_head -> abfd);
  807.       bfd_close (so_list_head -> abfd);
  808.     }
  809.       else
  810.     /* This happens for the executable on SVR4.  */
  811.     bfd_filename = NULL;
  812.  
  813.       next = so_list_head -> next;
  814.       if (bfd_filename)
  815.     free ((PTR)bfd_filename);
  816.       free (so_list_head->lm.o_path);
  817.       free ((PTR)so_list_head);
  818.       so_list_head = next;
  819.     }
  820.   debug_base = 0;
  821. }
  822.  
  823. /*
  824.  
  825. LOCAL FUNCTION
  826.  
  827.     disable_break -- remove the "mapping changed" breakpoint
  828.  
  829. SYNOPSIS
  830.  
  831.     static int disable_break ()
  832.  
  833. DESCRIPTION
  834.  
  835.     Removes the breakpoint that gets hit when the dynamic linker
  836.     completes a mapping change.
  837.  
  838. */
  839.  
  840. static int
  841. disable_break ()
  842. {
  843.   int status = 1;
  844.  
  845.  
  846.   /* Note that breakpoint address and original contents are in our address
  847.      space, so we just need to write the original contents back. */
  848.  
  849.   if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
  850.     {
  851.       status = 0;
  852.     }
  853.  
  854.   /* For the SVR4 version, we always know the breakpoint address.  For the
  855.      SunOS version we don't know it until the above code is executed.
  856.      Grumble if we are stopped anywhere besides the breakpoint address. */
  857.  
  858.   if (stop_pc != breakpoint_addr)
  859.     {
  860.       warning ("stopped at unknown breakpoint while handling shared libraries");
  861.     }
  862.  
  863.   return (status);
  864. }
  865.  
  866. /*
  867.  
  868. LOCAL FUNCTION
  869.  
  870.     enable_break -- arrange for dynamic linker to hit breakpoint
  871.  
  872. SYNOPSIS
  873.  
  874.     int enable_break (void)
  875.  
  876. DESCRIPTION
  877.  
  878.     Both the SunOS and the SVR4 dynamic linkers have, as part of their
  879.     debugger interface, support for arranging for the inferior to hit
  880.     a breakpoint after mapping in the shared libraries.  This function
  881.     enables that breakpoint.
  882.  
  883.     For SunOS, there is a special flag location (in_debugger) which we
  884.     set to 1.  When the dynamic linker sees this flag set, it will set
  885.     a breakpoint at a location known only to itself, after saving the
  886.     original contents of that place and the breakpoint address itself,
  887.     in it's own internal structures.  When we resume the inferior, it
  888.     will eventually take a SIGTRAP when it runs into the breakpoint.
  889.     We handle this (in a different place) by restoring the contents of
  890.     the breakpointed location (which is only known after it stops),
  891.     chasing around to locate the shared libraries that have been
  892.     loaded, then resuming.
  893.  
  894.     For SVR4, the debugger interface structure contains a member (r_brk)
  895.     which is statically initialized at the time the shared library is
  896.     built, to the offset of a function (_r_debug_state) which is guaran-
  897.     teed to be called once before mapping in a library, and again when
  898.     the mapping is complete.  At the time we are examining this member,
  899.     it contains only the unrelocated offset of the function, so we have
  900.     to do our own relocation.  Later, when the dynamic linker actually
  901.     runs, it relocates r_brk to be the actual address of _r_debug_state().
  902.  
  903.     The debugger interface structure also contains an enumeration which
  904.     is set to either RT_ADD or RT_DELETE prior to changing the mapping,
  905.     depending upon whether or not the library is being mapped or unmapped,
  906.     and then set to RT_CONSISTENT after the library is mapped/unmapped.
  907.  
  908.     Irix 5, on the other hand, has no such features.  Instead, we
  909.     set a breakpoint at main.
  910. */
  911.  
  912. static int
  913. enable_break ()
  914. {
  915.   int success = 0;
  916.   struct minimal_symbol *msymbol;
  917.   char **bkpt_namep;
  918.   CORE_ADDR bkpt_addr;
  919.  
  920.   /* Scan through the list of symbols, trying to look up the symbol and
  921.      set a breakpoint there.  Terminate loop when we/if we succeed. */
  922.  
  923.   breakpoint_addr = 0;
  924.   for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
  925.     {
  926.       msymbol = lookup_minimal_symbol (*bkpt_namep, symfile_objfile);
  927.       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
  928.     {
  929.       bkpt_addr = SYMBOL_VALUE_ADDRESS (msymbol);
  930. #ifdef SOLIB_BKPT_OFFSET
  931.       /* We only want to skip if bkpt_addr is currently pointing
  932.          at a GP setting instruction.  */
  933.       {
  934.         char buf[4];
  935.  
  936.         if (target_read_memory (bkpt_addr, buf, 4) == 0)
  937.           {
  938.         unsigned long insn;
  939.  
  940.         insn = extract_unsigned_integer (buf, 4);
  941.         if ((insn & 0xffff0000) == 0x3c1c0000) /* lui $gp,n */
  942.           bkpt_addr += SOLIB_BKPT_OFFSET;
  943.           }
  944.       }
  945. #endif
  946.       if (target_insert_breakpoint (bkpt_addr, shadow_contents) == 0)
  947.         {
  948.           breakpoint_addr = bkpt_addr;
  949.           success = 1;
  950.           break;
  951.         }
  952.     }
  953.     }
  954.  
  955.   return (success);
  956. }
  957.   
  958. /*
  959.   
  960. GLOBAL FUNCTION
  961.   
  962.     solib_create_inferior_hook -- shared library startup support
  963.   
  964. SYNOPSIS
  965.   
  966.     void solib_create_inferior_hook()
  967.   
  968. DESCRIPTION
  969.   
  970.     When gdb starts up the inferior, it nurses it along (through the
  971.     shell) until it is ready to execute it's first instruction.  At this
  972.     point, this function gets called via expansion of the macro
  973.     SOLIB_CREATE_INFERIOR_HOOK.
  974.  
  975.     For SunOS executables, this first instruction is typically the
  976.     one at "_start", or a similar text label, regardless of whether
  977.     the executable is statically or dynamically linked.  The runtime
  978.     startup code takes care of dynamically linking in any shared
  979.     libraries, once gdb allows the inferior to continue.
  980.  
  981.     For SVR4 executables, this first instruction is either the first
  982.     instruction in the dynamic linker (for dynamically linked
  983.     executables) or the instruction at "start" for statically linked
  984.     executables.  For dynamically linked executables, the system
  985.     first exec's /lib/libc.so.N, which contains the dynamic linker,
  986.     and starts it running.  The dynamic linker maps in any needed
  987.     shared libraries, maps in the actual user executable, and then
  988.     jumps to "start" in the user executable.
  989.  
  990.     For both SunOS shared libraries, and SVR4 shared libraries, we
  991.     can arrange to cooperate with the dynamic linker to discover the
  992.     names of shared libraries that are dynamically linked, and the
  993.     base addresses to which they are linked.
  994.  
  995.     This function is responsible for discovering those names and
  996.     addresses, and saving sufficient information about them to allow
  997.     their symbols to be read at a later time.
  998.  
  999. FIXME
  1000.  
  1001.     Between enable_break() and disable_break(), this code does not
  1002.     properly handle hitting breakpoints which the user might have
  1003.     set in the startup code or in the dynamic linker itself.  Proper
  1004.     handling will probably have to wait until the implementation is
  1005.     changed to use the "breakpoint handler function" method.
  1006.  
  1007.     Also, what if child has exit()ed?  Must exit loop somehow.
  1008.   */
  1009.  
  1010. void 
  1011. solib_create_inferior_hook()
  1012. {
  1013.   if (!enable_break ())
  1014.     {
  1015.       warning ("shared library handler failed to enable breakpoint");
  1016.       return;
  1017.     }
  1018.  
  1019.   /* Now run the target.  It will eventually hit the breakpoint, at
  1020.      which point all of the libraries will have been mapped in and we
  1021.      can go groveling around in the dynamic linker structures to find
  1022.      out what we need to know about them. */
  1023.  
  1024.   clear_proceed_status ();
  1025.   stop_soon_quietly = 1;
  1026.   stop_signal = 0;
  1027.   do
  1028.     {
  1029.       target_resume (-1, 0, stop_signal);
  1030.       wait_for_inferior ();
  1031.     }
  1032.   while (stop_signal != SIGTRAP);
  1033.   stop_soon_quietly = 0;
  1034.   
  1035.   /* We are now either at the "mapping complete" breakpoint (or somewhere
  1036.      else, a condition we aren't prepared to deal with anyway), so adjust
  1037.      the PC as necessary after a breakpoint, disable the breakpoint, and
  1038.      add any shared libraries that were mapped in. */
  1039.  
  1040.   if (DECR_PC_AFTER_BREAK)
  1041.     {
  1042.       stop_pc -= DECR_PC_AFTER_BREAK;
  1043.       write_register (PC_REGNUM, stop_pc);
  1044.     }
  1045.  
  1046.   if (!disable_break ())
  1047.     {
  1048.       warning ("shared library handler failed to disable breakpoint");
  1049.     }
  1050.  
  1051.   solib_add ((char *) 0, 0, (struct target_ops *) 0);
  1052. }
  1053.  
  1054. /*
  1055.  
  1056. LOCAL FUNCTION
  1057.  
  1058.     sharedlibrary_command -- handle command to explicitly add library
  1059.  
  1060. SYNOPSIS
  1061.  
  1062.     static void sharedlibrary_command (char *args, int from_tty)
  1063.  
  1064. DESCRIPTION
  1065.  
  1066. */
  1067.  
  1068. static void
  1069. sharedlibrary_command (args, from_tty)
  1070. char *args;
  1071. int from_tty;
  1072. {
  1073.   dont_repeat ();
  1074.   solib_add (args, from_tty, (struct target_ops *) 0);
  1075. }
  1076.  
  1077. void
  1078. _initialize_solib()
  1079. {
  1080.   
  1081.   add_com ("sharedlibrary", class_files, sharedlibrary_command,
  1082.        "Load shared object library symbols for files matching REGEXP.");
  1083.   add_info ("sharedlibrary", info_sharedlibrary_command, 
  1084.         "Status of loaded shared object libraries.");
  1085. }
  1086.