1 /* Handle SunOS shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990-2013 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include <sys/types.h>
24 #include "gdb_string.h"
27 /* SunOS shared libs need the nlist structure. */
37 #include "gdbthread.h"
42 /* The shared library implementation found on BSD a.out systems is
43 very similar to the SunOS implementation. However, the data
44 structures defined in <link.h> are named very differently. Make up
45 for those differences here. */
47 #ifdef HAVE_STRUCT_SO_MAP_WITH_SOM_MEMBERS
49 /* FIXME: Temporary until the equivalent defines have been removed
50 from all nm-*bsd*.h files. */
53 /* Map `struct link_map' and its members. */
54 #define link_map so_map
55 #define lm_addr som_addr
56 #define lm_name som_path
57 #define lm_next som_next
59 /* Map `struct link_dynamic_2' and its members. */
60 #define link_dynamic_2 section_dispatch_table
61 #define ld_loaded sdt_loaded
63 /* Map `struct rtc_symb' and its members. */
64 #define rtc_symb rt_symbol
66 #define rtc_next rt_next
68 /* Map `struct ld_debug' and its members. */
69 #define ld_debug so_debug
70 #define ldd_in_debugger dd_in_debugger
71 #define ldd_bp_addr dd_bpt_addr
72 #define ldd_bp_inst dd_bpt_shadow
75 /* Map `struct link_dynamic' and its members. */
76 #define link_dynamic _dynamic
77 #define ld_version d_version
86 /* Link map info to include in an allocated so_list entry. */
90 /* Pointer to copy of link map from inferior. The type is char *
91 rather than void *, so that we may use byte offsets to find the
92 various fields without the need for a cast. */
97 /* Symbols which are used to locate the base of the link map structures. */
99 static char *debug_base_symbols
[] =
106 static char *main_name_list
[] =
112 /* Macro to extract an address from a solib structure. When GDB is
113 configured for some 32-bit targets (e.g. Solaris 2.7 sparc), BFD is
114 configured to handle 64-bit targets, so CORE_ADDR is 64 bits. We
115 have to extract only the significant bits of addresses to get the
116 right address when accessing the core file BFD.
118 Assume that the address is unsigned. */
120 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
121 extract_unsigned_integer (&(MEMBER), sizeof (MEMBER), \
122 gdbarch_byte_order (target_gdbarch ()))
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 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
143 int lm_addr_offset
= offsetof (struct link_map
, lm_addr
);
144 int lm_addr_size
= fieldsize (struct link_map
, lm_addr
);
146 return (CORE_ADDR
) extract_signed_integer (so
->lm_info
->lm
+ lm_addr_offset
,
147 lm_addr_size
, byte_order
);
151 lm_next (struct so_list
*so
)
153 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
154 int lm_next_offset
= offsetof (struct link_map
, lm_next
);
155 int lm_next_size
= fieldsize (struct link_map
, lm_next
);
157 /* Assume that the address is unsigned. */
158 return extract_unsigned_integer (so
->lm_info
->lm
+ lm_next_offset
,
159 lm_next_size
, byte_order
);
163 lm_name (struct so_list
*so
)
165 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
166 int lm_name_offset
= offsetof (struct link_map
, lm_name
);
167 int lm_name_size
= fieldsize (struct link_map
, lm_name
);
169 /* Assume that the address is unsigned. */
170 return extract_unsigned_integer (so
->lm_info
->lm
+ lm_name_offset
,
171 lm_name_size
, byte_order
);
174 static CORE_ADDR debug_base
; /* Base of dynamic linker structures. */
176 /* Local function prototypes */
178 static int match_main (char *);
180 /* Allocate the runtime common object file. */
183 allocate_rt_common_objfile (void)
185 struct objfile
*objfile
;
186 struct objfile
*last_one
;
188 objfile
= (struct objfile
*) xmalloc (sizeof (struct objfile
));
189 memset (objfile
, 0, sizeof (struct objfile
));
190 objfile
->psymbol_cache
= psymbol_bcache_init ();
191 objfile
->macro_cache
= bcache_xmalloc (NULL
, NULL
);
192 objfile
->filename_cache
= bcache_xmalloc (NULL
, NULL
);
193 obstack_init (&objfile
->objfile_obstack
);
194 objfile
->name
= xstrdup ("rt_common");
196 /* Add this file onto the tail of the linked list of other such files. */
198 objfile
->next
= NULL
;
199 if (object_files
== NULL
)
200 object_files
= objfile
;
203 for (last_one
= object_files
;
205 last_one
= last_one
->next
);
206 last_one
->next
= objfile
;
209 rt_common_objfile
= objfile
;
212 /* Read all dynamically loaded common symbol definitions from the inferior
213 and put them into the minimal symbol table for the runtime common
217 solib_add_common_symbols (CORE_ADDR rtc_symp
)
219 struct rtc_symb inferior_rtc_symb
;
220 struct nlist inferior_rtc_nlist
;
224 /* Remove any runtime common symbols from previous runs. */
226 if (rt_common_objfile
!= NULL
&& rt_common_objfile
->minimal_symbol_count
)
228 obstack_free (&rt_common_objfile
->objfile_obstack
, 0);
229 obstack_init (&rt_common_objfile
->objfile_obstack
);
230 rt_common_objfile
->minimal_symbol_count
= 0;
231 rt_common_objfile
->msymbols
= NULL
;
232 terminate_minimal_symbol_table (rt_common_objfile
);
235 init_minimal_symbol_collection ();
236 make_cleanup_discard_minimal_symbols ();
240 read_memory (rtc_symp
,
241 (char *) &inferior_rtc_symb
,
242 sizeof (inferior_rtc_symb
));
243 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb
.rtc_sp
),
244 (char *) &inferior_rtc_nlist
,
245 sizeof (inferior_rtc_nlist
));
246 if (inferior_rtc_nlist
.n_type
== N_COMM
)
248 /* FIXME: The length of the symbol name is not available, but in the
249 current implementation the common symbol is allocated immediately
250 behind the name of the symbol. */
251 len
= inferior_rtc_nlist
.n_value
- inferior_rtc_nlist
.n_un
.n_strx
;
253 name
= xmalloc (len
);
254 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist
.n_un
.n_name
),
257 /* Allocate the runtime common objfile if necessary. */
258 if (rt_common_objfile
== NULL
)
259 allocate_rt_common_objfile ();
261 prim_record_minimal_symbol (name
, inferior_rtc_nlist
.n_value
,
262 mst_bss
, rt_common_objfile
);
265 rtc_symp
= SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb
.rtc_next
);
268 /* Install any minimal symbols that have been collected as the current
269 minimal symbols for the runtime common objfile. */
271 install_minimal_symbols (rt_common_objfile
);
275 /* Locate the base address of dynamic linker structs.
277 For both the SunOS and SVR4 shared library implementations, if the
278 inferior executable has been linked dynamically, there is a single
279 address somewhere in the inferior's data space which is the key to
280 locating all of the dynamic linker's runtime structures. This
281 address is the value of the debug base symbol. The job of this
282 function is to find and return that address, or to return 0 if there
283 is no such address (the executable is statically linked for example).
285 For SunOS, the job is almost trivial, since the dynamic linker and
286 all of it's structures are statically linked to the executable at
287 link time. Thus the symbol for the address we are looking for has
288 already been added to the minimal symbol table for the executable's
289 objfile at the time the symbol file's symbols were read, and all we
290 have to do is look it up there. Note that we explicitly do NOT want
291 to find the copies in the shared library.
293 The SVR4 version is a bit more complicated because the address
294 is contained somewhere in the dynamic info section. We have to go
295 to a lot more work to discover the address of the debug base symbol.
296 Because of this complexity, we cache the value we find and return that
297 value on subsequent invocations. Note there is no copy in the
298 executable symbol tables. */
303 struct minimal_symbol
*msymbol
;
304 CORE_ADDR address
= 0;
307 /* For SunOS, we want to limit the search for the debug base symbol to the
308 executable being debugged, since there is a duplicate named symbol in the
309 shared library. We don't want the shared library versions. */
311 for (symbolp
= debug_base_symbols
; *symbolp
!= NULL
; symbolp
++)
313 msymbol
= lookup_minimal_symbol (*symbolp
, NULL
, symfile_objfile
);
314 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
316 address
= SYMBOL_VALUE_ADDRESS (msymbol
);
323 /* Locate first member in dynamic linker's map.
325 Find the first element in the inferior's dynamic link map, and
326 return its address in the inferior. This function doesn't copy the
327 link map entry itself into our address space; current_sos actually
331 first_link_map_member (void)
335 read_memory (debug_base
, (char *) &dynamic_copy
, sizeof (dynamic_copy
));
336 if (dynamic_copy
.ld_version
>= 2)
338 /* It is a version that we can deal with, so read in the secondary
339 structure and find the address of the link map list from it. */
340 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ld_un
.ld_2
),
341 (char *) &ld_2_copy
, sizeof (struct link_dynamic_2
));
342 lm
= SOLIB_EXTRACT_ADDRESS (ld_2_copy
.ld_loaded
);
348 open_symbol_file_object (void *from_ttyp
)
354 /* Implement the "current_sos" target_so_ops method. */
356 static struct so_list
*
357 sunos_current_sos (void)
360 struct so_list
*head
= 0;
361 struct so_list
**link_ptr
= &head
;
365 /* Make sure we've looked up the inferior's dynamic linker's base
369 debug_base
= locate_base ();
371 /* If we can't find the dynamic linker's base structure, this
372 must not be a dynamically linked executable. Hmm. */
377 /* Walk the inferior's link map list, and build our list of
378 `struct so_list' nodes. */
379 lm
= first_link_map_member ();
383 = (struct so_list
*) xmalloc (sizeof (struct so_list
));
384 struct cleanup
*old_chain
= make_cleanup (xfree
, new);
386 memset (new, 0, sizeof (*new));
388 new->lm_info
= xmalloc (sizeof (struct lm_info
));
389 make_cleanup (xfree
, new->lm_info
);
391 new->lm_info
->lm
= xmalloc (sizeof (struct link_map
));
392 make_cleanup (xfree
, new->lm_info
->lm
);
393 memset (new->lm_info
->lm
, 0, sizeof (struct link_map
));
395 read_memory (lm
, new->lm_info
->lm
, sizeof (struct link_map
));
399 /* Extract this shared object's name. */
400 target_read_string (lm_name (new), &buffer
,
401 SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
403 warning (_("Can't read pathname for load map: %s."),
404 safe_strerror (errcode
));
407 strncpy (new->so_name
, buffer
, SO_NAME_MAX_PATH_SIZE
- 1);
408 new->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
410 strcpy (new->so_original_name
, new->so_name
);
413 /* If this entry has no name, or its name matches the name
414 for the main executable, don't include it in the list. */
415 if (! new->so_name
[0]
416 || match_main (new->so_name
))
422 link_ptr
= &new->next
;
425 discard_cleanups (old_chain
);
432 /* On some systems, the only way to recognize the link map entry for
433 the main executable file is by looking at its name. Return
434 non-zero iff SONAME matches one of the known main executable names. */
437 match_main (char *soname
)
441 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
443 if (strcmp (soname
, *mainp
) == 0)
452 sunos_in_dynsym_resolve_code (CORE_ADDR pc
)
457 /* Remove the "mapping changed" breakpoint.
459 Removes the breakpoint that gets hit when the dynamic linker
460 completes a mapping change. */
465 CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set. */
469 /* Read the debugger structure from the inferior to retrieve the
470 address of the breakpoint and the original contents of the
471 breakpoint address. Remove the breakpoint by writing the original
474 read_memory (debug_addr
, (char *) &debug_copy
, sizeof (debug_copy
));
476 /* Set `in_debugger' to zero now. */
478 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
480 breakpoint_addr
= SOLIB_EXTRACT_ADDRESS (debug_copy
.ldd_bp_addr
);
481 write_memory (breakpoint_addr
, (char *) &debug_copy
.ldd_bp_inst
,
482 sizeof (debug_copy
.ldd_bp_inst
));
484 /* For the SVR4 version, we always know the breakpoint address. For the
485 SunOS version we don't know it until the above code is executed.
486 Grumble if we are stopped anywhere besides the breakpoint address. */
488 if (stop_pc
!= breakpoint_addr
)
490 warning (_("stopped at unknown breakpoint "
491 "while handling shared libraries"));
497 /* Arrange for dynamic linker to hit breakpoint.
499 Both the SunOS and the SVR4 dynamic linkers have, as part of their
500 debugger interface, support for arranging for the inferior to hit
501 a breakpoint after mapping in the shared libraries. This function
502 enables that breakpoint.
504 For SunOS, there is a special flag location (in_debugger) which we
505 set to 1. When the dynamic linker sees this flag set, it will set
506 a breakpoint at a location known only to itself, after saving the
507 original contents of that place and the breakpoint address itself,
508 in it's own internal structures. When we resume the inferior, it
509 will eventually take a SIGTRAP when it runs into the breakpoint.
510 We handle this (in a different place) by restoring the contents of
511 the breakpointed location (which is only known after it stops),
512 chasing around to locate the shared libraries that have been
513 loaded, then resuming.
515 For SVR4, the debugger interface structure contains a member (r_brk)
516 which is statically initialized at the time the shared library is
517 built, to the offset of a function (_r_debug_state) which is guaran-
518 teed to be called once before mapping in a library, and again when
519 the mapping is complete. At the time we are examining this member,
520 it contains only the unrelocated offset of the function, so we have
521 to do our own relocation. Later, when the dynamic linker actually
522 runs, it relocates r_brk to be the actual address of _r_debug_state().
524 The debugger interface structure also contains an enumeration which
525 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
526 depending upon whether or not the library is being mapped or
527 unmapped, and then set to RT_CONSISTENT after the library is
537 /* Get link_dynamic structure. */
539 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
540 sizeof (dynamic_copy
));
547 /* Calc address of debugger interface structure. */
549 debug_addr
= SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ldd
);
551 /* Calc address of `in_debugger' member of debugger interface structure. */
553 flag_addr
= debug_addr
+ (CORE_ADDR
) ((char *) &debug_copy
.ldd_in_debugger
-
554 (char *) &debug_copy
);
556 /* Write a value of 1 to this member. */
559 write_memory (flag_addr
, (char *) &in_debugger
, sizeof (in_debugger
));
565 /* Implement the "special_symbol_handling" target_so_ops method.
567 For SunOS4, this consists of grunging around in the dynamic
568 linkers structures to find symbol definitions for "common" symbols
569 and adding them to the minimal symbol table for the runtime common
573 sunos_special_symbol_handling (void)
579 /* Get link_dynamic structure. */
581 j
= target_read_memory (debug_base
, (char *) &dynamic_copy
,
582 sizeof (dynamic_copy
));
589 /* Calc address of debugger interface structure. */
590 /* FIXME, this needs work for cross-debugging of core files
591 (byteorder, size, alignment, etc). */
593 debug_addr
= SOLIB_EXTRACT_ADDRESS (dynamic_copy
.ldd
);
596 /* Read the debugger structure from the inferior, just to make sure
597 we have a current copy. */
599 j
= target_read_memory (debug_addr
, (char *) &debug_copy
,
600 sizeof (debug_copy
));
602 return; /* unreadable */
604 /* Get common symbol definitions for the loaded object. */
606 if (debug_copy
.ldd_cp
)
608 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy
.ldd_cp
));
612 /* Implement the "create_inferior_hook" target_solib_ops method.
614 For SunOS executables, this first instruction is typically the
615 one at "_start", or a similar text label, regardless of whether
616 the executable is statically or dynamically linked. The runtime
617 startup code takes care of dynamically linking in any shared
618 libraries, once gdb allows the inferior to continue.
620 We can arrange to cooperate with the dynamic linker to discover the
621 names of shared libraries that are dynamically linked, and the base
622 addresses to which they are linked.
624 This function is responsible for discovering those names and
625 addresses, and saving sufficient information about them to allow
626 their symbols to be read at a later time.
630 Between enable_break() and disable_break(), this code does not
631 properly handle hitting breakpoints which the user might have
632 set in the startup code or in the dynamic linker itself. Proper
633 handling will probably have to wait until the implementation is
634 changed to use the "breakpoint handler function" method.
636 Also, what if child has exit()ed? Must exit loop somehow. */
639 sunos_solib_create_inferior_hook (int from_tty
)
641 struct thread_info
*tp
;
642 struct inferior
*inf
;
644 if ((debug_base
= locate_base ()) == 0)
646 /* Can't find the symbol or the executable is statically linked. */
650 if (!enable_break ())
652 warning (_("shared library handler failed to enable breakpoint"));
656 /* SCO and SunOS need the loop below, other systems should be using the
657 special shared library breakpoints and the shared library breakpoint
660 Now run the target. It will eventually hit the breakpoint, at
661 which point all of the libraries will have been mapped in and we
662 can go groveling around in the dynamic linker structures to find
663 out what we need to know about them. */
665 inf
= current_inferior ();
666 tp
= inferior_thread ();
668 clear_proceed_status ();
670 inf
->control
.stop_soon
= STOP_QUIETLY
;
671 tp
->suspend
.stop_signal
= GDB_SIGNAL_0
;
674 target_resume (pid_to_ptid (-1), 0, tp
->suspend
.stop_signal
);
675 wait_for_inferior ();
677 while (tp
->suspend
.stop_signal
!= GDB_SIGNAL_TRAP
);
678 inf
->control
.stop_soon
= NO_STOP_QUIETLY
;
680 /* We are now either at the "mapping complete" breakpoint (or somewhere
681 else, a condition we aren't prepared to deal with anyway), so adjust
682 the PC as necessary after a breakpoint, disable the breakpoint, and
683 add any shared libraries that were mapped in.
685 Note that adjust_pc_after_break did not perform any PC adjustment,
686 as the breakpoint the inferior just hit was not inserted by GDB,
687 but by the dynamic loader itself, and is therefore not found on
688 the GDB software break point list. Thus we have to adjust the
691 if (gdbarch_decr_pc_after_break (target_gdbarch ()))
693 stop_pc
-= gdbarch_decr_pc_after_break (target_gdbarch ());
694 regcache_write_pc (get_current_regcache (), stop_pc
);
697 if (!disable_break ())
699 warning (_("shared library handler failed to disable breakpoint"));
702 solib_add ((char *) 0, 0, (struct target_ops
*) 0, auto_solib_add
);
706 sunos_clear_solib (void)
712 sunos_free_so (struct so_list
*so
)
714 xfree (so
->lm_info
->lm
);
719 sunos_relocate_section_addresses (struct so_list
*so
,
720 struct target_section
*sec
)
722 sec
->addr
+= lm_addr (so
);
723 sec
->endaddr
+= lm_addr (so
);
726 static struct target_so_ops sunos_so_ops
;
729 _initialize_sunos_solib (void)
731 sunos_so_ops
.relocate_section_addresses
= sunos_relocate_section_addresses
;
732 sunos_so_ops
.free_so
= sunos_free_so
;
733 sunos_so_ops
.clear_solib
= sunos_clear_solib
;
734 sunos_so_ops
.solib_create_inferior_hook
= sunos_solib_create_inferior_hook
;
735 sunos_so_ops
.special_symbol_handling
= sunos_special_symbol_handling
;
736 sunos_so_ops
.current_sos
= sunos_current_sos
;
737 sunos_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
738 sunos_so_ops
.in_dynsym_resolve_code
= sunos_in_dynsym_resolve_code
;
739 sunos_so_ops
.bfd_open
= solib_bfd_open
;
741 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
742 current_target_so_ops
= &sunos_so_ops
;