| 1 | /* Handle SunOS shared libraries for GDB, the GNU Debugger. |
| 2 | |
| 3 | Copyright (C) 1990-1996, 1998-2001, 2004, 2007-2012 Free Software |
| 4 | Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <signal.h> |
| 25 | #include "gdb_string.h" |
| 26 | #include <sys/param.h> |
| 27 | #include <fcntl.h> |
| 28 | |
| 29 | /* SunOS shared libs need the nlist structure. */ |
| 30 | #include <a.out.h> |
| 31 | #include <link.h> |
| 32 | |
| 33 | #include "symtab.h" |
| 34 | #include "bfd.h" |
| 35 | #include "symfile.h" |
| 36 | #include "objfiles.h" |
| 37 | #include "gdbcore.h" |
| 38 | #include "inferior.h" |
| 39 | #include "gdbthread.h" |
| 40 | #include "solist.h" |
| 41 | #include "bcache.h" |
| 42 | #include "regcache.h" |
| 43 | |
| 44 | /* The shared library implementation found on BSD a.out systems is |
| 45 | very similar to the SunOS implementation. However, the data |
| 46 | structures defined in <link.h> are named very differently. Make up |
| 47 | for those differences here. */ |
| 48 | |
| 49 | #ifdef HAVE_STRUCT_SO_MAP_WITH_SOM_MEMBERS |
| 50 | |
| 51 | /* FIXME: Temporary until the equivalent defines have been removed |
| 52 | from all nm-*bsd*.h files. */ |
| 53 | #ifndef link_dynamic |
| 54 | |
| 55 | /* Map `struct link_map' and its members. */ |
| 56 | #define link_map so_map |
| 57 | #define lm_addr som_addr |
| 58 | #define lm_name som_path |
| 59 | #define lm_next som_next |
| 60 | |
| 61 | /* Map `struct link_dynamic_2' and its members. */ |
| 62 | #define link_dynamic_2 section_dispatch_table |
| 63 | #define ld_loaded sdt_loaded |
| 64 | |
| 65 | /* Map `struct rtc_symb' and its members. */ |
| 66 | #define rtc_symb rt_symbol |
| 67 | #define rtc_sp rt_sp |
| 68 | #define rtc_next rt_next |
| 69 | |
| 70 | /* Map `struct ld_debug' and its members. */ |
| 71 | #define ld_debug so_debug |
| 72 | #define ldd_in_debugger dd_in_debugger |
| 73 | #define ldd_bp_addr dd_bpt_addr |
| 74 | #define ldd_bp_inst dd_bpt_shadow |
| 75 | #define ldd_cp dd_cc |
| 76 | |
| 77 | /* Map `struct link_dynamic' and its members. */ |
| 78 | #define link_dynamic _dynamic |
| 79 | #define ld_version d_version |
| 80 | #define ldd d_debug |
| 81 | #define ld_un d_un |
| 82 | #define ld_2 d_sdt |
| 83 | |
| 84 | #endif |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | /* Link map info to include in an allocated so_list entry. */ |
| 89 | |
| 90 | struct lm_info |
| 91 | { |
| 92 | /* Pointer to copy of link map from inferior. The type is char * |
| 93 | rather than void *, so that we may use byte offsets to find the |
| 94 | various fields without the need for a cast. */ |
| 95 | char *lm; |
| 96 | }; |
| 97 | |
| 98 | |
| 99 | /* Symbols which are used to locate the base of the link map structures. */ |
| 100 | |
| 101 | static char *debug_base_symbols[] = |
| 102 | { |
| 103 | "_DYNAMIC", |
| 104 | "_DYNAMIC__MGC", |
| 105 | NULL |
| 106 | }; |
| 107 | |
| 108 | static char *main_name_list[] = |
| 109 | { |
| 110 | "main_$main", |
| 111 | NULL |
| 112 | }; |
| 113 | |
| 114 | /* Macro to extract an address from a solib structure. When GDB is |
| 115 | configured for some 32-bit targets (e.g. Solaris 2.7 sparc), BFD is |
| 116 | configured to handle 64-bit targets, so CORE_ADDR is 64 bits. We |
| 117 | have to extract only the significant bits of addresses to get the |
| 118 | right address when accessing the core file BFD. |
| 119 | |
| 120 | Assume that the address is unsigned. */ |
| 121 | |
| 122 | #define SOLIB_EXTRACT_ADDRESS(MEMBER) \ |
| 123 | extract_unsigned_integer (&(MEMBER), sizeof (MEMBER), \ |
| 124 | gdbarch_byte_order (target_gdbarch)) |
| 125 | |
| 126 | /* local data declarations */ |
| 127 | |
| 128 | static struct link_dynamic dynamic_copy; |
| 129 | static struct link_dynamic_2 ld_2_copy; |
| 130 | static struct ld_debug debug_copy; |
| 131 | static CORE_ADDR debug_addr; |
| 132 | static CORE_ADDR flag_addr; |
| 133 | |
| 134 | #ifndef offsetof |
| 135 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) |
| 136 | #endif |
| 137 | #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER)) |
| 138 | |
| 139 | /* link map access functions */ |
| 140 | |
| 141 | static CORE_ADDR |
| 142 | lm_addr (struct so_list *so) |
| 143 | { |
| 144 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
| 145 | int lm_addr_offset = offsetof (struct link_map, lm_addr); |
| 146 | int lm_addr_size = fieldsize (struct link_map, lm_addr); |
| 147 | |
| 148 | return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lm_addr_offset, |
| 149 | lm_addr_size, byte_order); |
| 150 | } |
| 151 | |
| 152 | static CORE_ADDR |
| 153 | lm_next (struct so_list *so) |
| 154 | { |
| 155 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
| 156 | int lm_next_offset = offsetof (struct link_map, lm_next); |
| 157 | int lm_next_size = fieldsize (struct link_map, lm_next); |
| 158 | |
| 159 | /* Assume that the address is unsigned. */ |
| 160 | return extract_unsigned_integer (so->lm_info->lm + lm_next_offset, |
| 161 | lm_next_size, byte_order); |
| 162 | } |
| 163 | |
| 164 | static CORE_ADDR |
| 165 | lm_name (struct so_list *so) |
| 166 | { |
| 167 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
| 168 | int lm_name_offset = offsetof (struct link_map, lm_name); |
| 169 | int lm_name_size = fieldsize (struct link_map, lm_name); |
| 170 | |
| 171 | /* Assume that the address is unsigned. */ |
| 172 | return extract_unsigned_integer (so->lm_info->lm + lm_name_offset, |
| 173 | lm_name_size, byte_order); |
| 174 | } |
| 175 | |
| 176 | static CORE_ADDR debug_base; /* Base of dynamic linker structures. */ |
| 177 | |
| 178 | /* Local function prototypes */ |
| 179 | |
| 180 | static int match_main (char *); |
| 181 | |
| 182 | /* Allocate the runtime common object file. */ |
| 183 | |
| 184 | static void |
| 185 | allocate_rt_common_objfile (void) |
| 186 | { |
| 187 | struct objfile *objfile; |
| 188 | struct objfile *last_one; |
| 189 | |
| 190 | objfile = (struct objfile *) xmalloc (sizeof (struct objfile)); |
| 191 | memset (objfile, 0, sizeof (struct objfile)); |
| 192 | objfile->psymbol_cache = psymbol_bcache_init (); |
| 193 | objfile->macro_cache = bcache_xmalloc (NULL, NULL); |
| 194 | objfile->filename_cache = bcache_xmalloc (NULL, NULL); |
| 195 | obstack_init (&objfile->objfile_obstack); |
| 196 | objfile->name = xstrdup ("rt_common"); |
| 197 | |
| 198 | /* Add this file onto the tail of the linked list of other such files. */ |
| 199 | |
| 200 | objfile->next = NULL; |
| 201 | if (object_files == NULL) |
| 202 | object_files = objfile; |
| 203 | else |
| 204 | { |
| 205 | for (last_one = object_files; |
| 206 | last_one->next; |
| 207 | last_one = last_one->next); |
| 208 | last_one->next = objfile; |
| 209 | } |
| 210 | |
| 211 | rt_common_objfile = objfile; |
| 212 | } |
| 213 | |
| 214 | /* Read all dynamically loaded common symbol definitions from the inferior |
| 215 | and put them into the minimal symbol table for the runtime common |
| 216 | objfile. */ |
| 217 | |
| 218 | static void |
| 219 | solib_add_common_symbols (CORE_ADDR rtc_symp) |
| 220 | { |
| 221 | struct rtc_symb inferior_rtc_symb; |
| 222 | struct nlist inferior_rtc_nlist; |
| 223 | int len; |
| 224 | char *name; |
| 225 | |
| 226 | /* Remove any runtime common symbols from previous runs. */ |
| 227 | |
| 228 | if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count) |
| 229 | { |
| 230 | obstack_free (&rt_common_objfile->objfile_obstack, 0); |
| 231 | obstack_init (&rt_common_objfile->objfile_obstack); |
| 232 | rt_common_objfile->minimal_symbol_count = 0; |
| 233 | rt_common_objfile->msymbols = NULL; |
| 234 | terminate_minimal_symbol_table (rt_common_objfile); |
| 235 | } |
| 236 | |
| 237 | init_minimal_symbol_collection (); |
| 238 | make_cleanup_discard_minimal_symbols (); |
| 239 | |
| 240 | while (rtc_symp) |
| 241 | { |
| 242 | read_memory (rtc_symp, |
| 243 | (char *) &inferior_rtc_symb, |
| 244 | sizeof (inferior_rtc_symb)); |
| 245 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp), |
| 246 | (char *) &inferior_rtc_nlist, |
| 247 | sizeof (inferior_rtc_nlist)); |
| 248 | if (inferior_rtc_nlist.n_type == N_COMM) |
| 249 | { |
| 250 | /* FIXME: The length of the symbol name is not available, but in the |
| 251 | current implementation the common symbol is allocated immediately |
| 252 | behind the name of the symbol. */ |
| 253 | len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx; |
| 254 | |
| 255 | name = xmalloc (len); |
| 256 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name), |
| 257 | name, len); |
| 258 | |
| 259 | /* Allocate the runtime common objfile if necessary. */ |
| 260 | if (rt_common_objfile == NULL) |
| 261 | allocate_rt_common_objfile (); |
| 262 | |
| 263 | prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value, |
| 264 | mst_bss, rt_common_objfile); |
| 265 | xfree (name); |
| 266 | } |
| 267 | rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next); |
| 268 | } |
| 269 | |
| 270 | /* Install any minimal symbols that have been collected as the current |
| 271 | minimal symbols for the runtime common objfile. */ |
| 272 | |
| 273 | install_minimal_symbols (rt_common_objfile); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /* Locate the base address of dynamic linker structs. |
| 278 | |
| 279 | For both the SunOS and SVR4 shared library implementations, if the |
| 280 | inferior executable has been linked dynamically, there is a single |
| 281 | address somewhere in the inferior's data space which is the key to |
| 282 | locating all of the dynamic linker's runtime structures. This |
| 283 | address is the value of the debug base symbol. The job of this |
| 284 | function is to find and return that address, or to return 0 if there |
| 285 | is no such address (the executable is statically linked for example). |
| 286 | |
| 287 | For SunOS, the job is almost trivial, since the dynamic linker and |
| 288 | all of it's structures are statically linked to the executable at |
| 289 | link time. Thus the symbol for the address we are looking for has |
| 290 | already been added to the minimal symbol table for the executable's |
| 291 | objfile at the time the symbol file's symbols were read, and all we |
| 292 | have to do is look it up there. Note that we explicitly do NOT want |
| 293 | to find the copies in the shared library. |
| 294 | |
| 295 | The SVR4 version is a bit more complicated because the address |
| 296 | is contained somewhere in the dynamic info section. We have to go |
| 297 | to a lot more work to discover the address of the debug base symbol. |
| 298 | Because of this complexity, we cache the value we find and return that |
| 299 | value on subsequent invocations. Note there is no copy in the |
| 300 | executable symbol tables. */ |
| 301 | |
| 302 | static CORE_ADDR |
| 303 | locate_base (void) |
| 304 | { |
| 305 | struct minimal_symbol *msymbol; |
| 306 | CORE_ADDR address = 0; |
| 307 | char **symbolp; |
| 308 | |
| 309 | /* For SunOS, we want to limit the search for the debug base symbol to the |
| 310 | executable being debugged, since there is a duplicate named symbol in the |
| 311 | shared library. We don't want the shared library versions. */ |
| 312 | |
| 313 | for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++) |
| 314 | { |
| 315 | msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile); |
| 316 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) |
| 317 | { |
| 318 | address = SYMBOL_VALUE_ADDRESS (msymbol); |
| 319 | return (address); |
| 320 | } |
| 321 | } |
| 322 | return (0); |
| 323 | } |
| 324 | |
| 325 | /* Locate first member in dynamic linker's map. |
| 326 | |
| 327 | Find the first element in the inferior's dynamic link map, and |
| 328 | return its address in the inferior. This function doesn't copy the |
| 329 | link map entry itself into our address space; current_sos actually |
| 330 | does the reading. */ |
| 331 | |
| 332 | static CORE_ADDR |
| 333 | first_link_map_member (void) |
| 334 | { |
| 335 | CORE_ADDR lm = 0; |
| 336 | |
| 337 | read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy)); |
| 338 | if (dynamic_copy.ld_version >= 2) |
| 339 | { |
| 340 | /* It is a version that we can deal with, so read in the secondary |
| 341 | structure and find the address of the link map list from it. */ |
| 342 | read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2), |
| 343 | (char *) &ld_2_copy, sizeof (struct link_dynamic_2)); |
| 344 | lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded); |
| 345 | } |
| 346 | return (lm); |
| 347 | } |
| 348 | |
| 349 | static int |
| 350 | open_symbol_file_object (void *from_ttyp) |
| 351 | { |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /* Implement the "current_sos" target_so_ops method. */ |
| 357 | |
| 358 | static struct so_list * |
| 359 | sunos_current_sos (void) |
| 360 | { |
| 361 | CORE_ADDR lm; |
| 362 | struct so_list *head = 0; |
| 363 | struct so_list **link_ptr = &head; |
| 364 | int errcode; |
| 365 | char *buffer; |
| 366 | |
| 367 | /* Make sure we've looked up the inferior's dynamic linker's base |
| 368 | structure. */ |
| 369 | if (! debug_base) |
| 370 | { |
| 371 | debug_base = locate_base (); |
| 372 | |
| 373 | /* If we can't find the dynamic linker's base structure, this |
| 374 | must not be a dynamically linked executable. Hmm. */ |
| 375 | if (! debug_base) |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* Walk the inferior's link map list, and build our list of |
| 380 | `struct so_list' nodes. */ |
| 381 | lm = first_link_map_member (); |
| 382 | while (lm) |
| 383 | { |
| 384 | struct so_list *new |
| 385 | = (struct so_list *) xmalloc (sizeof (struct so_list)); |
| 386 | struct cleanup *old_chain = make_cleanup (xfree, new); |
| 387 | |
| 388 | memset (new, 0, sizeof (*new)); |
| 389 | |
| 390 | new->lm_info = xmalloc (sizeof (struct lm_info)); |
| 391 | make_cleanup (xfree, new->lm_info); |
| 392 | |
| 393 | new->lm_info->lm = xmalloc (sizeof (struct link_map)); |
| 394 | make_cleanup (xfree, new->lm_info->lm); |
| 395 | memset (new->lm_info->lm, 0, sizeof (struct link_map)); |
| 396 | |
| 397 | read_memory (lm, new->lm_info->lm, sizeof (struct link_map)); |
| 398 | |
| 399 | lm = lm_next (new); |
| 400 | |
| 401 | /* Extract this shared object's name. */ |
| 402 | target_read_string (lm_name (new), &buffer, |
| 403 | SO_NAME_MAX_PATH_SIZE - 1, &errcode); |
| 404 | if (errcode != 0) |
| 405 | warning (_("Can't read pathname for load map: %s."), |
| 406 | safe_strerror (errcode)); |
| 407 | else |
| 408 | { |
| 409 | strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1); |
| 410 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; |
| 411 | xfree (buffer); |
| 412 | strcpy (new->so_original_name, new->so_name); |
| 413 | } |
| 414 | |
| 415 | /* If this entry has no name, or its name matches the name |
| 416 | for the main executable, don't include it in the list. */ |
| 417 | if (! new->so_name[0] |
| 418 | || match_main (new->so_name)) |
| 419 | free_so (new); |
| 420 | else |
| 421 | { |
| 422 | new->next = 0; |
| 423 | *link_ptr = new; |
| 424 | link_ptr = &new->next; |
| 425 | } |
| 426 | |
| 427 | discard_cleanups (old_chain); |
| 428 | } |
| 429 | |
| 430 | return head; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* On some systems, the only way to recognize the link map entry for |
| 435 | the main executable file is by looking at its name. Return |
| 436 | non-zero iff SONAME matches one of the known main executable names. */ |
| 437 | |
| 438 | static int |
| 439 | match_main (char *soname) |
| 440 | { |
| 441 | char **mainp; |
| 442 | |
| 443 | for (mainp = main_name_list; *mainp != NULL; mainp++) |
| 444 | { |
| 445 | if (strcmp (soname, *mainp) == 0) |
| 446 | return (1); |
| 447 | } |
| 448 | |
| 449 | return (0); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | static int |
| 454 | sunos_in_dynsym_resolve_code (CORE_ADDR pc) |
| 455 | { |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | /* Remove the "mapping changed" breakpoint. |
| 460 | |
| 461 | Removes the breakpoint that gets hit when the dynamic linker |
| 462 | completes a mapping change. */ |
| 463 | |
| 464 | static int |
| 465 | disable_break (void) |
| 466 | { |
| 467 | CORE_ADDR breakpoint_addr; /* Address where end bkpt is set. */ |
| 468 | |
| 469 | int in_debugger = 0; |
| 470 | |
| 471 | /* Read the debugger structure from the inferior to retrieve the |
| 472 | address of the breakpoint and the original contents of the |
| 473 | breakpoint address. Remove the breakpoint by writing the original |
| 474 | contents back. */ |
| 475 | |
| 476 | read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy)); |
| 477 | |
| 478 | /* Set `in_debugger' to zero now. */ |
| 479 | |
| 480 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); |
| 481 | |
| 482 | breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr); |
| 483 | write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst, |
| 484 | sizeof (debug_copy.ldd_bp_inst)); |
| 485 | |
| 486 | /* For the SVR4 version, we always know the breakpoint address. For the |
| 487 | SunOS version we don't know it until the above code is executed. |
| 488 | Grumble if we are stopped anywhere besides the breakpoint address. */ |
| 489 | |
| 490 | if (stop_pc != breakpoint_addr) |
| 491 | { |
| 492 | warning (_("stopped at unknown breakpoint " |
| 493 | "while handling shared libraries")); |
| 494 | } |
| 495 | |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | /* Arrange for dynamic linker to hit breakpoint. |
| 500 | |
| 501 | Both the SunOS and the SVR4 dynamic linkers have, as part of their |
| 502 | debugger interface, support for arranging for the inferior to hit |
| 503 | a breakpoint after mapping in the shared libraries. This function |
| 504 | enables that breakpoint. |
| 505 | |
| 506 | For SunOS, there is a special flag location (in_debugger) which we |
| 507 | set to 1. When the dynamic linker sees this flag set, it will set |
| 508 | a breakpoint at a location known only to itself, after saving the |
| 509 | original contents of that place and the breakpoint address itself, |
| 510 | in it's own internal structures. When we resume the inferior, it |
| 511 | will eventually take a SIGTRAP when it runs into the breakpoint. |
| 512 | We handle this (in a different place) by restoring the contents of |
| 513 | the breakpointed location (which is only known after it stops), |
| 514 | chasing around to locate the shared libraries that have been |
| 515 | loaded, then resuming. |
| 516 | |
| 517 | For SVR4, the debugger interface structure contains a member (r_brk) |
| 518 | which is statically initialized at the time the shared library is |
| 519 | built, to the offset of a function (_r_debug_state) which is guaran- |
| 520 | teed to be called once before mapping in a library, and again when |
| 521 | the mapping is complete. At the time we are examining this member, |
| 522 | it contains only the unrelocated offset of the function, so we have |
| 523 | to do our own relocation. Later, when the dynamic linker actually |
| 524 | runs, it relocates r_brk to be the actual address of _r_debug_state(). |
| 525 | |
| 526 | The debugger interface structure also contains an enumeration which |
| 527 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, |
| 528 | depending upon whether or not the library is being mapped or |
| 529 | unmapped, and then set to RT_CONSISTENT after the library is |
| 530 | mapped/unmapped. */ |
| 531 | |
| 532 | static int |
| 533 | enable_break (void) |
| 534 | { |
| 535 | int success = 0; |
| 536 | int j; |
| 537 | int in_debugger; |
| 538 | |
| 539 | /* Get link_dynamic structure. */ |
| 540 | |
| 541 | j = target_read_memory (debug_base, (char *) &dynamic_copy, |
| 542 | sizeof (dynamic_copy)); |
| 543 | if (j) |
| 544 | { |
| 545 | /* unreadable */ |
| 546 | return (0); |
| 547 | } |
| 548 | |
| 549 | /* Calc address of debugger interface structure. */ |
| 550 | |
| 551 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); |
| 552 | |
| 553 | /* Calc address of `in_debugger' member of debugger interface structure. */ |
| 554 | |
| 555 | flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger - |
| 556 | (char *) &debug_copy); |
| 557 | |
| 558 | /* Write a value of 1 to this member. */ |
| 559 | |
| 560 | in_debugger = 1; |
| 561 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); |
| 562 | success = 1; |
| 563 | |
| 564 | return (success); |
| 565 | } |
| 566 | |
| 567 | /* Implement the "special_symbol_handling" target_so_ops method. |
| 568 | |
| 569 | For SunOS4, this consists of grunging around in the dynamic |
| 570 | linkers structures to find symbol definitions for "common" symbols |
| 571 | and adding them to the minimal symbol table for the runtime common |
| 572 | objfile. */ |
| 573 | |
| 574 | static void |
| 575 | sunos_special_symbol_handling (void) |
| 576 | { |
| 577 | int j; |
| 578 | |
| 579 | if (debug_addr == 0) |
| 580 | { |
| 581 | /* Get link_dynamic structure. */ |
| 582 | |
| 583 | j = target_read_memory (debug_base, (char *) &dynamic_copy, |
| 584 | sizeof (dynamic_copy)); |
| 585 | if (j) |
| 586 | { |
| 587 | /* unreadable */ |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | /* Calc address of debugger interface structure. */ |
| 592 | /* FIXME, this needs work for cross-debugging of core files |
| 593 | (byteorder, size, alignment, etc). */ |
| 594 | |
| 595 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); |
| 596 | } |
| 597 | |
| 598 | /* Read the debugger structure from the inferior, just to make sure |
| 599 | we have a current copy. */ |
| 600 | |
| 601 | j = target_read_memory (debug_addr, (char *) &debug_copy, |
| 602 | sizeof (debug_copy)); |
| 603 | if (j) |
| 604 | return; /* unreadable */ |
| 605 | |
| 606 | /* Get common symbol definitions for the loaded object. */ |
| 607 | |
| 608 | if (debug_copy.ldd_cp) |
| 609 | { |
| 610 | solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp)); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /* Implement the "create_inferior_hook" target_solib_ops method. |
| 615 | |
| 616 | For SunOS executables, this first instruction is typically the |
| 617 | one at "_start", or a similar text label, regardless of whether |
| 618 | the executable is statically or dynamically linked. The runtime |
| 619 | startup code takes care of dynamically linking in any shared |
| 620 | libraries, once gdb allows the inferior to continue. |
| 621 | |
| 622 | We can arrange to cooperate with the dynamic linker to discover the |
| 623 | names of shared libraries that are dynamically linked, and the base |
| 624 | addresses to which they are linked. |
| 625 | |
| 626 | This function is responsible for discovering those names and |
| 627 | addresses, and saving sufficient information about them to allow |
| 628 | their symbols to be read at a later time. |
| 629 | |
| 630 | FIXME |
| 631 | |
| 632 | Between enable_break() and disable_break(), this code does not |
| 633 | properly handle hitting breakpoints which the user might have |
| 634 | set in the startup code or in the dynamic linker itself. Proper |
| 635 | handling will probably have to wait until the implementation is |
| 636 | changed to use the "breakpoint handler function" method. |
| 637 | |
| 638 | Also, what if child has exit()ed? Must exit loop somehow. */ |
| 639 | |
| 640 | static void |
| 641 | sunos_solib_create_inferior_hook (int from_tty) |
| 642 | { |
| 643 | struct thread_info *tp; |
| 644 | struct inferior *inf; |
| 645 | |
| 646 | if ((debug_base = locate_base ()) == 0) |
| 647 | { |
| 648 | /* Can't find the symbol or the executable is statically linked. */ |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | if (!enable_break ()) |
| 653 | { |
| 654 | warning (_("shared library handler failed to enable breakpoint")); |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | /* SCO and SunOS need the loop below, other systems should be using the |
| 659 | special shared library breakpoints and the shared library breakpoint |
| 660 | service routine. |
| 661 | |
| 662 | Now run the target. It will eventually hit the breakpoint, at |
| 663 | which point all of the libraries will have been mapped in and we |
| 664 | can go groveling around in the dynamic linker structures to find |
| 665 | out what we need to know about them. */ |
| 666 | |
| 667 | inf = current_inferior (); |
| 668 | tp = inferior_thread (); |
| 669 | |
| 670 | clear_proceed_status (); |
| 671 | |
| 672 | inf->control.stop_soon = STOP_QUIETLY; |
| 673 | tp->suspend.stop_signal = TARGET_SIGNAL_0; |
| 674 | do |
| 675 | { |
| 676 | target_resume (pid_to_ptid (-1), 0, tp->suspend.stop_signal); |
| 677 | wait_for_inferior (); |
| 678 | } |
| 679 | while (tp->suspend.stop_signal != TARGET_SIGNAL_TRAP); |
| 680 | inf->control.stop_soon = NO_STOP_QUIETLY; |
| 681 | |
| 682 | /* We are now either at the "mapping complete" breakpoint (or somewhere |
| 683 | else, a condition we aren't prepared to deal with anyway), so adjust |
| 684 | the PC as necessary after a breakpoint, disable the breakpoint, and |
| 685 | add any shared libraries that were mapped in. |
| 686 | |
| 687 | Note that adjust_pc_after_break did not perform any PC adjustment, |
| 688 | as the breakpoint the inferior just hit was not inserted by GDB, |
| 689 | but by the dynamic loader itself, and is therefore not found on |
| 690 | the GDB software break point list. Thus we have to adjust the |
| 691 | PC here. */ |
| 692 | |
| 693 | if (gdbarch_decr_pc_after_break (target_gdbarch)) |
| 694 | { |
| 695 | stop_pc -= gdbarch_decr_pc_after_break (target_gdbarch); |
| 696 | regcache_write_pc (get_current_regcache (), stop_pc); |
| 697 | } |
| 698 | |
| 699 | if (!disable_break ()) |
| 700 | { |
| 701 | warning (_("shared library handler failed to disable breakpoint")); |
| 702 | } |
| 703 | |
| 704 | solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add); |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | sunos_clear_solib (void) |
| 709 | { |
| 710 | debug_base = 0; |
| 711 | } |
| 712 | |
| 713 | static void |
| 714 | sunos_free_so (struct so_list *so) |
| 715 | { |
| 716 | xfree (so->lm_info->lm); |
| 717 | xfree (so->lm_info); |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | sunos_relocate_section_addresses (struct so_list *so, |
| 722 | struct target_section *sec) |
| 723 | { |
| 724 | sec->addr += lm_addr (so); |
| 725 | sec->endaddr += lm_addr (so); |
| 726 | } |
| 727 | |
| 728 | static struct target_so_ops sunos_so_ops; |
| 729 | |
| 730 | void |
| 731 | _initialize_sunos_solib (void) |
| 732 | { |
| 733 | sunos_so_ops.relocate_section_addresses = sunos_relocate_section_addresses; |
| 734 | sunos_so_ops.free_so = sunos_free_so; |
| 735 | sunos_so_ops.clear_solib = sunos_clear_solib; |
| 736 | sunos_so_ops.solib_create_inferior_hook = sunos_solib_create_inferior_hook; |
| 737 | sunos_so_ops.special_symbol_handling = sunos_special_symbol_handling; |
| 738 | sunos_so_ops.current_sos = sunos_current_sos; |
| 739 | sunos_so_ops.open_symbol_file_object = open_symbol_file_object; |
| 740 | sunos_so_ops.in_dynsym_resolve_code = sunos_in_dynsym_resolve_code; |
| 741 | sunos_so_ops.bfd_open = solib_bfd_open; |
| 742 | |
| 743 | /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */ |
| 744 | current_target_so_ops = &sunos_so_ops; |
| 745 | } |