home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / gdb / irix5-nat.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  29KB  |  1,073 lines

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