1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
27 #include <sys/param.h>
30 #ifndef SVR4_SHARED_LIBS
31 /* SunOS shared libs need the nlist structure. */
46 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
48 /* On SVR4 systems, for the initial implementation, use main() as the
49 "startup mapping complete" breakpoint address. The models for SunOS
50 and SVR4 dynamic linking debugger support are different in that SunOS
51 hits one breakpoint when all mapping is complete while using the SVR4
52 debugger support takes two breakpoint hits for each file mapped, and
53 there is no way to know when the "last" one is hit. Both these
54 mechanisms should be tied to a "breakpoint service routine" that
55 gets automatically executed whenever one of the breakpoints indicating
56 a change in mapping is hit. This is a future enhancement. (FIXME) */
58 #define BKPT_AT_MAIN 1
60 /* local data declarations */
62 #ifndef SVR4_SHARED_LIBS
64 #define DEBUG_BASE "_DYNAMIC"
65 #define LM_ADDR(so) ((so) -> lm.lm_addr)
66 #define LM_NEXT(so) ((so) -> lm.lm_next)
67 #define LM_NAME(so) ((so) -> lm.lm_name)
68 static struct link_dynamic dynamic_copy;
69 static struct link_dynamic_2 ld_2_copy;
70 static struct ld_debug debug_copy;
71 static CORE_ADDR debug_addr;
72 static CORE_ADDR flag_addr;
74 #else /* SVR4_SHARED_LIBS */
76 #define DEBUG_BASE "_r_debug"
77 #define LM_ADDR(so) ((so) -> lm.l_addr)
78 #define LM_NEXT(so) ((so) -> lm.l_next)
79 #define LM_NAME(so) ((so) -> lm.l_name)
80 static struct r_debug debug_copy;
81 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
83 #endif /* !SVR4_SHARED_LIBS */
86 struct so_list *next; /* next structure in linked list */
87 struct link_map lm; /* copy of link map from inferior */
88 struct link_map *lmaddr; /* addr in inferior lm was read from */
89 CORE_ADDR lmend; /* upper addr bound of mapped object */
90 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
91 char symbols_loaded; /* flag: symbols read in yet? */
92 char from_tty; /* flag: print msgs? */
93 struct objfile *objfile; /* objfile for loaded lib */
94 struct section_table *sections;
95 struct section_table *sections_end;
96 struct section_table *textsection;
99 static struct so_list *so_list_head; /* List of known shared objects */
100 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
101 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
104 fdmatch PARAMS ((int, int)); /* In libiberty */
106 /* Local function prototypes */
109 special_symbol_handling PARAMS ((struct so_list *));
112 sharedlibrary_command PARAMS ((char *, int));
115 enable_break PARAMS ((void));
118 disable_break PARAMS ((void));
121 info_sharedlibrary_command PARAMS ((char *, int));
124 symbol_add_stub PARAMS ((char *));
126 static struct so_list *
127 find_solib PARAMS ((struct so_list *));
129 static struct link_map *
130 first_link_map_member PARAMS ((void));
133 locate_base PARAMS ((void));
136 solib_map_sections PARAMS ((struct so_list *));
138 #ifdef SVR4_SHARED_LIBS
141 look_for_base PARAMS ((int, CORE_ADDR));
144 bfd_lookup_symbol PARAMS ((bfd *, char *));
149 solib_add_common_symbols PARAMS ((struct rtc_symb *, struct objfile *));
157 solib_map_sections -- open bfd and build sections for shared lib
161 static void solib_map_sections (struct so_list *so)
165 Given a pointer to one of the shared objects in our list
166 of mapped objects, use the recorded name to open a bfd
167 descriptor for the object, build a section table, and then
168 relocate all the section addresses by the base address at
169 which the shared object was mapped.
173 In most (all?) cases the shared object file name recorded in the
174 dynamic linkage tables will be a fully qualified pathname. For
175 cases where it isn't, do we really mimic the systems search
176 mechanism correctly in the below code (particularly the tilde
181 solib_map_sections (so)
185 char *scratch_pathname;
187 struct section_table *p;
188 struct cleanup *old_chain;
191 filename = tilde_expand (so -> so_name);
192 old_chain = make_cleanup (free, filename);
194 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
196 if (scratch_chan < 0)
198 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
199 O_RDONLY, 0, &scratch_pathname);
201 if (scratch_chan < 0)
203 perror_with_name (filename);
205 make_cleanup (free, scratch_pathname);
207 abfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
210 close (scratch_chan);
211 error ("Could not open `%s' as an executable file: %s",
212 scratch_pathname, bfd_errmsg (bfd_error));
215 make_cleanup (bfd_close, abfd); /* Zap bfd, close scratch_chan. */
217 if (!bfd_check_format (abfd, bfd_object))
219 error ("\"%s\": not in executable format: %s.",
220 scratch_pathname, bfd_errmsg (bfd_error));
222 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
224 error ("Can't find the file sections in `%s': %s",
225 exec_bfd -> filename, bfd_errmsg (bfd_error));
228 for (p = so -> sections; p < so -> sections_end; p++)
230 /* Relocate the section binding addresses as recorded in the shared
231 object's file by the base address to which the object was actually
233 p -> addr += (CORE_ADDR) LM_ADDR (so);
234 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
235 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
236 if (STREQ (p -> sec_ptr -> name, ".text"))
238 so -> textsection = p;
242 /* Free the file names, close the file now. */
243 do_cleanups (old_chain);
246 /* Read all dynamically loaded common symbol definitions from the inferior
247 and add them to the minimal symbol table for the shared library objfile. */
249 #ifndef SVR4_SHARED_LIBS
252 solib_add_common_symbols (rtc_symp, objfile)
253 struct rtc_symb *rtc_symp;
254 struct objfile *objfile;
256 struct rtc_symb inferior_rtc_symb;
257 struct nlist inferior_rtc_nlist;
262 init_minimal_symbol_collection ();
263 make_cleanup (discard_minimal_symbols, 0);
267 read_memory ((CORE_ADDR) rtc_symp,
268 (char *) &inferior_rtc_symb,
269 sizeof (inferior_rtc_symb));
270 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
271 (char *) &inferior_rtc_nlist,
272 sizeof(inferior_rtc_nlist));
273 if (inferior_rtc_nlist.n_type == N_COMM)
275 /* FIXME: The length of the symbol name is not available, but in the
276 current implementation the common symbol is allocated immediately
277 behind the name of the symbol. */
278 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
280 origname = name = xmalloc (len);
281 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
283 /* Don't enter the symbol twice if the target is re-run. */
285 if (name[0] == bfd_get_symbol_leading_char (objfile->obfd))
290 /* FIXME: Do we really want to exclude symbols which happen
291 to match symbols for other locations in the inferior's
292 address space, even when they are in different linkage units? */
293 if (lookup_minimal_symbol (name, (struct objfile *) NULL) == NULL)
295 name = obsavestring (name, strlen (name),
296 &objfile -> symbol_obstack);
297 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
302 rtc_symp = inferior_rtc_symb.rtc_next;
305 /* Install any minimal symbols that have been collected as the current
306 minimal symbols for this objfile. */
308 install_minimal_symbols (objfile);
311 #endif /* SVR4_SHARED_LIBS */
313 #ifdef SVR4_SHARED_LIBS
319 bfd_lookup_symbol -- lookup the value for a specific symbol
323 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
327 An expensive way to lookup the value of a single symbol for
328 bfd's that are only temporary anyway. This is used by the
329 shared library support to find the address of the debugger
330 interface structures in the shared library.
332 Note that 0 is specifically allowed as an error return (no
335 FIXME: See if there is a less "expensive" way of doing this.
336 Also see if there is already another bfd or gdb function
337 that specifically does this, and if so, use it.
341 bfd_lookup_symbol (abfd, symname)
345 unsigned int storage_needed;
347 asymbol **symbol_table;
348 unsigned int number_of_symbols;
350 struct cleanup *back_to;
351 CORE_ADDR symaddr = 0;
353 storage_needed = get_symtab_upper_bound (abfd);
355 if (storage_needed > 0)
357 symbol_table = (asymbol **) xmalloc (storage_needed);
358 back_to = make_cleanup (free, (PTR)symbol_table);
359 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
361 for (i = 0; i < number_of_symbols; i++)
363 sym = *symbol_table++;
364 if (STREQ (sym -> name, symname))
366 symaddr = sym -> value;
370 do_cleanups (back_to);
379 look_for_base -- examine file for each mapped address segment
383 static int look_for_base (int fd, CORE_ADDR baseaddr)
387 This function is passed to proc_iterate_over_mappings, which
388 causes it to get called once for each mapped address space, with
389 an open file descriptor for the file mapped to that space, and the
390 base address of that mapped space.
392 Our job is to find the symbol DEBUG_BASE in the file that this
393 fd is open on, if it exists, and if so, initialize the dynamic
394 linker structure base address debug_base.
396 Note that this is a computationally expensive proposition, since
397 we basically have to open a bfd on every call, so we specifically
398 avoid opening the exec file.
402 look_for_base (fd, baseaddr)
409 /* If the fd is -1, then there is no file that corresponds to this
410 mapped memory segment, so skip it. Also, if the fd corresponds
411 to the exec file, skip it as well. */
413 if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd))
418 /* Try to open whatever random file this fd corresponds to. Note that
419 we have no way currently to find the filename. Don't gripe about
420 any problems we might have, just fail. */
422 if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL)
426 if (!bfd_check_format (interp_bfd, bfd_object))
428 bfd_close (interp_bfd);
432 /* Now try to find our DEBUG_BASE symbol in this file, which we at
433 least know to be a valid ELF executable or shared library. */
435 if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0)
437 bfd_close (interp_bfd);
441 /* Eureka! We found the symbol. But now we may need to relocate it
442 by the base address. If the symbol's value is less than the base
443 address of the shared library, then it hasn't yet been relocated
444 by the dynamic linker, and we have to do it ourself. FIXME: Note
445 that we make the assumption that the first segment that corresponds
446 to the shared library has the base address to which the library
449 if (address < baseaddr)
453 debug_base = address;
454 bfd_close (interp_bfd);
464 locate_base -- locate the base address of dynamic linker structs
468 CORE_ADDR locate_base (void)
472 For both the SunOS and SVR4 shared library implementations, if the
473 inferior executable has been linked dynamically, there is a single
474 address somewhere in the inferior's data space which is the key to
475 locating all of the dynamic linker's runtime structures. This
476 address is the value of the symbol defined by the macro DEBUG_BASE.
477 The job of this function is to find and return that address, or to
478 return 0 if there is no such address (the executable is statically
481 For SunOS, the job is almost trivial, since the dynamic linker and
482 all of it's structures are statically linked to the executable at
483 link time. Thus the symbol for the address we are looking for has
484 already been added to the minimal symbol table for the executable's
485 objfile at the time the symbol file's symbols were read, and all we
486 have to do is look it up there. Note that we explicitly do NOT want
487 to find the copies in the shared library.
489 The SVR4 version is much more complicated because the dynamic linker
490 and it's structures are located in the shared C library, which gets
491 run as the executable's "interpreter" by the kernel. We have to go
492 to a lot more work to discover the address of DEBUG_BASE. Because
493 of this complexity, we cache the value we find and return that value
494 on subsequent invocations. Note there is no copy in the executable
497 Note that we can assume nothing about the process state at the time
498 we need to find this address. We may be stopped on the first instruc-
499 tion of the interpreter (C shared library), the first instruction of
500 the executable itself, or somewhere else entirely (if we attached
501 to the process for example).
509 #ifndef SVR4_SHARED_LIBS
511 struct minimal_symbol *msymbol;
512 CORE_ADDR address = 0;
514 /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
515 being debugged, since there is a duplicate named symbol in the shared
516 library. We don't want the shared library versions. */
518 msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
519 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
521 address = SYMBOL_VALUE_ADDRESS (msymbol);
525 #else /* SVR4_SHARED_LIBS */
527 /* Check to see if we have a currently valid address, and if so, avoid
528 doing all this work again and just return the cached address. If
529 we have no cached address, ask the /proc support interface to iterate
530 over the list of mapped address segments, calling look_for_base() for
531 each segment. When we are done, we will have either found the base
536 proc_iterate_over_mappings (look_for_base);
540 #endif /* !SVR4_SHARED_LIBS */
544 static struct link_map *
545 first_link_map_member ()
547 struct link_map *lm = NULL;
549 #ifndef SVR4_SHARED_LIBS
551 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
552 if (dynamic_copy.ld_version >= 2)
554 /* It is a version that we can deal with, so read in the secondary
555 structure and find the address of the link map list from it. */
556 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
557 sizeof (struct link_dynamic_2));
558 lm = ld_2_copy.ld_loaded;
561 #else /* SVR4_SHARED_LIBS */
563 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
564 lm = debug_copy.r_map;
566 #endif /* !SVR4_SHARED_LIBS */
575 find_solib -- step through list of shared objects
579 struct so_list *find_solib (struct so_list *so_list_ptr)
583 This module contains the routine which finds the names of any
584 loaded "images" in the current process. The argument in must be
585 NULL on the first call, and then the returned value must be passed
586 in on subsequent calls. This provides the capability to "step" down
587 the list of loaded objects. On the last object, a NULL value is
590 The arg and return value are "struct link_map" pointers, as defined
594 static struct so_list *
595 find_solib (so_list_ptr)
596 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
598 struct so_list *so_list_next = NULL;
599 struct link_map *lm = NULL;
602 if (so_list_ptr == NULL)
604 /* We are setting up for a new scan through the loaded images. */
605 if ((so_list_next = so_list_head) == NULL)
607 /* We have not already read in the dynamic linking structures
608 from the inferior, lookup the address of the base structure. */
609 debug_base = locate_base ();
612 /* Read the base structure in and find the address of the first
613 link map list member. */
614 lm = first_link_map_member ();
620 /* We have been called before, and are in the process of walking
621 the shared library list. Advance to the next shared object. */
622 if ((lm = LM_NEXT (so_list_ptr)) == NULL)
624 /* We have hit the end of the list, so check to see if any were
625 added, but be quiet if we can't read from the target any more. */
626 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
627 (char *) &(so_list_ptr -> lm),
628 sizeof (struct link_map));
631 lm = LM_NEXT (so_list_ptr);
638 so_list_next = so_list_ptr -> next;
640 if ((so_list_next == NULL) && (lm != NULL))
642 /* Get next link map structure from inferior image and build a local
643 abbreviated load_map structure */
644 new = (struct so_list *) xmalloc (sizeof (struct so_list));
645 memset ((char *) new, 0, sizeof (struct so_list));
647 /* Add the new node as the next node in the list, or as the root
648 node if this is the first one. */
649 if (so_list_ptr != NULL)
651 so_list_ptr -> next = new;
658 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
659 sizeof (struct link_map));
660 /* For the SVR4 version, there is one entry that has no name
661 (for the inferior executable) since it is not a shared object. */
662 if (LM_NAME (new) != 0)
664 if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
666 error ("find_solib: Can't read pathname for load map\n");
667 new -> so_name[MAX_PATH_SIZE - 1] = 0;
668 solib_map_sections (new);
671 return (so_list_next);
674 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
677 symbol_add_stub (arg)
680 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
682 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
683 (unsigned int) so -> textsection -> addr,
692 solib_add -- add a shared library file to the symtab and section list
696 void solib_add (char *arg_string, int from_tty,
697 struct target_ops *target)
704 solib_add (arg_string, from_tty, target)
707 struct target_ops *target;
709 register struct so_list *so = NULL; /* link map state variable */
714 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
716 error ("Invalid regexp: %s", re_err);
719 /* Getting new symbols may change our opinion about what is
721 reinit_frame_cache ();
723 while ((so = find_solib (so)) != NULL)
725 if (so -> so_name[0] && re_exec (so -> so_name))
727 if (so -> symbols_loaded)
731 printf ("Symbols already loaded for %s\n", so -> so_name);
736 catch_errors (symbol_add_stub, (char *) so,
737 "Error while reading shared library symbols:\n");
739 special_symbol_handling (so);
740 so -> symbols_loaded = 1;
741 so -> from_tty = from_tty;
746 /* Now add the shared library sections to the section table of the
747 specified target, if any. */
750 /* Count how many new section_table entries there are. */
753 while ((so = find_solib (so)) != NULL)
755 if (so -> so_name[0])
757 count += so -> sections_end - so -> sections;
763 /* Reallocate the target's section table including the new size. */
764 if (target -> to_sections)
766 old = target -> to_sections_end - target -> to_sections;
767 target -> to_sections = (struct section_table *)
768 realloc ((char *)target -> to_sections,
769 (sizeof (struct section_table)) * (count + old));
774 target -> to_sections = (struct section_table *)
775 malloc ((sizeof (struct section_table)) * count);
777 target -> to_sections_end = target -> to_sections + (count + old);
779 /* Add these section table entries to the target's table. */
780 while ((so = find_solib (so)) != NULL)
782 if (so -> so_name[0])
784 count = so -> sections_end - so -> sections;
785 memcpy ((char *) (target -> to_sections + old),
787 (sizeof (struct section_table)) * count);
799 info_sharedlibrary_command -- code for "info sharedlibrary"
803 static void info_sharedlibrary_command ()
807 Walk through the shared library list and print information
808 about each attached library.
812 info_sharedlibrary_command (ignore, from_tty)
816 register struct so_list *so = NULL; /* link map state variable */
819 if (exec_bfd == NULL)
821 printf ("No exec file.\n");
824 while ((so = find_solib (so)) != NULL)
826 if (so -> so_name[0])
830 printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
831 "Shared Object Library");
834 printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so), "08"));
835 printf ("%-12s", local_hex_string_custom (so -> lmend, "08"));
836 printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
837 printf ("%s\n", so -> so_name);
840 if (so_list_head == NULL)
842 printf ("No shared libraries loaded at this time.\n");
850 solib_address -- check to see if an address is in a shared lib
854 int solib_address (CORE_ADDR address)
858 Provides a hook for other gdb routines to discover whether or
859 not a particular address is within the mapped address space of
860 a shared library. Any address between the base mapping address
861 and the first address beyond the end of the last mapping, is
862 considered to be within the shared library address space, for
865 For example, this routine is called at one point to disable
866 breakpoints which are in shared libraries that are not currently
871 solib_address (address)
874 register struct so_list *so = 0; /* link map state variable */
876 while ((so = find_solib (so)) != NULL)
878 if (so -> so_name[0])
880 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
881 (address < (CORE_ADDR) so -> lmend))
890 /* Called by free_all_symtabs */
895 struct so_list *next;
899 if (so_list_head -> sections)
901 free ((PTR)so_list_head -> sections);
903 next = so_list_head -> next;
904 free((PTR)so_list_head);
914 disable_break -- remove the "mapping changed" breakpoint
918 static int disable_break ()
922 Removes the breakpoint that gets hit when the dynamic linker
923 completes a mapping change.
932 #ifndef SVR4_SHARED_LIBS
936 /* Read the debugger structure from the inferior to retrieve the
937 address of the breakpoint and the original contents of the
938 breakpoint address. Remove the breakpoint by writing the original
941 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
943 /* Set `in_debugger' to zero now. */
945 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
947 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
948 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
949 sizeof (debug_copy.ldd_bp_inst));
951 #else /* SVR4_SHARED_LIBS */
953 /* Note that breakpoint address and original contents are in our address
954 space, so we just need to write the original contents back. */
956 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
961 #endif /* !SVR4_SHARED_LIBS */
963 /* For the SVR4 version, we always know the breakpoint address. For the
964 SunOS version we don't know it until the above code is executed.
965 Grumble if we are stopped anywhere besides the breakpoint address. */
967 if (stop_pc != breakpoint_addr)
969 warning ("stopped at unknown breakpoint while handling shared libraries");
979 enable_break -- arrange for dynamic linker to hit breakpoint
983 int enable_break (void)
987 Both the SunOS and the SVR4 dynamic linkers have, as part of their
988 debugger interface, support for arranging for the inferior to hit
989 a breakpoint after mapping in the shared libraries. This function
990 enables that breakpoint.
992 For SunOS, there is a special flag location (in_debugger) which we
993 set to 1. When the dynamic linker sees this flag set, it will set
994 a breakpoint at a location known only to itself, after saving the
995 original contents of that place and the breakpoint address itself,
996 in it's own internal structures. When we resume the inferior, it
997 will eventually take a SIGTRAP when it runs into the breakpoint.
998 We handle this (in a different place) by restoring the contents of
999 the breakpointed location (which is only known after it stops),
1000 chasing around to locate the shared libraries that have been
1001 loaded, then resuming.
1003 For SVR4, the debugger interface structure contains a member (r_brk)
1004 which is statically initialized at the time the shared library is
1005 built, to the offset of a function (_r_debug_state) which is guaran-
1006 teed to be called once before mapping in a library, and again when
1007 the mapping is complete. At the time we are examining this member,
1008 it contains only the unrelocated offset of the function, so we have
1009 to do our own relocation. Later, when the dynamic linker actually
1010 runs, it relocates r_brk to be the actual address of _r_debug_state().
1012 The debugger interface structure also contains an enumeration which
1013 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1014 depending upon whether or not the library is being mapped or unmapped,
1015 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1022 #ifndef SVR4_SHARED_LIBS
1027 /* Get link_dynamic structure */
1029 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1030 sizeof (dynamic_copy));
1037 /* Calc address of debugger interface structure */
1039 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1041 /* Calc address of `in_debugger' member of debugger interface structure */
1043 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1044 (char *) &debug_copy);
1046 /* Write a value of 1 to this member. */
1050 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1052 #else /* SVR4_SHARED_LIBS */
1056 struct minimal_symbol *msymbol;
1058 msymbol = lookup_minimal_symbol ("main", symfile_objfile);
1059 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1061 breakpoint_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1068 if (target_insert_breakpoint (breakpoint_addr, shadow_contents) != 0)
1073 #else /* !BKPT_AT_MAIN */
1075 struct symtab_and_line sal;
1077 /* Read the debugger interface structure directly. */
1079 read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
1081 /* Set breakpoint at the debugger interface stub routine that will
1082 be called just prior to each mapping change and again after the
1083 mapping change is complete. Set up the (nonexistent) handler to
1084 deal with hitting these breakpoints. (FIXME). */
1086 warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
1088 #endif /* BKPT_AT_MAIN */
1090 #endif /* !SVR4_SHARED_LIBS */
1099 solib_create_inferior_hook -- shared library startup support
1103 void solib_create_inferior_hook()
1107 When gdb starts up the inferior, it nurses it along (through the
1108 shell) until it is ready to execute it's first instruction. At this
1109 point, this function gets called via expansion of the macro
1110 SOLIB_CREATE_INFERIOR_HOOK.
1112 For both SunOS shared libraries, and SVR4 shared libraries, we
1113 can arrange to cooperate with the dynamic linker to discover the
1114 names of shared libraries that are dynamically linked, and the
1115 base addresses to which they are linked.
1117 This function is responsible for discovering those names and
1118 addresses, and saving sufficient information about them to allow
1119 their symbols to be read at a later time.
1123 Between enable_break() and disable_break(), this code does not
1124 properly handle hitting breakpoints which the user might have
1125 set in the startup code or in the dynamic linker itself. Proper
1126 handling will probably have to wait until the implementation is
1127 changed to use the "breakpoint handler function" method.
1129 Also, what if child has exit()ed? Must exit loop somehow.
1133 solib_create_inferior_hook()
1136 if ((debug_base = locate_base ()) == 0)
1138 /* Can't find the symbol or the executable is statically linked. */
1142 if (!enable_break ())
1144 warning ("shared library handler failed to enable breakpoint");
1148 /* Now run the target. It will eventually hit the breakpoint, at
1149 which point all of the libraries will have been mapped in and we
1150 can go groveling around in the dynamic linker structures to find
1151 out what we need to know about them. */
1153 clear_proceed_status ();
1154 stop_soon_quietly = 1;
1158 target_resume (0, stop_signal);
1159 wait_for_inferior ();
1161 while (stop_signal != SIGTRAP);
1162 stop_soon_quietly = 0;
1164 /* We are now either at the "mapping complete" breakpoint (or somewhere
1165 else, a condition we aren't prepared to deal with anyway), so adjust
1166 the PC as necessary after a breakpoint, disable the breakpoint, and
1167 add any shared libraries that were mapped in. */
1169 if (DECR_PC_AFTER_BREAK)
1171 stop_pc -= DECR_PC_AFTER_BREAK;
1172 write_register (PC_REGNUM, stop_pc);
1175 if (!disable_break ())
1177 warning ("shared library handler failed to disable breakpoint");
1180 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1187 special_symbol_handling -- additional shared library symbol handling
1191 void special_symbol_handling (struct so_list *so)
1195 Once the symbols from a shared object have been loaded in the usual
1196 way, we are called to do any system specific symbol handling that
1199 For Suns, this consists of grunging around in the dynamic linkers
1200 structures to find symbol definitions for "common" symbols and
1201 adding them to the minimal symbol table for the corresponding
1207 special_symbol_handling (so)
1210 #ifndef SVR4_SHARED_LIBS
1213 if (debug_addr == 0)
1215 /* Get link_dynamic structure */
1217 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1218 sizeof (dynamic_copy));
1225 /* Calc address of debugger interface structure */
1226 /* FIXME, this needs work for cross-debugging of core files
1227 (byteorder, size, alignment, etc). */
1229 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1232 /* Read the debugger structure from the inferior, just to make sure
1233 we have a current copy. */
1235 j = target_read_memory (debug_addr, (char *) &debug_copy,
1236 sizeof (debug_copy));
1238 return; /* unreadable */
1240 /* Get common symbol definitions for the loaded object. */
1242 if (debug_copy.ldd_cp)
1244 solib_add_common_symbols (debug_copy.ldd_cp, so -> objfile);
1247 #endif /* !SVR4_SHARED_LIBS */
1255 sharedlibrary_command -- handle command to explicitly add library
1259 static void sharedlibrary_command (char *args, int from_tty)
1266 sharedlibrary_command (args, from_tty)
1271 solib_add (args, from_tty, (struct target_ops *) 0);
1278 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1279 "Load shared object library symbols for files matching REGEXP.");
1280 add_info ("sharedlibrary", info_sharedlibrary_command,
1281 "Status of loaded shared object libraries.");