| 1 | /* Handle SVR4 shared libraries for GDB, the GNU Debugger. |
| 2 | |
| 3 | Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, |
| 4 | 2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | |
| 23 | #include "elf/external.h" |
| 24 | #include "elf/common.h" |
| 25 | #include "elf/mips.h" |
| 26 | |
| 27 | #include "symtab.h" |
| 28 | #include "bfd.h" |
| 29 | #include "symfile.h" |
| 30 | #include "objfiles.h" |
| 31 | #include "gdbcore.h" |
| 32 | #include "target.h" |
| 33 | #include "inferior.h" |
| 34 | |
| 35 | #include "gdb_assert.h" |
| 36 | |
| 37 | #include "solist.h" |
| 38 | #include "solib.h" |
| 39 | #include "solib-svr4.h" |
| 40 | |
| 41 | #include "bfd-target.h" |
| 42 | #include "elf-bfd.h" |
| 43 | #include "exec.h" |
| 44 | |
| 45 | static struct link_map_offsets *svr4_fetch_link_map_offsets (void); |
| 46 | static int svr4_have_link_map_offsets (void); |
| 47 | |
| 48 | /* This hook is set to a function that provides native link map |
| 49 | offsets if the code in solib-legacy.c is linked in. */ |
| 50 | struct link_map_offsets *(*legacy_svr4_fetch_link_map_offsets_hook) (void); |
| 51 | |
| 52 | /* Link map info to include in an allocated so_list entry */ |
| 53 | |
| 54 | struct lm_info |
| 55 | { |
| 56 | /* Pointer to copy of link map from inferior. The type is char * |
| 57 | rather than void *, so that we may use byte offsets to find the |
| 58 | various fields without the need for a cast. */ |
| 59 | gdb_byte *lm; |
| 60 | |
| 61 | /* Amount by which addresses in the binary should be relocated to |
| 62 | match the inferior. This could most often be taken directly |
| 63 | from lm, but when prelinking is involved and the prelink base |
| 64 | address changes, we may need a different offset, we want to |
| 65 | warn about the difference and compute it only once. */ |
| 66 | CORE_ADDR l_addr; |
| 67 | }; |
| 68 | |
| 69 | /* On SVR4 systems, a list of symbols in the dynamic linker where |
| 70 | GDB can try to place a breakpoint to monitor shared library |
| 71 | events. |
| 72 | |
| 73 | If none of these symbols are found, or other errors occur, then |
| 74 | SVR4 systems will fall back to using a symbol as the "startup |
| 75 | mapping complete" breakpoint address. */ |
| 76 | |
| 77 | static char *solib_break_names[] = |
| 78 | { |
| 79 | "r_debug_state", |
| 80 | "_r_debug_state", |
| 81 | "_dl_debug_state", |
| 82 | "rtld_db_dlactivity", |
| 83 | "_rtld_debug_state", |
| 84 | |
| 85 | NULL |
| 86 | }; |
| 87 | |
| 88 | #define BKPT_AT_SYMBOL 1 |
| 89 | |
| 90 | #if defined (BKPT_AT_SYMBOL) |
| 91 | static char *bkpt_names[] = |
| 92 | { |
| 93 | #ifdef SOLIB_BKPT_NAME |
| 94 | SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */ |
| 95 | #endif |
| 96 | "_start", |
| 97 | "__start", |
| 98 | "main", |
| 99 | NULL |
| 100 | }; |
| 101 | #endif |
| 102 | |
| 103 | static char *main_name_list[] = |
| 104 | { |
| 105 | "main_$main", |
| 106 | NULL |
| 107 | }; |
| 108 | |
| 109 | /* link map access functions */ |
| 110 | |
| 111 | static CORE_ADDR |
| 112 | LM_ADDR_FROM_LINK_MAP (struct so_list *so) |
| 113 | { |
| 114 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 115 | |
| 116 | return extract_typed_address (so->lm_info->lm + lmo->l_addr_offset, |
| 117 | builtin_type_void_data_ptr); |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | HAS_LM_DYNAMIC_FROM_LINK_MAP () |
| 122 | { |
| 123 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 124 | |
| 125 | return lmo->l_ld_offset >= 0; |
| 126 | } |
| 127 | |
| 128 | static CORE_ADDR |
| 129 | LM_DYNAMIC_FROM_LINK_MAP (struct so_list *so) |
| 130 | { |
| 131 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 132 | |
| 133 | return extract_typed_address (so->lm_info->lm + lmo->l_ld_offset, |
| 134 | builtin_type_void_data_ptr); |
| 135 | } |
| 136 | |
| 137 | static CORE_ADDR |
| 138 | LM_ADDR_CHECK (struct so_list *so, bfd *abfd) |
| 139 | { |
| 140 | if (so->lm_info->l_addr == (CORE_ADDR)-1) |
| 141 | { |
| 142 | struct bfd_section *dyninfo_sect; |
| 143 | CORE_ADDR l_addr, l_dynaddr, dynaddr, align = 0x1000; |
| 144 | |
| 145 | l_addr = LM_ADDR_FROM_LINK_MAP (so); |
| 146 | |
| 147 | if (! abfd || ! HAS_LM_DYNAMIC_FROM_LINK_MAP ()) |
| 148 | goto set_addr; |
| 149 | |
| 150 | l_dynaddr = LM_DYNAMIC_FROM_LINK_MAP (so); |
| 151 | |
| 152 | dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic"); |
| 153 | if (dyninfo_sect == NULL) |
| 154 | goto set_addr; |
| 155 | |
| 156 | dynaddr = bfd_section_vma (abfd, dyninfo_sect); |
| 157 | |
| 158 | if (dynaddr + l_addr != l_dynaddr) |
| 159 | { |
| 160 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
| 161 | { |
| 162 | Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header; |
| 163 | Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr; |
| 164 | int i; |
| 165 | |
| 166 | align = 1; |
| 167 | |
| 168 | for (i = 0; i < ehdr->e_phnum; i++) |
| 169 | if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align) |
| 170 | align = phdr[i].p_align; |
| 171 | } |
| 172 | |
| 173 | /* Turn it into a mask. */ |
| 174 | align--; |
| 175 | |
| 176 | /* If the changes match the alignment requirements, we |
| 177 | assume we're using a core file that was generated by the |
| 178 | same binary, just prelinked with a different base offset. |
| 179 | If it doesn't match, we may have a different binary, the |
| 180 | same binary with the dynamic table loaded at an unrelated |
| 181 | location, or anything, really. To avoid regressions, |
| 182 | don't adjust the base offset in the latter case, although |
| 183 | odds are that, if things really changed, debugging won't |
| 184 | quite work. */ |
| 185 | if ((l_addr & align) == ((l_dynaddr - dynaddr) & align)) |
| 186 | { |
| 187 | l_addr = l_dynaddr - dynaddr; |
| 188 | |
| 189 | warning (_(".dynamic section for \"%s\" " |
| 190 | "is not at the expected address"), so->so_name); |
| 191 | warning (_("difference appears to be caused by prelink, " |
| 192 | "adjusting expectations")); |
| 193 | } |
| 194 | else |
| 195 | warning (_(".dynamic section for \"%s\" " |
| 196 | "is not at the expected address " |
| 197 | "(wrong library or version mismatch?)"), so->so_name); |
| 198 | } |
| 199 | |
| 200 | set_addr: |
| 201 | so->lm_info->l_addr = l_addr; |
| 202 | } |
| 203 | |
| 204 | return so->lm_info->l_addr; |
| 205 | } |
| 206 | |
| 207 | static CORE_ADDR |
| 208 | LM_NEXT (struct so_list *so) |
| 209 | { |
| 210 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 211 | |
| 212 | return extract_typed_address (so->lm_info->lm + lmo->l_next_offset, |
| 213 | builtin_type_void_data_ptr); |
| 214 | } |
| 215 | |
| 216 | static CORE_ADDR |
| 217 | LM_NAME (struct so_list *so) |
| 218 | { |
| 219 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 220 | |
| 221 | return extract_typed_address (so->lm_info->lm + lmo->l_name_offset, |
| 222 | builtin_type_void_data_ptr); |
| 223 | } |
| 224 | |
| 225 | static int |
| 226 | IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so) |
| 227 | { |
| 228 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 229 | |
| 230 | /* Assume that everything is a library if the dynamic loader was loaded |
| 231 | late by a static executable. */ |
| 232 | if (bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL) |
| 233 | return 0; |
| 234 | |
| 235 | return extract_typed_address (so->lm_info->lm + lmo->l_prev_offset, |
| 236 | builtin_type_void_data_ptr) == 0; |
| 237 | } |
| 238 | |
| 239 | static CORE_ADDR debug_base; /* Base of dynamic linker structures */ |
| 240 | |
| 241 | /* Validity flag for debug_loader_offset. */ |
| 242 | static int debug_loader_offset_p; |
| 243 | |
| 244 | /* Load address for the dynamic linker, inferred. */ |
| 245 | static CORE_ADDR debug_loader_offset; |
| 246 | |
| 247 | /* Name of the dynamic linker, valid if debug_loader_offset_p. */ |
| 248 | static char *debug_loader_name; |
| 249 | |
| 250 | /* Local function prototypes */ |
| 251 | |
| 252 | static int match_main (char *); |
| 253 | |
| 254 | static CORE_ADDR bfd_lookup_symbol (bfd *, char *); |
| 255 | |
| 256 | /* |
| 257 | |
| 258 | LOCAL FUNCTION |
| 259 | |
| 260 | bfd_lookup_symbol -- lookup the value for a specific symbol |
| 261 | |
| 262 | SYNOPSIS |
| 263 | |
| 264 | CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname) |
| 265 | |
| 266 | DESCRIPTION |
| 267 | |
| 268 | An expensive way to lookup the value of a single symbol for |
| 269 | bfd's that are only temporary anyway. This is used by the |
| 270 | shared library support to find the address of the debugger |
| 271 | notification routine in the shared library. |
| 272 | |
| 273 | The returned symbol may be in a code or data section; functions |
| 274 | will normally be in a code section, but may be in a data section |
| 275 | if this architecture uses function descriptors. |
| 276 | |
| 277 | Note that 0 is specifically allowed as an error return (no |
| 278 | such symbol). |
| 279 | */ |
| 280 | |
| 281 | static CORE_ADDR |
| 282 | bfd_lookup_symbol (bfd *abfd, char *symname) |
| 283 | { |
| 284 | long storage_needed; |
| 285 | asymbol *sym; |
| 286 | asymbol **symbol_table; |
| 287 | unsigned int number_of_symbols; |
| 288 | unsigned int i; |
| 289 | struct cleanup *back_to; |
| 290 | CORE_ADDR symaddr = 0; |
| 291 | |
| 292 | storage_needed = bfd_get_symtab_upper_bound (abfd); |
| 293 | |
| 294 | if (storage_needed > 0) |
| 295 | { |
| 296 | symbol_table = (asymbol **) xmalloc (storage_needed); |
| 297 | back_to = make_cleanup (xfree, symbol_table); |
| 298 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); |
| 299 | |
| 300 | for (i = 0; i < number_of_symbols; i++) |
| 301 | { |
| 302 | sym = *symbol_table++; |
| 303 | if (strcmp (sym->name, symname) == 0 |
| 304 | && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0) |
| 305 | { |
| 306 | /* BFD symbols are section relative. */ |
| 307 | symaddr = sym->value + sym->section->vma; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | do_cleanups (back_to); |
| 312 | } |
| 313 | |
| 314 | if (symaddr) |
| 315 | return symaddr; |
| 316 | |
| 317 | /* On FreeBSD, the dynamic linker is stripped by default. So we'll |
| 318 | have to check the dynamic string table too. */ |
| 319 | |
| 320 | storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd); |
| 321 | |
| 322 | if (storage_needed > 0) |
| 323 | { |
| 324 | symbol_table = (asymbol **) xmalloc (storage_needed); |
| 325 | back_to = make_cleanup (xfree, symbol_table); |
| 326 | number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table); |
| 327 | |
| 328 | for (i = 0; i < number_of_symbols; i++) |
| 329 | { |
| 330 | sym = *symbol_table++; |
| 331 | |
| 332 | if (strcmp (sym->name, symname) == 0 |
| 333 | && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0) |
| 334 | { |
| 335 | /* BFD symbols are section relative. */ |
| 336 | symaddr = sym->value + sym->section->vma; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | do_cleanups (back_to); |
| 341 | } |
| 342 | |
| 343 | return symaddr; |
| 344 | } |
| 345 | |
| 346 | /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is |
| 347 | returned and the corresponding PTR is set. */ |
| 348 | |
| 349 | static int |
| 350 | scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr) |
| 351 | { |
| 352 | int arch_size, step, sect_size; |
| 353 | long dyn_tag; |
| 354 | CORE_ADDR dyn_ptr, dyn_addr; |
| 355 | gdb_byte *bufend, *bufstart, *buf; |
| 356 | Elf32_External_Dyn *x_dynp_32; |
| 357 | Elf64_External_Dyn *x_dynp_64; |
| 358 | struct bfd_section *sect; |
| 359 | |
| 360 | if (abfd == NULL) |
| 361 | return 0; |
| 362 | arch_size = bfd_get_arch_size (abfd); |
| 363 | if (arch_size == -1) |
| 364 | return 0; |
| 365 | |
| 366 | /* Find the start address of the .dynamic section. */ |
| 367 | sect = bfd_get_section_by_name (abfd, ".dynamic"); |
| 368 | if (sect == NULL) |
| 369 | return 0; |
| 370 | dyn_addr = bfd_section_vma (abfd, sect); |
| 371 | |
| 372 | /* Read in .dynamic from the BFD. We will get the actual value |
| 373 | from memory later. */ |
| 374 | sect_size = bfd_section_size (abfd, sect); |
| 375 | buf = bufstart = alloca (sect_size); |
| 376 | if (!bfd_get_section_contents (abfd, sect, |
| 377 | buf, 0, sect_size)) |
| 378 | return 0; |
| 379 | |
| 380 | /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */ |
| 381 | step = (arch_size == 32) ? sizeof (Elf32_External_Dyn) |
| 382 | : sizeof (Elf64_External_Dyn); |
| 383 | for (bufend = buf + sect_size; |
| 384 | buf < bufend; |
| 385 | buf += step) |
| 386 | { |
| 387 | if (arch_size == 32) |
| 388 | { |
| 389 | x_dynp_32 = (Elf32_External_Dyn *) buf; |
| 390 | dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag); |
| 391 | dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | x_dynp_64 = (Elf64_External_Dyn *) buf; |
| 396 | dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag); |
| 397 | dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr); |
| 398 | } |
| 399 | if (dyn_tag == DT_NULL) |
| 400 | return 0; |
| 401 | if (dyn_tag == dyntag) |
| 402 | { |
| 403 | /* If requested, try to read the runtime value of this .dynamic |
| 404 | entry. */ |
| 405 | if (ptr) |
| 406 | { |
| 407 | gdb_byte ptr_buf[8]; |
| 408 | CORE_ADDR ptr_addr; |
| 409 | |
| 410 | ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8; |
| 411 | if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0) |
| 412 | dyn_ptr = extract_typed_address (ptr_buf, |
| 413 | builtin_type_void_data_ptr); |
| 414 | *ptr = dyn_ptr; |
| 415 | } |
| 416 | return 1; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /* |
| 425 | |
| 426 | LOCAL FUNCTION |
| 427 | |
| 428 | elf_locate_base -- locate the base address of dynamic linker structs |
| 429 | for SVR4 elf targets. |
| 430 | |
| 431 | SYNOPSIS |
| 432 | |
| 433 | CORE_ADDR elf_locate_base (void) |
| 434 | |
| 435 | DESCRIPTION |
| 436 | |
| 437 | For SVR4 elf targets the address of the dynamic linker's runtime |
| 438 | structure is contained within the dynamic info section in the |
| 439 | executable file. The dynamic section is also mapped into the |
| 440 | inferior address space. Because the runtime loader fills in the |
| 441 | real address before starting the inferior, we have to read in the |
| 442 | dynamic info section from the inferior address space. |
| 443 | If there are any errors while trying to find the address, we |
| 444 | silently return 0, otherwise the found address is returned. |
| 445 | |
| 446 | */ |
| 447 | |
| 448 | static CORE_ADDR |
| 449 | elf_locate_base (void) |
| 450 | { |
| 451 | struct minimal_symbol *msymbol; |
| 452 | CORE_ADDR dyn_ptr; |
| 453 | |
| 454 | /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this |
| 455 | instead of DT_DEBUG, although they sometimes contain an unused |
| 456 | DT_DEBUG. */ |
| 457 | if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr)) |
| 458 | { |
| 459 | gdb_byte *pbuf; |
| 460 | int pbuf_size = TYPE_LENGTH (builtin_type_void_data_ptr); |
| 461 | pbuf = alloca (pbuf_size); |
| 462 | /* DT_MIPS_RLD_MAP contains a pointer to the address |
| 463 | of the dynamic link structure. */ |
| 464 | if (target_read_memory (dyn_ptr, pbuf, pbuf_size)) |
| 465 | return 0; |
| 466 | return extract_typed_address (pbuf, builtin_type_void_data_ptr); |
| 467 | } |
| 468 | |
| 469 | /* Find DT_DEBUG. */ |
| 470 | if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr)) |
| 471 | return dyn_ptr; |
| 472 | |
| 473 | /* This may be a static executable. Look for the symbol |
| 474 | conventionally named _r_debug, as a last resort. */ |
| 475 | msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile); |
| 476 | if (msymbol != NULL) |
| 477 | return SYMBOL_VALUE_ADDRESS (msymbol); |
| 478 | |
| 479 | /* DT_DEBUG entry not found. */ |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | |
| 485 | LOCAL FUNCTION |
| 486 | |
| 487 | locate_base -- locate the base address of dynamic linker structs |
| 488 | |
| 489 | SYNOPSIS |
| 490 | |
| 491 | CORE_ADDR locate_base (void) |
| 492 | |
| 493 | DESCRIPTION |
| 494 | |
| 495 | For both the SunOS and SVR4 shared library implementations, if the |
| 496 | inferior executable has been linked dynamically, there is a single |
| 497 | address somewhere in the inferior's data space which is the key to |
| 498 | locating all of the dynamic linker's runtime structures. This |
| 499 | address is the value of the debug base symbol. The job of this |
| 500 | function is to find and return that address, or to return 0 if there |
| 501 | is no such address (the executable is statically linked for example). |
| 502 | |
| 503 | For SunOS, the job is almost trivial, since the dynamic linker and |
| 504 | all of it's structures are statically linked to the executable at |
| 505 | link time. Thus the symbol for the address we are looking for has |
| 506 | already been added to the minimal symbol table for the executable's |
| 507 | objfile at the time the symbol file's symbols were read, and all we |
| 508 | have to do is look it up there. Note that we explicitly do NOT want |
| 509 | to find the copies in the shared library. |
| 510 | |
| 511 | The SVR4 version is a bit more complicated because the address |
| 512 | is contained somewhere in the dynamic info section. We have to go |
| 513 | to a lot more work to discover the address of the debug base symbol. |
| 514 | Because of this complexity, we cache the value we find and return that |
| 515 | value on subsequent invocations. Note there is no copy in the |
| 516 | executable symbol tables. |
| 517 | |
| 518 | */ |
| 519 | |
| 520 | static CORE_ADDR |
| 521 | locate_base (void) |
| 522 | { |
| 523 | /* Check to see if we have a currently valid address, and if so, avoid |
| 524 | doing all this work again and just return the cached address. If |
| 525 | we have no cached address, try to locate it in the dynamic info |
| 526 | section for ELF executables. There's no point in doing any of this |
| 527 | though if we don't have some link map offsets to work with. */ |
| 528 | |
| 529 | if (debug_base == 0 && svr4_have_link_map_offsets ()) |
| 530 | { |
| 531 | if (exec_bfd != NULL |
| 532 | && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour) |
| 533 | debug_base = elf_locate_base (); |
| 534 | } |
| 535 | return (debug_base); |
| 536 | } |
| 537 | |
| 538 | /* Find the first element in the inferior's dynamic link map, and |
| 539 | return its address in the inferior. |
| 540 | |
| 541 | FIXME: Perhaps we should validate the info somehow, perhaps by |
| 542 | checking r_version for a known version number, or r_state for |
| 543 | RT_CONSISTENT. */ |
| 544 | |
| 545 | static CORE_ADDR |
| 546 | solib_svr4_r_map (void) |
| 547 | { |
| 548 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 549 | |
| 550 | return read_memory_typed_address (debug_base + lmo->r_map_offset, |
| 551 | builtin_type_void_data_ptr); |
| 552 | } |
| 553 | |
| 554 | /* Find the link map for the dynamic linker (if it is not in the |
| 555 | normal list of loaded shared objects). */ |
| 556 | |
| 557 | static CORE_ADDR |
| 558 | solib_svr4_r_ldsomap (void) |
| 559 | { |
| 560 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 561 | ULONGEST version; |
| 562 | |
| 563 | /* Check version, and return zero if `struct r_debug' doesn't have |
| 564 | the r_ldsomap member. */ |
| 565 | version = read_memory_unsigned_integer (debug_base + lmo->r_version_offset, |
| 566 | lmo->r_version_size); |
| 567 | if (version < 2 || lmo->r_ldsomap_offset == -1) |
| 568 | return 0; |
| 569 | |
| 570 | return read_memory_typed_address (debug_base + lmo->r_ldsomap_offset, |
| 571 | builtin_type_void_data_ptr); |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | |
| 576 | LOCAL FUNCTION |
| 577 | |
| 578 | open_symbol_file_object |
| 579 | |
| 580 | SYNOPSIS |
| 581 | |
| 582 | void open_symbol_file_object (void *from_tty) |
| 583 | |
| 584 | DESCRIPTION |
| 585 | |
| 586 | If no open symbol file, attempt to locate and open the main symbol |
| 587 | file. On SVR4 systems, this is the first link map entry. If its |
| 588 | name is here, we can open it. Useful when attaching to a process |
| 589 | without first loading its symbol file. |
| 590 | |
| 591 | If FROM_TTYP dereferences to a non-zero integer, allow messages to |
| 592 | be printed. This parameter is a pointer rather than an int because |
| 593 | open_symbol_file_object() is called via catch_errors() and |
| 594 | catch_errors() requires a pointer argument. */ |
| 595 | |
| 596 | static int |
| 597 | open_symbol_file_object (void *from_ttyp) |
| 598 | { |
| 599 | CORE_ADDR lm, l_name; |
| 600 | char *filename; |
| 601 | int errcode; |
| 602 | int from_tty = *(int *)from_ttyp; |
| 603 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 604 | int l_name_size = TYPE_LENGTH (builtin_type_void_data_ptr); |
| 605 | gdb_byte *l_name_buf = xmalloc (l_name_size); |
| 606 | struct cleanup *cleanups = make_cleanup (xfree, l_name_buf); |
| 607 | |
| 608 | if (symfile_objfile) |
| 609 | if (!query ("Attempt to reload symbols from process? ")) |
| 610 | return 0; |
| 611 | |
| 612 | if ((debug_base = locate_base ()) == 0) |
| 613 | return 0; /* failed somehow... */ |
| 614 | |
| 615 | /* First link map member should be the executable. */ |
| 616 | lm = solib_svr4_r_map (); |
| 617 | if (lm == 0) |
| 618 | return 0; /* failed somehow... */ |
| 619 | |
| 620 | /* Read address of name from target memory to GDB. */ |
| 621 | read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size); |
| 622 | |
| 623 | /* Convert the address to host format. */ |
| 624 | l_name = extract_typed_address (l_name_buf, builtin_type_void_data_ptr); |
| 625 | |
| 626 | /* Free l_name_buf. */ |
| 627 | do_cleanups (cleanups); |
| 628 | |
| 629 | if (l_name == 0) |
| 630 | return 0; /* No filename. */ |
| 631 | |
| 632 | /* Now fetch the filename from target memory. */ |
| 633 | target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode); |
| 634 | make_cleanup (xfree, filename); |
| 635 | |
| 636 | if (errcode) |
| 637 | { |
| 638 | warning (_("failed to read exec filename from attached file: %s"), |
| 639 | safe_strerror (errcode)); |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | /* Have a pathname: read the symbol file. */ |
| 644 | symbol_file_add_main (filename, from_tty); |
| 645 | |
| 646 | return 1; |
| 647 | } |
| 648 | |
| 649 | /* If no shared library information is available from the dynamic |
| 650 | linker, build a fallback list from other sources. */ |
| 651 | |
| 652 | static struct so_list * |
| 653 | svr4_default_sos (void) |
| 654 | { |
| 655 | struct so_list *head = NULL; |
| 656 | struct so_list **link_ptr = &head; |
| 657 | |
| 658 | if (debug_loader_offset_p) |
| 659 | { |
| 660 | struct so_list *new = XZALLOC (struct so_list); |
| 661 | |
| 662 | new->lm_info = xmalloc (sizeof (struct lm_info)); |
| 663 | |
| 664 | /* Nothing will ever check the cached copy of the link |
| 665 | map if we set l_addr. */ |
| 666 | new->lm_info->l_addr = debug_loader_offset; |
| 667 | new->lm_info->lm = NULL; |
| 668 | |
| 669 | strncpy (new->so_name, debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1); |
| 670 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; |
| 671 | strcpy (new->so_original_name, new->so_name); |
| 672 | |
| 673 | *link_ptr = new; |
| 674 | link_ptr = &new->next; |
| 675 | } |
| 676 | |
| 677 | return head; |
| 678 | } |
| 679 | |
| 680 | /* LOCAL FUNCTION |
| 681 | |
| 682 | current_sos -- build a list of currently loaded shared objects |
| 683 | |
| 684 | SYNOPSIS |
| 685 | |
| 686 | struct so_list *current_sos () |
| 687 | |
| 688 | DESCRIPTION |
| 689 | |
| 690 | Build a list of `struct so_list' objects describing the shared |
| 691 | objects currently loaded in the inferior. This list does not |
| 692 | include an entry for the main executable file. |
| 693 | |
| 694 | Note that we only gather information directly available from the |
| 695 | inferior --- we don't examine any of the shared library files |
| 696 | themselves. The declaration of `struct so_list' says which fields |
| 697 | we provide values for. */ |
| 698 | |
| 699 | static struct so_list * |
| 700 | svr4_current_sos (void) |
| 701 | { |
| 702 | CORE_ADDR lm; |
| 703 | struct so_list *head = 0; |
| 704 | struct so_list **link_ptr = &head; |
| 705 | CORE_ADDR ldsomap = 0; |
| 706 | |
| 707 | /* Make sure we've looked up the inferior's dynamic linker's base |
| 708 | structure. */ |
| 709 | if (! debug_base) |
| 710 | { |
| 711 | debug_base = locate_base (); |
| 712 | |
| 713 | /* If we can't find the dynamic linker's base structure, this |
| 714 | must not be a dynamically linked executable. Hmm. */ |
| 715 | if (! debug_base) |
| 716 | return svr4_default_sos (); |
| 717 | } |
| 718 | |
| 719 | /* Walk the inferior's link map list, and build our list of |
| 720 | `struct so_list' nodes. */ |
| 721 | lm = solib_svr4_r_map (); |
| 722 | |
| 723 | while (lm) |
| 724 | { |
| 725 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 726 | struct so_list *new = XZALLOC (struct so_list); |
| 727 | struct cleanup *old_chain = make_cleanup (xfree, new); |
| 728 | |
| 729 | new->lm_info = xmalloc (sizeof (struct lm_info)); |
| 730 | make_cleanup (xfree, new->lm_info); |
| 731 | |
| 732 | new->lm_info->l_addr = (CORE_ADDR)-1; |
| 733 | new->lm_info->lm = xzalloc (lmo->link_map_size); |
| 734 | make_cleanup (xfree, new->lm_info->lm); |
| 735 | |
| 736 | read_memory (lm, new->lm_info->lm, lmo->link_map_size); |
| 737 | |
| 738 | lm = LM_NEXT (new); |
| 739 | |
| 740 | /* For SVR4 versions, the first entry in the link map is for the |
| 741 | inferior executable, so we must ignore it. For some versions of |
| 742 | SVR4, it has no name. For others (Solaris 2.3 for example), it |
| 743 | does have a name, so we can no longer use a missing name to |
| 744 | decide when to ignore it. */ |
| 745 | if (IGNORE_FIRST_LINK_MAP_ENTRY (new) && ldsomap == 0) |
| 746 | free_so (new); |
| 747 | else |
| 748 | { |
| 749 | int errcode; |
| 750 | char *buffer; |
| 751 | |
| 752 | /* Extract this shared object's name. */ |
| 753 | target_read_string (LM_NAME (new), &buffer, |
| 754 | SO_NAME_MAX_PATH_SIZE - 1, &errcode); |
| 755 | if (errcode != 0) |
| 756 | warning (_("Can't read pathname for load map: %s."), |
| 757 | safe_strerror (errcode)); |
| 758 | else |
| 759 | { |
| 760 | strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1); |
| 761 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; |
| 762 | strcpy (new->so_original_name, new->so_name); |
| 763 | } |
| 764 | xfree (buffer); |
| 765 | |
| 766 | /* If this entry has no name, or its name matches the name |
| 767 | for the main executable, don't include it in the list. */ |
| 768 | if (! new->so_name[0] |
| 769 | || match_main (new->so_name)) |
| 770 | free_so (new); |
| 771 | else |
| 772 | { |
| 773 | new->next = 0; |
| 774 | *link_ptr = new; |
| 775 | link_ptr = &new->next; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* On Solaris, the dynamic linker is not in the normal list of |
| 780 | shared objects, so make sure we pick it up too. Having |
| 781 | symbol information for the dynamic linker is quite crucial |
| 782 | for skipping dynamic linker resolver code. */ |
| 783 | if (lm == 0 && ldsomap == 0) |
| 784 | lm = ldsomap = solib_svr4_r_ldsomap (); |
| 785 | |
| 786 | discard_cleanups (old_chain); |
| 787 | } |
| 788 | |
| 789 | if (head == NULL) |
| 790 | return svr4_default_sos (); |
| 791 | |
| 792 | return head; |
| 793 | } |
| 794 | |
| 795 | /* Get the address of the link_map for a given OBJFILE. Loop through |
| 796 | the link maps, and return the address of the one corresponding to |
| 797 | the given objfile. Note that this function takes into account that |
| 798 | objfile can be the main executable, not just a shared library. The |
| 799 | main executable has always an empty name field in the linkmap. */ |
| 800 | |
| 801 | CORE_ADDR |
| 802 | svr4_fetch_objfile_link_map (struct objfile *objfile) |
| 803 | { |
| 804 | CORE_ADDR lm; |
| 805 | |
| 806 | if ((debug_base = locate_base ()) == 0) |
| 807 | return 0; /* failed somehow... */ |
| 808 | |
| 809 | /* Position ourselves on the first link map. */ |
| 810 | lm = solib_svr4_r_map (); |
| 811 | while (lm) |
| 812 | { |
| 813 | /* Get info on the layout of the r_debug and link_map structures. */ |
| 814 | struct link_map_offsets *lmo = svr4_fetch_link_map_offsets (); |
| 815 | int errcode; |
| 816 | char *buffer; |
| 817 | struct lm_info objfile_lm_info; |
| 818 | struct cleanup *old_chain; |
| 819 | CORE_ADDR name_address; |
| 820 | int l_name_size = TYPE_LENGTH (builtin_type_void_data_ptr); |
| 821 | gdb_byte *l_name_buf = xmalloc (l_name_size); |
| 822 | old_chain = make_cleanup (xfree, l_name_buf); |
| 823 | |
| 824 | /* Set up the buffer to contain the portion of the link_map |
| 825 | structure that gdb cares about. Note that this is not the |
| 826 | whole link_map structure. */ |
| 827 | objfile_lm_info.lm = xzalloc (lmo->link_map_size); |
| 828 | make_cleanup (xfree, objfile_lm_info.lm); |
| 829 | |
| 830 | /* Read the link map into our internal structure. */ |
| 831 | read_memory (lm, objfile_lm_info.lm, lmo->link_map_size); |
| 832 | |
| 833 | /* Read address of name from target memory to GDB. */ |
| 834 | read_memory (lm + lmo->l_name_offset, l_name_buf, l_name_size); |
| 835 | |
| 836 | /* Extract this object's name. */ |
| 837 | name_address = extract_typed_address (l_name_buf, |
| 838 | builtin_type_void_data_ptr); |
| 839 | target_read_string (name_address, &buffer, |
| 840 | SO_NAME_MAX_PATH_SIZE - 1, &errcode); |
| 841 | make_cleanup (xfree, buffer); |
| 842 | if (errcode != 0) |
| 843 | warning (_("Can't read pathname for load map: %s."), |
| 844 | safe_strerror (errcode)); |
| 845 | else |
| 846 | { |
| 847 | /* Is this the linkmap for the file we want? */ |
| 848 | /* If the file is not a shared library and has no name, |
| 849 | we are sure it is the main executable, so we return that. */ |
| 850 | |
| 851 | if (buffer |
| 852 | && ((strcmp (buffer, objfile->name) == 0) |
| 853 | || (!(objfile->flags & OBJF_SHARED) |
| 854 | && (strcmp (buffer, "") == 0)))) |
| 855 | { |
| 856 | do_cleanups (old_chain); |
| 857 | return lm; |
| 858 | } |
| 859 | } |
| 860 | /* Not the file we wanted, continue checking. */ |
| 861 | lm = extract_typed_address (objfile_lm_info.lm + lmo->l_next_offset, |
| 862 | builtin_type_void_data_ptr); |
| 863 | do_cleanups (old_chain); |
| 864 | } |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /* On some systems, the only way to recognize the link map entry for |
| 869 | the main executable file is by looking at its name. Return |
| 870 | non-zero iff SONAME matches one of the known main executable names. */ |
| 871 | |
| 872 | static int |
| 873 | match_main (char *soname) |
| 874 | { |
| 875 | char **mainp; |
| 876 | |
| 877 | for (mainp = main_name_list; *mainp != NULL; mainp++) |
| 878 | { |
| 879 | if (strcmp (soname, *mainp) == 0) |
| 880 | return (1); |
| 881 | } |
| 882 | |
| 883 | return (0); |
| 884 | } |
| 885 | |
| 886 | /* Return 1 if PC lies in the dynamic symbol resolution code of the |
| 887 | SVR4 run time loader. */ |
| 888 | static CORE_ADDR interp_text_sect_low; |
| 889 | static CORE_ADDR interp_text_sect_high; |
| 890 | static CORE_ADDR interp_plt_sect_low; |
| 891 | static CORE_ADDR interp_plt_sect_high; |
| 892 | |
| 893 | int |
| 894 | svr4_in_dynsym_resolve_code (CORE_ADDR pc) |
| 895 | { |
| 896 | return ((pc >= interp_text_sect_low && pc < interp_text_sect_high) |
| 897 | || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high) |
| 898 | || in_plt_section (pc, NULL)); |
| 899 | } |
| 900 | |
| 901 | /* Given an executable's ABFD and target, compute the entry-point |
| 902 | address. */ |
| 903 | |
| 904 | static CORE_ADDR |
| 905 | exec_entry_point (struct bfd *abfd, struct target_ops *targ) |
| 906 | { |
| 907 | /* KevinB wrote ... for most targets, the address returned by |
| 908 | bfd_get_start_address() is the entry point for the start |
| 909 | function. But, for some targets, bfd_get_start_address() returns |
| 910 | the address of a function descriptor from which the entry point |
| 911 | address may be extracted. This address is extracted by |
| 912 | gdbarch_convert_from_func_ptr_addr(). The method |
| 913 | gdbarch_convert_from_func_ptr_addr() is the merely the identify |
| 914 | function for targets which don't use function descriptors. */ |
| 915 | return gdbarch_convert_from_func_ptr_addr (current_gdbarch, |
| 916 | bfd_get_start_address (abfd), |
| 917 | targ); |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | |
| 922 | LOCAL FUNCTION |
| 923 | |
| 924 | enable_break -- arrange for dynamic linker to hit breakpoint |
| 925 | |
| 926 | SYNOPSIS |
| 927 | |
| 928 | int enable_break (void) |
| 929 | |
| 930 | DESCRIPTION |
| 931 | |
| 932 | Both the SunOS and the SVR4 dynamic linkers have, as part of their |
| 933 | debugger interface, support for arranging for the inferior to hit |
| 934 | a breakpoint after mapping in the shared libraries. This function |
| 935 | enables that breakpoint. |
| 936 | |
| 937 | For SunOS, there is a special flag location (in_debugger) which we |
| 938 | set to 1. When the dynamic linker sees this flag set, it will set |
| 939 | a breakpoint at a location known only to itself, after saving the |
| 940 | original contents of that place and the breakpoint address itself, |
| 941 | in it's own internal structures. When we resume the inferior, it |
| 942 | will eventually take a SIGTRAP when it runs into the breakpoint. |
| 943 | We handle this (in a different place) by restoring the contents of |
| 944 | the breakpointed location (which is only known after it stops), |
| 945 | chasing around to locate the shared libraries that have been |
| 946 | loaded, then resuming. |
| 947 | |
| 948 | For SVR4, the debugger interface structure contains a member (r_brk) |
| 949 | which is statically initialized at the time the shared library is |
| 950 | built, to the offset of a function (_r_debug_state) which is guaran- |
| 951 | teed to be called once before mapping in a library, and again when |
| 952 | the mapping is complete. At the time we are examining this member, |
| 953 | it contains only the unrelocated offset of the function, so we have |
| 954 | to do our own relocation. Later, when the dynamic linker actually |
| 955 | runs, it relocates r_brk to be the actual address of _r_debug_state(). |
| 956 | |
| 957 | The debugger interface structure also contains an enumeration which |
| 958 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, |
| 959 | depending upon whether or not the library is being mapped or unmapped, |
| 960 | and then set to RT_CONSISTENT after the library is mapped/unmapped. |
| 961 | */ |
| 962 | |
| 963 | static int |
| 964 | enable_break (void) |
| 965 | { |
| 966 | #ifdef BKPT_AT_SYMBOL |
| 967 | |
| 968 | struct minimal_symbol *msymbol; |
| 969 | char **bkpt_namep; |
| 970 | asection *interp_sect; |
| 971 | |
| 972 | /* First, remove all the solib event breakpoints. Their addresses |
| 973 | may have changed since the last time we ran the program. */ |
| 974 | remove_solib_event_breakpoints (); |
| 975 | |
| 976 | interp_text_sect_low = interp_text_sect_high = 0; |
| 977 | interp_plt_sect_low = interp_plt_sect_high = 0; |
| 978 | |
| 979 | /* Find the .interp section; if not found, warn the user and drop |
| 980 | into the old breakpoint at symbol code. */ |
| 981 | interp_sect = bfd_get_section_by_name (exec_bfd, ".interp"); |
| 982 | if (interp_sect) |
| 983 | { |
| 984 | unsigned int interp_sect_size; |
| 985 | char *buf; |
| 986 | CORE_ADDR load_addr = 0; |
| 987 | int load_addr_found = 0; |
| 988 | struct so_list *so; |
| 989 | bfd *tmp_bfd = NULL; |
| 990 | struct target_ops *tmp_bfd_target; |
| 991 | int tmp_fd = -1; |
| 992 | char *tmp_pathname = NULL; |
| 993 | CORE_ADDR sym_addr = 0; |
| 994 | |
| 995 | /* Read the contents of the .interp section into a local buffer; |
| 996 | the contents specify the dynamic linker this program uses. */ |
| 997 | interp_sect_size = bfd_section_size (exec_bfd, interp_sect); |
| 998 | buf = alloca (interp_sect_size); |
| 999 | bfd_get_section_contents (exec_bfd, interp_sect, |
| 1000 | buf, 0, interp_sect_size); |
| 1001 | |
| 1002 | /* Now we need to figure out where the dynamic linker was |
| 1003 | loaded so that we can load its symbols and place a breakpoint |
| 1004 | in the dynamic linker itself. |
| 1005 | |
| 1006 | This address is stored on the stack. However, I've been unable |
| 1007 | to find any magic formula to find it for Solaris (appears to |
| 1008 | be trivial on GNU/Linux). Therefore, we have to try an alternate |
| 1009 | mechanism to find the dynamic linker's base address. */ |
| 1010 | |
| 1011 | /* TODO drow/2006-09-12: This is somewhat fragile, because it |
| 1012 | relies on read_pc. On both Solaris and GNU/Linux we can use |
| 1013 | the AT_BASE auxilliary entry, which GDB now knows how to |
| 1014 | access, to find the base address. */ |
| 1015 | |
| 1016 | tmp_fd = solib_open (buf, &tmp_pathname); |
| 1017 | if (tmp_fd >= 0) |
| 1018 | tmp_bfd = bfd_fopen (tmp_pathname, gnutarget, FOPEN_RB, tmp_fd); |
| 1019 | |
| 1020 | if (tmp_bfd == NULL) |
| 1021 | goto bkpt_at_symbol; |
| 1022 | |
| 1023 | /* Make sure the dynamic linker's really a useful object. */ |
| 1024 | if (!bfd_check_format (tmp_bfd, bfd_object)) |
| 1025 | { |
| 1026 | warning (_("Unable to grok dynamic linker %s as an object file"), buf); |
| 1027 | bfd_close (tmp_bfd); |
| 1028 | goto bkpt_at_symbol; |
| 1029 | } |
| 1030 | |
| 1031 | /* Now convert the TMP_BFD into a target. That way target, as |
| 1032 | well as BFD operations can be used. Note that closing the |
| 1033 | target will also close the underlying bfd. */ |
| 1034 | tmp_bfd_target = target_bfd_reopen (tmp_bfd); |
| 1035 | |
| 1036 | /* On a running target, we can get the dynamic linker's base |
| 1037 | address from the shared library table. */ |
| 1038 | solib_add (NULL, 0, ¤t_target, auto_solib_add); |
| 1039 | so = master_so_list (); |
| 1040 | while (so) |
| 1041 | { |
| 1042 | if (strcmp (buf, so->so_original_name) == 0) |
| 1043 | { |
| 1044 | load_addr_found = 1; |
| 1045 | load_addr = LM_ADDR_CHECK (so, tmp_bfd); |
| 1046 | break; |
| 1047 | } |
| 1048 | so = so->next; |
| 1049 | } |
| 1050 | |
| 1051 | /* Otherwise we find the dynamic linker's base address by examining |
| 1052 | the current pc (which should point at the entry point for the |
| 1053 | dynamic linker) and subtracting the offset of the entry point. */ |
| 1054 | if (!load_addr_found) |
| 1055 | { |
| 1056 | load_addr = (read_pc () |
| 1057 | - exec_entry_point (tmp_bfd, tmp_bfd_target)); |
| 1058 | debug_loader_name = xstrdup (buf); |
| 1059 | debug_loader_offset_p = 1; |
| 1060 | debug_loader_offset = load_addr; |
| 1061 | solib_add (NULL, 0, ¤t_target, auto_solib_add); |
| 1062 | } |
| 1063 | |
| 1064 | /* Record the relocated start and end address of the dynamic linker |
| 1065 | text and plt section for svr4_in_dynsym_resolve_code. */ |
| 1066 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".text"); |
| 1067 | if (interp_sect) |
| 1068 | { |
| 1069 | interp_text_sect_low = |
| 1070 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
| 1071 | interp_text_sect_high = |
| 1072 | interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect); |
| 1073 | } |
| 1074 | interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt"); |
| 1075 | if (interp_sect) |
| 1076 | { |
| 1077 | interp_plt_sect_low = |
| 1078 | bfd_section_vma (tmp_bfd, interp_sect) + load_addr; |
| 1079 | interp_plt_sect_high = |
| 1080 | interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect); |
| 1081 | } |
| 1082 | |
| 1083 | /* Now try to set a breakpoint in the dynamic linker. */ |
| 1084 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) |
| 1085 | { |
| 1086 | sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep); |
| 1087 | if (sym_addr != 0) |
| 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | if (sym_addr != 0) |
| 1092 | /* Convert 'sym_addr' from a function pointer to an address. |
| 1093 | Because we pass tmp_bfd_target instead of the current |
| 1094 | target, this will always produce an unrelocated value. */ |
| 1095 | sym_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch, |
| 1096 | sym_addr, |
| 1097 | tmp_bfd_target); |
| 1098 | |
| 1099 | /* We're done with both the temporary bfd and target. Remember, |
| 1100 | closing the target closes the underlying bfd. */ |
| 1101 | target_close (tmp_bfd_target, 0); |
| 1102 | |
| 1103 | if (sym_addr != 0) |
| 1104 | { |
| 1105 | create_solib_event_breakpoint (load_addr + sym_addr); |
| 1106 | return 1; |
| 1107 | } |
| 1108 | |
| 1109 | /* For whatever reason we couldn't set a breakpoint in the dynamic |
| 1110 | linker. Warn and drop into the old code. */ |
| 1111 | bkpt_at_symbol: |
| 1112 | xfree (tmp_pathname); |
| 1113 | warning (_("Unable to find dynamic linker breakpoint function.\n" |
| 1114 | "GDB will be unable to debug shared library initializers\n" |
| 1115 | "and track explicitly loaded dynamic code.")); |
| 1116 | } |
| 1117 | |
| 1118 | /* Scan through the lists of symbols, trying to look up the symbol and |
| 1119 | set a breakpoint there. Terminate loop when we/if we succeed. */ |
| 1120 | |
| 1121 | for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++) |
| 1122 | { |
| 1123 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile); |
| 1124 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) |
| 1125 | { |
| 1126 | create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol)); |
| 1127 | return 1; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++) |
| 1132 | { |
| 1133 | msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile); |
| 1134 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) |
| 1135 | { |
| 1136 | create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol)); |
| 1137 | return 1; |
| 1138 | } |
| 1139 | } |
| 1140 | #endif /* BKPT_AT_SYMBOL */ |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | |
| 1147 | LOCAL FUNCTION |
| 1148 | |
| 1149 | special_symbol_handling -- additional shared library symbol handling |
| 1150 | |
| 1151 | SYNOPSIS |
| 1152 | |
| 1153 | void special_symbol_handling () |
| 1154 | |
| 1155 | DESCRIPTION |
| 1156 | |
| 1157 | Once the symbols from a shared object have been loaded in the usual |
| 1158 | way, we are called to do any system specific symbol handling that |
| 1159 | is needed. |
| 1160 | |
| 1161 | For SunOS4, this consisted of grunging around in the dynamic |
| 1162 | linkers structures to find symbol definitions for "common" symbols |
| 1163 | and adding them to the minimal symbol table for the runtime common |
| 1164 | objfile. |
| 1165 | |
| 1166 | However, for SVR4, there's nothing to do. |
| 1167 | |
| 1168 | */ |
| 1169 | |
| 1170 | static void |
| 1171 | svr4_special_symbol_handling (void) |
| 1172 | { |
| 1173 | } |
| 1174 | |
| 1175 | /* Relocate the main executable. This function should be called upon |
| 1176 | stopping the inferior process at the entry point to the program. |
| 1177 | The entry point from BFD is compared to the PC and if they are |
| 1178 | different, the main executable is relocated by the proper amount. |
| 1179 | |
| 1180 | As written it will only attempt to relocate executables which |
| 1181 | lack interpreter sections. It seems likely that only dynamic |
| 1182 | linker executables will get relocated, though it should work |
| 1183 | properly for a position-independent static executable as well. */ |
| 1184 | |
| 1185 | static void |
| 1186 | svr4_relocate_main_executable (void) |
| 1187 | { |
| 1188 | asection *interp_sect; |
| 1189 | CORE_ADDR pc = read_pc (); |
| 1190 | |
| 1191 | /* Decide if the objfile needs to be relocated. As indicated above, |
| 1192 | we will only be here when execution is stopped at the beginning |
| 1193 | of the program. Relocation is necessary if the address at which |
| 1194 | we are presently stopped differs from the start address stored in |
| 1195 | the executable AND there's no interpreter section. The condition |
| 1196 | regarding the interpreter section is very important because if |
| 1197 | there *is* an interpreter section, execution will begin there |
| 1198 | instead. When there is an interpreter section, the start address |
| 1199 | is (presumably) used by the interpreter at some point to start |
| 1200 | execution of the program. |
| 1201 | |
| 1202 | If there is an interpreter, it is normal for it to be set to an |
| 1203 | arbitrary address at the outset. The job of finding it is |
| 1204 | handled in enable_break(). |
| 1205 | |
| 1206 | So, to summarize, relocations are necessary when there is no |
| 1207 | interpreter section and the start address obtained from the |
| 1208 | executable is different from the address at which GDB is |
| 1209 | currently stopped. |
| 1210 | |
| 1211 | [ The astute reader will note that we also test to make sure that |
| 1212 | the executable in question has the DYNAMIC flag set. It is my |
| 1213 | opinion that this test is unnecessary (undesirable even). It |
| 1214 | was added to avoid inadvertent relocation of an executable |
| 1215 | whose e_type member in the ELF header is not ET_DYN. There may |
| 1216 | be a time in the future when it is desirable to do relocations |
| 1217 | on other types of files as well in which case this condition |
| 1218 | should either be removed or modified to accomodate the new file |
| 1219 | type. (E.g, an ET_EXEC executable which has been built to be |
| 1220 | position-independent could safely be relocated by the OS if |
| 1221 | desired. It is true that this violates the ABI, but the ABI |
| 1222 | has been known to be bent from time to time.) - Kevin, Nov 2000. ] |
| 1223 | */ |
| 1224 | |
| 1225 | interp_sect = bfd_get_section_by_name (exec_bfd, ".interp"); |
| 1226 | if (interp_sect == NULL |
| 1227 | && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0 |
| 1228 | && (exec_entry_point (exec_bfd, &exec_ops) != pc)) |
| 1229 | { |
| 1230 | struct cleanup *old_chain; |
| 1231 | struct section_offsets *new_offsets; |
| 1232 | int i, changed; |
| 1233 | CORE_ADDR displacement; |
| 1234 | |
| 1235 | /* It is necessary to relocate the objfile. The amount to |
| 1236 | relocate by is simply the address at which we are stopped |
| 1237 | minus the starting address from the executable. |
| 1238 | |
| 1239 | We relocate all of the sections by the same amount. This |
| 1240 | behavior is mandated by recent editions of the System V ABI. |
| 1241 | According to the System V Application Binary Interface, |
| 1242 | Edition 4.1, page 5-5: |
| 1243 | |
| 1244 | ... Though the system chooses virtual addresses for |
| 1245 | individual processes, it maintains the segments' relative |
| 1246 | positions. Because position-independent code uses relative |
| 1247 | addressesing between segments, the difference between |
| 1248 | virtual addresses in memory must match the difference |
| 1249 | between virtual addresses in the file. The difference |
| 1250 | between the virtual address of any segment in memory and |
| 1251 | the corresponding virtual address in the file is thus a |
| 1252 | single constant value for any one executable or shared |
| 1253 | object in a given process. This difference is the base |
| 1254 | address. One use of the base address is to relocate the |
| 1255 | memory image of the program during dynamic linking. |
| 1256 | |
| 1257 | The same language also appears in Edition 4.0 of the System V |
| 1258 | ABI and is left unspecified in some of the earlier editions. */ |
| 1259 | |
| 1260 | displacement = pc - exec_entry_point (exec_bfd, &exec_ops); |
| 1261 | changed = 0; |
| 1262 | |
| 1263 | new_offsets = xcalloc (symfile_objfile->num_sections, |
| 1264 | sizeof (struct section_offsets)); |
| 1265 | old_chain = make_cleanup (xfree, new_offsets); |
| 1266 | |
| 1267 | for (i = 0; i < symfile_objfile->num_sections; i++) |
| 1268 | { |
| 1269 | if (displacement != ANOFFSET (symfile_objfile->section_offsets, i)) |
| 1270 | changed = 1; |
| 1271 | new_offsets->offsets[i] = displacement; |
| 1272 | } |
| 1273 | |
| 1274 | if (changed) |
| 1275 | objfile_relocate (symfile_objfile, new_offsets); |
| 1276 | |
| 1277 | do_cleanups (old_chain); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | |
| 1283 | GLOBAL FUNCTION |
| 1284 | |
| 1285 | svr4_solib_create_inferior_hook -- shared library startup support |
| 1286 | |
| 1287 | SYNOPSIS |
| 1288 | |
| 1289 | void svr4_solib_create_inferior_hook () |
| 1290 | |
| 1291 | DESCRIPTION |
| 1292 | |
| 1293 | When gdb starts up the inferior, it nurses it along (through the |
| 1294 | shell) until it is ready to execute it's first instruction. At this |
| 1295 | point, this function gets called via expansion of the macro |
| 1296 | SOLIB_CREATE_INFERIOR_HOOK. |
| 1297 | |
| 1298 | For SunOS executables, this first instruction is typically the |
| 1299 | one at "_start", or a similar text label, regardless of whether |
| 1300 | the executable is statically or dynamically linked. The runtime |
| 1301 | startup code takes care of dynamically linking in any shared |
| 1302 | libraries, once gdb allows the inferior to continue. |
| 1303 | |
| 1304 | For SVR4 executables, this first instruction is either the first |
| 1305 | instruction in the dynamic linker (for dynamically linked |
| 1306 | executables) or the instruction at "start" for statically linked |
| 1307 | executables. For dynamically linked executables, the system |
| 1308 | first exec's /lib/libc.so.N, which contains the dynamic linker, |
| 1309 | and starts it running. The dynamic linker maps in any needed |
| 1310 | shared libraries, maps in the actual user executable, and then |
| 1311 | jumps to "start" in the user executable. |
| 1312 | |
| 1313 | For both SunOS shared libraries, and SVR4 shared libraries, we |
| 1314 | can arrange to cooperate with the dynamic linker to discover the |
| 1315 | names of shared libraries that are dynamically linked, and the |
| 1316 | base addresses to which they are linked. |
| 1317 | |
| 1318 | This function is responsible for discovering those names and |
| 1319 | addresses, and saving sufficient information about them to allow |
| 1320 | their symbols to be read at a later time. |
| 1321 | |
| 1322 | FIXME |
| 1323 | |
| 1324 | Between enable_break() and disable_break(), this code does not |
| 1325 | properly handle hitting breakpoints which the user might have |
| 1326 | set in the startup code or in the dynamic linker itself. Proper |
| 1327 | handling will probably have to wait until the implementation is |
| 1328 | changed to use the "breakpoint handler function" method. |
| 1329 | |
| 1330 | Also, what if child has exit()ed? Must exit loop somehow. |
| 1331 | */ |
| 1332 | |
| 1333 | static void |
| 1334 | svr4_solib_create_inferior_hook (void) |
| 1335 | { |
| 1336 | /* Relocate the main executable if necessary. */ |
| 1337 | svr4_relocate_main_executable (); |
| 1338 | |
| 1339 | if (!svr4_have_link_map_offsets ()) |
| 1340 | return; |
| 1341 | |
| 1342 | if (!enable_break ()) |
| 1343 | return; |
| 1344 | |
| 1345 | #if defined(_SCO_DS) |
| 1346 | /* SCO needs the loop below, other systems should be using the |
| 1347 | special shared library breakpoints and the shared library breakpoint |
| 1348 | service routine. |
| 1349 | |
| 1350 | Now run the target. It will eventually hit the breakpoint, at |
| 1351 | which point all of the libraries will have been mapped in and we |
| 1352 | can go groveling around in the dynamic linker structures to find |
| 1353 | out what we need to know about them. */ |
| 1354 | |
| 1355 | clear_proceed_status (); |
| 1356 | stop_soon = STOP_QUIETLY; |
| 1357 | stop_signal = TARGET_SIGNAL_0; |
| 1358 | do |
| 1359 | { |
| 1360 | target_resume (pid_to_ptid (-1), 0, stop_signal); |
| 1361 | wait_for_inferior (); |
| 1362 | } |
| 1363 | while (stop_signal != TARGET_SIGNAL_TRAP); |
| 1364 | stop_soon = NO_STOP_QUIETLY; |
| 1365 | #endif /* defined(_SCO_DS) */ |
| 1366 | } |
| 1367 | |
| 1368 | static void |
| 1369 | svr4_clear_solib (void) |
| 1370 | { |
| 1371 | debug_base = 0; |
| 1372 | debug_loader_offset_p = 0; |
| 1373 | debug_loader_offset = 0; |
| 1374 | xfree (debug_loader_name); |
| 1375 | debug_loader_name = NULL; |
| 1376 | } |
| 1377 | |
| 1378 | static void |
| 1379 | svr4_free_so (struct so_list *so) |
| 1380 | { |
| 1381 | xfree (so->lm_info->lm); |
| 1382 | xfree (so->lm_info); |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | /* Clear any bits of ADDR that wouldn't fit in a target-format |
| 1387 | data pointer. "Data pointer" here refers to whatever sort of |
| 1388 | address the dynamic linker uses to manage its sections. At the |
| 1389 | moment, we don't support shared libraries on any processors where |
| 1390 | code and data pointers are different sizes. |
| 1391 | |
| 1392 | This isn't really the right solution. What we really need here is |
| 1393 | a way to do arithmetic on CORE_ADDR values that respects the |
| 1394 | natural pointer/address correspondence. (For example, on the MIPS, |
| 1395 | converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to |
| 1396 | sign-extend the value. There, simply truncating the bits above |
| 1397 | gdbarch_ptr_bit, as we do below, is no good.) This should probably |
| 1398 | be a new gdbarch method or something. */ |
| 1399 | static CORE_ADDR |
| 1400 | svr4_truncate_ptr (CORE_ADDR addr) |
| 1401 | { |
| 1402 | if (gdbarch_ptr_bit (current_gdbarch) == sizeof (CORE_ADDR) * 8) |
| 1403 | /* We don't need to truncate anything, and the bit twiddling below |
| 1404 | will fail due to overflow problems. */ |
| 1405 | return addr; |
| 1406 | else |
| 1407 | return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (current_gdbarch)) - 1); |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | static void |
| 1412 | svr4_relocate_section_addresses (struct so_list *so, |
| 1413 | struct section_table *sec) |
| 1414 | { |
| 1415 | sec->addr = svr4_truncate_ptr (sec->addr + LM_ADDR_CHECK (so, |
| 1416 | sec->bfd)); |
| 1417 | sec->endaddr = svr4_truncate_ptr (sec->endaddr + LM_ADDR_CHECK (so, |
| 1418 | sec->bfd)); |
| 1419 | } |
| 1420 | \f |
| 1421 | |
| 1422 | /* Architecture-specific operations. */ |
| 1423 | |
| 1424 | /* Per-architecture data key. */ |
| 1425 | static struct gdbarch_data *solib_svr4_data; |
| 1426 | |
| 1427 | struct solib_svr4_ops |
| 1428 | { |
| 1429 | /* Return a description of the layout of `struct link_map'. */ |
| 1430 | struct link_map_offsets *(*fetch_link_map_offsets)(void); |
| 1431 | }; |
| 1432 | |
| 1433 | /* Return a default for the architecture-specific operations. */ |
| 1434 | |
| 1435 | static void * |
| 1436 | solib_svr4_init (struct obstack *obstack) |
| 1437 | { |
| 1438 | struct solib_svr4_ops *ops; |
| 1439 | |
| 1440 | ops = OBSTACK_ZALLOC (obstack, struct solib_svr4_ops); |
| 1441 | ops->fetch_link_map_offsets = legacy_svr4_fetch_link_map_offsets_hook; |
| 1442 | return ops; |
| 1443 | } |
| 1444 | |
| 1445 | /* Set the architecture-specific `struct link_map_offsets' fetcher for |
| 1446 | GDBARCH to FLMO. */ |
| 1447 | |
| 1448 | void |
| 1449 | set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch, |
| 1450 | struct link_map_offsets *(*flmo) (void)) |
| 1451 | { |
| 1452 | struct solib_svr4_ops *ops = gdbarch_data (gdbarch, solib_svr4_data); |
| 1453 | |
| 1454 | ops->fetch_link_map_offsets = flmo; |
| 1455 | } |
| 1456 | |
| 1457 | /* Fetch a link_map_offsets structure using the architecture-specific |
| 1458 | `struct link_map_offsets' fetcher. */ |
| 1459 | |
| 1460 | static struct link_map_offsets * |
| 1461 | svr4_fetch_link_map_offsets (void) |
| 1462 | { |
| 1463 | struct solib_svr4_ops *ops = gdbarch_data (current_gdbarch, solib_svr4_data); |
| 1464 | |
| 1465 | gdb_assert (ops->fetch_link_map_offsets); |
| 1466 | return ops->fetch_link_map_offsets (); |
| 1467 | } |
| 1468 | |
| 1469 | /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */ |
| 1470 | |
| 1471 | static int |
| 1472 | svr4_have_link_map_offsets (void) |
| 1473 | { |
| 1474 | struct solib_svr4_ops *ops = gdbarch_data (current_gdbarch, solib_svr4_data); |
| 1475 | return (ops->fetch_link_map_offsets != NULL); |
| 1476 | } |
| 1477 | \f |
| 1478 | |
| 1479 | /* Most OS'es that have SVR4-style ELF dynamic libraries define a |
| 1480 | `struct r_debug' and a `struct link_map' that are binary compatible |
| 1481 | with the origional SVR4 implementation. */ |
| 1482 | |
| 1483 | /* Fetch (and possibly build) an appropriate `struct link_map_offsets' |
| 1484 | for an ILP32 SVR4 system. */ |
| 1485 | |
| 1486 | struct link_map_offsets * |
| 1487 | svr4_ilp32_fetch_link_map_offsets (void) |
| 1488 | { |
| 1489 | static struct link_map_offsets lmo; |
| 1490 | static struct link_map_offsets *lmp = NULL; |
| 1491 | |
| 1492 | if (lmp == NULL) |
| 1493 | { |
| 1494 | lmp = &lmo; |
| 1495 | |
| 1496 | lmo.r_version_offset = 0; |
| 1497 | lmo.r_version_size = 4; |
| 1498 | lmo.r_map_offset = 4; |
| 1499 | lmo.r_ldsomap_offset = 20; |
| 1500 | |
| 1501 | /* Everything we need is in the first 20 bytes. */ |
| 1502 | lmo.link_map_size = 20; |
| 1503 | lmo.l_addr_offset = 0; |
| 1504 | lmo.l_name_offset = 4; |
| 1505 | lmo.l_ld_offset = 8; |
| 1506 | lmo.l_next_offset = 12; |
| 1507 | lmo.l_prev_offset = 16; |
| 1508 | } |
| 1509 | |
| 1510 | return lmp; |
| 1511 | } |
| 1512 | |
| 1513 | /* Fetch (and possibly build) an appropriate `struct link_map_offsets' |
| 1514 | for an LP64 SVR4 system. */ |
| 1515 | |
| 1516 | struct link_map_offsets * |
| 1517 | svr4_lp64_fetch_link_map_offsets (void) |
| 1518 | { |
| 1519 | static struct link_map_offsets lmo; |
| 1520 | static struct link_map_offsets *lmp = NULL; |
| 1521 | |
| 1522 | if (lmp == NULL) |
| 1523 | { |
| 1524 | lmp = &lmo; |
| 1525 | |
| 1526 | lmo.r_version_offset = 0; |
| 1527 | lmo.r_version_size = 4; |
| 1528 | lmo.r_map_offset = 8; |
| 1529 | lmo.r_ldsomap_offset = 40; |
| 1530 | |
| 1531 | /* Everything we need is in the first 40 bytes. */ |
| 1532 | lmo.link_map_size = 40; |
| 1533 | lmo.l_addr_offset = 0; |
| 1534 | lmo.l_name_offset = 8; |
| 1535 | lmo.l_ld_offset = 16; |
| 1536 | lmo.l_next_offset = 24; |
| 1537 | lmo.l_prev_offset = 32; |
| 1538 | } |
| 1539 | |
| 1540 | return lmp; |
| 1541 | } |
| 1542 | \f |
| 1543 | |
| 1544 | struct target_so_ops svr4_so_ops; |
| 1545 | |
| 1546 | /* Lookup global symbol for ELF DSOs linked with -Bsymbolic. Those DSOs have a |
| 1547 | different rule for symbol lookup. The lookup begins here in the DSO, not in |
| 1548 | the main executable. */ |
| 1549 | |
| 1550 | static struct symbol * |
| 1551 | elf_lookup_lib_symbol (const struct objfile *objfile, |
| 1552 | const char *name, |
| 1553 | const char *linkage_name, |
| 1554 | const domain_enum domain, struct symtab **symtab) |
| 1555 | { |
| 1556 | if (objfile->obfd == NULL |
| 1557 | || scan_dyntag (DT_SYMBOLIC, objfile->obfd, NULL) != 1) |
| 1558 | return NULL; |
| 1559 | |
| 1560 | return lookup_global_symbol_from_objfile |
| 1561 | (objfile, name, linkage_name, domain, symtab); |
| 1562 | } |
| 1563 | |
| 1564 | extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */ |
| 1565 | |
| 1566 | void |
| 1567 | _initialize_svr4_solib (void) |
| 1568 | { |
| 1569 | solib_svr4_data = gdbarch_data_register_pre_init (solib_svr4_init); |
| 1570 | |
| 1571 | svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses; |
| 1572 | svr4_so_ops.free_so = svr4_free_so; |
| 1573 | svr4_so_ops.clear_solib = svr4_clear_solib; |
| 1574 | svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook; |
| 1575 | svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling; |
| 1576 | svr4_so_ops.current_sos = svr4_current_sos; |
| 1577 | svr4_so_ops.open_symbol_file_object = open_symbol_file_object; |
| 1578 | svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code; |
| 1579 | svr4_so_ops.lookup_lib_global_symbol = elf_lookup_lib_symbol; |
| 1580 | |
| 1581 | /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */ |
| 1582 | current_target_so_ops = &svr4_so_ops; |
| 1583 | } |