1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include <sys/types.h>
26 #include "gdb_string.h"
27 #include <sys/param.h>
31 #ifndef SVR4_SHARED_LIBS
32 /* SunOS shared libs need the nlist structure. */
35 #include "elf/external.h"
53 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
55 /* On SVR4 systems, for the initial implementation, use some runtime startup
56 symbol as the "startup mapping complete" breakpoint address. The models
57 for SunOS and SVR4 dynamic linking debugger support are different in that
58 SunOS hits one breakpoint when all mapping is complete while using the SVR4
59 debugger support takes two breakpoint hits for each file mapped, and
60 there is no way to know when the "last" one is hit. Both these
61 mechanisms should be tied to a "breakpoint service routine" that
62 gets automatically executed whenever one of the breakpoints indicating
63 a change in mapping is hit. This is a future enhancement. (FIXME) */
65 #define BKPT_AT_SYMBOL 1
67 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
68 static char *bkpt_names[] = {
69 #ifdef SOLIB_BKPT_NAME
70 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
78 /* Symbols which are used to locate the base of the link map structures. */
80 #ifndef SVR4_SHARED_LIBS
81 static char *debug_base_symbols[] = {
88 static char *main_name_list[] = {
93 /* local data declarations */
95 /* If true, then shared library symbols will be added automatically
96 when the inferior is created. This is almost always what users
97 will want to have happen; but for very large programs, the startup
98 time will be excessive, and so if this is a problem, the user can
99 clear this flag and then add the shared library symbols as needed.
100 Note that there is a potential for confusion, since if the shared
101 library symbols are not loaded, commands like "info fun" will *not*
102 report all the functions that are actually present. */
104 int auto_solib_add_at_startup = 1;
106 #ifndef SVR4_SHARED_LIBS
108 #define LM_ADDR(so) ((so) -> lm.lm_addr)
109 #define LM_NEXT(so) ((so) -> lm.lm_next)
110 #define LM_NAME(so) ((so) -> lm.lm_name)
111 /* Test for first link map entry; first entry is a shared library. */
112 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
113 static struct link_dynamic dynamic_copy;
114 static struct link_dynamic_2 ld_2_copy;
115 static struct ld_debug debug_copy;
116 static CORE_ADDR debug_addr;
117 static CORE_ADDR flag_addr;
119 #else /* SVR4_SHARED_LIBS */
121 #define LM_ADDR(so) ((so) -> lm.l_addr)
122 #define LM_NEXT(so) ((so) -> lm.l_next)
123 #define LM_NAME(so) ((so) -> lm.l_name)
124 /* Test for first link map entry; first entry is the exec-file. */
125 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
126 static struct r_debug debug_copy;
127 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
129 #endif /* !SVR4_SHARED_LIBS */
132 struct so_list *next; /* next structure in linked list */
133 struct link_map lm; /* copy of link map from inferior */
134 struct link_map *lmaddr; /* addr in inferior lm was read from */
135 CORE_ADDR lmend; /* upper addr bound of mapped object */
136 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
137 char symbols_loaded; /* flag: symbols read in yet? */
138 char from_tty; /* flag: print msgs? */
139 struct objfile *objfile; /* objfile for loaded lib */
140 struct section_table *sections;
141 struct section_table *sections_end;
142 struct section_table *textsection;
146 static struct so_list *so_list_head; /* List of known shared objects */
147 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
148 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
151 fdmatch PARAMS ((int, int)); /* In libiberty */
153 /* Local function prototypes */
156 special_symbol_handling PARAMS ((struct so_list *));
159 sharedlibrary_command PARAMS ((char *, int));
162 enable_break PARAMS ((void));
165 disable_break PARAMS ((void));
168 info_sharedlibrary_command PARAMS ((char *, int));
171 symbol_add_stub PARAMS ((char *));
173 static struct so_list *
174 find_solib PARAMS ((struct so_list *));
176 static struct link_map *
177 first_link_map_member PARAMS ((void));
180 locate_base PARAMS ((void));
183 solib_map_sections PARAMS ((struct so_list *));
185 #ifdef SVR4_SHARED_LIBS
188 elf_locate_base PARAMS ((void));
193 allocate_rt_common_objfile PARAMS ((void));
196 solib_add_common_symbols PARAMS ((struct rtc_symb *));
204 solib_map_sections -- open bfd and build sections for shared lib
208 static void solib_map_sections (struct so_list *so)
212 Given a pointer to one of the shared objects in our list
213 of mapped objects, use the recorded name to open a bfd
214 descriptor for the object, build a section table, and then
215 relocate all the section addresses by the base address at
216 which the shared object was mapped.
220 In most (all?) cases the shared object file name recorded in the
221 dynamic linkage tables will be a fully qualified pathname. For
222 cases where it isn't, do we really mimic the systems search
223 mechanism correctly in the below code (particularly the tilde
228 solib_map_sections (so)
232 char *scratch_pathname;
234 struct section_table *p;
235 struct cleanup *old_chain;
238 filename = tilde_expand (so -> so_name);
239 old_chain = make_cleanup (free, filename);
241 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
243 if (scratch_chan < 0)
245 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
246 O_RDONLY, 0, &scratch_pathname);
248 if (scratch_chan < 0)
250 perror_with_name (filename);
252 /* Leave scratch_pathname allocated. abfd->name will point to it. */
254 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
257 close (scratch_chan);
258 error ("Could not open `%s' as an executable file: %s",
259 scratch_pathname, bfd_errmsg (bfd_get_error ()));
261 /* Leave bfd open, core_xfer_memory and "info files" need it. */
263 abfd -> cacheable = true;
265 if (!bfd_check_format (abfd, bfd_object))
267 error ("\"%s\": not in executable format: %s.",
268 scratch_pathname, bfd_errmsg (bfd_get_error ()));
270 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
272 error ("Can't find the file sections in `%s': %s",
273 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
276 for (p = so -> sections; p < so -> sections_end; p++)
278 /* Relocate the section binding addresses as recorded in the shared
279 object's file by the base address to which the object was actually
281 p -> addr += (CORE_ADDR) LM_ADDR (so);
282 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
283 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
284 if (STREQ (p -> the_bfd_section -> name, ".text"))
286 so -> textsection = p;
290 /* Free the file names, close the file now. */
291 do_cleanups (old_chain);
294 #ifndef SVR4_SHARED_LIBS
296 /* Allocate the runtime common object file. */
299 allocate_rt_common_objfile ()
301 struct objfile *objfile;
302 struct objfile *last_one;
304 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
305 memset (objfile, 0, sizeof (struct objfile));
306 objfile -> md = NULL;
307 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
309 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
311 obstack_specify_allocation (&objfile -> type_obstack, 0, 0, xmalloc,
313 objfile -> name = mstrsave (objfile -> md, "rt_common");
315 /* Add this file onto the tail of the linked list of other such files. */
317 objfile -> next = NULL;
318 if (object_files == NULL)
319 object_files = objfile;
322 for (last_one = object_files;
324 last_one = last_one -> next);
325 last_one -> next = objfile;
328 rt_common_objfile = objfile;
331 /* Read all dynamically loaded common symbol definitions from the inferior
332 and put them into the minimal symbol table for the runtime common
336 solib_add_common_symbols (rtc_symp)
337 struct rtc_symb *rtc_symp;
339 struct rtc_symb inferior_rtc_symb;
340 struct nlist inferior_rtc_nlist;
345 /* Remove any runtime common symbols from previous runs. */
347 if (rt_common_objfile != NULL && rt_common_objfile -> minimal_symbol_count)
349 obstack_free (&rt_common_objfile -> symbol_obstack, 0);
350 obstack_specify_allocation (&rt_common_objfile -> symbol_obstack, 0, 0,
352 rt_common_objfile -> minimal_symbol_count = 0;
353 rt_common_objfile -> msymbols = NULL;
356 init_minimal_symbol_collection ();
357 make_cleanup (discard_minimal_symbols, 0);
361 read_memory ((CORE_ADDR) rtc_symp,
362 (char *) &inferior_rtc_symb,
363 sizeof (inferior_rtc_symb));
364 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
365 (char *) &inferior_rtc_nlist,
366 sizeof(inferior_rtc_nlist));
367 if (inferior_rtc_nlist.n_type == N_COMM)
369 /* FIXME: The length of the symbol name is not available, but in the
370 current implementation the common symbol is allocated immediately
371 behind the name of the symbol. */
372 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
374 origname = name = xmalloc (len);
375 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
377 /* Allocate the runtime common objfile if necessary. */
378 if (rt_common_objfile == NULL)
379 allocate_rt_common_objfile ();
381 name = obsavestring (name, strlen (name),
382 &rt_common_objfile -> symbol_obstack);
383 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
384 mst_bss, rt_common_objfile);
387 rtc_symp = inferior_rtc_symb.rtc_next;
390 /* Install any minimal symbols that have been collected as the current
391 minimal symbols for the runtime common objfile. */
393 install_minimal_symbols (rt_common_objfile);
396 #endif /* SVR4_SHARED_LIBS */
399 #ifdef SVR4_SHARED_LIBS
401 #ifdef HANDLE_SVR4_EXEC_EMULATORS
404 Solaris BCP (the part of Solaris which allows it to run SunOS4
405 a.out files) throws in another wrinkle. Solaris does not fill
406 in the usual a.out link map structures when running BCP programs,
407 the only way to get at them is via groping around in the dynamic
409 The dynamic linker and it's structures are located in the shared
410 C library, which gets run as the executable's "interpreter" by
413 Note that we can assume nothing about the process state at the time
414 we need to find these structures. We may be stopped on the first
415 instruction of the interpreter (C shared library), the first
416 instruction of the executable itself, or somewhere else entirely
417 (if we attached to the process for example).
420 static char *debug_base_symbols[] = {
421 "r_debug", /* Solaris 2.3 */
422 "_r_debug", /* Solaris 2.1, 2.2 */
427 look_for_base PARAMS ((int, CORE_ADDR));
430 bfd_lookup_symbol PARAMS ((bfd *, char *));
436 bfd_lookup_symbol -- lookup the value for a specific symbol
440 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
444 An expensive way to lookup the value of a single symbol for
445 bfd's that are only temporary anyway. This is used by the
446 shared library support to find the address of the debugger
447 interface structures in the shared library.
449 Note that 0 is specifically allowed as an error return (no
454 bfd_lookup_symbol (abfd, symname)
458 unsigned int storage_needed;
460 asymbol **symbol_table;
461 unsigned int number_of_symbols;
463 struct cleanup *back_to;
464 CORE_ADDR symaddr = 0;
466 storage_needed = bfd_get_symtab_upper_bound (abfd);
468 if (storage_needed > 0)
470 symbol_table = (asymbol **) xmalloc (storage_needed);
471 back_to = make_cleanup (free, (PTR)symbol_table);
472 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
474 for (i = 0; i < number_of_symbols; i++)
476 sym = *symbol_table++;
477 if (STREQ (sym -> name, symname))
479 /* Bfd symbols are section relative. */
480 symaddr = sym -> value + sym -> section -> vma;
484 do_cleanups (back_to);
493 look_for_base -- examine file for each mapped address segment
497 static int look_for_base (int fd, CORE_ADDR baseaddr)
501 This function is passed to proc_iterate_over_mappings, which
502 causes it to get called once for each mapped address space, with
503 an open file descriptor for the file mapped to that space, and the
504 base address of that mapped space.
506 Our job is to find the debug base symbol in the file that this
507 fd is open on, if it exists, and if so, initialize the dynamic
508 linker structure base address debug_base.
510 Note that this is a computationally expensive proposition, since
511 we basically have to open a bfd on every call, so we specifically
512 avoid opening the exec file.
516 look_for_base (fd, baseaddr)
521 CORE_ADDR address = 0;
524 /* If the fd is -1, then there is no file that corresponds to this
525 mapped memory segment, so skip it. Also, if the fd corresponds
526 to the exec file, skip it as well. */
530 && fdmatch (fileno ((GDB_FILE *)(exec_bfd -> iostream)), fd)))
535 /* Try to open whatever random file this fd corresponds to. Note that
536 we have no way currently to find the filename. Don't gripe about
537 any problems we might have, just fail. */
539 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
543 if (!bfd_check_format (interp_bfd, bfd_object))
545 /* FIXME-leak: on failure, might not free all memory associated with
547 bfd_close (interp_bfd);
551 /* Now try to find our debug base symbol in this file, which we at
552 least know to be a valid ELF executable or shared library. */
554 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
556 address = bfd_lookup_symbol (interp_bfd, *symbolp);
564 /* FIXME-leak: on failure, might not free all memory associated with
566 bfd_close (interp_bfd);
570 /* Eureka! We found the symbol. But now we may need to relocate it
571 by the base address. If the symbol's value is less than the base
572 address of the shared library, then it hasn't yet been relocated
573 by the dynamic linker, and we have to do it ourself. FIXME: Note
574 that we make the assumption that the first segment that corresponds
575 to the shared library has the base address to which the library
578 if (address < baseaddr)
582 debug_base = address;
583 /* FIXME-leak: on failure, might not free all memory associated with
585 bfd_close (interp_bfd);
588 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
594 elf_locate_base -- locate the base address of dynamic linker structs
595 for SVR4 elf targets.
599 CORE_ADDR elf_locate_base (void)
603 For SVR4 elf targets the address of the dynamic linker's runtime
604 structure is contained within the dynamic info section in the
605 executable file. The dynamic section is also mapped into the
606 inferior address space. Because the runtime loader fills in the
607 real address before starting the inferior, we have to read in the
608 dynamic info section from the inferior address space.
609 If there are any errors while trying to find the address, we
610 silently return 0, otherwise the found address is returned.
617 sec_ptr dyninfo_sect;
618 int dyninfo_sect_size;
619 CORE_ADDR dyninfo_addr;
623 /* Find the start address of the .dynamic section. */
624 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
625 if (dyninfo_sect == NULL)
627 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
629 /* Read in .dynamic section, silently ignore errors. */
630 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
631 buf = alloca (dyninfo_sect_size);
632 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
635 /* Find the DT_DEBUG entry in the the .dynamic section.
636 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
637 no DT_DEBUG entries. */
638 /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
639 a 32 bit ELF ABI target. */
640 for (bufend = buf + dyninfo_sect_size;
642 buf += sizeof (Elf32_External_Dyn))
644 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *)buf;
648 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
649 if (dyn_tag == DT_NULL)
651 else if (dyn_tag == DT_DEBUG)
653 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
656 #ifdef DT_MIPS_RLD_MAP
657 else if (dyn_tag == DT_MIPS_RLD_MAP)
659 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
661 /* DT_MIPS_RLD_MAP contains a pointer to the address
662 of the dynamic link structure. */
663 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
664 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
666 return extract_unsigned_integer (pbuf, sizeof (pbuf));
671 /* DT_DEBUG entry not found. */
675 #endif /* SVR4_SHARED_LIBS */
681 locate_base -- locate the base address of dynamic linker structs
685 CORE_ADDR locate_base (void)
689 For both the SunOS and SVR4 shared library implementations, if the
690 inferior executable has been linked dynamically, there is a single
691 address somewhere in the inferior's data space which is the key to
692 locating all of the dynamic linker's runtime structures. This
693 address is the value of the debug base symbol. The job of this
694 function is to find and return that address, or to return 0 if there
695 is no such address (the executable is statically linked for example).
697 For SunOS, the job is almost trivial, since the dynamic linker and
698 all of it's structures are statically linked to the executable at
699 link time. Thus the symbol for the address we are looking for has
700 already been added to the minimal symbol table for the executable's
701 objfile at the time the symbol file's symbols were read, and all we
702 have to do is look it up there. Note that we explicitly do NOT want
703 to find the copies in the shared library.
705 The SVR4 version is a bit more complicated because the address
706 is contained somewhere in the dynamic info section. We have to go
707 to a lot more work to discover the address of the debug base symbol.
708 Because of this complexity, we cache the value we find and return that
709 value on subsequent invocations. Note there is no copy in the
710 executable symbol tables.
718 #ifndef SVR4_SHARED_LIBS
720 struct minimal_symbol *msymbol;
721 CORE_ADDR address = 0;
724 /* For SunOS, we want to limit the search for the debug base symbol to the
725 executable being debugged, since there is a duplicate named symbol in the
726 shared library. We don't want the shared library versions. */
728 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
730 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
731 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
733 address = SYMBOL_VALUE_ADDRESS (msymbol);
739 #else /* SVR4_SHARED_LIBS */
741 /* Check to see if we have a currently valid address, and if so, avoid
742 doing all this work again and just return the cached address. If
743 we have no cached address, try to locate it in the dynamic info
744 section for ELF executables. */
749 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
750 debug_base = elf_locate_base ();
751 #ifdef HANDLE_SVR4_EXEC_EMULATORS
752 /* Try it the hard way for emulated executables. */
753 else if (inferior_pid != 0)
754 proc_iterate_over_mappings (look_for_base);
759 #endif /* !SVR4_SHARED_LIBS */
767 first_link_map_member -- locate first member in dynamic linker's map
771 static struct link_map *first_link_map_member (void)
775 Read in a copy of the first member in the inferior's dynamic
776 link map from the inferior's dynamic linker structures, and return
777 a pointer to the copy in our address space.
780 static struct link_map *
781 first_link_map_member ()
783 struct link_map *lm = NULL;
785 #ifndef SVR4_SHARED_LIBS
787 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
788 if (dynamic_copy.ld_version >= 2)
790 /* It is a version that we can deal with, so read in the secondary
791 structure and find the address of the link map list from it. */
792 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
793 sizeof (struct link_dynamic_2));
794 lm = ld_2_copy.ld_loaded;
797 #else /* SVR4_SHARED_LIBS */
799 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
800 /* FIXME: Perhaps we should validate the info somehow, perhaps by
801 checking r_version for a known version number, or r_state for
803 lm = debug_copy.r_map;
805 #endif /* !SVR4_SHARED_LIBS */
814 find_solib -- step through list of shared objects
818 struct so_list *find_solib (struct so_list *so_list_ptr)
822 This module contains the routine which finds the names of any
823 loaded "images" in the current process. The argument in must be
824 NULL on the first call, and then the returned value must be passed
825 in on subsequent calls. This provides the capability to "step" down
826 the list of loaded objects. On the last object, a NULL value is
829 The arg and return value are "struct link_map" pointers, as defined
833 static struct so_list *
834 find_solib (so_list_ptr)
835 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
837 struct so_list *so_list_next = NULL;
838 struct link_map *lm = NULL;
841 if (so_list_ptr == NULL)
843 /* We are setting up for a new scan through the loaded images. */
844 if ((so_list_next = so_list_head) == NULL)
846 /* We have not already read in the dynamic linking structures
847 from the inferior, lookup the address of the base structure. */
848 debug_base = locate_base ();
851 /* Read the base structure in and find the address of the first
852 link map list member. */
853 lm = first_link_map_member ();
859 /* We have been called before, and are in the process of walking
860 the shared library list. Advance to the next shared object. */
861 if ((lm = LM_NEXT (so_list_ptr)) == NULL)
863 /* We have hit the end of the list, so check to see if any were
864 added, but be quiet if we can't read from the target any more. */
865 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
866 (char *) &(so_list_ptr -> lm),
867 sizeof (struct link_map));
870 lm = LM_NEXT (so_list_ptr);
877 so_list_next = so_list_ptr -> next;
879 if ((so_list_next == NULL) && (lm != NULL))
881 /* Get next link map structure from inferior image and build a local
882 abbreviated load_map structure */
883 new = (struct so_list *) xmalloc (sizeof (struct so_list));
884 memset ((char *) new, 0, sizeof (struct so_list));
886 /* Add the new node as the next node in the list, or as the root
887 node if this is the first one. */
888 if (so_list_ptr != NULL)
890 so_list_ptr -> next = new;
897 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
898 sizeof (struct link_map));
899 /* For SVR4 versions, the first entry in the link map is for the
900 inferior executable, so we must ignore it. For some versions of
901 SVR4, it has no name. For others (Solaris 2.3 for example), it
902 does have a name, so we can no longer use a missing name to
903 decide when to ignore it. */
904 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
908 target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
909 MAX_PATH_SIZE - 1, &errcode);
911 error ("find_solib: Can't read pathname for load map: %s\n",
912 safe_strerror (errcode));
913 strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
914 new -> so_name[MAX_PATH_SIZE - 1] = '\0';
916 solib_map_sections (new);
919 return (so_list_next);
922 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
925 symbol_add_stub (arg)
928 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
931 symbol_file_add (so -> so_name, so -> from_tty,
932 (so->textsection == NULL
934 : (unsigned int) so -> textsection -> addr),
939 /* This function will check the so name to see if matches the main list.
940 In some system the main object is in the list, which we want to exclude */
942 static int match_main (soname)
947 for (mainp = main_name_list; *mainp != NULL; mainp++)
949 if (strcmp (soname, *mainp) == 0)
960 solib_add -- add a shared library file to the symtab and section list
964 void solib_add (char *arg_string, int from_tty,
965 struct target_ops *target)
972 solib_add (arg_string, from_tty, target)
975 struct target_ops *target;
977 register struct so_list *so = NULL; /* link map state variable */
979 /* Last shared library that we read. */
980 struct so_list *so_last = NULL;
986 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
988 error ("Invalid regexp: %s", re_err);
991 /* Add the shared library sections to the section table of the
992 specified target, if any. */
995 /* Count how many new section_table entries there are. */
998 while ((so = find_solib (so)) != NULL)
1000 if (so -> so_name[0] && !match_main (so -> so_name))
1002 count += so -> sections_end - so -> sections;
1008 /* Reallocate the target's section table including the new size. */
1009 if (target -> to_sections)
1011 old = target -> to_sections_end - target -> to_sections;
1012 target -> to_sections = (struct section_table *)
1013 xrealloc ((char *)target -> to_sections,
1014 (sizeof (struct section_table)) * (count + old));
1019 target -> to_sections = (struct section_table *)
1020 xmalloc ((sizeof (struct section_table)) * count);
1022 target -> to_sections_end = target -> to_sections + (count + old);
1024 /* Add these section table entries to the target's table. */
1025 while ((so = find_solib (so)) != NULL)
1027 if (so -> so_name[0])
1029 count = so -> sections_end - so -> sections;
1030 memcpy ((char *) (target -> to_sections + old),
1032 (sizeof (struct section_table)) * count);
1039 /* Now add the symbol files. */
1040 while ((so = find_solib (so)) != NULL)
1042 if (so -> so_name[0] && re_exec (so -> so_name) &&
1043 !match_main (so -> so_name))
1045 so -> from_tty = from_tty;
1046 if (so -> symbols_loaded)
1050 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
1053 else if (catch_errors
1054 (symbol_add_stub, (char *) so,
1055 "Error while reading shared library symbols:\n",
1059 so -> symbols_loaded = 1;
1064 /* Getting new symbols may change our opinion about what is
1067 reinit_frame_cache ();
1070 special_symbol_handling (so_last);
1077 info_sharedlibrary_command -- code for "info sharedlibrary"
1081 static void info_sharedlibrary_command ()
1085 Walk through the shared library list and print information
1086 about each attached library.
1090 info_sharedlibrary_command (ignore, from_tty)
1094 register struct so_list *so = NULL; /* link map state variable */
1095 int header_done = 0;
1097 if (exec_bfd == NULL)
1099 printf_unfiltered ("No exec file.\n");
1102 while ((so = find_solib (so)) != NULL)
1104 if (so -> so_name[0])
1108 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
1109 "Shared Object Library");
1112 /* FIXME-32x64: need print_address_numeric with field width or
1114 printf_unfiltered ("%-12s",
1115 local_hex_string_custom ((unsigned long) LM_ADDR (so),
1117 printf_unfiltered ("%-12s",
1118 local_hex_string_custom ((unsigned long) so -> lmend,
1120 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
1121 printf_unfiltered ("%s\n", so -> so_name);
1124 if (so_list_head == NULL)
1126 printf_unfiltered ("No shared libraries loaded at this time.\n");
1134 solib_address -- check to see if an address is in a shared lib
1138 int solib_address (CORE_ADDR address)
1142 Provides a hook for other gdb routines to discover whether or
1143 not a particular address is within the mapped address space of
1144 a shared library. Any address between the base mapping address
1145 and the first address beyond the end of the last mapping, is
1146 considered to be within the shared library address space, for
1149 For example, this routine is called at one point to disable
1150 breakpoints which are in shared libraries that are not currently
1155 solib_address (address)
1158 register struct so_list *so = 0; /* link map state variable */
1160 while ((so = find_solib (so)) != NULL)
1162 if (so -> so_name[0])
1164 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
1165 (address < (CORE_ADDR) so -> lmend))
1174 /* Called by free_all_symtabs */
1179 struct so_list *next;
1182 while (so_list_head)
1184 if (so_list_head -> sections)
1186 free ((PTR)so_list_head -> sections);
1188 if (so_list_head -> abfd)
1190 bfd_filename = bfd_get_filename (so_list_head -> abfd);
1191 if (!bfd_close (so_list_head -> abfd))
1192 warning ("cannot close \"%s\": %s",
1193 bfd_filename, bfd_errmsg (bfd_get_error ()));
1196 /* This happens for the executable on SVR4. */
1197 bfd_filename = NULL;
1199 next = so_list_head -> next;
1201 free ((PTR)bfd_filename);
1202 free ((PTR)so_list_head);
1203 so_list_head = next;
1212 disable_break -- remove the "mapping changed" breakpoint
1216 static int disable_break ()
1220 Removes the breakpoint that gets hit when the dynamic linker
1221 completes a mapping change.
1230 #ifndef SVR4_SHARED_LIBS
1232 int in_debugger = 0;
1234 /* Read the debugger structure from the inferior to retrieve the
1235 address of the breakpoint and the original contents of the
1236 breakpoint address. Remove the breakpoint by writing the original
1239 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1241 /* Set `in_debugger' to zero now. */
1243 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1245 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
1246 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1247 sizeof (debug_copy.ldd_bp_inst));
1249 #else /* SVR4_SHARED_LIBS */
1251 /* Note that breakpoint address and original contents are in our address
1252 space, so we just need to write the original contents back. */
1254 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1259 #endif /* !SVR4_SHARED_LIBS */
1261 /* For the SVR4 version, we always know the breakpoint address. For the
1262 SunOS version we don't know it until the above code is executed.
1263 Grumble if we are stopped anywhere besides the breakpoint address. */
1265 if (stop_pc != breakpoint_addr)
1267 warning ("stopped at unknown breakpoint while handling shared libraries");
1277 enable_break -- arrange for dynamic linker to hit breakpoint
1281 int enable_break (void)
1285 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1286 debugger interface, support for arranging for the inferior to hit
1287 a breakpoint after mapping in the shared libraries. This function
1288 enables that breakpoint.
1290 For SunOS, there is a special flag location (in_debugger) which we
1291 set to 1. When the dynamic linker sees this flag set, it will set
1292 a breakpoint at a location known only to itself, after saving the
1293 original contents of that place and the breakpoint address itself,
1294 in it's own internal structures. When we resume the inferior, it
1295 will eventually take a SIGTRAP when it runs into the breakpoint.
1296 We handle this (in a different place) by restoring the contents of
1297 the breakpointed location (which is only known after it stops),
1298 chasing around to locate the shared libraries that have been
1299 loaded, then resuming.
1301 For SVR4, the debugger interface structure contains a member (r_brk)
1302 which is statically initialized at the time the shared library is
1303 built, to the offset of a function (_r_debug_state) which is guaran-
1304 teed to be called once before mapping in a library, and again when
1305 the mapping is complete. At the time we are examining this member,
1306 it contains only the unrelocated offset of the function, so we have
1307 to do our own relocation. Later, when the dynamic linker actually
1308 runs, it relocates r_brk to be the actual address of _r_debug_state().
1310 The debugger interface structure also contains an enumeration which
1311 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1312 depending upon whether or not the library is being mapped or unmapped,
1313 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1321 #ifndef SVR4_SHARED_LIBS
1326 /* Get link_dynamic structure */
1328 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1329 sizeof (dynamic_copy));
1336 /* Calc address of debugger interface structure */
1338 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1340 /* Calc address of `in_debugger' member of debugger interface structure */
1342 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1343 (char *) &debug_copy);
1345 /* Write a value of 1 to this member. */
1348 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1351 #else /* SVR4_SHARED_LIBS */
1353 #ifdef BKPT_AT_SYMBOL
1355 struct minimal_symbol *msymbol;
1357 CORE_ADDR bkpt_addr;
1359 /* Scan through the list of symbols, trying to look up the symbol and
1360 set a breakpoint there. Terminate loop when we/if we succeed. */
1362 breakpoint_addr = 0;
1363 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1365 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1366 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1368 bkpt_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1369 if (target_insert_breakpoint (bkpt_addr, shadow_contents) == 0)
1371 breakpoint_addr = bkpt_addr;
1378 #else /* !BKPT_AT_SYMBOL */
1380 struct symtab_and_line sal;
1382 /* Read the debugger interface structure directly. */
1384 read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
1386 /* Set breakpoint at the debugger interface stub routine that will
1387 be called just prior to each mapping change and again after the
1388 mapping change is complete. Set up the (nonexistent) handler to
1389 deal with hitting these breakpoints. (FIXME). */
1391 warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
1394 #endif /* BKPT_AT_SYMBOL */
1396 #endif /* !SVR4_SHARED_LIBS */
1405 solib_create_inferior_hook -- shared library startup support
1409 void solib_create_inferior_hook()
1413 When gdb starts up the inferior, it nurses it along (through the
1414 shell) until it is ready to execute it's first instruction. At this
1415 point, this function gets called via expansion of the macro
1416 SOLIB_CREATE_INFERIOR_HOOK.
1418 For SunOS executables, this first instruction is typically the
1419 one at "_start", or a similar text label, regardless of whether
1420 the executable is statically or dynamically linked. The runtime
1421 startup code takes care of dynamically linking in any shared
1422 libraries, once gdb allows the inferior to continue.
1424 For SVR4 executables, this first instruction is either the first
1425 instruction in the dynamic linker (for dynamically linked
1426 executables) or the instruction at "start" for statically linked
1427 executables. For dynamically linked executables, the system
1428 first exec's /lib/libc.so.N, which contains the dynamic linker,
1429 and starts it running. The dynamic linker maps in any needed
1430 shared libraries, maps in the actual user executable, and then
1431 jumps to "start" in the user executable.
1433 For both SunOS shared libraries, and SVR4 shared libraries, we
1434 can arrange to cooperate with the dynamic linker to discover the
1435 names of shared libraries that are dynamically linked, and the
1436 base addresses to which they are linked.
1438 This function is responsible for discovering those names and
1439 addresses, and saving sufficient information about them to allow
1440 their symbols to be read at a later time.
1444 Between enable_break() and disable_break(), this code does not
1445 properly handle hitting breakpoints which the user might have
1446 set in the startup code or in the dynamic linker itself. Proper
1447 handling will probably have to wait until the implementation is
1448 changed to use the "breakpoint handler function" method.
1450 Also, what if child has exit()ed? Must exit loop somehow.
1454 solib_create_inferior_hook()
1456 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1457 yet. In fact, in the case of a SunOS4 executable being run on
1458 Solaris, we can't get it yet. find_solib will get it when it needs
1460 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1461 if ((debug_base = locate_base ()) == 0)
1463 /* Can't find the symbol or the executable is statically linked. */
1468 if (!enable_break ())
1470 warning ("shared library handler failed to enable breakpoint");
1474 /* Now run the target. It will eventually hit the breakpoint, at
1475 which point all of the libraries will have been mapped in and we
1476 can go groveling around in the dynamic linker structures to find
1477 out what we need to know about them. */
1479 clear_proceed_status ();
1480 stop_soon_quietly = 1;
1481 stop_signal = TARGET_SIGNAL_0;
1484 target_resume (-1, 0, stop_signal);
1485 wait_for_inferior ();
1487 while (stop_signal != TARGET_SIGNAL_TRAP);
1488 stop_soon_quietly = 0;
1490 /* We are now either at the "mapping complete" breakpoint (or somewhere
1491 else, a condition we aren't prepared to deal with anyway), so adjust
1492 the PC as necessary after a breakpoint, disable the breakpoint, and
1493 add any shared libraries that were mapped in. */
1495 if (DECR_PC_AFTER_BREAK)
1497 stop_pc -= DECR_PC_AFTER_BREAK;
1498 write_register (PC_REGNUM, stop_pc);
1501 if (!disable_break ())
1503 warning ("shared library handler failed to disable breakpoint");
1506 if (auto_solib_add_at_startup)
1507 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1514 special_symbol_handling -- additional shared library symbol handling
1518 void special_symbol_handling (struct so_list *so)
1522 Once the symbols from a shared object have been loaded in the usual
1523 way, we are called to do any system specific symbol handling that
1526 For SunOS4, this consists of grunging around in the dynamic
1527 linkers structures to find symbol definitions for "common" symbols
1528 and adding them to the minimal symbol table for the runtime common
1534 special_symbol_handling (so)
1537 #ifndef SVR4_SHARED_LIBS
1540 if (debug_addr == 0)
1542 /* Get link_dynamic structure */
1544 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1545 sizeof (dynamic_copy));
1552 /* Calc address of debugger interface structure */
1553 /* FIXME, this needs work for cross-debugging of core files
1554 (byteorder, size, alignment, etc). */
1556 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1559 /* Read the debugger structure from the inferior, just to make sure
1560 we have a current copy. */
1562 j = target_read_memory (debug_addr, (char *) &debug_copy,
1563 sizeof (debug_copy));
1565 return; /* unreadable */
1567 /* Get common symbol definitions for the loaded object. */
1569 if (debug_copy.ldd_cp)
1571 solib_add_common_symbols (debug_copy.ldd_cp);
1574 #endif /* !SVR4_SHARED_LIBS */
1582 sharedlibrary_command -- handle command to explicitly add library
1586 static void sharedlibrary_command (char *args, int from_tty)
1593 sharedlibrary_command (args, from_tty)
1598 solib_add (args, from_tty, (struct target_ops *) 0);
1605 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1606 "Load shared object library symbols for files matching REGEXP.");
1607 add_info ("sharedlibrary", info_sharedlibrary_command,
1608 "Status of loaded shared object libraries.");
1611 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
1612 (char *) &auto_solib_add_at_startup,
1613 "Set autoloading of shared library symbols at startup.\n\
1614 If nonzero, symbols from all shared object libraries will be loaded\n\
1615 automatically when the inferior begins execution. Otherwise, symbols\n\
1616 must be loaded manually, using `sharedlibrary'.",