1 /* Handle SunOS shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2004, 2007, 2008 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
25 #include "gdb_string.h"
26 #include <sys/param.h>
29 /* SunOS shared libs need the nlist structure. */
43 /* The shared library implementation found on BSD a.out systems is
44 very similar to the SunOS implementation. However, the data
45 structures defined in <link.h> are named very differently. Make up
46 for those differences here. */
48 #ifdef HAVE_STRUCT_SO_MAP_WITH_SOM_MEMBERS
50 /* FIXME: Temporary until the equivalent defines have been removed
51 from all nm-*bsd*.h files. */
54 /* Map `struct link_map' and its members. */
55 #define link_map so_map
56 #define lm_addr som_addr
57 #define lm_name som_path
58 #define lm_next som_next
60 /* Map `struct link_dynamic_2' and its members. */
61 #define link_dynamic_2 section_dispatch_table
62 #define ld_loaded sdt_loaded
64 /* Map `struct rtc_symb' and its members. */
65 #define rtc_symb rt_symbol
67 #define rtc_next rt_next
69 /* Map `struct ld_debug' and its members. */
70 #define ld_debug so_debug
71 #define ldd_in_debugger dd_in_debugger
72 #define ldd_bp_addr dd_bpt_addr
73 #define ldd_bp_inst dd_bpt_shadow
76 /* Map `struct link_dynamic' and its members. */
77 #define link_dynamic _dynamic
78 #define ld_version d_version
87 /* Link map info to include in an allocated so_list entry */
91 /* Pointer to copy of link map from inferior. The type is char *
92 rather than void *, so that we may use byte offsets to find the
93 various fields without the need for a cast. */
98 /* Symbols which are used to locate the base of the link map structures. */
100 static char *debug_base_symbols
[] =
107 static char *main_name_list
[] =
113 /* Macro to extract an address from a solib structure. When GDB is
114 configured for some 32-bit targets (e.g. Solaris 2.7 sparc), BFD is
115 configured to handle 64-bit targets, so CORE_ADDR is 64 bits. We
116 have to extract only the significant bits of addresses to get the
117 right address when accessing the core file BFD.
119 Assume that the address is unsigned. */
121 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
122 extract_unsigned_integer (&(MEMBER), sizeof (MEMBER))
124 /* local data declarations */
126 static struct link_dynamic dynamic_copy
;
127 static struct link_dynamic_2 ld_2_copy
;
128 static struct ld_debug debug_copy
;
129 static CORE_ADDR debug_addr
;
130 static CORE_ADDR flag_addr
;
133 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
135 #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER))
137 /* link map access functions */
140 LM_ADDR (struct so_list
*so
)
142 int lm_addr_offset
= offsetof (struct link_map
, lm_addr
);
143 int lm_addr_size
= fieldsize (struct link_map
, lm_addr
);
145 return (CORE_ADDR
) extract_signed_integer (so
->lm_info
->lm
+ lm_addr_offset
,
150 LM_NEXT (struct so_list
*so
)
152 int lm_next_offset
= offsetof (struct link_map
, lm_next
);
153 int lm_next_size
= fieldsize (struct link_map
, lm_next
);
155 /* Assume that the address is unsigned. */
156 return extract_unsigned_integer (so
->lm_info
->lm
+ lm_next_offset
,
161 LM_NAME (struct so_list
*so
)
163 int lm_name_offset
= offsetof (struct link_map
, lm_name
);
164 int lm_name_size
= fieldsize (struct link_map
, lm_name
);
166 /* Assume that the address is unsigned. */
167 return extract_unsigned_integer (so
->lm_info
->lm
+ lm_name_offset
,
171 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
173 /* Local function prototypes */
175 static int match_main (char *);
177 /* Allocate the runtime common object file. */
180 allocate_rt_common_objfile (void)
182 struct objfile
*objfile
;
183 struct objfile
*last_one
;
185 objfile
= (struct objfile
*) xmalloc (sizeof (struct objfile
));
186 memset (objfile
, 0, sizeof (struct objfile
));
187 objfile
->psymbol_cache
= bcache_xmalloc ();
188 objfile
->macro_cache
= bcache_xmalloc ();
189 obstack_init (&objfile
->objfile_obstack
);
190 objfile
->name
= xstrdup ("rt_common");
192 /* Add this file onto the tail of the linked list of other such files. */
194 objfile
->next
= NULL
;
195 if (object_files
== NULL
)
196 object_files
= objfile
;
199 for (last_one
= object_files
;
201 last_one
= last_one
->next
);
202 last_one
->next
= objfile
;
205 rt_common_objfile
= objfile
;
208 /* Read all dynamically loaded common symbol definitions from the inferior
209 and put them into the minimal symbol table for the runtime common
213 solib_add_common_symbols (CORE_ADDR rtc_symp
)
215 struct rtc_symb inferior_rtc_symb
;
216 struct nlist inferior_rtc_nlist
;
220 /* Remove any runtime common symbols from previous runs. */
222 if (rt_common_objfile
!= NULL
&& rt_common_objfile
->minimal_symbol_count
)
224 obstack_free (&rt_common_objfile
->objfile_obstack
, 0);
225 obstack_init (&rt_common_objfile
->objfile_obstack
);
226 rt_common_objfile
->minimal_symbol_count
= 0;
227 rt_common_objfile
->msymbols
= NULL
;
228 terminate_minimal_symbol_table (rt_common_objfile
);
231 init_minimal_symbol_collection ();
232 make_cleanup_discard_minimal_symbols ();
236 read_memory (rtc_symp
,
237 (char *) &inferior_rtc_symb
,
238 sizeof (inferior_rtc_symb
));
239 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb
.rtc_sp
),
240 (char *) &inferior_rtc_nlist
,
241 sizeof (inferior_rtc_nlist
));
242 if (inferior_rtc_nlist
.n_type
== N_COMM
)
244 /* FIXME: The length of the symbol name is not available, but in the
245 current implementation the common symbol is allocated immediately
246 behind the name of the symbol. */
247 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
249 name
= xmalloc (len
);
250 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist
.n_un
.n_name
),
253 /* Allocate the runtime common objfile if necessary. */
254 if (rt_common_objfile
== NULL
)
255 allocate_rt_common_objfile ();
257 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
258 mst_bss
, rt_common_objfile
);
261 rtc_symp
= SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb
.rtc_next
);
264 /* Install any minimal symbols that have been collected as the current
265 minimal symbols for the runtime common objfile. */
267 install_minimal_symbols (rt_common_objfile
);
275 locate_base -- locate the base address of dynamic linker structs
279 CORE_ADDR locate_base (void)
283 For both the SunOS and SVR4 shared library implementations, if the
284 inferior executable has been linked dynamically, there is a single
285 address somewhere in the inferior's data space which is the key to
286 locating all of the dynamic linker's runtime structures. This
287 address is the value of the debug base symbol. The job of this
288 function is to find and return that address, or to return 0 if there
289 is no such address (the executable is statically linked for example).
291 For SunOS, the job is almost trivial, since the dynamic linker and
292 all of it's structures are statically linked to the executable at
293 link time. Thus the symbol for the address we are looking for has
294 already been added to the minimal symbol table for the executable's
295 objfile at the time the symbol file's symbols were read, and all we
296 have to do is look it up there. Note that we explicitly do NOT want
297 to find the copies in the shared library.
299 The SVR4 version is a bit more complicated because the address
300 is contained somewhere in the dynamic info section. We have to go
301 to a lot more work to discover the address of the debug base symbol.
302 Because of this complexity, we cache the value we find and return that
303 value on subsequent invocations. Note there is no copy in the
304 executable symbol tables.
311 struct minimal_symbol
*msymbol
;
312 CORE_ADDR address
= 0;
315 /* For SunOS, we want to limit the search for the debug base symbol to the
316 executable being debugged, since there is a duplicate named symbol in the
317 shared library. We don't want the shared library versions. */
319 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
321 msymbol
= lookup_minimal_symbol (*symbolp
, NULL
, symfile_objfile
);
322 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
324 address
= SYMBOL_VALUE_ADDRESS (msymbol
);
335 first_link_map_member -- locate first member in dynamic linker's map
339 static CORE_ADDR first_link_map_member (void)
343 Find the first element in the inferior's dynamic link map, and
344 return its address in the inferior. This function doesn't copy the
345 link map entry itself into our address space; current_sos actually
349 first_link_map_member (void)
353 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
354 if (dynamic_copy
.ld_version
>= 2)
356 /* It is a version that we can deal with, so read in the secondary
357 structure and find the address of the link map list from it. */
358 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ld_un
.ld_2
),
359 (char *) &ld_2_copy
, sizeof (struct link_dynamic_2
));
360 lm
= SOLIB_EXTRACT_ADDRESS (ld_2_copy
.ld_loaded
);
366 open_symbol_file_object (void *from_ttyp
)
374 current_sos -- build a list of currently loaded shared objects
378 struct so_list *current_sos ()
382 Build a list of `struct so_list' objects describing the shared
383 objects currently loaded in the inferior. This list does not
384 include an entry for the main executable file.
386 Note that we only gather information directly available from the
387 inferior --- we don't examine any of the shared library files
388 themselves. The declaration of `struct so_list' says which fields
389 we provide values for. */
391 static struct so_list
*
392 sunos_current_sos (void)
395 struct so_list
*head
= 0;
396 struct so_list
**link_ptr
= &head
;
400 /* Make sure we've looked up the inferior's dynamic linker's base
404 debug_base
= locate_base ();
406 /* If we can't find the dynamic linker's base structure, this
407 must not be a dynamically linked executable. Hmm. */
412 /* Walk the inferior's link map list, and build our list of
413 `struct so_list' nodes. */
414 lm
= first_link_map_member ();
418 = (struct so_list
*) xmalloc (sizeof (struct so_list
));
419 struct cleanup
*old_chain
= make_cleanup (xfree
, new);
421 memset (new, 0, sizeof (*new));
423 new->lm_info
= xmalloc (sizeof (struct lm_info
));
424 make_cleanup (xfree
, new->lm_info
);
426 new->lm_info
->lm
= xmalloc (sizeof (struct link_map
));
427 make_cleanup (xfree
, new->lm_info
->lm
);
428 memset (new->lm_info
->lm
, 0, sizeof (struct link_map
));
430 read_memory (lm
, new->lm_info
->lm
, sizeof (struct link_map
));
434 /* Extract this shared object's name. */
435 target_read_string (LM_NAME (new), &buffer
,
436 SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
438 warning (_("Can't read pathname for load map: %s."),
439 safe_strerror (errcode
));
442 strncpy (new->so_name
, buffer
, SO_NAME_MAX_PATH_SIZE
- 1);
443 new->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
445 strcpy (new->so_original_name
, new->so_name
);
448 /* If this entry has no name, or its name matches the name
449 for the main executable, don't include it in the list. */
450 if (! new->so_name
[0]
451 || match_main (new->so_name
))
457 link_ptr
= &new->next
;
460 discard_cleanups (old_chain
);
467 /* On some systems, the only way to recognize the link map entry for
468 the main executable file is by looking at its name. Return
469 non-zero iff SONAME matches one of the known main executable names. */
472 match_main (char *soname
)
476 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
478 if (strcmp (soname
, *mainp
) == 0)
487 sunos_in_dynsym_resolve_code (CORE_ADDR pc
)
496 disable_break -- remove the "mapping changed" breakpoint
500 static int disable_break ()
504 Removes the breakpoint that gets hit when the dynamic linker
505 completes a mapping change.
512 CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
516 /* Read the debugger structure from the inferior to retrieve the
517 address of the breakpoint and the original contents of the
518 breakpoint address. Remove the breakpoint by writing the original
521 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
523 /* Set `in_debugger' to zero now. */
525 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
527 breakpoint_addr
= SOLIB_EXTRACT_ADDRESS (debug_copy
.ldd_bp_addr
);
528 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
529 sizeof (debug_copy
.ldd_bp_inst
));
531 /* For the SVR4 version, we always know the breakpoint address. For the
532 SunOS version we don't know it until the above code is executed.
533 Grumble if we are stopped anywhere besides the breakpoint address. */
535 if (stop_pc
!= breakpoint_addr
)
537 warning (_("stopped at unknown breakpoint while handling shared libraries"));
548 enable_break -- arrange for dynamic linker to hit breakpoint
552 int enable_break (void)
556 Both the SunOS and the SVR4 dynamic linkers have, as part of their
557 debugger interface, support for arranging for the inferior to hit
558 a breakpoint after mapping in the shared libraries. This function
559 enables that breakpoint.
561 For SunOS, there is a special flag location (in_debugger) which we
562 set to 1. When the dynamic linker sees this flag set, it will set
563 a breakpoint at a location known only to itself, after saving the
564 original contents of that place and the breakpoint address itself,
565 in it's own internal structures. When we resume the inferior, it
566 will eventually take a SIGTRAP when it runs into the breakpoint.
567 We handle this (in a different place) by restoring the contents of
568 the breakpointed location (which is only known after it stops),
569 chasing around to locate the shared libraries that have been
570 loaded, then resuming.
572 For SVR4, the debugger interface structure contains a member (r_brk)
573 which is statically initialized at the time the shared library is
574 built, to the offset of a function (_r_debug_state) which is guaran-
575 teed to be called once before mapping in a library, and again when
576 the mapping is complete. At the time we are examining this member,
577 it contains only the unrelocated offset of the function, so we have
578 to do our own relocation. Later, when the dynamic linker actually
579 runs, it relocates r_brk to be the actual address of _r_debug_state().
581 The debugger interface structure also contains an enumeration which
582 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
583 depending upon whether or not the library is being mapped or unmapped,
584 and then set to RT_CONSISTENT after the library is mapped/unmapped.
594 /* Get link_dynamic structure */
596 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
597 sizeof (dynamic_copy
));
604 /* Calc address of debugger interface structure */
606 debug_addr
= SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ldd
);
608 /* Calc address of `in_debugger' member of debugger interface structure */
610 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
611 (char *) &debug_copy
);
613 /* Write a value of 1 to this member. */
616 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
626 special_symbol_handling -- additional shared library symbol handling
630 void special_symbol_handling ()
634 Once the symbols from a shared object have been loaded in the usual
635 way, we are called to do any system specific symbol handling that
638 For SunOS4, this consists of grunging around in the dynamic
639 linkers structures to find symbol definitions for "common" symbols
640 and adding them to the minimal symbol table for the runtime common
646 sunos_special_symbol_handling (void)
652 /* Get link_dynamic structure */
654 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
655 sizeof (dynamic_copy
));
662 /* Calc address of debugger interface structure */
663 /* FIXME, this needs work for cross-debugging of core files
664 (byteorder, size, alignment, etc). */
666 debug_addr
= SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ldd
);
669 /* Read the debugger structure from the inferior, just to make sure
670 we have a current copy. */
672 j
= target_read_memory (debug_addr
, (char *) &debug_copy
,
673 sizeof (debug_copy
));
675 return; /* unreadable */
677 /* Get common symbol definitions for the loaded object. */
679 if (debug_copy
.ldd_cp
)
681 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy
.ldd_cp
));
689 sunos_solib_create_inferior_hook -- shared library startup support
693 void sunos_solib_create_inferior_hook ()
697 When gdb starts up the inferior, it nurses it along (through the
698 shell) until it is ready to execute it's first instruction. At this
699 point, this function gets called via expansion of the macro
700 SOLIB_CREATE_INFERIOR_HOOK.
702 For SunOS executables, this first instruction is typically the
703 one at "_start", or a similar text label, regardless of whether
704 the executable is statically or dynamically linked. The runtime
705 startup code takes care of dynamically linking in any shared
706 libraries, once gdb allows the inferior to continue.
708 For SVR4 executables, this first instruction is either the first
709 instruction in the dynamic linker (for dynamically linked
710 executables) or the instruction at "start" for statically linked
711 executables. For dynamically linked executables, the system
712 first exec's /lib/libc.so.N, which contains the dynamic linker,
713 and starts it running. The dynamic linker maps in any needed
714 shared libraries, maps in the actual user executable, and then
715 jumps to "start" in the user executable.
717 For both SunOS shared libraries, and SVR4 shared libraries, we
718 can arrange to cooperate with the dynamic linker to discover the
719 names of shared libraries that are dynamically linked, and the
720 base addresses to which they are linked.
722 This function is responsible for discovering those names and
723 addresses, and saving sufficient information about them to allow
724 their symbols to be read at a later time.
728 Between enable_break() and disable_break(), this code does not
729 properly handle hitting breakpoints which the user might have
730 set in the startup code or in the dynamic linker itself. Proper
731 handling will probably have to wait until the implementation is
732 changed to use the "breakpoint handler function" method.
734 Also, what if child has exit()ed? Must exit loop somehow.
738 sunos_solib_create_inferior_hook (void)
740 if ((debug_base
= locate_base ()) == 0)
742 /* Can't find the symbol or the executable is statically linked. */
746 if (!enable_break ())
748 warning (_("shared library handler failed to enable breakpoint"));
752 /* SCO and SunOS need the loop below, other systems should be using the
753 special shared library breakpoints and the shared library breakpoint
756 Now run the target. It will eventually hit the breakpoint, at
757 which point all of the libraries will have been mapped in and we
758 can go groveling around in the dynamic linker structures to find
759 out what we need to know about them. */
761 clear_proceed_status ();
762 stop_soon
= STOP_QUIETLY
;
763 stop_signal
= TARGET_SIGNAL_0
;
766 target_resume (pid_to_ptid (-1), 0, stop_signal
);
767 wait_for_inferior (0);
769 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
770 stop_soon
= NO_STOP_QUIETLY
;
772 /* We are now either at the "mapping complete" breakpoint (or somewhere
773 else, a condition we aren't prepared to deal with anyway), so adjust
774 the PC as necessary after a breakpoint, disable the breakpoint, and
775 add any shared libraries that were mapped in.
777 Note that adjust_pc_after_break did not perform any PC adjustment,
778 as the breakpoint the inferior just hit was not inserted by GDB,
779 but by the dynamic loader itself, and is therefore not found on
780 the GDB software break point list. Thus we have to adjust the
783 if (gdbarch_decr_pc_after_break (target_gdbarch
))
785 stop_pc
-= gdbarch_decr_pc_after_break (target_gdbarch
);
789 if (!disable_break ())
791 warning (_("shared library handler failed to disable breakpoint"));
794 solib_add ((char *) 0, 0, (struct target_ops
*) 0, auto_solib_add
);
798 sunos_clear_solib (void)
804 sunos_free_so (struct so_list
*so
)
806 xfree (so
->lm_info
->lm
);
811 sunos_relocate_section_addresses (struct so_list
*so
,
812 struct section_table
*sec
)
814 sec
->addr
+= LM_ADDR (so
);
815 sec
->endaddr
+= LM_ADDR (so
);
818 static struct target_so_ops sunos_so_ops
;
821 _initialize_sunos_solib (void)
823 sunos_so_ops
.relocate_section_addresses
= sunos_relocate_section_addresses
;
824 sunos_so_ops
.free_so
= sunos_free_so
;
825 sunos_so_ops
.clear_solib
= sunos_clear_solib
;
826 sunos_so_ops
.solib_create_inferior_hook
= sunos_solib_create_inferior_hook
;
827 sunos_so_ops
.special_symbol_handling
= sunos_special_symbol_handling
;
828 sunos_so_ops
.current_sos
= sunos_current_sos
;
829 sunos_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
830 sunos_so_ops
.in_dynsym_resolve_code
= sunos_in_dynsym_resolve_code
;
832 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
833 current_target_so_ops
= &sunos_so_ops
;