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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
26 #include "gdb_string.h"
27 #include <sys/param.h>
31 #ifndef SVR4_SHARED_LIBS
32 /* SunOS shared libs need the nlist structure. */
35 #include "elf/external.h"
54 #define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */
56 /* On SVR4 systems, for the initial implementation, use some runtime startup
57 symbol as the "startup mapping complete" breakpoint address. The models
58 for SunOS and SVR4 dynamic linking debugger support are different in that
59 SunOS hits one breakpoint when all mapping is complete while using the SVR4
60 debugger support takes two breakpoint hits for each file mapped, and
61 there is no way to know when the "last" one is hit. Both these
62 mechanisms should be tied to a "breakpoint service routine" that
63 gets automatically executed whenever one of the breakpoints indicating
64 a change in mapping is hit. This is a future enhancement. (FIXME) */
66 #define BKPT_AT_SYMBOL 1
68 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
69 static char *bkpt_names[] = {
70 #ifdef SOLIB_BKPT_NAME
71 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
79 /* Symbols which are used to locate the base of the link map structures. */
81 #ifndef SVR4_SHARED_LIBS
82 static char *debug_base_symbols[] = {
89 static char *main_name_list[] = {
94 /* local data declarations */
96 /* If true, then shared library symbols will be added automatically
97 when the inferior is created. This is almost always what users
98 will want to have happen; but for very large programs, the startup
99 time will be excessive, and so if this is a problem, the user can
100 clear this flag and then add the shared library symbols as needed.
101 Note that there is a potential for confusion, since if the shared
102 library symbols are not loaded, commands like "info fun" will *not*
103 report all the functions that are actually present. */
105 int auto_solib_add_at_startup = 1;
107 #ifndef SVR4_SHARED_LIBS
109 #define LM_ADDR(so) ((so) -> lm.lm_addr)
110 #define LM_NEXT(so) ((so) -> lm.lm_next)
111 #define LM_NAME(so) ((so) -> lm.lm_name)
112 /* Test for first link map entry; first entry is a shared library. */
113 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
114 static struct link_dynamic dynamic_copy;
115 static struct link_dynamic_2 ld_2_copy;
116 static struct ld_debug debug_copy;
117 static CORE_ADDR debug_addr;
118 static CORE_ADDR flag_addr;
120 #else /* SVR4_SHARED_LIBS */
122 #define LM_ADDR(so) ((so) -> lm.l_addr)
123 #define LM_NEXT(so) ((so) -> lm.l_next)
124 #define LM_NAME(so) ((so) -> lm.l_name)
125 /* Test for first link map entry; first entry is the exec-file. */
126 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
127 static struct r_debug debug_copy;
128 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
130 #endif /* !SVR4_SHARED_LIBS */
133 struct so_list *next; /* next structure in linked list */
134 struct link_map lm; /* copy of link map from inferior */
135 struct link_map *lmaddr; /* addr in inferior lm was read from */
136 CORE_ADDR lmend; /* upper addr bound of mapped object */
137 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
138 char symbols_loaded; /* flag: symbols read in yet? */
139 char from_tty; /* flag: print msgs? */
140 struct objfile *objfile; /* objfile for loaded lib */
141 struct section_table *sections;
142 struct section_table *sections_end;
143 struct section_table *textsection;
147 static struct so_list *so_list_head; /* List of known shared objects */
148 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
149 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
152 fdmatch PARAMS ((int, int)); /* In libiberty */
154 /* Local function prototypes */
157 special_symbol_handling PARAMS ((struct so_list *));
160 sharedlibrary_command PARAMS ((char *, int));
163 enable_break PARAMS ((void));
166 disable_break PARAMS ((void));
169 info_sharedlibrary_command PARAMS ((char *, int));
172 symbol_add_stub PARAMS ((char *));
174 static struct so_list *
175 find_solib PARAMS ((struct so_list *));
177 static struct link_map *
178 first_link_map_member PARAMS ((void));
181 locate_base PARAMS ((void));
184 solib_map_sections PARAMS ((struct so_list *));
186 #ifdef SVR4_SHARED_LIBS
189 elf_locate_base PARAMS ((void));
194 allocate_rt_common_objfile PARAMS ((void));
197 solib_add_common_symbols PARAMS ((struct rtc_symb *));
205 solib_map_sections -- open bfd and build sections for shared lib
209 static void solib_map_sections (struct so_list *so)
213 Given a pointer to one of the shared objects in our list
214 of mapped objects, use the recorded name to open a bfd
215 descriptor for the object, build a section table, and then
216 relocate all the section addresses by the base address at
217 which the shared object was mapped.
221 In most (all?) cases the shared object file name recorded in the
222 dynamic linkage tables will be a fully qualified pathname. For
223 cases where it isn't, do we really mimic the systems search
224 mechanism correctly in the below code (particularly the tilde
229 solib_map_sections (so)
233 char *scratch_pathname;
235 struct section_table *p;
236 struct cleanup *old_chain;
239 filename = tilde_expand (so -> so_name);
240 old_chain = make_cleanup (free, filename);
242 scratch_chan = openp (get_in_environ (inferior_environ, "PATH"),
243 1, filename, O_RDONLY, 0, &scratch_pathname);
244 if (scratch_chan < 0)
246 scratch_chan = openp (get_in_environ
247 (inferior_environ, "LD_LIBRARY_PATH"),
248 1, filename, O_RDONLY, 0, &scratch_pathname);
250 if (scratch_chan < 0)
252 perror_with_name (filename);
254 /* Leave scratch_pathname allocated. abfd->name will point to it. */
256 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
259 close (scratch_chan);
260 error ("Could not open `%s' as an executable file: %s",
261 scratch_pathname, bfd_errmsg (bfd_get_error ()));
263 /* Leave bfd open, core_xfer_memory and "info files" need it. */
265 abfd -> cacheable = true;
267 /* copy full path name into so_name, so that later symbol_file_add can find
269 if (strlen (scratch_pathname) >= MAX_PATH_SIZE)
270 error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
271 strcpy (so->so_name, scratch_pathname);
273 if (!bfd_check_format (abfd, bfd_object))
275 error ("\"%s\": not in executable format: %s.",
276 scratch_pathname, bfd_errmsg (bfd_get_error ()));
278 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
280 error ("Can't find the file sections in `%s': %s",
281 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
284 for (p = so -> sections; p < so -> sections_end; p++)
286 /* Relocate the section binding addresses as recorded in the shared
287 object's file by the base address to which the object was actually
289 p -> addr += (CORE_ADDR) LM_ADDR (so);
290 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
291 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
292 if (STREQ (p -> the_bfd_section -> name, ".text"))
294 so -> textsection = p;
298 /* Free the file names, close the file now. */
299 do_cleanups (old_chain);
302 #ifndef SVR4_SHARED_LIBS
304 /* Allocate the runtime common object file. */
307 allocate_rt_common_objfile ()
309 struct objfile *objfile;
310 struct objfile *last_one;
312 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
313 memset (objfile, 0, sizeof (struct objfile));
314 objfile -> md = NULL;
315 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
317 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
319 obstack_specify_allocation (&objfile -> type_obstack, 0, 0, xmalloc,
321 objfile -> name = mstrsave (objfile -> md, "rt_common");
323 /* Add this file onto the tail of the linked list of other such files. */
325 objfile -> next = NULL;
326 if (object_files == NULL)
327 object_files = objfile;
330 for (last_one = object_files;
332 last_one = last_one -> next);
333 last_one -> next = objfile;
336 rt_common_objfile = objfile;
339 /* Read all dynamically loaded common symbol definitions from the inferior
340 and put them into the minimal symbol table for the runtime common
344 solib_add_common_symbols (rtc_symp)
345 struct rtc_symb *rtc_symp;
347 struct rtc_symb inferior_rtc_symb;
348 struct nlist inferior_rtc_nlist;
353 /* Remove any runtime common symbols from previous runs. */
355 if (rt_common_objfile != NULL && rt_common_objfile -> minimal_symbol_count)
357 obstack_free (&rt_common_objfile -> symbol_obstack, 0);
358 obstack_specify_allocation (&rt_common_objfile -> symbol_obstack, 0, 0,
360 rt_common_objfile -> minimal_symbol_count = 0;
361 rt_common_objfile -> msymbols = NULL;
364 init_minimal_symbol_collection ();
365 make_cleanup (discard_minimal_symbols, 0);
369 read_memory ((CORE_ADDR) rtc_symp,
370 (char *) &inferior_rtc_symb,
371 sizeof (inferior_rtc_symb));
372 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
373 (char *) &inferior_rtc_nlist,
374 sizeof(inferior_rtc_nlist));
375 if (inferior_rtc_nlist.n_type == N_COMM)
377 /* FIXME: The length of the symbol name is not available, but in the
378 current implementation the common symbol is allocated immediately
379 behind the name of the symbol. */
380 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
382 origname = name = xmalloc (len);
383 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
385 /* Allocate the runtime common objfile if necessary. */
386 if (rt_common_objfile == NULL)
387 allocate_rt_common_objfile ();
389 name = obsavestring (name, strlen (name),
390 &rt_common_objfile -> symbol_obstack);
391 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
392 mst_bss, rt_common_objfile);
395 rtc_symp = inferior_rtc_symb.rtc_next;
398 /* Install any minimal symbols that have been collected as the current
399 minimal symbols for the runtime common objfile. */
401 install_minimal_symbols (rt_common_objfile);
404 #endif /* SVR4_SHARED_LIBS */
407 #ifdef SVR4_SHARED_LIBS
409 #ifdef HANDLE_SVR4_EXEC_EMULATORS
412 Solaris BCP (the part of Solaris which allows it to run SunOS4
413 a.out files) throws in another wrinkle. Solaris does not fill
414 in the usual a.out link map structures when running BCP programs,
415 the only way to get at them is via groping around in the dynamic
417 The dynamic linker and it's structures are located in the shared
418 C library, which gets run as the executable's "interpreter" by
421 Note that we can assume nothing about the process state at the time
422 we need to find these structures. We may be stopped on the first
423 instruction of the interpreter (C shared library), the first
424 instruction of the executable itself, or somewhere else entirely
425 (if we attached to the process for example).
428 static char *debug_base_symbols[] = {
429 "r_debug", /* Solaris 2.3 */
430 "_r_debug", /* Solaris 2.1, 2.2 */
435 look_for_base PARAMS ((int, CORE_ADDR));
438 bfd_lookup_symbol PARAMS ((bfd *, char *));
444 bfd_lookup_symbol -- lookup the value for a specific symbol
448 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
452 An expensive way to lookup the value of a single symbol for
453 bfd's that are only temporary anyway. This is used by the
454 shared library support to find the address of the debugger
455 interface structures in the shared library.
457 Note that 0 is specifically allowed as an error return (no
462 bfd_lookup_symbol (abfd, symname)
466 unsigned int storage_needed;
468 asymbol **symbol_table;
469 unsigned int number_of_symbols;
471 struct cleanup *back_to;
472 CORE_ADDR symaddr = 0;
474 storage_needed = bfd_get_symtab_upper_bound (abfd);
476 if (storage_needed > 0)
478 symbol_table = (asymbol **) xmalloc (storage_needed);
479 back_to = make_cleanup (free, (PTR)symbol_table);
480 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
482 for (i = 0; i < number_of_symbols; i++)
484 sym = *symbol_table++;
485 if (STREQ (sym -> name, symname))
487 /* Bfd symbols are section relative. */
488 symaddr = sym -> value + sym -> section -> vma;
492 do_cleanups (back_to);
501 look_for_base -- examine file for each mapped address segment
505 static int look_for_base (int fd, CORE_ADDR baseaddr)
509 This function is passed to proc_iterate_over_mappings, which
510 causes it to get called once for each mapped address space, with
511 an open file descriptor for the file mapped to that space, and the
512 base address of that mapped space.
514 Our job is to find the debug base symbol in the file that this
515 fd is open on, if it exists, and if so, initialize the dynamic
516 linker structure base address debug_base.
518 Note that this is a computationally expensive proposition, since
519 we basically have to open a bfd on every call, so we specifically
520 avoid opening the exec file.
524 look_for_base (fd, baseaddr)
529 CORE_ADDR address = 0;
532 /* If the fd is -1, then there is no file that corresponds to this
533 mapped memory segment, so skip it. Also, if the fd corresponds
534 to the exec file, skip it as well. */
538 && fdmatch (fileno ((GDB_FILE *)(exec_bfd -> iostream)), fd)))
543 /* Try to open whatever random file this fd corresponds to. Note that
544 we have no way currently to find the filename. Don't gripe about
545 any problems we might have, just fail. */
547 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
551 if (!bfd_check_format (interp_bfd, bfd_object))
553 /* FIXME-leak: on failure, might not free all memory associated with
555 bfd_close (interp_bfd);
559 /* Now try to find our debug base symbol in this file, which we at
560 least know to be a valid ELF executable or shared library. */
562 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
564 address = bfd_lookup_symbol (interp_bfd, *symbolp);
572 /* FIXME-leak: on failure, might not free all memory associated with
574 bfd_close (interp_bfd);
578 /* Eureka! We found the symbol. But now we may need to relocate it
579 by the base address. If the symbol's value is less than the base
580 address of the shared library, then it hasn't yet been relocated
581 by the dynamic linker, and we have to do it ourself. FIXME: Note
582 that we make the assumption that the first segment that corresponds
583 to the shared library has the base address to which the library
586 if (address < baseaddr)
590 debug_base = address;
591 /* FIXME-leak: on failure, might not free all memory associated with
593 bfd_close (interp_bfd);
596 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
602 elf_locate_base -- locate the base address of dynamic linker structs
603 for SVR4 elf targets.
607 CORE_ADDR elf_locate_base (void)
611 For SVR4 elf targets the address of the dynamic linker's runtime
612 structure is contained within the dynamic info section in the
613 executable file. The dynamic section is also mapped into the
614 inferior address space. Because the runtime loader fills in the
615 real address before starting the inferior, we have to read in the
616 dynamic info section from the inferior address space.
617 If there are any errors while trying to find the address, we
618 silently return 0, otherwise the found address is returned.
625 sec_ptr dyninfo_sect;
626 int dyninfo_sect_size;
627 CORE_ADDR dyninfo_addr;
631 /* Find the start address of the .dynamic section. */
632 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
633 if (dyninfo_sect == NULL)
635 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
637 /* Read in .dynamic section, silently ignore errors. */
638 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
639 buf = alloca (dyninfo_sect_size);
640 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
643 /* Find the DT_DEBUG entry in the the .dynamic section.
644 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
645 no DT_DEBUG entries. */
646 /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
647 a 32 bit ELF ABI target. */
648 for (bufend = buf + dyninfo_sect_size;
650 buf += sizeof (Elf32_External_Dyn))
652 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *)buf;
656 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
657 if (dyn_tag == DT_NULL)
659 else if (dyn_tag == DT_DEBUG)
661 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
664 #ifdef DT_MIPS_RLD_MAP
665 else if (dyn_tag == DT_MIPS_RLD_MAP)
667 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
669 /* DT_MIPS_RLD_MAP contains a pointer to the address
670 of the dynamic link structure. */
671 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
672 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
674 return extract_unsigned_integer (pbuf, sizeof (pbuf));
679 /* DT_DEBUG entry not found. */
683 #endif /* SVR4_SHARED_LIBS */
689 locate_base -- locate the base address of dynamic linker structs
693 CORE_ADDR locate_base (void)
697 For both the SunOS and SVR4 shared library implementations, if the
698 inferior executable has been linked dynamically, there is a single
699 address somewhere in the inferior's data space which is the key to
700 locating all of the dynamic linker's runtime structures. This
701 address is the value of the debug base symbol. The job of this
702 function is to find and return that address, or to return 0 if there
703 is no such address (the executable is statically linked for example).
705 For SunOS, the job is almost trivial, since the dynamic linker and
706 all of it's structures are statically linked to the executable at
707 link time. Thus the symbol for the address we are looking for has
708 already been added to the minimal symbol table for the executable's
709 objfile at the time the symbol file's symbols were read, and all we
710 have to do is look it up there. Note that we explicitly do NOT want
711 to find the copies in the shared library.
713 The SVR4 version is a bit more complicated because the address
714 is contained somewhere in the dynamic info section. We have to go
715 to a lot more work to discover the address of the debug base symbol.
716 Because of this complexity, we cache the value we find and return that
717 value on subsequent invocations. Note there is no copy in the
718 executable symbol tables.
726 #ifndef SVR4_SHARED_LIBS
728 struct minimal_symbol *msymbol;
729 CORE_ADDR address = 0;
732 /* For SunOS, we want to limit the search for the debug base symbol to the
733 executable being debugged, since there is a duplicate named symbol in the
734 shared library. We don't want the shared library versions. */
736 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
738 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
739 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
741 address = SYMBOL_VALUE_ADDRESS (msymbol);
747 #else /* SVR4_SHARED_LIBS */
749 /* Check to see if we have a currently valid address, and if so, avoid
750 doing all this work again and just return the cached address. If
751 we have no cached address, try to locate it in the dynamic info
752 section for ELF executables. */
757 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
758 debug_base = elf_locate_base ();
759 #ifdef HANDLE_SVR4_EXEC_EMULATORS
760 /* Try it the hard way for emulated executables. */
761 else if (inferior_pid != 0)
762 proc_iterate_over_mappings (look_for_base);
767 #endif /* !SVR4_SHARED_LIBS */
775 first_link_map_member -- locate first member in dynamic linker's map
779 static struct link_map *first_link_map_member (void)
783 Read in a copy of the first member in the inferior's dynamic
784 link map from the inferior's dynamic linker structures, and return
785 a pointer to the copy in our address space.
788 static struct link_map *
789 first_link_map_member ()
791 struct link_map *lm = NULL;
793 #ifndef SVR4_SHARED_LIBS
795 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
796 if (dynamic_copy.ld_version >= 2)
798 /* It is a version that we can deal with, so read in the secondary
799 structure and find the address of the link map list from it. */
800 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
801 sizeof (struct link_dynamic_2));
802 lm = ld_2_copy.ld_loaded;
805 #else /* SVR4_SHARED_LIBS */
807 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
808 /* FIXME: Perhaps we should validate the info somehow, perhaps by
809 checking r_version for a known version number, or r_state for
811 lm = debug_copy.r_map;
813 #endif /* !SVR4_SHARED_LIBS */
822 find_solib -- step through list of shared objects
826 struct so_list *find_solib (struct so_list *so_list_ptr)
830 This module contains the routine which finds the names of any
831 loaded "images" in the current process. The argument in must be
832 NULL on the first call, and then the returned value must be passed
833 in on subsequent calls. This provides the capability to "step" down
834 the list of loaded objects. On the last object, a NULL value is
837 The arg and return value are "struct link_map" pointers, as defined
841 static struct so_list *
842 find_solib (so_list_ptr)
843 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
845 struct so_list *so_list_next = NULL;
846 struct link_map *lm = NULL;
849 if (so_list_ptr == NULL)
851 /* We are setting up for a new scan through the loaded images. */
852 if ((so_list_next = so_list_head) == NULL)
854 /* We have not already read in the dynamic linking structures
855 from the inferior, lookup the address of the base structure. */
856 debug_base = locate_base ();
859 /* Read the base structure in and find the address of the first
860 link map list member. */
861 lm = first_link_map_member ();
867 /* We have been called before, and are in the process of walking
868 the shared library list. Advance to the next shared object. */
869 if ((lm = LM_NEXT (so_list_ptr)) == NULL)
871 /* We have hit the end of the list, so check to see if any were
872 added, but be quiet if we can't read from the target any more. */
873 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
874 (char *) &(so_list_ptr -> lm),
875 sizeof (struct link_map));
878 lm = LM_NEXT (so_list_ptr);
885 so_list_next = so_list_ptr -> next;
887 if ((so_list_next == NULL) && (lm != NULL))
889 /* Get next link map structure from inferior image and build a local
890 abbreviated load_map structure */
891 new = (struct so_list *) xmalloc (sizeof (struct so_list));
892 memset ((char *) new, 0, sizeof (struct so_list));
894 /* Add the new node as the next node in the list, or as the root
895 node if this is the first one. */
896 if (so_list_ptr != NULL)
898 so_list_ptr -> next = new;
905 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
906 sizeof (struct link_map));
907 /* For SVR4 versions, the first entry in the link map is for the
908 inferior executable, so we must ignore it. For some versions of
909 SVR4, it has no name. For others (Solaris 2.3 for example), it
910 does have a name, so we can no longer use a missing name to
911 decide when to ignore it. */
912 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
916 target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
917 MAX_PATH_SIZE - 1, &errcode);
919 error ("find_solib: Can't read pathname for load map: %s\n",
920 safe_strerror (errcode));
921 strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
922 new -> so_name[MAX_PATH_SIZE - 1] = '\0';
924 solib_map_sections (new);
927 return (so_list_next);
930 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
933 symbol_add_stub (arg)
936 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
939 symbol_file_add (so -> so_name, so -> from_tty,
940 (so->textsection == NULL
942 : (unsigned int) so -> textsection -> addr),
947 /* This function will check the so name to see if matches the main list.
948 In some system the main object is in the list, which we want to exclude */
950 static int match_main (soname)
955 for (mainp = main_name_list; *mainp != NULL; mainp++)
957 if (strcmp (soname, *mainp) == 0)
968 solib_add -- add a shared library file to the symtab and section list
972 void solib_add (char *arg_string, int from_tty,
973 struct target_ops *target)
980 solib_add (arg_string, from_tty, target)
983 struct target_ops *target;
985 register struct so_list *so = NULL; /* link map state variable */
987 /* Last shared library that we read. */
988 struct so_list *so_last = NULL;
994 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
996 error ("Invalid regexp: %s", re_err);
999 /* Add the shared library sections to the section table of the
1000 specified target, if any. */
1003 /* Count how many new section_table entries there are. */
1006 while ((so = find_solib (so)) != NULL)
1008 if (so -> so_name[0] && !match_main (so -> so_name))
1010 count += so -> sections_end - so -> sections;
1016 /* Reallocate the target's section table including the new size. */
1017 if (target -> to_sections)
1019 old = target -> to_sections_end - target -> to_sections;
1020 target -> to_sections = (struct section_table *)
1021 xrealloc ((char *)target -> to_sections,
1022 (sizeof (struct section_table)) * (count + old));
1027 target -> to_sections = (struct section_table *)
1028 xmalloc ((sizeof (struct section_table)) * count);
1030 target -> to_sections_end = target -> to_sections + (count + old);
1032 /* Add these section table entries to the target's table. */
1033 while ((so = find_solib (so)) != NULL)
1035 if (so -> so_name[0])
1037 count = so -> sections_end - so -> sections;
1038 memcpy ((char *) (target -> to_sections + old),
1040 (sizeof (struct section_table)) * count);
1047 /* Now add the symbol files. */
1048 while ((so = find_solib (so)) != NULL)
1050 if (so -> so_name[0] && re_exec (so -> so_name) &&
1051 !match_main (so -> so_name))
1053 so -> from_tty = from_tty;
1054 if (so -> symbols_loaded)
1058 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
1061 else if (catch_errors
1062 (symbol_add_stub, (char *) so,
1063 "Error while reading shared library symbols:\n",
1067 so -> symbols_loaded = 1;
1072 /* Getting new symbols may change our opinion about what is
1075 reinit_frame_cache ();
1078 special_symbol_handling (so_last);
1085 info_sharedlibrary_command -- code for "info sharedlibrary"
1089 static void info_sharedlibrary_command ()
1093 Walk through the shared library list and print information
1094 about each attached library.
1098 info_sharedlibrary_command (ignore, from_tty)
1102 register struct so_list *so = NULL; /* link map state variable */
1103 int header_done = 0;
1105 if (exec_bfd == NULL)
1107 printf_unfiltered ("No exec file.\n");
1110 while ((so = find_solib (so)) != NULL)
1112 if (so -> so_name[0])
1116 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
1117 "Shared Object Library");
1120 /* FIXME-32x64: need print_address_numeric with field width or
1122 printf_unfiltered ("%-12s",
1123 local_hex_string_custom ((unsigned long) LM_ADDR (so),
1125 printf_unfiltered ("%-12s",
1126 local_hex_string_custom ((unsigned long) so -> lmend,
1128 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
1129 printf_unfiltered ("%s\n", so -> so_name);
1132 if (so_list_head == NULL)
1134 printf_unfiltered ("No shared libraries loaded at this time.\n");
1142 solib_address -- check to see if an address is in a shared lib
1146 int solib_address (CORE_ADDR address)
1150 Provides a hook for other gdb routines to discover whether or
1151 not a particular address is within the mapped address space of
1152 a shared library. Any address between the base mapping address
1153 and the first address beyond the end of the last mapping, is
1154 considered to be within the shared library address space, for
1157 For example, this routine is called at one point to disable
1158 breakpoints which are in shared libraries that are not currently
1163 solib_address (address)
1166 register struct so_list *so = 0; /* link map state variable */
1168 while ((so = find_solib (so)) != NULL)
1170 if (so -> so_name[0])
1172 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
1173 (address < (CORE_ADDR) so -> lmend))
1182 /* Called by free_all_symtabs */
1187 struct so_list *next;
1190 while (so_list_head)
1192 if (so_list_head -> sections)
1194 free ((PTR)so_list_head -> sections);
1196 if (so_list_head -> abfd)
1198 bfd_filename = bfd_get_filename (so_list_head -> abfd);
1199 if (!bfd_close (so_list_head -> abfd))
1200 warning ("cannot close \"%s\": %s",
1201 bfd_filename, bfd_errmsg (bfd_get_error ()));
1204 /* This happens for the executable on SVR4. */
1205 bfd_filename = NULL;
1207 next = so_list_head -> next;
1209 free ((PTR)bfd_filename);
1210 free ((PTR)so_list_head);
1211 so_list_head = next;
1220 disable_break -- remove the "mapping changed" breakpoint
1224 static int disable_break ()
1228 Removes the breakpoint that gets hit when the dynamic linker
1229 completes a mapping change.
1238 #ifndef SVR4_SHARED_LIBS
1240 int in_debugger = 0;
1242 /* Read the debugger structure from the inferior to retrieve the
1243 address of the breakpoint and the original contents of the
1244 breakpoint address. Remove the breakpoint by writing the original
1247 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1249 /* Set `in_debugger' to zero now. */
1251 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1253 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
1254 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1255 sizeof (debug_copy.ldd_bp_inst));
1257 #else /* SVR4_SHARED_LIBS */
1259 /* Note that breakpoint address and original contents are in our address
1260 space, so we just need to write the original contents back. */
1262 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1267 #endif /* !SVR4_SHARED_LIBS */
1269 /* For the SVR4 version, we always know the breakpoint address. For the
1270 SunOS version we don't know it until the above code is executed.
1271 Grumble if we are stopped anywhere besides the breakpoint address. */
1273 if (stop_pc != breakpoint_addr)
1275 warning ("stopped at unknown breakpoint while handling shared libraries");
1285 enable_break -- arrange for dynamic linker to hit breakpoint
1289 int enable_break (void)
1293 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1294 debugger interface, support for arranging for the inferior to hit
1295 a breakpoint after mapping in the shared libraries. This function
1296 enables that breakpoint.
1298 For SunOS, there is a special flag location (in_debugger) which we
1299 set to 1. When the dynamic linker sees this flag set, it will set
1300 a breakpoint at a location known only to itself, after saving the
1301 original contents of that place and the breakpoint address itself,
1302 in it's own internal structures. When we resume the inferior, it
1303 will eventually take a SIGTRAP when it runs into the breakpoint.
1304 We handle this (in a different place) by restoring the contents of
1305 the breakpointed location (which is only known after it stops),
1306 chasing around to locate the shared libraries that have been
1307 loaded, then resuming.
1309 For SVR4, the debugger interface structure contains a member (r_brk)
1310 which is statically initialized at the time the shared library is
1311 built, to the offset of a function (_r_debug_state) which is guaran-
1312 teed to be called once before mapping in a library, and again when
1313 the mapping is complete. At the time we are examining this member,
1314 it contains only the unrelocated offset of the function, so we have
1315 to do our own relocation. Later, when the dynamic linker actually
1316 runs, it relocates r_brk to be the actual address of _r_debug_state().
1318 The debugger interface structure also contains an enumeration which
1319 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1320 depending upon whether or not the library is being mapped or unmapped,
1321 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1329 #ifndef SVR4_SHARED_LIBS
1334 /* Get link_dynamic structure */
1336 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1337 sizeof (dynamic_copy));
1344 /* Calc address of debugger interface structure */
1346 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1348 /* Calc address of `in_debugger' member of debugger interface structure */
1350 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1351 (char *) &debug_copy);
1353 /* Write a value of 1 to this member. */
1356 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1359 #else /* SVR4_SHARED_LIBS */
1361 #ifdef BKPT_AT_SYMBOL
1363 struct minimal_symbol *msymbol;
1365 CORE_ADDR bkpt_addr;
1367 /* Scan through the list of symbols, trying to look up the symbol and
1368 set a breakpoint there. Terminate loop when we/if we succeed. */
1370 breakpoint_addr = 0;
1371 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1373 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1374 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1376 bkpt_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1377 if (target_insert_breakpoint (bkpt_addr, shadow_contents) == 0)
1379 breakpoint_addr = bkpt_addr;
1386 #else /* !BKPT_AT_SYMBOL */
1388 struct symtab_and_line sal;
1390 /* Read the debugger interface structure directly. */
1392 read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
1394 /* Set breakpoint at the debugger interface stub routine that will
1395 be called just prior to each mapping change and again after the
1396 mapping change is complete. Set up the (nonexistent) handler to
1397 deal with hitting these breakpoints. (FIXME). */
1399 warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
1402 #endif /* BKPT_AT_SYMBOL */
1404 #endif /* !SVR4_SHARED_LIBS */
1413 solib_create_inferior_hook -- shared library startup support
1417 void solib_create_inferior_hook()
1421 When gdb starts up the inferior, it nurses it along (through the
1422 shell) until it is ready to execute it's first instruction. At this
1423 point, this function gets called via expansion of the macro
1424 SOLIB_CREATE_INFERIOR_HOOK.
1426 For SunOS executables, this first instruction is typically the
1427 one at "_start", or a similar text label, regardless of whether
1428 the executable is statically or dynamically linked. The runtime
1429 startup code takes care of dynamically linking in any shared
1430 libraries, once gdb allows the inferior to continue.
1432 For SVR4 executables, this first instruction is either the first
1433 instruction in the dynamic linker (for dynamically linked
1434 executables) or the instruction at "start" for statically linked
1435 executables. For dynamically linked executables, the system
1436 first exec's /lib/libc.so.N, which contains the dynamic linker,
1437 and starts it running. The dynamic linker maps in any needed
1438 shared libraries, maps in the actual user executable, and then
1439 jumps to "start" in the user executable.
1441 For both SunOS shared libraries, and SVR4 shared libraries, we
1442 can arrange to cooperate with the dynamic linker to discover the
1443 names of shared libraries that are dynamically linked, and the
1444 base addresses to which they are linked.
1446 This function is responsible for discovering those names and
1447 addresses, and saving sufficient information about them to allow
1448 their symbols to be read at a later time.
1452 Between enable_break() and disable_break(), this code does not
1453 properly handle hitting breakpoints which the user might have
1454 set in the startup code or in the dynamic linker itself. Proper
1455 handling will probably have to wait until the implementation is
1456 changed to use the "breakpoint handler function" method.
1458 Also, what if child has exit()ed? Must exit loop somehow.
1462 solib_create_inferior_hook()
1464 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1465 yet. In fact, in the case of a SunOS4 executable being run on
1466 Solaris, we can't get it yet. find_solib will get it when it needs
1468 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1469 if ((debug_base = locate_base ()) == 0)
1471 /* Can't find the symbol or the executable is statically linked. */
1476 if (!enable_break ())
1478 warning ("shared library handler failed to enable breakpoint");
1482 /* Now run the target. It will eventually hit the breakpoint, at
1483 which point all of the libraries will have been mapped in and we
1484 can go groveling around in the dynamic linker structures to find
1485 out what we need to know about them. */
1487 clear_proceed_status ();
1488 stop_soon_quietly = 1;
1489 stop_signal = TARGET_SIGNAL_0;
1492 target_resume (-1, 0, stop_signal);
1493 wait_for_inferior ();
1495 while (stop_signal != TARGET_SIGNAL_TRAP);
1496 stop_soon_quietly = 0;
1498 /* We are now either at the "mapping complete" breakpoint (or somewhere
1499 else, a condition we aren't prepared to deal with anyway), so adjust
1500 the PC as necessary after a breakpoint, disable the breakpoint, and
1501 add any shared libraries that were mapped in. */
1503 if (DECR_PC_AFTER_BREAK)
1505 stop_pc -= DECR_PC_AFTER_BREAK;
1506 write_register (PC_REGNUM, stop_pc);
1509 if (!disable_break ())
1511 warning ("shared library handler failed to disable breakpoint");
1514 if (auto_solib_add_at_startup)
1515 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1522 special_symbol_handling -- additional shared library symbol handling
1526 void special_symbol_handling (struct so_list *so)
1530 Once the symbols from a shared object have been loaded in the usual
1531 way, we are called to do any system specific symbol handling that
1534 For SunOS4, this consists of grunging around in the dynamic
1535 linkers structures to find symbol definitions for "common" symbols
1536 and adding them to the minimal symbol table for the runtime common
1542 special_symbol_handling (so)
1545 #ifndef SVR4_SHARED_LIBS
1548 if (debug_addr == 0)
1550 /* Get link_dynamic structure */
1552 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1553 sizeof (dynamic_copy));
1560 /* Calc address of debugger interface structure */
1561 /* FIXME, this needs work for cross-debugging of core files
1562 (byteorder, size, alignment, etc). */
1564 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1567 /* Read the debugger structure from the inferior, just to make sure
1568 we have a current copy. */
1570 j = target_read_memory (debug_addr, (char *) &debug_copy,
1571 sizeof (debug_copy));
1573 return; /* unreadable */
1575 /* Get common symbol definitions for the loaded object. */
1577 if (debug_copy.ldd_cp)
1579 solib_add_common_symbols (debug_copy.ldd_cp);
1582 #endif /* !SVR4_SHARED_LIBS */
1590 sharedlibrary_command -- handle command to explicitly add library
1594 static void sharedlibrary_command (char *args, int from_tty)
1601 sharedlibrary_command (args, from_tty)
1606 solib_add (args, from_tty, (struct target_ops *) 0);
1613 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1614 "Load shared object library symbols for files matching REGEXP.");
1615 add_info ("sharedlibrary", info_sharedlibrary_command,
1616 "Status of loaded shared object libraries.");
1619 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
1620 (char *) &auto_solib_add_at_startup,
1621 "Set autoloading of shared library symbols at startup.\n\
1622 If nonzero, symbols from all shared object libraries will be loaded\n\
1623 automatically when the inferior begins execution. Otherwise, symbols\n\
1624 must be loaded manually, using `sharedlibrary'.",