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>
41 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
43 /* On SVR4 systems, for the initial implementation, use main() as the
44 "startup mapping complete" breakpoint address. The models for SunOS
45 and SVR4 dynamic linking debugger support are different in that SunOS
46 hits one breakpoint when all mapping is complete while using the SVR4
47 debugger support takes two breakpoint hits for each file mapped, and
48 there is no way to know when the "last" one is hit. Both these
49 mechanisms should be tied to a "breakpoint service routine" that
50 gets automatically executed whenever one of the breakpoints indicating
51 a change in mapping is hit. This is a future enhancement. (FIXME) */
53 #define BKPT_AT_MAIN 1
55 /* local data declarations */
57 #ifndef SVR4_SHARED_LIBS
59 #define DEBUG_BASE "_DYNAMIC"
60 #define LM_ADDR(so) ((so) -> lm.lm_addr)
61 #define LM_NEXT(so) ((so) -> lm.lm_next)
62 #define LM_NAME(so) ((so) -> lm.lm_name)
63 static struct link_dynamic dynamic_copy
;
64 static struct link_dynamic_2 ld_2_copy
;
65 static struct ld_debug debug_copy
;
66 static CORE_ADDR debug_addr
;
67 static CORE_ADDR flag_addr
;
69 #else /* SVR4_SHARED_LIBS */
71 #define DEBUG_BASE "_r_debug"
72 #define LM_ADDR(so) ((so) -> lm.l_addr)
73 #define LM_NEXT(so) ((so) -> lm.l_next)
74 #define LM_NAME(so) ((so) -> lm.l_name)
75 static struct r_debug debug_copy
;
76 char shadow_contents
[BREAKPOINT_MAX
]; /* Stash old bkpt addr contents */
78 #endif /* !SVR4_SHARED_LIBS */
81 struct so_list
*next
; /* next structure in linked list */
82 struct link_map lm
; /* copy of link map from inferior */
83 struct link_map
*lmaddr
; /* addr in inferior lm was read from */
84 CORE_ADDR lmend
; /* upper addr bound of mapped object */
85 char so_name
[MAX_PATH_SIZE
]; /* shared object lib name (FIXME) */
86 char symbols_loaded
; /* flag: symbols read in yet? */
87 char from_tty
; /* flag: print msgs? */
88 bfd
*so_bfd
; /* bfd for so_name */
89 struct objfile
*objfile
; /* objfile for loaded lib */
90 struct section_table
*sections
;
91 struct section_table
*sections_end
;
94 static struct so_list
*so_list_head
; /* List of known shared objects */
95 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
96 static CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
98 /* Local function prototypes */
101 special_symbol_handling
PARAMS ((struct so_list
*));
104 sharedlibrary_command
PARAMS ((char *, int));
107 enable_break
PARAMS ((void));
110 disable_break
PARAMS ((void));
113 info_sharedlibrary_command
PARAMS ((void));
116 symbol_add_stub
PARAMS ((char *));
118 static struct so_list
*
119 find_solib
PARAMS ((struct so_list
*));
121 static struct link_map
*
122 first_link_map_member
PARAMS ((void));
125 locate_base
PARAMS ((void));
128 look_for_base
PARAMS ((int, CORE_ADDR
));
131 bfd_lookup_symbol
PARAMS ((bfd
*, char *));
134 solib_map_sections
PARAMS ((struct so_list
*));
136 #ifndef SVR4_SHARED_LIBS
139 solib_add_common_symbols
PARAMS ((struct rtc_symb
*, struct objfile
*));
147 solib_map_sections -- open bfd and build sections for shared lib
151 static void solib_map_sections (struct so_list *so)
155 Given a pointer to one of the shared objects in our list
156 of mapped objects, use the recorded name to open a bfd
157 descriptor for the object, build a section table, and then
158 relocate all the section addresses by the base address at
159 which the shared object was mapped.
163 In most (all?) cases the shared object file name recorded in the
164 dynamic linkage tables will be a fully qualified pathname. For
165 cases where it isn't, do we really mimic the systems search
166 mechanism correctly in the below code (particularly the tilde
171 solib_map_sections (so
)
175 char *scratch_pathname
;
177 struct section_table
*p
;
179 filename
= tilde_expand (so
-> so_name
);
180 make_cleanup (free
, filename
);
182 scratch_chan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
184 if (scratch_chan
< 0)
186 scratch_chan
= openp (getenv ("LD_LIBRARY_PATH"), 1, filename
,
187 O_RDONLY
, 0, &scratch_pathname
);
189 if (scratch_chan
< 0)
191 perror_with_name (filename
);
194 so
-> so_bfd
= bfd_fdopenr (scratch_pathname
, NULL
, scratch_chan
);
197 error ("Could not open `%s' as an executable file: %s",
198 scratch_pathname
, bfd_errmsg (bfd_error
));
200 if (!bfd_check_format (so
-> so_bfd
, bfd_object
))
202 error ("\"%s\": not in executable format: %s.",
203 scratch_pathname
, bfd_errmsg (bfd_error
));
205 if (build_section_table (so
-> so_bfd
, &so
-> sections
, &so
-> sections_end
))
207 error ("Can't find the file sections in `%s': %s",
208 exec_bfd
-> filename
, bfd_errmsg (bfd_error
));
211 for (p
= so
-> sections
; p
< so
-> sections_end
; p
++)
213 /* Relocate the section binding addresses as recorded in the shared
214 object's file by the base address to which the object was actually
216 p
-> addr
+= (CORE_ADDR
) LM_ADDR (so
);
217 p
-> endaddr
+= (CORE_ADDR
) LM_ADDR (so
);
218 so
-> lmend
= (CORE_ADDR
) max (p
-> endaddr
, so
-> lmend
);
222 /* Read all dynamically loaded common symbol definitions from the inferior
223 and add them to the minimal symbol table for the shared library objfile. */
225 #ifndef SVR4_SHARED_LIBS
228 solib_add_common_symbols (rtc_symp
, objfile
)
229 struct rtc_symb
*rtc_symp
;
230 struct objfile
*objfile
;
232 struct rtc_symb inferior_rtc_symb
;
233 struct nlist inferior_rtc_nlist
;
238 init_minimal_symbol_collection ();
239 make_cleanup (discard_minimal_symbols
, 0);
243 read_memory ((CORE_ADDR
) rtc_symp
,
244 (char *) &inferior_rtc_symb
,
245 sizeof (inferior_rtc_symb
));
246 read_memory ((CORE_ADDR
) inferior_rtc_symb
.rtc_sp
,
247 (char *) &inferior_rtc_nlist
,
248 sizeof(inferior_rtc_nlist
));
249 if (inferior_rtc_nlist
.n_type
== N_COMM
)
251 /* FIXME: The length of the symbol name is not available, but in the
252 current implementation the common symbol is allocated immediately
253 behind the name of the symbol. */
254 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
256 origname
= name
= xmalloc (len
);
257 read_memory ((CORE_ADDR
) inferior_rtc_nlist
.n_un
.n_name
, name
, len
);
259 /* Don't enter the symbol twice if the target is re-run. */
261 #ifdef NAMES_HAVE_UNDERSCORE
267 /* FIXME: Do we really want to exclude symbols which happen
268 to match symbols for other locations in the inferior's
269 address space, even when they are in different linkage units? */
270 if (lookup_minimal_symbol (name
, (struct objfile
*) NULL
) == NULL
)
272 name
= obsavestring (name
, strlen (name
),
273 &objfile
-> symbol_obstack
);
274 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
279 rtc_symp
= inferior_rtc_symb
.rtc_next
;
282 /* Install any minimal symbols that have been collected as the current
283 minimal symbols for this objfile. */
285 install_minimal_symbols (objfile
);
288 #endif /* SVR4_SHARED_LIBS */
294 bfd_lookup_symbol -- lookup the value for a specific symbol
298 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
302 An expensive way to lookup the value of a single symbol for
303 bfd's that are only temporary anyway. This is used by the
304 shared library support to find the address of the debugger
305 interface structures in the shared library.
307 Note that 0 is specifically allowed as an error return (no
310 FIXME: See if there is a less "expensive" way of doing this.
311 Also see if there is already another bfd or gdb function
312 that specifically does this, and if so, use it.
316 bfd_lookup_symbol (abfd
, symname
)
320 unsigned int storage_needed
;
322 asymbol
**symbol_table
;
323 unsigned int number_of_symbols
;
325 struct cleanup
*back_to
;
326 CORE_ADDR symaddr
= 0;
328 storage_needed
= get_symtab_upper_bound (abfd
);
330 if (storage_needed
> 0)
332 symbol_table
= (asymbol
**) bfd_xmalloc (storage_needed
);
333 back_to
= make_cleanup (free
, symbol_table
);
334 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
336 for (i
= 0; i
< number_of_symbols
; i
++)
338 sym
= *symbol_table
++;
339 if (strcmp (sym
-> name
, symname
) == 0)
341 symaddr
= sym
-> value
;
345 do_cleanups (back_to
);
354 look_for_base -- examine file for each mapped address segment
358 static int look_for_base (int fd, CORE_ADDR baseaddr)
362 This function is passed to proc_iterate_over_mappings, which
363 causes it to get called once for each mapped address space, with
364 an open file descriptor for the file mapped to that space, and the
365 base address of that mapped space.
367 Our job is to find the symbol DEBUG_BASE in the file that this
368 fd is open on, if it exists, and if so, initialize the dynamic
369 linker structure base address debug_base.
371 Note that this is a computationally expensive proposition, since
372 we basically have to open a bfd on every call, so we specifically
373 avoid opening the exec file.
377 look_for_base (fd
, baseaddr
)
384 /* If the fd is -1, then there is no file that corresponds to this
385 mapped memory segment, so skip it. Also, if the fd corresponds
386 to the exec file, skip it as well. */
388 if ((fd
== -1) || fdmatch (fileno ((FILE *)(exec_bfd
-> iostream
)), fd
))
393 /* Try to open whatever random file this fd corresponds to. Note that
394 we have no way currently to find the filename. Don't gripe about
395 any problems we might have, just fail. */
397 if ((interp_bfd
= bfd_fdopenr ("unnamed", NULL
, fd
)) == NULL
)
401 if (!bfd_check_format (interp_bfd
, bfd_object
))
403 bfd_close (interp_bfd
);
407 /* Now try to find our DEBUG_BASE symbol in this file, which we at
408 least know to be a valid ELF executable or shared library. */
410 if ((address
= bfd_lookup_symbol (interp_bfd
, DEBUG_BASE
)) == 0)
412 bfd_close (interp_bfd
);
416 /* Eureka! We found the symbol. But now we may need to relocate it
417 by the base address. If the symbol's value is less than the base
418 address of the shared library, then it hasn't yet been relocated
419 by the dynamic linker, and we have to do it ourself. FIXME: Note
420 that we make the assumption that the first segment that corresponds
421 to the shared library has the base address to which the library
424 if (address
< baseaddr
)
428 debug_base
= address
;
429 bfd_close (interp_bfd
);
437 locate_base -- locate the base address of dynamic linker structs
441 CORE_ADDR locate_base (void)
445 For both the SunOS and SVR4 shared library implementations, if the
446 inferior executable has been linked dynamically, there is a single
447 address somewhere in the inferior's data space which is the key to
448 locating all of the dynamic linker's runtime structures. This
449 address is the value of the symbol defined by the macro DEBUG_BASE.
450 The job of this function is to find and return that address, or to
451 return 0 if there is no such address (the executable is statically
454 For SunOS, the job is almost trivial, since the dynamic linker and
455 all of it's structures are statically linked to the executable at
456 link time. Thus the symbol for the address we are looking for has
457 already been added to the minimal symbol table for the executable's
458 objfile at the time the symbol file's symbols were read, and all we
459 have to do is look it up there. Note that we explicitly do NOT want
460 to find the copies in the shared library.
462 The SVR4 version is much more complicated because the dynamic linker
463 and it's structures are located in the shared C library, which gets
464 run as the executable's "interpreter" by the kernel. We have to go
465 to a lot more work to discover the address of DEBUG_BASE. Because
466 of this complexity, we cache the value we find and return that value
467 on subsequent invocations. Note there is no copy in the executable
470 Note that we can assume nothing about the process state at the time
471 we need to find this address. We may be stopped on the first instruc-
472 tion of the interpreter (C shared library), the first instruction of
473 the executable itself, or somewhere else entirely (if we attached
474 to the process for example).
482 #ifndef SVR4_SHARED_LIBS
484 struct minimal_symbol
*msymbol
;
485 CORE_ADDR address
= 0;
487 /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
488 being debugged, since there is a duplicate named symbol in the shared
489 library. We don't want the shared library versions. */
491 msymbol
= lookup_minimal_symbol (DEBUG_BASE
, symfile_objfile
);
492 if ((msymbol
!= NULL
) && (msymbol
-> address
!= 0))
494 address
= msymbol
-> address
;
498 #else /* SVR4_SHARED_LIBS */
500 /* Check to see if we have a currently valid address, and if so, avoid
501 doing all this work again and just return the cached address. If
502 we have no cached address, ask the /proc support interface to iterate
503 over the list of mapped address segments, calling look_for_base() for
504 each segment. When we are done, we will have either found the base
509 proc_iterate_over_mappings (look_for_base
);
513 #endif /* !SVR4_SHARED_LIBS */
517 static struct link_map
*
518 first_link_map_member ()
520 struct link_map
*lm
= NULL
;
522 #ifndef SVR4_SHARED_LIBS
524 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
525 if (dynamic_copy
.ld_version
>= 2)
527 /* It is a version that we can deal with, so read in the secondary
528 structure and find the address of the link map list from it. */
529 read_memory ((CORE_ADDR
) dynamic_copy
.ld_un
.ld_2
, (char *) &ld_2_copy
,
530 sizeof (struct link_dynamic_2
));
531 lm
= ld_2_copy
.ld_loaded
;
534 #else /* SVR4_SHARED_LIBS */
536 read_memory (debug_base
, (char *) &debug_copy
, sizeof (struct r_debug
));
537 lm
= debug_copy
.r_map
;
539 #endif /* !SVR4_SHARED_LIBS */
548 find_solib -- step through list of shared objects
552 struct so_list *find_solib (struct so_list *so_list_ptr)
556 This module contains the routine which finds the names of any
557 loaded "images" in the current process. The argument in must be
558 NULL on the first call, and then the returned value must be passed
559 in on subsequent calls. This provides the capability to "step" down
560 the list of loaded objects. On the last object, a NULL value is
563 The arg and return value are "struct link_map" pointers, as defined
567 static struct so_list
*
568 find_solib (so_list_ptr
)
569 struct so_list
*so_list_ptr
; /* Last lm or NULL for first one */
571 struct so_list
*so_list_next
= NULL
;
572 struct link_map
*lm
= NULL
;
575 if (so_list_ptr
== NULL
)
577 /* We are setting up for a new scan through the loaded images. */
578 if ((so_list_next
= so_list_head
) == NULL
)
580 /* We have not already read in the dynamic linking structures
581 from the inferior, lookup the address of the base structure. */
582 debug_base
= locate_base ();
585 /* Read the base structure in and find the address of the first
586 link map list member. */
587 lm
= first_link_map_member ();
593 /* We have been called before, and are in the process of walking
594 the shared library list. Advance to the next shared object. */
595 if ((lm
= LM_NEXT (so_list_ptr
)) == NULL
)
597 /* We have hit the end of the list, so check to see if any were
598 added, but be quiet if we can't read from the target any more. */
599 int status
= target_read_memory ((CORE_ADDR
) so_list_ptr
-> lmaddr
,
600 (char *) &(so_list_ptr
-> lm
),
601 sizeof (struct link_map
));
604 lm
= LM_NEXT (so_list_ptr
);
611 so_list_next
= so_list_ptr
-> next
;
613 if ((so_list_next
== NULL
) && (lm
!= NULL
))
615 /* Get next link map structure from inferior image and build a local
616 abbreviated load_map structure */
617 new = (struct so_list
*) xmalloc (sizeof (struct so_list
));
618 (void) memset ((char *) new, 0, sizeof (struct so_list
));
620 /* Add the new node as the next node in the list, or as the root
621 node if this is the first one. */
622 if (so_list_ptr
!= NULL
)
624 so_list_ptr
-> next
= new;
631 read_memory ((CORE_ADDR
) lm
, (char *) &(new -> lm
),
632 sizeof (struct link_map
));
633 /* For the SVR4 version, there is one entry that has no name
634 (for the inferior executable) since it is not a shared object. */
635 if (LM_NAME (new) != 0)
637 if (!target_read_string((CORE_ADDR
) LM_NAME (new), new -> so_name
,
639 error ("find_solib: Can't read pathname for load map\n");
640 new -> so_name
[MAX_PATH_SIZE
- 1] = 0;
641 solib_map_sections (new);
644 return (so_list_next
);
647 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
650 symbol_add_stub (arg
)
653 register struct so_list
*so
= (struct so_list
*) arg
; /* catch_errs bogon */
655 so
-> objfile
= symbol_file_add (so
-> so_name
, so
-> from_tty
,
656 (unsigned int) LM_ADDR (so
), 0, 0, 0);
664 solib_add -- add a shared library file to the symtab and section list
668 void solib_add (char *arg_string, int from_tty,
669 struct target_ops *target)
676 solib_add (arg_string
, from_tty
, target
)
679 struct target_ops
*target
;
681 register struct so_list
*so
= NULL
; /* link map state variable */
686 if ((re_err
= re_comp (arg_string
? arg_string
: ".")) != NULL
)
688 error ("Invalid regexp: %s", re_err
);
691 /* Getting new symbols may change our opinion about what is
693 reinit_frame_cache ();
695 while ((so
= find_solib (so
)) != NULL
)
697 if (so
-> so_name
[0] && re_exec (so
-> so_name
))
699 if (so
-> symbols_loaded
)
703 printf ("Symbols already loaded for %s\n", so
-> so_name
);
708 catch_errors (symbol_add_stub
, (char *) so
,
709 "Error while reading shared library symbols:\n");
711 special_symbol_handling (so
);
712 so
-> symbols_loaded
= 1;
713 so
-> from_tty
= from_tty
;
718 /* Now add the shared library sections to the section table of the
719 specified target, if any. */
722 /* Count how many new section_table entries there are. */
725 while ((so
= find_solib (so
)) != NULL
)
727 if (so
-> so_name
[0])
729 count
+= so
-> sections_end
- so
-> sections
;
735 /* Reallocate the target's section table including the new size. */
736 if (target
-> to_sections
)
738 old
= target
-> to_sections_end
- target
-> to_sections
;
739 target
-> to_sections
= (struct section_table
*)
740 realloc ((char *)target
-> to_sections
,
741 (sizeof (struct section_table
)) * (count
+ old
));
746 target
-> to_sections
= (struct section_table
*)
747 malloc ((sizeof (struct section_table
)) * count
);
749 target
-> to_sections_end
= target
-> to_sections
+ (count
+ old
);
751 /* Add these section table entries to the target's table. */
752 while ((so
= find_solib (so
)) != NULL
)
754 if (so
-> so_name
[0])
756 count
= so
-> sections_end
- so
-> sections
;
757 bcopy (so
-> sections
, (char *)(target
-> to_sections
+ old
),
758 (sizeof (struct section_table
)) * count
);
770 info_sharedlibrary_command -- code for "info sharedlibrary"
774 static void info_sharedlibrary_command ()
778 Walk through the shared library list and print information
779 about each attached library.
783 info_sharedlibrary_command ()
785 register struct so_list
*so
= NULL
; /* link map state variable */
788 if (exec_bfd
== NULL
)
790 printf ("No exec file.\n");
793 while ((so
= find_solib (so
)) != NULL
)
795 if (so
-> so_name
[0])
799 printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
800 "Shared Object Library");
803 printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so
), "08"));
804 printf ("%-12s", local_hex_string_custom (so
-> lmend
, "08"));
805 printf ("%-12s", so
-> symbols_loaded
? "Yes" : "No");
806 printf ("%s\n", so
-> so_name
);
809 if (so_list_head
== NULL
)
811 printf ("No shared libraries loaded at this time.\n");
819 solib_address -- check to see if an address is in a shared lib
823 int solib_address (CORE_ADDR address)
827 Provides a hook for other gdb routines to discover whether or
828 not a particular address is within the mapped address space of
829 a shared library. Any address between the base mapping address
830 and the first address beyond the end of the last mapping, is
831 considered to be within the shared library address space, for
834 For example, this routine is called at one point to disable
835 breakpoints which are in shared libraries that are not currently
840 solib_address (address
)
843 register struct so_list
*so
= 0; /* link map state variable */
845 while ((so
= find_solib (so
)) != NULL
)
847 if (so
-> so_name
[0])
849 if ((address
>= (CORE_ADDR
) LM_ADDR (so
)) &&
850 (address
< (CORE_ADDR
) so
-> lmend
))
859 /* Called by free_all_symtabs */
864 struct so_list
*next
;
868 if (so_list_head
-> sections
)
870 free (so_list_head
-> sections
);
872 if (so_list_head
-> so_bfd
)
874 bfd_close (so_list_head
-> so_bfd
);
876 next
= so_list_head
-> next
;
887 disable_break -- remove the "mapping changed" breakpoint
891 static int disable_break ()
895 Removes the breakpoint that gets hit when the dynamic linker
896 completes a mapping change.
905 #ifndef SVR4_SHARED_LIBS
909 /* Read the debugger structure from the inferior to retrieve the
910 address of the breakpoint and the original contents of the
911 breakpoint address. Remove the breakpoint by writing the original
914 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
916 /* Set `in_debugger' to zero now. */
918 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
920 breakpoint_addr
= (CORE_ADDR
) debug_copy
.ldd_bp_addr
;
921 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
922 sizeof (debug_copy
.ldd_bp_inst
));
924 #else /* SVR4_SHARED_LIBS */
926 /* Note that breakpoint address and original contents are in our address
927 space, so we just need to write the original contents back. */
929 if (memory_remove_breakpoint (breakpoint_addr
, shadow_contents
) != 0)
934 #endif /* !SVR4_SHARED_LIBS */
936 /* For the SVR4 version, we always know the breakpoint address. For the
937 SunOS version we don't know it until the above code is executed.
938 Grumble if we are stopped anywhere besides the breakpoint address. */
940 if (stop_pc
!= breakpoint_addr
)
942 warning ("stopped at unknown breakpoint while handling shared libraries");
952 enable_break -- arrange for dynamic linker to hit breakpoint
956 int enable_break (void)
960 Both the SunOS and the SVR4 dynamic linkers have, as part of their
961 debugger interface, support for arranging for the inferior to hit
962 a breakpoint after mapping in the shared libraries. This function
963 enables that breakpoint.
965 For SunOS, there is a special flag location (in_debugger) which we
966 set to 1. When the dynamic linker sees this flag set, it will set
967 a breakpoint at a location known only to itself, after saving the
968 original contents of that place and the breakpoint address itself,
969 in it's own internal structures. When we resume the inferior, it
970 will eventually take a SIGTRAP when it runs into the breakpoint.
971 We handle this (in a different place) by restoring the contents of
972 the breakpointed location (which is only known after it stops),
973 chasing around to locate the shared libraries that have been
974 loaded, then resuming.
976 For SVR4, the debugger interface structure contains a member (r_brk)
977 which is statically initialized at the time the shared library is
978 built, to the offset of a function (_r_debug_state) which is guaran-
979 teed to be called once before mapping in a library, and again when
980 the mapping is complete. At the time we are examining this member,
981 it contains only the unrelocated offset of the function, so we have
982 to do our own relocation. Later, when the dynamic linker actually
983 runs, it relocates r_brk to be the actual address of _r_debug_state().
985 The debugger interface structure also contains an enumeration which
986 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
987 depending upon whether or not the library is being mapped or unmapped,
988 and then set to RT_CONSISTENT after the library is mapped/unmapped.
997 #ifndef SVR4_SHARED_LIBS
1001 /* Get link_dynamic structure */
1003 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1004 sizeof (dynamic_copy
));
1011 /* Calc address of debugger interface structure */
1013 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1015 /* Calc address of `in_debugger' member of debugger interface structure */
1017 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
1018 (char *) &debug_copy
);
1020 /* Write a value of 1 to this member. */
1024 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1026 #else /* SVR4_SHARED_LIBS */
1030 struct minimal_symbol
*msymbol
;
1032 msymbol
= lookup_minimal_symbol ("main", symfile_objfile
);
1033 if ((msymbol
!= NULL
) && (msymbol
-> address
!= 0))
1035 breakpoint_addr
= msymbol
-> address
;
1042 if (target_insert_breakpoint (breakpoint_addr
, shadow_contents
) != 0)
1047 #else /* !BKPT_AT_MAIN */
1049 struct symtab_and_line sal
;
1051 /* Read the debugger interface structure directly. */
1053 read_memory (debug_base
, (char *) &debug_copy
, sizeof (debug_copy
));
1055 /* Set breakpoint at the debugger interface stub routine that will
1056 be called just prior to each mapping change and again after the
1057 mapping change is complete. Set up the (nonexistent) handler to
1058 deal with hitting these breakpoints. (FIXME). */
1060 warning ("'%s': line %d: missing SVR4 support code", __FILE__
, __LINE__
);
1062 #endif /* BKPT_AT_MAIN */
1064 #endif /* !SVR4_SHARED_LIBS */
1073 solib_create_inferior_hook -- shared library startup support
1077 void solib_create_inferior_hook()
1081 When gdb starts up the inferior, it nurses it along (through the
1082 shell) until it is ready to execute it's first instruction. At this
1083 point, this function gets called via expansion of the macro
1084 SOLIB_CREATE_INFERIOR_HOOK.
1086 For both SunOS shared libraries, and SVR4 shared libraries, we
1087 can arrange to cooperate with the dynamic linker to discover the
1088 names of shared libraries that are dynamically linked, and the
1089 base addresses to which they are linked.
1091 This function is responsible for discovering those names and
1092 addresses, and saving sufficient information about them to allow
1093 their symbols to be read at a later time.
1097 Between enable_break() and disable_break(), this code does not
1098 properly handle hitting breakpoints which the user might have
1099 set in the startup code or in the dynamic linker itself. Proper
1100 handling will probably have to wait until the implementation is
1101 changed to use the "breakpoint handler function" method.
1103 Also, what if child has exit()ed? Must exit loop somehow.
1107 solib_create_inferior_hook()
1109 CORE_ADDR debug_addr
;
1111 CORE_ADDR in_debugger_addr
;
1112 CORE_ADDR breakpoint_addr
;
1115 if ((debug_base
= locate_base ()) == 0)
1117 /* Can't find the symbol or the executable is statically linked. */
1121 if (!enable_break ())
1123 warning ("shared library handler failed to enable breakpoint");
1127 /* Now run the target. It will eventually hit the breakpoint, at
1128 which point all of the libraries will have been mapped in and we
1129 can go groveling around in the dynamic linker structures to find
1130 out what we need to know about them. */
1132 clear_proceed_status ();
1133 stop_soon_quietly
= 1;
1137 target_resume (0, stop_signal
);
1138 wait_for_inferior ();
1140 while (stop_signal
!= SIGTRAP
);
1141 stop_soon_quietly
= 0;
1143 /* We are now either at the "mapping complete" breakpoint (or somewhere
1144 else, a condition we aren't prepared to deal with anyway), so adjust
1145 the PC as necessary after a breakpoint, disable the breakpoint, and
1146 add any shared libraries that were mapped in. */
1148 if (DECR_PC_AFTER_BREAK
)
1150 stop_pc
-= DECR_PC_AFTER_BREAK
;
1151 write_register (PC_REGNUM
, stop_pc
);
1154 if (!disable_break ())
1156 warning ("shared library handler failed to disable breakpoint");
1159 solib_add ((char *) 0, 0, (struct target_ops
*) 0);
1166 special_symbol_handling -- additional shared library symbol handling
1170 void special_symbol_handling (struct so_list *so)
1174 Once the symbols from a shared object have been loaded in the usual
1175 way, we are called to do any system specific symbol handling that
1178 For Suns, this consists of grunging around in the dynamic linkers
1179 structures to find symbol definitions for "common" symbols and
1180 adding them to the minimal symbol table for the corresponding
1186 special_symbol_handling (so
)
1189 #ifndef SVR4_SHARED_LIBS
1191 /* Read the debugger structure from the inferior, just to make sure
1192 we have a current copy. */
1194 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
1196 /* Get common symbol definitions for the loaded object. */
1198 if (debug_copy
.ldd_cp
)
1200 solib_add_common_symbols (debug_copy
.ldd_cp
, so
-> objfile
);
1203 #endif /* !SVR4_SHARED_LIBS */
1211 sharedlibrary_command -- handle command to explicitly add library
1215 static void sharedlibrary_command (char *args, int from_tty)
1222 sharedlibrary_command (args
, from_tty
)
1227 solib_add (args
, from_tty
, (struct target_ops
*) 0);
1234 add_com ("sharedlibrary", class_files
, sharedlibrary_command
,
1235 "Load shared object library symbols for files matching REGEXP.");
1236 add_info ("sharedlibrary", info_sharedlibrary_command
,
1237 "Status of loaded shared object libraries.");