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"
25 #include <sys/param.h>
28 /* SunOS shared libs need the nlist structure. */
38 #include "gdbthread.h"
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), \
123 gdbarch_byte_order (target_gdbarch ()))
125 /* local data declarations */
127 static struct link_dynamic dynamic_copy;
128 static struct link_dynamic_2 ld_2_copy;
129 static struct ld_debug debug_copy;
130 static CORE_ADDR debug_addr;
131 static CORE_ADDR flag_addr;
134 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
136 #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER))
138 /* link map access functions */
141 lm_addr (struct so_list *so)
143 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
144 int lm_addr_offset = offsetof (struct link_map, lm_addr);
145 int lm_addr_size = fieldsize (struct link_map, lm_addr);
147 return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lm_addr_offset,
148 lm_addr_size, byte_order);
152 lm_next (struct so_list *so)
154 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
155 int lm_next_offset = offsetof (struct link_map, lm_next);
156 int lm_next_size = fieldsize (struct link_map, lm_next);
158 /* Assume that the address is unsigned. */
159 return extract_unsigned_integer (so->lm_info->lm + lm_next_offset,
160 lm_next_size, byte_order);
164 lm_name (struct so_list *so)
166 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
167 int lm_name_offset = offsetof (struct link_map, lm_name);
168 int lm_name_size = fieldsize (struct link_map, lm_name);
170 /* Assume that the address is unsigned. */
171 return extract_unsigned_integer (so->lm_info->lm + lm_name_offset,
172 lm_name_size, byte_order);
175 static CORE_ADDR debug_base; /* Base of dynamic linker structures. */
177 /* Local function prototypes */
179 static int match_main (char *);
181 /* Allocate the runtime common object file. */
184 allocate_rt_common_objfile (void)
186 struct objfile *objfile;
187 struct objfile *last_one;
189 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
190 memset (objfile, 0, sizeof (struct objfile));
191 objfile->psymbol_cache = psymbol_bcache_init ();
192 objfile->macro_cache = bcache_xmalloc (NULL, NULL);
193 objfile->filename_cache = bcache_xmalloc (NULL, NULL);
194 obstack_init (&objfile->objfile_obstack);
195 objfile->name = xstrdup ("rt_common");
197 /* Add this file onto the tail of the linked list of other such files. */
199 objfile->next = NULL;
200 if (object_files == NULL)
201 object_files = objfile;
204 for (last_one = object_files;
206 last_one = last_one->next);
207 last_one->next = objfile;
210 rt_common_objfile = objfile;
213 /* Read all dynamically loaded common symbol definitions from the inferior
214 and put them into the minimal symbol table for the runtime common
218 solib_add_common_symbols (CORE_ADDR rtc_symp)
220 struct rtc_symb inferior_rtc_symb;
221 struct nlist inferior_rtc_nlist;
225 /* Remove any runtime common symbols from previous runs. */
227 if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count)
229 obstack_free (&rt_common_objfile->objfile_obstack, 0);
230 obstack_init (&rt_common_objfile->objfile_obstack);
231 rt_common_objfile->minimal_symbol_count = 0;
232 rt_common_objfile->msymbols = NULL;
233 terminate_minimal_symbol_table (rt_common_objfile);
236 init_minimal_symbol_collection ();
237 make_cleanup_discard_minimal_symbols ();
241 read_memory (rtc_symp,
242 (char *) &inferior_rtc_symb,
243 sizeof (inferior_rtc_symb));
244 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp),
245 (char *) &inferior_rtc_nlist,
246 sizeof (inferior_rtc_nlist));
247 if (inferior_rtc_nlist.n_type == N_COMM)
249 /* FIXME: The length of the symbol name is not available, but in the
250 current implementation the common symbol is allocated immediately
251 behind the name of the symbol. */
252 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
254 name = xmalloc (len);
255 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name),
258 /* Allocate the runtime common objfile if necessary. */
259 if (rt_common_objfile == NULL)
260 allocate_rt_common_objfile ();
262 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
263 mst_bss, rt_common_objfile);
266 rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next);
269 /* Install any minimal symbols that have been collected as the current
270 minimal symbols for the runtime common objfile. */
272 install_minimal_symbols (rt_common_objfile);
276 /* Locate the base address of dynamic linker structs.
278 For both the SunOS and SVR4 shared library implementations, if the
279 inferior executable has been linked dynamically, there is a single
280 address somewhere in the inferior's data space which is the key to
281 locating all of the dynamic linker's runtime structures. This
282 address is the value of the debug base symbol. The job of this
283 function is to find and return that address, or to return 0 if there
284 is no such address (the executable is statically linked for example).
286 For SunOS, the job is almost trivial, since the dynamic linker and
287 all of it's structures are statically linked to the executable at
288 link time. Thus the symbol for the address we are looking for has
289 already been added to the minimal symbol table for the executable's
290 objfile at the time the symbol file's symbols were read, and all we
291 have to do is look it up there. Note that we explicitly do NOT want
292 to find the copies in the shared library.
294 The SVR4 version is a bit more complicated because the address
295 is contained somewhere in the dynamic info section. We have to go
296 to a lot more work to discover the address of the debug base symbol.
297 Because of this complexity, we cache the value we find and return that
298 value on subsequent invocations. Note there is no copy in the
299 executable symbol tables. */
304 struct minimal_symbol *msymbol;
305 CORE_ADDR address = 0;
308 /* For SunOS, we want to limit the search for the debug base symbol to the
309 executable being debugged, since there is a duplicate named symbol in the
310 shared library. We don't want the shared library versions. */
312 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
314 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
315 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
317 address = SYMBOL_VALUE_ADDRESS (msymbol);
324 /* Locate first member in dynamic linker's map.
326 Find the first element in the inferior's dynamic link map, and
327 return its address in the inferior. This function doesn't copy the
328 link map entry itself into our address space; current_sos actually
332 first_link_map_member (void)
336 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
337 if (dynamic_copy.ld_version >= 2)
339 /* It is a version that we can deal with, so read in the secondary
340 structure and find the address of the link map list from it. */
341 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2),
342 (char *) &ld_2_copy, sizeof (struct link_dynamic_2));
343 lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded);
349 open_symbol_file_object (void *from_ttyp)
355 /* Implement the "current_sos" target_so_ops method. */
357 static struct so_list *
358 sunos_current_sos (void)
361 struct so_list *head = 0;
362 struct so_list **link_ptr = &head;
366 /* Make sure we've looked up the inferior's dynamic linker's base
370 debug_base = locate_base ();
372 /* If we can't find the dynamic linker's base structure, this
373 must not be a dynamically linked executable. Hmm. */
378 /* Walk the inferior's link map list, and build our list of
379 `struct so_list' nodes. */
380 lm = first_link_map_member ();
384 = (struct so_list *) xmalloc (sizeof (struct so_list));
385 struct cleanup *old_chain = make_cleanup (xfree, new);
387 memset (new, 0, sizeof (*new));
389 new->lm_info = xmalloc (sizeof (struct lm_info));
390 make_cleanup (xfree, new->lm_info);
392 new->lm_info->lm = xmalloc (sizeof (struct link_map));
393 make_cleanup (xfree, new->lm_info->lm);
394 memset (new->lm_info->lm, 0, sizeof (struct link_map));
396 read_memory (lm, new->lm_info->lm, sizeof (struct link_map));
400 /* Extract this shared object's name. */
401 target_read_string (lm_name (new), &buffer,
402 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
404 warning (_("Can't read pathname for load map: %s."),
405 safe_strerror (errcode));
408 strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
409 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
411 strcpy (new->so_original_name, new->so_name);
414 /* If this entry has no name, or its name matches the name
415 for the main executable, don't include it in the list. */
416 if (! new->so_name[0]
417 || match_main (new->so_name))
423 link_ptr = &new->next;
426 discard_cleanups (old_chain);
433 /* On some systems, the only way to recognize the link map entry for
434 the main executable file is by looking at its name. Return
435 non-zero iff SONAME matches one of the known main executable names. */
438 match_main (char *soname)
442 for (mainp = main_name_list; *mainp != NULL; mainp++)
444 if (strcmp (soname, *mainp) == 0)
453 sunos_in_dynsym_resolve_code (CORE_ADDR pc)
458 /* Remove the "mapping changed" breakpoint.
460 Removes the breakpoint that gets hit when the dynamic linker
461 completes a mapping change. */
466 CORE_ADDR breakpoint_addr; /* Address where end bkpt is set. */
470 /* Read the debugger structure from the inferior to retrieve the
471 address of the breakpoint and the original contents of the
472 breakpoint address. Remove the breakpoint by writing the original
475 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
477 /* Set `in_debugger' to zero now. */
479 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
481 breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr);
482 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
483 sizeof (debug_copy.ldd_bp_inst));
485 /* For the SVR4 version, we always know the breakpoint address. For the
486 SunOS version we don't know it until the above code is executed.
487 Grumble if we are stopped anywhere besides the breakpoint address. */
489 if (stop_pc != breakpoint_addr)
491 warning (_("stopped at unknown breakpoint "
492 "while handling shared libraries"));
498 /* Arrange for dynamic linker to hit breakpoint.
500 Both the SunOS and the SVR4 dynamic linkers have, as part of their
501 debugger interface, support for arranging for the inferior to hit
502 a breakpoint after mapping in the shared libraries. This function
503 enables that breakpoint.
505 For SunOS, there is a special flag location (in_debugger) which we
506 set to 1. When the dynamic linker sees this flag set, it will set
507 a breakpoint at a location known only to itself, after saving the
508 original contents of that place and the breakpoint address itself,
509 in it's own internal structures. When we resume the inferior, it
510 will eventually take a SIGTRAP when it runs into the breakpoint.
511 We handle this (in a different place) by restoring the contents of
512 the breakpointed location (which is only known after it stops),
513 chasing around to locate the shared libraries that have been
514 loaded, then resuming.
516 For SVR4, the debugger interface structure contains a member (r_brk)
517 which is statically initialized at the time the shared library is
518 built, to the offset of a function (_r_debug_state) which is guaran-
519 teed to be called once before mapping in a library, and again when
520 the mapping is complete. At the time we are examining this member,
521 it contains only the unrelocated offset of the function, so we have
522 to do our own relocation. Later, when the dynamic linker actually
523 runs, it relocates r_brk to be the actual address of _r_debug_state().
525 The debugger interface structure also contains an enumeration which
526 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
527 depending upon whether or not the library is being mapped or
528 unmapped, and then set to RT_CONSISTENT after the library is
538 /* Get link_dynamic structure. */
540 j = target_read_memory (debug_base, (char *) &dynamic_copy,
541 sizeof (dynamic_copy));
548 /* Calc address of debugger interface structure. */
550 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
552 /* Calc address of `in_debugger' member of debugger interface structure. */
554 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
555 (char *) &debug_copy);
557 /* Write a value of 1 to this member. */
560 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
566 /* Implement the "special_symbol_handling" target_so_ops method.
568 For SunOS4, this consists of grunging around in the dynamic
569 linkers structures to find symbol definitions for "common" symbols
570 and adding them to the minimal symbol table for the runtime common
574 sunos_special_symbol_handling (void)
580 /* Get link_dynamic structure. */
582 j = target_read_memory (debug_base, (char *) &dynamic_copy,
583 sizeof (dynamic_copy));
590 /* Calc address of debugger interface structure. */
591 /* FIXME, this needs work for cross-debugging of core files
592 (byteorder, size, alignment, etc). */
594 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
597 /* Read the debugger structure from the inferior, just to make sure
598 we have a current copy. */
600 j = target_read_memory (debug_addr, (char *) &debug_copy,
601 sizeof (debug_copy));
603 return; /* unreadable */
605 /* Get common symbol definitions for the loaded object. */
607 if (debug_copy.ldd_cp)
609 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp));
613 /* Implement the "create_inferior_hook" target_solib_ops method.
615 For SunOS executables, this first instruction is typically the
616 one at "_start", or a similar text label, regardless of whether
617 the executable is statically or dynamically linked. The runtime
618 startup code takes care of dynamically linking in any shared
619 libraries, once gdb allows the inferior to continue.
621 We can arrange to cooperate with the dynamic linker to discover the
622 names of shared libraries that are dynamically linked, and the base
623 addresses to which they are linked.
625 This function is responsible for discovering those names and
626 addresses, and saving sufficient information about them to allow
627 their symbols to be read at a later time.
631 Between enable_break() and disable_break(), this code does not
632 properly handle hitting breakpoints which the user might have
633 set in the startup code or in the dynamic linker itself. Proper
634 handling will probably have to wait until the implementation is
635 changed to use the "breakpoint handler function" method.
637 Also, what if child has exit()ed? Must exit loop somehow. */
640 sunos_solib_create_inferior_hook (int from_tty)
642 struct thread_info *tp;
643 struct inferior *inf;
645 if ((debug_base = locate_base ()) == 0)
647 /* Can't find the symbol or the executable is statically linked. */
651 if (!enable_break ())
653 warning (_("shared library handler failed to enable breakpoint"));
657 /* SCO and SunOS need the loop below, other systems should be using the
658 special shared library breakpoints and the shared library breakpoint
661 Now run the target. It will eventually hit the breakpoint, at
662 which point all of the libraries will have been mapped in and we
663 can go groveling around in the dynamic linker structures to find
664 out what we need to know about them. */
666 inf = current_inferior ();
667 tp = inferior_thread ();
669 clear_proceed_status ();
671 inf->control.stop_soon = STOP_QUIETLY;
672 tp->suspend.stop_signal = GDB_SIGNAL_0;
675 target_resume (pid_to_ptid (-1), 0, tp->suspend.stop_signal);
676 wait_for_inferior ();
678 while (tp->suspend.stop_signal != GDB_SIGNAL_TRAP);
679 inf->control.stop_soon = NO_STOP_QUIETLY;
681 /* We are now either at the "mapping complete" breakpoint (or somewhere
682 else, a condition we aren't prepared to deal with anyway), so adjust
683 the PC as necessary after a breakpoint, disable the breakpoint, and
684 add any shared libraries that were mapped in.
686 Note that adjust_pc_after_break did not perform any PC adjustment,
687 as the breakpoint the inferior just hit was not inserted by GDB,
688 but by the dynamic loader itself, and is therefore not found on
689 the GDB software break point list. Thus we have to adjust the
692 if (gdbarch_decr_pc_after_break (target_gdbarch ()))
694 stop_pc -= gdbarch_decr_pc_after_break (target_gdbarch ());
695 regcache_write_pc (get_current_regcache (), stop_pc);
698 if (!disable_break ())
700 warning (_("shared library handler failed to disable breakpoint"));
703 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
707 sunos_clear_solib (void)
713 sunos_free_so (struct so_list *so)
715 xfree (so->lm_info->lm);
720 sunos_relocate_section_addresses (struct so_list *so,
721 struct target_section *sec)
723 sec->addr += lm_addr (so);
724 sec->endaddr += lm_addr (so);
727 static struct target_so_ops sunos_so_ops;
730 _initialize_sunos_solib (void)
732 sunos_so_ops.relocate_section_addresses = sunos_relocate_section_addresses;
733 sunos_so_ops.free_so = sunos_free_so;
734 sunos_so_ops.clear_solib = sunos_clear_solib;
735 sunos_so_ops.solib_create_inferior_hook = sunos_solib_create_inferior_hook;
736 sunos_so_ops.special_symbol_handling = sunos_special_symbol_handling;
737 sunos_so_ops.current_sos = sunos_current_sos;
738 sunos_so_ops.open_symbol_file_object = open_symbol_file_object;
739 sunos_so_ops.in_dynsym_resolve_code = sunos_in_dynsym_resolve_code;
740 sunos_so_ops.bfd_open = solib_bfd_open;
742 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
743 current_target_so_ops = &sunos_so_ops;