1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 98, 1999
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,
20 Boston, MA 02111-1307, USA. */
25 /* This file is only compilable if link.h is available. */
29 #include <sys/types.h>
31 #include "gdb_string.h"
32 #include <sys/param.h>
35 #ifndef SVR4_SHARED_LIBS
36 /* SunOS shared libs need the nlist structure. */
39 #include "elf/external.h"
52 #include "gdb_regex.h"
58 #define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */
60 /* On SVR4 systems, a list of symbols in the dynamic linker where
61 GDB can try to place a breakpoint to monitor shared library
64 If none of these symbols are found, or other errors occur, then
65 SVR4 systems will fall back to using a symbol as the "startup
66 mapping complete" breakpoint address. */
68 #ifdef SVR4_SHARED_LIBS
69 static char *solib_break_names
[] =
79 #define BKPT_AT_SYMBOL 1
81 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
82 static char *bkpt_names
[] =
84 #ifdef SOLIB_BKPT_NAME
85 SOLIB_BKPT_NAME
, /* Prefer configured name if it exists. */
93 /* Symbols which are used to locate the base of the link map structures. */
95 #ifndef SVR4_SHARED_LIBS
96 static char *debug_base_symbols
[] =
104 static char *main_name_list
[] =
110 /* local data declarations */
112 /* Macro to extract an address from a solib structure.
113 When GDB is configured for some 32-bit targets (e.g. Solaris 2.7
114 sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is
115 64 bits. We have to extract only the significant bits of addresses
116 to get the right address when accessing the core file BFD. */
118 #define SOLIB_EXTRACT_ADDRESS(member) \
119 extract_address (&member, sizeof (member))
121 #ifndef SVR4_SHARED_LIBS
123 #define LM_ADDR(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.lm_addr))
124 #define LM_NEXT(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.lm_next))
125 #define LM_NAME(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.lm_name))
126 /* Test for first link map entry; first entry is a shared library. */
127 #define IGNORE_FIRST_LINK_MAP_ENTRY(so) (0)
128 static struct link_dynamic dynamic_copy
;
129 static struct link_dynamic_2 ld_2_copy
;
130 static struct ld_debug debug_copy
;
131 static CORE_ADDR debug_addr
;
132 static CORE_ADDR flag_addr
;
134 #else /* SVR4_SHARED_LIBS */
136 #define LM_ADDR(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_addr))
137 #define LM_NEXT(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_next))
138 #define LM_NAME(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_name))
139 /* Test for first link map entry; first entry is the exec-file. */
140 #define IGNORE_FIRST_LINK_MAP_ENTRY(so) \
141 (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_prev) == 0)
142 static struct r_debug debug_copy
;
143 char shadow_contents
[BREAKPOINT_MAX
]; /* Stash old bkpt addr contents */
145 #endif /* !SVR4_SHARED_LIBS */
149 /* The following fields of the structure come directly from the
150 dynamic linker's tables in the inferior, and are initialized by
153 struct so_list
*next
; /* next structure in linked list */
154 struct link_map lm
; /* copy of link map from inferior */
155 CORE_ADDR lmaddr
; /* addr in inferior lm was read from */
157 /* Shared object file name, exactly as it appears in the
158 inferior's link map. This may be a relative path, or something
159 which needs to be looked up in LD_LIBRARY_PATH, etc. We use it
160 to tell which entries in the inferior's dynamic linker's link
161 map we've already loaded. */
162 char so_original_name
[MAX_PATH_SIZE
];
164 /* shared object file name, expanded to something GDB can open */
165 char so_name
[MAX_PATH_SIZE
];
167 /* The following fields of the structure are built from
168 information gathered from the shared object file itself, and
169 are initialized when we actually add it to our symbol tables. */
172 CORE_ADDR lmend
; /* upper addr bound of mapped object */
173 char symbols_loaded
; /* flag: symbols read in yet? */
174 char from_tty
; /* flag: print msgs? */
175 struct objfile
*objfile
; /* objfile for loaded lib */
176 struct section_table
*sections
;
177 struct section_table
*sections_end
;
178 struct section_table
*textsection
;
181 static struct so_list
*so_list_head
; /* List of known shared objects */
182 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
183 static CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
185 static int solib_cleanup_queued
= 0; /* make_run_cleanup called */
188 fdmatch
PARAMS ((int, int)); /* In libiberty */
190 /* Local function prototypes */
193 do_clear_solib
PARAMS ((PTR
));
196 match_main
PARAMS ((char *));
199 special_symbol_handling
PARAMS ((void));
202 sharedlibrary_command
PARAMS ((char *, int));
205 enable_break
PARAMS ((void));
208 info_sharedlibrary_command
PARAMS ((char *, int));
210 static int symbol_add_stub
PARAMS ((PTR
));
213 first_link_map_member
PARAMS ((void));
216 locate_base
PARAMS ((void));
218 static int solib_map_sections
PARAMS ((PTR
));
220 #ifdef SVR4_SHARED_LIBS
223 elf_locate_base
PARAMS ((void));
227 static struct so_list
*current_sos (void);
228 static void free_so (struct so_list
*node
);
231 disable_break
PARAMS ((void));
234 allocate_rt_common_objfile
PARAMS ((void));
237 solib_add_common_symbols (CORE_ADDR
);
241 void _initialize_solib
PARAMS ((void));
243 /* If non-zero, this is a prefix that will be added to the front of the name
244 shared libraries with an absolute filename for loading. */
245 static char *solib_absolute_prefix
= NULL
;
247 /* If non-empty, this is a search path for loading non-absolute shared library
248 symbol files. This takes precedence over the environment variables PATH
249 and LD_LIBRARY_PATH. */
250 static char *solib_search_path
= NULL
;
256 solib_map_sections -- open bfd and build sections for shared lib
260 static int solib_map_sections (struct so_list *so)
264 Given a pointer to one of the shared objects in our list
265 of mapped objects, use the recorded name to open a bfd
266 descriptor for the object, build a section table, and then
267 relocate all the section addresses by the base address at
268 which the shared object was mapped.
272 In most (all?) cases the shared object file name recorded in the
273 dynamic linkage tables will be a fully qualified pathname. For
274 cases where it isn't, do we really mimic the systems search
275 mechanism correctly in the below code (particularly the tilde
280 solib_map_sections (arg
)
283 struct so_list
*so
= (struct so_list
*) arg
; /* catch_errors bogon */
285 char *scratch_pathname
;
287 struct section_table
*p
;
288 struct cleanup
*old_chain
;
291 filename
= tilde_expand (so
->so_name
);
293 if (solib_absolute_prefix
&& ROOTED_P (filename
))
294 /* Prefix shared libraries with absolute filenames with
295 SOLIB_ABSOLUTE_PREFIX. */
300 pfx_len
= strlen (solib_absolute_prefix
);
302 /* Remove trailing slashes. */
303 while (pfx_len
> 0 && SLASH_P (solib_absolute_prefix
[pfx_len
- 1]))
306 pfxed_fn
= xmalloc (pfx_len
+ strlen (filename
) + 1);
307 strcpy (pfxed_fn
, solib_absolute_prefix
);
308 strcat (pfxed_fn
, filename
);
314 old_chain
= make_cleanup (free
, filename
);
318 if (solib_search_path
)
319 scratch_chan
= openp (solib_search_path
,
320 1, filename
, O_RDONLY
, 0, &scratch_pathname
);
321 if (scratch_chan
< 0)
322 scratch_chan
= openp (get_in_environ (inferior_environ
, "PATH"),
323 1, filename
, O_RDONLY
, 0, &scratch_pathname
);
324 if (scratch_chan
< 0)
326 scratch_chan
= openp (get_in_environ
327 (inferior_environ
, "LD_LIBRARY_PATH"),
328 1, filename
, O_RDONLY
, 0, &scratch_pathname
);
330 if (scratch_chan
< 0)
332 perror_with_name (filename
);
334 /* Leave scratch_pathname allocated. abfd->name will point to it. */
336 abfd
= bfd_fdopenr (scratch_pathname
, gnutarget
, scratch_chan
);
339 close (scratch_chan
);
340 error ("Could not open `%s' as an executable file: %s",
341 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
343 /* Leave bfd open, core_xfer_memory and "info files" need it. */
345 abfd
->cacheable
= true;
347 /* copy full path name into so_name, so that later symbol_file_add can find
349 if (strlen (scratch_pathname
) >= MAX_PATH_SIZE
)
350 error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
351 strcpy (so
->so_name
, scratch_pathname
);
353 if (!bfd_check_format (abfd
, bfd_object
))
355 error ("\"%s\": not in executable format: %s.",
356 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
358 if (build_section_table (abfd
, &so
->sections
, &so
->sections_end
))
360 error ("Can't find the file sections in `%s': %s",
361 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
364 for (p
= so
->sections
; p
< so
->sections_end
; p
++)
366 /* Relocate the section binding addresses as recorded in the shared
367 object's file by the base address to which the object was actually
369 p
->addr
+= LM_ADDR (so
);
370 p
->endaddr
+= LM_ADDR (so
);
371 so
->lmend
= max (p
->endaddr
, so
->lmend
);
372 if (STREQ (p
->the_bfd_section
->name
, ".text"))
378 /* Free the file names, close the file now. */
379 do_cleanups (old_chain
);
384 #ifndef SVR4_SHARED_LIBS
386 /* Allocate the runtime common object file. */
389 allocate_rt_common_objfile ()
391 struct objfile
*objfile
;
392 struct objfile
*last_one
;
394 objfile
= (struct objfile
*) xmalloc (sizeof (struct objfile
));
395 memset (objfile
, 0, sizeof (struct objfile
));
397 obstack_specify_allocation (&objfile
->psymbol_cache
.cache
, 0, 0,
399 obstack_specify_allocation (&objfile
->psymbol_obstack
, 0, 0, xmalloc
,
401 obstack_specify_allocation (&objfile
->symbol_obstack
, 0, 0, xmalloc
,
403 obstack_specify_allocation (&objfile
->type_obstack
, 0, 0, xmalloc
,
405 objfile
->name
= mstrsave (objfile
->md
, "rt_common");
407 /* Add this file onto the tail of the linked list of other such files. */
409 objfile
->next
= NULL
;
410 if (object_files
== NULL
)
411 object_files
= objfile
;
414 for (last_one
= object_files
;
416 last_one
= last_one
->next
);
417 last_one
->next
= objfile
;
420 rt_common_objfile
= objfile
;
423 /* Read all dynamically loaded common symbol definitions from the inferior
424 and put them into the minimal symbol table for the runtime common
428 solib_add_common_symbols (rtc_symp
)
431 struct rtc_symb inferior_rtc_symb
;
432 struct nlist inferior_rtc_nlist
;
436 /* Remove any runtime common symbols from previous runs. */
438 if (rt_common_objfile
!= NULL
&& rt_common_objfile
->minimal_symbol_count
)
440 obstack_free (&rt_common_objfile
->symbol_obstack
, 0);
441 obstack_specify_allocation (&rt_common_objfile
->symbol_obstack
, 0, 0,
443 rt_common_objfile
->minimal_symbol_count
= 0;
444 rt_common_objfile
->msymbols
= NULL
;
447 init_minimal_symbol_collection ();
448 make_cleanup ((make_cleanup_func
) discard_minimal_symbols
, 0);
452 read_memory (rtc_symp
,
453 (char *) &inferior_rtc_symb
,
454 sizeof (inferior_rtc_symb
));
455 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb
.rtc_sp
),
456 (char *) &inferior_rtc_nlist
,
457 sizeof (inferior_rtc_nlist
));
458 if (inferior_rtc_nlist
.n_type
== N_COMM
)
460 /* FIXME: The length of the symbol name is not available, but in the
461 current implementation the common symbol is allocated immediately
462 behind the name of the symbol. */
463 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
465 name
= xmalloc (len
);
466 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist
.n_un
.n_name
),
469 /* Allocate the runtime common objfile if necessary. */
470 if (rt_common_objfile
== NULL
)
471 allocate_rt_common_objfile ();
473 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
474 mst_bss
, rt_common_objfile
);
477 rtc_symp
= SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb
.rtc_next
);
480 /* Install any minimal symbols that have been collected as the current
481 minimal symbols for the runtime common objfile. */
483 install_minimal_symbols (rt_common_objfile
);
486 #endif /* SVR4_SHARED_LIBS */
489 #ifdef SVR4_SHARED_LIBS
492 bfd_lookup_symbol
PARAMS ((bfd
*, char *));
498 bfd_lookup_symbol -- lookup the value for a specific symbol
502 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
506 An expensive way to lookup the value of a single symbol for
507 bfd's that are only temporary anyway. This is used by the
508 shared library support to find the address of the debugger
509 interface structures in the shared library.
511 Note that 0 is specifically allowed as an error return (no
516 bfd_lookup_symbol (abfd
, symname
)
520 unsigned int storage_needed
;
522 asymbol
**symbol_table
;
523 unsigned int number_of_symbols
;
525 struct cleanup
*back_to
;
526 CORE_ADDR symaddr
= 0;
528 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
530 if (storage_needed
> 0)
532 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
533 back_to
= make_cleanup (free
, (PTR
) symbol_table
);
534 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
536 for (i
= 0; i
< number_of_symbols
; i
++)
538 sym
= *symbol_table
++;
539 if (STREQ (sym
->name
, symname
))
541 /* Bfd symbols are section relative. */
542 symaddr
= sym
->value
+ sym
->section
->vma
;
546 do_cleanups (back_to
);
551 #ifdef HANDLE_SVR4_EXEC_EMULATORS
554 Solaris BCP (the part of Solaris which allows it to run SunOS4
555 a.out files) throws in another wrinkle. Solaris does not fill
556 in the usual a.out link map structures when running BCP programs,
557 the only way to get at them is via groping around in the dynamic
559 The dynamic linker and it's structures are located in the shared
560 C library, which gets run as the executable's "interpreter" by
563 Note that we can assume nothing about the process state at the time
564 we need to find these structures. We may be stopped on the first
565 instruction of the interpreter (C shared library), the first
566 instruction of the executable itself, or somewhere else entirely
567 (if we attached to the process for example).
570 static char *debug_base_symbols
[] =
572 "r_debug", /* Solaris 2.3 */
573 "_r_debug", /* Solaris 2.1, 2.2 */
578 look_for_base
PARAMS ((int, CORE_ADDR
));
584 look_for_base -- examine file for each mapped address segment
588 static int look_for_base (int fd, CORE_ADDR baseaddr)
592 This function is passed to proc_iterate_over_mappings, which
593 causes it to get called once for each mapped address space, with
594 an open file descriptor for the file mapped to that space, and the
595 base address of that mapped space.
597 Our job is to find the debug base symbol in the file that this
598 fd is open on, if it exists, and if so, initialize the dynamic
599 linker structure base address debug_base.
601 Note that this is a computationally expensive proposition, since
602 we basically have to open a bfd on every call, so we specifically
603 avoid opening the exec file.
607 look_for_base (fd
, baseaddr
)
612 CORE_ADDR address
= 0;
615 /* If the fd is -1, then there is no file that corresponds to this
616 mapped memory segment, so skip it. Also, if the fd corresponds
617 to the exec file, skip it as well. */
621 && fdmatch (fileno ((FILE *) (exec_bfd
->iostream
)), fd
)))
626 /* Try to open whatever random file this fd corresponds to. Note that
627 we have no way currently to find the filename. Don't gripe about
628 any problems we might have, just fail. */
630 if ((interp_bfd
= bfd_fdopenr ("unnamed", gnutarget
, fd
)) == NULL
)
634 if (!bfd_check_format (interp_bfd
, bfd_object
))
636 /* FIXME-leak: on failure, might not free all memory associated with
638 bfd_close (interp_bfd
);
642 /* Now try to find our debug base symbol in this file, which we at
643 least know to be a valid ELF executable or shared library. */
645 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
647 address
= bfd_lookup_symbol (interp_bfd
, *symbolp
);
655 /* FIXME-leak: on failure, might not free all memory associated with
657 bfd_close (interp_bfd
);
661 /* Eureka! We found the symbol. But now we may need to relocate it
662 by the base address. If the symbol's value is less than the base
663 address of the shared library, then it hasn't yet been relocated
664 by the dynamic linker, and we have to do it ourself. FIXME: Note
665 that we make the assumption that the first segment that corresponds
666 to the shared library has the base address to which the library
669 if (address
< baseaddr
)
673 debug_base
= address
;
674 /* FIXME-leak: on failure, might not free all memory associated with
676 bfd_close (interp_bfd
);
679 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
685 elf_locate_base -- locate the base address of dynamic linker structs
686 for SVR4 elf targets.
690 CORE_ADDR elf_locate_base (void)
694 For SVR4 elf targets the address of the dynamic linker's runtime
695 structure is contained within the dynamic info section in the
696 executable file. The dynamic section is also mapped into the
697 inferior address space. Because the runtime loader fills in the
698 real address before starting the inferior, we have to read in the
699 dynamic info section from the inferior address space.
700 If there are any errors while trying to find the address, we
701 silently return 0, otherwise the found address is returned.
708 sec_ptr dyninfo_sect
;
709 int dyninfo_sect_size
;
710 CORE_ADDR dyninfo_addr
;
715 /* Find the start address of the .dynamic section. */
716 dyninfo_sect
= bfd_get_section_by_name (exec_bfd
, ".dynamic");
717 if (dyninfo_sect
== NULL
)
719 dyninfo_addr
= bfd_section_vma (exec_bfd
, dyninfo_sect
);
721 /* Read in .dynamic section, silently ignore errors. */
722 dyninfo_sect_size
= bfd_section_size (exec_bfd
, dyninfo_sect
);
723 buf
= alloca (dyninfo_sect_size
);
724 if (target_read_memory (dyninfo_addr
, buf
, dyninfo_sect_size
))
727 /* Find the DT_DEBUG entry in the the .dynamic section.
728 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
729 no DT_DEBUG entries. */
731 arch_size
= bfd_elf_get_arch_size (exec_bfd
);
732 if (arch_size
== -1) /* failure */
737 for (bufend
= buf
+ dyninfo_sect_size
;
739 buf
+= sizeof (Elf32_External_Dyn
))
741 Elf32_External_Dyn
*x_dynp
= (Elf32_External_Dyn
*) buf
;
745 dyn_tag
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_tag
);
746 if (dyn_tag
== DT_NULL
)
748 else if (dyn_tag
== DT_DEBUG
)
750 dyn_ptr
= bfd_h_get_32 (exec_bfd
,
751 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
754 #ifdef DT_MIPS_RLD_MAP
755 else if (dyn_tag
== DT_MIPS_RLD_MAP
)
757 char pbuf
[TARGET_PTR_BIT
/ HOST_CHAR_BIT
];
759 /* DT_MIPS_RLD_MAP contains a pointer to the address
760 of the dynamic link structure. */
761 dyn_ptr
= bfd_h_get_32 (exec_bfd
,
762 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
763 if (target_read_memory (dyn_ptr
, pbuf
, sizeof (pbuf
)))
765 return extract_unsigned_integer (pbuf
, sizeof (pbuf
));
770 else /* 64-bit elf */
772 for (bufend
= buf
+ dyninfo_sect_size
;
774 buf
+= sizeof (Elf64_External_Dyn
))
776 Elf64_External_Dyn
*x_dynp
= (Elf64_External_Dyn
*) buf
;
780 dyn_tag
= bfd_h_get_64 (exec_bfd
, (bfd_byte
*) x_dynp
->d_tag
);
781 if (dyn_tag
== DT_NULL
)
783 else if (dyn_tag
== DT_DEBUG
)
785 dyn_ptr
= bfd_h_get_64 (exec_bfd
,
786 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
792 /* DT_DEBUG entry not found. */
796 #endif /* SVR4_SHARED_LIBS */
802 locate_base -- locate the base address of dynamic linker structs
806 CORE_ADDR locate_base (void)
810 For both the SunOS and SVR4 shared library implementations, if the
811 inferior executable has been linked dynamically, there is a single
812 address somewhere in the inferior's data space which is the key to
813 locating all of the dynamic linker's runtime structures. This
814 address is the value of the debug base symbol. The job of this
815 function is to find and return that address, or to return 0 if there
816 is no such address (the executable is statically linked for example).
818 For SunOS, the job is almost trivial, since the dynamic linker and
819 all of it's structures are statically linked to the executable at
820 link time. Thus the symbol for the address we are looking for has
821 already been added to the minimal symbol table for the executable's
822 objfile at the time the symbol file's symbols were read, and all we
823 have to do is look it up there. Note that we explicitly do NOT want
824 to find the copies in the shared library.
826 The SVR4 version is a bit more complicated because the address
827 is contained somewhere in the dynamic info section. We have to go
828 to a lot more work to discover the address of the debug base symbol.
829 Because of this complexity, we cache the value we find and return that
830 value on subsequent invocations. Note there is no copy in the
831 executable symbol tables.
839 #ifndef SVR4_SHARED_LIBS
841 struct minimal_symbol
*msymbol
;
842 CORE_ADDR address
= 0;
845 /* For SunOS, we want to limit the search for the debug base symbol to the
846 executable being debugged, since there is a duplicate named symbol in the
847 shared library. We don't want the shared library versions. */
849 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
851 msymbol
= lookup_minimal_symbol (*symbolp
, NULL
, symfile_objfile
);
852 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
854 address
= SYMBOL_VALUE_ADDRESS (msymbol
);
860 #else /* SVR4_SHARED_LIBS */
862 /* Check to see if we have a currently valid address, and if so, avoid
863 doing all this work again and just return the cached address. If
864 we have no cached address, try to locate it in the dynamic info
865 section for ELF executables. */
870 && bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
871 debug_base
= elf_locate_base ();
872 #ifdef HANDLE_SVR4_EXEC_EMULATORS
873 /* Try it the hard way for emulated executables. */
874 else if (inferior_pid
!= 0 && target_has_execution
)
875 proc_iterate_over_mappings (look_for_base
);
880 #endif /* !SVR4_SHARED_LIBS */
888 first_link_map_member -- locate first member in dynamic linker's map
892 static CORE_ADDR first_link_map_member (void)
896 Find the first element in the inferior's dynamic link map, and
897 return its address in the inferior. This function doesn't copy the
898 link map entry itself into our address space; current_sos actually
902 first_link_map_member ()
906 #ifndef SVR4_SHARED_LIBS
908 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
909 if (dynamic_copy
.ld_version
>= 2)
911 /* It is a version that we can deal with, so read in the secondary
912 structure and find the address of the link map list from it. */
913 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ld_un
.ld_2
),
914 (char *) &ld_2_copy
, sizeof (struct link_dynamic_2
));
915 lm
= SOLIB_EXTRACT_ADDRESS (ld_2_copy
.ld_loaded
);
918 #else /* SVR4_SHARED_LIBS */
920 read_memory (debug_base
, (char *) &debug_copy
, sizeof (struct r_debug
));
921 /* FIXME: Perhaps we should validate the info somehow, perhaps by
922 checking r_version for a known version number, or r_state for
924 lm
= SOLIB_EXTRACT_ADDRESS (debug_copy
.r_map
);
926 #endif /* !SVR4_SHARED_LIBS */
931 #ifdef SVR4_SHARED_LIBS
936 open_symbol_file_object
940 void open_symbol_file_object (int from_tty)
944 If no open symbol file, attempt to locate and open the main symbol
945 file. On SVR4 systems, this is the first link map entry. If its
946 name is here, we can open it. Useful when attaching to a process
947 without first loading its symbol file.
952 open_symbol_file_object (from_ttyp
)
953 int *from_ttyp
; /* sneak past catch_errors */
956 struct link_map lmcopy
;
961 if (!query ("Attempt to reload symbols from process? "))
964 if ((debug_base
= locate_base ()) == 0)
965 return 0; /* failed somehow... */
967 /* First link map member should be the executable. */
968 if ((lm
= first_link_map_member ()) == 0)
969 return 0; /* failed somehow... */
971 /* Read from target memory to GDB. */
972 read_memory (lm
, (void *) &lmcopy
, sizeof (lmcopy
));
974 if (lmcopy
.l_name
== 0)
975 return 0; /* no filename. */
977 /* Now fetch the filename from target memory. */
978 target_read_string (SOLIB_EXTRACT_ADDRESS (lmcopy
.l_name
), &filename
,
979 MAX_PATH_SIZE
- 1, &errcode
);
982 warning ("failed to read exec filename from attached file: %s",
983 safe_strerror (errcode
));
987 make_cleanup ((make_cleanup_func
) free
, (void *) filename
);
988 /* Have a pathname: read the symbol file. */
989 symbol_file_command (filename
, *from_ttyp
);
993 #endif /* SVR4_SHARED_LIBS */
998 free_so --- free a `struct so_list' object
1002 void free_so (struct so_list *so)
1006 Free the storage associated with the `struct so_list' object SO.
1007 If we have opened a BFD for SO, close it.
1009 The caller is responsible for removing SO from whatever list it is
1010 a member of. If we have placed SO's sections in some target's
1011 section table, the caller is responsible for removing them.
1013 This function doesn't mess with objfiles at all. If there is an
1014 objfile associated with SO that needs to be removed, the caller is
1015 responsible for taking care of that. */
1018 free_so (struct so_list
*so
)
1020 char *bfd_filename
= 0;
1023 free (so
->sections
);
1027 bfd_filename
= bfd_get_filename (so
->abfd
);
1028 if (! bfd_close (so
->abfd
))
1029 warning ("cannot close \"%s\": %s",
1030 bfd_filename
, bfd_errmsg (bfd_get_error ()));
1034 free (bfd_filename
);
1040 /* On some systems, the only way to recognize the link map entry for
1041 the main executable file is by looking at its name. Return
1042 non-zero iff SONAME matches one of the known main executable names. */
1050 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
1052 if (strcmp (soname
, *mainp
) == 0)
1062 current_sos -- build a list of currently loaded shared objects
1066 struct so_list *current_sos ()
1070 Build a list of `struct so_list' objects describing the shared
1071 objects currently loaded in the inferior. This list does not
1072 include an entry for the main executable file.
1074 Note that we only gather information directly available from the
1075 inferior --- we don't examine any of the shared library files
1076 themselves. The declaration of `struct so_list' says which fields
1077 we provide values for. */
1079 static struct so_list
*
1083 struct so_list
*head
= 0;
1084 struct so_list
**link_ptr
= &head
;
1086 /* Make sure we've looked up the inferior's dynamic linker's base
1090 debug_base
= locate_base ();
1092 /* If we can't find the dynamic linker's base structure, this
1093 must not be a dynamically linked executable. Hmm. */
1098 /* Walk the inferior's link map list, and build our list of
1099 `struct so_list' nodes. */
1100 lm
= first_link_map_member ();
1104 = (struct so_list
*) xmalloc (sizeof (struct so_list
));
1105 struct cleanup
*old_chain
= make_cleanup (free
, new);
1106 memset (new, 0, sizeof (*new));
1109 read_memory (lm
, (char *) &(new->lm
), sizeof (struct link_map
));
1113 /* For SVR4 versions, the first entry in the link map is for the
1114 inferior executable, so we must ignore it. For some versions of
1115 SVR4, it has no name. For others (Solaris 2.3 for example), it
1116 does have a name, so we can no longer use a missing name to
1117 decide when to ignore it. */
1118 if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
1125 /* Extract this shared object's name. */
1126 target_read_string (LM_NAME (new), &buffer
,
1127 MAX_PATH_SIZE
- 1, &errcode
);
1130 warning ("current_sos: Can't read pathname for load map: %s\n",
1131 safe_strerror (errcode
));
1135 strncpy (new->so_name
, buffer
, MAX_PATH_SIZE
- 1);
1136 new->so_name
[MAX_PATH_SIZE
- 1] = '\0';
1138 strcpy (new->so_original_name
, new->so_name
);
1141 /* If this entry has no name, or its name matches the name
1142 for the main executable, don't include it in the list. */
1143 if (! new->so_name
[0]
1144 || match_main (new->so_name
))
1150 link_ptr
= &new->next
;
1154 discard_cleanups (old_chain
);
1161 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
1164 symbol_add_stub (arg
)
1167 register struct so_list
*so
= (struct so_list
*) arg
; /* catch_errs bogon */
1168 struct section_addr_info
*sap
;
1169 CORE_ADDR lowest_addr
= 0;
1171 asection
*lowest_sect
= NULL
;
1173 /* Have we already loaded this shared object? */
1174 ALL_OBJFILES (so
->objfile
)
1176 if (strcmp (so
->objfile
->name
, so
->so_name
) == 0)
1180 /* Find the shared object's text segment. */
1181 if (so
->textsection
)
1183 lowest_addr
= so
->textsection
->addr
;
1184 lowest_sect
= bfd_get_section_by_name (so
->abfd
, ".text");
1185 lowest_index
= lowest_sect
->index
;
1187 else if (so
->abfd
!= NULL
)
1189 /* If we didn't find a mapped non zero sized .text section, set
1190 up lowest_addr so that the relocation in symbol_file_add does
1192 lowest_sect
= bfd_get_section_by_name (so
->abfd
, ".text");
1193 if (lowest_sect
== NULL
)
1194 bfd_map_over_sections (so
->abfd
, find_lowest_section
,
1195 (PTR
) &lowest_sect
);
1198 lowest_addr
= bfd_section_vma (so
->abfd
, lowest_sect
)
1200 lowest_index
= lowest_sect
->index
;
1204 sap
= build_section_addr_info_from_section_table (so
->sections
,
1207 sap
->other
[lowest_index
].addr
= lowest_addr
;
1209 so
->objfile
= symbol_file_add (so
->so_name
, so
->from_tty
,
1210 sap
, 0, OBJF_SHARED
);
1211 free_section_addr_info (sap
);
1219 update_solib_list --- synchronize GDB's shared object list with inferior's
1223 void update_solib_list (int from_tty, struct target_ops *TARGET)
1225 Extract the list of currently loaded shared objects from the
1226 inferior, and compare it with the list of shared objects currently
1227 in GDB's so_list_head list. Edit so_list_head to bring it in sync
1228 with the inferior's new list.
1230 If we notice that the inferior has unloaded some shared objects,
1231 free any symbolic info GDB had read about those shared objects.
1233 Don't load symbolic info for any new shared objects; just add them
1234 to the list, and leave their symbols_loaded flag clear.
1236 If FROM_TTY is non-null, feel free to print messages about what
1239 If TARGET is non-null, add the sections of all new shared objects
1240 to TARGET's section table. Note that this doesn't remove any
1241 sections for shared objects that have been unloaded, and it
1242 doesn't check to see if the new shared objects are already present in
1243 the section table. But we only use this for core files and
1244 processes we've just attached to, so that's okay. */
1247 update_solib_list (int from_tty
, struct target_ops
*target
)
1249 struct so_list
*inferior
= current_sos ();
1250 struct so_list
*gdb
, **gdb_link
;
1252 #ifdef SVR4_SHARED_LIBS
1253 /* If we are attaching to a running process for which we
1254 have not opened a symbol file, we may be able to get its
1257 symfile_objfile
== NULL
)
1258 catch_errors (open_symbol_file_object
, (PTR
) &from_tty
,
1259 "Error reading attached process's symbol file.\n",
1262 #endif SVR4_SHARED_LIBS
1264 /* Since this function might actually add some elements to the
1265 so_list_head list, arrange for it to be cleaned up when
1267 if (!solib_cleanup_queued
)
1269 make_run_cleanup (do_clear_solib
, NULL
);
1270 solib_cleanup_queued
= 1;
1273 /* GDB and the inferior's dynamic linker each maintain their own
1274 list of currently loaded shared objects; we want to bring the
1275 former in sync with the latter. Scan both lists, seeing which
1276 shared objects appear where. There are three cases:
1278 - A shared object appears on both lists. This means that GDB
1279 knows about it already, and it's still loaded in the inferior.
1280 Nothing needs to happen.
1282 - A shared object appears only on GDB's list. This means that
1283 the inferior has unloaded it. We should remove the shared
1284 object from GDB's tables.
1286 - A shared object appears only on the inferior's list. This
1287 means that it's just been loaded. We should add it to GDB's
1290 So we walk GDB's list, checking each entry to see if it appears
1291 in the inferior's list too. If it does, no action is needed, and
1292 we remove it from the inferior's list. If it doesn't, the
1293 inferior has unloaded it, and we remove it from GDB's list. By
1294 the time we're done walking GDB's list, the inferior's list
1295 contains only the new shared objects, which we then add. */
1298 gdb_link
= &so_list_head
;
1301 struct so_list
*i
= inferior
;
1302 struct so_list
**i_link
= &inferior
;
1304 /* Check to see whether the shared object *gdb also appears in
1305 the inferior's current list. */
1308 if (! strcmp (gdb
->so_original_name
, i
->so_original_name
))
1315 /* If the shared object appears on the inferior's list too, then
1316 it's still loaded, so we don't need to do anything. Delete
1317 it from the inferior's list, and leave it on GDB's list. */
1322 gdb_link
= &gdb
->next
;
1326 /* If it's not on the inferior's list, remove it from GDB's tables. */
1329 *gdb_link
= gdb
->next
;
1331 /* Unless the user loaded it explicitly, free SO's objfile. */
1332 if (gdb
->objfile
&& ! (gdb
->objfile
->flags
& OBJF_USERLOADED
))
1333 free_objfile (gdb
->objfile
);
1335 /* Some targets' section tables might be referring to
1336 sections from so->abfd; remove them. */
1337 remove_target_sections (gdb
->abfd
);
1344 /* Now the inferior's list contains only shared objects that don't
1345 appear in GDB's list --- those that are newly loaded. Add them
1346 to GDB's shared object list. */
1351 /* Add the new shared objects to GDB's list. */
1352 *gdb_link
= inferior
;
1354 /* Fill in the rest of each of the `struct so_list' nodes. */
1355 for (i
= inferior
; i
; i
= i
->next
)
1357 i
->from_tty
= from_tty
;
1359 /* Fill in the rest of the `struct so_list' node. */
1360 catch_errors (solib_map_sections
, i
,
1361 "Error while mapping shared library sections:\n",
1365 /* If requested, add the shared objects' sections to the the
1366 TARGET's section table. */
1371 /* Figure out how many sections we'll need to add in total. */
1373 for (i
= inferior
; i
; i
= i
->next
)
1374 new_sections
+= (i
->sections_end
- i
->sections
);
1376 if (new_sections
> 0)
1378 int space
= target_resize_to_sections (target
, new_sections
);
1380 for (i
= inferior
; i
; i
= i
->next
)
1382 int count
= (i
->sections_end
- i
->sections
);
1383 memcpy (target
->to_sections
+ space
,
1385 count
* sizeof (i
->sections
[0]));
1396 solib_add -- read in symbol info for newly added shared libraries
1400 void solib_add (char *pattern, int from_tty, struct target_ops *TARGET)
1404 Read in symbolic information for any shared objects whose names
1405 match PATTERN. (If we've already read a shared object's symbol
1406 info, leave it alone.) If PATTERN is zero, read them all.
1408 FROM_TTY and TARGET are as described for update_solib_list, above. */
1411 solib_add (char *pattern
, int from_tty
, struct target_ops
*target
)
1413 struct so_list
*gdb
;
1417 char *re_err
= re_comp (pattern
);
1420 error ("Invalid regexp: %s", re_err
);
1423 update_solib_list (from_tty
, target
);
1425 /* Walk the list of currently loaded shared libraries, and read
1426 symbols for any that match the pattern --- or any whose symbols
1427 aren't already loaded, if no pattern was given. */
1429 int any_matches
= 0;
1430 int loaded_any_symbols
= 0;
1432 for (gdb
= so_list_head
; gdb
; gdb
= gdb
->next
)
1433 if (! pattern
|| re_exec (gdb
->so_name
))
1437 if (gdb
->symbols_loaded
)
1440 printf_unfiltered ("Symbols already loaded for %s\n",
1446 (symbol_add_stub
, gdb
,
1447 "Error while reading shared library symbols:\n",
1451 printf_unfiltered ("Loaded symbols for %s\n",
1453 gdb
->symbols_loaded
= 1;
1454 loaded_any_symbols
= 1;
1459 if (from_tty
&& pattern
&& ! any_matches
)
1461 ("No loaded shared libraries match the pattern `%s'.\n", pattern
);
1463 if (loaded_any_symbols
)
1465 /* Getting new symbols may change our opinion about what is
1467 reinit_frame_cache ();
1469 special_symbol_handling ();
1479 info_sharedlibrary_command -- code for "info sharedlibrary"
1483 static void info_sharedlibrary_command ()
1487 Walk through the shared library list and print information
1488 about each attached library.
1492 info_sharedlibrary_command (ignore
, from_tty
)
1496 register struct so_list
*so
= NULL
; /* link map state variable */
1497 int header_done
= 0;
1502 if (exec_bfd
== NULL
)
1504 printf_unfiltered ("No executable file.\n");
1508 arch_size
= bfd_elf_get_arch_size (exec_bfd
);
1509 /* Default to 32-bit in case of failure (non-elf). */
1510 if (arch_size
== 32 || arch_size
== -1)
1515 else if (arch_size
== 64)
1517 addr_width
= 16 + 4;
1521 update_solib_list (from_tty
, 0);
1523 for (so
= so_list_head
; so
; so
= so
->next
)
1529 printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width
, "From",
1530 addr_width
, "To", "Syms Read",
1531 "Shared Object Library");
1535 printf_unfiltered ("%-*s", addr_width
,
1536 local_hex_string_custom ((unsigned long) LM_ADDR (so
),
1538 printf_unfiltered ("%-*s", addr_width
,
1539 local_hex_string_custom ((unsigned long) so
->lmend
,
1541 printf_unfiltered ("%-12s", so
->symbols_loaded
? "Yes" : "No");
1542 printf_unfiltered ("%s\n", so
->so_name
);
1545 if (so_list_head
== NULL
)
1547 printf_unfiltered ("No shared libraries loaded at this time.\n");
1555 solib_address -- check to see if an address is in a shared lib
1559 char * solib_address (CORE_ADDR address)
1563 Provides a hook for other gdb routines to discover whether or
1564 not a particular address is within the mapped address space of
1565 a shared library. Any address between the base mapping address
1566 and the first address beyond the end of the last mapping, is
1567 considered to be within the shared library address space, for
1570 For example, this routine is called at one point to disable
1571 breakpoints which are in shared libraries that are not currently
1576 solib_address (address
)
1579 register struct so_list
*so
= 0; /* link map state variable */
1581 for (so
= so_list_head
; so
; so
= so
->next
)
1583 if (LM_ADDR (so
) <= address
&& address
< so
->lmend
)
1584 return (so
->so_name
);
1590 /* Called by free_all_symtabs */
1595 /* This function is expected to handle ELF shared libraries. It is
1596 also used on Solaris, which can run either ELF or a.out binaries
1597 (for compatibility with SunOS 4), both of which can use shared
1598 libraries. So we don't know whether we have an ELF executable or
1599 an a.out executable until the user chooses an executable file.
1601 ELF shared libraries don't get mapped into the address space
1602 until after the program starts, so we'd better not try to insert
1603 breakpoints in them immediately. We have to wait until the
1604 dynamic linker has loaded them; we'll hit a bp_shlib_event
1605 breakpoint (look for calls to create_solib_event_breakpoint) when
1608 SunOS shared libraries seem to be different --- they're present
1609 as soon as the process begins execution, so there's no need to
1610 put off inserting breakpoints. There's also nowhere to put a
1611 bp_shlib_event breakpoint, so if we put it off, we'll never get
1614 So: disable breakpoints only if we're using ELF shared libs. */
1615 if (exec_bfd
!= NULL
1616 && bfd_get_flavour (exec_bfd
) != bfd_target_aout_flavour
)
1617 disable_breakpoints_in_shlibs (1);
1619 while (so_list_head
)
1621 struct so_list
*so
= so_list_head
;
1622 so_list_head
= so
->next
;
1630 do_clear_solib (dummy
)
1633 solib_cleanup_queued
= 0;
1637 #ifdef SVR4_SHARED_LIBS
1639 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1640 SVR4 run time loader. */
1642 static CORE_ADDR interp_text_sect_low
;
1643 static CORE_ADDR interp_text_sect_high
;
1644 static CORE_ADDR interp_plt_sect_low
;
1645 static CORE_ADDR interp_plt_sect_high
;
1648 in_svr4_dynsym_resolve_code (pc
)
1651 return ((pc
>= interp_text_sect_low
&& pc
< interp_text_sect_high
)
1652 || (pc
>= interp_plt_sect_low
&& pc
< interp_plt_sect_high
)
1653 || in_plt_section (pc
, NULL
));
1661 disable_break -- remove the "mapping changed" breakpoint
1665 static int disable_break ()
1669 Removes the breakpoint that gets hit when the dynamic linker
1670 completes a mapping change.
1674 #ifndef SVR4_SHARED_LIBS
1681 #ifndef SVR4_SHARED_LIBS
1683 int in_debugger
= 0;
1685 /* Read the debugger structure from the inferior to retrieve the
1686 address of the breakpoint and the original contents of the
1687 breakpoint address. Remove the breakpoint by writing the original
1690 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
1692 /* Set `in_debugger' to zero now. */
1694 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1696 breakpoint_addr
= SOLIB_EXTRACT_ADDRESS (debug_copy
.ldd_bp_addr
);
1697 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
1698 sizeof (debug_copy
.ldd_bp_inst
));
1700 #else /* SVR4_SHARED_LIBS */
1702 /* Note that breakpoint address and original contents are in our address
1703 space, so we just need to write the original contents back. */
1705 if (memory_remove_breakpoint (breakpoint_addr
, shadow_contents
) != 0)
1710 #endif /* !SVR4_SHARED_LIBS */
1712 /* For the SVR4 version, we always know the breakpoint address. For the
1713 SunOS version we don't know it until the above code is executed.
1714 Grumble if we are stopped anywhere besides the breakpoint address. */
1716 if (stop_pc
!= breakpoint_addr
)
1718 warning ("stopped at unknown breakpoint while handling shared libraries");
1724 #endif /* #ifdef SVR4_SHARED_LIBS */
1730 enable_break -- arrange for dynamic linker to hit breakpoint
1734 int enable_break (void)
1738 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1739 debugger interface, support for arranging for the inferior to hit
1740 a breakpoint after mapping in the shared libraries. This function
1741 enables that breakpoint.
1743 For SunOS, there is a special flag location (in_debugger) which we
1744 set to 1. When the dynamic linker sees this flag set, it will set
1745 a breakpoint at a location known only to itself, after saving the
1746 original contents of that place and the breakpoint address itself,
1747 in it's own internal structures. When we resume the inferior, it
1748 will eventually take a SIGTRAP when it runs into the breakpoint.
1749 We handle this (in a different place) by restoring the contents of
1750 the breakpointed location (which is only known after it stops),
1751 chasing around to locate the shared libraries that have been
1752 loaded, then resuming.
1754 For SVR4, the debugger interface structure contains a member (r_brk)
1755 which is statically initialized at the time the shared library is
1756 built, to the offset of a function (_r_debug_state) which is guaran-
1757 teed to be called once before mapping in a library, and again when
1758 the mapping is complete. At the time we are examining this member,
1759 it contains only the unrelocated offset of the function, so we have
1760 to do our own relocation. Later, when the dynamic linker actually
1761 runs, it relocates r_brk to be the actual address of _r_debug_state().
1763 The debugger interface structure also contains an enumeration which
1764 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1765 depending upon whether or not the library is being mapped or unmapped,
1766 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1774 #ifndef SVR4_SHARED_LIBS
1779 /* Get link_dynamic structure */
1781 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1782 sizeof (dynamic_copy
));
1789 /* Calc address of debugger interface structure */
1791 debug_addr
= SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ldd
);
1793 /* Calc address of `in_debugger' member of debugger interface structure */
1795 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
1796 (char *) &debug_copy
);
1798 /* Write a value of 1 to this member. */
1801 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1804 #else /* SVR4_SHARED_LIBS */
1806 #ifdef BKPT_AT_SYMBOL
1808 struct minimal_symbol
*msymbol
;
1810 asection
*interp_sect
;
1812 /* First, remove all the solib event breakpoints. Their addresses
1813 may have changed since the last time we ran the program. */
1814 remove_solib_event_breakpoints ();
1816 #ifdef SVR4_SHARED_LIBS
1817 interp_text_sect_low
= interp_text_sect_high
= 0;
1818 interp_plt_sect_low
= interp_plt_sect_high
= 0;
1820 /* Find the .interp section; if not found, warn the user and drop
1821 into the old breakpoint at symbol code. */
1822 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
1825 unsigned int interp_sect_size
;
1827 CORE_ADDR load_addr
;
1829 CORE_ADDR sym_addr
= 0;
1831 /* Read the contents of the .interp section into a local buffer;
1832 the contents specify the dynamic linker this program uses. */
1833 interp_sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
1834 buf
= alloca (interp_sect_size
);
1835 bfd_get_section_contents (exec_bfd
, interp_sect
,
1836 buf
, 0, interp_sect_size
);
1838 /* Now we need to figure out where the dynamic linker was
1839 loaded so that we can load its symbols and place a breakpoint
1840 in the dynamic linker itself.
1842 This address is stored on the stack. However, I've been unable
1843 to find any magic formula to find it for Solaris (appears to
1844 be trivial on GNU/Linux). Therefore, we have to try an alternate
1845 mechanism to find the dynamic linker's base address. */
1846 tmp_bfd
= bfd_openr (buf
, gnutarget
);
1847 if (tmp_bfd
== NULL
)
1848 goto bkpt_at_symbol
;
1850 /* Make sure the dynamic linker's really a useful object. */
1851 if (!bfd_check_format (tmp_bfd
, bfd_object
))
1853 warning ("Unable to grok dynamic linker %s as an object file", buf
);
1854 bfd_close (tmp_bfd
);
1855 goto bkpt_at_symbol
;
1858 /* We find the dynamic linker's base address by examining the
1859 current pc (which point at the entry point for the dynamic
1860 linker) and subtracting the offset of the entry point. */
1861 load_addr
= read_pc () - tmp_bfd
->start_address
;
1863 /* Record the relocated start and end address of the dynamic linker
1864 text and plt section for in_svr4_dynsym_resolve_code. */
1865 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
1868 interp_text_sect_low
=
1869 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1870 interp_text_sect_high
=
1871 interp_text_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
1873 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
1876 interp_plt_sect_low
=
1877 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1878 interp_plt_sect_high
=
1879 interp_plt_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
1882 /* Now try to set a breakpoint in the dynamic linker. */
1883 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1885 sym_addr
= bfd_lookup_symbol (tmp_bfd
, *bkpt_namep
);
1890 /* We're done with the temporary bfd. */
1891 bfd_close (tmp_bfd
);
1895 create_solib_event_breakpoint (load_addr
+ sym_addr
);
1899 /* For whatever reason we couldn't set a breakpoint in the dynamic
1900 linker. Warn and drop into the old code. */
1902 warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
1906 /* Scan through the list of symbols, trying to look up the symbol and
1907 set a breakpoint there. Terminate loop when we/if we succeed. */
1909 breakpoint_addr
= 0;
1910 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1912 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
1913 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1915 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol
));
1920 /* Nothing good happened. */
1923 #endif /* BKPT_AT_SYMBOL */
1925 #endif /* !SVR4_SHARED_LIBS */
1934 solib_create_inferior_hook -- shared library startup support
1938 void solib_create_inferior_hook()
1942 When gdb starts up the inferior, it nurses it along (through the
1943 shell) until it is ready to execute it's first instruction. At this
1944 point, this function gets called via expansion of the macro
1945 SOLIB_CREATE_INFERIOR_HOOK.
1947 For SunOS executables, this first instruction is typically the
1948 one at "_start", or a similar text label, regardless of whether
1949 the executable is statically or dynamically linked. The runtime
1950 startup code takes care of dynamically linking in any shared
1951 libraries, once gdb allows the inferior to continue.
1953 For SVR4 executables, this first instruction is either the first
1954 instruction in the dynamic linker (for dynamically linked
1955 executables) or the instruction at "start" for statically linked
1956 executables. For dynamically linked executables, the system
1957 first exec's /lib/libc.so.N, which contains the dynamic linker,
1958 and starts it running. The dynamic linker maps in any needed
1959 shared libraries, maps in the actual user executable, and then
1960 jumps to "start" in the user executable.
1962 For both SunOS shared libraries, and SVR4 shared libraries, we
1963 can arrange to cooperate with the dynamic linker to discover the
1964 names of shared libraries that are dynamically linked, and the
1965 base addresses to which they are linked.
1967 This function is responsible for discovering those names and
1968 addresses, and saving sufficient information about them to allow
1969 their symbols to be read at a later time.
1973 Between enable_break() and disable_break(), this code does not
1974 properly handle hitting breakpoints which the user might have
1975 set in the startup code or in the dynamic linker itself. Proper
1976 handling will probably have to wait until the implementation is
1977 changed to use the "breakpoint handler function" method.
1979 Also, what if child has exit()ed? Must exit loop somehow.
1983 solib_create_inferior_hook ()
1985 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1986 yet. In fact, in the case of a SunOS4 executable being run on
1987 Solaris, we can't get it yet. current_sos will get it when it needs
1989 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1990 if ((debug_base
= locate_base ()) == 0)
1992 /* Can't find the symbol or the executable is statically linked. */
1997 if (!enable_break ())
1999 warning ("shared library handler failed to enable breakpoint");
2003 #if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS)
2004 /* SCO and SunOS need the loop below, other systems should be using the
2005 special shared library breakpoints and the shared library breakpoint
2008 Now run the target. It will eventually hit the breakpoint, at
2009 which point all of the libraries will have been mapped in and we
2010 can go groveling around in the dynamic linker structures to find
2011 out what we need to know about them. */
2013 clear_proceed_status ();
2014 stop_soon_quietly
= 1;
2015 stop_signal
= TARGET_SIGNAL_0
;
2018 target_resume (-1, 0, stop_signal
);
2019 wait_for_inferior ();
2021 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
2022 stop_soon_quietly
= 0;
2024 #if !defined(_SCO_DS)
2025 /* We are now either at the "mapping complete" breakpoint (or somewhere
2026 else, a condition we aren't prepared to deal with anyway), so adjust
2027 the PC as necessary after a breakpoint, disable the breakpoint, and
2028 add any shared libraries that were mapped in. */
2030 if (DECR_PC_AFTER_BREAK
)
2032 stop_pc
-= DECR_PC_AFTER_BREAK
;
2033 write_register (PC_REGNUM
, stop_pc
);
2036 if (!disable_break ())
2038 warning ("shared library handler failed to disable breakpoint");
2042 solib_add ((char *) 0, 0, (struct target_ops
*) 0);
2043 #endif /* ! _SCO_DS */
2051 special_symbol_handling -- additional shared library symbol handling
2055 void special_symbol_handling ()
2059 Once the symbols from a shared object have been loaded in the usual
2060 way, we are called to do any system specific symbol handling that
2063 For SunOS4, this consists of grunging around in the dynamic
2064 linkers structures to find symbol definitions for "common" symbols
2065 and adding them to the minimal symbol table for the runtime common
2071 special_symbol_handling ()
2073 #ifndef SVR4_SHARED_LIBS
2076 if (debug_addr
== 0)
2078 /* Get link_dynamic structure */
2080 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
2081 sizeof (dynamic_copy
));
2088 /* Calc address of debugger interface structure */
2089 /* FIXME, this needs work for cross-debugging of core files
2090 (byteorder, size, alignment, etc). */
2092 debug_addr
= SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ldd
);
2095 /* Read the debugger structure from the inferior, just to make sure
2096 we have a current copy. */
2098 j
= target_read_memory (debug_addr
, (char *) &debug_copy
,
2099 sizeof (debug_copy
));
2101 return; /* unreadable */
2103 /* Get common symbol definitions for the loaded object. */
2105 if (debug_copy
.ldd_cp
)
2107 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy
.ldd_cp
));
2110 #endif /* !SVR4_SHARED_LIBS */
2118 sharedlibrary_command -- handle command to explicitly add library
2122 static void sharedlibrary_command (char *args, int from_tty)
2129 sharedlibrary_command (args
, from_tty
)
2134 solib_add (args
, from_tty
, (struct target_ops
*) 0);
2137 #endif /* HAVE_LINK_H */
2140 _initialize_solib ()
2144 add_com ("sharedlibrary", class_files
, sharedlibrary_command
,
2145 "Load shared object library symbols for files matching REGEXP.");
2146 add_info ("sharedlibrary", info_sharedlibrary_command
,
2147 "Status of loaded shared object libraries.");
2150 (add_set_cmd ("auto-solib-add", class_support
, var_zinteger
,
2151 (char *) &auto_solib_add
,
2152 "Set autoloading of shared library symbols.\n\
2153 If nonzero, symbols from all shared object libraries will be loaded\n\
2154 automatically when the inferior begins execution or when the dynamic linker\n\
2155 informs gdb that a new library has been loaded. Otherwise, symbols\n\
2156 must be loaded manually, using `sharedlibrary'.",
2161 (add_set_cmd ("solib-absolute-prefix", class_support
, var_filename
,
2162 (char *) &solib_absolute_prefix
,
2163 "Set prefix for loading absolute shared library symbol files.\n\
2164 For other (relative) files, you can add values using `set solib-search-path'.",
2168 (add_set_cmd ("solib-search-path", class_support
, var_string
,
2169 (char *) &solib_search_path
,
2170 "Set the search path for loading non-absolute shared library symbol files.\n\
2171 This takes precedence over the environment variables PATH and LD_LIBRARY_PATH.",
2175 #endif /* HAVE_LINK_H */