| 1 | /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger. |
| 2 | Copyright 1990, 91, 92, 93, 94, 95, 96, 98, 1999 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | Boston, MA 02111-1307, USA. */ |
| 21 | |
| 22 | |
| 23 | #include "defs.h" |
| 24 | |
| 25 | /* This file is only compilable if link.h is available. */ |
| 26 | |
| 27 | #ifdef HAVE_LINK_H |
| 28 | |
| 29 | #include <sys/types.h> |
| 30 | #include <signal.h> |
| 31 | #include "gdb_string.h" |
| 32 | #include <sys/param.h> |
| 33 | #include <fcntl.h> |
| 34 | |
| 35 | #ifndef SVR4_SHARED_LIBS |
| 36 | /* SunOS shared libs need the nlist structure. */ |
| 37 | #include <a.out.h> |
| 38 | #else |
| 39 | #include "elf/external.h" |
| 40 | #endif |
| 41 | |
| 42 | #include <link.h> |
| 43 | |
| 44 | #include "symtab.h" |
| 45 | #include "bfd.h" |
| 46 | #include "symfile.h" |
| 47 | #include "objfiles.h" |
| 48 | #include "gdbcore.h" |
| 49 | #include "command.h" |
| 50 | #include "target.h" |
| 51 | #include "frame.h" |
| 52 | #include "gdb_regex.h" |
| 53 | #include "inferior.h" |
| 54 | #include "environ.h" |
| 55 | #include "language.h" |
| 56 | #include "gdbcmd.h" |
| 57 | |
| 58 | #define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */ |
| 59 | |
| 60 | /* On SVR4 systems, a list of symbols in the dynamic linker where |
| 61 | GDB can try to place a breakpoint to monitor shared library |
| 62 | events. |
| 63 | |
| 64 | If none of these symbols are found, or other errors occur, then |
| 65 | SVR4 systems will fall back to using a symbol as the "startup |
| 66 | mapping complete" breakpoint address. */ |
| 67 | |
| 68 | #ifdef SVR4_SHARED_LIBS |
| 69 | static char *solib_break_names[] = |
| 70 | { |
| 71 | "r_debug_state", |
| 72 | "_r_debug_state", |
| 73 | "_dl_debug_state", |
| 74 | "rtld_db_dlactivity", |
| 75 | NULL |
| 76 | }; |
| 77 | #endif |
| 78 | |
| 79 | #define BKPT_AT_SYMBOL 1 |
| 80 | |
| 81 | #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS) |
| 82 | static char *bkpt_names[] = |
| 83 | { |
| 84 | #ifdef SOLIB_BKPT_NAME |
| 85 | SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */ |
| 86 | #endif |
| 87 | "_start", |
| 88 | "main", |
| 89 | NULL |
| 90 | }; |
| 91 | #endif |
| 92 | |
| 93 | /* Symbols which are used to locate the base of the link map structures. */ |
| 94 | |
| 95 | #ifndef SVR4_SHARED_LIBS |
| 96 | static char *debug_base_symbols[] = |
| 97 | { |
| 98 | "_DYNAMIC", |
| 99 | "_DYNAMIC__MGC", |
| 100 | NULL |
| 101 | }; |
| 102 | #endif |
| 103 | |
| 104 | static char *main_name_list[] = |
| 105 | { |
| 106 | "main_$main", |
| 107 | NULL |
| 108 | }; |
| 109 | |
| 110 | /* local data declarations */ |
| 111 | |
| 112 | /* Macro to extract an address from a solib structure. |
| 113 | When GDB is configured for some 32-bit targets (e.g. Solaris 2.7 |
| 114 | sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is |
| 115 | 64 bits. We have to extract only the significant bits of addresses |
| 116 | to get the right address when accessing the core file BFD. */ |
| 117 | |
| 118 | #define SOLIB_EXTRACT_ADDRESS(member) \ |
| 119 | extract_address (&member, sizeof (member)) |
| 120 | |
| 121 | #ifndef SVR4_SHARED_LIBS |
| 122 | |
| 123 | #define LM_ADDR(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.lm_addr)) |
| 124 | #define LM_NEXT(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.lm_next)) |
| 125 | #define LM_NAME(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.lm_name)) |
| 126 | /* Test for first link map entry; first entry is a shared library. */ |
| 127 | #define IGNORE_FIRST_LINK_MAP_ENTRY(so) (0) |
| 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 | #else /* SVR4_SHARED_LIBS */ |
| 135 | |
| 136 | #define LM_ADDR(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_addr)) |
| 137 | #define LM_NEXT(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_next)) |
| 138 | #define LM_NAME(so) (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_name)) |
| 139 | /* Test for first link map entry; first entry is the exec-file. */ |
| 140 | #define IGNORE_FIRST_LINK_MAP_ENTRY(so) \ |
| 141 | (SOLIB_EXTRACT_ADDRESS ((so) -> lm.l_prev) == 0) |
| 142 | static struct r_debug debug_copy; |
| 143 | char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */ |
| 144 | |
| 145 | #endif /* !SVR4_SHARED_LIBS */ |
| 146 | |
| 147 | struct so_list |
| 148 | { |
| 149 | /* The following fields of the structure come directly from the |
| 150 | dynamic linker's tables in the inferior, and are initialized by |
| 151 | current_sos. */ |
| 152 | |
| 153 | struct so_list *next; /* next structure in linked list */ |
| 154 | struct link_map lm; /* copy of link map from inferior */ |
| 155 | CORE_ADDR lmaddr; /* addr in inferior lm was read from */ |
| 156 | |
| 157 | /* Shared object file name, exactly as it appears in the |
| 158 | inferior's link map. This may be a relative path, or something |
| 159 | which needs to be looked up in LD_LIBRARY_PATH, etc. We use it |
| 160 | to tell which entries in the inferior's dynamic linker's link |
| 161 | map we've already loaded. */ |
| 162 | char so_original_name[MAX_PATH_SIZE]; |
| 163 | |
| 164 | /* shared object file name, expanded to something GDB can open */ |
| 165 | char so_name[MAX_PATH_SIZE]; |
| 166 | |
| 167 | /* The following fields of the structure are built from |
| 168 | information gathered from the shared object file itself, and |
| 169 | are initialized when we actually add it to our symbol tables. */ |
| 170 | |
| 171 | bfd *abfd; |
| 172 | CORE_ADDR lmend; /* upper addr bound of mapped object */ |
| 173 | char symbols_loaded; /* flag: symbols read in yet? */ |
| 174 | char from_tty; /* flag: print msgs? */ |
| 175 | struct objfile *objfile; /* objfile for loaded lib */ |
| 176 | struct section_table *sections; |
| 177 | struct section_table *sections_end; |
| 178 | struct section_table *textsection; |
| 179 | }; |
| 180 | |
| 181 | static struct so_list *so_list_head; /* List of known shared objects */ |
| 182 | static CORE_ADDR debug_base; /* Base of dynamic linker structures */ |
| 183 | static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */ |
| 184 | |
| 185 | static int solib_cleanup_queued = 0; /* make_run_cleanup called */ |
| 186 | |
| 187 | extern int fdmatch (int, int); /* In libiberty */ |
| 188 | |
| 189 | /* Local function prototypes */ |
| 190 | |
| 191 | static void do_clear_solib (PTR); |
| 192 | |
| 193 | static int match_main (char *); |
| 194 | |
| 195 | static void special_symbol_handling (void); |
| 196 | |
| 197 | static void sharedlibrary_command (char *, int); |
| 198 | |
| 199 | static int enable_break (void); |
| 200 | |
| 201 | static void info_sharedlibrary_command (char *, int); |
| 202 | |
| 203 | static int symbol_add_stub (PTR); |
| 204 | |
| 205 | static CORE_ADDR first_link_map_member (void); |
| 206 | |
| 207 | static CORE_ADDR locate_base (void); |
| 208 | |
| 209 | static int solib_map_sections (PTR); |
| 210 | |
| 211 | #ifdef SVR4_SHARED_LIBS |
| 212 | |
| 213 | static CORE_ADDR elf_locate_base (void); |
| 214 | |
| 215 | #else |
| 216 | |
| 217 | static struct so_list *current_sos (void); |
| 218 | static void free_so (struct so_list *node); |
| 219 | |
| 220 | static int disable_break (void); |
| 221 | |
| 222 | static void allocate_rt_common_objfile (void); |
| 223 | |
| 224 | static void |
| 225 | solib_add_common_symbols (CORE_ADDR); |
| 226 | |
| 227 | #endif |
| 228 | |
| 229 | void _initialize_solib (void); |
| 230 | |
| 231 | /* If non-zero, this is a prefix that will be added to the front of the name |
| 232 | shared libraries with an absolute filename for loading. */ |
| 233 | static char *solib_absolute_prefix = NULL; |
| 234 | |
| 235 | /* If non-empty, this is a search path for loading non-absolute shared library |
| 236 | symbol files. This takes precedence over the environment variables PATH |
| 237 | and LD_LIBRARY_PATH. */ |
| 238 | static char *solib_search_path = NULL; |
| 239 | |
| 240 | /* |
| 241 | |
| 242 | LOCAL FUNCTION |
| 243 | |
| 244 | solib_map_sections -- open bfd and build sections for shared lib |
| 245 | |
| 246 | SYNOPSIS |
| 247 | |
| 248 | static int solib_map_sections (struct so_list *so) |
| 249 | |
| 250 | DESCRIPTION |
| 251 | |
| 252 | Given a pointer to one of the shared objects in our list |
| 253 | of mapped objects, use the recorded name to open a bfd |
| 254 | descriptor for the object, build a section table, and then |
| 255 | relocate all the section addresses by the base address at |
| 256 | which the shared object was mapped. |
| 257 | |
| 258 | FIXMES |
| 259 | |
| 260 | In most (all?) cases the shared object file name recorded in the |
| 261 | dynamic linkage tables will be a fully qualified pathname. For |
| 262 | cases where it isn't, do we really mimic the systems search |
| 263 | mechanism correctly in the below code (particularly the tilde |
| 264 | expansion stuff?). |
| 265 | */ |
| 266 | |
| 267 | static int |
| 268 | solib_map_sections (arg) |
| 269 | PTR arg; |
| 270 | { |
| 271 | struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */ |
| 272 | char *filename; |
| 273 | char *scratch_pathname; |
| 274 | int scratch_chan; |
| 275 | struct section_table *p; |
| 276 | struct cleanup *old_chain; |
| 277 | bfd *abfd; |
| 278 | |
| 279 | filename = tilde_expand (so->so_name); |
| 280 | |
| 281 | if (solib_absolute_prefix && ROOTED_P (filename)) |
| 282 | /* Prefix shared libraries with absolute filenames with |
| 283 | SOLIB_ABSOLUTE_PREFIX. */ |
| 284 | { |
| 285 | char *pfxed_fn; |
| 286 | int pfx_len; |
| 287 | |
| 288 | pfx_len = strlen (solib_absolute_prefix); |
| 289 | |
| 290 | /* Remove trailing slashes. */ |
| 291 | while (pfx_len > 0 && SLASH_P (solib_absolute_prefix[pfx_len - 1])) |
| 292 | pfx_len--; |
| 293 | |
| 294 | pfxed_fn = xmalloc (pfx_len + strlen (filename) + 1); |
| 295 | strcpy (pfxed_fn, solib_absolute_prefix); |
| 296 | strcat (pfxed_fn, filename); |
| 297 | free (filename); |
| 298 | |
| 299 | filename = pfxed_fn; |
| 300 | } |
| 301 | |
| 302 | old_chain = make_cleanup (free, filename); |
| 303 | |
| 304 | scratch_chan = -1; |
| 305 | |
| 306 | if (solib_search_path) |
| 307 | scratch_chan = openp (solib_search_path, |
| 308 | 1, filename, O_RDONLY, 0, &scratch_pathname); |
| 309 | if (scratch_chan < 0) |
| 310 | scratch_chan = openp (get_in_environ (inferior_environ, "PATH"), |
| 311 | 1, filename, O_RDONLY, 0, &scratch_pathname); |
| 312 | if (scratch_chan < 0) |
| 313 | { |
| 314 | scratch_chan = openp (get_in_environ |
| 315 | (inferior_environ, "LD_LIBRARY_PATH"), |
| 316 | 1, filename, O_RDONLY, 0, &scratch_pathname); |
| 317 | } |
| 318 | if (scratch_chan < 0) |
| 319 | { |
| 320 | perror_with_name (filename); |
| 321 | } |
| 322 | /* Leave scratch_pathname allocated. abfd->name will point to it. */ |
| 323 | |
| 324 | abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan); |
| 325 | if (!abfd) |
| 326 | { |
| 327 | close (scratch_chan); |
| 328 | error ("Could not open `%s' as an executable file: %s", |
| 329 | scratch_pathname, bfd_errmsg (bfd_get_error ())); |
| 330 | } |
| 331 | /* Leave bfd open, core_xfer_memory and "info files" need it. */ |
| 332 | so->abfd = abfd; |
| 333 | abfd->cacheable = true; |
| 334 | |
| 335 | /* copy full path name into so_name, so that later symbol_file_add can find |
| 336 | it */ |
| 337 | if (strlen (scratch_pathname) >= MAX_PATH_SIZE) |
| 338 | error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure."); |
| 339 | strcpy (so->so_name, scratch_pathname); |
| 340 | |
| 341 | if (!bfd_check_format (abfd, bfd_object)) |
| 342 | { |
| 343 | error ("\"%s\": not in executable format: %s.", |
| 344 | scratch_pathname, bfd_errmsg (bfd_get_error ())); |
| 345 | } |
| 346 | if (build_section_table (abfd, &so->sections, &so->sections_end)) |
| 347 | { |
| 348 | error ("Can't find the file sections in `%s': %s", |
| 349 | bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ())); |
| 350 | } |
| 351 | |
| 352 | for (p = so->sections; p < so->sections_end; p++) |
| 353 | { |
| 354 | /* Relocate the section binding addresses as recorded in the shared |
| 355 | object's file by the base address to which the object was actually |
| 356 | mapped. */ |
| 357 | p->addr += LM_ADDR (so); |
| 358 | p->endaddr += LM_ADDR (so); |
| 359 | so->lmend = max (p->endaddr, so->lmend); |
| 360 | if (STREQ (p->the_bfd_section->name, ".text")) |
| 361 | { |
| 362 | so->textsection = p; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /* Free the file names, close the file now. */ |
| 367 | do_cleanups (old_chain); |
| 368 | |
| 369 | return (1); |
| 370 | } |
| 371 | |
| 372 | #ifndef SVR4_SHARED_LIBS |
| 373 | |
| 374 | /* Allocate the runtime common object file. */ |
| 375 | |
| 376 | static void |
| 377 | allocate_rt_common_objfile () |
| 378 | { |
| 379 | struct objfile *objfile; |
| 380 | struct objfile *last_one; |
| 381 | |
| 382 | objfile = (struct objfile *) xmalloc (sizeof (struct objfile)); |
| 383 | memset (objfile, 0, sizeof (struct objfile)); |
| 384 | objfile->md = NULL; |
| 385 | obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0, |
| 386 | xmalloc, free); |
| 387 | obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc, |
| 388 | free); |
| 389 | obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc, |
| 390 | free); |
| 391 | obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc, |
| 392 | free); |
| 393 | objfile->name = mstrsave (objfile->md, "rt_common"); |
| 394 | |
| 395 | /* Add this file onto the tail of the linked list of other such files. */ |
| 396 | |
| 397 | objfile->next = NULL; |
| 398 | if (object_files == NULL) |
| 399 | object_files = objfile; |
| 400 | else |
| 401 | { |
| 402 | for (last_one = object_files; |
| 403 | last_one->next; |
| 404 | last_one = last_one->next); |
| 405 | last_one->next = objfile; |
| 406 | } |
| 407 | |
| 408 | rt_common_objfile = objfile; |
| 409 | } |
| 410 | |
| 411 | /* Read all dynamically loaded common symbol definitions from the inferior |
| 412 | and put them into the minimal symbol table for the runtime common |
| 413 | objfile. */ |
| 414 | |
| 415 | static void |
| 416 | solib_add_common_symbols (rtc_symp) |
| 417 | CORE_ADDR rtc_symp; |
| 418 | { |
| 419 | struct rtc_symb inferior_rtc_symb; |
| 420 | struct nlist inferior_rtc_nlist; |
| 421 | int len; |
| 422 | char *name; |
| 423 | |
| 424 | /* Remove any runtime common symbols from previous runs. */ |
| 425 | |
| 426 | if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count) |
| 427 | { |
| 428 | obstack_free (&rt_common_objfile->symbol_obstack, 0); |
| 429 | obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0, |
| 430 | xmalloc, free); |
| 431 | rt_common_objfile->minimal_symbol_count = 0; |
| 432 | rt_common_objfile->msymbols = NULL; |
| 433 | } |
| 434 | |
| 435 | init_minimal_symbol_collection (); |
| 436 | make_cleanup_discard_minimal_symbols (); |
| 437 | |
| 438 | while (rtc_symp) |
| 439 | { |
| 440 | read_memory (rtc_symp, |
| 441 | (char *) &inferior_rtc_symb, |
| 442 | sizeof (inferior_rtc_symb)); |
| 443 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp), |
| 444 | (char *) &inferior_rtc_nlist, |
| 445 | sizeof (inferior_rtc_nlist)); |
| 446 | if (inferior_rtc_nlist.n_type == N_COMM) |
| 447 | { |
| 448 | /* FIXME: The length of the symbol name is not available, but in the |
| 449 | current implementation the common symbol is allocated immediately |
| 450 | behind the name of the symbol. */ |
| 451 | len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx; |
| 452 | |
| 453 | name = xmalloc (len); |
| 454 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name), |
| 455 | name, len); |
| 456 | |
| 457 | /* Allocate the runtime common objfile if necessary. */ |
| 458 | if (rt_common_objfile == NULL) |
| 459 | allocate_rt_common_objfile (); |
| 460 | |
| 461 | prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value, |
| 462 | mst_bss, rt_common_objfile); |
| 463 | free (name); |
| 464 | } |
| 465 | rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next); |
| 466 | } |
| 467 | |
| 468 | /* Install any minimal symbols that have been collected as the current |
| 469 | minimal symbols for the runtime common objfile. */ |
| 470 | |
| 471 | install_minimal_symbols (rt_common_objfile); |
| 472 | } |
| 473 | |
| 474 | #endif /* SVR4_SHARED_LIBS */ |
| 475 | |
| 476 | |
| 477 | #ifdef SVR4_SHARED_LIBS |
| 478 | |
| 479 | static CORE_ADDR bfd_lookup_symbol (bfd *, char *); |
| 480 | |
| 481 | /* |
| 482 | |
| 483 | LOCAL FUNCTION |
| 484 | |
| 485 | bfd_lookup_symbol -- lookup the value for a specific symbol |
| 486 | |
| 487 | SYNOPSIS |
| 488 | |
| 489 | CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname) |
| 490 | |
| 491 | DESCRIPTION |
| 492 | |
| 493 | An expensive way to lookup the value of a single symbol for |
| 494 | bfd's that are only temporary anyway. This is used by the |
| 495 | shared library support to find the address of the debugger |
| 496 | interface structures in the shared library. |
| 497 | |
| 498 | Note that 0 is specifically allowed as an error return (no |
| 499 | such symbol). |
| 500 | */ |
| 501 | |
| 502 | static CORE_ADDR |
| 503 | bfd_lookup_symbol (abfd, symname) |
| 504 | bfd *abfd; |
| 505 | char *symname; |
| 506 | { |
| 507 | unsigned int storage_needed; |
| 508 | asymbol *sym; |
| 509 | asymbol **symbol_table; |
| 510 | unsigned int number_of_symbols; |
| 511 | unsigned int i; |
| 512 | struct cleanup *back_to; |
| 513 | CORE_ADDR symaddr = 0; |
| 514 | |
| 515 | storage_needed = bfd_get_symtab_upper_bound (abfd); |
| 516 | |
| 517 | if (storage_needed > 0) |
| 518 | { |
| 519 | symbol_table = (asymbol **) xmalloc (storage_needed); |
| 520 | back_to = make_cleanup (free, (PTR) symbol_table); |
| 521 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); |
| 522 | |
| 523 | for (i = 0; i < number_of_symbols; i++) |
| 524 | { |
| 525 | sym = *symbol_table++; |
| 526 | if (STREQ (sym->name, symname)) |
| 527 | { |
| 528 | /* Bfd symbols are section relative. */ |
| 529 | symaddr = sym->value + sym->section->vma; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | do_cleanups (back_to); |
| 534 | } |
| 535 | return (symaddr); |
| 536 | } |
| 537 | |
| 538 | #ifdef HANDLE_SVR4_EXEC_EMULATORS |
| 539 | |
| 540 | /* |
| 541 | Solaris BCP (the part of Solaris which allows it to run SunOS4 |
| 542 | a.out files) throws in another wrinkle. Solaris does not fill |
| 543 | in the usual a.out link map structures when running BCP programs, |
| 544 | the only way to get at them is via groping around in the dynamic |
| 545 | linker. |
| 546 | The dynamic linker and it's structures are located in the shared |
| 547 | C library, which gets run as the executable's "interpreter" by |
| 548 | the kernel. |
| 549 | |
| 550 | Note that we can assume nothing about the process state at the time |
| 551 | we need to find these structures. We may be stopped on the first |
| 552 | instruction of the interpreter (C shared library), the first |
| 553 | instruction of the executable itself, or somewhere else entirely |
| 554 | (if we attached to the process for example). |
| 555 | */ |
| 556 | |
| 557 | static char *debug_base_symbols[] = |
| 558 | { |
| 559 | "r_debug", /* Solaris 2.3 */ |
| 560 | "_r_debug", /* Solaris 2.1, 2.2 */ |
| 561 | NULL |
| 562 | }; |
| 563 | |
| 564 | static int look_for_base (int, CORE_ADDR); |
| 565 | |
| 566 | /* |
| 567 | |
| 568 | LOCAL FUNCTION |
| 569 | |
| 570 | look_for_base -- examine file for each mapped address segment |
| 571 | |
| 572 | SYNOPSYS |
| 573 | |
| 574 | static int look_for_base (int fd, CORE_ADDR baseaddr) |
| 575 | |
| 576 | DESCRIPTION |
| 577 | |
| 578 | This function is passed to proc_iterate_over_mappings, which |
| 579 | causes it to get called once for each mapped address space, with |
| 580 | an open file descriptor for the file mapped to that space, and the |
| 581 | base address of that mapped space. |
| 582 | |
| 583 | Our job is to find the debug base symbol in the file that this |
| 584 | fd is open on, if it exists, and if so, initialize the dynamic |
| 585 | linker structure base address debug_base. |
| 586 | |
| 587 | Note that this is a computationally expensive proposition, since |
| 588 | we basically have to open a bfd on every call, so we specifically |
| 589 | avoid opening the exec file. |
| 590 | */ |
| 591 | |
| 592 | static int |
| 593 | look_for_base (fd, baseaddr) |
| 594 | int fd; |
| 595 | CORE_ADDR baseaddr; |
| 596 | { |
| 597 | bfd *interp_bfd; |
| 598 | CORE_ADDR address = 0; |
| 599 | char **symbolp; |
| 600 | |
| 601 | /* If the fd is -1, then there is no file that corresponds to this |
| 602 | mapped memory segment, so skip it. Also, if the fd corresponds |
| 603 | to the exec file, skip it as well. */ |
| 604 | |
| 605 | if (fd == -1 |
| 606 | || (exec_bfd != NULL |
| 607 | && fdmatch (fileno ((FILE *) (exec_bfd->iostream)), fd))) |
| 608 | { |
| 609 | return (0); |
| 610 | } |
| 611 | |
| 612 | /* Try to open whatever random file this fd corresponds to. Note that |
| 613 | we have no way currently to find the filename. Don't gripe about |
| 614 | any problems we might have, just fail. */ |
| 615 | |
| 616 | if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL) |
| 617 | { |
| 618 | return (0); |
| 619 | } |
| 620 | if (!bfd_check_format (interp_bfd, bfd_object)) |
| 621 | { |
| 622 | /* FIXME-leak: on failure, might not free all memory associated with |
| 623 | interp_bfd. */ |
| 624 | bfd_close (interp_bfd); |
| 625 | return (0); |
| 626 | } |
| 627 | |
| 628 | /* Now try to find our debug base symbol in this file, which we at |
| 629 | least know to be a valid ELF executable or shared library. */ |
| 630 | |
| 631 | for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++) |
| 632 | { |
| 633 | address = bfd_lookup_symbol (interp_bfd, *symbolp); |
| 634 | if (address != 0) |
| 635 | { |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | if (address == 0) |
| 640 | { |
| 641 | /* FIXME-leak: on failure, might not free all memory associated with |
| 642 | interp_bfd. */ |
| 643 | bfd_close (interp_bfd); |
| 644 | return (0); |
| 645 | } |
| 646 | |
| 647 | /* Eureka! We found the symbol. But now we may need to relocate it |
| 648 | by the base address. If the symbol's value is less than the base |
| 649 | address of the shared library, then it hasn't yet been relocated |
| 650 | by the dynamic linker, and we have to do it ourself. FIXME: Note |
| 651 | that we make the assumption that the first segment that corresponds |
| 652 | to the shared library has the base address to which the library |
| 653 | was relocated. */ |
| 654 | |
| 655 | if (address < baseaddr) |
| 656 | { |
| 657 | address += baseaddr; |
| 658 | } |
| 659 | debug_base = address; |
| 660 | /* FIXME-leak: on failure, might not free all memory associated with |
| 661 | interp_bfd. */ |
| 662 | bfd_close (interp_bfd); |
| 663 | return (1); |
| 664 | } |
| 665 | #endif /* HANDLE_SVR4_EXEC_EMULATORS */ |
| 666 | |
| 667 | /* |
| 668 | |
| 669 | LOCAL FUNCTION |
| 670 | |
| 671 | elf_locate_base -- locate the base address of dynamic linker structs |
| 672 | for SVR4 elf targets. |
| 673 | |
| 674 | SYNOPSIS |
| 675 | |
| 676 | CORE_ADDR elf_locate_base (void) |
| 677 | |
| 678 | DESCRIPTION |
| 679 | |
| 680 | For SVR4 elf targets the address of the dynamic linker's runtime |
| 681 | structure is contained within the dynamic info section in the |
| 682 | executable file. The dynamic section is also mapped into the |
| 683 | inferior address space. Because the runtime loader fills in the |
| 684 | real address before starting the inferior, we have to read in the |
| 685 | dynamic info section from the inferior address space. |
| 686 | If there are any errors while trying to find the address, we |
| 687 | silently return 0, otherwise the found address is returned. |
| 688 | |
| 689 | */ |
| 690 | |
| 691 | static CORE_ADDR |
| 692 | elf_locate_base () |
| 693 | { |
| 694 | sec_ptr dyninfo_sect; |
| 695 | int dyninfo_sect_size; |
| 696 | CORE_ADDR dyninfo_addr; |
| 697 | char *buf; |
| 698 | char *bufend; |
| 699 | int arch_size; |
| 700 | |
| 701 | /* Find the start address of the .dynamic section. */ |
| 702 | dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic"); |
| 703 | if (dyninfo_sect == NULL) |
| 704 | return 0; |
| 705 | dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect); |
| 706 | |
| 707 | /* Read in .dynamic section, silently ignore errors. */ |
| 708 | dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect); |
| 709 | buf = alloca (dyninfo_sect_size); |
| 710 | if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size)) |
| 711 | return 0; |
| 712 | |
| 713 | /* Find the DT_DEBUG entry in the the .dynamic section. |
| 714 | For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has |
| 715 | no DT_DEBUG entries. */ |
| 716 | |
| 717 | arch_size = bfd_elf_get_arch_size (exec_bfd); |
| 718 | if (arch_size == -1) /* failure */ |
| 719 | return 0; |
| 720 | |
| 721 | if (arch_size == 32) |
| 722 | { /* 32-bit elf */ |
| 723 | for (bufend = buf + dyninfo_sect_size; |
| 724 | buf < bufend; |
| 725 | buf += sizeof (Elf32_External_Dyn)) |
| 726 | { |
| 727 | Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *) buf; |
| 728 | long dyn_tag; |
| 729 | CORE_ADDR dyn_ptr; |
| 730 | |
| 731 | dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag); |
| 732 | if (dyn_tag == DT_NULL) |
| 733 | break; |
| 734 | else if (dyn_tag == DT_DEBUG) |
| 735 | { |
| 736 | dyn_ptr = bfd_h_get_32 (exec_bfd, |
| 737 | (bfd_byte *) x_dynp->d_un.d_ptr); |
| 738 | return dyn_ptr; |
| 739 | } |
| 740 | #ifdef DT_MIPS_RLD_MAP |
| 741 | else if (dyn_tag == DT_MIPS_RLD_MAP) |
| 742 | { |
| 743 | char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT]; |
| 744 | |
| 745 | /* DT_MIPS_RLD_MAP contains a pointer to the address |
| 746 | of the dynamic link structure. */ |
| 747 | dyn_ptr = bfd_h_get_32 (exec_bfd, |
| 748 | (bfd_byte *) x_dynp->d_un.d_ptr); |
| 749 | if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf))) |
| 750 | return 0; |
| 751 | return extract_unsigned_integer (pbuf, sizeof (pbuf)); |
| 752 | } |
| 753 | #endif |
| 754 | } |
| 755 | } |
| 756 | else /* 64-bit elf */ |
| 757 | { |
| 758 | for (bufend = buf + dyninfo_sect_size; |
| 759 | buf < bufend; |
| 760 | buf += sizeof (Elf64_External_Dyn)) |
| 761 | { |
| 762 | Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *) buf; |
| 763 | long dyn_tag; |
| 764 | CORE_ADDR dyn_ptr; |
| 765 | |
| 766 | dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag); |
| 767 | if (dyn_tag == DT_NULL) |
| 768 | break; |
| 769 | else if (dyn_tag == DT_DEBUG) |
| 770 | { |
| 771 | dyn_ptr = bfd_h_get_64 (exec_bfd, |
| 772 | (bfd_byte *) x_dynp->d_un.d_ptr); |
| 773 | return dyn_ptr; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* DT_DEBUG entry not found. */ |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | #endif /* SVR4_SHARED_LIBS */ |
| 783 | |
| 784 | /* |
| 785 | |
| 786 | LOCAL FUNCTION |
| 787 | |
| 788 | locate_base -- locate the base address of dynamic linker structs |
| 789 | |
| 790 | SYNOPSIS |
| 791 | |
| 792 | CORE_ADDR locate_base (void) |
| 793 | |
| 794 | DESCRIPTION |
| 795 | |
| 796 | For both the SunOS and SVR4 shared library implementations, if the |
| 797 | inferior executable has been linked dynamically, there is a single |
| 798 | address somewhere in the inferior's data space which is the key to |
| 799 | locating all of the dynamic linker's runtime structures. This |
| 800 | address is the value of the debug base symbol. The job of this |
| 801 | function is to find and return that address, or to return 0 if there |
| 802 | is no such address (the executable is statically linked for example). |
| 803 | |
| 804 | For SunOS, the job is almost trivial, since the dynamic linker and |
| 805 | all of it's structures are statically linked to the executable at |
| 806 | link time. Thus the symbol for the address we are looking for has |
| 807 | already been added to the minimal symbol table for the executable's |
| 808 | objfile at the time the symbol file's symbols were read, and all we |
| 809 | have to do is look it up there. Note that we explicitly do NOT want |
| 810 | to find the copies in the shared library. |
| 811 | |
| 812 | The SVR4 version is a bit more complicated because the address |
| 813 | is contained somewhere in the dynamic info section. We have to go |
| 814 | to a lot more work to discover the address of the debug base symbol. |
| 815 | Because of this complexity, we cache the value we find and return that |
| 816 | value on subsequent invocations. Note there is no copy in the |
| 817 | executable symbol tables. |
| 818 | |
| 819 | */ |
| 820 | |
| 821 | static CORE_ADDR |
| 822 | locate_base () |
| 823 | { |
| 824 | |
| 825 | #ifndef SVR4_SHARED_LIBS |
| 826 | |
| 827 | struct minimal_symbol *msymbol; |
| 828 | CORE_ADDR address = 0; |
| 829 | char **symbolp; |
| 830 | |
| 831 | /* For SunOS, we want to limit the search for the debug base symbol to the |
| 832 | executable being debugged, since there is a duplicate named symbol in the |
| 833 | shared library. We don't want the shared library versions. */ |
| 834 | |
| 835 | for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++) |
| 836 | { |
| 837 | msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile); |
| 838 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) |
| 839 | { |
| 840 | address = SYMBOL_VALUE_ADDRESS (msymbol); |
| 841 | return (address); |
| 842 | } |
| 843 | } |
| 844 | return (0); |
| 845 | |
| 846 | #else /* SVR4_SHARED_LIBS */ |
| 847 | |
| 848 | /* Check to see if we have a currently valid address, and if so, avoid |
| 849 | doing all this work again and just return the cached address. If |
| 850 | we have no cached address, try to locate it in the dynamic info |
| 851 | section for ELF executables. */ |
| 852 | |
| 853 | if (debug_base == 0) |
| 854 | { |
| 855 | if (exec_bfd != NULL |
| 856 | && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour) |
| 857 | debug_base = elf_locate_base (); |
| 858 | #ifdef HANDLE_SVR4_EXEC_EMULATORS |
| 859 | /* Try it the hard way for emulated executables. */ |
| 860 | else if (inferior_pid != 0 && target_has_execution) |
| 861 | proc_iterate_over_mappings (look_for_base); |
| 862 | #endif |
| 863 | } |
| 864 | return (debug_base); |
| 865 | |
| 866 | #endif /* !SVR4_SHARED_LIBS */ |
| 867 | |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | |
| 872 | LOCAL FUNCTION |
| 873 | |
| 874 | first_link_map_member -- locate first member in dynamic linker's map |
| 875 | |
| 876 | SYNOPSIS |
| 877 | |
| 878 | static CORE_ADDR first_link_map_member (void) |
| 879 | |
| 880 | DESCRIPTION |
| 881 | |
| 882 | Find the first element in the inferior's dynamic link map, and |
| 883 | return its address in the inferior. This function doesn't copy the |
| 884 | link map entry itself into our address space; current_sos actually |
| 885 | does the reading. */ |
| 886 | |
| 887 | static CORE_ADDR |
| 888 | first_link_map_member () |
| 889 | { |
| 890 | CORE_ADDR lm = 0; |
| 891 | |
| 892 | #ifndef SVR4_SHARED_LIBS |
| 893 | |
| 894 | read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy)); |
| 895 | if (dynamic_copy.ld_version >= 2) |
| 896 | { |
| 897 | /* It is a version that we can deal with, so read in the secondary |
| 898 | structure and find the address of the link map list from it. */ |
| 899 | read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2), |
| 900 | (char *) &ld_2_copy, sizeof (struct link_dynamic_2)); |
| 901 | lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded); |
| 902 | } |
| 903 | |
| 904 | #else /* SVR4_SHARED_LIBS */ |
| 905 | |
| 906 | read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug)); |
| 907 | /* FIXME: Perhaps we should validate the info somehow, perhaps by |
| 908 | checking r_version for a known version number, or r_state for |
| 909 | RT_CONSISTENT. */ |
| 910 | lm = SOLIB_EXTRACT_ADDRESS (debug_copy.r_map); |
| 911 | |
| 912 | #endif /* !SVR4_SHARED_LIBS */ |
| 913 | |
| 914 | return (lm); |
| 915 | } |
| 916 | |
| 917 | #ifdef SVR4_SHARED_LIBS |
| 918 | /* |
| 919 | |
| 920 | LOCAL FUNCTION |
| 921 | |
| 922 | open_symbol_file_object |
| 923 | |
| 924 | SYNOPSIS |
| 925 | |
| 926 | void open_symbol_file_object (int from_tty) |
| 927 | |
| 928 | DESCRIPTION |
| 929 | |
| 930 | If no open symbol file, attempt to locate and open the main symbol |
| 931 | file. On SVR4 systems, this is the first link map entry. If its |
| 932 | name is here, we can open it. Useful when attaching to a process |
| 933 | without first loading its symbol file. |
| 934 | |
| 935 | */ |
| 936 | |
| 937 | static int |
| 938 | open_symbol_file_object (from_ttyp) |
| 939 | int *from_ttyp; /* sneak past catch_errors */ |
| 940 | { |
| 941 | CORE_ADDR lm; |
| 942 | struct link_map lmcopy; |
| 943 | char *filename; |
| 944 | int errcode; |
| 945 | |
| 946 | if (symfile_objfile) |
| 947 | if (!query ("Attempt to reload symbols from process? ")) |
| 948 | return 0; |
| 949 | |
| 950 | if ((debug_base = locate_base ()) == 0) |
| 951 | return 0; /* failed somehow... */ |
| 952 | |
| 953 | /* First link map member should be the executable. */ |
| 954 | if ((lm = first_link_map_member ()) == 0) |
| 955 | return 0; /* failed somehow... */ |
| 956 | |
| 957 | /* Read from target memory to GDB. */ |
| 958 | read_memory (lm, (void *) &lmcopy, sizeof (lmcopy)); |
| 959 | |
| 960 | if (lmcopy.l_name == 0) |
| 961 | return 0; /* no filename. */ |
| 962 | |
| 963 | /* Now fetch the filename from target memory. */ |
| 964 | target_read_string (SOLIB_EXTRACT_ADDRESS (lmcopy.l_name), &filename, |
| 965 | MAX_PATH_SIZE - 1, &errcode); |
| 966 | if (errcode) |
| 967 | { |
| 968 | warning ("failed to read exec filename from attached file: %s", |
| 969 | safe_strerror (errcode)); |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | make_cleanup (free, filename); |
| 974 | /* Have a pathname: read the symbol file. */ |
| 975 | symbol_file_command (filename, *from_ttyp); |
| 976 | |
| 977 | return 1; |
| 978 | } |
| 979 | #endif /* SVR4_SHARED_LIBS */ |
| 980 | |
| 981 | |
| 982 | /* LOCAL FUNCTION |
| 983 | |
| 984 | free_so --- free a `struct so_list' object |
| 985 | |
| 986 | SYNOPSIS |
| 987 | |
| 988 | void free_so (struct so_list *so) |
| 989 | |
| 990 | DESCRIPTION |
| 991 | |
| 992 | Free the storage associated with the `struct so_list' object SO. |
| 993 | If we have opened a BFD for SO, close it. |
| 994 | |
| 995 | The caller is responsible for removing SO from whatever list it is |
| 996 | a member of. If we have placed SO's sections in some target's |
| 997 | section table, the caller is responsible for removing them. |
| 998 | |
| 999 | This function doesn't mess with objfiles at all. If there is an |
| 1000 | objfile associated with SO that needs to be removed, the caller is |
| 1001 | responsible for taking care of that. */ |
| 1002 | |
| 1003 | static void |
| 1004 | free_so (struct so_list *so) |
| 1005 | { |
| 1006 | char *bfd_filename = 0; |
| 1007 | |
| 1008 | if (so->sections) |
| 1009 | free (so->sections); |
| 1010 | |
| 1011 | if (so->abfd) |
| 1012 | { |
| 1013 | bfd_filename = bfd_get_filename (so->abfd); |
| 1014 | if (! bfd_close (so->abfd)) |
| 1015 | warning ("cannot close \"%s\": %s", |
| 1016 | bfd_filename, bfd_errmsg (bfd_get_error ())); |
| 1017 | } |
| 1018 | |
| 1019 | if (bfd_filename) |
| 1020 | free (bfd_filename); |
| 1021 | |
| 1022 | free (so); |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | /* On some systems, the only way to recognize the link map entry for |
| 1027 | the main executable file is by looking at its name. Return |
| 1028 | non-zero iff SONAME matches one of the known main executable names. */ |
| 1029 | |
| 1030 | static int |
| 1031 | match_main (soname) |
| 1032 | char *soname; |
| 1033 | { |
| 1034 | char **mainp; |
| 1035 | |
| 1036 | for (mainp = main_name_list; *mainp != NULL; mainp++) |
| 1037 | { |
| 1038 | if (strcmp (soname, *mainp) == 0) |
| 1039 | return (1); |
| 1040 | } |
| 1041 | |
| 1042 | return (0); |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | /* LOCAL FUNCTION |
| 1047 | |
| 1048 | current_sos -- build a list of currently loaded shared objects |
| 1049 | |
| 1050 | SYNOPSIS |
| 1051 | |
| 1052 | struct so_list *current_sos () |
| 1053 | |
| 1054 | DESCRIPTION |
| 1055 | |
| 1056 | Build a list of `struct so_list' objects describing the shared |
| 1057 | objects currently loaded in the inferior. This list does not |
| 1058 | include an entry for the main executable file. |
| 1059 | |
| 1060 | Note that we only gather information directly available from the |
| 1061 | inferior --- we don't examine any of the shared library files |
| 1062 | themselves. The declaration of `struct so_list' says which fields |
| 1063 | we provide values for. */ |
| 1064 | |
| 1065 | static struct so_list * |
| 1066 | current_sos () |
| 1067 | { |
| 1068 | CORE_ADDR lm; |
| 1069 | struct so_list *head = 0; |
| 1070 | struct so_list **link_ptr = &head; |
| 1071 | |
| 1072 | /* Make sure we've looked up the inferior's dynamic linker's base |
| 1073 | structure. */ |
| 1074 | if (! debug_base) |
| 1075 | { |
| 1076 | debug_base = locate_base (); |
| 1077 | |
| 1078 | /* If we can't find the dynamic linker's base structure, this |
| 1079 | must not be a dynamically linked executable. Hmm. */ |
| 1080 | if (! debug_base) |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | /* Walk the inferior's link map list, and build our list of |
| 1085 | `struct so_list' nodes. */ |
| 1086 | lm = first_link_map_member (); |
| 1087 | while (lm) |
| 1088 | { |
| 1089 | struct so_list *new |
| 1090 | = (struct so_list *) xmalloc (sizeof (struct so_list)); |
| 1091 | struct cleanup *old_chain = make_cleanup (free, new); |
| 1092 | memset (new, 0, sizeof (*new)); |
| 1093 | |
| 1094 | new->lmaddr = lm; |
| 1095 | read_memory (lm, (char *) &(new->lm), sizeof (struct link_map)); |
| 1096 | |
| 1097 | lm = LM_NEXT (new); |
| 1098 | |
| 1099 | /* For SVR4 versions, the first entry in the link map is for the |
| 1100 | inferior executable, so we must ignore it. For some versions of |
| 1101 | SVR4, it has no name. For others (Solaris 2.3 for example), it |
| 1102 | does have a name, so we can no longer use a missing name to |
| 1103 | decide when to ignore it. */ |
| 1104 | if (IGNORE_FIRST_LINK_MAP_ENTRY (new)) |
| 1105 | free_so (new); |
| 1106 | else |
| 1107 | { |
| 1108 | int errcode; |
| 1109 | char *buffer; |
| 1110 | |
| 1111 | /* Extract this shared object's name. */ |
| 1112 | target_read_string (LM_NAME (new), &buffer, |
| 1113 | MAX_PATH_SIZE - 1, &errcode); |
| 1114 | if (errcode != 0) |
| 1115 | { |
| 1116 | warning ("current_sos: Can't read pathname for load map: %s\n", |
| 1117 | safe_strerror (errcode)); |
| 1118 | } |
| 1119 | else |
| 1120 | { |
| 1121 | strncpy (new->so_name, buffer, MAX_PATH_SIZE - 1); |
| 1122 | new->so_name[MAX_PATH_SIZE - 1] = '\0'; |
| 1123 | free (buffer); |
| 1124 | strcpy (new->so_original_name, new->so_name); |
| 1125 | } |
| 1126 | |
| 1127 | /* If this entry has no name, or its name matches the name |
| 1128 | for the main executable, don't include it in the list. */ |
| 1129 | if (! new->so_name[0] |
| 1130 | || match_main (new->so_name)) |
| 1131 | free_so (new); |
| 1132 | else |
| 1133 | { |
| 1134 | new->next = 0; |
| 1135 | *link_ptr = new; |
| 1136 | link_ptr = &new->next; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | discard_cleanups (old_chain); |
| 1141 | } |
| 1142 | |
| 1143 | return head; |
| 1144 | } |
| 1145 | |
| 1146 | |
| 1147 | /* A small stub to get us past the arg-passing pinhole of catch_errors. */ |
| 1148 | |
| 1149 | static int |
| 1150 | symbol_add_stub (arg) |
| 1151 | PTR arg; |
| 1152 | { |
| 1153 | register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */ |
| 1154 | struct section_addr_info *sap; |
| 1155 | CORE_ADDR lowest_addr = 0; |
| 1156 | int lowest_index; |
| 1157 | asection *lowest_sect = NULL; |
| 1158 | |
| 1159 | /* Have we already loaded this shared object? */ |
| 1160 | ALL_OBJFILES (so->objfile) |
| 1161 | { |
| 1162 | if (strcmp (so->objfile->name, so->so_name) == 0) |
| 1163 | return 1; |
| 1164 | } |
| 1165 | |
| 1166 | /* Find the shared object's text segment. */ |
| 1167 | if (so->textsection) |
| 1168 | { |
| 1169 | lowest_addr = so->textsection->addr; |
| 1170 | lowest_sect = bfd_get_section_by_name (so->abfd, ".text"); |
| 1171 | lowest_index = lowest_sect->index; |
| 1172 | } |
| 1173 | else if (so->abfd != NULL) |
| 1174 | { |
| 1175 | /* If we didn't find a mapped non zero sized .text section, set |
| 1176 | up lowest_addr so that the relocation in symbol_file_add does |
| 1177 | no harm. */ |
| 1178 | lowest_sect = bfd_get_section_by_name (so->abfd, ".text"); |
| 1179 | if (lowest_sect == NULL) |
| 1180 | bfd_map_over_sections (so->abfd, find_lowest_section, |
| 1181 | (PTR) &lowest_sect); |
| 1182 | if (lowest_sect) |
| 1183 | { |
| 1184 | lowest_addr = bfd_section_vma (so->abfd, lowest_sect) |
| 1185 | + LM_ADDR (so); |
| 1186 | lowest_index = lowest_sect->index; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | sap = build_section_addr_info_from_section_table (so->sections, |
| 1191 | so->sections_end); |
| 1192 | |
| 1193 | sap->other[lowest_index].addr = lowest_addr; |
| 1194 | |
| 1195 | so->objfile = symbol_file_add (so->so_name, so->from_tty, |
| 1196 | sap, 0, OBJF_SHARED); |
| 1197 | free_section_addr_info (sap); |
| 1198 | |
| 1199 | return (1); |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | /* LOCAL FUNCTION |
| 1204 | |
| 1205 | update_solib_list --- synchronize GDB's shared object list with inferior's |
| 1206 | |
| 1207 | SYNOPSIS |
| 1208 | |
| 1209 | void update_solib_list (int from_tty, struct target_ops *TARGET) |
| 1210 | |
| 1211 | Extract the list of currently loaded shared objects from the |
| 1212 | inferior, and compare it with the list of shared objects currently |
| 1213 | in GDB's so_list_head list. Edit so_list_head to bring it in sync |
| 1214 | with the inferior's new list. |
| 1215 | |
| 1216 | If we notice that the inferior has unloaded some shared objects, |
| 1217 | free any symbolic info GDB had read about those shared objects. |
| 1218 | |
| 1219 | Don't load symbolic info for any new shared objects; just add them |
| 1220 | to the list, and leave their symbols_loaded flag clear. |
| 1221 | |
| 1222 | If FROM_TTY is non-null, feel free to print messages about what |
| 1223 | we're doing. |
| 1224 | |
| 1225 | If TARGET is non-null, add the sections of all new shared objects |
| 1226 | to TARGET's section table. Note that this doesn't remove any |
| 1227 | sections for shared objects that have been unloaded, and it |
| 1228 | doesn't check to see if the new shared objects are already present in |
| 1229 | the section table. But we only use this for core files and |
| 1230 | processes we've just attached to, so that's okay. */ |
| 1231 | |
| 1232 | void |
| 1233 | update_solib_list (int from_tty, struct target_ops *target) |
| 1234 | { |
| 1235 | struct so_list *inferior = current_sos (); |
| 1236 | struct so_list *gdb, **gdb_link; |
| 1237 | |
| 1238 | #ifdef SVR4_SHARED_LIBS |
| 1239 | /* If we are attaching to a running process for which we |
| 1240 | have not opened a symbol file, we may be able to get its |
| 1241 | symbols now! */ |
| 1242 | if (attach_flag && |
| 1243 | symfile_objfile == NULL) |
| 1244 | catch_errors (open_symbol_file_object, (PTR) &from_tty, |
| 1245 | "Error reading attached process's symbol file.\n", |
| 1246 | RETURN_MASK_ALL); |
| 1247 | |
| 1248 | #endif SVR4_SHARED_LIBS |
| 1249 | |
| 1250 | /* Since this function might actually add some elements to the |
| 1251 | so_list_head list, arrange for it to be cleaned up when |
| 1252 | appropriate. */ |
| 1253 | if (!solib_cleanup_queued) |
| 1254 | { |
| 1255 | make_run_cleanup (do_clear_solib, NULL); |
| 1256 | solib_cleanup_queued = 1; |
| 1257 | } |
| 1258 | |
| 1259 | /* GDB and the inferior's dynamic linker each maintain their own |
| 1260 | list of currently loaded shared objects; we want to bring the |
| 1261 | former in sync with the latter. Scan both lists, seeing which |
| 1262 | shared objects appear where. There are three cases: |
| 1263 | |
| 1264 | - A shared object appears on both lists. This means that GDB |
| 1265 | knows about it already, and it's still loaded in the inferior. |
| 1266 | Nothing needs to happen. |
| 1267 | |
| 1268 | - A shared object appears only on GDB's list. This means that |
| 1269 | the inferior has unloaded it. We should remove the shared |
| 1270 | object from GDB's tables. |
| 1271 | |
| 1272 | - A shared object appears only on the inferior's list. This |
| 1273 | means that it's just been loaded. We should add it to GDB's |
| 1274 | tables. |
| 1275 | |
| 1276 | So we walk GDB's list, checking each entry to see if it appears |
| 1277 | in the inferior's list too. If it does, no action is needed, and |
| 1278 | we remove it from the inferior's list. If it doesn't, the |
| 1279 | inferior has unloaded it, and we remove it from GDB's list. By |
| 1280 | the time we're done walking GDB's list, the inferior's list |
| 1281 | contains only the new shared objects, which we then add. */ |
| 1282 | |
| 1283 | gdb = so_list_head; |
| 1284 | gdb_link = &so_list_head; |
| 1285 | while (gdb) |
| 1286 | { |
| 1287 | struct so_list *i = inferior; |
| 1288 | struct so_list **i_link = &inferior; |
| 1289 | |
| 1290 | /* Check to see whether the shared object *gdb also appears in |
| 1291 | the inferior's current list. */ |
| 1292 | while (i) |
| 1293 | { |
| 1294 | if (! strcmp (gdb->so_original_name, i->so_original_name)) |
| 1295 | break; |
| 1296 | |
| 1297 | i_link = &i->next; |
| 1298 | i = *i_link; |
| 1299 | } |
| 1300 | |
| 1301 | /* If the shared object appears on the inferior's list too, then |
| 1302 | it's still loaded, so we don't need to do anything. Delete |
| 1303 | it from the inferior's list, and leave it on GDB's list. */ |
| 1304 | if (i) |
| 1305 | { |
| 1306 | *i_link = i->next; |
| 1307 | free_so (i); |
| 1308 | gdb_link = &gdb->next; |
| 1309 | gdb = *gdb_link; |
| 1310 | } |
| 1311 | |
| 1312 | /* If it's not on the inferior's list, remove it from GDB's tables. */ |
| 1313 | else |
| 1314 | { |
| 1315 | *gdb_link = gdb->next; |
| 1316 | |
| 1317 | /* Unless the user loaded it explicitly, free SO's objfile. */ |
| 1318 | if (gdb->objfile && ! (gdb->objfile->flags & OBJF_USERLOADED)) |
| 1319 | free_objfile (gdb->objfile); |
| 1320 | |
| 1321 | /* Some targets' section tables might be referring to |
| 1322 | sections from so->abfd; remove them. */ |
| 1323 | remove_target_sections (gdb->abfd); |
| 1324 | |
| 1325 | free_so (gdb); |
| 1326 | gdb = *gdb_link; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /* Now the inferior's list contains only shared objects that don't |
| 1331 | appear in GDB's list --- those that are newly loaded. Add them |
| 1332 | to GDB's shared object list. */ |
| 1333 | if (inferior) |
| 1334 | { |
| 1335 | struct so_list *i; |
| 1336 | |
| 1337 | /* Add the new shared objects to GDB's list. */ |
| 1338 | *gdb_link = inferior; |
| 1339 | |
| 1340 | /* Fill in the rest of each of the `struct so_list' nodes. */ |
| 1341 | for (i = inferior; i; i = i->next) |
| 1342 | { |
| 1343 | i->from_tty = from_tty; |
| 1344 | |
| 1345 | /* Fill in the rest of the `struct so_list' node. */ |
| 1346 | catch_errors (solib_map_sections, i, |
| 1347 | "Error while mapping shared library sections:\n", |
| 1348 | RETURN_MASK_ALL); |
| 1349 | } |
| 1350 | |
| 1351 | /* If requested, add the shared objects' sections to the the |
| 1352 | TARGET's section table. */ |
| 1353 | if (target) |
| 1354 | { |
| 1355 | int new_sections; |
| 1356 | |
| 1357 | /* Figure out how many sections we'll need to add in total. */ |
| 1358 | new_sections = 0; |
| 1359 | for (i = inferior; i; i = i->next) |
| 1360 | new_sections += (i->sections_end - i->sections); |
| 1361 | |
| 1362 | if (new_sections > 0) |
| 1363 | { |
| 1364 | int space = target_resize_to_sections (target, new_sections); |
| 1365 | |
| 1366 | for (i = inferior; i; i = i->next) |
| 1367 | { |
| 1368 | int count = (i->sections_end - i->sections); |
| 1369 | memcpy (target->to_sections + space, |
| 1370 | i->sections, |
| 1371 | count * sizeof (i->sections[0])); |
| 1372 | space += count; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | /* GLOBAL FUNCTION |
| 1381 | |
| 1382 | solib_add -- read in symbol info for newly added shared libraries |
| 1383 | |
| 1384 | SYNOPSIS |
| 1385 | |
| 1386 | void solib_add (char *pattern, int from_tty, struct target_ops *TARGET) |
| 1387 | |
| 1388 | DESCRIPTION |
| 1389 | |
| 1390 | Read in symbolic information for any shared objects whose names |
| 1391 | match PATTERN. (If we've already read a shared object's symbol |
| 1392 | info, leave it alone.) If PATTERN is zero, read them all. |
| 1393 | |
| 1394 | FROM_TTY and TARGET are as described for update_solib_list, above. */ |
| 1395 | |
| 1396 | void |
| 1397 | solib_add (char *pattern, int from_tty, struct target_ops *target) |
| 1398 | { |
| 1399 | struct so_list *gdb; |
| 1400 | |
| 1401 | if (pattern) |
| 1402 | { |
| 1403 | char *re_err = re_comp (pattern); |
| 1404 | |
| 1405 | if (re_err) |
| 1406 | error ("Invalid regexp: %s", re_err); |
| 1407 | } |
| 1408 | |
| 1409 | update_solib_list (from_tty, target); |
| 1410 | |
| 1411 | /* Walk the list of currently loaded shared libraries, and read |
| 1412 | symbols for any that match the pattern --- or any whose symbols |
| 1413 | aren't already loaded, if no pattern was given. */ |
| 1414 | { |
| 1415 | int any_matches = 0; |
| 1416 | int loaded_any_symbols = 0; |
| 1417 | |
| 1418 | for (gdb = so_list_head; gdb; gdb = gdb->next) |
| 1419 | if (! pattern || re_exec (gdb->so_name)) |
| 1420 | { |
| 1421 | any_matches = 1; |
| 1422 | |
| 1423 | if (gdb->symbols_loaded) |
| 1424 | { |
| 1425 | if (from_tty) |
| 1426 | printf_unfiltered ("Symbols already loaded for %s\n", |
| 1427 | gdb->so_name); |
| 1428 | } |
| 1429 | else |
| 1430 | { |
| 1431 | if (catch_errors |
| 1432 | (symbol_add_stub, gdb, |
| 1433 | "Error while reading shared library symbols:\n", |
| 1434 | RETURN_MASK_ALL)) |
| 1435 | { |
| 1436 | if (from_tty) |
| 1437 | printf_unfiltered ("Loaded symbols for %s\n", |
| 1438 | gdb->so_name); |
| 1439 | gdb->symbols_loaded = 1; |
| 1440 | loaded_any_symbols = 1; |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (from_tty && pattern && ! any_matches) |
| 1446 | printf_unfiltered |
| 1447 | ("No loaded shared libraries match the pattern `%s'.\n", pattern); |
| 1448 | |
| 1449 | if (loaded_any_symbols) |
| 1450 | { |
| 1451 | /* Getting new symbols may change our opinion about what is |
| 1452 | frameless. */ |
| 1453 | reinit_frame_cache (); |
| 1454 | |
| 1455 | special_symbol_handling (); |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | |
| 1461 | /* |
| 1462 | |
| 1463 | LOCAL FUNCTION |
| 1464 | |
| 1465 | info_sharedlibrary_command -- code for "info sharedlibrary" |
| 1466 | |
| 1467 | SYNOPSIS |
| 1468 | |
| 1469 | static void info_sharedlibrary_command () |
| 1470 | |
| 1471 | DESCRIPTION |
| 1472 | |
| 1473 | Walk through the shared library list and print information |
| 1474 | about each attached library. |
| 1475 | */ |
| 1476 | |
| 1477 | static void |
| 1478 | info_sharedlibrary_command (ignore, from_tty) |
| 1479 | char *ignore; |
| 1480 | int from_tty; |
| 1481 | { |
| 1482 | register struct so_list *so = NULL; /* link map state variable */ |
| 1483 | int header_done = 0; |
| 1484 | int addr_width; |
| 1485 | char *addr_fmt; |
| 1486 | int arch_size; |
| 1487 | |
| 1488 | if (exec_bfd == NULL) |
| 1489 | { |
| 1490 | printf_unfiltered ("No executable file.\n"); |
| 1491 | return; |
| 1492 | } |
| 1493 | |
| 1494 | arch_size = bfd_elf_get_arch_size (exec_bfd); |
| 1495 | /* Default to 32-bit in case of failure (non-elf). */ |
| 1496 | if (arch_size == 32 || arch_size == -1) |
| 1497 | { |
| 1498 | addr_width = 8 + 4; |
| 1499 | addr_fmt = "08l"; |
| 1500 | } |
| 1501 | else if (arch_size == 64) |
| 1502 | { |
| 1503 | addr_width = 16 + 4; |
| 1504 | addr_fmt = "016l"; |
| 1505 | } |
| 1506 | |
| 1507 | update_solib_list (from_tty, 0); |
| 1508 | |
| 1509 | for (so = so_list_head; so; so = so->next) |
| 1510 | { |
| 1511 | if (so->so_name[0]) |
| 1512 | { |
| 1513 | if (!header_done) |
| 1514 | { |
| 1515 | printf_unfiltered ("%-*s%-*s%-12s%s\n", addr_width, "From", |
| 1516 | addr_width, "To", "Syms Read", |
| 1517 | "Shared Object Library"); |
| 1518 | header_done++; |
| 1519 | } |
| 1520 | |
| 1521 | printf_unfiltered ("%-*s", addr_width, |
| 1522 | local_hex_string_custom ((unsigned long) LM_ADDR (so), |
| 1523 | addr_fmt)); |
| 1524 | printf_unfiltered ("%-*s", addr_width, |
| 1525 | local_hex_string_custom ((unsigned long) so->lmend, |
| 1526 | addr_fmt)); |
| 1527 | printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No"); |
| 1528 | printf_unfiltered ("%s\n", so->so_name); |
| 1529 | } |
| 1530 | } |
| 1531 | if (so_list_head == NULL) |
| 1532 | { |
| 1533 | printf_unfiltered ("No shared libraries loaded at this time.\n"); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | /* |
| 1538 | |
| 1539 | GLOBAL FUNCTION |
| 1540 | |
| 1541 | solib_address -- check to see if an address is in a shared lib |
| 1542 | |
| 1543 | SYNOPSIS |
| 1544 | |
| 1545 | char * solib_address (CORE_ADDR address) |
| 1546 | |
| 1547 | DESCRIPTION |
| 1548 | |
| 1549 | Provides a hook for other gdb routines to discover whether or |
| 1550 | not a particular address is within the mapped address space of |
| 1551 | a shared library. Any address between the base mapping address |
| 1552 | and the first address beyond the end of the last mapping, is |
| 1553 | considered to be within the shared library address space, for |
| 1554 | our purposes. |
| 1555 | |
| 1556 | For example, this routine is called at one point to disable |
| 1557 | breakpoints which are in shared libraries that are not currently |
| 1558 | mapped in. |
| 1559 | */ |
| 1560 | |
| 1561 | char * |
| 1562 | solib_address (address) |
| 1563 | CORE_ADDR address; |
| 1564 | { |
| 1565 | register struct so_list *so = 0; /* link map state variable */ |
| 1566 | |
| 1567 | for (so = so_list_head; so; so = so->next) |
| 1568 | { |
| 1569 | if (LM_ADDR (so) <= address && address < so->lmend) |
| 1570 | return (so->so_name); |
| 1571 | } |
| 1572 | |
| 1573 | return (0); |
| 1574 | } |
| 1575 | |
| 1576 | /* Called by free_all_symtabs */ |
| 1577 | |
| 1578 | void |
| 1579 | clear_solib () |
| 1580 | { |
| 1581 | /* This function is expected to handle ELF shared libraries. It is |
| 1582 | also used on Solaris, which can run either ELF or a.out binaries |
| 1583 | (for compatibility with SunOS 4), both of which can use shared |
| 1584 | libraries. So we don't know whether we have an ELF executable or |
| 1585 | an a.out executable until the user chooses an executable file. |
| 1586 | |
| 1587 | ELF shared libraries don't get mapped into the address space |
| 1588 | until after the program starts, so we'd better not try to insert |
| 1589 | breakpoints in them immediately. We have to wait until the |
| 1590 | dynamic linker has loaded them; we'll hit a bp_shlib_event |
| 1591 | breakpoint (look for calls to create_solib_event_breakpoint) when |
| 1592 | it's ready. |
| 1593 | |
| 1594 | SunOS shared libraries seem to be different --- they're present |
| 1595 | as soon as the process begins execution, so there's no need to |
| 1596 | put off inserting breakpoints. There's also nowhere to put a |
| 1597 | bp_shlib_event breakpoint, so if we put it off, we'll never get |
| 1598 | around to it. |
| 1599 | |
| 1600 | So: disable breakpoints only if we're using ELF shared libs. */ |
| 1601 | if (exec_bfd != NULL |
| 1602 | && bfd_get_flavour (exec_bfd) != bfd_target_aout_flavour) |
| 1603 | disable_breakpoints_in_shlibs (1); |
| 1604 | |
| 1605 | while (so_list_head) |
| 1606 | { |
| 1607 | struct so_list *so = so_list_head; |
| 1608 | so_list_head = so->next; |
| 1609 | free_so (so); |
| 1610 | } |
| 1611 | |
| 1612 | debug_base = 0; |
| 1613 | } |
| 1614 | |
| 1615 | static void |
| 1616 | do_clear_solib (dummy) |
| 1617 | PTR dummy; |
| 1618 | { |
| 1619 | solib_cleanup_queued = 0; |
| 1620 | clear_solib (); |
| 1621 | } |
| 1622 | |
| 1623 | #ifdef SVR4_SHARED_LIBS |
| 1624 | |
| 1625 | /* Return 1 if PC lies in the dynamic symbol resolution code of the |
| 1626 | SVR4 run time loader. */ |
| 1627 | |
| 1628 | static CORE_ADDR interp_text_sect_low; |
| 1629 | static CORE_ADDR interp_text_sect_high; |
| 1630 | static CORE_ADDR interp_plt_sect_low; |
| 1631 | static CORE_ADDR interp_plt_sect_high; |
| 1632 | |
| 1633 | int |
| 1634 | in_svr4_dynsym_resolve_code (pc) |
| 1635 | CORE_ADDR pc; |
| 1636 | { |
| 1637 | return ((pc >= interp_text_sect_low && pc < interp_text_sect_high) |
| 1638 | || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high) |
| 1639 | || in_plt_section (pc, NULL)); |
| 1640 | } |
| 1641 | #endif |
| 1642 | |
| 1643 | /* |
| 1644 | |
| 1645 | LOCAL FUNCTION |
| 1646 | |
| 1647 | disable_break -- remove the "mapping changed" breakpoint |
| 1648 | |
| 1649 | SYNOPSIS |
| 1650 | |
| 1651 | static int disable_break () |
| 1652 | |
| 1653 | DESCRIPTION |
| 1654 | |
| 1655 | Removes the breakpoint that gets hit when the dynamic linker |
| 1656 | completes a mapping change. |
| 1657 | |
| 1658 | */ |
| 1659 | |
| 1660 | #ifndef SVR4_SHARED_LIBS |
| 1661 | |
| 1662 | static int |
| 1663 | disable_break () |
| 1664 | { |
| 1665 | int status = 1; |
| 1666 | |
| 1667 | #ifndef SVR4_SHARED_LIBS |
| 1668 | |
| 1669 | int in_debugger = 0; |
| 1670 | |
| 1671 | /* Read the debugger structure from the inferior to retrieve the |
| 1672 | address of the breakpoint and the original contents of the |
| 1673 | breakpoint address. Remove the breakpoint by writing the original |
| 1674 | contents back. */ |
| 1675 | |
| 1676 | read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy)); |
| 1677 | |
| 1678 | /* Set `in_debugger' to zero now. */ |
| 1679 | |
| 1680 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); |
| 1681 | |
| 1682 | breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr); |
| 1683 | write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst, |
| 1684 | sizeof (debug_copy.ldd_bp_inst)); |
| 1685 | |
| 1686 | #else /* SVR4_SHARED_LIBS */ |
| 1687 | |
| 1688 | /* Note that breakpoint address and original contents are in our address |
| 1689 | space, so we just need to write the original contents back. */ |
| 1690 | |
| 1691 | if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0) |
| 1692 | { |
| 1693 | status = 0; |
| 1694 | } |
| 1695 | |
| 1696 | #endif /* !SVR4_SHARED_LIBS */ |
| 1697 | |
| 1698 | /* For the SVR4 version, we always know the breakpoint address. For the |
| 1699 | SunOS version we don't know it until the above code is executed. |
| 1700 | Grumble if we are stopped anywhere besides the breakpoint address. */ |
| 1701 | |
| 1702 | if (stop_pc != breakpoint_addr) |
| 1703 | { |
| 1704 | warning ("stopped at unknown breakpoint while handling shared libraries"); |
| 1705 | } |
| 1706 | |
| 1707 | return (status); |
| 1708 | } |
| 1709 | |
| 1710 | #endif /* #ifdef SVR4_SHARED_LIBS */ |
| 1711 | |
| 1712 | /* |
| 1713 | |
| 1714 | LOCAL FUNCTION |
| 1715 | |
| 1716 | enable_break -- arrange for dynamic linker to hit breakpoint |
| 1717 | |
| 1718 | SYNOPSIS |
| 1719 | |
| 1720 | int enable_break (void) |
| 1721 | |
| 1722 | DESCRIPTION |
| 1723 | |
| 1724 | Both the SunOS and the SVR4 dynamic linkers have, as part of their |
| 1725 | debugger interface, support for arranging for the inferior to hit |
| 1726 | a breakpoint after mapping in the shared libraries. This function |
| 1727 | enables that breakpoint. |
| 1728 | |
| 1729 | For SunOS, there is a special flag location (in_debugger) which we |
| 1730 | set to 1. When the dynamic linker sees this flag set, it will set |
| 1731 | a breakpoint at a location known only to itself, after saving the |
| 1732 | original contents of that place and the breakpoint address itself, |
| 1733 | in it's own internal structures. When we resume the inferior, it |
| 1734 | will eventually take a SIGTRAP when it runs into the breakpoint. |
| 1735 | We handle this (in a different place) by restoring the contents of |
| 1736 | the breakpointed location (which is only known after it stops), |
| 1737 | chasing around to locate the shared libraries that have been |
| 1738 | loaded, then resuming. |
| 1739 | |
| 1740 | For SVR4, the debugger interface structure contains a member (r_brk) |
| 1741 | which is statically initialized at the time the shared library is |
| 1742 | built, to the offset of a function (_r_debug_state) which is guaran- |
| 1743 | teed to be called once before mapping in a library, and again when |
| 1744 | the mapping is complete. At the time we are examining this member, |
| 1745 | it contains only the unrelocated offset of the function, so we have |
| 1746 | to do our own relocation. Later, when the dynamic linker actually |
| 1747 | runs, it relocates r_brk to be the actual address of _r_debug_state(). |
| 1748 | |
| 1749 | The debugger interface structure also contains an enumeration which |
| 1750 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, |
| 1751 | depending upon whether or not the library is being mapped or unmapped, |
| 1752 | and then set to RT_CONSISTENT after the library is mapped/unmapped. |
| 1753 | */ |
| 1754 | |
| 1755 | static int |
| 1756 | enable_break () |
| 1757 | { |
| 1758 | int success = 0; |
| 1759 | |
| 1760 | #ifndef SVR4_SHARED_LIBS |
| 1761 | |
| 1762 | int j; |
| 1763 | int in_debugger; |
| 1764 | |
| 1765 | /* Get link_dynamic structure */ |
| 1766 | |
| 1767 | j = target_read_memory (debug_base, (char *) &dynamic_copy, |
| 1768 | sizeof (dynamic_copy)); |
| 1769 | if (j) |
| 1770 | { |
| 1771 | /* unreadable */ |
| 1772 | return (0); |
| 1773 | } |
| 1774 | |
| 1775 | /* Calc address of debugger interface structure */ |
| 1776 | |
| 1777 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); |
| 1778 | |
| 1779 | /* Calc address of `in_debugger' member of debugger interface structure */ |
| 1780 | |
| 1781 | flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger - |
| 1782 | (char *) &debug_copy); |
| 1783 | |
| 1784 | /* Write a value of 1 to this member. */ |
| 1785 | |
| 1786 | in_debugger = 1; |
| 1787 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); |
| 1788 | success = 1; |
| 1789 | |
| 1790 | #else /* SVR4_SHARED_LIBS */ |
| 1791 | |
| 1792 | #ifdef BKPT_AT_SYMBOL |
| 1793 | |
| 1794 | struct minimal_symbol *msymbol; |
| 1795 | char **bkpt_namep; |
| 1796 | asection *interp_sect; |
| 1797 | |
| 1798 | /* First, remove all the solib event breakpoints. Their addresses |
| 1799 | may have changed since the last time we ran the program. */ |
| 1800 | remove_solib_event_breakpoints (); |
| 1801 | |
| 1802 | #ifdef SVR4_SHARED_LIBS |
| 1803 | interp_text_sect_low = interp_text_sect_high = 0; |
| 1804 | interp_plt_sect_low = interp_plt_sect_high = 0; |
| 1805 | |
| 1806 | /* Find the .interp section; if not found, warn the user and drop |
| 1807 | into the old breakpoint at symbol code. */ |
| 1808 | interp_sect = bfd_get_section_by_name (exec_bfd, ".interp"); |
| 1809 | if (interp_sect) |
| 1810 | { |
| 1811 | unsigned int interp_sect_size; |
| 1812 | char *buf; |
| 1813 | CORE_ADDR load_addr; |
| 1814 | bfd *tmp_bfd; |
| 1815 | CORE_ADDR sym_addr = 0; |
| 1816 | |
| 1817 | /* Read the contents of the .interp section into a local buffer; |
| 1818 | the contents specify the dynamic linker this program uses. */ |
| 1819 | interp_sect_size = bfd_section_size (exec_bfd, interp_sect); |
| 1820 | buf = alloca (interp_sect_size); |
| 1821 | bfd_get_section_contents (exec_bfd, interp_sect, |
| 1822 | buf, 0, interp_sect_size); |
| 1823 | |
| 1824 | /* Now we need to figure out where the dynamic linker was |
| 1825 | loaded so that we can load its symbols and place a breakpoint |
| 1826 | in the dynamic linker itself. |
| 1827 | |
| 1828 | This address is stored on the stack. However, I've been unable |
| 1829 | to find any magic formula to find it for Solaris (appears to |
| 1830 | be trivial on GNU/Linux). Therefore, we have to try an alternate |
| 1831 | mechanism to find the dynamic linker's base address. */ |
| 1832 | tmp_bfd = bfd_openr (buf, gnutarget); |
| 1833 | if (tmp_bfd == NULL) |
| 1834 | goto bkpt_at_symbol; |
| 1835 | |
| 1836 | /* Make sure the dynamic linker's really a useful object. */ |
| 1837 | if (!bfd_check_format (tmp_bfd, bfd_object)) |
| 1838 | { |
| 1839 | warning ("Unable to grok dynamic linker %s as an object file", buf); |
| 1840 | bfd_close (tmp_bfd); |
| 1841 | goto bkpt_at_symbol; |
| 1842 | } |
| 1843 | |
| 1844 | /* We find the dynamic linker's base address by examining the |
| 1845 | current pc (which point at the entry point for the dynamic |
| 1846 | linker) and subtracting the offset of the entry point. */ |
| 1847 | load_addr = read_pc () - tmp_bfd->start_address; |
| 1848 | |
| 1849 | /* Record the relocated start and end address of the dynamic linker |
| 1850 | text and plt section for in_svr4_dynsym_resolve_code. */ |
| 1851 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".text"); |
| 1852 | if (interp_sect) |
| 1853 | { |
| 1854 | interp_text_sect_low = |
| 1855 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
| 1856 | interp_text_sect_high = |
| 1857 | interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect); |
| 1858 | } |
| 1859 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt"); |
| 1860 | if (interp_sect) |
| 1861 | { |
| 1862 | interp_plt_sect_low = |
| 1863 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
| 1864 | interp_plt_sect_high = |
| 1865 | interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect); |
| 1866 | } |
| 1867 | |
| 1868 | /* Now try to set a breakpoint in the dynamic linker. */ |
| 1869 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) |
| 1870 | { |
| 1871 | sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep); |
| 1872 | if (sym_addr != 0) |
| 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | /* We're done with the temporary bfd. */ |
| 1877 | bfd_close (tmp_bfd); |
| 1878 | |
| 1879 | if (sym_addr != 0) |
| 1880 | { |
| 1881 | create_solib_event_breakpoint (load_addr + sym_addr); |
| 1882 | return 1; |
| 1883 | } |
| 1884 | |
| 1885 | /* For whatever reason we couldn't set a breakpoint in the dynamic |
| 1886 | linker. Warn and drop into the old code. */ |
| 1887 | bkpt_at_symbol: |
| 1888 | warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code."); |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
| 1892 | /* Scan through the list of symbols, trying to look up the symbol and |
| 1893 | set a breakpoint there. Terminate loop when we/if we succeed. */ |
| 1894 | |
| 1895 | breakpoint_addr = 0; |
| 1896 | for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++) |
| 1897 | { |
| 1898 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile); |
| 1899 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) |
| 1900 | { |
| 1901 | create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol)); |
| 1902 | return 1; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | /* Nothing good happened. */ |
| 1907 | success = 0; |
| 1908 | |
| 1909 | #endif /* BKPT_AT_SYMBOL */ |
| 1910 | |
| 1911 | #endif /* !SVR4_SHARED_LIBS */ |
| 1912 | |
| 1913 | return (success); |
| 1914 | } |
| 1915 | |
| 1916 | /* |
| 1917 | |
| 1918 | GLOBAL FUNCTION |
| 1919 | |
| 1920 | solib_create_inferior_hook -- shared library startup support |
| 1921 | |
| 1922 | SYNOPSIS |
| 1923 | |
| 1924 | void solib_create_inferior_hook() |
| 1925 | |
| 1926 | DESCRIPTION |
| 1927 | |
| 1928 | When gdb starts up the inferior, it nurses it along (through the |
| 1929 | shell) until it is ready to execute it's first instruction. At this |
| 1930 | point, this function gets called via expansion of the macro |
| 1931 | SOLIB_CREATE_INFERIOR_HOOK. |
| 1932 | |
| 1933 | For SunOS executables, this first instruction is typically the |
| 1934 | one at "_start", or a similar text label, regardless of whether |
| 1935 | the executable is statically or dynamically linked. The runtime |
| 1936 | startup code takes care of dynamically linking in any shared |
| 1937 | libraries, once gdb allows the inferior to continue. |
| 1938 | |
| 1939 | For SVR4 executables, this first instruction is either the first |
| 1940 | instruction in the dynamic linker (for dynamically linked |
| 1941 | executables) or the instruction at "start" for statically linked |
| 1942 | executables. For dynamically linked executables, the system |
| 1943 | first exec's /lib/libc.so.N, which contains the dynamic linker, |
| 1944 | and starts it running. The dynamic linker maps in any needed |
| 1945 | shared libraries, maps in the actual user executable, and then |
| 1946 | jumps to "start" in the user executable. |
| 1947 | |
| 1948 | For both SunOS shared libraries, and SVR4 shared libraries, we |
| 1949 | can arrange to cooperate with the dynamic linker to discover the |
| 1950 | names of shared libraries that are dynamically linked, and the |
| 1951 | base addresses to which they are linked. |
| 1952 | |
| 1953 | This function is responsible for discovering those names and |
| 1954 | addresses, and saving sufficient information about them to allow |
| 1955 | their symbols to be read at a later time. |
| 1956 | |
| 1957 | FIXME |
| 1958 | |
| 1959 | Between enable_break() and disable_break(), this code does not |
| 1960 | properly handle hitting breakpoints which the user might have |
| 1961 | set in the startup code or in the dynamic linker itself. Proper |
| 1962 | handling will probably have to wait until the implementation is |
| 1963 | changed to use the "breakpoint handler function" method. |
| 1964 | |
| 1965 | Also, what if child has exit()ed? Must exit loop somehow. |
| 1966 | */ |
| 1967 | |
| 1968 | void |
| 1969 | solib_create_inferior_hook () |
| 1970 | { |
| 1971 | /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base |
| 1972 | yet. In fact, in the case of a SunOS4 executable being run on |
| 1973 | Solaris, we can't get it yet. current_sos will get it when it needs |
| 1974 | it. */ |
| 1975 | #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL)) |
| 1976 | if ((debug_base = locate_base ()) == 0) |
| 1977 | { |
| 1978 | /* Can't find the symbol or the executable is statically linked. */ |
| 1979 | return; |
| 1980 | } |
| 1981 | #endif |
| 1982 | |
| 1983 | if (!enable_break ()) |
| 1984 | { |
| 1985 | warning ("shared library handler failed to enable breakpoint"); |
| 1986 | return; |
| 1987 | } |
| 1988 | |
| 1989 | #if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS) |
| 1990 | /* SCO and SunOS need the loop below, other systems should be using the |
| 1991 | special shared library breakpoints and the shared library breakpoint |
| 1992 | service routine. |
| 1993 | |
| 1994 | Now run the target. It will eventually hit the breakpoint, at |
| 1995 | which point all of the libraries will have been mapped in and we |
| 1996 | can go groveling around in the dynamic linker structures to find |
| 1997 | out what we need to know about them. */ |
| 1998 | |
| 1999 | clear_proceed_status (); |
| 2000 | stop_soon_quietly = 1; |
| 2001 | stop_signal = TARGET_SIGNAL_0; |
| 2002 | do |
| 2003 | { |
| 2004 | target_resume (-1, 0, stop_signal); |
| 2005 | wait_for_inferior (); |
| 2006 | } |
| 2007 | while (stop_signal != TARGET_SIGNAL_TRAP); |
| 2008 | stop_soon_quietly = 0; |
| 2009 | |
| 2010 | #if !defined(_SCO_DS) |
| 2011 | /* We are now either at the "mapping complete" breakpoint (or somewhere |
| 2012 | else, a condition we aren't prepared to deal with anyway), so adjust |
| 2013 | the PC as necessary after a breakpoint, disable the breakpoint, and |
| 2014 | add any shared libraries that were mapped in. */ |
| 2015 | |
| 2016 | if (DECR_PC_AFTER_BREAK) |
| 2017 | { |
| 2018 | stop_pc -= DECR_PC_AFTER_BREAK; |
| 2019 | write_register (PC_REGNUM, stop_pc); |
| 2020 | } |
| 2021 | |
| 2022 | if (!disable_break ()) |
| 2023 | { |
| 2024 | warning ("shared library handler failed to disable breakpoint"); |
| 2025 | } |
| 2026 | |
| 2027 | if (auto_solib_add) |
| 2028 | solib_add ((char *) 0, 0, (struct target_ops *) 0); |
| 2029 | #endif /* ! _SCO_DS */ |
| 2030 | #endif |
| 2031 | } |
| 2032 | |
| 2033 | /* |
| 2034 | |
| 2035 | LOCAL FUNCTION |
| 2036 | |
| 2037 | special_symbol_handling -- additional shared library symbol handling |
| 2038 | |
| 2039 | SYNOPSIS |
| 2040 | |
| 2041 | void special_symbol_handling () |
| 2042 | |
| 2043 | DESCRIPTION |
| 2044 | |
| 2045 | Once the symbols from a shared object have been loaded in the usual |
| 2046 | way, we are called to do any system specific symbol handling that |
| 2047 | is needed. |
| 2048 | |
| 2049 | For SunOS4, this consists of grunging around in the dynamic |
| 2050 | linkers structures to find symbol definitions for "common" symbols |
| 2051 | and adding them to the minimal symbol table for the runtime common |
| 2052 | objfile. |
| 2053 | |
| 2054 | */ |
| 2055 | |
| 2056 | static void |
| 2057 | special_symbol_handling () |
| 2058 | { |
| 2059 | #ifndef SVR4_SHARED_LIBS |
| 2060 | int j; |
| 2061 | |
| 2062 | if (debug_addr == 0) |
| 2063 | { |
| 2064 | /* Get link_dynamic structure */ |
| 2065 | |
| 2066 | j = target_read_memory (debug_base, (char *) &dynamic_copy, |
| 2067 | sizeof (dynamic_copy)); |
| 2068 | if (j) |
| 2069 | { |
| 2070 | /* unreadable */ |
| 2071 | return; |
| 2072 | } |
| 2073 | |
| 2074 | /* Calc address of debugger interface structure */ |
| 2075 | /* FIXME, this needs work for cross-debugging of core files |
| 2076 | (byteorder, size, alignment, etc). */ |
| 2077 | |
| 2078 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); |
| 2079 | } |
| 2080 | |
| 2081 | /* Read the debugger structure from the inferior, just to make sure |
| 2082 | we have a current copy. */ |
| 2083 | |
| 2084 | j = target_read_memory (debug_addr, (char *) &debug_copy, |
| 2085 | sizeof (debug_copy)); |
| 2086 | if (j) |
| 2087 | return; /* unreadable */ |
| 2088 | |
| 2089 | /* Get common symbol definitions for the loaded object. */ |
| 2090 | |
| 2091 | if (debug_copy.ldd_cp) |
| 2092 | { |
| 2093 | solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp)); |
| 2094 | } |
| 2095 | |
| 2096 | #endif /* !SVR4_SHARED_LIBS */ |
| 2097 | } |
| 2098 | |
| 2099 | |
| 2100 | /* |
| 2101 | |
| 2102 | LOCAL FUNCTION |
| 2103 | |
| 2104 | sharedlibrary_command -- handle command to explicitly add library |
| 2105 | |
| 2106 | SYNOPSIS |
| 2107 | |
| 2108 | static void sharedlibrary_command (char *args, int from_tty) |
| 2109 | |
| 2110 | DESCRIPTION |
| 2111 | |
| 2112 | */ |
| 2113 | |
| 2114 | static void |
| 2115 | sharedlibrary_command (args, from_tty) |
| 2116 | char *args; |
| 2117 | int from_tty; |
| 2118 | { |
| 2119 | dont_repeat (); |
| 2120 | solib_add (args, from_tty, (struct target_ops *) 0); |
| 2121 | } |
| 2122 | |
| 2123 | #endif /* HAVE_LINK_H */ |
| 2124 | |
| 2125 | void |
| 2126 | _initialize_solib () |
| 2127 | { |
| 2128 | #ifdef HAVE_LINK_H |
| 2129 | |
| 2130 | add_com ("sharedlibrary", class_files, sharedlibrary_command, |
| 2131 | "Load shared object library symbols for files matching REGEXP."); |
| 2132 | add_info ("sharedlibrary", info_sharedlibrary_command, |
| 2133 | "Status of loaded shared object libraries."); |
| 2134 | |
| 2135 | add_show_from_set |
| 2136 | (add_set_cmd ("auto-solib-add", class_support, var_zinteger, |
| 2137 | (char *) &auto_solib_add, |
| 2138 | "Set autoloading of shared library symbols.\n\ |
| 2139 | If nonzero, symbols from all shared object libraries will be loaded\n\ |
| 2140 | automatically when the inferior begins execution or when the dynamic linker\n\ |
| 2141 | informs gdb that a new library has been loaded. Otherwise, symbols\n\ |
| 2142 | must be loaded manually, using `sharedlibrary'.", |
| 2143 | &setlist), |
| 2144 | &showlist); |
| 2145 | |
| 2146 | add_show_from_set |
| 2147 | (add_set_cmd ("solib-absolute-prefix", class_support, var_filename, |
| 2148 | (char *) &solib_absolute_prefix, |
| 2149 | "Set prefix for loading absolute shared library symbol files.\n\ |
| 2150 | For other (relative) files, you can add values using `set solib-search-path'.", |
| 2151 | &setlist), |
| 2152 | &showlist); |
| 2153 | add_show_from_set |
| 2154 | (add_set_cmd ("solib-search-path", class_support, var_string, |
| 2155 | (char *) &solib_search_path, |
| 2156 | "Set the search path for loading non-absolute shared library symbol files.\n\ |
| 2157 | This takes precedence over the environment variables PATH and LD_LIBRARY_PATH.", |
| 2158 | &setlist), |
| 2159 | &showlist); |
| 2160 | |
| 2161 | #endif /* HAVE_LINK_H */ |
| 2162 | } |