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 /* This file is only compilable if link.h is available. */
28 #include <sys/types.h>
30 #include "gdb_string.h"
31 #include <sys/param.h>
35 #ifndef SVR4_SHARED_LIBS
36 /* SunOS shared libs need the nlist structure. */
39 #include "elf/external.h"
40 #include "elf/common.h"
53 #include "gnu-regex.h"
59 #define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */
61 /* On SVR4 systems, a list of symbols in the dynamic linker where
62 GDB can try to place a breakpoint to monitor shared library
65 If none of these symbols are found, or other errors occur, then
66 SVR4 systems will fall back to using a symbol as the "startup
67 mapping complete" breakpoint address. */
69 #ifdef SVR4_SHARED_LIBS
70 static char *solib_break_names[] = {
78 #define BKPT_AT_SYMBOL 1
80 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
81 static char *bkpt_names[] = {
82 #ifdef SOLIB_BKPT_NAME
83 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
91 /* Symbols which are used to locate the base of the link map structures. */
93 #ifndef SVR4_SHARED_LIBS
94 static char *debug_base_symbols[] = {
101 static char *main_name_list[] = {
106 /* local data declarations */
108 #ifndef SVR4_SHARED_LIBS
110 #define LM_ADDR(so) ((so) -> lm.lm_addr)
111 #define LM_NEXT(so) ((so) -> lm.lm_next)
112 #define LM_NAME(so) ((so) -> lm.lm_name)
113 /* Test for first link map entry; first entry is a shared library. */
114 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
115 static struct link_dynamic dynamic_copy;
116 static struct link_dynamic_2 ld_2_copy;
117 static struct ld_debug debug_copy;
118 static CORE_ADDR debug_addr;
119 static CORE_ADDR flag_addr;
121 #else /* SVR4_SHARED_LIBS */
123 #define LM_ADDR(so) ((so) -> lm.l_addr)
124 #define LM_NEXT(so) ((so) -> lm.l_next)
125 #define LM_NAME(so) ((so) -> lm.l_name)
126 /* Test for first link map entry; first entry is the exec-file. */
127 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
128 static struct r_debug debug_copy;
129 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
131 #endif /* !SVR4_SHARED_LIBS */
134 struct so_list *next; /* next structure in linked list */
135 struct link_map lm; /* copy of link map from inferior */
136 struct link_map *lmaddr; /* addr in inferior lm was read from */
137 CORE_ADDR lmend; /* upper addr bound of mapped object */
138 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
139 char symbols_loaded; /* flag: symbols read in yet? */
140 char from_tty; /* flag: print msgs? */
141 struct objfile *objfile; /* objfile for loaded lib */
142 struct section_table *sections;
143 struct section_table *sections_end;
144 struct section_table *textsection;
148 static struct so_list *so_list_head; /* List of known shared objects */
149 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
150 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
153 fdmatch PARAMS ((int, int)); /* In libiberty */
155 /* Local function prototypes */
158 special_symbol_handling PARAMS ((struct so_list *));
161 sharedlibrary_command PARAMS ((char *, int));
164 enable_break PARAMS ((void));
167 info_sharedlibrary_command PARAMS ((char *, int));
170 symbol_add_stub PARAMS ((char *));
172 static struct so_list *
173 alloc_solib PARAMS ((struct link_map *));
176 free_solib PARAMS ((struct so_list *));
178 static struct so_list *
179 find_solib PARAMS ((struct so_list *, int maybe_changed));
181 static struct link_map *
182 first_link_map_member PARAMS ((void));
185 locate_base PARAMS ((void));
188 solib_map_sections PARAMS ((struct so_list *));
190 #ifdef SVR4_SHARED_LIBS
193 elf_locate_base PARAMS ((void));
198 disable_break PARAMS ((void));
201 allocate_rt_common_objfile PARAMS ((void));
204 solib_add_common_symbols PARAMS ((struct rtc_symb *));
212 solib_map_sections -- open bfd and build sections for shared lib
216 static void solib_map_sections (struct so_list *so)
220 Given a pointer to one of the shared objects in our list
221 of mapped objects, use the recorded name to open a bfd
222 descriptor for the object, build a section table, and then
223 relocate all the section addresses by the base address at
224 which the shared object was mapped.
228 In most (all?) cases the shared object file name recorded in the
229 dynamic linkage tables will be a fully qualified pathname. For
230 cases where it isn't, do we really mimic the systems search
231 mechanism correctly in the below code (particularly the tilde
236 solib_map_sections (so)
240 char *scratch_pathname;
242 struct section_table *p;
243 struct cleanup *old_chain;
246 filename = tilde_expand (so -> so_name);
247 old_chain = make_cleanup (free, filename);
249 scratch_chan = openp (get_in_environ (inferior_environ, "PATH"),
250 1, filename, O_RDONLY, 0, &scratch_pathname);
251 if (scratch_chan < 0)
253 scratch_chan = openp (get_in_environ
254 (inferior_environ, "LD_LIBRARY_PATH"),
255 1, filename, O_RDONLY, 0, &scratch_pathname);
257 if (scratch_chan < 0)
259 perror_with_name (filename);
261 /* Leave scratch_pathname allocated. abfd->name will point to it. */
263 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
266 close (scratch_chan);
267 error ("Could not open `%s' as an executable file: %s",
268 scratch_pathname, bfd_errmsg (bfd_get_error ()));
270 /* Leave bfd open, core_xfer_memory and "info files" need it. */
272 abfd -> cacheable = true;
274 /* copy full path name into so_name, so that later symbol_file_add can find
276 if (strlen (scratch_pathname) >= MAX_PATH_SIZE)
277 error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
278 strcpy (so->so_name, scratch_pathname);
280 if (!bfd_check_format (abfd, bfd_object))
282 error ("\"%s\": not in executable format: %s.",
283 scratch_pathname, bfd_errmsg (bfd_get_error ()));
285 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
287 error ("Can't find the file sections in `%s': %s",
288 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
291 for (p = so -> sections; p < so -> sections_end; p++)
293 /* Relocate the section binding addresses as recorded in the shared
294 object's file by the base address to which the object was actually
296 p -> addr += (CORE_ADDR) LM_ADDR (so);
297 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
298 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
299 if (STREQ (p -> the_bfd_section -> name, ".text"))
301 so -> textsection = p;
305 /* Free the file names, close the file now. */
306 do_cleanups (old_chain);
309 #ifndef SVR4_SHARED_LIBS
311 /* Allocate the runtime common object file. */
314 allocate_rt_common_objfile ()
316 struct objfile *objfile;
317 struct objfile *last_one;
319 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
320 memset (objfile, 0, sizeof (struct objfile));
321 objfile -> md = NULL;
322 obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
324 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
326 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
328 obstack_specify_allocation (&objfile -> type_obstack, 0, 0, xmalloc,
330 objfile -> name = mstrsave (objfile -> md, "rt_common");
332 /* Add this file onto the tail of the linked list of other such files. */
334 objfile -> next = NULL;
335 if (object_files == NULL)
336 object_files = objfile;
339 for (last_one = object_files;
341 last_one = last_one -> next);
342 last_one -> next = objfile;
345 rt_common_objfile = objfile;
348 /* Read all dynamically loaded common symbol definitions from the inferior
349 and put them into the minimal symbol table for the runtime common
353 solib_add_common_symbols (rtc_symp)
354 struct rtc_symb *rtc_symp;
356 struct rtc_symb inferior_rtc_symb;
357 struct nlist inferior_rtc_nlist;
362 /* Remove any runtime common symbols from previous runs. */
364 if (rt_common_objfile != NULL && rt_common_objfile -> minimal_symbol_count)
366 obstack_free (&rt_common_objfile -> symbol_obstack, 0);
367 obstack_specify_allocation (&rt_common_objfile -> symbol_obstack, 0, 0,
369 rt_common_objfile -> minimal_symbol_count = 0;
370 rt_common_objfile -> msymbols = NULL;
373 init_minimal_symbol_collection ();
374 make_cleanup (discard_minimal_symbols, 0);
378 read_memory ((CORE_ADDR) rtc_symp,
379 (char *) &inferior_rtc_symb,
380 sizeof (inferior_rtc_symb));
381 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
382 (char *) &inferior_rtc_nlist,
383 sizeof(inferior_rtc_nlist));
384 if (inferior_rtc_nlist.n_type == N_COMM)
386 /* FIXME: The length of the symbol name is not available, but in the
387 current implementation the common symbol is allocated immediately
388 behind the name of the symbol. */
389 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
391 origname = name = xmalloc (len);
392 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
394 /* Allocate the runtime common objfile if necessary. */
395 if (rt_common_objfile == NULL)
396 allocate_rt_common_objfile ();
398 name = obsavestring (name, strlen (name),
399 &rt_common_objfile -> symbol_obstack);
400 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
401 mst_bss, rt_common_objfile);
404 rtc_symp = inferior_rtc_symb.rtc_next;
407 /* Install any minimal symbols that have been collected as the current
408 minimal symbols for the runtime common objfile. */
410 install_minimal_symbols (rt_common_objfile);
413 #endif /* SVR4_SHARED_LIBS */
416 #ifdef SVR4_SHARED_LIBS
419 bfd_lookup_symbol PARAMS ((bfd *, char *));
425 bfd_lookup_symbol -- lookup the value for a specific symbol
429 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
433 An expensive way to lookup the value of a single symbol for
434 bfd's that are only temporary anyway. This is used by the
435 shared library support to find the address of the debugger
436 interface structures in the shared library.
438 Note that 0 is specifically allowed as an error return (no
443 bfd_lookup_symbol (abfd, symname)
447 unsigned int storage_needed;
449 asymbol **symbol_table;
450 unsigned int number_of_symbols;
452 struct cleanup *back_to;
453 CORE_ADDR symaddr = 0;
455 storage_needed = bfd_get_symtab_upper_bound (abfd);
457 if (storage_needed > 0)
459 symbol_table = (asymbol **) xmalloc (storage_needed);
460 back_to = make_cleanup (free, (PTR)symbol_table);
461 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
463 for (i = 0; i < number_of_symbols; i++)
465 sym = *symbol_table++;
466 if (STREQ (sym -> name, symname))
468 /* Bfd symbols are section relative. */
469 symaddr = sym -> value + sym -> section -> vma;
473 do_cleanups (back_to);
476 if (symaddr) return (symaddr);
478 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
480 if (storage_needed > 0)
482 symbol_table = (asymbol **) xmalloc (storage_needed);
483 back_to = make_cleanup (free, (PTR)symbol_table);
484 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
486 for (i = 0; i < number_of_symbols; i++)
488 sym = *symbol_table++;
489 if (STREQ (sym -> name, symname))
491 /* Bfd symbols are section relative. */
492 symaddr = sym -> value + sym -> section -> vma;
496 do_cleanups (back_to);
501 #ifdef HANDLE_SVR4_EXEC_EMULATORS
504 Solaris BCP (the part of Solaris which allows it to run SunOS4
505 a.out files) throws in another wrinkle. Solaris does not fill
506 in the usual a.out link map structures when running BCP programs,
507 the only way to get at them is via groping around in the dynamic
509 The dynamic linker and it's structures are located in the shared
510 C library, which gets run as the executable's "interpreter" by
513 Note that we can assume nothing about the process state at the time
514 we need to find these structures. We may be stopped on the first
515 instruction of the interpreter (C shared library), the first
516 instruction of the executable itself, or somewhere else entirely
517 (if we attached to the process for example).
520 static char *debug_base_symbols[] = {
521 "r_debug", /* Solaris 2.3 */
522 "_r_debug", /* Solaris 2.1, 2.2 */
527 look_for_base PARAMS ((int, CORE_ADDR));
533 look_for_base -- examine file for each mapped address segment
537 static int look_for_base (int fd, CORE_ADDR baseaddr)
541 This function is passed to proc_iterate_over_mappings, which
542 causes it to get called once for each mapped address space, with
543 an open file descriptor for the file mapped to that space, and the
544 base address of that mapped space.
546 Our job is to find the debug base symbol in the file that this
547 fd is open on, if it exists, and if so, initialize the dynamic
548 linker structure base address debug_base.
550 Note that this is a computationally expensive proposition, since
551 we basically have to open a bfd on every call, so we specifically
552 avoid opening the exec file.
556 look_for_base (fd, baseaddr)
561 CORE_ADDR address = 0;
564 /* If the fd is -1, then there is no file that corresponds to this
565 mapped memory segment, so skip it. Also, if the fd corresponds
566 to the exec file, skip it as well. */
570 && fdmatch (fileno ((GDB_FILE *)(exec_bfd -> iostream)), fd)))
575 /* Try to open whatever random file this fd corresponds to. Note that
576 we have no way currently to find the filename. Don't gripe about
577 any problems we might have, just fail. */
579 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
583 if (!bfd_check_format (interp_bfd, bfd_object))
585 /* FIXME-leak: on failure, might not free all memory associated with
587 bfd_close (interp_bfd);
591 /* Now try to find our debug base symbol in this file, which we at
592 least know to be a valid ELF executable or shared library. */
594 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
596 address = bfd_lookup_symbol (interp_bfd, *symbolp);
604 /* FIXME-leak: on failure, might not free all memory associated with
606 bfd_close (interp_bfd);
610 /* Eureka! We found the symbol. But now we may need to relocate it
611 by the base address. If the symbol's value is less than the base
612 address of the shared library, then it hasn't yet been relocated
613 by the dynamic linker, and we have to do it ourself. FIXME: Note
614 that we make the assumption that the first segment that corresponds
615 to the shared library has the base address to which the library
618 if (address < baseaddr)
622 debug_base = address;
623 /* FIXME-leak: on failure, might not free all memory associated with
625 bfd_close (interp_bfd);
628 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
634 elf_locate_base -- locate the base address of dynamic linker structs
635 for SVR4 elf targets.
639 CORE_ADDR elf_locate_base (void)
643 For SVR4 elf targets the address of the dynamic linker's runtime
644 structure is contained within the dynamic info section in the
645 executable file. The dynamic section is also mapped into the
646 inferior address space. Because the runtime loader fills in the
647 real address before starting the inferior, we have to read in the
648 dynamic info section from the inferior address space.
649 If there are any errors while trying to find the address, we
650 silently return 0, otherwise the found address is returned.
657 sec_ptr dyninfo_sect;
658 int dyninfo_sect_size;
659 CORE_ADDR dyninfo_addr;
663 /* Find the start address of the .dynamic section. */
664 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
665 if (dyninfo_sect == NULL)
667 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
669 /* Read in .dynamic section, silently ignore errors. */
670 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
671 buf = alloca (dyninfo_sect_size);
672 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
675 /* Find the DT_DEBUG entry in the the .dynamic section.
676 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
677 no DT_DEBUG entries. */
678 /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
679 a 32 bit ELF ABI target. */
680 for (bufend = buf + dyninfo_sect_size;
682 buf += sizeof (Elf32_External_Dyn))
684 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *)buf;
688 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
689 if (dyn_tag == DT_NULL)
691 else if (dyn_tag == DT_DEBUG)
693 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
696 #ifdef DT_MIPS_RLD_MAP
697 else if (dyn_tag == DT_MIPS_RLD_MAP)
699 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
701 /* DT_MIPS_RLD_MAP contains a pointer to the address
702 of the dynamic link structure. */
703 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
704 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
706 return extract_unsigned_integer (pbuf, sizeof (pbuf));
711 /* DT_DEBUG entry not found. */
715 #endif /* SVR4_SHARED_LIBS */
721 locate_base -- locate the base address of dynamic linker structs
725 CORE_ADDR locate_base (void)
729 For both the SunOS and SVR4 shared library implementations, if the
730 inferior executable has been linked dynamically, there is a single
731 address somewhere in the inferior's data space which is the key to
732 locating all of the dynamic linker's runtime structures. This
733 address is the value of the debug base symbol. The job of this
734 function is to find and return that address, or to return 0 if there
735 is no such address (the executable is statically linked for example).
737 For SunOS, the job is almost trivial, since the dynamic linker and
738 all of it's structures are statically linked to the executable at
739 link time. Thus the symbol for the address we are looking for has
740 already been added to the minimal symbol table for the executable's
741 objfile at the time the symbol file's symbols were read, and all we
742 have to do is look it up there. Note that we explicitly do NOT want
743 to find the copies in the shared library.
745 The SVR4 version is a bit more complicated because the address
746 is contained somewhere in the dynamic info section. We have to go
747 to a lot more work to discover the address of the debug base symbol.
748 Because of this complexity, we cache the value we find and return that
749 value on subsequent invocations. Note there is no copy in the
750 executable symbol tables.
758 #ifndef SVR4_SHARED_LIBS
760 struct minimal_symbol *msymbol;
761 CORE_ADDR address = 0;
764 /* For SunOS, we want to limit the search for the debug base symbol to the
765 executable being debugged, since there is a duplicate named symbol in the
766 shared library. We don't want the shared library versions. */
768 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
770 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
771 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
773 address = SYMBOL_VALUE_ADDRESS (msymbol);
779 #else /* SVR4_SHARED_LIBS */
781 /* Check to see if we have a currently valid address, and if so, avoid
782 doing all this work again and just return the cached address. If
783 we have no cached address, try to locate it in the dynamic info
784 section for ELF executables. */
789 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
790 debug_base = elf_locate_base ();
791 #ifdef HANDLE_SVR4_EXEC_EMULATORS
792 /* Try it the hard way for emulated executables. */
793 else if (inferior_pid != 0)
794 proc_iterate_over_mappings (look_for_base);
799 #endif /* !SVR4_SHARED_LIBS */
807 first_link_map_member -- locate first member in dynamic linker's map
811 static struct link_map *first_link_map_member (void)
815 Read in a copy of the first member in the inferior's dynamic
816 link map from the inferior's dynamic linker structures, and return
817 a pointer to the copy in our address space.
820 static struct link_map *
821 first_link_map_member ()
823 struct link_map *lm = NULL;
825 #ifndef SVR4_SHARED_LIBS
827 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
828 if (dynamic_copy.ld_version >= 2)
830 /* It is a version that we can deal with, so read in the secondary
831 structure and find the address of the link map list from it. */
832 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
833 sizeof (struct link_dynamic_2));
834 lm = ld_2_copy.ld_loaded;
837 #else /* SVR4_SHARED_LIBS */
839 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
840 /* FIXME: Perhaps we should validate the info somehow, perhaps by
841 checking r_version for a known version number, or r_state for
843 lm = debug_copy.r_map;
845 #endif /* !SVR4_SHARED_LIBS */
854 free_solib -- free a so_list structure
858 void free_solib (struct so_list *so_list_ptr)
862 Free the memory used by a struct so_list.
873 free ((PTR)so -> sections);
877 bfd_filename = bfd_get_filename (so -> abfd);
878 if (!bfd_close (so -> abfd))
879 warning ("cannot close \"%s\": %s",
880 bfd_filename, bfd_errmsg (bfd_get_error ()));
883 /* This happens for the executable on SVR4. */
887 free ((PTR)bfd_filename);
895 alloc_solib -- free a so_list structure
899 struct so_list *alloc_solib (struct link_map *lm)
903 Allocate the struct so_list to cache debugging information
904 for a struct link_map entry in the target.
914 /* Get next link map structure from inferior image and build a local
915 abbreviated load_map structure */
916 new = (struct so_list *) xmalloc (sizeof (struct so_list));
917 memset ((char *) new, 0, sizeof (struct so_list));
920 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
921 sizeof (struct link_map));
922 /* For SVR4 versions, the first entry in the link map is for the
923 inferior executable, so we must ignore it. For some versions of
924 SVR4, it has no name. For others (Solaris 2.3 for example), it
925 does have a name, so we can no longer use a missing name to
926 decide when to ignore it. */
927 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
931 target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
932 MAX_PATH_SIZE - 1, &errcode);
934 error ("find_solib: Can't read pathname for load map: %s\n",
935 safe_strerror (errcode));
936 strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
937 new -> so_name[MAX_PATH_SIZE - 1] = '\0';
939 solib_map_sections (new);
949 find_solib -- step through list of shared objects
953 struct so_list *find_solib (struct so_list *so_list_ptr, int maybe_changed)
957 This module contains the routine which finds the names of any
958 loaded "images" in the current process. The argument in must be
959 NULL on the first call, and then the returned value must be passed
960 in on subsequent calls. This provides the capability to "step" down
961 the list of loaded objects. On the last object, a NULL value is
964 The arg and return value are "struct link_map" pointers, as defined
967 If it is expected that the contents of the shared library list has changed
968 (e.g. when the special shared library breakpoint is hit) then pass non-zero
969 for maybe_changed, otherwise zero.
972 static struct so_list *
973 find_solib (so_list_ptr, maybe_changed)
974 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
975 int maybe_changed; /* non-zero if shlib list might have changed */
977 struct link_map *lm = NULL;
979 struct so_list *p, **prev;
981 if (so_list_ptr == NULL)
983 struct so_list **map;
985 /* If we have not already read in the dynamic linking structures
986 from the inferior, lookup the address of the base structure. */
988 debug_base = locate_base ();
991 /* Read the base structure in and find the address of the first
992 link map list member. */
993 lm = first_link_map_member ();
998 prev = &so_list_head;
999 so_list_ptr = so_list_head;
1003 /* We have been called before, and are in the process of walking
1004 the shared library list. Advance to the next shared object.
1006 Always read from the target to check to see if any were
1007 added, but be quiet if we can't read from the target any more. */
1008 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
1009 (char *) &(so_list_ptr -> lm),
1010 sizeof (struct link_map));
1014 lm = LM_NEXT (so_list_ptr);
1021 prev = &so_list_ptr -> next;
1022 so_list_ptr = so_list_ptr -> next;
1025 /* If we don't believe that the list has changed, just return the cached copy. */
1027 return (so_list_ptr);
1029 /* At this point, lm is the address of the next list element in the target and
1030 so_list_ptr is our cached entry for it. */
1034 if (so_list_ptr == NULL || so_list_ptr -> lmaddr != lm)
1036 /* We have detected a change in the list. Check for a deletion by searching
1037 forward in the cached list */
1040 for (p = so_list_ptr -> next; p; p = p -> next)
1041 if (p -> lmaddr == lm)
1049 /* This lib has been deleted */
1050 while (so_list_ptr != p)
1052 *prev = so_list_ptr -> next;
1053 free_solib (so_list_ptr);
1054 so_list_ptr = *prev;
1059 /* A new lib has been inserted into the list */
1060 new = alloc_solib (lm);
1061 new -> next = so_list_ptr;
1069 if (so_list_ptr != NULL)
1071 /* Libs have been deleted from the end of the list */
1072 while (so_list_ptr != NULL)
1074 *prev = so_list_ptr -> next;
1075 free_solib (so_list_ptr);
1076 so_list_ptr = *prev;
1081 return (so_list_ptr);
1084 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
1087 symbol_add_stub (arg)
1090 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
1093 symbol_file_add (so -> so_name, so -> from_tty,
1094 (so->textsection == NULL
1096 : (unsigned int) so -> textsection -> addr),
1101 /* This function will check the so name to see if matches the main list.
1102 In some system the main object is in the list, which we want to exclude */
1104 static int match_main (soname)
1109 for (mainp = main_name_list; *mainp != NULL; mainp++)
1111 if (strcmp (soname, *mainp) == 0)
1122 solib_add -- add a shared library file to the symtab and section list
1126 void solib_add (char *arg_string, int from_tty,
1127 struct target_ops *target)
1134 solib_add (arg_string, from_tty, target)
1137 struct target_ops *target;
1139 register struct so_list *so = NULL; /* link map state variable */
1141 /* Last shared library that we read. */
1142 struct so_list *so_last = NULL;
1148 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
1150 error ("Invalid regexp: %s", re_err);
1153 /* Add the shared library sections to the section table of the
1154 specified target, if any. */
1157 /* Count how many new section_table entries there are. */
1160 while ((so = find_solib (so, 1)) != NULL)
1162 if (so -> so_name[0] && !match_main (so -> so_name))
1164 count += so -> sections_end - so -> sections;
1172 /* We must update the to_sections field in the core_ops structure
1173 here, otherwise we dereference a potential dangling pointer
1174 for each call to target_read/write_memory within this routine. */
1175 update_coreops = core_ops.to_sections == target->to_sections;
1177 /* Reallocate the target's section table including the new size. */
1178 if (target -> to_sections)
1180 old = target -> to_sections_end - target -> to_sections;
1181 target -> to_sections = (struct section_table *)
1182 xrealloc ((char *)target -> to_sections,
1183 (sizeof (struct section_table)) * (count + old));
1188 target -> to_sections = (struct section_table *)
1189 xmalloc ((sizeof (struct section_table)) * count);
1191 target -> to_sections_end = target -> to_sections + (count + old);
1193 /* Update the to_sections field in the core_ops structure
1197 core_ops.to_sections = target->to_sections;
1198 core_ops.to_sections_end = target->to_sections_end;
1201 /* Add these section table entries to the target's table. */
1202 while ((so = find_solib (so, 1)) != NULL)
1204 if (so -> so_name[0])
1206 count = so -> sections_end - so -> sections;
1207 memcpy ((char *) (target -> to_sections + old),
1209 (sizeof (struct section_table)) * count);
1216 /* Now add the symbol files. */
1217 while ((so = find_solib (so, 1)) != NULL)
1219 if (so -> so_name[0] && re_exec (so -> so_name) &&
1220 !match_main (so -> so_name))
1222 so -> from_tty = from_tty;
1223 if (so -> symbols_loaded)
1227 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
1230 else if (catch_errors
1231 (symbol_add_stub, (char *) so,
1232 "Error while reading shared library symbols:\n",
1236 so -> symbols_loaded = 1;
1241 /* Getting new symbols may change our opinion about what is
1244 reinit_frame_cache ();
1247 special_symbol_handling (so_last);
1254 info_sharedlibrary_command -- code for "info sharedlibrary"
1258 static void info_sharedlibrary_command ()
1262 Walk through the shared library list and print information
1263 about each attached library.
1267 info_sharedlibrary_command (ignore, from_tty)
1271 register struct so_list *so = NULL; /* link map state variable */
1272 int header_done = 0;
1274 if (exec_bfd == NULL)
1276 printf_unfiltered ("No exec file.\n");
1279 while ((so = find_solib (so, 0)) != NULL)
1281 if (so -> so_name[0])
1285 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
1286 "Shared Object Library");
1289 /* FIXME-32x64: need print_address_numeric with field width or
1291 printf_unfiltered ("%-12s",
1292 local_hex_string_custom ((unsigned long) LM_ADDR (so),
1294 printf_unfiltered ("%-12s",
1295 local_hex_string_custom ((unsigned long) so -> lmend,
1297 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
1298 printf_unfiltered ("%s\n", so -> so_name);
1301 if (so_list_head == NULL)
1303 printf_unfiltered ("No shared libraries loaded at this time.\n");
1311 solib_address -- check to see if an address is in a shared lib
1315 char * solib_address (CORE_ADDR address)
1319 Provides a hook for other gdb routines to discover whether or
1320 not a particular address is within the mapped address space of
1321 a shared library. Any address between the base mapping address
1322 and the first address beyond the end of the last mapping, is
1323 considered to be within the shared library address space, for
1326 For example, this routine is called at one point to disable
1327 breakpoints which are in shared libraries that are not currently
1332 solib_address (address)
1335 register struct so_list *so = 0; /* link map state variable */
1337 while ((so = find_solib (so, 0)) != NULL)
1339 if (so -> so_name[0])
1341 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
1342 (address < (CORE_ADDR) so -> lmend))
1343 return (so->so_name);
1349 /* Called by free_all_symtabs */
1354 struct so_list *next;
1356 while (so_list_head)
1358 next = so_list_head -> next;
1359 free_solib (so_list_head);
1360 so_list_head = next;
1369 disable_break -- remove the "mapping changed" breakpoint
1373 static int disable_break ()
1377 Removes the breakpoint that gets hit when the dynamic linker
1378 completes a mapping change.
1382 #ifndef SVR4_SHARED_LIBS
1389 #ifndef SVR4_SHARED_LIBS
1391 int in_debugger = 0;
1393 /* Read the debugger structure from the inferior to retrieve the
1394 address of the breakpoint and the original contents of the
1395 breakpoint address. Remove the breakpoint by writing the original
1398 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1400 /* Set `in_debugger' to zero now. */
1402 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1404 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
1405 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1406 sizeof (debug_copy.ldd_bp_inst));
1408 #else /* SVR4_SHARED_LIBS */
1410 /* Note that breakpoint address and original contents are in our address
1411 space, so we just need to write the original contents back. */
1413 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1418 #endif /* !SVR4_SHARED_LIBS */
1420 /* For the SVR4 version, we always know the breakpoint address. For the
1421 SunOS version we don't know it until the above code is executed.
1422 Grumble if we are stopped anywhere besides the breakpoint address. */
1424 if (stop_pc != breakpoint_addr)
1426 warning ("stopped at unknown breakpoint while handling shared libraries");
1432 #endif /* #ifdef SVR4_SHARED_LIBS */
1438 enable_break -- arrange for dynamic linker to hit breakpoint
1442 int enable_break (void)
1446 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1447 debugger interface, support for arranging for the inferior to hit
1448 a breakpoint after mapping in the shared libraries. This function
1449 enables that breakpoint.
1451 For SunOS, there is a special flag location (in_debugger) which we
1452 set to 1. When the dynamic linker sees this flag set, it will set
1453 a breakpoint at a location known only to itself, after saving the
1454 original contents of that place and the breakpoint address itself,
1455 in it's own internal structures. When we resume the inferior, it
1456 will eventually take a SIGTRAP when it runs into the breakpoint.
1457 We handle this (in a different place) by restoring the contents of
1458 the breakpointed location (which is only known after it stops),
1459 chasing around to locate the shared libraries that have been
1460 loaded, then resuming.
1462 For SVR4, the debugger interface structure contains a member (r_brk)
1463 which is statically initialized at the time the shared library is
1464 built, to the offset of a function (_r_debug_state) which is guaran-
1465 teed to be called once before mapping in a library, and again when
1466 the mapping is complete. At the time we are examining this member,
1467 it contains only the unrelocated offset of the function, so we have
1468 to do our own relocation. Later, when the dynamic linker actually
1469 runs, it relocates r_brk to be the actual address of _r_debug_state().
1471 The debugger interface structure also contains an enumeration which
1472 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1473 depending upon whether or not the library is being mapped or unmapped,
1474 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1482 #ifndef SVR4_SHARED_LIBS
1487 /* Get link_dynamic structure */
1489 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1490 sizeof (dynamic_copy));
1497 /* Calc address of debugger interface structure */
1499 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1501 /* Calc address of `in_debugger' member of debugger interface structure */
1503 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1504 (char *) &debug_copy);
1506 /* Write a value of 1 to this member. */
1509 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1512 #else /* SVR4_SHARED_LIBS */
1514 #ifdef BKPT_AT_SYMBOL
1516 struct minimal_symbol *msymbol;
1518 asection *interp_sect;
1520 /* First, remove all the solib event breakpoints. Their addresses
1521 may have changed since the last time we ran the program. */
1522 remove_solib_event_breakpoints ();
1524 #ifdef SVR4_SHARED_LIBS
1525 /* Find the .interp section; if not found, warn the user and drop
1526 into the old breakpoint at symbol code. */
1527 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1530 unsigned int interp_sect_size;
1532 CORE_ADDR load_addr;
1534 CORE_ADDR sym_addr = 0;
1536 /* Read the contents of the .interp section into a local buffer;
1537 the contents specify the dynamic linker this program uses. */
1538 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1539 buf = alloca (interp_sect_size);
1540 bfd_get_section_contents (exec_bfd, interp_sect,
1541 buf, 0, interp_sect_size);
1543 /* Now we need to figure out where the dynamic linker was
1544 loaded so that we can load its symbols and place a breakpoint
1545 in the dynamic linker itself.
1547 This address is stored on the stack. However, I've been unable
1548 to find any magic formula to find it for Solaris (appears to
1549 be trivial on Linux). Therefore, we have to try an alternate
1550 mechanism to find the dynamic linker's base address. */
1551 tmp_bfd = bfd_openr (buf, gnutarget);
1552 if (tmp_bfd == NULL)
1553 goto bkpt_at_symbol;
1555 /* Make sure the dynamic linker's really a useful object. */
1556 if (!bfd_check_format (tmp_bfd, bfd_object))
1558 warning ("Unable to grok dynamic linker %s as an object file", buf);
1559 bfd_close (tmp_bfd);
1560 goto bkpt_at_symbol;
1563 /* We find the dynamic linker's base address by examining the
1564 current pc (which point at the entry point for the dynamic
1565 linker) and subtracting the offset of the entry point. */
1566 load_addr = read_pc () - tmp_bfd->start_address;
1568 /* Now try to set a breakpoint in the dynamic linker. */
1569 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1571 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1576 /* We're done with the temporary bfd. */
1577 bfd_close (tmp_bfd);
1581 create_solib_event_breakpoint (load_addr + sym_addr);
1585 /* For whatever reason we couldn't set a breakpoint in the dynamic
1586 linker. Warn and drop into the old code. */
1588 warning ("Unable to find dynamic linker breakpoint function.");
1589 warning ("GDB will be unable to debug shared library initializers");
1590 warning ("and track explicitly loaded dynamic code.");
1594 /* Scan through the list of symbols, trying to look up the symbol and
1595 set a breakpoint there. Terminate loop when we/if we succeed. */
1597 breakpoint_addr = 0;
1598 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1600 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1601 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1603 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1608 /* Nothing good happened. */
1611 #endif /* BKPT_AT_SYMBOL */
1613 #endif /* !SVR4_SHARED_LIBS */
1622 solib_create_inferior_hook -- shared library startup support
1626 void solib_create_inferior_hook()
1630 When gdb starts up the inferior, it nurses it along (through the
1631 shell) until it is ready to execute it's first instruction. At this
1632 point, this function gets called via expansion of the macro
1633 SOLIB_CREATE_INFERIOR_HOOK.
1635 For SunOS executables, this first instruction is typically the
1636 one at "_start", or a similar text label, regardless of whether
1637 the executable is statically or dynamically linked. The runtime
1638 startup code takes care of dynamically linking in any shared
1639 libraries, once gdb allows the inferior to continue.
1641 For SVR4 executables, this first instruction is either the first
1642 instruction in the dynamic linker (for dynamically linked
1643 executables) or the instruction at "start" for statically linked
1644 executables. For dynamically linked executables, the system
1645 first exec's /lib/libc.so.N, which contains the dynamic linker,
1646 and starts it running. The dynamic linker maps in any needed
1647 shared libraries, maps in the actual user executable, and then
1648 jumps to "start" in the user executable.
1650 For both SunOS shared libraries, and SVR4 shared libraries, we
1651 can arrange to cooperate with the dynamic linker to discover the
1652 names of shared libraries that are dynamically linked, and the
1653 base addresses to which they are linked.
1655 This function is responsible for discovering those names and
1656 addresses, and saving sufficient information about them to allow
1657 their symbols to be read at a later time.
1661 Between enable_break() and disable_break(), this code does not
1662 properly handle hitting breakpoints which the user might have
1663 set in the startup code or in the dynamic linker itself. Proper
1664 handling will probably have to wait until the implementation is
1665 changed to use the "breakpoint handler function" method.
1667 Also, what if child has exit()ed? Must exit loop somehow.
1671 solib_create_inferior_hook()
1673 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1674 yet. In fact, in the case of a SunOS4 executable being run on
1675 Solaris, we can't get it yet. find_solib will get it when it needs
1677 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1678 if ((debug_base = locate_base ()) == 0)
1680 /* Can't find the symbol or the executable is statically linked. */
1685 if (!enable_break ())
1687 warning ("shared library handler failed to enable breakpoint");
1691 #ifndef SVR4_SHARED_LIBS
1692 /* Only SunOS needs the loop below, other systems should be using the
1693 special shared library breakpoints and the shared library breakpoint
1696 Now run the target. It will eventually hit the breakpoint, at
1697 which point all of the libraries will have been mapped in and we
1698 can go groveling around in the dynamic linker structures to find
1699 out what we need to know about them. */
1701 clear_proceed_status ();
1702 stop_soon_quietly = 1;
1703 stop_signal = TARGET_SIGNAL_0;
1706 target_resume (-1, 0, stop_signal);
1707 wait_for_inferior ();
1709 while (stop_signal != TARGET_SIGNAL_TRAP);
1710 stop_soon_quietly = 0;
1712 /* We are now either at the "mapping complete" breakpoint (or somewhere
1713 else, a condition we aren't prepared to deal with anyway), so adjust
1714 the PC as necessary after a breakpoint, disable the breakpoint, and
1715 add any shared libraries that were mapped in. */
1717 if (DECR_PC_AFTER_BREAK)
1719 stop_pc -= DECR_PC_AFTER_BREAK;
1720 write_register (PC_REGNUM, stop_pc);
1723 if (!disable_break ())
1725 warning ("shared library handler failed to disable breakpoint");
1729 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1737 special_symbol_handling -- additional shared library symbol handling
1741 void special_symbol_handling (struct so_list *so)
1745 Once the symbols from a shared object have been loaded in the usual
1746 way, we are called to do any system specific symbol handling that
1749 For SunOS4, this consists of grunging around in the dynamic
1750 linkers structures to find symbol definitions for "common" symbols
1751 and adding them to the minimal symbol table for the runtime common
1757 special_symbol_handling (so)
1760 #ifndef SVR4_SHARED_LIBS
1763 if (debug_addr == 0)
1765 /* Get link_dynamic structure */
1767 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1768 sizeof (dynamic_copy));
1775 /* Calc address of debugger interface structure */
1776 /* FIXME, this needs work for cross-debugging of core files
1777 (byteorder, size, alignment, etc). */
1779 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1782 /* Read the debugger structure from the inferior, just to make sure
1783 we have a current copy. */
1785 j = target_read_memory (debug_addr, (char *) &debug_copy,
1786 sizeof (debug_copy));
1788 return; /* unreadable */
1790 /* Get common symbol definitions for the loaded object. */
1792 if (debug_copy.ldd_cp)
1794 solib_add_common_symbols (debug_copy.ldd_cp);
1797 #endif /* !SVR4_SHARED_LIBS */
1805 sharedlibrary_command -- handle command to explicitly add library
1809 static void sharedlibrary_command (char *args, int from_tty)
1816 sharedlibrary_command (args, from_tty)
1821 solib_add (args, from_tty, (struct target_ops *) 0);
1824 #endif /* HAVE_LINK_H */
1831 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1832 "Load shared object library symbols for files matching REGEXP.");
1833 add_info ("sharedlibrary", info_sharedlibrary_command,
1834 "Status of loaded shared object libraries.");
1837 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
1838 (char *) &auto_solib_add,
1839 "Set autoloading of shared library symbols.\n\
1840 If nonzero, symbols from all shared object libraries will be loaded\n\
1841 automatically when the inferior begins execution or when the dynamic linker\n\
1842 informs gdb that a new library has been loaded. Otherwise, symbols\n\
1843 must be loaded manually, using `sharedlibrary'.",
1847 #endif /* HAVE_LINK_H */