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