1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include <sys/types.h>
27 #include <sys/param.h>
30 #ifndef SVR4_SHARED_LIBS
31 /* SunOS shared libs need the nlist structure. */
35 #ifndef DT_MIPS_RLD_MAP
52 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
54 /* On SVR4 systems, for the initial implementation, use some runtime startup
55 symbol as the "startup mapping complete" breakpoint address. The models
56 for SunOS and SVR4 dynamic linking debugger support are different in that
57 SunOS hits one breakpoint when all mapping is complete while using the SVR4
58 debugger support takes two breakpoint hits for each file mapped, and
59 there is no way to know when the "last" one is hit. Both these
60 mechanisms should be tied to a "breakpoint service routine" that
61 gets automatically executed whenever one of the breakpoints indicating
62 a change in mapping is hit. This is a future enhancement. (FIXME) */
64 #define BKPT_AT_SYMBOL 1
66 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
67 static char *bkpt_names
[] = {
68 #ifdef SOLIB_BKPT_NAME
69 SOLIB_BKPT_NAME
, /* Prefer configured name if it exists. */
77 /* Symbols which are used to locate the base of the link map structures. */
79 #ifndef SVR4_SHARED_LIBS
80 static char *debug_base_symbols
[] = {
86 /* local data declarations */
88 #ifndef SVR4_SHARED_LIBS
90 #define LM_ADDR(so) ((so) -> lm.lm_addr)
91 #define LM_NEXT(so) ((so) -> lm.lm_next)
92 #define LM_NAME(so) ((so) -> lm.lm_name)
93 /* Test for first link map entry; first entry is a shared library. */
94 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
95 static struct link_dynamic dynamic_copy
;
96 static struct link_dynamic_2 ld_2_copy
;
97 static struct ld_debug debug_copy
;
98 static CORE_ADDR debug_addr
;
99 static CORE_ADDR flag_addr
;
101 #else /* SVR4_SHARED_LIBS */
103 #define LM_ADDR(so) ((so) -> lm.l_addr)
104 #define LM_NEXT(so) ((so) -> lm.l_next)
105 #define LM_NAME(so) ((so) -> lm.l_name)
106 /* Test for first link map entry; first entry is the exec-file. */
107 #define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
108 static struct r_debug debug_copy
;
109 char shadow_contents
[BREAKPOINT_MAX
]; /* Stash old bkpt addr contents */
111 #endif /* !SVR4_SHARED_LIBS */
114 struct so_list
*next
; /* next structure in linked list */
115 struct link_map lm
; /* copy of link map from inferior */
116 struct link_map
*lmaddr
; /* addr in inferior lm was read from */
117 CORE_ADDR lmend
; /* upper addr bound of mapped object */
118 char so_name
[MAX_PATH_SIZE
]; /* shared object lib name (FIXME) */
119 char symbols_loaded
; /* flag: symbols read in yet? */
120 char from_tty
; /* flag: print msgs? */
121 struct objfile
*objfile
; /* objfile for loaded lib */
122 struct section_table
*sections
;
123 struct section_table
*sections_end
;
124 struct section_table
*textsection
;
128 static struct so_list
*so_list_head
; /* List of known shared objects */
129 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
130 static CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
133 fdmatch
PARAMS ((int, int)); /* In libiberty */
135 /* Local function prototypes */
138 special_symbol_handling
PARAMS ((struct so_list
*));
141 sharedlibrary_command
PARAMS ((char *, int));
144 enable_break
PARAMS ((void));
147 disable_break
PARAMS ((void));
150 info_sharedlibrary_command
PARAMS ((char *, int));
153 symbol_add_stub
PARAMS ((char *));
155 static struct so_list
*
156 find_solib
PARAMS ((struct so_list
*));
158 static struct link_map
*
159 first_link_map_member
PARAMS ((void));
162 locate_base
PARAMS ((void));
165 solib_map_sections
PARAMS ((struct so_list
*));
167 #ifdef SVR4_SHARED_LIBS
170 elf_locate_base
PARAMS ((void));
175 solib_add_common_symbols
PARAMS ((struct rtc_symb
*, struct objfile
*));
183 solib_map_sections -- open bfd and build sections for shared lib
187 static void solib_map_sections (struct so_list *so)
191 Given a pointer to one of the shared objects in our list
192 of mapped objects, use the recorded name to open a bfd
193 descriptor for the object, build a section table, and then
194 relocate all the section addresses by the base address at
195 which the shared object was mapped.
199 In most (all?) cases the shared object file name recorded in the
200 dynamic linkage tables will be a fully qualified pathname. For
201 cases where it isn't, do we really mimic the systems search
202 mechanism correctly in the below code (particularly the tilde
207 solib_map_sections (so
)
211 char *scratch_pathname
;
213 struct section_table
*p
;
214 struct cleanup
*old_chain
;
217 filename
= tilde_expand (so
-> so_name
);
218 old_chain
= make_cleanup (free
, filename
);
220 scratch_chan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
222 if (scratch_chan
< 0)
224 scratch_chan
= openp (getenv ("LD_LIBRARY_PATH"), 1, filename
,
225 O_RDONLY
, 0, &scratch_pathname
);
227 if (scratch_chan
< 0)
229 perror_with_name (filename
);
231 /* Leave scratch_pathname allocated. abfd->name will point to it. */
233 abfd
= bfd_fdopenr (scratch_pathname
, gnutarget
, scratch_chan
);
236 close (scratch_chan
);
237 error ("Could not open `%s' as an executable file: %s",
238 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
240 /* Leave bfd open, core_xfer_memory and "info files" need it. */
242 abfd
-> cacheable
= true;
244 if (!bfd_check_format (abfd
, bfd_object
))
246 error ("\"%s\": not in executable format: %s.",
247 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
249 if (build_section_table (abfd
, &so
-> sections
, &so
-> sections_end
))
251 error ("Can't find the file sections in `%s': %s",
252 bfd_get_filename (abfd
), bfd_errmsg (bfd_get_error ()));
255 for (p
= so
-> sections
; p
< so
-> sections_end
; p
++)
257 /* Relocate the section binding addresses as recorded in the shared
258 object's file by the base address to which the object was actually
260 p
-> addr
+= (CORE_ADDR
) LM_ADDR (so
);
261 p
-> endaddr
+= (CORE_ADDR
) LM_ADDR (so
);
262 so
-> lmend
= (CORE_ADDR
) max (p
-> endaddr
, so
-> lmend
);
263 if (STREQ (p
-> the_bfd_section
-> name
, ".text"))
265 so
-> textsection
= p
;
269 /* Free the file names, close the file now. */
270 do_cleanups (old_chain
);
273 /* Read all dynamically loaded common symbol definitions from the inferior
274 and add them to the minimal symbol table for the shared library objfile. */
276 #ifndef SVR4_SHARED_LIBS
278 /* In GDB 4.9 this routine was a real performance hog. According to
279 some gprof data which mtranle@paris.IntelliCorp.COM (Minh Tran-Le)
280 sent, almost all the time spend in solib_add (up to 20 minutes with
281 35 shared libraries) was spent here, with 5/6 in
282 lookup_minimal_symbol and 1/6 in read_memory.
284 To fix this, we moved the call to special_symbol_handling out of the
285 loop in solib_add, so this only gets called once, rather than once
286 for every shared library, and also removed the call to lookup_minimal_symbol
290 solib_add_common_symbols (rtc_symp
, objfile
)
291 struct rtc_symb
*rtc_symp
;
292 struct objfile
*objfile
;
294 struct rtc_symb inferior_rtc_symb
;
295 struct nlist inferior_rtc_nlist
;
300 init_minimal_symbol_collection ();
301 make_cleanup (discard_minimal_symbols
, 0);
305 read_memory ((CORE_ADDR
) rtc_symp
,
306 (char *) &inferior_rtc_symb
,
307 sizeof (inferior_rtc_symb
));
308 read_memory ((CORE_ADDR
) inferior_rtc_symb
.rtc_sp
,
309 (char *) &inferior_rtc_nlist
,
310 sizeof(inferior_rtc_nlist
));
311 if (inferior_rtc_nlist
.n_type
== N_COMM
)
313 /* FIXME: The length of the symbol name is not available, but in the
314 current implementation the common symbol is allocated immediately
315 behind the name of the symbol. */
316 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
318 origname
= name
= xmalloc (len
);
319 read_memory ((CORE_ADDR
) inferior_rtc_nlist
.n_un
.n_name
, name
, len
);
321 /* Don't enter the symbol twice if the target is re-run. */
323 if (name
[0] == bfd_get_symbol_leading_char (objfile
->obfd
))
329 /* I think this is unnecessary, GDB can probably deal with
330 duplicate minimal symbols, more or less. And the duplication
331 which used to happen because this was called for each shared
332 library is gone now that we are just called once. */
333 /* FIXME: Do we really want to exclude symbols which happen
334 to match symbols for other locations in the inferior's
335 address space, even when they are in different linkage units? */
336 if (lookup_minimal_symbol (name
, (struct objfile
*) NULL
) == NULL
)
339 name
= obsavestring (name
, strlen (name
),
340 &objfile
-> symbol_obstack
);
341 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
346 rtc_symp
= inferior_rtc_symb
.rtc_next
;
349 /* Install any minimal symbols that have been collected as the current
350 minimal symbols for this objfile. */
352 install_minimal_symbols (objfile
);
355 #endif /* SVR4_SHARED_LIBS */
358 #ifdef SVR4_SHARED_LIBS
364 elf_locate_base -- locate the base address of dynamic linker structs
365 for SVR4 elf targets.
369 CORE_ADDR elf_locate_base (void)
373 For SVR4 elf targets the address of the dynamic linker's runtime
374 structure is contained within the dynamic info section in the
375 executable file. The dynamic section is also mapped into the
376 inferior address space. Because the runtime loader fills in the
377 real address before starting the inferior, we have to read in the
378 dynamic info section from the inferior address space.
379 If there are any errors while trying to find the address, we
380 silently return 0, otherwise the found address is returned.
387 struct elf_internal_shdr
*dyninfo_sect
;
388 int dyninfo_sect_size
;
389 CORE_ADDR dyninfo_addr
;
393 /* Find the start address of the .dynamic section. */
394 if (exec_bfd
== NULL
|| bfd_get_flavour (exec_bfd
) != bfd_target_elf_flavour
)
396 dyninfo_sect
= bfd_elf_find_section (exec_bfd
, ".dynamic");
397 if (dyninfo_sect
== NULL
)
399 dyninfo_addr
= dyninfo_sect
->sh_addr
;
401 /* Read in .dynamic section, silently ignore errors. */
402 dyninfo_sect_size
= dyninfo_sect
->sh_size
;
403 buf
= alloca (dyninfo_sect_size
);
404 if (target_read_memory (dyninfo_addr
, buf
, dyninfo_sect_size
))
407 /* Find the DT_DEBUG entry in the the .dynamic section.
408 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
409 no DT_DEBUG entries. */
410 /* FIXME: In lack of a 64 bit ELF ABI the following code assumes
411 a 32 bit ELF ABI target. */
412 for (bufend
= buf
+ dyninfo_sect_size
;
414 buf
+= sizeof (Elf32_External_Dyn
))
416 Elf32_External_Dyn
*x_dynp
= (Elf32_External_Dyn
*)buf
;
420 dyn_tag
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_tag
);
421 if (dyn_tag
== DT_NULL
)
423 else if (dyn_tag
== DT_DEBUG
)
425 dyn_ptr
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
428 else if (dyn_tag
== DT_MIPS_RLD_MAP
)
430 char pbuf
[TARGET_PTR_BIT
/ HOST_CHAR_BIT
];
432 /* DT_MIPS_RLD_MAP contains a pointer to the address
433 of the dynamic link structure. */
434 dyn_ptr
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
435 if (target_read_memory (dyn_ptr
, pbuf
, sizeof (pbuf
)))
437 return extract_unsigned_integer (pbuf
, sizeof (pbuf
));
441 /* DT_DEBUG entry not found. */
445 #endif /* SVR4_SHARED_LIBS */
451 locate_base -- locate the base address of dynamic linker structs
455 CORE_ADDR locate_base (void)
459 For both the SunOS and SVR4 shared library implementations, if the
460 inferior executable has been linked dynamically, there is a single
461 address somewhere in the inferior's data space which is the key to
462 locating all of the dynamic linker's runtime structures. This
463 address is the value of the debug base symbol. The job of this
464 function is to find and return that address, or to return 0 if there
465 is no such address (the executable is statically linked for example).
467 For SunOS, the job is almost trivial, since the dynamic linker and
468 all of it's structures are statically linked to the executable at
469 link time. Thus the symbol for the address we are looking for has
470 already been added to the minimal symbol table for the executable's
471 objfile at the time the symbol file's symbols were read, and all we
472 have to do is look it up there. Note that we explicitly do NOT want
473 to find the copies in the shared library.
475 The SVR4 version is a bit more complicated because the address
476 is contained somewhere in the dynamic info section. We have to go
477 to a lot more work to discover the address of the debug base symbol.
478 Because of this complexity, we cache the value we find and return that
479 value on subsequent invocations. Note there is no copy in the
480 executable symbol tables.
488 #ifndef SVR4_SHARED_LIBS
490 struct minimal_symbol
*msymbol
;
491 CORE_ADDR address
= 0;
494 /* For SunOS, we want to limit the search for the debug base symbol to the
495 executable being debugged, since there is a duplicate named symbol in the
496 shared library. We don't want the shared library versions. */
498 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
500 msymbol
= lookup_minimal_symbol (*symbolp
, symfile_objfile
);
501 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
503 address
= SYMBOL_VALUE_ADDRESS (msymbol
);
509 #else /* SVR4_SHARED_LIBS */
511 /* Check to see if we have a currently valid address, and if so, avoid
512 doing all this work again and just return the cached address. If
513 we have no cached address, try to locate it in the dynamic info
518 debug_base
= elf_locate_base ();
522 #endif /* !SVR4_SHARED_LIBS */
530 first_link_map_member -- locate first member in dynamic linker's map
534 static struct link_map *first_link_map_member (void)
538 Read in a copy of the first member in the inferior's dynamic
539 link map from the inferior's dynamic linker structures, and return
540 a pointer to the copy in our address space.
543 static struct link_map
*
544 first_link_map_member ()
546 struct link_map
*lm
= NULL
;
548 #ifndef SVR4_SHARED_LIBS
550 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
551 if (dynamic_copy
.ld_version
>= 2)
553 /* It is a version that we can deal with, so read in the secondary
554 structure and find the address of the link map list from it. */
555 read_memory ((CORE_ADDR
) dynamic_copy
.ld_un
.ld_2
, (char *) &ld_2_copy
,
556 sizeof (struct link_dynamic_2
));
557 lm
= ld_2_copy
.ld_loaded
;
560 #else /* SVR4_SHARED_LIBS */
562 read_memory (debug_base
, (char *) &debug_copy
, sizeof (struct r_debug
));
563 /* FIXME: Perhaps we should validate the info somehow, perhaps by
564 checking r_version for a known version number, or r_state for
566 lm
= debug_copy
.r_map
;
568 #endif /* !SVR4_SHARED_LIBS */
577 find_solib -- step through list of shared objects
581 struct so_list *find_solib (struct so_list *so_list_ptr)
585 This module contains the routine which finds the names of any
586 loaded "images" in the current process. The argument in must be
587 NULL on the first call, and then the returned value must be passed
588 in on subsequent calls. This provides the capability to "step" down
589 the list of loaded objects. On the last object, a NULL value is
592 The arg and return value are "struct link_map" pointers, as defined
596 static struct so_list
*
597 find_solib (so_list_ptr
)
598 struct so_list
*so_list_ptr
; /* Last lm or NULL for first one */
600 struct so_list
*so_list_next
= NULL
;
601 struct link_map
*lm
= NULL
;
604 if (so_list_ptr
== NULL
)
606 /* We are setting up for a new scan through the loaded images. */
607 if ((so_list_next
= so_list_head
) == NULL
)
609 /* We have not already read in the dynamic linking structures
610 from the inferior, lookup the address of the base structure. */
611 debug_base
= locate_base ();
614 /* Read the base structure in and find the address of the first
615 link map list member. */
616 lm
= first_link_map_member ();
622 /* We have been called before, and are in the process of walking
623 the shared library list. Advance to the next shared object. */
624 if ((lm
= LM_NEXT (so_list_ptr
)) == NULL
)
626 /* We have hit the end of the list, so check to see if any were
627 added, but be quiet if we can't read from the target any more. */
628 int status
= target_read_memory ((CORE_ADDR
) so_list_ptr
-> lmaddr
,
629 (char *) &(so_list_ptr
-> lm
),
630 sizeof (struct link_map
));
633 lm
= LM_NEXT (so_list_ptr
);
640 so_list_next
= so_list_ptr
-> next
;
642 if ((so_list_next
== NULL
) && (lm
!= NULL
))
644 /* Get next link map structure from inferior image and build a local
645 abbreviated load_map structure */
646 new = (struct so_list
*) xmalloc (sizeof (struct so_list
));
647 memset ((char *) new, 0, sizeof (struct so_list
));
649 /* Add the new node as the next node in the list, or as the root
650 node if this is the first one. */
651 if (so_list_ptr
!= NULL
)
653 so_list_ptr
-> next
= new;
660 read_memory ((CORE_ADDR
) lm
, (char *) &(new -> lm
),
661 sizeof (struct link_map
));
662 /* For SVR4 versions, the first entry in the link map is for the
663 inferior executable, so we must ignore it. For some versions of
664 SVR4, it has no name. For others (Solaris 2.3 for example), it
665 does have a name, so we can no longer use a missing name to
666 decide when to ignore it. */
667 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm
))
671 target_read_string ((CORE_ADDR
) LM_NAME (new), &buffer
,
672 MAX_PATH_SIZE
- 1, &errcode
);
674 error ("find_solib: Can't read pathname for load map: %s\n",
675 safe_strerror (errcode
));
676 strncpy (new -> so_name
, buffer
, MAX_PATH_SIZE
- 1);
677 new -> so_name
[MAX_PATH_SIZE
- 1] = '\0';
679 solib_map_sections (new);
682 return (so_list_next
);
685 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
688 symbol_add_stub (arg
)
691 register struct so_list
*so
= (struct so_list
*) arg
; /* catch_errs bogon */
693 so
-> objfile
= symbol_file_add (so
-> so_name
, so
-> from_tty
,
694 (unsigned int) so
-> textsection
-> addr
,
703 solib_add -- add a shared library file to the symtab and section list
707 void solib_add (char *arg_string, int from_tty,
708 struct target_ops *target)
715 solib_add (arg_string
, from_tty
, target
)
718 struct target_ops
*target
;
720 register struct so_list
*so
= NULL
; /* link map state variable */
722 /* Last shared library that we read. */
723 struct so_list
*so_last
= NULL
;
729 if ((re_err
= re_comp (arg_string
? arg_string
: ".")) != NULL
)
731 error ("Invalid regexp: %s", re_err
);
734 /* Add the shared library sections to the section table of the
735 specified target, if any. We have to do this before reading the
736 symbol files as symbol_file_add calls reinit_frame_cache and
737 creating a new frame might access memory in the shared library. */
740 /* Count how many new section_table entries there are. */
743 while ((so
= find_solib (so
)) != NULL
)
745 if (so
-> so_name
[0])
747 count
+= so
-> sections_end
- so
-> sections
;
753 /* Reallocate the target's section table including the new size. */
754 if (target
-> to_sections
)
756 old
= target
-> to_sections_end
- target
-> to_sections
;
757 target
-> to_sections
= (struct section_table
*)
758 xrealloc ((char *)target
-> to_sections
,
759 (sizeof (struct section_table
)) * (count
+ old
));
764 target
-> to_sections
= (struct section_table
*)
765 xmalloc ((sizeof (struct section_table
)) * count
);
767 target
-> to_sections_end
= target
-> to_sections
+ (count
+ old
);
769 /* Add these section table entries to the target's table. */
770 while ((so
= find_solib (so
)) != NULL
)
772 if (so
-> so_name
[0])
774 count
= so
-> sections_end
- so
-> sections
;
775 memcpy ((char *) (target
-> to_sections
+ old
),
777 (sizeof (struct section_table
)) * count
);
784 /* Now add the symbol files. */
785 while ((so
= find_solib (so
)) != NULL
)
787 if (so
-> so_name
[0] && re_exec (so
-> so_name
))
789 so
-> from_tty
= from_tty
;
790 if (so
-> symbols_loaded
)
794 printf_unfiltered ("Symbols already loaded for %s\n", so
-> so_name
);
797 else if (catch_errors
798 (symbol_add_stub
, (char *) so
,
799 "Error while reading shared library symbols:\n",
803 so
-> symbols_loaded
= 1;
808 /* Calling this once at the end means that we put all the minimal
809 symbols for commons into the objfile for the last shared library.
810 Since they are in common, this should not be a problem. If we
811 delete the objfile with the minimal symbols, we can put all the
812 symbols into a new objfile (and will on the next call to solib_add).
814 An alternate approach would be to create an objfile just for
815 common minsyms, thus not needing any objfile argument to
816 solib_add_common_symbols. */
819 special_symbol_handling (so_last
);
826 info_sharedlibrary_command -- code for "info sharedlibrary"
830 static void info_sharedlibrary_command ()
834 Walk through the shared library list and print information
835 about each attached library.
839 info_sharedlibrary_command (ignore
, from_tty
)
843 register struct so_list
*so
= NULL
; /* link map state variable */
846 if (exec_bfd
== NULL
)
848 printf_unfiltered ("No exec file.\n");
851 while ((so
= find_solib (so
)) != NULL
)
853 if (so
-> so_name
[0])
857 printf_unfiltered("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
858 "Shared Object Library");
861 /* FIXME-32x64: need print_address_numeric with field width or
863 printf_unfiltered ("%-12s",
864 local_hex_string_custom ((unsigned long) LM_ADDR (so
),
866 printf_unfiltered ("%-12s",
867 local_hex_string_custom ((unsigned long) so
-> lmend
,
869 printf_unfiltered ("%-12s", so
-> symbols_loaded
? "Yes" : "No");
870 printf_unfiltered ("%s\n", so
-> so_name
);
873 if (so_list_head
== NULL
)
875 printf_unfiltered ("No shared libraries loaded at this time.\n");
883 solib_address -- check to see if an address is in a shared lib
887 int solib_address (CORE_ADDR address)
891 Provides a hook for other gdb routines to discover whether or
892 not a particular address is within the mapped address space of
893 a shared library. Any address between the base mapping address
894 and the first address beyond the end of the last mapping, is
895 considered to be within the shared library address space, for
898 For example, this routine is called at one point to disable
899 breakpoints which are in shared libraries that are not currently
904 solib_address (address
)
907 register struct so_list
*so
= 0; /* link map state variable */
909 while ((so
= find_solib (so
)) != NULL
)
911 if (so
-> so_name
[0])
913 if ((address
>= (CORE_ADDR
) LM_ADDR (so
)) &&
914 (address
< (CORE_ADDR
) so
-> lmend
))
923 /* Called by free_all_symtabs */
928 struct so_list
*next
;
933 if (so_list_head
-> sections
)
935 free ((PTR
)so_list_head
-> sections
);
937 if (so_list_head
-> abfd
)
939 bfd_filename
= bfd_get_filename (so_list_head
-> abfd
);
940 bfd_close (so_list_head
-> abfd
);
943 /* This happens for the executable on SVR4. */
946 next
= so_list_head
-> next
;
948 free ((PTR
)bfd_filename
);
949 free ((PTR
)so_list_head
);
959 disable_break -- remove the "mapping changed" breakpoint
963 static int disable_break ()
967 Removes the breakpoint that gets hit when the dynamic linker
968 completes a mapping change.
977 #ifndef SVR4_SHARED_LIBS
981 /* Read the debugger structure from the inferior to retrieve the
982 address of the breakpoint and the original contents of the
983 breakpoint address. Remove the breakpoint by writing the original
986 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
988 /* Set `in_debugger' to zero now. */
990 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
992 breakpoint_addr
= (CORE_ADDR
) debug_copy
.ldd_bp_addr
;
993 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
994 sizeof (debug_copy
.ldd_bp_inst
));
996 #else /* SVR4_SHARED_LIBS */
998 /* Note that breakpoint address and original contents are in our address
999 space, so we just need to write the original contents back. */
1001 if (memory_remove_breakpoint (breakpoint_addr
, shadow_contents
) != 0)
1006 #endif /* !SVR4_SHARED_LIBS */
1008 /* For the SVR4 version, we always know the breakpoint address. For the
1009 SunOS version we don't know it until the above code is executed.
1010 Grumble if we are stopped anywhere besides the breakpoint address. */
1012 if (stop_pc
!= breakpoint_addr
)
1014 warning ("stopped at unknown breakpoint while handling shared libraries");
1024 enable_break -- arrange for dynamic linker to hit breakpoint
1028 int enable_break (void)
1032 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1033 debugger interface, support for arranging for the inferior to hit
1034 a breakpoint after mapping in the shared libraries. This function
1035 enables that breakpoint.
1037 For SunOS, there is a special flag location (in_debugger) which we
1038 set to 1. When the dynamic linker sees this flag set, it will set
1039 a breakpoint at a location known only to itself, after saving the
1040 original contents of that place and the breakpoint address itself,
1041 in it's own internal structures. When we resume the inferior, it
1042 will eventually take a SIGTRAP when it runs into the breakpoint.
1043 We handle this (in a different place) by restoring the contents of
1044 the breakpointed location (which is only known after it stops),
1045 chasing around to locate the shared libraries that have been
1046 loaded, then resuming.
1048 For SVR4, the debugger interface structure contains a member (r_brk)
1049 which is statically initialized at the time the shared library is
1050 built, to the offset of a function (_r_debug_state) which is guaran-
1051 teed to be called once before mapping in a library, and again when
1052 the mapping is complete. At the time we are examining this member,
1053 it contains only the unrelocated offset of the function, so we have
1054 to do our own relocation. Later, when the dynamic linker actually
1055 runs, it relocates r_brk to be the actual address of _r_debug_state().
1057 The debugger interface structure also contains an enumeration which
1058 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1059 depending upon whether or not the library is being mapped or unmapped,
1060 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1068 #ifndef SVR4_SHARED_LIBS
1073 /* Get link_dynamic structure */
1075 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1076 sizeof (dynamic_copy
));
1083 /* Calc address of debugger interface structure */
1085 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1087 /* Calc address of `in_debugger' member of debugger interface structure */
1089 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
1090 (char *) &debug_copy
);
1092 /* Write a value of 1 to this member. */
1095 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
1098 #else /* SVR4_SHARED_LIBS */
1100 #ifdef BKPT_AT_SYMBOL
1102 struct minimal_symbol
*msymbol
;
1104 CORE_ADDR bkpt_addr
;
1106 /* Scan through the list of symbols, trying to look up the symbol and
1107 set a breakpoint there. Terminate loop when we/if we succeed. */
1109 breakpoint_addr
= 0;
1110 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1112 msymbol
= lookup_minimal_symbol (*bkpt_namep
, symfile_objfile
);
1113 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1115 bkpt_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1116 if (target_insert_breakpoint (bkpt_addr
, shadow_contents
) == 0)
1118 breakpoint_addr
= bkpt_addr
;
1125 #else /* !BKPT_AT_SYMBOL */
1127 struct symtab_and_line sal
;
1129 /* Read the debugger interface structure directly. */
1131 read_memory (debug_base
, (char *) &debug_copy
, sizeof (debug_copy
));
1133 /* Set breakpoint at the debugger interface stub routine that will
1134 be called just prior to each mapping change and again after the
1135 mapping change is complete. Set up the (nonexistent) handler to
1136 deal with hitting these breakpoints. (FIXME). */
1138 warning ("'%s': line %d: missing SVR4 support code", __FILE__
, __LINE__
);
1141 #endif /* BKPT_AT_SYMBOL */
1143 #endif /* !SVR4_SHARED_LIBS */
1152 solib_create_inferior_hook -- shared library startup support
1156 void solib_create_inferior_hook()
1160 When gdb starts up the inferior, it nurses it along (through the
1161 shell) until it is ready to execute it's first instruction. At this
1162 point, this function gets called via expansion of the macro
1163 SOLIB_CREATE_INFERIOR_HOOK.
1165 For SunOS executables, this first instruction is typically the
1166 one at "_start", or a similar text label, regardless of whether
1167 the executable is statically or dynamically linked. The runtime
1168 startup code takes care of dynamically linking in any shared
1169 libraries, once gdb allows the inferior to continue.
1171 For SVR4 executables, this first instruction is either the first
1172 instruction in the dynamic linker (for dynamically linked
1173 executables) or the instruction at "start" for statically linked
1174 executables. For dynamically linked executables, the system
1175 first exec's /lib/libc.so.N, which contains the dynamic linker,
1176 and starts it running. The dynamic linker maps in any needed
1177 shared libraries, maps in the actual user executable, and then
1178 jumps to "start" in the user executable.
1180 For both SunOS shared libraries, and SVR4 shared libraries, we
1181 can arrange to cooperate with the dynamic linker to discover the
1182 names of shared libraries that are dynamically linked, and the
1183 base addresses to which they are linked.
1185 This function is responsible for discovering those names and
1186 addresses, and saving sufficient information about them to allow
1187 their symbols to be read at a later time.
1191 Between enable_break() and disable_break(), this code does not
1192 properly handle hitting breakpoints which the user might have
1193 set in the startup code or in the dynamic linker itself. Proper
1194 handling will probably have to wait until the implementation is
1195 changed to use the "breakpoint handler function" method.
1197 Also, what if child has exit()ed? Must exit loop somehow.
1201 solib_create_inferior_hook()
1203 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1204 yet. In fact, in the case of a SunOS4 executable being run on
1205 Solaris, we can't get it yet. find_solib will get it when it needs
1207 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1208 if ((debug_base
= locate_base ()) == 0)
1210 /* Can't find the symbol or the executable is statically linked. */
1215 if (!enable_break ())
1217 warning ("shared library handler failed to enable breakpoint");
1221 /* Now run the target. It will eventually hit the breakpoint, at
1222 which point all of the libraries will have been mapped in and we
1223 can go groveling around in the dynamic linker structures to find
1224 out what we need to know about them. */
1226 clear_proceed_status ();
1227 stop_soon_quietly
= 1;
1228 stop_signal
= TARGET_SIGNAL_0
;
1231 target_resume (-1, 0, stop_signal
);
1232 wait_for_inferior ();
1234 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
1235 stop_soon_quietly
= 0;
1237 /* We are now either at the "mapping complete" breakpoint (or somewhere
1238 else, a condition we aren't prepared to deal with anyway), so adjust
1239 the PC as necessary after a breakpoint, disable the breakpoint, and
1240 add any shared libraries that were mapped in. */
1242 if (DECR_PC_AFTER_BREAK
)
1244 stop_pc
-= DECR_PC_AFTER_BREAK
;
1245 write_register (PC_REGNUM
, stop_pc
);
1248 if (!disable_break ())
1250 warning ("shared library handler failed to disable breakpoint");
1253 solib_add ((char *) 0, 0, (struct target_ops
*) 0);
1260 special_symbol_handling -- additional shared library symbol handling
1264 void special_symbol_handling (struct so_list *so)
1268 Once the symbols from a shared object have been loaded in the usual
1269 way, we are called to do any system specific symbol handling that
1272 For Suns, this consists of grunging around in the dynamic linkers
1273 structures to find symbol definitions for "common" symbols and
1274 adding them to the minimal symbol table for the corresponding
1280 special_symbol_handling (so
)
1283 #ifndef SVR4_SHARED_LIBS
1286 if (debug_addr
== 0)
1288 /* Get link_dynamic structure */
1290 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
1291 sizeof (dynamic_copy
));
1298 /* Calc address of debugger interface structure */
1299 /* FIXME, this needs work for cross-debugging of core files
1300 (byteorder, size, alignment, etc). */
1302 debug_addr
= (CORE_ADDR
) dynamic_copy
.ldd
;
1305 /* Read the debugger structure from the inferior, just to make sure
1306 we have a current copy. */
1308 j
= target_read_memory (debug_addr
, (char *) &debug_copy
,
1309 sizeof (debug_copy
));
1311 return; /* unreadable */
1313 /* Get common symbol definitions for the loaded object. */
1315 if (debug_copy
.ldd_cp
)
1317 solib_add_common_symbols (debug_copy
.ldd_cp
, so
-> objfile
);
1320 #endif /* !SVR4_SHARED_LIBS */
1328 sharedlibrary_command -- handle command to explicitly add library
1332 static void sharedlibrary_command (char *args, int from_tty)
1339 sharedlibrary_command (args
, from_tty
)
1344 solib_add (args
, from_tty
, (struct target_ops
*) 0);
1351 add_com ("sharedlibrary", class_files
, sharedlibrary_command
,
1352 "Load shared object library symbols for files matching REGEXP.");
1353 add_info ("sharedlibrary", info_sharedlibrary_command
,
1354 "Status of loaded shared object libraries.");