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.
6 Implemented for Irix 4.x by Garrett A. Wollman.
7 Modified for Irix 5.x by Ian Lance Taylor.
9 This file is part of GDB.
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.
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.
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. */
31 #include <sys/procfs.h>
32 #include <setjmp.h> /* For JB_XXX. */
34 /* Size of elements in jmpbuf */
36 #define JB_ELEMENT_SIZE 4
39 * See the comment in m68k-tdep.c regarding the utility of these functions.
41 * These definitions are from the MIPS SVR4 ABI, so they may work for
42 * any MIPS SVR4 target.
46 supply_gregset (gregsetp)
50 register greg_t *regp = &(*gregsetp)[0];
52 for(regi = 0; regi <= CTX_RA; regi++)
53 supply_register (regi, (char *)(regp + regi));
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));
62 fill_gregset (gregsetp, regno)
67 register greg_t *regp = &(*gregsetp)[0];
69 for (regi = 0; regi <= CTX_RA; regi++)
70 if ((regno == -1) || (regno == regi))
71 *(regp + regi) = *(greg_t *) ®isters[REGISTER_BYTE (regi)];
73 if ((regno == -1) || (regno == PC_REGNUM))
74 *(regp + CTX_EPC) = *(greg_t *) ®isters[REGISTER_BYTE (PC_REGNUM)];
76 if ((regno == -1) || (regno == CAUSE_REGNUM))
77 *(regp + CTX_CAUSE) = *(greg_t *) ®isters[REGISTER_BYTE (PS_REGNUM)];
79 if ((regno == -1) || (regno == HI_REGNUM))
80 *(regp + CTX_MDHI) = *(greg_t *) ®isters[REGISTER_BYTE (HI_REGNUM)];
82 if ((regno == -1) || (regno == LO_REGNUM))
83 *(regp + CTX_MDLO) = *(greg_t *) ®isters[REGISTER_BYTE (LO_REGNUM)];
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.
91 * Again, see the comments in m68k-tdep.c.
95 supply_fpregset (fpregsetp)
96 fpregset_t *fpregsetp;
100 for (regi = 0; regi < 32; regi++)
101 supply_register (FP0_REGNUM + regi,
102 (char *)&fpregsetp->fp_r.fp_regs[regi]);
104 supply_register (FCRCS_REGNUM, (char *)&fpregsetp->fp_csr);
106 /* FIXME: how can we supply FCRIR_REGNUM? SGI doesn't tell us. */
110 fill_fpregset (fpregsetp, regno)
111 fpregset_t *fpregsetp;
117 for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
119 if ((regno == -1) || (regno == regi))
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));
127 if ((regno == -1) || (regno == FCRCS_REGNUM))
128 fpregsetp->fp_csr = *(unsigned *) ®isters[REGISTER_BYTE(FCRCS_REGNUM)];
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. */
138 get_longjmp_target (pc)
141 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
144 jb_addr = read_register (A0_REGNUM);
146 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
147 TARGET_PTR_BIT / TARGET_CHAR_BIT))
150 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
156 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
158 unsigned core_reg_size;
159 int which; /* Unused */
160 unsigned int reg_addr; /* Unused */
162 if (core_reg_size != REGISTER_BYTES)
164 warning ("wrong size gregset struct in core file");
168 memcpy ((char *)registers, core_reg_sect, core_reg_size);
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. */
174 #include <sys/types.h>
177 #include <sys/param.h>
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. */
184 #define __SYMCONST_H__
190 #include "objfiles.h"
194 #include "inferior.h"
195 #include "language.h"
197 /* The symbol which starts off the list of shared libraries. */
198 #define DEBUG_BASE "__rld_obj_head"
200 /* How to get the loaded address of a shared library. */
201 #define LM_ADDR(so) ((so)->lm.o_praw)
203 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
206 struct so_list *next; /* next structure in linked list */
208 struct obj lm; /* copy of link map from inferior */
209 struct obj_list *lladdr; /* addr in inferior lm was read from */
210 CORE_ADDR lmend; /* upper addr bound of mapped object */
211 char symbols_loaded; /* flag: symbols read in yet? */
212 char from_tty; /* flag: print msgs? */
213 struct objfile *objfile; /* objfile for loaded lib */
214 struct section_table *sections;
215 struct section_table *sections_end;
216 struct section_table *textsection;
220 static struct so_list *so_list_head; /* List of known shared objects */
221 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
222 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
224 /* Local function prototypes */
227 sharedlibrary_command PARAMS ((char *, int));
230 enable_break PARAMS ((void));
233 disable_break PARAMS ((void));
236 info_sharedlibrary_command PARAMS ((char *, int));
239 symbol_add_stub PARAMS ((char *));
241 static struct so_list *
242 find_solib PARAMS ((struct so_list *));
244 static struct obj_list *
245 first_link_map_member PARAMS ((void));
248 locate_base PARAMS ((void));
251 solib_map_sections PARAMS ((struct so_list *));
257 solib_map_sections -- open bfd and build sections for shared lib
261 static void solib_map_sections (struct so_list *so)
265 Given a pointer to one of the shared objects in our list
266 of mapped objects, use the recorded name to open a bfd
267 descriptor for the object, build a section table, and then
268 relocate all the section addresses by the base address at
269 which the shared object was mapped.
273 In most (all?) cases the shared object file name recorded in the
274 dynamic linkage tables will be a fully qualified pathname. For
275 cases where it isn't, do we really mimic the systems search
276 mechanism correctly in the below code (particularly the tilde
281 solib_map_sections (so)
285 char *scratch_pathname;
287 struct section_table *p;
288 struct cleanup *old_chain;
292 filename = tilde_expand (so -> lm.o_path);
293 old_chain = make_cleanup (free, filename);
295 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
297 if (scratch_chan < 0)
299 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
300 O_RDONLY, 0, &scratch_pathname);
302 if (scratch_chan < 0)
304 perror_with_name (filename);
306 /* Leave scratch_pathname allocated. abfd->name will point to it. */
308 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
311 close (scratch_chan);
312 error ("Could not open `%s' as an executable file: %s",
313 scratch_pathname, bfd_errmsg (bfd_get_error ()));
315 /* Leave bfd open, core_xfer_memory and "info files" need it. */
317 abfd -> cacheable = true;
319 if (!bfd_check_format (abfd, bfd_object))
321 error ("\"%s\": not in executable format: %s.",
322 scratch_pathname, bfd_errmsg (bfd_get_error ()));
324 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
326 error ("Can't find the file sections in `%s': %s",
327 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
330 /* Irix 5 shared objects are pre-linked to particular addresses
331 although the dynamic linker may have to relocate them if the
332 address ranges of the libraries used by the main program clash.
333 The offset is the difference between the address where the object
334 is mapped and the binding address of the shared library. */
335 offset = (CORE_ADDR) LM_ADDR (so) - so -> lm.o_base_address;
337 for (p = so -> sections; p < so -> sections_end; p++)
339 /* Relocate the section binding addresses as recorded in the shared
340 object's file by the offset to get the address to which the
341 object was actually mapped. */
343 p -> endaddr += offset;
344 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
345 if (STREQ (p -> the_bfd_section -> name, ".text"))
347 so -> textsection = p;
351 /* Free the file names, close the file now. */
352 do_cleanups (old_chain);
359 locate_base -- locate the base address of dynamic linker structs
363 CORE_ADDR locate_base (void)
367 For both the SunOS and SVR4 shared library implementations, if the
368 inferior executable has been linked dynamically, there is a single
369 address somewhere in the inferior's data space which is the key to
370 locating all of the dynamic linker's runtime structures. This
371 address is the value of the symbol defined by the macro DEBUG_BASE.
372 The job of this function is to find and return that address, or to
373 return 0 if there is no such address (the executable is statically
376 For SunOS, the job is almost trivial, since the dynamic linker and
377 all of it's structures are statically linked to the executable at
378 link time. Thus the symbol for the address we are looking for has
379 already been added to the minimal symbol table for the executable's
380 objfile at the time the symbol file's symbols were read, and all we
381 have to do is look it up there. Note that we explicitly do NOT want
382 to find the copies in the shared library.
384 The SVR4 version is much more complicated because the dynamic linker
385 and it's structures are located in the shared C library, which gets
386 run as the executable's "interpreter" by the kernel. We have to go
387 to a lot more work to discover the address of DEBUG_BASE. Because
388 of this complexity, we cache the value we find and return that value
389 on subsequent invocations. Note there is no copy in the executable
392 Irix 5 is basically like SunOS.
394 Note that we can assume nothing about the process state at the time
395 we need to find this address. We may be stopped on the first instruc-
396 tion of the interpreter (C shared library), the first instruction of
397 the executable itself, or somewhere else entirely (if we attached
398 to the process for example).
405 struct minimal_symbol *msymbol;
406 CORE_ADDR address = 0;
408 msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
409 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
411 address = SYMBOL_VALUE_ADDRESS (msymbol);
420 first_link_map_member -- locate first member in dynamic linker's map
424 static struct link_map *first_link_map_member (void)
428 Read in a copy of the first member in the inferior's dynamic
429 link map from the inferior's dynamic linker structures, and return
430 a pointer to the copy in our address space.
433 static struct obj_list *
434 first_link_map_member ()
439 read_memory (debug_base, (char *) &lm, sizeof (struct obj_list *));
444 /* The first entry in the list is the object file we are debugging,
446 read_memory ((CORE_ADDR) lm, (char *) &s, sizeof (struct obj_list));
455 find_solib -- step through list of shared objects
459 struct so_list *find_solib (struct so_list *so_list_ptr)
463 This module contains the routine which finds the names of any
464 loaded "images" in the current process. The argument in must be
465 NULL on the first call, and then the returned value must be passed
466 in on subsequent calls. This provides the capability to "step" down
467 the list of loaded objects. On the last object, a NULL value is
471 static struct so_list *
472 find_solib (so_list_ptr)
473 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
475 struct so_list *so_list_next = NULL;
476 struct obj_list *lm = NULL;
479 if (so_list_ptr == NULL)
481 /* We are setting up for a new scan through the loaded images. */
482 if ((so_list_next = so_list_head) == NULL)
484 /* We have not already read in the dynamic linking structures
485 from the inferior, lookup the address of the base structure. */
486 debug_base = locate_base ();
489 /* Read the base structure in and find the address of the first
490 link map list member. */
491 lm = first_link_map_member ();
497 /* We have been called before, and are in the process of walking
498 the shared library list. Advance to the next shared object. */
499 if ((lm = so_list_ptr->ll.next) == NULL)
501 /* We have hit the end of the list, so check to see if any were
502 added, but be quiet if we can't read from the target any more. */
503 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lladdr,
504 (char *) &(so_list_ptr -> ll),
505 sizeof (struct obj_list));
508 lm = so_list_ptr->ll.next;
515 so_list_next = so_list_ptr -> next;
517 if ((so_list_next == NULL) && (lm != NULL))
522 /* Get next link map structure from inferior image and build a local
523 abbreviated load_map structure */
524 new = (struct so_list *) xmalloc (sizeof (struct so_list));
525 memset ((char *) new, 0, sizeof (struct so_list));
527 /* Add the new node as the next node in the list, or as the root
528 node if this is the first one. */
529 if (so_list_ptr != NULL)
531 so_list_ptr -> next = new;
538 read_memory ((CORE_ADDR) lm, (char *) &(new -> ll),
539 sizeof (struct obj_list));
540 read_memory ((CORE_ADDR) new->ll.data, (char *) &(new -> lm),
541 sizeof (struct obj));
542 target_read_string ((CORE_ADDR)new->lm.o_path, &buffer,
545 memory_error (errcode, (CORE_ADDR)new->lm.o_path);
546 new->lm.o_path = buffer;
547 solib_map_sections (new);
549 return (so_list_next);
552 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
555 symbol_add_stub (arg)
558 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
560 so -> objfile = symbol_file_add (so -> lm.o_path, so -> from_tty,
561 (unsigned int) so -> textsection -> addr,
570 solib_add -- add a shared library file to the symtab and section list
574 void solib_add (char *arg_string, int from_tty,
575 struct target_ops *target)
582 solib_add (arg_string, from_tty, target)
585 struct target_ops *target;
587 register struct so_list *so = NULL; /* link map state variable */
589 /* Last shared library that we read. */
590 struct so_list *so_last = NULL;
596 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
598 error ("Invalid regexp: %s", re_err);
601 /* Add the shared library sections to the section table of the
602 specified target, if any. */
605 /* Count how many new section_table entries there are. */
608 while ((so = find_solib (so)) != NULL)
610 if (so -> lm.o_path[0])
612 count += so -> sections_end - so -> sections;
618 /* Reallocate the target's section table including the new size. */
619 if (target -> to_sections)
621 old = target -> to_sections_end - target -> to_sections;
622 target -> to_sections = (struct section_table *)
623 xrealloc ((char *)target -> to_sections,
624 (sizeof (struct section_table)) * (count + old));
629 target -> to_sections = (struct section_table *)
630 xmalloc ((sizeof (struct section_table)) * count);
632 target -> to_sections_end = target -> to_sections + (count + old);
634 /* Add these section table entries to the target's table. */
635 while ((so = find_solib (so)) != NULL)
637 if (so -> lm.o_path[0])
639 count = so -> sections_end - so -> sections;
640 memcpy ((char *) (target -> to_sections + old),
642 (sizeof (struct section_table)) * count);
649 /* Now add the symbol files. */
650 while ((so = find_solib (so)) != NULL)
652 if (so -> lm.o_path[0] && re_exec (so -> lm.o_path))
654 so -> from_tty = from_tty;
655 if (so -> symbols_loaded)
659 printf_unfiltered ("Symbols already loaded for %s\n", so -> lm.o_path);
662 else if (catch_errors
663 (symbol_add_stub, (char *) so,
664 "Error while reading shared library symbols:\n",
668 so -> symbols_loaded = 1;
673 /* Getting new symbols may change our opinion about what is
676 reinit_frame_cache ();
683 info_sharedlibrary_command -- code for "info sharedlibrary"
687 static void info_sharedlibrary_command ()
691 Walk through the shared library list and print information
692 about each attached library.
696 info_sharedlibrary_command (ignore, from_tty)
700 register struct so_list *so = NULL; /* link map state variable */
703 if (exec_bfd == NULL)
705 printf_unfiltered ("No exec file.\n");
708 while ((so = find_solib (so)) != NULL)
710 if (so -> lm.o_path[0])
714 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
715 "Shared Object Library");
718 printf_unfiltered ("%-12s",
719 local_hex_string_custom ((unsigned long) LM_ADDR (so),
721 printf_unfiltered ("%-12s",
722 local_hex_string_custom ((unsigned long) so -> lmend,
724 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
725 printf_unfiltered ("%s\n", so -> lm.o_path);
728 if (so_list_head == NULL)
730 printf_unfiltered ("No shared libraries loaded at this time.\n");
738 solib_address -- check to see if an address is in a shared lib
742 int solib_address (CORE_ADDR address)
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
753 For example, this routine is called at one point to disable
754 breakpoints which are in shared libraries that are not currently
759 solib_address (address)
762 register struct so_list *so = 0; /* link map state variable */
764 while ((so = find_solib (so)) != NULL)
766 if (so -> lm.o_path[0])
768 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
769 (address < (CORE_ADDR) so -> lmend))
778 /* Called by free_all_symtabs */
783 struct so_list *next;
788 if (so_list_head -> sections)
790 free ((PTR)so_list_head -> sections);
792 if (so_list_head -> abfd)
794 bfd_filename = bfd_get_filename (so_list_head -> abfd);
795 bfd_close (so_list_head -> abfd);
798 /* This happens for the executable on SVR4. */
801 next = so_list_head -> next;
803 free ((PTR)bfd_filename);
804 free (so_list_head->lm.o_path);
805 free ((PTR)so_list_head);
815 disable_break -- remove the "mapping changed" breakpoint
819 static int disable_break ()
823 Removes the breakpoint that gets hit when the dynamic linker
824 completes a mapping change.
834 /* Note that breakpoint address and original contents are in our address
835 space, so we just need to write the original contents back. */
837 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
842 /* For the SVR4 version, we always know the breakpoint address. For the
843 SunOS version we don't know it until the above code is executed.
844 Grumble if we are stopped anywhere besides the breakpoint address. */
846 if (stop_pc != breakpoint_addr)
848 warning ("stopped at unknown breakpoint while handling shared libraries");
858 enable_break -- arrange for dynamic linker to hit breakpoint
862 int enable_break (void)
866 This functions inserts a breakpoint at the entry point of the
867 main executable, where all shared libraries are mapped in.
873 if (symfile_objfile != NULL
874 && target_insert_breakpoint (symfile_objfile->ei.entry_point,
875 shadow_contents) == 0)
877 breakpoint_addr = symfile_objfile->ei.entry_point;
888 solib_create_inferior_hook -- shared library startup support
892 void solib_create_inferior_hook()
896 When gdb starts up the inferior, it nurses it along (through the
897 shell) until it is ready to execute it's first instruction. At this
898 point, this function gets called via expansion of the macro
899 SOLIB_CREATE_INFERIOR_HOOK.
901 For SunOS executables, this first instruction is typically the
902 one at "_start", or a similar text label, regardless of whether
903 the executable is statically or dynamically linked. The runtime
904 startup code takes care of dynamically linking in any shared
905 libraries, once gdb allows the inferior to continue.
907 For SVR4 executables, this first instruction is either the first
908 instruction in the dynamic linker (for dynamically linked
909 executables) or the instruction at "start" for statically linked
910 executables. For dynamically linked executables, the system
911 first exec's /lib/libc.so.N, which contains the dynamic linker,
912 and starts it running. The dynamic linker maps in any needed
913 shared libraries, maps in the actual user executable, and then
914 jumps to "start" in the user executable.
916 For both SunOS shared libraries, and SVR4 shared libraries, we
917 can arrange to cooperate with the dynamic linker to discover the
918 names of shared libraries that are dynamically linked, and the
919 base addresses to which they are linked.
921 This function is responsible for discovering those names and
922 addresses, and saving sufficient information about them to allow
923 their symbols to be read at a later time.
927 Between enable_break() and disable_break(), this code does not
928 properly handle hitting breakpoints which the user might have
929 set in the startup code or in the dynamic linker itself. Proper
930 handling will probably have to wait until the implementation is
931 changed to use the "breakpoint handler function" method.
933 Also, what if child has exit()ed? Must exit loop somehow.
937 solib_create_inferior_hook()
939 if (!enable_break ())
941 warning ("shared library handler failed to enable breakpoint");
945 /* Now run the target. It will eventually hit the breakpoint, at
946 which point all of the libraries will have been mapped in and we
947 can go groveling around in the dynamic linker structures to find
948 out what we need to know about them. */
950 clear_proceed_status ();
951 stop_soon_quietly = 1;
955 target_resume (-1, 0, stop_signal);
956 wait_for_inferior ();
958 while (stop_signal != SIGTRAP);
960 /* We are now either at the "mapping complete" breakpoint (or somewhere
961 else, a condition we aren't prepared to deal with anyway), so adjust
962 the PC as necessary after a breakpoint, disable the breakpoint, and
963 add any shared libraries that were mapped in. */
965 if (DECR_PC_AFTER_BREAK)
967 stop_pc -= DECR_PC_AFTER_BREAK;
968 write_register (PC_REGNUM, stop_pc);
971 if (!disable_break ())
973 warning ("shared library handler failed to disable breakpoint");
976 /* solib_add will call reinit_frame_cache.
977 But we are stopped in the startup code and we might not have symbols
978 for the startup code, so heuristic_proc_start could be called
979 and will put out an annoying warning.
980 Delaying the resetting of stop_soon_quietly until after symbol loading
981 suppresses the warning. */
982 solib_add ((char *) 0, 0, (struct target_ops *) 0);
983 stop_soon_quietly = 0;
990 sharedlibrary_command -- handle command to explicitly add library
994 static void sharedlibrary_command (char *args, int from_tty)
1001 sharedlibrary_command (args, from_tty)
1006 solib_add (args, from_tty, (struct target_ops *) 0);
1013 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1014 "Load shared object library symbols for files matching REGEXP.");
1015 add_info ("sharedlibrary", info_sharedlibrary_command,
1016 "Status of loaded shared object libraries.");