| 1 | /* Handle HP SOM shared libraries for GDB, the GNU Debugger. |
| 2 | Copyright 1993, 1996, 1999 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. |
| 20 | |
| 21 | Written by the Center for Software Science at the Univerity of Utah |
| 22 | and by Cygnus Support. */ |
| 23 | |
| 24 | |
| 25 | #include "defs.h" |
| 26 | |
| 27 | #include "frame.h" |
| 28 | #include "bfd.h" |
| 29 | #include "som.h" |
| 30 | #include "libhppa.h" |
| 31 | #include "gdbcore.h" |
| 32 | #include "symtab.h" |
| 33 | #include "breakpoint.h" |
| 34 | #include "symfile.h" |
| 35 | #include "objfiles.h" |
| 36 | #include "inferior.h" |
| 37 | #include "gdb-stabs.h" |
| 38 | #include "gdb_stat.h" |
| 39 | #include "gdbcmd.h" |
| 40 | #include "assert.h" |
| 41 | #include "language.h" |
| 42 | |
| 43 | #include <fcntl.h> |
| 44 | |
| 45 | #ifndef O_BINARY |
| 46 | #define O_BINARY 0 |
| 47 | #endif |
| 48 | |
| 49 | /* Uncomment this to turn on some debugging output. |
| 50 | */ |
| 51 | |
| 52 | /* #define SOLIB_DEBUG |
| 53 | */ |
| 54 | |
| 55 | /* Defined in exec.c; used to prevent dangling pointer bug. |
| 56 | */ |
| 57 | extern struct target_ops exec_ops; |
| 58 | |
| 59 | /* This lives in hppa-tdep.c. */ |
| 60 | extern struct unwind_table_entry *find_unwind_entry (CORE_ADDR pc); |
| 61 | |
| 62 | /* These ought to be defined in some public interface, but aren't. They |
| 63 | define the meaning of the various bits in the distinguished __dld_flags |
| 64 | variable that is declared in every debuggable a.out on HP-UX, and that |
| 65 | is shared between the debugger and the dynamic linker. |
| 66 | */ |
| 67 | #define DLD_FLAGS_MAPPRIVATE 0x1 |
| 68 | #define DLD_FLAGS_HOOKVALID 0x2 |
| 69 | #define DLD_FLAGS_LISTVALID 0x4 |
| 70 | #define DLD_FLAGS_BOR_ENABLE 0x8 |
| 71 | |
| 72 | /* TODO: |
| 73 | |
| 74 | * Most of this code should work for hp300 shared libraries. Does |
| 75 | anyone care enough to weed out any SOM-isms. |
| 76 | |
| 77 | * Support for hpux8 dynamic linker. */ |
| 78 | |
| 79 | /* The basic structure which describes a dynamically loaded object. This |
| 80 | data structure is private to the dynamic linker and isn't found in |
| 81 | any HPUX include file. */ |
| 82 | |
| 83 | struct som_solib_mapped_entry |
| 84 | { |
| 85 | /* The name of the library. */ |
| 86 | char *name; |
| 87 | |
| 88 | /* Version of this structure (it is expected to change again in hpux10). */ |
| 89 | unsigned char struct_version; |
| 90 | |
| 91 | /* Binding mode for this library. */ |
| 92 | unsigned char bind_mode; |
| 93 | |
| 94 | /* Version of this library. */ |
| 95 | short library_version; |
| 96 | |
| 97 | /* Start of text address, |
| 98 | * link-time text location (length of text area), |
| 99 | * end of text address. */ |
| 100 | CORE_ADDR text_addr; |
| 101 | CORE_ADDR text_link_addr; |
| 102 | CORE_ADDR text_end; |
| 103 | |
| 104 | /* Start of data, start of bss and end of data. */ |
| 105 | CORE_ADDR data_start; |
| 106 | CORE_ADDR bss_start; |
| 107 | CORE_ADDR data_end; |
| 108 | |
| 109 | /* Value of linkage pointer (%r19). */ |
| 110 | CORE_ADDR got_value; |
| 111 | |
| 112 | /* Next entry. */ |
| 113 | struct som_solib_mapped_entry *next; |
| 114 | |
| 115 | /* There are other fields, but I don't have information as to what is |
| 116 | contained in them. */ |
| 117 | |
| 118 | /* For versions from HPUX-10.30 and up */ |
| 119 | |
| 120 | /* Address in target of offset from thread-local register of |
| 121 | * start of this thread's data. I.e., the first thread-local |
| 122 | * variable in this shared library starts at *(tsd_start_addr) |
| 123 | * from that area pointed to by cr27 (mpsfu_hi). |
| 124 | * |
| 125 | * We do the indirection as soon as we read it, so from then |
| 126 | * on it's the offset itself. |
| 127 | */ |
| 128 | CORE_ADDR tsd_start_addr; |
| 129 | |
| 130 | /* Following this are longwords holding: |
| 131 | |
| 132 | * ?, ?, ?, ptr to -1, ptr to-1, ptr to lib name (leaf name), |
| 133 | * ptr to __data_start, ptr to __data_end |
| 134 | */ |
| 135 | |
| 136 | |
| 137 | }; |
| 138 | |
| 139 | /* A structure to keep track of all the known shared objects. */ |
| 140 | struct so_list |
| 141 | { |
| 142 | struct som_solib_mapped_entry som_solib; |
| 143 | struct objfile *objfile; |
| 144 | bfd *abfd; |
| 145 | struct section_table *sections; |
| 146 | struct section_table *sections_end; |
| 147 | /* elz: added this field to store the address in target space (in the |
| 148 | library) of the library descriptor (handle) which we read into |
| 149 | som_solib_mapped_entry structure */ |
| 150 | CORE_ADDR solib_addr; |
| 151 | struct so_list *next; |
| 152 | |
| 153 | }; |
| 154 | |
| 155 | static struct so_list *so_list_head; |
| 156 | |
| 157 | |
| 158 | /* This is the cumulative size in bytes of the symbol tables of all |
| 159 | shared objects on the so_list_head list. (When we say size, here |
| 160 | we mean of the information before it is brought into memory and |
| 161 | potentially expanded by GDB.) When adding a new shlib, this value |
| 162 | is compared against the threshold size, held by auto_solib_add |
| 163 | (in megabytes). If adding symbols for the new shlib would cause |
| 164 | the total size to exceed the threshold, then the new shlib's symbols |
| 165 | are not loaded. |
| 166 | */ |
| 167 | static LONGEST som_solib_total_st_size; |
| 168 | |
| 169 | /* When the threshold is reached for any shlib, we refuse to add |
| 170 | symbols for subsequent shlibs, even if those shlibs' symbols would |
| 171 | be small enough to fit under the threshold. (Although this may |
| 172 | result in one, early large shlib preventing the loading of later, |
| 173 | smalller shlibs' symbols, it allows us to issue one informational |
| 174 | message. The alternative, to issue a message for each shlib whose |
| 175 | symbols aren't loaded, could be a big annoyance where the threshold |
| 176 | is exceeded due to a very large number of shlibs.) |
| 177 | */ |
| 178 | static int som_solib_st_size_threshold_exceeded; |
| 179 | |
| 180 | /* These addresses should be filled in by som_solib_create_inferior_hook. |
| 181 | They are also used elsewhere in this module. |
| 182 | */ |
| 183 | typedef struct |
| 184 | { |
| 185 | CORE_ADDR address; |
| 186 | struct unwind_table_entry *unwind; |
| 187 | } |
| 188 | addr_and_unwind_t; |
| 189 | |
| 190 | /* When adding fields, be sure to clear them in _initialize_som_solib. */ |
| 191 | static struct |
| 192 | { |
| 193 | boolean is_valid; |
| 194 | addr_and_unwind_t hook; |
| 195 | addr_and_unwind_t hook_stub; |
| 196 | addr_and_unwind_t load; |
| 197 | addr_and_unwind_t load_stub; |
| 198 | addr_and_unwind_t unload; |
| 199 | addr_and_unwind_t unload2; |
| 200 | addr_and_unwind_t unload_stub; |
| 201 | } |
| 202 | dld_cache; |
| 203 | |
| 204 | |
| 205 | |
| 206 | static void som_sharedlibrary_info_command (char *, int); |
| 207 | |
| 208 | static void som_solib_sharedlibrary_command (char *, int); |
| 209 | |
| 210 | static LONGEST |
| 211 | som_solib_sizeof_symbol_table (char *filename) |
| 212 | { |
| 213 | bfd *abfd; |
| 214 | int desc; |
| 215 | char *absolute_name; |
| 216 | LONGEST st_size = (LONGEST) 0; |
| 217 | asection *sect; |
| 218 | |
| 219 | /* We believe that filename was handed to us by the dynamic linker, and |
| 220 | is therefore always an absolute path. |
| 221 | */ |
| 222 | desc = openp (getenv ("PATH"), 1, filename, O_RDONLY | O_BINARY, 0, &absolute_name); |
| 223 | if (desc < 0) |
| 224 | { |
| 225 | perror_with_name (filename); |
| 226 | } |
| 227 | filename = absolute_name; |
| 228 | |
| 229 | abfd = bfd_fdopenr (filename, gnutarget, desc); |
| 230 | if (!abfd) |
| 231 | { |
| 232 | close (desc); |
| 233 | make_cleanup (free, filename); |
| 234 | error ("\"%s\": can't open to read symbols: %s.", filename, |
| 235 | bfd_errmsg (bfd_get_error ())); |
| 236 | } |
| 237 | |
| 238 | if (!bfd_check_format (abfd, bfd_object)) /* Reads in section info */ |
| 239 | { |
| 240 | bfd_close (abfd); /* This also closes desc */ |
| 241 | make_cleanup (free, filename); |
| 242 | error ("\"%s\": can't read symbols: %s.", filename, |
| 243 | bfd_errmsg (bfd_get_error ())); |
| 244 | } |
| 245 | |
| 246 | /* Sum the sizes of the various sections that compose debug info. */ |
| 247 | |
| 248 | /* This contains non-DOC information. */ |
| 249 | sect = bfd_get_section_by_name (abfd, "$DEBUG$"); |
| 250 | if (sect) |
| 251 | st_size += (LONGEST) bfd_section_size (abfd, sect); |
| 252 | |
| 253 | /* This contains DOC information. */ |
| 254 | sect = bfd_get_section_by_name (abfd, "$PINFO$"); |
| 255 | if (sect) |
| 256 | st_size += (LONGEST) bfd_section_size (abfd, sect); |
| 257 | |
| 258 | bfd_close (abfd); /* This also closes desc */ |
| 259 | free (filename); |
| 260 | |
| 261 | /* Unfortunately, just summing the sizes of various debug info |
| 262 | sections isn't a very accurate measurement of how much heap |
| 263 | space the debugger will need to hold them. It also doesn't |
| 264 | account for space needed by linker (aka "minimal") symbols. |
| 265 | |
| 266 | Anecdotal evidence suggests that just summing the sizes of |
| 267 | debug-info-related sections understates the heap space needed |
| 268 | to represent it internally by about an order of magnitude. |
| 269 | |
| 270 | Since it's not exactly brain surgery we're doing here, rather |
| 271 | than attempt to more accurately measure the size of a shlib's |
| 272 | symbol table in GDB's heap, we'll just apply a 10x fudge- |
| 273 | factor to the debug info sections' size-sum. No, this doesn't |
| 274 | account for minimal symbols in non-debuggable shlibs. But it |
| 275 | all roughly washes out in the end. |
| 276 | */ |
| 277 | return st_size * (LONGEST) 10; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | static void |
| 282 | som_solib_add_solib_objfile (struct so_list *so, char *name, int from_tty, |
| 283 | CORE_ADDR text_addr) |
| 284 | { |
| 285 | obj_private_data_t *obj_private; |
| 286 | struct section_addr_info section_addrs; |
| 287 | |
| 288 | memset (§ion_addrs, 0, sizeof (section_addrs)); |
| 289 | section_addrs.other[0].name = ".text"; |
| 290 | section_addrs.other[0].addr = text_addr; |
| 291 | so->objfile = symbol_file_add (name, from_tty, §ion_addrs, 0, OBJF_SHARED); |
| 292 | so->abfd = so->objfile->obfd; |
| 293 | |
| 294 | /* Mark this as a shared library and save private data. |
| 295 | */ |
| 296 | so->objfile->flags |= OBJF_SHARED; |
| 297 | |
| 298 | if (so->objfile->obj_private == NULL) |
| 299 | { |
| 300 | obj_private = (obj_private_data_t *) |
| 301 | obstack_alloc (&so->objfile->psymbol_obstack, |
| 302 | sizeof (obj_private_data_t)); |
| 303 | obj_private->unwind_info = NULL; |
| 304 | obj_private->so_info = NULL; |
| 305 | so->objfile->obj_private = (PTR) obj_private; |
| 306 | } |
| 307 | |
| 308 | obj_private = (obj_private_data_t *) so->objfile->obj_private; |
| 309 | obj_private->so_info = so; |
| 310 | |
| 311 | if (!bfd_check_format (so->abfd, bfd_object)) |
| 312 | { |
| 313 | error ("\"%s\": not in executable format: %s.", |
| 314 | name, bfd_errmsg (bfd_get_error ())); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | |
| 319 | static void |
| 320 | som_solib_load_symbols (struct so_list *so, char *name, int from_tty, |
| 321 | CORE_ADDR text_addr, struct target_ops *target) |
| 322 | { |
| 323 | struct section_table *p; |
| 324 | int status; |
| 325 | char buf[4]; |
| 326 | CORE_ADDR presumed_data_start; |
| 327 | |
| 328 | #ifdef SOLIB_DEBUG |
| 329 | printf ("--Adding symbols for shared library \"%s\"\n", name); |
| 330 | #endif |
| 331 | |
| 332 | som_solib_add_solib_objfile (so, name, from_tty, text_addr); |
| 333 | |
| 334 | /* Now we need to build a section table for this library since |
| 335 | we might be debugging a core file from a dynamically linked |
| 336 | executable in which the libraries were not privately mapped. */ |
| 337 | if (build_section_table (so->abfd, |
| 338 | &so->sections, |
| 339 | &so->sections_end)) |
| 340 | { |
| 341 | error ("Unable to build section table for shared library\n."); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | /* Relocate all the sections based on where they got loaded. */ |
| 346 | for (p = so->sections; p < so->sections_end; p++) |
| 347 | { |
| 348 | if (p->the_bfd_section->flags & SEC_CODE) |
| 349 | { |
| 350 | p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile)); |
| 351 | p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile)); |
| 352 | } |
| 353 | else if (p->the_bfd_section->flags & SEC_DATA) |
| 354 | { |
| 355 | p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile)); |
| 356 | p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile)); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* Now see if we need to map in the text and data for this shared |
| 361 | library (for example debugging a core file which does not use |
| 362 | private shared libraries.). |
| 363 | |
| 364 | Carefully peek at the first text address in the library. If the |
| 365 | read succeeds, then the libraries were privately mapped and were |
| 366 | included in the core dump file. |
| 367 | |
| 368 | If the peek failed, then the libraries were not privately mapped |
| 369 | and are not in the core file, we'll have to read them in ourselves. */ |
| 370 | status = target_read_memory (text_addr, buf, 4); |
| 371 | if (status != 0) |
| 372 | { |
| 373 | int old, new; |
| 374 | |
| 375 | new = so->sections_end - so->sections; |
| 376 | |
| 377 | old = target_resize_to_sections (target, new); |
| 378 | |
| 379 | /* Copy over the old data before it gets clobbered. */ |
| 380 | memcpy ((char *) (target->to_sections + old), |
| 381 | so->sections, |
| 382 | ((sizeof (struct section_table)) * new)); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /* Add symbols from shared libraries into the symtab list, unless the |
| 388 | size threshold (specified by auto_solib_add, in megabytes) would |
| 389 | be exceeded. */ |
| 390 | |
| 391 | void |
| 392 | som_solib_add (char *arg_string, int from_tty, struct target_ops *target) |
| 393 | { |
| 394 | struct minimal_symbol *msymbol; |
| 395 | struct so_list *so_list_tail; |
| 396 | CORE_ADDR addr; |
| 397 | asection *shlib_info; |
| 398 | int status; |
| 399 | unsigned int dld_flags; |
| 400 | char buf[4], *re_err; |
| 401 | int threshold_warning_given = 0; |
| 402 | |
| 403 | /* First validate our arguments. */ |
| 404 | if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL) |
| 405 | { |
| 406 | error ("Invalid regexp: %s", re_err); |
| 407 | } |
| 408 | |
| 409 | /* If we're debugging a core file, or have attached to a running |
| 410 | process, then som_solib_create_inferior_hook will not have been |
| 411 | called. |
| 412 | |
| 413 | We need to first determine if we're dealing with a dynamically |
| 414 | linked executable. If not, then return without an error or warning. |
| 415 | |
| 416 | We also need to examine __dld_flags to determine if the shared library |
| 417 | list is valid and to determine if the libraries have been privately |
| 418 | mapped. */ |
| 419 | if (symfile_objfile == NULL) |
| 420 | return; |
| 421 | |
| 422 | /* First see if the objfile was dynamically linked. */ |
| 423 | shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$"); |
| 424 | if (!shlib_info) |
| 425 | return; |
| 426 | |
| 427 | /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */ |
| 428 | if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0) |
| 429 | return; |
| 430 | |
| 431 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
| 432 | if (msymbol == NULL) |
| 433 | { |
| 434 | error ("Unable to find __dld_flags symbol in object file.\n"); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | addr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 439 | /* Read the current contents. */ |
| 440 | status = target_read_memory (addr, buf, 4); |
| 441 | if (status != 0) |
| 442 | { |
| 443 | error ("Unable to read __dld_flags\n"); |
| 444 | return; |
| 445 | } |
| 446 | dld_flags = extract_unsigned_integer (buf, 4); |
| 447 | |
| 448 | /* __dld_list may not be valid. If not, then we punt, warning the user if |
| 449 | we were called as a result of the add-symfile command. |
| 450 | */ |
| 451 | if ((dld_flags & DLD_FLAGS_LISTVALID) == 0) |
| 452 | { |
| 453 | if (from_tty) |
| 454 | error ("__dld_list is not valid according to __dld_flags.\n"); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | /* If the libraries were not mapped private, warn the user. */ |
| 459 | if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0) |
| 460 | warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n"); |
| 461 | |
| 462 | msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL); |
| 463 | if (!msymbol) |
| 464 | { |
| 465 | /* Older crt0.o files (hpux8) don't have __dld_list as a symbol, |
| 466 | but the data is still available if you know where to look. */ |
| 467 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
| 468 | if (!msymbol) |
| 469 | { |
| 470 | error ("Unable to find dynamic library list.\n"); |
| 471 | return; |
| 472 | } |
| 473 | addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8; |
| 474 | } |
| 475 | else |
| 476 | addr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 477 | |
| 478 | status = target_read_memory (addr, buf, 4); |
| 479 | if (status != 0) |
| 480 | { |
| 481 | error ("Unable to find dynamic library list.\n"); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | addr = extract_unsigned_integer (buf, 4); |
| 486 | |
| 487 | /* If addr is zero, then we're using an old dynamic loader which |
| 488 | doesn't maintain __dld_list. We'll have to use a completely |
| 489 | different approach to get shared library information. */ |
| 490 | if (addr == 0) |
| 491 | goto old_dld; |
| 492 | |
| 493 | /* Using the information in __dld_list is the preferred method |
| 494 | to get at shared library information. It doesn't depend on |
| 495 | any functions in /opt/langtools/lib/end.o and has a chance of working |
| 496 | with hpux10 when it is released. */ |
| 497 | status = target_read_memory (addr, buf, 4); |
| 498 | if (status != 0) |
| 499 | { |
| 500 | error ("Unable to find dynamic library list.\n"); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | /* addr now holds the address of the first entry in the dynamic |
| 505 | library list. */ |
| 506 | addr = extract_unsigned_integer (buf, 4); |
| 507 | |
| 508 | /* Now that we have a pointer to the dynamic library list, walk |
| 509 | through it and add the symbols for each library. */ |
| 510 | |
| 511 | so_list_tail = so_list_head; |
| 512 | /* Find the end of the list of shared objects. */ |
| 513 | while (so_list_tail && so_list_tail->next) |
| 514 | so_list_tail = so_list_tail->next; |
| 515 | |
| 516 | #ifdef SOLIB_DEBUG |
| 517 | printf ("--About to read shared library list data\n"); |
| 518 | #endif |
| 519 | |
| 520 | /* "addr" will always point to the base of the |
| 521 | * current data entry describing the current |
| 522 | * shared library. |
| 523 | */ |
| 524 | while (1) |
| 525 | { |
| 526 | CORE_ADDR name_addr, text_addr; |
| 527 | unsigned int name_len; |
| 528 | char *name; |
| 529 | struct so_list *new_so; |
| 530 | struct so_list *so_list = so_list_head; |
| 531 | struct stat statbuf; |
| 532 | LONGEST st_size; |
| 533 | int is_main_program; |
| 534 | |
| 535 | if (addr == 0) |
| 536 | break; |
| 537 | |
| 538 | /* Get a pointer to the name of this library. */ |
| 539 | status = target_read_memory (addr, buf, 4); |
| 540 | if (status != 0) |
| 541 | goto err; |
| 542 | |
| 543 | name_addr = extract_unsigned_integer (buf, 4); |
| 544 | name_len = 0; |
| 545 | while (1) |
| 546 | { |
| 547 | target_read_memory (name_addr + name_len, buf, 1); |
| 548 | if (status != 0) |
| 549 | goto err; |
| 550 | |
| 551 | name_len++; |
| 552 | if (*buf == '\0') |
| 553 | break; |
| 554 | } |
| 555 | name = alloca (name_len); |
| 556 | status = target_read_memory (name_addr, name, name_len); |
| 557 | if (status != 0) |
| 558 | goto err; |
| 559 | |
| 560 | /* See if we've already loaded something with this name. */ |
| 561 | while (so_list) |
| 562 | { |
| 563 | if (!strcmp (so_list->som_solib.name, name)) |
| 564 | break; |
| 565 | so_list = so_list->next; |
| 566 | } |
| 567 | |
| 568 | /* See if the file exists. If not, give a warning, but don't |
| 569 | die. */ |
| 570 | status = stat (name, &statbuf); |
| 571 | if (status == -1) |
| 572 | { |
| 573 | warning ("Can't find file %s referenced in dld_list.", name); |
| 574 | |
| 575 | status = target_read_memory (addr + 36, buf, 4); |
| 576 | if (status != 0) |
| 577 | goto err; |
| 578 | |
| 579 | addr = (CORE_ADDR) extract_unsigned_integer (buf, 4); |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | /* If we've already loaded this one or it's the main program, skip it. */ |
| 584 | is_main_program = (strcmp (name, symfile_objfile->name) == 0); |
| 585 | if (so_list || is_main_program) |
| 586 | { |
| 587 | /* This is the "next" pointer in the strcuture. |
| 588 | */ |
| 589 | status = target_read_memory (addr + 36, buf, 4); |
| 590 | if (status != 0) |
| 591 | goto err; |
| 592 | |
| 593 | addr = (CORE_ADDR) extract_unsigned_integer (buf, 4); |
| 594 | |
| 595 | /* Record the main program's symbol table size. */ |
| 596 | if (is_main_program && !so_list) |
| 597 | { |
| 598 | st_size = som_solib_sizeof_symbol_table (name); |
| 599 | som_solib_total_st_size += st_size; |
| 600 | } |
| 601 | |
| 602 | /* Was this a shlib that we noted but didn't load the symbols for? |
| 603 | If so, were we invoked this time from the command-line, via |
| 604 | a 'sharedlibrary' or 'add-symbol-file' command? If yes to |
| 605 | both, we'd better load the symbols this time. |
| 606 | */ |
| 607 | if (from_tty && so_list && !is_main_program && (so_list->objfile == NULL)) |
| 608 | som_solib_load_symbols (so_list, |
| 609 | name, |
| 610 | from_tty, |
| 611 | so_list->som_solib.text_addr, |
| 612 | target); |
| 613 | |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | name = obsavestring (name, name_len - 1, |
| 618 | &symfile_objfile->symbol_obstack); |
| 619 | |
| 620 | status = target_read_memory (addr + 8, buf, 4); |
| 621 | if (status != 0) |
| 622 | goto err; |
| 623 | |
| 624 | text_addr = extract_unsigned_integer (buf, 4); |
| 625 | |
| 626 | new_so = (struct so_list *) xmalloc (sizeof (struct so_list)); |
| 627 | memset ((char *) new_so, 0, sizeof (struct so_list)); |
| 628 | if (so_list_head == NULL) |
| 629 | { |
| 630 | so_list_head = new_so; |
| 631 | so_list_tail = new_so; |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | so_list_tail->next = new_so; |
| 636 | so_list_tail = new_so; |
| 637 | } |
| 638 | |
| 639 | /* Fill in all the entries in GDB's shared library list. |
| 640 | */ |
| 641 | |
| 642 | new_so->solib_addr = addr; |
| 643 | new_so->som_solib.name = name; |
| 644 | status = target_read_memory (addr + 4, buf, 4); |
| 645 | if (status != 0) |
| 646 | goto err; |
| 647 | |
| 648 | new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1); |
| 649 | new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1); |
| 650 | /* Following is "high water mark", highest version number |
| 651 | * seen, rather than plain version number. |
| 652 | */ |
| 653 | new_so->som_solib.library_version = extract_unsigned_integer (buf, 2); |
| 654 | new_so->som_solib.text_addr = text_addr; |
| 655 | |
| 656 | /* Q: What about longword at "addr + 8"? |
| 657 | * A: It's read above, out of order, into "text_addr". |
| 658 | */ |
| 659 | |
| 660 | status = target_read_memory (addr + 12, buf, 4); |
| 661 | if (status != 0) |
| 662 | goto err; |
| 663 | |
| 664 | new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4); |
| 665 | |
| 666 | status = target_read_memory (addr + 16, buf, 4); |
| 667 | if (status != 0) |
| 668 | goto err; |
| 669 | |
| 670 | new_so->som_solib.text_end = extract_unsigned_integer (buf, 4); |
| 671 | |
| 672 | status = target_read_memory (addr + 20, buf, 4); |
| 673 | if (status != 0) |
| 674 | goto err; |
| 675 | |
| 676 | new_so->som_solib.data_start = extract_unsigned_integer (buf, 4); |
| 677 | |
| 678 | status = target_read_memory (addr + 24, buf, 4); |
| 679 | if (status != 0) |
| 680 | goto err; |
| 681 | |
| 682 | new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4); |
| 683 | |
| 684 | status = target_read_memory (addr + 28, buf, 4); |
| 685 | if (status != 0) |
| 686 | goto err; |
| 687 | |
| 688 | new_so->som_solib.data_end = extract_unsigned_integer (buf, 4); |
| 689 | |
| 690 | status = target_read_memory (addr + 32, buf, 4); |
| 691 | if (status != 0) |
| 692 | goto err; |
| 693 | |
| 694 | new_so->som_solib.got_value = extract_unsigned_integer (buf, 4); |
| 695 | |
| 696 | status = target_read_memory (addr + 36, buf, 4); |
| 697 | if (status != 0) |
| 698 | goto err; |
| 699 | |
| 700 | new_so->som_solib.next = (void *) extract_unsigned_integer (buf, 4); |
| 701 | |
| 702 | /* Note that we don't re-set "addr" to the next pointer |
| 703 | * until after we've read the trailing data. |
| 704 | */ |
| 705 | |
| 706 | status = target_read_memory (addr + 40, buf, 4); |
| 707 | new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4); |
| 708 | if (status != 0) |
| 709 | goto err; |
| 710 | |
| 711 | /* Now indirect via that value! |
| 712 | */ |
| 713 | status = target_read_memory (new_so->som_solib.tsd_start_addr, buf, 4); |
| 714 | new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4); |
| 715 | if (status != 0) |
| 716 | goto err; |
| 717 | #ifdef SOLIB_DEBUG |
| 718 | printf ("\n+ library \"%s\" is described at 0x%x\n", name, addr); |
| 719 | printf (" 'version' is %d\n", new_so->som_solib.struct_version); |
| 720 | printf (" 'bind_mode' is %d\n", new_so->som_solib.bind_mode); |
| 721 | printf (" 'library_version' is %d\n", new_so->som_solib.library_version); |
| 722 | printf (" 'text_addr' is 0x%x\n", new_so->som_solib.text_addr); |
| 723 | printf (" 'text_link_addr' is 0x%x\n", new_so->som_solib.text_link_addr); |
| 724 | printf (" 'text_end' is 0x%x\n", new_so->som_solib.text_end); |
| 725 | printf (" 'data_start' is 0x%x\n", new_so->som_solib.data_start); |
| 726 | printf (" 'bss_start' is 0x%x\n", new_so->som_solib.bss_start); |
| 727 | printf (" 'data_end' is 0x%x\n", new_so->som_solib.data_end); |
| 728 | printf (" 'got_value' is %x\n", new_so->som_solib.got_value); |
| 729 | printf (" 'next' is 0x%x\n", new_so->som_solib.next); |
| 730 | printf (" 'tsd_start_addr' is 0x%x\n", new_so->som_solib.tsd_start_addr); |
| 731 | #endif |
| 732 | |
| 733 | /* Go on to the next shared library descriptor. |
| 734 | */ |
| 735 | addr = (CORE_ADDR) new_so->som_solib.next; |
| 736 | |
| 737 | |
| 738 | |
| 739 | /* At this point, we have essentially hooked the shlib into the |
| 740 | "info share" command. However, we haven't yet loaded its |
| 741 | symbol table. We must now decide whether we ought to, i.e., |
| 742 | whether doing so would exceed the symbol table size threshold. |
| 743 | |
| 744 | If the threshold has just now been exceeded, then we'll issue |
| 745 | a warning message (which explains how to load symbols manually, |
| 746 | if the user so desires). |
| 747 | |
| 748 | If the threshold has just now or previously been exceeded, |
| 749 | we'll just add the shlib to the list of object files, but won't |
| 750 | actually load its symbols. (This is more useful than it might |
| 751 | sound, for it allows us to e.g., still load and use the shlibs' |
| 752 | unwind information for stack tracebacks.) |
| 753 | */ |
| 754 | |
| 755 | /* Note that we DON'T want to preclude the user from using the |
| 756 | add-symbol-file command! Thus, we only worry about the threshold |
| 757 | when we're invoked for other reasons. |
| 758 | */ |
| 759 | st_size = som_solib_sizeof_symbol_table (name); |
| 760 | som_solib_st_size_threshold_exceeded = |
| 761 | !from_tty && |
| 762 | ((st_size + som_solib_total_st_size) > (auto_solib_add * (LONGEST) 1000000)); |
| 763 | |
| 764 | if (som_solib_st_size_threshold_exceeded) |
| 765 | { |
| 766 | if (!threshold_warning_given) |
| 767 | warning ("Symbols for some libraries have not been loaded, because\ndoing so would exceed the size threshold specified by auto-solib-add.\nTo manually load symbols, use the 'sharedlibrary' command.\nTo raise the threshold, set auto-solib-add to a larger value and rerun\nthe program.\n"); |
| 768 | threshold_warning_given = 1; |
| 769 | |
| 770 | /* We'll still make note of this shlib, even if we don't |
| 771 | read its symbols. This allows us to use its unwind |
| 772 | information well enough to know how to e.g., correctly |
| 773 | do a traceback from a PC within the shlib, even if we |
| 774 | can't symbolize those PCs... |
| 775 | */ |
| 776 | som_solib_add_solib_objfile (new_so, name, from_tty, text_addr); |
| 777 | continue; |
| 778 | } |
| 779 | |
| 780 | som_solib_total_st_size += st_size; |
| 781 | |
| 782 | /* This fills in new_so->objfile, among others. */ |
| 783 | som_solib_load_symbols (new_so, name, from_tty, text_addr, target); |
| 784 | } |
| 785 | |
| 786 | #ifdef SOLIB_DEBUG |
| 787 | printf ("--Done reading shared library data\n"); |
| 788 | #endif |
| 789 | |
| 790 | /* Getting new symbols may change our opinion about what is |
| 791 | frameless. */ |
| 792 | reinit_frame_cache (); |
| 793 | return; |
| 794 | |
| 795 | old_dld: |
| 796 | error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n"); |
| 797 | return; |
| 798 | |
| 799 | err: |
| 800 | error ("Error while reading dynamic library list.\n"); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | /* This hook gets called just before the first instruction in the |
| 806 | inferior process is executed. |
| 807 | |
| 808 | This is our opportunity to set magic flags in the inferior so |
| 809 | that GDB can be notified when a shared library is mapped in and |
| 810 | to tell the dynamic linker that a private copy of the library is |
| 811 | needed (so GDB can set breakpoints in the library). |
| 812 | |
| 813 | __dld_flags is the location of the magic flags; as of this implementation |
| 814 | there are 3 flags of interest: |
| 815 | |
| 816 | bit 0 when set indicates that private copies of the libraries are needed |
| 817 | bit 1 when set indicates that the callback hook routine is valid |
| 818 | bit 2 when set indicates that the dynamic linker should maintain the |
| 819 | __dld_list structure when loading/unloading libraries. |
| 820 | |
| 821 | Note that shared libraries are not mapped in at this time, so we have |
| 822 | run the inferior until the libraries are mapped in. Typically this |
| 823 | means running until the "_start" is called. */ |
| 824 | |
| 825 | void |
| 826 | som_solib_create_inferior_hook (void) |
| 827 | { |
| 828 | struct minimal_symbol *msymbol; |
| 829 | unsigned int dld_flags, status, have_endo; |
| 830 | asection *shlib_info; |
| 831 | char buf[4]; |
| 832 | struct objfile *objfile; |
| 833 | CORE_ADDR anaddr; |
| 834 | |
| 835 | /* First, remove all the solib event breakpoints. Their addresses |
| 836 | may have changed since the last time we ran the program. */ |
| 837 | remove_solib_event_breakpoints (); |
| 838 | |
| 839 | if (symfile_objfile == NULL) |
| 840 | return; |
| 841 | |
| 842 | /* First see if the objfile was dynamically linked. */ |
| 843 | shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$"); |
| 844 | if (!shlib_info) |
| 845 | return; |
| 846 | |
| 847 | /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */ |
| 848 | if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0) |
| 849 | return; |
| 850 | |
| 851 | have_endo = 0; |
| 852 | /* Slam the pid of the process into __d_pid; failing is only a warning! */ |
| 853 | msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile); |
| 854 | if (msymbol == NULL) |
| 855 | { |
| 856 | warning ("Unable to find __d_pid symbol in object file."); |
| 857 | warning ("Suggest linking with /opt/langtools/lib/end.o."); |
| 858 | warning ("GDB will be unable to track shl_load/shl_unload calls"); |
| 859 | goto keep_going; |
| 860 | } |
| 861 | |
| 862 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 863 | store_unsigned_integer (buf, 4, inferior_pid); |
| 864 | status = target_write_memory (anaddr, buf, 4); |
| 865 | if (status != 0) |
| 866 | { |
| 867 | warning ("Unable to write __d_pid"); |
| 868 | warning ("Suggest linking with /opt/langtools/lib/end.o."); |
| 869 | warning ("GDB will be unable to track shl_load/shl_unload calls"); |
| 870 | goto keep_going; |
| 871 | } |
| 872 | |
| 873 | /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook; |
| 874 | This will force the dynamic linker to call __d_trap when significant |
| 875 | events occur. |
| 876 | |
| 877 | Note that the above is the pre-HP-UX 9.0 behaviour. At 9.0 and above, |
| 878 | the dld provides an export stub named "__d_trap" as well as the |
| 879 | function named "__d_trap" itself, but doesn't provide "_DLD_HOOK". |
| 880 | We'll look first for the old flavor and then the new. |
| 881 | */ |
| 882 | msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile); |
| 883 | if (msymbol == NULL) |
| 884 | msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile); |
| 885 | if (msymbol == NULL) |
| 886 | { |
| 887 | warning ("Unable to find _DLD_HOOK symbol in object file."); |
| 888 | warning ("Suggest linking with /opt/langtools/lib/end.o."); |
| 889 | warning ("GDB will be unable to track shl_load/shl_unload calls"); |
| 890 | goto keep_going; |
| 891 | } |
| 892 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 893 | dld_cache.hook.address = anaddr; |
| 894 | |
| 895 | /* Grrr, this might not be an export symbol! We have to find the |
| 896 | export stub. */ |
| 897 | ALL_OBJFILES (objfile) |
| 898 | { |
| 899 | struct unwind_table_entry *u; |
| 900 | struct minimal_symbol *msymbol2; |
| 901 | |
| 902 | /* What a crock. */ |
| 903 | msymbol2 = lookup_minimal_symbol_solib_trampoline (SYMBOL_NAME (msymbol), |
| 904 | NULL, objfile); |
| 905 | /* Found a symbol with the right name. */ |
| 906 | if (msymbol2) |
| 907 | { |
| 908 | struct unwind_table_entry *u; |
| 909 | /* It must be a shared library trampoline. */ |
| 910 | if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline) |
| 911 | continue; |
| 912 | |
| 913 | /* It must also be an export stub. */ |
| 914 | u = find_unwind_entry (SYMBOL_VALUE (msymbol2)); |
| 915 | if (!u || u->stub_unwind.stub_type != EXPORT) |
| 916 | continue; |
| 917 | |
| 918 | /* OK. Looks like the correct import stub. */ |
| 919 | anaddr = SYMBOL_VALUE (msymbol2); |
| 920 | dld_cache.hook_stub.address = anaddr; |
| 921 | } |
| 922 | } |
| 923 | store_unsigned_integer (buf, 4, anaddr); |
| 924 | |
| 925 | msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile); |
| 926 | if (msymbol == NULL) |
| 927 | { |
| 928 | warning ("Unable to find __dld_hook symbol in object file."); |
| 929 | warning ("Suggest linking with /opt/langtools/lib/end.o."); |
| 930 | warning ("GDB will be unable to track shl_load/shl_unload calls"); |
| 931 | goto keep_going; |
| 932 | } |
| 933 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 934 | status = target_write_memory (anaddr, buf, 4); |
| 935 | |
| 936 | /* Now set a shlib_event breakpoint at __d_trap so we can track |
| 937 | significant shared library events. */ |
| 938 | msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile); |
| 939 | if (msymbol == NULL) |
| 940 | { |
| 941 | warning ("Unable to find __dld_d_trap symbol in object file."); |
| 942 | warning ("Suggest linking with /opt/langtools/lib/end.o."); |
| 943 | warning ("GDB will be unable to track shl_load/shl_unload calls"); |
| 944 | goto keep_going; |
| 945 | } |
| 946 | create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol)); |
| 947 | |
| 948 | /* We have all the support usually found in end.o, so we can track |
| 949 | shl_load and shl_unload calls. */ |
| 950 | have_endo = 1; |
| 951 | |
| 952 | keep_going: |
| 953 | |
| 954 | /* Get the address of __dld_flags, if no such symbol exists, then we can |
| 955 | not debug the shared code. */ |
| 956 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
| 957 | if (msymbol == NULL) |
| 958 | { |
| 959 | error ("Unable to find __dld_flags symbol in object file.\n"); |
| 960 | } |
| 961 | |
| 962 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 963 | |
| 964 | /* Read the current contents. */ |
| 965 | status = target_read_memory (anaddr, buf, 4); |
| 966 | if (status != 0) |
| 967 | { |
| 968 | error ("Unable to read __dld_flags\n"); |
| 969 | } |
| 970 | dld_flags = extract_unsigned_integer (buf, 4); |
| 971 | |
| 972 | /* Turn on the flags we care about. */ |
| 973 | dld_flags |= DLD_FLAGS_MAPPRIVATE; |
| 974 | if (have_endo) |
| 975 | dld_flags |= DLD_FLAGS_HOOKVALID; |
| 976 | store_unsigned_integer (buf, 4, dld_flags); |
| 977 | status = target_write_memory (anaddr, buf, 4); |
| 978 | if (status != 0) |
| 979 | { |
| 980 | error ("Unable to write __dld_flags\n"); |
| 981 | } |
| 982 | |
| 983 | /* Now find the address of _start and set a breakpoint there. |
| 984 | We still need this code for two reasons: |
| 985 | |
| 986 | * Not all sites have /opt/langtools/lib/end.o, so it's not always |
| 987 | possible to track the dynamic linker's events. |
| 988 | |
| 989 | * At this time no events are triggered for shared libraries |
| 990 | loaded at startup time (what a crock). */ |
| 991 | |
| 992 | msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile); |
| 993 | if (msymbol == NULL) |
| 994 | { |
| 995 | error ("Unable to find _start symbol in object file.\n"); |
| 996 | } |
| 997 | |
| 998 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 999 | |
| 1000 | /* Make the breakpoint at "_start" a shared library event breakpoint. */ |
| 1001 | create_solib_event_breakpoint (anaddr); |
| 1002 | |
| 1003 | /* Wipe out all knowledge of old shared libraries since their |
| 1004 | mapping can change from one exec to another! */ |
| 1005 | while (so_list_head) |
| 1006 | { |
| 1007 | struct so_list *temp; |
| 1008 | |
| 1009 | temp = so_list_head; |
| 1010 | free (so_list_head); |
| 1011 | so_list_head = temp->next; |
| 1012 | } |
| 1013 | clear_symtab_users (); |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | static void |
| 1018 | reset_inferior_pid (int saved_inferior_pid) |
| 1019 | { |
| 1020 | inferior_pid = saved_inferior_pid; |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | /* This operation removes the "hook" between GDB and the dynamic linker, |
| 1025 | which causes the dld to notify GDB of shared library events. |
| 1026 | |
| 1027 | After this operation completes, the dld will no longer notify GDB of |
| 1028 | shared library events. To resume notifications, GDB must call |
| 1029 | som_solib_create_inferior_hook. |
| 1030 | |
| 1031 | This operation does not remove any knowledge of shared libraries which |
| 1032 | GDB may already have been notified of. |
| 1033 | */ |
| 1034 | void |
| 1035 | som_solib_remove_inferior_hook (int pid) |
| 1036 | { |
| 1037 | CORE_ADDR addr; |
| 1038 | struct minimal_symbol *msymbol; |
| 1039 | int status; |
| 1040 | char dld_flags_buffer[TARGET_INT_BIT / TARGET_CHAR_BIT]; |
| 1041 | unsigned int dld_flags_value; |
| 1042 | int saved_inferior_pid = inferior_pid; |
| 1043 | struct cleanup *old_cleanups = make_cleanup (reset_inferior_pid, saved_inferior_pid); |
| 1044 | |
| 1045 | /* Ensure that we're really operating on the specified process. */ |
| 1046 | inferior_pid = pid; |
| 1047 | |
| 1048 | /* We won't bother to remove the solib breakpoints from this process. |
| 1049 | |
| 1050 | In fact, on PA64 the breakpoint is hard-coded into the dld callback, |
| 1051 | and thus we're not supposed to remove it. |
| 1052 | |
| 1053 | Rather, we'll merely clear the dld_flags bit that enables callbacks. |
| 1054 | */ |
| 1055 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
| 1056 | |
| 1057 | addr = SYMBOL_VALUE_ADDRESS (msymbol); |
| 1058 | status = target_read_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT); |
| 1059 | |
| 1060 | dld_flags_value = extract_unsigned_integer (dld_flags_buffer, |
| 1061 | sizeof (dld_flags_value)); |
| 1062 | |
| 1063 | dld_flags_value &= ~DLD_FLAGS_HOOKVALID; |
| 1064 | store_unsigned_integer (dld_flags_buffer, |
| 1065 | sizeof (dld_flags_value), |
| 1066 | dld_flags_value); |
| 1067 | status = target_write_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT); |
| 1068 | |
| 1069 | do_cleanups (old_cleanups); |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | /* This function creates a breakpoint on the dynamic linker hook, which |
| 1074 | is called when e.g., a shl_load or shl_unload call is made. This |
| 1075 | breakpoint will only trigger when a shl_load call is made. |
| 1076 | |
| 1077 | If filename is NULL, then loads of any dll will be caught. Else, |
| 1078 | only loads of the file whose pathname is the string contained by |
| 1079 | filename will be caught. |
| 1080 | |
| 1081 | Undefined behaviour is guaranteed if this function is called before |
| 1082 | som_solib_create_inferior_hook. |
| 1083 | */ |
| 1084 | void |
| 1085 | som_solib_create_catch_load_hook (int pid, int tempflag, char *filename, |
| 1086 | char *cond_string) |
| 1087 | { |
| 1088 | create_solib_load_event_breakpoint ("__d_trap", tempflag, filename, cond_string); |
| 1089 | } |
| 1090 | |
| 1091 | /* This function creates a breakpoint on the dynamic linker hook, which |
| 1092 | is called when e.g., a shl_load or shl_unload call is made. This |
| 1093 | breakpoint will only trigger when a shl_unload call is made. |
| 1094 | |
| 1095 | If filename is NULL, then unloads of any dll will be caught. Else, |
| 1096 | only unloads of the file whose pathname is the string contained by |
| 1097 | filename will be caught. |
| 1098 | |
| 1099 | Undefined behaviour is guaranteed if this function is called before |
| 1100 | som_solib_create_inferior_hook. |
| 1101 | */ |
| 1102 | void |
| 1103 | som_solib_create_catch_unload_hook (int pid, int tempflag, char *filename, |
| 1104 | char *cond_string) |
| 1105 | { |
| 1106 | create_solib_unload_event_breakpoint ("__d_trap", tempflag, filename, cond_string); |
| 1107 | } |
| 1108 | |
| 1109 | int |
| 1110 | som_solib_have_load_event (int pid) |
| 1111 | { |
| 1112 | CORE_ADDR event_kind; |
| 1113 | |
| 1114 | event_kind = read_register (ARG0_REGNUM); |
| 1115 | return (event_kind == SHL_LOAD); |
| 1116 | } |
| 1117 | |
| 1118 | int |
| 1119 | som_solib_have_unload_event (int pid) |
| 1120 | { |
| 1121 | CORE_ADDR event_kind; |
| 1122 | |
| 1123 | event_kind = read_register (ARG0_REGNUM); |
| 1124 | return (event_kind == SHL_UNLOAD); |
| 1125 | } |
| 1126 | |
| 1127 | static char * |
| 1128 | som_solib_library_pathname (int pid) |
| 1129 | { |
| 1130 | CORE_ADDR dll_handle_address; |
| 1131 | CORE_ADDR dll_pathname_address; |
| 1132 | struct som_solib_mapped_entry dll_descriptor; |
| 1133 | char *p; |
| 1134 | static char dll_pathname[1024]; |
| 1135 | |
| 1136 | /* Read the descriptor of this newly-loaded library. */ |
| 1137 | dll_handle_address = read_register (ARG1_REGNUM); |
| 1138 | read_memory (dll_handle_address, (char *) &dll_descriptor, sizeof (dll_descriptor)); |
| 1139 | |
| 1140 | /* We can find a pointer to the dll's pathname within the descriptor. */ |
| 1141 | dll_pathname_address = (CORE_ADDR) dll_descriptor.name; |
| 1142 | |
| 1143 | /* Read the pathname, one byte at a time. */ |
| 1144 | p = dll_pathname; |
| 1145 | for (;;) |
| 1146 | { |
| 1147 | char b; |
| 1148 | read_memory (dll_pathname_address++, (char *) &b, 1); |
| 1149 | *p++ = b; |
| 1150 | if (b == '\0') |
| 1151 | break; |
| 1152 | } |
| 1153 | |
| 1154 | return dll_pathname; |
| 1155 | } |
| 1156 | |
| 1157 | char * |
| 1158 | som_solib_loaded_library_pathname (int pid) |
| 1159 | { |
| 1160 | if (!som_solib_have_load_event (pid)) |
| 1161 | error ("Must have a load event to use this query"); |
| 1162 | |
| 1163 | return som_solib_library_pathname (pid); |
| 1164 | } |
| 1165 | |
| 1166 | char * |
| 1167 | som_solib_unloaded_library_pathname (int pid) |
| 1168 | { |
| 1169 | if (!som_solib_have_unload_event (pid)) |
| 1170 | error ("Must have an unload event to use this query"); |
| 1171 | |
| 1172 | return som_solib_library_pathname (pid); |
| 1173 | } |
| 1174 | |
| 1175 | static void |
| 1176 | som_solib_desire_dynamic_linker_symbols (void) |
| 1177 | { |
| 1178 | struct objfile *objfile; |
| 1179 | struct unwind_table_entry *u; |
| 1180 | struct minimal_symbol *dld_msymbol; |
| 1181 | |
| 1182 | /* Do we already know the value of these symbols? If so, then |
| 1183 | we've no work to do. |
| 1184 | |
| 1185 | (If you add clauses to this test, be sure to likewise update the |
| 1186 | test within the loop.) |
| 1187 | */ |
| 1188 | if (dld_cache.is_valid) |
| 1189 | return; |
| 1190 | |
| 1191 | ALL_OBJFILES (objfile) |
| 1192 | { |
| 1193 | dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile); |
| 1194 | if (dld_msymbol != NULL) |
| 1195 | { |
| 1196 | dld_cache.load.address = SYMBOL_VALUE (dld_msymbol); |
| 1197 | dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address); |
| 1198 | } |
| 1199 | |
| 1200 | dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load", |
| 1201 | NULL, |
| 1202 | objfile); |
| 1203 | if (dld_msymbol != NULL) |
| 1204 | { |
| 1205 | if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline) |
| 1206 | { |
| 1207 | u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol)); |
| 1208 | if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT)) |
| 1209 | { |
| 1210 | dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol); |
| 1211 | dld_cache.load_stub.unwind = u; |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile); |
| 1217 | if (dld_msymbol != NULL) |
| 1218 | { |
| 1219 | dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol); |
| 1220 | dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address); |
| 1221 | |
| 1222 | /* ??rehrauer: I'm not sure exactly what this is, but it appears |
| 1223 | that on some HPUX 10.x versions, there's two unwind regions to |
| 1224 | cover the body of "shl_unload", the second being 4 bytes past |
| 1225 | the end of the first. This is a large hack to handle that |
| 1226 | case, but since I don't seem to have any legitimate way to |
| 1227 | look for this thing via the symbol table... |
| 1228 | */ |
| 1229 | if (dld_cache.unload.unwind != NULL) |
| 1230 | { |
| 1231 | u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4); |
| 1232 | if (u != NULL) |
| 1233 | { |
| 1234 | dld_cache.unload2.address = u->region_start; |
| 1235 | dld_cache.unload2.unwind = u; |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload", |
| 1241 | NULL, |
| 1242 | objfile); |
| 1243 | if (dld_msymbol != NULL) |
| 1244 | { |
| 1245 | if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline) |
| 1246 | { |
| 1247 | u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol)); |
| 1248 | if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT)) |
| 1249 | { |
| 1250 | dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol); |
| 1251 | dld_cache.unload_stub.unwind = u; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* Did we find everything we were looking for? If so, stop. */ |
| 1257 | if ((dld_cache.load.address != NULL) && (dld_cache.load_stub.address != NULL) |
| 1258 | && (dld_cache.unload.address != NULL) && (dld_cache.unload_stub.address != NULL)) |
| 1259 | { |
| 1260 | dld_cache.is_valid = 1; |
| 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address); |
| 1266 | dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address); |
| 1267 | |
| 1268 | /* We're prepared not to find some of these symbols, which is why |
| 1269 | this function is a "desire" operation, and not a "require". |
| 1270 | */ |
| 1271 | } |
| 1272 | |
| 1273 | int |
| 1274 | som_solib_in_dynamic_linker (int pid, CORE_ADDR pc) |
| 1275 | { |
| 1276 | struct unwind_table_entry *u_pc; |
| 1277 | |
| 1278 | /* Are we in the dld itself? |
| 1279 | |
| 1280 | ??rehrauer: Large hack -- We'll assume that any address in a |
| 1281 | shared text region is the dld's text. This would obviously |
| 1282 | fall down if the user attached to a process, whose shlibs |
| 1283 | weren't mapped to a (writeable) private region. However, in |
| 1284 | that case the debugger probably isn't able to set the fundamental |
| 1285 | breakpoint in the dld callback anyways, so this hack should be |
| 1286 | safe. |
| 1287 | */ |
| 1288 | if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000) |
| 1289 | return 1; |
| 1290 | |
| 1291 | /* Cache the address of some symbols that are part of the dynamic |
| 1292 | linker, if not already known. |
| 1293 | */ |
| 1294 | som_solib_desire_dynamic_linker_symbols (); |
| 1295 | |
| 1296 | /* Are we in the dld callback? Or its export stub? */ |
| 1297 | u_pc = find_unwind_entry (pc); |
| 1298 | if (u_pc == NULL) |
| 1299 | return 0; |
| 1300 | |
| 1301 | if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind)) |
| 1302 | return 1; |
| 1303 | |
| 1304 | /* Or the interface of the dld (i.e., "shl_load" or friends)? */ |
| 1305 | if ((u_pc == dld_cache.load.unwind) |
| 1306 | || (u_pc == dld_cache.unload.unwind) |
| 1307 | || (u_pc == dld_cache.unload2.unwind) |
| 1308 | || (u_pc == dld_cache.load_stub.unwind) |
| 1309 | || (u_pc == dld_cache.unload_stub.unwind)) |
| 1310 | return 1; |
| 1311 | |
| 1312 | /* Apparently this address isn't part of the dld's text. */ |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | /* Return the GOT value for the shared library in which ADDR belongs. If |
| 1318 | ADDR isn't in any known shared library, return zero. */ |
| 1319 | |
| 1320 | CORE_ADDR |
| 1321 | som_solib_get_got_by_pc (CORE_ADDR addr) |
| 1322 | { |
| 1323 | struct so_list *so_list = so_list_head; |
| 1324 | CORE_ADDR got_value = 0; |
| 1325 | |
| 1326 | while (so_list) |
| 1327 | { |
| 1328 | if (so_list->som_solib.text_addr <= addr |
| 1329 | && so_list->som_solib.text_end > addr) |
| 1330 | { |
| 1331 | got_value = so_list->som_solib.got_value; |
| 1332 | break; |
| 1333 | } |
| 1334 | so_list = so_list->next; |
| 1335 | } |
| 1336 | return got_value; |
| 1337 | } |
| 1338 | |
| 1339 | /* elz: |
| 1340 | Return the address of the handle of the shared library |
| 1341 | in which ADDR belongs. If |
| 1342 | ADDR isn't in any known shared library, return zero. */ |
| 1343 | /* this function is used in hppa_fix_call_dummy in hppa-tdep.c */ |
| 1344 | |
| 1345 | CORE_ADDR |
| 1346 | som_solib_get_solib_by_pc (CORE_ADDR addr) |
| 1347 | { |
| 1348 | struct so_list *so_list = so_list_head; |
| 1349 | |
| 1350 | while (so_list) |
| 1351 | { |
| 1352 | if (so_list->som_solib.text_addr <= addr |
| 1353 | && so_list->som_solib.text_end > addr) |
| 1354 | { |
| 1355 | break; |
| 1356 | } |
| 1357 | so_list = so_list->next; |
| 1358 | } |
| 1359 | if (so_list) |
| 1360 | return so_list->solib_addr; |
| 1361 | else |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | int |
| 1367 | som_solib_section_offsets (struct objfile *objfile, |
| 1368 | struct section_offsets *offsets) |
| 1369 | { |
| 1370 | struct so_list *so_list = so_list_head; |
| 1371 | |
| 1372 | while (so_list) |
| 1373 | { |
| 1374 | /* Oh what a pain! We need the offsets before so_list->objfile |
| 1375 | is valid. The BFDs will never match. Make a best guess. */ |
| 1376 | if (strstr (objfile->name, so_list->som_solib.name)) |
| 1377 | { |
| 1378 | asection *private_section; |
| 1379 | |
| 1380 | /* The text offset is easy. */ |
| 1381 | offsets->offsets[SECT_OFF_TEXT (objfile)] |
| 1382 | = (so_list->som_solib.text_addr |
| 1383 | - so_list->som_solib.text_link_addr); |
| 1384 | offsets->offsets[SECT_OFF_RODATA (objfile)] |
| 1385 | = ANOFFSET (offsets, SECT_OFF_TEXT (objfile)); |
| 1386 | |
| 1387 | /* We should look at presumed_dp in the SOM header, but |
| 1388 | that's not easily available. This should be OK though. */ |
| 1389 | private_section = bfd_get_section_by_name (objfile->obfd, |
| 1390 | "$PRIVATE$"); |
| 1391 | if (!private_section) |
| 1392 | { |
| 1393 | warning ("Unable to find $PRIVATE$ in shared library!"); |
| 1394 | offsets->offsets[SECT_OFF_DATA (objfile)] = 0; |
| 1395 | offsets->offsets[SECT_OFF_BSS (objfile)] = 0; |
| 1396 | return 1; |
| 1397 | } |
| 1398 | offsets->offsets[SECT_OFF_DATA (objfile)] |
| 1399 | = (so_list->som_solib.data_start - private_section->vma); |
| 1400 | offsets->offsets[SECT_OFF_BSS (objfile)] |
| 1401 | = ANOFFSET (offsets, SECT_OFF_DATA (objfile)); |
| 1402 | return 1; |
| 1403 | } |
| 1404 | so_list = so_list->next; |
| 1405 | } |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
| 1409 | /* Dump information about all the currently loaded shared libraries. */ |
| 1410 | |
| 1411 | static void |
| 1412 | som_sharedlibrary_info_command (char *ignore, int from_tty) |
| 1413 | { |
| 1414 | struct so_list *so_list = so_list_head; |
| 1415 | |
| 1416 | if (exec_bfd == NULL) |
| 1417 | { |
| 1418 | printf_unfiltered ("No executable file.\n"); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | if (so_list == NULL) |
| 1423 | { |
| 1424 | printf_unfiltered ("No shared libraries loaded at this time.\n"); |
| 1425 | return; |
| 1426 | } |
| 1427 | |
| 1428 | printf_unfiltered ("Shared Object Libraries\n"); |
| 1429 | printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n", |
| 1430 | " flags", " tstart", " tend", " dstart", " dend", " dlt"); |
| 1431 | while (so_list) |
| 1432 | { |
| 1433 | unsigned int flags; |
| 1434 | |
| 1435 | flags = so_list->som_solib.struct_version << 24; |
| 1436 | flags |= so_list->som_solib.bind_mode << 16; |
| 1437 | flags |= so_list->som_solib.library_version; |
| 1438 | printf_unfiltered ("%s", so_list->som_solib.name); |
| 1439 | if (so_list->objfile == NULL) |
| 1440 | printf_unfiltered (" (symbols not loaded)"); |
| 1441 | printf_unfiltered ("\n"); |
| 1442 | printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l")); |
| 1443 | printf_unfiltered ("%-12s", |
| 1444 | local_hex_string_custom (so_list->som_solib.text_addr, "08l")); |
| 1445 | printf_unfiltered ("%-12s", |
| 1446 | local_hex_string_custom (so_list->som_solib.text_end, "08l")); |
| 1447 | printf_unfiltered ("%-12s", |
| 1448 | local_hex_string_custom (so_list->som_solib.data_start, "08l")); |
| 1449 | printf_unfiltered ("%-12s", |
| 1450 | local_hex_string_custom (so_list->som_solib.data_end, "08l")); |
| 1451 | printf_unfiltered ("%-12s\n", |
| 1452 | local_hex_string_custom (so_list->som_solib.got_value, "08l")); |
| 1453 | so_list = so_list->next; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | static void |
| 1458 | som_solib_sharedlibrary_command (char *args, int from_tty) |
| 1459 | { |
| 1460 | dont_repeat (); |
| 1461 | som_solib_add (args, from_tty, (struct target_ops *) 0); |
| 1462 | } |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | char * |
| 1467 | som_solib_address (CORE_ADDR addr) |
| 1468 | { |
| 1469 | struct so_list *so = so_list_head; |
| 1470 | |
| 1471 | while (so) |
| 1472 | { |
| 1473 | /* Is this address within this shlib's text range? If so, |
| 1474 | return the shlib's name. |
| 1475 | */ |
| 1476 | if ((addr >= so->som_solib.text_addr) && (addr <= so->som_solib.text_end)) |
| 1477 | return so->som_solib.name; |
| 1478 | |
| 1479 | /* Nope, keep looking... */ |
| 1480 | so = so->next; |
| 1481 | } |
| 1482 | |
| 1483 | /* No, we couldn't prove that the address is within a shlib. */ |
| 1484 | return NULL; |
| 1485 | } |
| 1486 | |
| 1487 | |
| 1488 | void |
| 1489 | som_solib_restart (void) |
| 1490 | { |
| 1491 | struct so_list *sl = so_list_head; |
| 1492 | |
| 1493 | /* Before the shlib info vanishes, use it to disable any breakpoints |
| 1494 | that may still be active in those shlibs. |
| 1495 | */ |
| 1496 | disable_breakpoints_in_shlibs (0); |
| 1497 | |
| 1498 | /* Discard all the shlib descriptors. |
| 1499 | */ |
| 1500 | while (sl) |
| 1501 | { |
| 1502 | struct so_list *next_sl = sl->next; |
| 1503 | free (sl); |
| 1504 | sl = next_sl; |
| 1505 | } |
| 1506 | so_list_head = NULL; |
| 1507 | |
| 1508 | som_solib_total_st_size = (LONGEST) 0; |
| 1509 | som_solib_st_size_threshold_exceeded = 0; |
| 1510 | |
| 1511 | dld_cache.is_valid = 0; |
| 1512 | |
| 1513 | dld_cache.hook.address = 0; |
| 1514 | dld_cache.hook.unwind = NULL; |
| 1515 | |
| 1516 | dld_cache.hook_stub.address = 0; |
| 1517 | dld_cache.hook_stub.unwind = NULL; |
| 1518 | |
| 1519 | dld_cache.load.address = 0; |
| 1520 | dld_cache.load.unwind = NULL; |
| 1521 | |
| 1522 | dld_cache.load_stub.address = 0; |
| 1523 | dld_cache.load_stub.unwind = NULL; |
| 1524 | |
| 1525 | dld_cache.unload.address = 0; |
| 1526 | dld_cache.unload.unwind = NULL; |
| 1527 | |
| 1528 | dld_cache.unload2.address = 0; |
| 1529 | dld_cache.unload2.unwind = NULL; |
| 1530 | |
| 1531 | dld_cache.unload_stub.address = 0; |
| 1532 | dld_cache.unload_stub.unwind = NULL; |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | void |
| 1538 | _initialize_som_solib (void) |
| 1539 | { |
| 1540 | add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command, |
| 1541 | "Load shared object library symbols for files matching REGEXP."); |
| 1542 | add_info ("sharedlibrary", som_sharedlibrary_info_command, |
| 1543 | "Status of loaded shared object libraries."); |
| 1544 | add_show_from_set |
| 1545 | (add_set_cmd ("auto-solib-add", class_support, var_zinteger, |
| 1546 | (char *) &auto_solib_add, |
| 1547 | "Set autoloading size threshold (in megabytes) of shared library symbols.\n\ |
| 1548 | If nonzero, symbols from all shared object libraries will be loaded\n\ |
| 1549 | automatically when the inferior begins execution or when the dynamic linker\n\ |
| 1550 | informs gdb that a new library has been loaded, until the symbol table\n\ |
| 1551 | of the program and libraries exceeds this threshold.\n\ |
| 1552 | Otherwise, symbols must be loaded manually, using `sharedlibrary'.", |
| 1553 | &setlist), |
| 1554 | &showlist); |
| 1555 | |
| 1556 | /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how much |
| 1557 | data space a process can use. We ought to be reading MAXDSIZ and |
| 1558 | setting auto_solib_add to some large fraction of that value. If |
| 1559 | not that, we maybe ought to be setting it smaller than the default |
| 1560 | for MAXDSIZ (that being 64Mb, I believe). However, [1] this threshold |
| 1561 | is only crudely approximated rather than actually measured, and [2] |
| 1562 | 50 Mbytes is too small for debugging gdb itself. Thus, the arbitrary |
| 1563 | 100 figure. |
| 1564 | */ |
| 1565 | auto_solib_add = 100; /* Megabytes */ |
| 1566 | |
| 1567 | som_solib_restart (); |
| 1568 | } |
| 1569 | |
| 1570 | /* Get some HPUX-specific data from a shared lib. |
| 1571 | */ |
| 1572 | CORE_ADDR |
| 1573 | so_lib_thread_start_addr (struct so_list *so) |
| 1574 | { |
| 1575 | return so->som_solib.tsd_start_addr; |
| 1576 | } |