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. */
47 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
49 /* On SVR4 systems, for the initial implementation, use some runtime startup
50 symbol as the "startup mapping complete" breakpoint address. The models
51 for SunOS and SVR4 dynamic linking debugger support are different in that
52 SunOS hits one breakpoint when all mapping is complete while using the SVR4
53 debugger support takes two breakpoint hits for each file mapped, and
54 there is no way to know when the "last" one is hit. Both these
55 mechanisms should be tied to a "breakpoint service routine" that
56 gets automatically executed whenever one of the breakpoints indicating
57 a change in mapping is hit. This is a future enhancement. (FIXME) */
59 #define BKPT_AT_SYMBOL 1
61 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
62 static char *bkpt_names
[] = {
63 #ifdef SOLIB_BKPT_NAME
64 SOLIB_BKPT_NAME
, /* Prefer configured name if it exists. */
72 /* Symbols which are used to locate the base of the link map structures. */
74 static char *debug_base_symbols
[] = {
75 #ifdef SVR4_SHARED_LIBS
76 "_r_debug", /* Most SVR4 systems, Solaris 2.1, 2.2 */
77 "r_debug", /* Solaris 2.3 */
79 "_DYNAMIC", /* SunOS */
84 /* local data declarations */
86 #ifndef SVR4_SHARED_LIBS
88 #define LM_ADDR(so) ((so) -> lm.lm_addr)
89 #define LM_NEXT(so) ((so) -> lm.lm_next)
90 #define LM_NAME(so) ((so) -> lm.lm_name)
91 /* Test for first link map entry; first entry is a shared library. */
92 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
93 static struct link_dynamic dynamic_copy
;
94 static struct link_dynamic_2 ld_2_copy
;
95 static struct ld_debug debug_copy
;
96 static CORE_ADDR debug_addr
;
97 static CORE_ADDR flag_addr
;
99 #else /* SVR4_SHARED_LIBS */
101 #define LM_ADDR(so) ((so) -> lm.l_addr)
102 #define LM_NEXT(so) ((so) -> lm.l_next)
103 #define LM_NAME(so) ((so) -> lm.l_name)
104 /* Test for first link map entry; first entry is the exec-file. */
105 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
106 static struct r_debug debug_copy
;
107 char shadow_contents
[BREAKPOINT_MAX
]; /* Stash old bkpt addr contents */
109 #endif /* !SVR4_SHARED_LIBS */
112 struct so_list
*next
; /* next structure in linked list */
113 struct link_map lm
; /* copy of link map from inferior */
114 struct link_map
*lmaddr
; /* addr in inferior lm was read from */
115 CORE_ADDR lmend
; /* upper addr bound of mapped object */
116 char so_name
[MAX_PATH_SIZE
]; /* shared object lib name (FIXME) */
117 char symbols_loaded
; /* flag: symbols read in yet? */
118 char from_tty
; /* flag: print msgs? */
119 struct objfile
*objfile
; /* objfile for loaded lib */
120 struct section_table
*sections
;
121 struct section_table
*sections_end
;
122 struct section_table
*textsection
;
126 static struct so_list
*so_list_head
; /* List of known shared objects */
127 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
128 static CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
131 fdmatch
PARAMS ((int, int)); /* In libiberty */
133 /* Local function prototypes */
136 special_symbol_handling
PARAMS ((struct so_list
*));
139 sharedlibrary_command
PARAMS ((char *, int));
142 enable_break
PARAMS ((void));
145 disable_break
PARAMS ((void));
148 info_sharedlibrary_command
PARAMS ((char *, int));
151 symbol_add_stub
PARAMS ((char *));
153 static struct so_list
*
154 find_solib
PARAMS ((struct so_list
*));
156 static struct link_map
*
157 first_link_map_member
PARAMS ((void));
160 locate_base
PARAMS ((void));
163 solib_map_sections
PARAMS ((struct so_list
*));
165 #ifdef SVR4_SHARED_LIBS
168 look_for_base
PARAMS ((int, CORE_ADDR
));
171 bfd_lookup_symbol
PARAMS ((bfd
*, char *));
176 solib_add_common_symbols
PARAMS ((struct rtc_symb
*, struct objfile
*));
184 solib_map_sections -- open bfd and build sections for shared lib
188 static void solib_map_sections (struct so_list *so)
192 Given a pointer to one of the shared objects in our list
193 of mapped objects, use the recorded name to open a bfd
194 descriptor for the object, build a section table, and then
195 relocate all the section addresses by the base address at
196 which the shared object was mapped.
200 In most (all?) cases the shared object file name recorded in the
201 dynamic linkage tables will be a fully qualified pathname. For
202 cases where it isn't, do we really mimic the systems search
203 mechanism correctly in the below code (particularly the tilde
208 solib_map_sections (so
)
212 char *scratch_pathname
;
214 struct section_table
*p
;
215 struct cleanup
*old_chain
;
218 filename
= tilde_expand (so
-> so_name
);
219 old_chain
= make_cleanup (free
, filename
);
221 scratch_chan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
223 if (scratch_chan
< 0)
225 scratch_chan
= openp (getenv ("LD_LIBRARY_PATH"), 1, filename
,
226 O_RDONLY
, 0, &scratch_pathname
);
228 if (scratch_chan
< 0)
230 perror_with_name (filename
);
232 /* Leave scratch_pathname allocated. abfd->name will point to it. */
234 abfd
= bfd_fdopenr (scratch_pathname
, gnutarget
, scratch_chan
);
237 close (scratch_chan
);
238 error ("Could not open `%s' as an executable file: %s",
239 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
241 /* Leave bfd open, core_xfer_memory and "info files" need it. */
243 abfd
-> cacheable
= true;
245 if (!bfd_check_format (abfd
, bfd_object
))
247 error ("\"%s\": not in executable format: %s.",
248 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
250 if (build_section_table (abfd
, &so
-> sections
, &so
-> sections_end
))
252 error ("Can't find the file sections in `%s': %s",
253 bfd_get_filename (exec_bfd
), bfd_errmsg (bfd_get_error ()));
256 for (p
= so
-> sections
; p
< so
-> sections_end
; p
++)
258 /* Relocate the section binding addresses as recorded in the shared
259 object's file by the base address to which the object was actually
261 p
-> addr
+= (CORE_ADDR
) LM_ADDR (so
);
262 p
-> endaddr
+= (CORE_ADDR
) LM_ADDR (so
);
263 so
-> lmend
= (CORE_ADDR
) max (p
-> endaddr
, so
-> lmend
);
264 if (STREQ (p
-> sec_ptr
-> name
, ".text"))
266 so
-> textsection
= p
;
270 /* Free the file names, close the file now. */
271 do_cleanups (old_chain
);
274 /* Read all dynamically loaded common symbol definitions from the inferior
275 and add them to the minimal symbol table for the shared library objfile. */
277 #ifndef SVR4_SHARED_LIBS
279 /* In GDB 4.9 this routine was a real performance hog. According to
280 some gprof data which mtranle@paris.IntelliCorp.COM (Minh Tran-Le)
281 sent, almost all the time spend in solib_add (up to 20 minutes with
282 35 shared libraries) was spent here, with 5/6 in
283 lookup_minimal_symbol and 1/6 in read_memory.
285 To fix this, we moved the call to special_symbol_handling out of the
286 loop in solib_add, so this only gets called once, rather than once
287 for every shared library, and also removed the call to lookup_minimal_symbol
291 solib_add_common_symbols (rtc_symp
, objfile
)
292 struct rtc_symb
*rtc_symp
;
293 struct objfile
*objfile
;
295 struct rtc_symb inferior_rtc_symb
;
296 struct nlist inferior_rtc_nlist
;
301 init_minimal_symbol_collection ();
302 make_cleanup (discard_minimal_symbols
, 0);
306 read_memory ((CORE_ADDR
) rtc_symp
,
307 (char *) &inferior_rtc_symb
,
308 sizeof (inferior_rtc_symb
));
309 read_memory ((CORE_ADDR
) inferior_rtc_symb
.rtc_sp
,
310 (char *) &inferior_rtc_nlist
,
311 sizeof(inferior_rtc_nlist
));
312 if (inferior_rtc_nlist
.n_type
== N_COMM
)
314 /* FIXME: The length of the symbol name is not available, but in the
315 current implementation the common symbol is allocated immediately
316 behind the name of the symbol. */
317 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
319 origname
= name
= xmalloc (len
);
320 read_memory ((CORE_ADDR
) inferior_rtc_nlist
.n_un
.n_name
, name
, len
);
322 /* Don't enter the symbol twice if the target is re-run. */
324 if (name
[0] == bfd_get_symbol_leading_char (objfile
->obfd
))
330 /* I think this is unnecessary, GDB can probably deal with
331 duplicate minimal symbols, more or less. And the duplication
332 which used to happen because this was called for each shared
333 library is gone now that we are just called once. */
334 /* FIXME: Do we really want to exclude symbols which happen
335 to match symbols for other locations in the inferior's
336 address space, even when they are in different linkage units? */
337 if (lookup_minimal_symbol (name
, (struct objfile
*) NULL
) == NULL
)
340 name
= obsavestring (name
, strlen (name
),
341 &objfile
-> symbol_obstack
);
342 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
347 rtc_symp
= inferior_rtc_symb
.rtc_next
;
350 /* Install any minimal symbols that have been collected as the current
351 minimal symbols for this objfile. */
353 install_minimal_symbols (objfile
);
356 #endif /* SVR4_SHARED_LIBS */
358 #ifdef SVR4_SHARED_LIBS
364 bfd_lookup_symbol -- lookup the value for a specific symbol
368 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
372 An expensive way to lookup the value of a single symbol for
373 bfd's that are only temporary anyway. This is used by the
374 shared library support to find the address of the debugger
375 interface structures in the shared library.
377 Note that 0 is specifically allowed as an error return (no
380 FIXME: See if there is a less "expensive" way of doing this.
381 Also see if there is already another bfd or gdb function
382 that specifically does this, and if so, use it.
386 bfd_lookup_symbol (abfd
, symname
)
390 unsigned int storage_needed
;
392 asymbol
**symbol_table
;
393 unsigned int number_of_symbols
;
395 struct cleanup
*back_to
;
396 CORE_ADDR symaddr
= 0;
398 storage_needed
= get_symtab_upper_bound (abfd
);
400 if (storage_needed
> 0)
402 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
403 back_to
= make_cleanup (free
, (PTR
)symbol_table
);
404 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
406 for (i
= 0; i
< number_of_symbols
; i
++)
408 sym
= *symbol_table
++;
409 if (STREQ (sym
-> name
, symname
))
411 /* Bfd symbols are section relative. */
412 symaddr
= sym
-> value
+ sym
-> section
-> vma
;
416 do_cleanups (back_to
);
425 look_for_base -- examine file for each mapped address segment
429 static int look_for_base (int fd, CORE_ADDR baseaddr)
433 This function is passed to proc_iterate_over_mappings, which
434 causes it to get called once for each mapped address space, with
435 an open file descriptor for the file mapped to that space, and the
436 base address of that mapped space.
438 Our job is to find the debug base symbol in the file that this
439 fd is open on, if it exists, and if so, initialize the dynamic
440 linker structure base address debug_base.
442 Note that this is a computationally expensive proposition, since
443 we basically have to open a bfd on every call, so we specifically
444 avoid opening the exec file.
448 look_for_base (fd
, baseaddr
)
456 /* If the fd is -1, then there is no file that corresponds to this
457 mapped memory segment, so skip it. Also, if the fd corresponds
458 to the exec file, skip it as well. */
460 if ((fd
== -1) || fdmatch (fileno ((GDB_FILE
*)(exec_bfd
-> iostream
)), fd
))
465 /* Try to open whatever random file this fd corresponds to. Note that
466 we have no way currently to find the filename. Don't gripe about
467 any problems we might have, just fail. */
469 if ((interp_bfd
= bfd_fdopenr ("unnamed", gnutarget
, fd
)) == NULL
)
473 if (!bfd_check_format (interp_bfd
, bfd_object
))
475 bfd_close (interp_bfd
);
479 /* Now try to find our debug base symbol in this file, which we at
480 least know to be a valid ELF executable or shared library. */
482 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
484 address
= bfd_lookup_symbol (interp_bfd
, *symbolp
);
492 bfd_close (interp_bfd
);
496 /* Eureka! We found the symbol. But now we may need to relocate it
497 by the base address. If the symbol's value is less than the base
498 address of the shared library, then it hasn't yet been relocated
499 by the dynamic linker, and we have to do it ourself. FIXME: Note
500 that we make the assumption that the first segment that corresponds
501 to the shared library has the base address to which the library
504 if (address
< baseaddr
)
508 debug_base
= address
;
509 bfd_close (interp_bfd
);
519 locate_base -- locate the base address of dynamic linker structs
523 CORE_ADDR locate_base (void)
527 For both the SunOS and SVR4 shared library implementations, if the
528 inferior executable has been linked dynamically, there is a single
529 address somewhere in the inferior's data space which is the key to
530 locating all of the dynamic linker's runtime structures. This
531 address is the value of the debug base symbol. The job of this
532 function is to find and return that address, or to return 0 if there
533 is no such address (the executable is statically linked for example).
535 For SunOS, the job is almost trivial, since the dynamic linker and
536 all of it's structures are statically linked to the executable at
537 link time. Thus the symbol for the address we are looking for has
538 already been added to the minimal symbol table for the executable's
539 objfile at the time the symbol file's symbols were read, and all we
540 have to do is look it up there. Note that we explicitly do NOT want
541 to find the copies in the shared library.
543 The SVR4 version is much more complicated because the dynamic linker
544 and it's structures are located in the shared C library, which gets
545 run as the executable's "interpreter" by the kernel. We have to go
546 to a lot more work to discover the address of the debug base symbol.
547 Because of this complexity, we cache the value we find and return that
548 value on subsequent invocations. Note there is no copy in the
549 executable symbol tables.
551 Note that we can assume nothing about the process state at the time
552 we need to find this address. We may be stopped on the first instruc-
553 tion of the interpreter (C shared library), the first instruction of
554 the executable itself, or somewhere else entirely (if we attached
555 to the process for example).
563 #ifndef SVR4_SHARED_LIBS
565 struct minimal_symbol
*msymbol
;
566 CORE_ADDR address
= 0;
569 /* For SunOS, we want to limit the search for the debug base symbol to the
570 executable being debugged, since there is a duplicate named symbol in the
571 shared library. We don't want the shared library versions. */
573 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
575 msymbol
= lookup_minimal_symbol (*symbolp
, symfile_objfile
);
576 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
578 address
= SYMBOL_VALUE_ADDRESS (msymbol
);
584 #else /* SVR4_SHARED_LIBS */
586 /* Check to see if we have a currently valid address, and if so, avoid
587 doing all this work again and just return the cached address. If
588 we have no cached address, ask the /proc support interface to iterate
589 over the list of mapped address segments, calling look_for_base() for
590 each segment. When we are done, we will have either found the base
595 proc_iterate_over_mappings (look_for_base
);
599 #endif /* !SVR4_SHARED_LIBS */
607 first_link_map_member -- locate first member in dynamic linker's map
611 static struct link_map *first_link_map_member (void)
615 Read in a copy of the first member in the inferior's dynamic
616 link map from the inferior's dynamic linker structures, and return
617 a pointer to the copy in our address space.
620 static struct link_map
*
621 first_link_map_member ()
623 struct link_map
*lm
= NULL
;
625 #ifndef SVR4_SHARED_LIBS
627 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
628 if (dynamic_copy
.ld_version
>= 2)
630 /* It is a version that we can deal with, so read in the secondary
631 structure and find the address of the link map list from it. */
632 read_memory ((CORE_ADDR
) dynamic_copy
.ld_un
.ld_2
, (char *) &ld_2_copy
,
633 sizeof (struct link_dynamic_2
));
634 lm
= ld_2_copy
.ld_loaded
;
637 #else /* SVR4_SHARED_LIBS */
639 read_memory (debug_base
, (char *) &debug_copy
, sizeof (struct r_debug
));
640 /* FIXME: Perhaps we should validate the info somehow, perhaps by
641 checking r_version for a known version number, or r_state for
643 lm
= debug_copy
.r_map
;
645 #endif /* !SVR4_SHARED_LIBS */
654 find_solib -- step through list of shared objects
658 struct so_list *find_solib (struct so_list *so_list_ptr)
662 This module contains the routine which finds the names of any
663 loaded "images" in the current process. The argument in must be
664 NULL on the first call, and then the returned value must be passed
665 in on subsequent calls. This provides the capability to "step" down
666 the list of loaded objects. On the last object, a NULL value is
669 The arg and return value are "struct link_map" pointers, as defined
673 static struct so_list
*
674 find_solib (so_list_ptr
)
675 struct so_list
*so_list_ptr
; /* Last lm or NULL for first one */
677 struct so_list
*so_list_next
= NULL
;
678 struct link_map
*lm
= NULL
;
681 if (so_list_ptr
== NULL
)
683 /* We are setting up for a new scan through the loaded images. */
684 if ((so_list_next
= so_list_head
) == NULL
)
686 /* We have not already read in the dynamic linking structures
687 from the inferior, lookup the address of the base structure. */
688 debug_base
= locate_base ();
691 /* Read the base structure in and find the address of the first
692 link map list member. */
693 lm
= first_link_map_member ();
699 /* We have been called before, and are in the process of walking
700 the shared library list. Advance to the next shared object. */
701 if ((lm
= LM_NEXT (so_list_ptr
)) == NULL
)
703 /* We have hit the end of the list, so check to see if any were
704 added, but be quiet if we can't read from the target any more. */
705 int status
= target_read_memory ((CORE_ADDR
) so_list_ptr
-> lmaddr
,
706 (char *) &(so_list_ptr
-> lm
),
707 sizeof (struct link_map
));
710 lm
= LM_NEXT (so_list_ptr
);
717 so_list_next
= so_list_ptr
-> next
;
719 if ((so_list_next
== NULL
) && (lm
!= NULL
))
721 /* Get next link map structure from inferior image and build a local
722 abbreviated load_map structure */
723 new = (struct so_list
*) xmalloc (sizeof (struct so_list
));
724 memset ((char *) new, 0, sizeof (struct so_list
));
726 /* Add the new node as the next node in the list, or as the root
727 node if this is the first one. */
728 if (so_list_ptr
!= NULL
)
730 so_list_ptr
-> next
= new;
737 read_memory ((CORE_ADDR
) lm
, (char *) &(new -> lm
),
738 sizeof (struct link_map
));
739 /* For SVR4 versions, the first entry in the link map is for the
740 inferior executable, so we must ignore it. For some versions of
741 SVR4, it has no name. For others (Solaris 2.3 for example), it
742 does have a name, so we can no longer use a missing name to
743 decide when to ignore it. */
744 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm
))
748 target_read_string ((CORE_ADDR
) LM_NAME (new), &buffer
,
749 MAX_PATH_SIZE
- 1, &errcode
);
751 error ("find_solib: Can't read pathname for load map: %s\n",
752 safe_strerror (errcode
));
753 strncpy (new -> so_name
, buffer
, MAX_PATH_SIZE
- 1);
754 new -> so_name
[MAX_PATH_SIZE
- 1] = '\0';
756 solib_map_sections (new);
759 return (so_list_next
);
762 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
765 symbol_add_stub (arg
)
768 register struct so_list
*so
= (struct so_list
*) arg
; /* catch_errs bogon */
770 so
-> objfile
= symbol_file_add (so
-> so_name
, so
-> from_tty
,
771 (unsigned int) so
-> textsection
-> addr
,
780 solib_add -- add a shared library file to the symtab and section list
784 void solib_add (char *arg_string, int from_tty,
785 struct target_ops *target)
792 solib_add (arg_string
, from_tty
, target
)
795 struct target_ops
*target
;
797 register struct so_list
*so
= NULL
; /* link map state variable */
799 /* Last shared library that we read. */
800 struct so_list
*so_last
= NULL
;
806 if ((re_err
= re_comp (arg_string
? arg_string
: ".")) != NULL
)
808 error ("Invalid regexp: %s", re_err
);
811 /* Getting new symbols may change our opinion about what is
813 reinit_frame_cache ();
815 while ((so
= find_solib (so
)) != NULL
)
817 if (so
-> so_name
[0] && re_exec (so
-> so_name
))
819 so
-> from_tty
= from_tty
;
820 if (so
-> symbols_loaded
)
824 printf_unfiltered ("Symbols already loaded for %s\n", so
-> so_name
);
827 else if (catch_errors
828 (symbol_add_stub
, (char *) so
,
829 "Error while reading shared library symbols:\n",
833 so
-> symbols_loaded
= 1;
838 /* Now add the shared library sections to the section table of the
839 specified target, if any. */
842 /* Count how many new section_table entries there are. */
845 while ((so
= find_solib (so
)) != NULL
)
847 if (so
-> so_name
[0])
849 count
+= so
-> sections_end
- so
-> sections
;
855 /* Reallocate the target's section table including the new size. */
856 if (target
-> to_sections
)
858 old
= target
-> to_sections_end
- target
-> to_sections
;
859 target
-> to_sections
= (struct section_table
*)
860 xrealloc ((char *)target
-> to_sections
,
861 (sizeof (struct section_table
)) * (count
+ old
));
866 target
-> to_sections
= (struct section_table
*)
867 xmalloc ((sizeof (struct section_table
)) * count
);
869 target
-> to_sections_end
= target
-> to_sections
+ (count
+ old
);
871 /* Add these section table entries to the target's table. */
872 while ((so
= find_solib (so
)) != NULL
)
874 if (so
-> so_name
[0])
876 count
= so
-> sections_end
- so
-> sections
;
877 memcpy ((char *) (target
-> to_sections
+ old
),
879 (sizeof (struct section_table
)) * count
);
886 /* Calling this once at the end means that we put all the minimal
887 symbols for commons into the objfile for the last shared library.
888 Since they are in common, this should not be a problem. If we
889 delete the objfile with the minimal symbols, we can put all the
890 symbols into a new objfile (and will on the next call to solib_add).
892 An alternate approach would be to create an objfile just for
893 common minsyms, thus not needing any objfile argument to
894 solib_add_common_symbols. */
897 special_symbol_handling (so_last
);
904 info_sharedlibrary_command -- code for "info sharedlibrary"
908 static void info_sharedlibrary_command ()
912 Walk through the shared library list and print information
913 about each attached library.
917 info_sharedlibrary_command (ignore
, from_tty
)
921 register struct so_list
*so
= NULL
; /* link map state variable */
924 if (exec_bfd
== NULL
)
926 printf_unfiltered ("No exec file.\n");
929 while ((so
= find_solib (so
)) != NULL
)
931 if (so
-> so_name
[0])
935 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
936 "Shared Object Library");
939 /* FIXME-32x64: need print_address_numeric with field width or
941 printf_unfiltered ("%-12s",
942 local_hex_string_custom ((unsigned long) LM_ADDR (so
),
944 printf_unfiltered ("%-12s",
945 local_hex_string_custom ((unsigned long) so
-> lmend
,
947 printf_unfiltered ("%-12s", so
-> symbols_loaded
? "Yes" : "No");
948 printf_unfiltered ("%s\n", so
-> so_name
);
951 if (so_list_head
== NULL
)
953 printf_unfiltered ("No shared libraries loaded at this time.\n");
961 solib_address -- check to see if an address is in a shared lib
965 int solib_address (CORE_ADDR address)
969 Provides a hook for other gdb routines to discover whether or
970 not a particular address is within the mapped address space of
971 a shared library. Any address between the base mapping address
972 and the first address beyond the end of the last mapping, is
973 considered to be within the shared library address space, for
976 For example, this routine is called at one point to disable
977 breakpoints which are in shared libraries that are not currently
982 solib_address (address
)
985 register struct so_list
*so
= 0; /* link map state variable */
987 while ((so
= find_solib (so
)) != NULL
)
989 if (so
-> so_name
[0])
991 if ((address
>= (CORE_ADDR
) LM_ADDR (so
)) &&
992 (address
< (CORE_ADDR
) so
-> lmend
))
1001 /* Called by free_all_symtabs */
1006 struct so_list
*next
;
1009 while (so_list_head
)
1011 if (so_list_head
-> sections
)
1013 free ((PTR
)so_list_head
-> sections
);
1015 if (so_list_head
-> abfd
)
1017 bfd_filename
= bfd_get_filename (so_list_head
-> abfd
);
1018 bfd_close (so_list_head
-> abfd
);
1021 /* This happens for the executable on SVR4. */
1022 bfd_filename
= NULL
;
1024 next
= so_list_head
-> next
;
1026 free ((PTR
)bfd_filename
);
1027 free ((PTR
)so_list_head
);
1028 so_list_head
= next
;
1037 disable_break -- remove the "mapping changed" breakpoint
1041 static int disable_break ()
1045 Removes the breakpoint that gets hit when the dynamic linker
1046 completes a mapping change.
1055 #ifndef SVR4_SHARED_LIBS
1057 int in_debugger
= 0;
1059 /* Read the debugger structure from the inferior to retrieve the
1060 address of the breakpoint and the original contents of the
1061 breakpoint address. Remove the breakpoint by writing the original
1064 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
1066 /* Set `in_debugger' to zero now. */
1068 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1070 breakpoint_addr
= (CORE_ADDR
) debug_copy
.ldd_bp_addr
;
1071 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
1072 sizeof (debug_copy
.ldd_bp_inst
));
1074 #else /* SVR4_SHARED_LIBS */
1076 /* Note that breakpoint address and original contents are in our address
1077 space, so we just need to write the original contents back. */
1079 if (memory_remove_breakpoint (breakpoint_addr
, shadow_contents
) != 0)
1084 #endif /* !SVR4_SHARED_LIBS */
1086 /* For the SVR4 version, we always know the breakpoint address. For the
1087 SunOS version we don't know it until the above code is executed.
1088 Grumble if we are stopped anywhere besides the breakpoint address. */
1090 if (stop_pc
!= breakpoint_addr
)
1092 warning ("stopped at unknown breakpoint while handling shared libraries");
1102 enable_break -- arrange for dynamic linker to hit breakpoint
1106 int enable_break (void)
1110 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1111 debugger interface, support for arranging for the inferior to hit
1112 a breakpoint after mapping in the shared libraries. This function
1113 enables that breakpoint.
1115 For SunOS, there is a special flag location (in_debugger) which we
1116 set to 1. When the dynamic linker sees this flag set, it will set
1117 a breakpoint at a location known only to itself, after saving the
1118 original contents of that place and the breakpoint address itself,
1119 in it's own internal structures. When we resume the inferior, it
1120 will eventually take a SIGTRAP when it runs into the breakpoint.
1121 We handle this (in a different place) by restoring the contents of
1122 the breakpointed location (which is only known after it stops),
1123 chasing around to locate the shared libraries that have been
1124 loaded, then resuming.
1126 For SVR4, the debugger interface structure contains a member (r_brk)
1127 which is statically initialized at the time the shared library is
1128 built, to the offset of a function (_r_debug_state) which is guaran-
1129 teed to be called once before mapping in a library, and again when
1130 the mapping is complete. At the time we are examining this member,
1131 it contains only the unrelocated offset of the function, so we have
1132 to do our own relocation. Later, when the dynamic linker actually
1133 runs, it relocates r_brk to be the actual address of _r_debug_state().
1135 The debugger interface structure also contains an enumeration which
1136 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1137 depending upon whether or not the library is being mapped or unmapped,
1138 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1146 #ifndef SVR4_SHARED_LIBS
1151 /* Get link_dynamic structure */
1153 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1154 sizeof (dynamic_copy
));
1161 /* Calc address of debugger interface structure */
1163 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1165 /* Calc address of `in_debugger' member of debugger interface structure */
1167 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
1168 (char *) &debug_copy
);
1170 /* Write a value of 1 to this member. */
1173 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1176 #else /* SVR4_SHARED_LIBS */
1178 #ifdef BKPT_AT_SYMBOL
1180 struct minimal_symbol
*msymbol
;
1182 CORE_ADDR bkpt_addr
;
1184 /* Scan through the list of symbols, trying to look up the symbol and
1185 set a breakpoint there. Terminate loop when we/if we succeed. */
1187 breakpoint_addr
= 0;
1188 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1190 msymbol
= lookup_minimal_symbol (*bkpt_namep
, symfile_objfile
);
1191 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1193 bkpt_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1194 if (target_insert_breakpoint (bkpt_addr
, shadow_contents
) == 0)
1196 breakpoint_addr
= bkpt_addr
;
1203 #else /* !BKPT_AT_SYMBOL */
1205 struct symtab_and_line sal
;
1207 /* Read the debugger interface structure directly. */
1209 read_memory (debug_base
, (char *) &debug_copy
, sizeof (debug_copy
));
1211 /* Set breakpoint at the debugger interface stub routine that will
1212 be called just prior to each mapping change and again after the
1213 mapping change is complete. Set up the (nonexistent) handler to
1214 deal with hitting these breakpoints. (FIXME). */
1216 warning ("'%s': line %d: missing SVR4 support code", __FILE__
, __LINE__
);
1219 #endif /* BKPT_AT_SYMBOL */
1221 #endif /* !SVR4_SHARED_LIBS */
1230 solib_create_inferior_hook -- shared library startup support
1234 void solib_create_inferior_hook()
1238 When gdb starts up the inferior, it nurses it along (through the
1239 shell) until it is ready to execute it's first instruction. At this
1240 point, this function gets called via expansion of the macro
1241 SOLIB_CREATE_INFERIOR_HOOK.
1243 For SunOS executables, this first instruction is typically the
1244 one at "_start", or a similar text label, regardless of whether
1245 the executable is statically or dynamically linked. The runtime
1246 startup code takes care of dynamically linking in any shared
1247 libraries, once gdb allows the inferior to continue.
1249 For SVR4 executables, this first instruction is either the first
1250 instruction in the dynamic linker (for dynamically linked
1251 executables) or the instruction at "start" for statically linked
1252 executables. For dynamically linked executables, the system
1253 first exec's /lib/libc.so.N, which contains the dynamic linker,
1254 and starts it running. The dynamic linker maps in any needed
1255 shared libraries, maps in the actual user executable, and then
1256 jumps to "start" in the user executable.
1258 For both SunOS shared libraries, and SVR4 shared libraries, we
1259 can arrange to cooperate with the dynamic linker to discover the
1260 names of shared libraries that are dynamically linked, and the
1261 base addresses to which they are linked.
1263 This function is responsible for discovering those names and
1264 addresses, and saving sufficient information about them to allow
1265 their symbols to be read at a later time.
1269 Between enable_break() and disable_break(), this code does not
1270 properly handle hitting breakpoints which the user might have
1271 set in the startup code or in the dynamic linker itself. Proper
1272 handling will probably have to wait until the implementation is
1273 changed to use the "breakpoint handler function" method.
1275 Also, what if child has exit()ed? Must exit loop somehow.
1279 solib_create_inferior_hook()
1281 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1282 yet. In fact, in the case of a SunOS4 executable being run on
1283 Solaris, we can't get it yet. find_solib will get it when it needs
1285 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1286 if ((debug_base
= locate_base ()) == 0)
1288 /* Can't find the symbol or the executable is statically linked. */
1293 if (!enable_break ())
1295 warning ("shared library handler failed to enable breakpoint");
1299 /* Now run the target. It will eventually hit the breakpoint, at
1300 which point all of the libraries will have been mapped in and we
1301 can go groveling around in the dynamic linker structures to find
1302 out what we need to know about them. */
1304 clear_proceed_status ();
1305 stop_soon_quietly
= 1;
1306 stop_signal
= TARGET_SIGNAL_0
;
1309 target_resume (-1, 0, stop_signal
);
1310 wait_for_inferior ();
1312 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
1313 stop_soon_quietly
= 0;
1315 /* We are now either at the "mapping complete" breakpoint (or somewhere
1316 else, a condition we aren't prepared to deal with anyway), so adjust
1317 the PC as necessary after a breakpoint, disable the breakpoint, and
1318 add any shared libraries that were mapped in. */
1320 if (DECR_PC_AFTER_BREAK
)
1322 stop_pc
-= DECR_PC_AFTER_BREAK
;
1323 write_register (PC_REGNUM
, stop_pc
);
1326 if (!disable_break ())
1328 warning ("shared library handler failed to disable breakpoint");
1331 solib_add ((char *) 0, 0, (struct target_ops
*) 0);
1338 special_symbol_handling -- additional shared library symbol handling
1342 void special_symbol_handling (struct so_list *so)
1346 Once the symbols from a shared object have been loaded in the usual
1347 way, we are called to do any system specific symbol handling that
1350 For Suns, this consists of grunging around in the dynamic linkers
1351 structures to find symbol definitions for "common" symbols and
1352 adding them to the minimal symbol table for the corresponding
1358 special_symbol_handling (so
)
1361 #ifndef SVR4_SHARED_LIBS
1364 if (debug_addr
== 0)
1366 /* Get link_dynamic structure */
1368 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1369 sizeof (dynamic_copy
));
1376 /* Calc address of debugger interface structure */
1377 /* FIXME, this needs work for cross-debugging of core files
1378 (byteorder, size, alignment, etc). */
1380 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1383 /* Read the debugger structure from the inferior, just to make sure
1384 we have a current copy. */
1386 j
= target_read_memory (debug_addr
, (char *) &debug_copy
,
1387 sizeof (debug_copy
));
1389 return; /* unreadable */
1391 /* Get common symbol definitions for the loaded object. */
1393 if (debug_copy
.ldd_cp
)
1395 solib_add_common_symbols (debug_copy
.ldd_cp
, so
-> objfile
);
1398 #endif /* !SVR4_SHARED_LIBS */
1406 sharedlibrary_command -- handle command to explicitly add library
1410 static void sharedlibrary_command (char *args, int from_tty)
1417 sharedlibrary_command (args
, from_tty
)
1422 solib_add (args
, from_tty
, (struct target_ops
*) 0);
1429 add_com ("sharedlibrary", class_files
, sharedlibrary_command
,
1430 "Load shared object library symbols for files matching REGEXP.");
1431 add_info ("sharedlibrary", info_sharedlibrary_command
,
1432 "Status of loaded shared object libraries.");