1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
26 #include "gdb_string.h"
27 #include <sys/param.h>
31 #ifndef SVR4_SHARED_LIBS
32 /* SunOS shared libs need the nlist structure. */
35 #include "elf/external.h"
48 #include "gnu-regex.h"
54 #define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */
56 /* On SVR4 systems, a list of symbols in the dynamic linker where
57 GDB can try to place a breakpoint to monitor shared library
60 If none of these symbols are found, or other errors occur, then
61 SVR4 systems will fall back to using a symbol as the "startup
62 mapping complete" breakpoint address. */
64 #ifdef SVR4_SHARED_LIBS
65 static char *solib_break_names
[] = {
72 #define BKPT_AT_SYMBOL 1
74 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
75 static char *bkpt_names
[] = {
76 #ifdef SOLIB_BKPT_NAME
77 SOLIB_BKPT_NAME
, /* Prefer configured name if it exists. */
85 /* Symbols which are used to locate the base of the link map structures. */
87 #ifndef SVR4_SHARED_LIBS
88 static char *debug_base_symbols
[] = {
95 static char *main_name_list
[] = {
100 /* local data declarations */
102 #ifndef SVR4_SHARED_LIBS
104 #define LM_ADDR(so) ((so) -> lm.lm_addr)
105 #define LM_NEXT(so) ((so) -> lm.lm_next)
106 #define LM_NAME(so) ((so) -> lm.lm_name)
107 /* Test for first link map entry; first entry is a shared library. */
108 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
109 static struct link_dynamic dynamic_copy
;
110 static struct link_dynamic_2 ld_2_copy
;
111 static struct ld_debug debug_copy
;
112 static CORE_ADDR debug_addr
;
113 static CORE_ADDR flag_addr
;
115 #else /* SVR4_SHARED_LIBS */
117 #define LM_ADDR(so) ((so) -> lm.l_addr)
118 #define LM_NEXT(so) ((so) -> lm.l_next)
119 #define LM_NAME(so) ((so) -> lm.l_name)
120 /* Test for first link map entry; first entry is the exec-file. */
121 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
122 static struct r_debug debug_copy
;
123 char shadow_contents
[BREAKPOINT_MAX
]; /* Stash old bkpt addr contents */
125 #endif /* !SVR4_SHARED_LIBS */
128 struct so_list
*next
; /* next structure in linked list */
129 struct link_map lm
; /* copy of link map from inferior */
130 struct link_map
*lmaddr
; /* addr in inferior lm was read from */
131 CORE_ADDR lmend
; /* upper addr bound of mapped object */
132 char so_name
[MAX_PATH_SIZE
]; /* shared object lib name (FIXME) */
133 char symbols_loaded
; /* flag: symbols read in yet? */
134 char from_tty
; /* flag: print msgs? */
135 struct objfile
*objfile
; /* objfile for loaded lib */
136 struct section_table
*sections
;
137 struct section_table
*sections_end
;
138 struct section_table
*textsection
;
142 static struct so_list
*so_list_head
; /* List of known shared objects */
143 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
144 static CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
147 fdmatch
PARAMS ((int, int)); /* In libiberty */
149 /* Local function prototypes */
152 special_symbol_handling
PARAMS ((struct so_list
*));
155 sharedlibrary_command
PARAMS ((char *, int));
158 enable_break
PARAMS ((void));
161 disable_break
PARAMS ((void));
164 info_sharedlibrary_command
PARAMS ((char *, int));
167 symbol_add_stub
PARAMS ((char *));
169 static struct so_list
*
170 find_solib
PARAMS ((struct so_list
*));
172 static struct link_map
*
173 first_link_map_member
PARAMS ((void));
176 locate_base
PARAMS ((void));
179 solib_map_sections
PARAMS ((struct so_list
*));
181 #ifdef SVR4_SHARED_LIBS
184 elf_locate_base
PARAMS ((void));
189 allocate_rt_common_objfile
PARAMS ((void));
192 solib_add_common_symbols
PARAMS ((struct rtc_symb
*));
200 solib_map_sections -- open bfd and build sections for shared lib
204 static void solib_map_sections (struct so_list *so)
208 Given a pointer to one of the shared objects in our list
209 of mapped objects, use the recorded name to open a bfd
210 descriptor for the object, build a section table, and then
211 relocate all the section addresses by the base address at
212 which the shared object was mapped.
216 In most (all?) cases the shared object file name recorded in the
217 dynamic linkage tables will be a fully qualified pathname. For
218 cases where it isn't, do we really mimic the systems search
219 mechanism correctly in the below code (particularly the tilde
224 solib_map_sections (so
)
228 char *scratch_pathname
;
230 struct section_table
*p
;
231 struct cleanup
*old_chain
;
234 filename
= tilde_expand (so
-> so_name
);
235 old_chain
= make_cleanup (free
, filename
);
237 scratch_chan
= openp (get_in_environ (inferior_environ
, "PATH"),
238 1, filename
, O_RDONLY
, 0, &scratch_pathname
);
239 if (scratch_chan
< 0)
241 scratch_chan
= openp (get_in_environ
242 (inferior_environ
, "LD_LIBRARY_PATH"),
243 1, filename
, O_RDONLY
, 0, &scratch_pathname
);
245 if (scratch_chan
< 0)
247 perror_with_name (filename
);
249 /* Leave scratch_pathname allocated. abfd->name will point to it. */
251 abfd
= bfd_fdopenr (scratch_pathname
, gnutarget
, scratch_chan
);
254 close (scratch_chan
);
255 error ("Could not open `%s' as an executable file: %s",
256 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
258 /* Leave bfd open, core_xfer_memory and "info files" need it. */
260 abfd
-> cacheable
= true;
262 /* copy full path name into so_name, so that later symbol_file_add can find
264 if (strlen (scratch_pathname
) >= MAX_PATH_SIZE
)
265 error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
266 strcpy (so
->so_name
, scratch_pathname
);
268 if (!bfd_check_format (abfd
, bfd_object
))
270 error ("\"%s\": not in executable format: %s.",
271 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
273 if (build_section_table (abfd
, &so
-> sections
, &so
-> sections_end
))
275 error ("Can't find the file sections in `%s': %s",
276 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
279 for (p
= so
-> sections
; p
< so
-> sections_end
; p
++)
281 /* Relocate the section binding addresses as recorded in the shared
282 object's file by the base address to which the object was actually
284 p
-> addr
+= (CORE_ADDR
) LM_ADDR (so
);
285 p
-> endaddr
+= (CORE_ADDR
) LM_ADDR (so
);
286 so
-> lmend
= (CORE_ADDR
) max (p
-> endaddr
, so
-> lmend
);
287 if (STREQ (p
-> the_bfd_section
-> name
, ".text"))
289 so
-> textsection
= p
;
293 /* Free the file names, close the file now. */
294 do_cleanups (old_chain
);
297 #ifndef SVR4_SHARED_LIBS
299 /* Allocate the runtime common object file. */
302 allocate_rt_common_objfile ()
304 struct objfile
*objfile
;
305 struct objfile
*last_one
;
307 objfile
= (struct objfile
*) xmalloc (sizeof (struct objfile
));
308 memset (objfile
, 0, sizeof (struct objfile
));
309 objfile
-> md
= NULL
;
310 obstack_specify_allocation (&objfile
-> psymbol_cache
.cache
, 0, 0,
312 obstack_specify_allocation (&objfile
-> psymbol_obstack
, 0, 0, xmalloc
,
314 obstack_specify_allocation (&objfile
-> symbol_obstack
, 0, 0, xmalloc
,
316 obstack_specify_allocation (&objfile
-> type_obstack
, 0, 0, xmalloc
,
318 objfile
-> name
= mstrsave (objfile
-> md
, "rt_common");
320 /* Add this file onto the tail of the linked list of other such files. */
322 objfile
-> next
= NULL
;
323 if (object_files
== NULL
)
324 object_files
= objfile
;
327 for (last_one
= object_files
;
329 last_one
= last_one
-> next
);
330 last_one
-> next
= objfile
;
333 rt_common_objfile
= objfile
;
336 /* Read all dynamically loaded common symbol definitions from the inferior
337 and put them into the minimal symbol table for the runtime common
341 solib_add_common_symbols (rtc_symp
)
342 struct rtc_symb
*rtc_symp
;
344 struct rtc_symb inferior_rtc_symb
;
345 struct nlist inferior_rtc_nlist
;
350 /* Remove any runtime common symbols from previous runs. */
352 if (rt_common_objfile
!= NULL
&& rt_common_objfile
-> minimal_symbol_count
)
354 obstack_free (&rt_common_objfile
-> symbol_obstack
, 0);
355 obstack_specify_allocation (&rt_common_objfile
-> symbol_obstack
, 0, 0,
357 rt_common_objfile
-> minimal_symbol_count
= 0;
358 rt_common_objfile
-> msymbols
= NULL
;
361 init_minimal_symbol_collection ();
362 make_cleanup (discard_minimal_symbols
, 0);
366 read_memory ((CORE_ADDR
) rtc_symp
,
367 (char *) &inferior_rtc_symb
,
368 sizeof (inferior_rtc_symb
));
369 read_memory ((CORE_ADDR
) inferior_rtc_symb
.rtc_sp
,
370 (char *) &inferior_rtc_nlist
,
371 sizeof(inferior_rtc_nlist
));
372 if (inferior_rtc_nlist
.n_type
== N_COMM
)
374 /* FIXME: The length of the symbol name is not available, but in the
375 current implementation the common symbol is allocated immediately
376 behind the name of the symbol. */
377 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
379 origname
= name
= xmalloc (len
);
380 read_memory ((CORE_ADDR
) inferior_rtc_nlist
.n_un
.n_name
, name
, len
);
382 /* Allocate the runtime common objfile if necessary. */
383 if (rt_common_objfile
== NULL
)
384 allocate_rt_common_objfile ();
386 name
= obsavestring (name
, strlen (name
),
387 &rt_common_objfile
-> symbol_obstack
);
388 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
389 mst_bss
, rt_common_objfile
);
392 rtc_symp
= inferior_rtc_symb
.rtc_next
;
395 /* Install any minimal symbols that have been collected as the current
396 minimal symbols for the runtime common objfile. */
398 install_minimal_symbols (rt_common_objfile
);
401 #endif /* SVR4_SHARED_LIBS */
404 #ifdef SVR4_SHARED_LIBS
406 #ifdef HANDLE_SVR4_EXEC_EMULATORS
409 Solaris BCP (the part of Solaris which allows it to run SunOS4
410 a.out files) throws in another wrinkle. Solaris does not fill
411 in the usual a.out link map structures when running BCP programs,
412 the only way to get at them is via groping around in the dynamic
414 The dynamic linker and it's structures are located in the shared
415 C library, which gets run as the executable's "interpreter" by
418 Note that we can assume nothing about the process state at the time
419 we need to find these structures. We may be stopped on the first
420 instruction of the interpreter (C shared library), the first
421 instruction of the executable itself, or somewhere else entirely
422 (if we attached to the process for example).
425 static char *debug_base_symbols
[] = {
426 "r_debug", /* Solaris 2.3 */
427 "_r_debug", /* Solaris 2.1, 2.2 */
432 look_for_base
PARAMS ((int, CORE_ADDR
));
435 bfd_lookup_symbol
PARAMS ((bfd
*, char *));
441 bfd_lookup_symbol -- lookup the value for a specific symbol
445 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
449 An expensive way to lookup the value of a single symbol for
450 bfd's that are only temporary anyway. This is used by the
451 shared library support to find the address of the debugger
452 interface structures in the shared library.
454 Note that 0 is specifically allowed as an error return (no
459 bfd_lookup_symbol (abfd
, symname
)
463 unsigned int storage_needed
;
465 asymbol
**symbol_table
;
466 unsigned int number_of_symbols
;
468 struct cleanup
*back_to
;
469 CORE_ADDR symaddr
= 0;
471 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
473 if (storage_needed
> 0)
475 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
476 back_to
= make_cleanup (free
, (PTR
)symbol_table
);
477 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
479 for (i
= 0; i
< number_of_symbols
; i
++)
481 sym
= *symbol_table
++;
482 if (STREQ (sym
-> name
, symname
))
484 /* Bfd symbols are section relative. */
485 symaddr
= sym
-> value
+ sym
-> section
-> vma
;
489 do_cleanups (back_to
);
498 look_for_base -- examine file for each mapped address segment
502 static int look_for_base (int fd, CORE_ADDR baseaddr)
506 This function is passed to proc_iterate_over_mappings, which
507 causes it to get called once for each mapped address space, with
508 an open file descriptor for the file mapped to that space, and the
509 base address of that mapped space.
511 Our job is to find the debug base symbol in the file that this
512 fd is open on, if it exists, and if so, initialize the dynamic
513 linker structure base address debug_base.
515 Note that this is a computationally expensive proposition, since
516 we basically have to open a bfd on every call, so we specifically
517 avoid opening the exec file.
521 look_for_base (fd
, baseaddr
)
526 CORE_ADDR address
= 0;
529 /* If the fd is -1, then there is no file that corresponds to this
530 mapped memory segment, so skip it. Also, if the fd corresponds
531 to the exec file, skip it as well. */
535 && fdmatch (fileno ((GDB_FILE
*)(exec_bfd
-> iostream
)), fd
)))
540 /* Try to open whatever random file this fd corresponds to. Note that
541 we have no way currently to find the filename. Don't gripe about
542 any problems we might have, just fail. */
544 if ((interp_bfd
= bfd_fdopenr ("unnamed", gnutarget
, fd
)) == NULL
)
548 if (!bfd_check_format (interp_bfd
, bfd_object
))
550 /* FIXME-leak: on failure, might not free all memory associated with
552 bfd_close (interp_bfd
);
556 /* Now try to find our debug base symbol in this file, which we at
557 least know to be a valid ELF executable or shared library. */
559 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
561 address
= bfd_lookup_symbol (interp_bfd
, *symbolp
);
569 /* FIXME-leak: on failure, might not free all memory associated with
571 bfd_close (interp_bfd
);
575 /* Eureka! We found the symbol. But now we may need to relocate it
576 by the base address. If the symbol's value is less than the base
577 address of the shared library, then it hasn't yet been relocated
578 by the dynamic linker, and we have to do it ourself. FIXME: Note
579 that we make the assumption that the first segment that corresponds
580 to the shared library has the base address to which the library
583 if (address
< baseaddr
)
587 debug_base
= address
;
588 /* FIXME-leak: on failure, might not free all memory associated with
590 bfd_close (interp_bfd
);
593 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
599 elf_locate_base -- locate the base address of dynamic linker structs
600 for SVR4 elf targets.
604 CORE_ADDR elf_locate_base (void)
608 For SVR4 elf targets the address of the dynamic linker's runtime
609 structure is contained within the dynamic info section in the
610 executable file. The dynamic section is also mapped into the
611 inferior address space. Because the runtime loader fills in the
612 real address before starting the inferior, we have to read in the
613 dynamic info section from the inferior address space.
614 If there are any errors while trying to find the address, we
615 silently return 0, otherwise the found address is returned.
622 sec_ptr dyninfo_sect
;
623 int dyninfo_sect_size
;
624 CORE_ADDR dyninfo_addr
;
628 /* Find the start address of the .dynamic section. */
629 dyninfo_sect
= bfd_get_section_by_name (exec_bfd
, ".dynamic");
630 if (dyninfo_sect
== NULL
)
632 dyninfo_addr
= bfd_section_vma (exec_bfd
, dyninfo_sect
);
634 /* Read in .dynamic section, silently ignore errors. */
635 dyninfo_sect_size
= bfd_section_size (exec_bfd
, dyninfo_sect
);
636 buf
= alloca (dyninfo_sect_size
);
637 if (target_read_memory (dyninfo_addr
, buf
, dyninfo_sect_size
))
640 /* Find the DT_DEBUG entry in the the .dynamic section.
641 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
642 no DT_DEBUG entries. */
643 /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
644 a 32 bit ELF ABI target. */
645 for (bufend
= buf
+ dyninfo_sect_size
;
647 buf
+= sizeof (Elf32_External_Dyn
))
649 Elf32_External_Dyn
*x_dynp
= (Elf32_External_Dyn
*)buf
;
653 dyn_tag
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_tag
);
654 if (dyn_tag
== DT_NULL
)
656 else if (dyn_tag
== DT_DEBUG
)
658 dyn_ptr
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
661 #ifdef DT_MIPS_RLD_MAP
662 else if (dyn_tag
== DT_MIPS_RLD_MAP
)
664 char pbuf
[TARGET_PTR_BIT
/ HOST_CHAR_BIT
];
666 /* DT_MIPS_RLD_MAP contains a pointer to the address
667 of the dynamic link structure. */
668 dyn_ptr
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
669 if (target_read_memory (dyn_ptr
, pbuf
, sizeof (pbuf
)))
671 return extract_unsigned_integer (pbuf
, sizeof (pbuf
));
676 /* DT_DEBUG entry not found. */
680 #endif /* SVR4_SHARED_LIBS */
686 locate_base -- locate the base address of dynamic linker structs
690 CORE_ADDR locate_base (void)
694 For both the SunOS and SVR4 shared library implementations, if the
695 inferior executable has been linked dynamically, there is a single
696 address somewhere in the inferior's data space which is the key to
697 locating all of the dynamic linker's runtime structures. This
698 address is the value of the debug base symbol. The job of this
699 function is to find and return that address, or to return 0 if there
700 is no such address (the executable is statically linked for example).
702 For SunOS, the job is almost trivial, since the dynamic linker and
703 all of it's structures are statically linked to the executable at
704 link time. Thus the symbol for the address we are looking for has
705 already been added to the minimal symbol table for the executable's
706 objfile at the time the symbol file's symbols were read, and all we
707 have to do is look it up there. Note that we explicitly do NOT want
708 to find the copies in the shared library.
710 The SVR4 version is a bit more complicated because the address
711 is contained somewhere in the dynamic info section. We have to go
712 to a lot more work to discover the address of the debug base symbol.
713 Because of this complexity, we cache the value we find and return that
714 value on subsequent invocations. Note there is no copy in the
715 executable symbol tables.
723 #ifndef SVR4_SHARED_LIBS
725 struct minimal_symbol
*msymbol
;
726 CORE_ADDR address
= 0;
729 /* For SunOS, we want to limit the search for the debug base symbol to the
730 executable being debugged, since there is a duplicate named symbol in the
731 shared library. We don't want the shared library versions. */
733 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
735 msymbol
= lookup_minimal_symbol (*symbolp
, NULL
, symfile_objfile
);
736 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
738 address
= SYMBOL_VALUE_ADDRESS (msymbol
);
744 #else /* SVR4_SHARED_LIBS */
746 /* Check to see if we have a currently valid address, and if so, avoid
747 doing all this work again and just return the cached address. If
748 we have no cached address, try to locate it in the dynamic info
749 section for ELF executables. */
754 && bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
755 debug_base
= elf_locate_base ();
756 #ifdef HANDLE_SVR4_EXEC_EMULATORS
757 /* Try it the hard way for emulated executables. */
758 else if (inferior_pid
!= 0)
759 proc_iterate_over_mappings (look_for_base
);
764 #endif /* !SVR4_SHARED_LIBS */
772 first_link_map_member -- locate first member in dynamic linker's map
776 static struct link_map *first_link_map_member (void)
780 Read in a copy of the first member in the inferior's dynamic
781 link map from the inferior's dynamic linker structures, and return
782 a pointer to the copy in our address space.
785 static struct link_map
*
786 first_link_map_member ()
788 struct link_map
*lm
= NULL
;
790 #ifndef SVR4_SHARED_LIBS
792 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
793 if (dynamic_copy
.ld_version
>= 2)
795 /* It is a version that we can deal with, so read in the secondary
796 structure and find the address of the link map list from it. */
797 read_memory ((CORE_ADDR
) dynamic_copy
.ld_un
.ld_2
, (char *) &ld_2_copy
,
798 sizeof (struct link_dynamic_2
));
799 lm
= ld_2_copy
.ld_loaded
;
802 #else /* SVR4_SHARED_LIBS */
804 read_memory (debug_base
, (char *) &debug_copy
, sizeof (struct r_debug
));
805 /* FIXME: Perhaps we should validate the info somehow, perhaps by
806 checking r_version for a known version number, or r_state for
808 lm
= debug_copy
.r_map
;
810 #endif /* !SVR4_SHARED_LIBS */
819 find_solib -- step through list of shared objects
823 struct so_list *find_solib (struct so_list *so_list_ptr)
827 This module contains the routine which finds the names of any
828 loaded "images" in the current process. The argument in must be
829 NULL on the first call, and then the returned value must be passed
830 in on subsequent calls. This provides the capability to "step" down
831 the list of loaded objects. On the last object, a NULL value is
834 The arg and return value are "struct link_map" pointers, as defined
838 static struct so_list
*
839 find_solib (so_list_ptr
)
840 struct so_list
*so_list_ptr
; /* Last lm or NULL for first one */
842 struct so_list
*so_list_next
= NULL
;
843 struct link_map
*lm
= NULL
;
846 if (so_list_ptr
== NULL
)
848 /* We are setting up for a new scan through the loaded images. */
849 if ((so_list_next
= so_list_head
) == NULL
)
851 /* We have not already read in the dynamic linking structures
852 from the inferior, lookup the address of the base structure. */
853 debug_base
= locate_base ();
856 /* Read the base structure in and find the address of the first
857 link map list member. */
858 lm
= first_link_map_member ();
864 /* We have been called before, and are in the process of walking
865 the shared library list. Advance to the next shared object. */
866 if ((lm
= LM_NEXT (so_list_ptr
)) == NULL
)
868 /* We have hit the end of the list, so check to see if any were
869 added, but be quiet if we can't read from the target any more. */
870 int status
= target_read_memory ((CORE_ADDR
) so_list_ptr
-> lmaddr
,
871 (char *) &(so_list_ptr
-> lm
),
872 sizeof (struct link_map
));
875 lm
= LM_NEXT (so_list_ptr
);
882 so_list_next
= so_list_ptr
-> next
;
884 if ((so_list_next
== NULL
) && (lm
!= NULL
))
886 /* Get next link map structure from inferior image and build a local
887 abbreviated load_map structure */
888 new = (struct so_list
*) xmalloc (sizeof (struct so_list
));
889 memset ((char *) new, 0, sizeof (struct so_list
));
891 /* Add the new node as the next node in the list, or as the root
892 node if this is the first one. */
893 if (so_list_ptr
!= NULL
)
895 so_list_ptr
-> next
= new;
902 read_memory ((CORE_ADDR
) lm
, (char *) &(new -> lm
),
903 sizeof (struct link_map
));
904 /* For SVR4 versions, the first entry in the link map is for the
905 inferior executable, so we must ignore it. For some versions of
906 SVR4, it has no name. For others (Solaris 2.3 for example), it
907 does have a name, so we can no longer use a missing name to
908 decide when to ignore it. */
909 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm
))
913 target_read_string ((CORE_ADDR
) LM_NAME (new), &buffer
,
914 MAX_PATH_SIZE
- 1, &errcode
);
916 error ("find_solib: Can't read pathname for load map: %s\n",
917 safe_strerror (errcode
));
918 strncpy (new -> so_name
, buffer
, MAX_PATH_SIZE
- 1);
919 new -> so_name
[MAX_PATH_SIZE
- 1] = '\0';
921 solib_map_sections (new);
924 return (so_list_next
);
927 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
930 symbol_add_stub (arg
)
933 register struct so_list
*so
= (struct so_list
*) arg
; /* catch_errs bogon */
936 symbol_file_add (so
-> so_name
, so
-> from_tty
,
937 (so
->textsection
== NULL
939 : (unsigned int) so
-> textsection
-> addr
),
944 /* This function will check the so name to see if matches the main list.
945 In some system the main object is in the list, which we want to exclude */
947 static int match_main (soname
)
952 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
954 if (strcmp (soname
, *mainp
) == 0)
965 solib_add -- add a shared library file to the symtab and section list
969 void solib_add (char *arg_string, int from_tty,
970 struct target_ops *target)
977 solib_add (arg_string
, from_tty
, target
)
980 struct target_ops
*target
;
982 register struct so_list
*so
= NULL
; /* link map state variable */
984 /* Last shared library that we read. */
985 struct so_list
*so_last
= NULL
;
991 if ((re_err
= re_comp (arg_string
? arg_string
: ".")) != NULL
)
993 error ("Invalid regexp: %s", re_err
);
996 /* Add the shared library sections to the section table of the
997 specified target, if any. */
1000 /* Count how many new section_table entries there are. */
1003 while ((so
= find_solib (so
)) != NULL
)
1005 if (so
-> so_name
[0] && !match_main (so
-> so_name
))
1007 count
+= so
-> sections_end
- so
-> sections
;
1015 /* We must update the to_sections field in the core_ops structure
1016 here, otherwise we dereference a potential dangling pointer
1017 for each call to target_read/write_memory within this routine. */
1018 update_coreops
= core_ops
.to_sections
== target
->to_sections
;
1020 /* Reallocate the target's section table including the new size. */
1021 if (target
-> to_sections
)
1023 old
= target
-> to_sections_end
- target
-> to_sections
;
1024 target
-> to_sections
= (struct section_table
*)
1025 xrealloc ((char *)target
-> to_sections
,
1026 (sizeof (struct section_table
)) * (count
+ old
));
1031 target
-> to_sections
= (struct section_table
*)
1032 xmalloc ((sizeof (struct section_table
)) * count
);
1034 target
-> to_sections_end
= target
-> to_sections
+ (count
+ old
);
1036 /* Update the to_sections field in the core_ops structure
1040 core_ops
.to_sections
= target
->to_sections
;
1041 core_ops
.to_sections_end
= target
->to_sections_end
;
1044 /* Add these section table entries to the target's table. */
1045 while ((so
= find_solib (so
)) != NULL
)
1047 if (so
-> so_name
[0])
1049 count
= so
-> sections_end
- so
-> sections
;
1050 memcpy ((char *) (target
-> to_sections
+ old
),
1052 (sizeof (struct section_table
)) * count
);
1059 /* Now add the symbol files. */
1060 while ((so
= find_solib (so
)) != NULL
)
1062 if (so
-> so_name
[0] && re_exec (so
-> so_name
) &&
1063 !match_main (so
-> so_name
))
1065 so
-> from_tty
= from_tty
;
1066 if (so
-> symbols_loaded
)
1070 printf_unfiltered ("Symbols already loaded for %s\n", so
-> so_name
);
1073 else if (catch_errors
1074 (symbol_add_stub
, (char *) so
,
1075 "Error while reading shared library symbols:\n",
1079 so
-> symbols_loaded
= 1;
1084 /* Getting new symbols may change our opinion about what is
1087 reinit_frame_cache ();
1090 special_symbol_handling (so_last
);
1097 info_sharedlibrary_command -- code for "info sharedlibrary"
1101 static void info_sharedlibrary_command ()
1105 Walk through the shared library list and print information
1106 about each attached library.
1110 info_sharedlibrary_command (ignore
, from_tty
)
1114 register struct so_list
*so
= NULL
; /* link map state variable */
1115 int header_done
= 0;
1117 if (exec_bfd
== NULL
)
1119 printf_unfiltered ("No exec file.\n");
1122 while ((so
= find_solib (so
)) != NULL
)
1124 if (so
-> so_name
[0])
1128 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
1129 "Shared Object Library");
1132 /* FIXME-32x64: need print_address_numeric with field width or
1134 printf_unfiltered ("%-12s",
1135 local_hex_string_custom ((unsigned long) LM_ADDR (so
),
1137 printf_unfiltered ("%-12s",
1138 local_hex_string_custom ((unsigned long) so
-> lmend
,
1140 printf_unfiltered ("%-12s", so
-> symbols_loaded
? "Yes" : "No");
1141 printf_unfiltered ("%s\n", so
-> so_name
);
1144 if (so_list_head
== NULL
)
1146 printf_unfiltered ("No shared libraries loaded at this time.\n");
1154 solib_address -- check to see if an address is in a shared lib
1158 char * solib_address (CORE_ADDR address)
1162 Provides a hook for other gdb routines to discover whether or
1163 not a particular address is within the mapped address space of
1164 a shared library. Any address between the base mapping address
1165 and the first address beyond the end of the last mapping, is
1166 considered to be within the shared library address space, for
1169 For example, this routine is called at one point to disable
1170 breakpoints which are in shared libraries that are not currently
1175 solib_address (address
)
1178 register struct so_list
*so
= 0; /* link map state variable */
1180 while ((so
= find_solib (so
)) != NULL
)
1182 if (so
-> so_name
[0])
1184 if ((address
>= (CORE_ADDR
) LM_ADDR (so
)) &&
1185 (address
< (CORE_ADDR
) so
-> lmend
))
1186 return (so
->so_name
);
1192 /* Called by free_all_symtabs */
1197 struct so_list
*next
;
1200 while (so_list_head
)
1202 if (so_list_head
-> sections
)
1204 free ((PTR
)so_list_head
-> sections
);
1206 if (so_list_head
-> abfd
)
1208 bfd_filename
= bfd_get_filename (so_list_head
-> abfd
);
1209 if (!bfd_close (so_list_head
-> abfd
))
1210 warning ("cannot close \"%s\": %s",
1211 bfd_filename
, bfd_errmsg (bfd_get_error ()));
1214 /* This happens for the executable on SVR4. */
1215 bfd_filename
= NULL
;
1217 next
= so_list_head
-> next
;
1219 free ((PTR
)bfd_filename
);
1220 free ((PTR
)so_list_head
);
1221 so_list_head
= next
;
1230 disable_break -- remove the "mapping changed" breakpoint
1234 static int disable_break ()
1238 Removes the breakpoint that gets hit when the dynamic linker
1239 completes a mapping change.
1248 #ifndef SVR4_SHARED_LIBS
1250 int in_debugger
= 0;
1252 /* Read the debugger structure from the inferior to retrieve the
1253 address of the breakpoint and the original contents of the
1254 breakpoint address. Remove the breakpoint by writing the original
1257 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
1259 /* Set `in_debugger' to zero now. */
1261 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1263 breakpoint_addr
= (CORE_ADDR
) debug_copy
.ldd_bp_addr
;
1264 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
1265 sizeof (debug_copy
.ldd_bp_inst
));
1267 #else /* SVR4_SHARED_LIBS */
1269 /* Note that breakpoint address and original contents are in our address
1270 space, so we just need to write the original contents back. */
1272 if (memory_remove_breakpoint (breakpoint_addr
, shadow_contents
) != 0)
1277 #endif /* !SVR4_SHARED_LIBS */
1279 /* For the SVR4 version, we always know the breakpoint address. For the
1280 SunOS version we don't know it until the above code is executed.
1281 Grumble if we are stopped anywhere besides the breakpoint address. */
1283 if (stop_pc
!= breakpoint_addr
)
1285 warning ("stopped at unknown breakpoint while handling shared libraries");
1295 enable_break -- arrange for dynamic linker to hit breakpoint
1299 int enable_break (void)
1303 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1304 debugger interface, support for arranging for the inferior to hit
1305 a breakpoint after mapping in the shared libraries. This function
1306 enables that breakpoint.
1308 For SunOS, there is a special flag location (in_debugger) which we
1309 set to 1. When the dynamic linker sees this flag set, it will set
1310 a breakpoint at a location known only to itself, after saving the
1311 original contents of that place and the breakpoint address itself,
1312 in it's own internal structures. When we resume the inferior, it
1313 will eventually take a SIGTRAP when it runs into the breakpoint.
1314 We handle this (in a different place) by restoring the contents of
1315 the breakpointed location (which is only known after it stops),
1316 chasing around to locate the shared libraries that have been
1317 loaded, then resuming.
1319 For SVR4, the debugger interface structure contains a member (r_brk)
1320 which is statically initialized at the time the shared library is
1321 built, to the offset of a function (_r_debug_state) which is guaran-
1322 teed to be called once before mapping in a library, and again when
1323 the mapping is complete. At the time we are examining this member,
1324 it contains only the unrelocated offset of the function, so we have
1325 to do our own relocation. Later, when the dynamic linker actually
1326 runs, it relocates r_brk to be the actual address of _r_debug_state().
1328 The debugger interface structure also contains an enumeration which
1329 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1330 depending upon whether or not the library is being mapped or unmapped,
1331 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1339 #ifndef SVR4_SHARED_LIBS
1344 /* Get link_dynamic structure */
1346 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1347 sizeof (dynamic_copy
));
1354 /* Calc address of debugger interface structure */
1356 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1358 /* Calc address of `in_debugger' member of debugger interface structure */
1360 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
1361 (char *) &debug_copy
);
1363 /* Write a value of 1 to this member. */
1366 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1369 #else /* SVR4_SHARED_LIBS */
1371 #ifdef BKPT_AT_SYMBOL
1373 struct minimal_symbol
*msymbol
;
1374 struct objfile
*objfile
;
1376 CORE_ADDR bkpt_addr
;
1377 asection
*interp_sect
;
1379 /* First, remove all the solib event breakpoints. Their addresses
1380 may have changed since the last time we ran the program. */
1381 remove_solib_event_breakpoints ();
1383 #ifdef SVR4_SHARED_LIBS
1384 /* Find the .interp section; if not found, warn the user and drop
1385 into the old breakpoint at symbol code. */
1386 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
1389 unsigned int interp_sect_size
;
1391 CORE_ADDR load_addr
;
1393 asection
*lowest_sect
;
1395 /* Read the contents of the .interp section into a local buffer;
1396 the contents specify the dynamic linker this program uses. */
1397 interp_sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
1398 buf
= alloca (interp_sect_size
);
1399 bfd_get_section_contents (exec_bfd
, interp_sect
,
1400 buf
, 0, interp_sect_size
);
1402 /* Now we need to figure out where the dynamic linker was
1403 loaded so that we can load its symbols and place a breakpoint
1404 in the dynamic linker itself.
1406 This address is stored on the stack. However, I've been unable
1407 to find any magic formula to find it for Solaris (appears to
1408 be trivial on Linux). Therefore, we have to try an alternate
1409 mechanism to find the dynamic linker's base address. */
1410 tmp_bfd
= bfd_openr (buf
, gnutarget
);
1411 if (tmp_bfd
== NULL
)
1412 goto bkpt_at_symbol
;
1414 /* Make sure the dynamic linker's really a useful object. */
1415 if (!bfd_check_format (tmp_bfd
, bfd_object
))
1417 warning ("Unable to grok dynamic linker %s as an object file", buf
);
1418 bfd_close (tmp_bfd
);
1419 goto bkpt_at_symbol
;
1422 /* We find the dynamic linker's base address by examining the
1423 current pc (which point at the entry point for the dynamic
1424 linker) and subtracting the offset of the entry point. */
1425 load_addr
= read_pc () - tmp_bfd
->start_address
;
1427 /* load_addr now has the base address of the dynamic linker;
1428 however, due to severe braindamage in syms_from_objfile
1429 we need to add the address of the .text section, or the
1430 lowest section of .text doesn't exist to work around the
1431 braindamage. Gross. */
1432 lowest_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
1433 if (lowest_sect
== NULL
)
1434 bfd_map_over_sections (tmp_bfd
, find_lowest_section
,
1435 (PTR
) &lowest_sect
);
1437 if (lowest_sect
== NULL
)
1439 warning ("Unable to find base address for dynamic linker %s\n", buf
);
1440 bfd_close (tmp_bfd
);
1441 goto bkpt_at_symbol
;
1444 load_addr
+= bfd_section_vma (tmp_bfd
, lowest_sect
);
1446 /* We're done with the temporary bfd. */
1447 bfd_close (tmp_bfd
);
1449 /* Now make GDB aware of the symbols in the dynamic linker. Some
1450 might complain about namespace pollution, but as a developer I've
1451 often wanted these symbols available from within the debugger. */
1452 objfile
= symbol_file_add (buf
, 0, load_addr
, 0, 0, 1);
1454 /* Now try to set a breakpoint in the dynamic linker. */
1455 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1457 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, objfile
);
1458 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1460 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol
));
1465 /* For whatever reason we couldn't set a breakpoint in the dynamic
1466 linker. Warn and drop into the old code. */
1468 warning ("Unable to find dynamic linker breakpoint function.");
1469 warning ("GDB will be unable to debug shared library initializers");
1470 warning ("and track explicitly loaded dynamic code.");
1474 /* Scan through the list of symbols, trying to look up the symbol and
1475 set a breakpoint there. Terminate loop when we/if we succeed. */
1477 breakpoint_addr
= 0;
1478 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1480 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
1481 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1483 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol
));
1488 /* Nothing good happened. */
1491 #endif /* BKPT_AT_SYMBOL */
1493 #endif /* !SVR4_SHARED_LIBS */
1502 solib_create_inferior_hook -- shared library startup support
1506 void solib_create_inferior_hook()
1510 When gdb starts up the inferior, it nurses it along (through the
1511 shell) until it is ready to execute it's first instruction. At this
1512 point, this function gets called via expansion of the macro
1513 SOLIB_CREATE_INFERIOR_HOOK.
1515 For SunOS executables, this first instruction is typically the
1516 one at "_start", or a similar text label, regardless of whether
1517 the executable is statically or dynamically linked. The runtime
1518 startup code takes care of dynamically linking in any shared
1519 libraries, once gdb allows the inferior to continue.
1521 For SVR4 executables, this first instruction is either the first
1522 instruction in the dynamic linker (for dynamically linked
1523 executables) or the instruction at "start" for statically linked
1524 executables. For dynamically linked executables, the system
1525 first exec's /lib/libc.so.N, which contains the dynamic linker,
1526 and starts it running. The dynamic linker maps in any needed
1527 shared libraries, maps in the actual user executable, and then
1528 jumps to "start" in the user executable.
1530 For both SunOS shared libraries, and SVR4 shared libraries, we
1531 can arrange to cooperate with the dynamic linker to discover the
1532 names of shared libraries that are dynamically linked, and the
1533 base addresses to which they are linked.
1535 This function is responsible for discovering those names and
1536 addresses, and saving sufficient information about them to allow
1537 their symbols to be read at a later time.
1541 Between enable_break() and disable_break(), this code does not
1542 properly handle hitting breakpoints which the user might have
1543 set in the startup code or in the dynamic linker itself. Proper
1544 handling will probably have to wait until the implementation is
1545 changed to use the "breakpoint handler function" method.
1547 Also, what if child has exit()ed? Must exit loop somehow.
1551 solib_create_inferior_hook()
1553 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1554 yet. In fact, in the case of a SunOS4 executable being run on
1555 Solaris, we can't get it yet. find_solib will get it when it needs
1557 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1558 if ((debug_base
= locate_base ()) == 0)
1560 /* Can't find the symbol or the executable is statically linked. */
1565 if (!enable_break ())
1567 warning ("shared library handler failed to enable breakpoint");
1571 #ifndef SVR4_SHARED_LIBS
1572 /* Only SunOS needs the loop below, other systems should be using the
1573 special shared library breakpoints and the shared library breakpoint
1576 Now run the target. It will eventually hit the breakpoint, at
1577 which point all of the libraries will have been mapped in and we
1578 can go groveling around in the dynamic linker structures to find
1579 out what we need to know about them. */
1581 clear_proceed_status ();
1582 stop_soon_quietly
= 1;
1583 stop_signal
= TARGET_SIGNAL_0
;
1586 target_resume (-1, 0, stop_signal
);
1587 wait_for_inferior ();
1589 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
1590 stop_soon_quietly
= 0;
1592 /* We are now either at the "mapping complete" breakpoint (or somewhere
1593 else, a condition we aren't prepared to deal with anyway), so adjust
1594 the PC as necessary after a breakpoint, disable the breakpoint, and
1595 add any shared libraries that were mapped in. */
1597 if (DECR_PC_AFTER_BREAK
)
1599 stop_pc
-= DECR_PC_AFTER_BREAK
;
1600 write_register (PC_REGNUM
, stop_pc
);
1603 if (!disable_break ())
1605 warning ("shared library handler failed to disable breakpoint");
1609 solib_add ((char *) 0, 0, (struct target_ops
*) 0);
1617 special_symbol_handling -- additional shared library symbol handling
1621 void special_symbol_handling (struct so_list *so)
1625 Once the symbols from a shared object have been loaded in the usual
1626 way, we are called to do any system specific symbol handling that
1629 For SunOS4, this consists of grunging around in the dynamic
1630 linkers structures to find symbol definitions for "common" symbols
1631 and adding them to the minimal symbol table for the runtime common
1637 special_symbol_handling (so
)
1640 #ifndef SVR4_SHARED_LIBS
1643 if (debug_addr
== 0)
1645 /* Get link_dynamic structure */
1647 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1648 sizeof (dynamic_copy
));
1655 /* Calc address of debugger interface structure */
1656 /* FIXME, this needs work for cross-debugging of core files
1657 (byteorder, size, alignment, etc). */
1659 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1662 /* Read the debugger structure from the inferior, just to make sure
1663 we have a current copy. */
1665 j
= target_read_memory (debug_addr
, (char *) &debug_copy
,
1666 sizeof (debug_copy
));
1668 return; /* unreadable */
1670 /* Get common symbol definitions for the loaded object. */
1672 if (debug_copy
.ldd_cp
)
1674 solib_add_common_symbols (debug_copy
.ldd_cp
);
1677 #endif /* !SVR4_SHARED_LIBS */
1685 sharedlibrary_command -- handle command to explicitly add library
1689 static void sharedlibrary_command (char *args, int from_tty)
1696 sharedlibrary_command (args
, from_tty
)
1701 solib_add (args
, from_tty
, (struct target_ops
*) 0);
1708 add_com ("sharedlibrary", class_files
, sharedlibrary_command
,
1709 "Load shared object library symbols for files matching REGEXP.");
1710 add_info ("sharedlibrary", info_sharedlibrary_command
,
1711 "Status of loaded shared object libraries.");
1714 (add_set_cmd ("auto-solib-add", class_support
, var_zinteger
,
1715 (char *) &auto_solib_add
,
1716 "Set autoloading of shared library symbols.\n\
1717 If nonzero, symbols from all shared object libraries will be loaded\n\
1718 automatically when the inferior begins execution or when the dynamic linker\n\
1719 informs gdb that a new library has been loaded. Otherwise, symbols\n\
1720 must be loaded manually, using `sharedlibrary'.",