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 some runtime startup
49 symbol as the "startup mapping complete" breakpoint address. The models
50 for SunOS and SVR4 dynamic linking debugger support are different in that
51 SunOS 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_SYMBOL 1
60 static char *bkpt_names[] = {
61 #ifdef SOLIB_BKPT_NAME
62 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
69 /* local data declarations */
71 #ifndef SVR4_SHARED_LIBS
73 #define DEBUG_BASE "_DYNAMIC"
74 #define LM_ADDR(so) ((so) -> lm.lm_addr)
75 #define LM_NEXT(so) ((so) -> lm.lm_next)
76 #define LM_NAME(so) ((so) -> lm.lm_name)
77 static struct link_dynamic dynamic_copy;
78 static struct link_dynamic_2 ld_2_copy;
79 static struct ld_debug debug_copy;
80 static CORE_ADDR debug_addr;
81 static CORE_ADDR flag_addr;
83 #else /* SVR4_SHARED_LIBS */
85 #define DEBUG_BASE "_r_debug"
86 #define LM_ADDR(so) ((so) -> lm.l_addr)
87 #define LM_NEXT(so) ((so) -> lm.l_next)
88 #define LM_NAME(so) ((so) -> lm.l_name)
89 static struct r_debug debug_copy;
90 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
92 #endif /* !SVR4_SHARED_LIBS */
95 struct so_list *next; /* next structure in linked list */
96 struct link_map lm; /* copy of link map from inferior */
97 struct link_map *lmaddr; /* addr in inferior lm was read from */
98 CORE_ADDR lmend; /* upper addr bound of mapped object */
99 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
100 char symbols_loaded; /* flag: symbols read in yet? */
101 char from_tty; /* flag: print msgs? */
102 struct objfile *objfile; /* objfile for loaded lib */
103 struct section_table *sections;
104 struct section_table *sections_end;
105 struct section_table *textsection;
109 static struct so_list *so_list_head; /* List of known shared objects */
110 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
111 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
114 fdmatch PARAMS ((int, int)); /* In libiberty */
116 /* Local function prototypes */
119 special_symbol_handling PARAMS ((struct so_list *));
122 sharedlibrary_command PARAMS ((char *, int));
125 enable_break PARAMS ((void));
128 disable_break PARAMS ((void));
131 info_sharedlibrary_command PARAMS ((char *, int));
134 symbol_add_stub PARAMS ((char *));
136 static struct so_list *
137 find_solib PARAMS ((struct so_list *));
139 static struct link_map *
140 first_link_map_member PARAMS ((void));
143 locate_base PARAMS ((void));
146 solib_map_sections PARAMS ((struct so_list *));
148 #ifdef SVR4_SHARED_LIBS
151 look_for_base PARAMS ((int, CORE_ADDR));
154 bfd_lookup_symbol PARAMS ((bfd *, char *));
159 solib_add_common_symbols PARAMS ((struct rtc_symb *, struct objfile *));
167 solib_map_sections -- open bfd and build sections for shared lib
171 static void solib_map_sections (struct so_list *so)
175 Given a pointer to one of the shared objects in our list
176 of mapped objects, use the recorded name to open a bfd
177 descriptor for the object, build a section table, and then
178 relocate all the section addresses by the base address at
179 which the shared object was mapped.
183 In most (all?) cases the shared object file name recorded in the
184 dynamic linkage tables will be a fully qualified pathname. For
185 cases where it isn't, do we really mimic the systems search
186 mechanism correctly in the below code (particularly the tilde
191 solib_map_sections (so)
195 char *scratch_pathname;
197 struct section_table *p;
198 struct cleanup *old_chain;
201 filename = tilde_expand (so -> so_name);
202 old_chain = make_cleanup (free, filename);
204 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
206 if (scratch_chan < 0)
208 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
209 O_RDONLY, 0, &scratch_pathname);
211 if (scratch_chan < 0)
213 perror_with_name (filename);
215 /* Leave scratch_pathname allocated. bfd->name will point to it. */
217 abfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
220 close (scratch_chan);
221 error ("Could not open `%s' as an executable file: %s",
222 scratch_pathname, bfd_errmsg (bfd_error));
224 /* Leave bfd open, core_xfer_memory and "info files" need it. */
226 abfd -> cacheable = true;
228 if (!bfd_check_format (abfd, bfd_object))
230 error ("\"%s\": not in executable format: %s.",
231 scratch_pathname, bfd_errmsg (bfd_error));
233 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
235 error ("Can't find the file sections in `%s': %s",
236 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_error));
239 for (p = so -> sections; p < so -> sections_end; p++)
241 /* Relocate the section binding addresses as recorded in the shared
242 object's file by the base address to which the object was actually
244 p -> addr += (CORE_ADDR) LM_ADDR (so);
245 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
246 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
247 if (STREQ (p -> sec_ptr -> name, ".text"))
249 so -> textsection = p;
253 /* Free the file names, close the file now. */
254 do_cleanups (old_chain);
257 /* Read all dynamically loaded common symbol definitions from the inferior
258 and add them to the minimal symbol table for the shared library objfile. */
260 #ifndef SVR4_SHARED_LIBS
262 /* This routine can be a real performance hog. According to some gprof data
264 time spend in solib_add (up to 20 minutes with 35 shared libraries) is
265 spent here, with 5/6 in lookup_minimal_symbol and 1/6 in read_memory.
269 * Hash the minimal symbols.
271 * Just record the name of the minimal symbol and lazily patch the
274 * Tell everyone to switch to Solaris2.
276 (1) Move the call to special_symbol_handling out of the find_solib
277 loop in solib_add. This will call it once, rather than 35 times, when
278 you have 35 shared libraries. It's in the loop to pass the current
279 solib's objfile so the symbols are added to that objfile's minsym.
280 But since the symbols are in common (BSS), it doesn't really matter
281 which objfile's minsyms they are added to, I think.
283 (2) Indeed, it might be best to create an objfile just for common minsyms,
284 thus not needing any objfile argument to solib_add_common_symbols.
286 (3) Remove the call to lookup_minimal_symbol from
287 solib_add_common_symbols. If a symbol appears multiple times in the
288 minsyms, we probably cope, more or less. Note that if we had an
289 objfile for just minsyms, install_minimal_symbols would automatically
290 remove duplicates caused by running solib_add_common_symbols several
295 solib_add_common_symbols (rtc_symp, objfile)
296 struct rtc_symb *rtc_symp;
297 struct objfile *objfile;
299 struct rtc_symb inferior_rtc_symb;
300 struct nlist inferior_rtc_nlist;
305 init_minimal_symbol_collection ();
306 make_cleanup (discard_minimal_symbols, 0);
310 read_memory ((CORE_ADDR) rtc_symp,
311 (char *) &inferior_rtc_symb,
312 sizeof (inferior_rtc_symb));
313 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
314 (char *) &inferior_rtc_nlist,
315 sizeof(inferior_rtc_nlist));
316 if (inferior_rtc_nlist.n_type == N_COMM)
318 /* FIXME: The length of the symbol name is not available, but in the
319 current implementation the common symbol is allocated immediately
320 behind the name of the symbol. */
321 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
323 origname = name = xmalloc (len);
324 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
326 /* Don't enter the symbol twice if the target is re-run. */
328 if (name[0] == bfd_get_symbol_leading_char (objfile->obfd))
333 /* FIXME: Do we really want to exclude symbols which happen
334 to match symbols for other locations in the inferior's
335 address space, even when they are in different linkage units? */
336 if (lookup_minimal_symbol (name, (struct objfile *) NULL) == NULL)
338 name = obsavestring (name, strlen (name),
339 &objfile -> symbol_obstack);
340 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
345 rtc_symp = inferior_rtc_symb.rtc_next;
348 /* Install any minimal symbols that have been collected as the current
349 minimal symbols for this objfile. */
351 install_minimal_symbols (objfile);
354 #endif /* SVR4_SHARED_LIBS */
356 #ifdef SVR4_SHARED_LIBS
362 bfd_lookup_symbol -- lookup the value for a specific symbol
366 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
370 An expensive way to lookup the value of a single symbol for
371 bfd's that are only temporary anyway. This is used by the
372 shared library support to find the address of the debugger
373 interface structures in the shared library.
375 Note that 0 is specifically allowed as an error return (no
378 FIXME: See if there is a less "expensive" way of doing this.
379 Also see if there is already another bfd or gdb function
380 that specifically does this, and if so, use it.
384 bfd_lookup_symbol (abfd, symname)
388 unsigned int storage_needed;
390 asymbol **symbol_table;
391 unsigned int number_of_symbols;
393 struct cleanup *back_to;
394 CORE_ADDR symaddr = 0;
396 storage_needed = get_symtab_upper_bound (abfd);
398 if (storage_needed > 0)
400 symbol_table = (asymbol **) xmalloc (storage_needed);
401 back_to = make_cleanup (free, (PTR)symbol_table);
402 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
404 for (i = 0; i < number_of_symbols; i++)
406 sym = *symbol_table++;
407 if (STREQ (sym -> name, symname))
409 /* Bfd symbols are section relative. */
410 symaddr = sym -> value + sym -> section -> vma;
414 do_cleanups (back_to);
423 look_for_base -- examine file for each mapped address segment
427 static int look_for_base (int fd, CORE_ADDR baseaddr)
431 This function is passed to proc_iterate_over_mappings, which
432 causes it to get called once for each mapped address space, with
433 an open file descriptor for the file mapped to that space, and the
434 base address of that mapped space.
436 Our job is to find the symbol DEBUG_BASE in the file that this
437 fd is open on, if it exists, and if so, initialize the dynamic
438 linker structure base address debug_base.
440 Note that this is a computationally expensive proposition, since
441 we basically have to open a bfd on every call, so we specifically
442 avoid opening the exec file.
446 look_for_base (fd, baseaddr)
453 /* If the fd is -1, then there is no file that corresponds to this
454 mapped memory segment, so skip it. Also, if the fd corresponds
455 to the exec file, skip it as well. */
457 if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd))
462 /* Try to open whatever random file this fd corresponds to. Note that
463 we have no way currently to find the filename. Don't gripe about
464 any problems we might have, just fail. */
466 if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL)
470 if (!bfd_check_format (interp_bfd, bfd_object))
472 bfd_close (interp_bfd);
476 /* Now try to find our DEBUG_BASE symbol in this file, which we at
477 least know to be a valid ELF executable or shared library. */
479 if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0)
481 bfd_close (interp_bfd);
485 /* Eureka! We found the symbol. But now we may need to relocate it
486 by the base address. If the symbol's value is less than the base
487 address of the shared library, then it hasn't yet been relocated
488 by the dynamic linker, and we have to do it ourself. FIXME: Note
489 that we make the assumption that the first segment that corresponds
490 to the shared library has the base address to which the library
493 if (address < baseaddr)
497 debug_base = address;
498 bfd_close (interp_bfd);
508 locate_base -- locate the base address of dynamic linker structs
512 CORE_ADDR locate_base (void)
516 For both the SunOS and SVR4 shared library implementations, if the
517 inferior executable has been linked dynamically, there is a single
518 address somewhere in the inferior's data space which is the key to
519 locating all of the dynamic linker's runtime structures. This
520 address is the value of the symbol defined by the macro DEBUG_BASE.
521 The job of this function is to find and return that address, or to
522 return 0 if there is no such address (the executable is statically
525 For SunOS, the job is almost trivial, since the dynamic linker and
526 all of it's structures are statically linked to the executable at
527 link time. Thus the symbol for the address we are looking for has
528 already been added to the minimal symbol table for the executable's
529 objfile at the time the symbol file's symbols were read, and all we
530 have to do is look it up there. Note that we explicitly do NOT want
531 to find the copies in the shared library.
533 The SVR4 version is much more complicated because the dynamic linker
534 and it's structures are located in the shared C library, which gets
535 run as the executable's "interpreter" by the kernel. We have to go
536 to a lot more work to discover the address of DEBUG_BASE. Because
537 of this complexity, we cache the value we find and return that value
538 on subsequent invocations. Note there is no copy in the executable
541 Note that we can assume nothing about the process state at the time
542 we need to find this address. We may be stopped on the first instruc-
543 tion of the interpreter (C shared library), the first instruction of
544 the executable itself, or somewhere else entirely (if we attached
545 to the process for example).
553 #ifndef SVR4_SHARED_LIBS
555 struct minimal_symbol *msymbol;
556 CORE_ADDR address = 0;
558 /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
559 being debugged, since there is a duplicate named symbol in the shared
560 library. We don't want the shared library versions. */
562 msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
563 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
565 address = SYMBOL_VALUE_ADDRESS (msymbol);
569 #else /* SVR4_SHARED_LIBS */
571 /* Check to see if we have a currently valid address, and if so, avoid
572 doing all this work again and just return the cached address. If
573 we have no cached address, ask the /proc support interface to iterate
574 over the list of mapped address segments, calling look_for_base() for
575 each segment. When we are done, we will have either found the base
580 proc_iterate_over_mappings (look_for_base);
584 #endif /* !SVR4_SHARED_LIBS */
592 first_link_map_member -- locate first member in dynamic linker's map
596 static struct link_map *first_link_map_member (void)
600 Read in a copy of the first member in the inferior's dynamic
601 link map from the inferior's dynamic linker structures, and return
602 a pointer to the copy in our address space.
605 static struct link_map *
606 first_link_map_member ()
608 struct link_map *lm = NULL;
610 #ifndef SVR4_SHARED_LIBS
612 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
613 if (dynamic_copy.ld_version >= 2)
615 /* It is a version that we can deal with, so read in the secondary
616 structure and find the address of the link map list from it. */
617 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
618 sizeof (struct link_dynamic_2));
619 lm = ld_2_copy.ld_loaded;
622 #else /* SVR4_SHARED_LIBS */
624 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
625 /* FIXME: Perhaps we should validate the info somehow, perhaps by
626 checking r_version for a known version number, or r_state for
628 lm = debug_copy.r_map;
630 #endif /* !SVR4_SHARED_LIBS */
639 find_solib -- step through list of shared objects
643 struct so_list *find_solib (struct so_list *so_list_ptr)
647 This module contains the routine which finds the names of any
648 loaded "images" in the current process. The argument in must be
649 NULL on the first call, and then the returned value must be passed
650 in on subsequent calls. This provides the capability to "step" down
651 the list of loaded objects. On the last object, a NULL value is
654 The arg and return value are "struct link_map" pointers, as defined
658 static struct so_list *
659 find_solib (so_list_ptr)
660 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
662 struct so_list *so_list_next = NULL;
663 struct link_map *lm = NULL;
666 if (so_list_ptr == NULL)
668 /* We are setting up for a new scan through the loaded images. */
669 if ((so_list_next = so_list_head) == NULL)
671 /* We have not already read in the dynamic linking structures
672 from the inferior, lookup the address of the base structure. */
673 debug_base = locate_base ();
676 /* Read the base structure in and find the address of the first
677 link map list member. */
678 lm = first_link_map_member ();
684 /* We have been called before, and are in the process of walking
685 the shared library list. Advance to the next shared object. */
686 if ((lm = LM_NEXT (so_list_ptr)) == NULL)
688 /* We have hit the end of the list, so check to see if any were
689 added, but be quiet if we can't read from the target any more. */
690 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
691 (char *) &(so_list_ptr -> lm),
692 sizeof (struct link_map));
695 lm = LM_NEXT (so_list_ptr);
702 so_list_next = so_list_ptr -> next;
704 if ((so_list_next == NULL) && (lm != NULL))
706 /* Get next link map structure from inferior image and build a local
707 abbreviated load_map structure */
708 new = (struct so_list *) xmalloc (sizeof (struct so_list));
709 memset ((char *) new, 0, sizeof (struct so_list));
711 /* Add the new node as the next node in the list, or as the root
712 node if this is the first one. */
713 if (so_list_ptr != NULL)
715 so_list_ptr -> next = new;
722 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
723 sizeof (struct link_map));
724 /* For the SVR4 version, there is one entry that has no name
725 (for the inferior executable) since it is not a shared object. */
726 if (LM_NAME (new) != 0)
728 if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
730 error ("find_solib: Can't read pathname for load map\n");
731 new -> so_name[MAX_PATH_SIZE - 1] = 0;
732 solib_map_sections (new);
735 return (so_list_next);
738 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
741 symbol_add_stub (arg)
744 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
746 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
747 (unsigned int) so -> textsection -> addr,
756 solib_add -- add a shared library file to the symtab and section list
760 void solib_add (char *arg_string, int from_tty,
761 struct target_ops *target)
768 solib_add (arg_string, from_tty, target)
771 struct target_ops *target;
773 register struct so_list *so = NULL; /* link map state variable */
778 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
780 error ("Invalid regexp: %s", re_err);
783 /* Getting new symbols may change our opinion about what is
785 reinit_frame_cache ();
787 while ((so = find_solib (so)) != NULL)
789 if (so -> so_name[0] && re_exec (so -> so_name))
791 so -> from_tty = from_tty;
792 if (so -> symbols_loaded)
796 printf ("Symbols already loaded for %s\n", so -> so_name);
799 else if (catch_errors
800 (symbol_add_stub, (char *) so,
801 "Error while reading shared library symbols:\n",
804 special_symbol_handling (so);
805 so -> symbols_loaded = 1;
810 /* Now add the shared library sections to the section table of the
811 specified target, if any. */
814 /* Count how many new section_table entries there are. */
817 while ((so = find_solib (so)) != NULL)
819 if (so -> so_name[0])
821 count += so -> sections_end - so -> sections;
827 /* Reallocate the target's section table including the new size. */
828 if (target -> to_sections)
830 old = target -> to_sections_end - target -> to_sections;
831 target -> to_sections = (struct section_table *)
832 realloc ((char *)target -> to_sections,
833 (sizeof (struct section_table)) * (count + old));
838 target -> to_sections = (struct section_table *)
839 malloc ((sizeof (struct section_table)) * count);
841 target -> to_sections_end = target -> to_sections + (count + old);
843 /* Add these section table entries to the target's table. */
844 while ((so = find_solib (so)) != NULL)
846 if (so -> so_name[0])
848 count = so -> sections_end - so -> sections;
849 memcpy ((char *) (target -> to_sections + old),
851 (sizeof (struct section_table)) * count);
863 info_sharedlibrary_command -- code for "info sharedlibrary"
867 static void info_sharedlibrary_command ()
871 Walk through the shared library list and print information
872 about each attached library.
876 info_sharedlibrary_command (ignore, from_tty)
880 register struct so_list *so = NULL; /* link map state variable */
883 if (exec_bfd == NULL)
885 printf ("No exec file.\n");
888 while ((so = find_solib (so)) != NULL)
890 if (so -> so_name[0])
894 printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
895 "Shared Object Library");
898 printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so), "08"));
899 printf ("%-12s", local_hex_string_custom (so -> lmend, "08"));
900 printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
901 printf ("%s\n", so -> so_name);
904 if (so_list_head == NULL)
906 printf ("No shared libraries loaded at this time.\n");
914 solib_address -- check to see if an address is in a shared lib
918 int solib_address (CORE_ADDR address)
922 Provides a hook for other gdb routines to discover whether or
923 not a particular address is within the mapped address space of
924 a shared library. Any address between the base mapping address
925 and the first address beyond the end of the last mapping, is
926 considered to be within the shared library address space, for
929 For example, this routine is called at one point to disable
930 breakpoints which are in shared libraries that are not currently
935 solib_address (address)
938 register struct so_list *so = 0; /* link map state variable */
940 while ((so = find_solib (so)) != NULL)
942 if (so -> so_name[0])
944 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
945 (address < (CORE_ADDR) so -> lmend))
954 /* Called by free_all_symtabs */
959 struct so_list *next;
964 if (so_list_head -> sections)
966 free ((PTR)so_list_head -> sections);
968 if (so_list_head -> bfd)
970 bfd_filename = bfd_get_filename (so_list_head -> bfd);
971 bfd_close (so_list_head -> bfd);
974 /* This happens for the executable on SVR4. */
977 next = so_list_head -> next;
979 free ((PTR)bfd_filename);
980 free ((PTR)so_list_head);
990 disable_break -- remove the "mapping changed" breakpoint
994 static int disable_break ()
998 Removes the breakpoint that gets hit when the dynamic linker
999 completes a mapping change.
1008 #ifndef SVR4_SHARED_LIBS
1010 int in_debugger = 0;
1012 /* Read the debugger structure from the inferior to retrieve the
1013 address of the breakpoint and the original contents of the
1014 breakpoint address. Remove the breakpoint by writing the original
1017 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1019 /* Set `in_debugger' to zero now. */
1021 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1023 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
1024 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1025 sizeof (debug_copy.ldd_bp_inst));
1027 #else /* SVR4_SHARED_LIBS */
1029 /* Note that breakpoint address and original contents are in our address
1030 space, so we just need to write the original contents back. */
1032 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1037 #endif /* !SVR4_SHARED_LIBS */
1039 /* For the SVR4 version, we always know the breakpoint address. For the
1040 SunOS version we don't know it until the above code is executed.
1041 Grumble if we are stopped anywhere besides the breakpoint address. */
1043 if (stop_pc != breakpoint_addr)
1045 warning ("stopped at unknown breakpoint while handling shared libraries");
1055 enable_break -- arrange for dynamic linker to hit breakpoint
1059 int enable_break (void)
1063 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1064 debugger interface, support for arranging for the inferior to hit
1065 a breakpoint after mapping in the shared libraries. This function
1066 enables that breakpoint.
1068 For SunOS, there is a special flag location (in_debugger) which we
1069 set to 1. When the dynamic linker sees this flag set, it will set
1070 a breakpoint at a location known only to itself, after saving the
1071 original contents of that place and the breakpoint address itself,
1072 in it's own internal structures. When we resume the inferior, it
1073 will eventually take a SIGTRAP when it runs into the breakpoint.
1074 We handle this (in a different place) by restoring the contents of
1075 the breakpointed location (which is only known after it stops),
1076 chasing around to locate the shared libraries that have been
1077 loaded, then resuming.
1079 For SVR4, the debugger interface structure contains a member (r_brk)
1080 which is statically initialized at the time the shared library is
1081 built, to the offset of a function (_r_debug_state) which is guaran-
1082 teed to be called once before mapping in a library, and again when
1083 the mapping is complete. At the time we are examining this member,
1084 it contains only the unrelocated offset of the function, so we have
1085 to do our own relocation. Later, when the dynamic linker actually
1086 runs, it relocates r_brk to be the actual address of _r_debug_state().
1088 The debugger interface structure also contains an enumeration which
1089 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1090 depending upon whether or not the library is being mapped or unmapped,
1091 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1099 #ifndef SVR4_SHARED_LIBS
1104 /* Get link_dynamic structure */
1106 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1107 sizeof (dynamic_copy));
1114 /* Calc address of debugger interface structure */
1116 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1118 /* Calc address of `in_debugger' member of debugger interface structure */
1120 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1121 (char *) &debug_copy);
1123 /* Write a value of 1 to this member. */
1126 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1129 #else /* SVR4_SHARED_LIBS */
1131 #ifdef BKPT_AT_SYMBOL
1133 struct minimal_symbol *msymbol;
1135 CORE_ADDR bkpt_addr;
1137 /* Scan through the list of symbols, trying to look up the symbol and
1138 set a breakpoint there. Terminate loop when we/if we succeed. */
1140 breakpoint_addr = 0;
1141 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1143 msymbol = lookup_minimal_symbol (*bkpt_namep, symfile_objfile);
1144 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1146 bkpt_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1147 if (target_insert_breakpoint (bkpt_addr, shadow_contents) == 0)
1149 breakpoint_addr = bkpt_addr;
1156 #else /* !BKPT_AT_SYMBOL */
1158 struct symtab_and_line sal;
1160 /* Read the debugger interface structure directly. */
1162 read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
1164 /* Set breakpoint at the debugger interface stub routine that will
1165 be called just prior to each mapping change and again after the
1166 mapping change is complete. Set up the (nonexistent) handler to
1167 deal with hitting these breakpoints. (FIXME). */
1169 warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
1172 #endif /* BKPT_AT_SYMBOL */
1174 #endif /* !SVR4_SHARED_LIBS */
1183 solib_create_inferior_hook -- shared library startup support
1187 void solib_create_inferior_hook()
1191 When gdb starts up the inferior, it nurses it along (through the
1192 shell) until it is ready to execute it's first instruction. At this
1193 point, this function gets called via expansion of the macro
1194 SOLIB_CREATE_INFERIOR_HOOK.
1196 For SunOS executables, this first instruction is typically the
1197 one at "_start", or a similar text label, regardless of whether
1198 the executable is statically or dynamically linked. The runtime
1199 startup code takes care of dynamically linking in any shared
1200 libraries, once gdb allows the inferior to continue.
1202 For SVR4 executables, this first instruction is either the first
1203 instruction in the dynamic linker (for dynamically linked
1204 executables) or the instruction at "start" for statically linked
1205 executables. For dynamically linked executables, the system
1206 first exec's /lib/libc.so.N, which contains the dynamic linker,
1207 and starts it running. The dynamic linker maps in any needed
1208 shared libraries, maps in the actual user executable, and then
1209 jumps to "start" in the user executable.
1211 For both SunOS shared libraries, and SVR4 shared libraries, we
1212 can arrange to cooperate with the dynamic linker to discover the
1213 names of shared libraries that are dynamically linked, and the
1214 base addresses to which they are linked.
1216 This function is responsible for discovering those names and
1217 addresses, and saving sufficient information about them to allow
1218 their symbols to be read at a later time.
1222 Between enable_break() and disable_break(), this code does not
1223 properly handle hitting breakpoints which the user might have
1224 set in the startup code or in the dynamic linker itself. Proper
1225 handling will probably have to wait until the implementation is
1226 changed to use the "breakpoint handler function" method.
1228 Also, what if child has exit()ed? Must exit loop somehow.
1232 solib_create_inferior_hook()
1234 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1235 yet. In fact, in the case of a SunOS4 executable being run on
1236 Solaris, we can't get it yet. find_solib will get it when it needs
1238 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1239 if ((debug_base = locate_base ()) == 0)
1241 /* Can't find the symbol or the executable is statically linked. */
1246 if (!enable_break ())
1248 warning ("shared library handler failed to enable breakpoint");
1252 /* Now run the target. It will eventually hit the breakpoint, at
1253 which point all of the libraries will have been mapped in and we
1254 can go groveling around in the dynamic linker structures to find
1255 out what we need to know about them. */
1257 clear_proceed_status ();
1258 stop_soon_quietly = 1;
1262 target_resume (0, stop_signal);
1263 wait_for_inferior ();
1265 while (stop_signal != SIGTRAP);
1266 stop_soon_quietly = 0;
1268 /* We are now either at the "mapping complete" breakpoint (or somewhere
1269 else, a condition we aren't prepared to deal with anyway), so adjust
1270 the PC as necessary after a breakpoint, disable the breakpoint, and
1271 add any shared libraries that were mapped in. */
1273 if (DECR_PC_AFTER_BREAK)
1275 stop_pc -= DECR_PC_AFTER_BREAK;
1276 write_register (PC_REGNUM, stop_pc);
1279 if (!disable_break ())
1281 warning ("shared library handler failed to disable breakpoint");
1284 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1291 special_symbol_handling -- additional shared library symbol handling
1295 void special_symbol_handling (struct so_list *so)
1299 Once the symbols from a shared object have been loaded in the usual
1300 way, we are called to do any system specific symbol handling that
1303 For Suns, this consists of grunging around in the dynamic linkers
1304 structures to find symbol definitions for "common" symbols and
1305 adding them to the minimal symbol table for the corresponding
1311 special_symbol_handling (so)
1314 #ifndef SVR4_SHARED_LIBS
1317 if (debug_addr == 0)
1319 /* Get link_dynamic structure */
1321 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1322 sizeof (dynamic_copy));
1329 /* Calc address of debugger interface structure */
1330 /* FIXME, this needs work for cross-debugging of core files
1331 (byteorder, size, alignment, etc). */
1333 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1336 /* Read the debugger structure from the inferior, just to make sure
1337 we have a current copy. */
1339 j = target_read_memory (debug_addr, (char *) &debug_copy,
1340 sizeof (debug_copy));
1342 return; /* unreadable */
1344 /* Get common symbol definitions for the loaded object. */
1346 if (debug_copy.ldd_cp)
1348 solib_add_common_symbols (debug_copy.ldd_cp, so -> objfile);
1351 #endif /* !SVR4_SHARED_LIBS */
1359 sharedlibrary_command -- handle command to explicitly add library
1363 static void sharedlibrary_command (char *args, int from_tty)
1370 sharedlibrary_command (args, from_tty)
1375 solib_add (args, from_tty, (struct target_ops *) 0);
1382 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1383 "Load shared object library symbols for files matching REGEXP.");
1384 add_info ("sharedlibrary", info_sharedlibrary_command,
1385 "Status of loaded shared object libraries.");