1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger. 2 Copyright (C) 2010-2020 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 20 #include "defs.h" 21 #include "inferior.h" 22 #include "gdbcore.h" 23 #include "solib.h" 24 #include "solist.h" 25 #include "objfiles.h" 26 #include "symtab.h" 27 #include "language.h" 28 #include "command.h" 29 #include "gdbcmd.h" 30 #include "elf-bfd.h" 31 #include "gdb_bfd.h" 32 33 #define GOT_MODULE_OFFSET 4 34 35 /* Flag which indicates whether internal debug messages should be printed. */ 36 static unsigned int solib_dsbt_debug = 0; 37 38 /* TIC6X pointers are four bytes wide. */ 39 enum { TIC6X_PTR_SIZE = 4 }; 40 41 /* Representation of loadmap and related structs for the TIC6X DSBT. */ 42 43 /* External versions; the size and alignment of the fields should be 44 the same as those on the target. When loaded, the placement of 45 the bits in each field will be the same as on the target. */ 46 typedef gdb_byte ext_Elf32_Half[2]; 47 typedef gdb_byte ext_Elf32_Addr[4]; 48 typedef gdb_byte ext_Elf32_Word[4]; 49 50 struct ext_elf32_dsbt_loadseg 51 { 52 /* Core address to which the segment is mapped. */ 53 ext_Elf32_Addr addr; 54 /* VMA recorded in the program header. */ 55 ext_Elf32_Addr p_vaddr; 56 /* Size of this segment in memory. */ 57 ext_Elf32_Word p_memsz; 58 }; 59 60 struct ext_elf32_dsbt_loadmap { 61 /* Protocol version number, must be zero. */ 62 ext_Elf32_Word version; 63 /* A pointer to the DSBT table; the DSBT size and the index of this 64 module. */ 65 ext_Elf32_Word dsbt_table_ptr; 66 ext_Elf32_Word dsbt_size; 67 ext_Elf32_Word dsbt_index; 68 /* Number of segments in this map. */ 69 ext_Elf32_Word nsegs; 70 /* The actual memory map. */ 71 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */]; 72 }; 73 74 /* Internal versions; the types are GDB types and the data in each 75 of the fields is (or will be) decoded from the external struct 76 for ease of consumption. */ 77 struct int_elf32_dsbt_loadseg 78 { 79 /* Core address to which the segment is mapped. */ 80 CORE_ADDR addr; 81 /* VMA recorded in the program header. */ 82 CORE_ADDR p_vaddr; 83 /* Size of this segment in memory. */ 84 long p_memsz; 85 }; 86 87 struct int_elf32_dsbt_loadmap 88 { 89 /* Protocol version number, must be zero. */ 90 int version; 91 CORE_ADDR dsbt_table_ptr; 92 /* A pointer to the DSBT table; the DSBT size and the index of this 93 module. */ 94 int dsbt_size, dsbt_index; 95 /* Number of segments in this map. */ 96 int nsegs; 97 /* The actual memory map. */ 98 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */]; 99 }; 100 101 /* External link_map and elf32_dsbt_loadaddr struct definitions. */ 102 103 typedef gdb_byte ext_ptr[4]; 104 105 struct ext_elf32_dsbt_loadaddr 106 { 107 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */ 108 }; 109 110 struct ext_link_map 111 { 112 struct ext_elf32_dsbt_loadaddr l_addr; 113 114 /* Absolute file name object was found in. */ 115 ext_ptr l_name; /* char *l_name; */ 116 117 /* Dynamic section of the shared object. */ 118 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */ 119 120 /* Chain of loaded objects. */ 121 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */ 122 }; 123 124 /* Link map info to include in an allocated so_list entry */ 125 126 struct lm_info_dsbt : public lm_info_base 127 { 128 ~lm_info_dsbt () 129 { 130 xfree (this->map); 131 } 132 133 /* The loadmap, digested into an easier to use form. */ 134 int_elf32_dsbt_loadmap *map = NULL; 135 }; 136 137 /* Per pspace dsbt specific data. */ 138 139 struct dsbt_info 140 { 141 /* The load map, got value, etc. are not available from the chain 142 of loaded shared objects. ``main_executable_lm_info'' provides 143 a way to get at this information so that it doesn't need to be 144 frequently recomputed. Initialized by dsbt_relocate_main_executable. */ 145 struct lm_info_dsbt *main_executable_lm_info = nullptr; 146 147 /* Load maps for the main executable and the interpreter. These are obtained 148 from ptrace. They are the starting point for getting into the program, 149 and are required to find the solib list with the individual load maps for 150 each module. */ 151 struct int_elf32_dsbt_loadmap *exec_loadmap = nullptr; 152 struct int_elf32_dsbt_loadmap *interp_loadmap = nullptr; 153 154 /* Cached value for lm_base, below. */ 155 CORE_ADDR lm_base_cache = 0; 156 157 /* Link map address for main module. */ 158 CORE_ADDR main_lm_addr = 0; 159 160 CORE_ADDR interp_text_sect_low = 0; 161 CORE_ADDR interp_text_sect_high = 0; 162 CORE_ADDR interp_plt_sect_low = 0; 163 CORE_ADDR interp_plt_sect_high = 0; 164 }; 165 166 /* Per-program-space data key. */ 167 static program_space_key<dsbt_info> solib_dsbt_pspace_data; 168 169 /* Get the current dsbt data. If none is found yet, add it now. This 170 function always returns a valid object. */ 171 172 static struct dsbt_info * 173 get_dsbt_info (void) 174 { 175 struct dsbt_info *info; 176 177 info = solib_dsbt_pspace_data.get (current_program_space); 178 if (info != NULL) 179 return info; 180 181 return solib_dsbt_pspace_data.emplace (current_program_space); 182 } 183 184 185 static void 186 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map) 187 { 188 int i; 189 190 if (map == NULL) 191 printf_filtered ("(null)\n"); 192 else if (map->version != 0) 193 printf_filtered (_("Unsupported map version: %d\n"), map->version); 194 else 195 { 196 printf_filtered ("version %d\n", map->version); 197 198 for (i = 0; i < map->nsegs; i++) 199 printf_filtered ("%s:%s -> %s:%s\n", 200 print_core_address (target_gdbarch (), 201 map->segs[i].p_vaddr), 202 print_core_address (target_gdbarch (), 203 map->segs[i].p_vaddr 204 + map->segs[i].p_memsz), 205 print_core_address (target_gdbarch (), map->segs[i].addr), 206 print_core_address (target_gdbarch (), map->segs[i].addr 207 + map->segs[i].p_memsz)); 208 } 209 } 210 211 /* Decode int_elf32_dsbt_loadmap from BUF. */ 212 213 static struct int_elf32_dsbt_loadmap * 214 decode_loadmap (const gdb_byte *buf) 215 { 216 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); 217 const struct ext_elf32_dsbt_loadmap *ext_ldmbuf; 218 struct int_elf32_dsbt_loadmap *int_ldmbuf; 219 220 int version, seg, nsegs; 221 int int_ldmbuf_size; 222 223 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf; 224 225 /* Extract the version. */ 226 version = extract_unsigned_integer (ext_ldmbuf->version, 227 sizeof ext_ldmbuf->version, 228 byte_order); 229 if (version != 0) 230 { 231 /* We only handle version 0. */ 232 return NULL; 233 } 234 235 /* Extract the number of segments. */ 236 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs, 237 sizeof ext_ldmbuf->nsegs, 238 byte_order); 239 240 if (nsegs <= 0) 241 return NULL; 242 243 /* Allocate space into which to put information extract from the 244 external loadsegs. I.e, allocate the internal loadsegs. */ 245 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap) 246 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg)); 247 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size); 248 249 /* Place extracted information in internal structs. */ 250 int_ldmbuf->version = version; 251 int_ldmbuf->nsegs = nsegs; 252 for (seg = 0; seg < nsegs; seg++) 253 { 254 int_ldmbuf->segs[seg].addr 255 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr, 256 sizeof (ext_ldmbuf->segs[seg].addr), 257 byte_order); 258 int_ldmbuf->segs[seg].p_vaddr 259 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr, 260 sizeof (ext_ldmbuf->segs[seg].p_vaddr), 261 byte_order); 262 int_ldmbuf->segs[seg].p_memsz 263 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz, 264 sizeof (ext_ldmbuf->segs[seg].p_memsz), 265 byte_order); 266 } 267 268 return int_ldmbuf; 269 } 270 271 272 static struct dsbt_info *get_dsbt_info (void); 273 274 /* Interrogate the Linux kernel to find out where the program was loaded. 275 There are two load maps; one for the executable and one for the 276 interpreter (only in the case of a dynamically linked executable). */ 277 278 static void 279 dsbt_get_initial_loadmaps (void) 280 { 281 struct dsbt_info *info = get_dsbt_info (); 282 gdb::optional<gdb::byte_vector> buf 283 = target_read_alloc (current_top_target (), TARGET_OBJECT_FDPIC, "exec"); 284 285 if (!buf || buf->empty ()) 286 { 287 info->exec_loadmap = NULL; 288 error (_("Error reading DSBT exec loadmap")); 289 } 290 info->exec_loadmap = decode_loadmap (buf->data ()); 291 if (solib_dsbt_debug) 292 dsbt_print_loadmap (info->exec_loadmap); 293 294 buf = target_read_alloc (current_top_target (), TARGET_OBJECT_FDPIC, "exec"); 295 if (!buf || buf->empty ()) 296 { 297 info->interp_loadmap = NULL; 298 error (_("Error reading DSBT interp loadmap")); 299 } 300 info->interp_loadmap = decode_loadmap (buf->data ()); 301 if (solib_dsbt_debug) 302 dsbt_print_loadmap (info->interp_loadmap); 303 } 304 305 /* Given address LDMADDR, fetch and decode the loadmap at that address. 306 Return NULL if there is a problem reading the target memory or if 307 there doesn't appear to be a loadmap at the given address. The 308 allocated space (representing the loadmap) returned by this 309 function may be freed via a single call to xfree. */ 310 311 static struct int_elf32_dsbt_loadmap * 312 fetch_loadmap (CORE_ADDR ldmaddr) 313 { 314 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); 315 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial; 316 struct ext_elf32_dsbt_loadmap *ext_ldmbuf; 317 struct int_elf32_dsbt_loadmap *int_ldmbuf; 318 int ext_ldmbuf_size, int_ldmbuf_size; 319 int version, seg, nsegs; 320 321 /* Fetch initial portion of the loadmap. */ 322 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial, 323 sizeof ext_ldmbuf_partial)) 324 { 325 /* Problem reading the target's memory. */ 326 return NULL; 327 } 328 329 /* Extract the version. */ 330 version = extract_unsigned_integer (ext_ldmbuf_partial.version, 331 sizeof ext_ldmbuf_partial.version, 332 byte_order); 333 if (version != 0) 334 { 335 /* We only handle version 0. */ 336 return NULL; 337 } 338 339 /* Extract the number of segments. */ 340 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs, 341 sizeof ext_ldmbuf_partial.nsegs, 342 byte_order); 343 344 if (nsegs <= 0) 345 return NULL; 346 347 /* Allocate space for the complete (external) loadmap. */ 348 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap) 349 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg); 350 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size); 351 352 /* Copy over the portion of the loadmap that's already been read. */ 353 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial); 354 355 /* Read the rest of the loadmap from the target. */ 356 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial, 357 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial, 358 ext_ldmbuf_size - sizeof ext_ldmbuf_partial)) 359 { 360 /* Couldn't read rest of the loadmap. */ 361 xfree (ext_ldmbuf); 362 return NULL; 363 } 364 365 /* Allocate space into which to put information extract from the 366 external loadsegs. I.e, allocate the internal loadsegs. */ 367 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap) 368 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg); 369 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size); 370 371 /* Place extracted information in internal structs. */ 372 int_ldmbuf->version = version; 373 int_ldmbuf->nsegs = nsegs; 374 for (seg = 0; seg < nsegs; seg++) 375 { 376 int_ldmbuf->segs[seg].addr 377 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr, 378 sizeof (ext_ldmbuf->segs[seg].addr), 379 byte_order); 380 int_ldmbuf->segs[seg].p_vaddr 381 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr, 382 sizeof (ext_ldmbuf->segs[seg].p_vaddr), 383 byte_order); 384 int_ldmbuf->segs[seg].p_memsz 385 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz, 386 sizeof (ext_ldmbuf->segs[seg].p_memsz), 387 byte_order); 388 } 389 390 xfree (ext_ldmbuf); 391 return int_ldmbuf; 392 } 393 394 static void dsbt_relocate_main_executable (void); 395 static int enable_break (void); 396 397 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is 398 returned and the corresponding PTR is set. */ 399 400 static int 401 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr) 402 { 403 int arch_size, step, sect_size; 404 long dyn_tag; 405 CORE_ADDR dyn_ptr, dyn_addr; 406 gdb_byte *bufend, *bufstart, *buf; 407 Elf32_External_Dyn *x_dynp_32; 408 Elf64_External_Dyn *x_dynp_64; 409 struct bfd_section *sect; 410 struct target_section *target_section; 411 412 if (abfd == NULL) 413 return 0; 414 415 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 416 return 0; 417 418 arch_size = bfd_get_arch_size (abfd); 419 if (arch_size == -1) 420 return 0; 421 422 /* Find the start address of the .dynamic section. */ 423 sect = bfd_get_section_by_name (abfd, ".dynamic"); 424 if (sect == NULL) 425 return 0; 426 427 for (target_section = current_target_sections->sections; 428 target_section < current_target_sections->sections_end; 429 target_section++) 430 if (sect == target_section->the_bfd_section) 431 break; 432 if (target_section < current_target_sections->sections_end) 433 dyn_addr = target_section->addr; 434 else 435 { 436 /* ABFD may come from OBJFILE acting only as a symbol file without being 437 loaded into the target (see add_symbol_file_command). This case is 438 such fallback to the file VMA address without the possibility of 439 having the section relocated to its actual in-memory address. */ 440 441 dyn_addr = bfd_section_vma (sect); 442 } 443 444 /* Read in .dynamic from the BFD. We will get the actual value 445 from memory later. */ 446 sect_size = bfd_section_size (sect); 447 buf = bufstart = (gdb_byte *) alloca (sect_size); 448 if (!bfd_get_section_contents (abfd, sect, 449 buf, 0, sect_size)) 450 return 0; 451 452 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */ 453 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn) 454 : sizeof (Elf64_External_Dyn); 455 for (bufend = buf + sect_size; 456 buf < bufend; 457 buf += step) 458 { 459 if (arch_size == 32) 460 { 461 x_dynp_32 = (Elf32_External_Dyn *) buf; 462 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag); 463 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr); 464 } 465 else 466 { 467 x_dynp_64 = (Elf64_External_Dyn *) buf; 468 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag); 469 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr); 470 } 471 if (dyn_tag == DT_NULL) 472 return 0; 473 if (dyn_tag == dyntag) 474 { 475 /* If requested, try to read the runtime value of this .dynamic 476 entry. */ 477 if (ptr) 478 { 479 struct type *ptr_type; 480 gdb_byte ptr_buf[8]; 481 CORE_ADDR ptr_addr; 482 483 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; 484 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8; 485 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0) 486 dyn_ptr = extract_typed_address (ptr_buf, ptr_type); 487 *ptr = dyn_ptr; 488 } 489 return 1; 490 } 491 } 492 493 return 0; 494 } 495 496 /* See solist.h. */ 497 498 static int 499 open_symbol_file_object (int from_tty) 500 { 501 /* Unimplemented. */ 502 return 0; 503 } 504 505 /* Given a loadmap and an address, return the displacement needed 506 to relocate the address. */ 507 508 static CORE_ADDR 509 displacement_from_map (struct int_elf32_dsbt_loadmap *map, 510 CORE_ADDR addr) 511 { 512 int seg; 513 514 for (seg = 0; seg < map->nsegs; seg++) 515 if (map->segs[seg].p_vaddr <= addr 516 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz) 517 return map->segs[seg].addr - map->segs[seg].p_vaddr; 518 519 return 0; 520 } 521 522 /* Return the address from which the link map chain may be found. On 523 DSBT, a pointer to the start of the link map will be located at the 524 word found at base of GOT + GOT_MODULE_OFFSET. 525 526 The base of GOT may be found in a number of ways. Assuming that the 527 main executable has already been relocated, 528 1 The easiest way to find this value is to look up the address of 529 _GLOBAL_OFFSET_TABLE_. 530 2 The other way is to look for tag DT_PLTGOT, which contains the virtual 531 address of Global Offset Table. .*/ 532 533 static CORE_ADDR 534 lm_base (void) 535 { 536 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); 537 struct bound_minimal_symbol got_sym; 538 CORE_ADDR addr; 539 gdb_byte buf[TIC6X_PTR_SIZE]; 540 struct dsbt_info *info = get_dsbt_info (); 541 542 /* One of our assumptions is that the main executable has been relocated. 543 Bail out if this has not happened. (Note that post_create_inferior 544 in infcmd.c will call solib_add prior to solib_create_inferior_hook. 545 If we allow this to happen, lm_base_cache will be initialized with 546 a bogus value. */ 547 if (info->main_executable_lm_info == 0) 548 return 0; 549 550 /* If we already have a cached value, return it. */ 551 if (info->lm_base_cache) 552 return info->lm_base_cache; 553 554 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL, 555 symfile_objfile); 556 557 if (got_sym.minsym != 0) 558 { 559 addr = BMSYMBOL_VALUE_ADDRESS (got_sym); 560 if (solib_dsbt_debug) 561 fprintf_unfiltered (gdb_stdlog, 562 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n", 563 (unsigned int) addr); 564 } 565 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr)) 566 { 567 struct int_elf32_dsbt_loadmap *ldm; 568 569 dsbt_get_initial_loadmaps (); 570 ldm = info->exec_loadmap; 571 addr += displacement_from_map (ldm, addr); 572 if (solib_dsbt_debug) 573 fprintf_unfiltered (gdb_stdlog, 574 "lm_base: get addr %x by DT_PLTGOT.\n", 575 (unsigned int) addr); 576 } 577 else 578 { 579 if (solib_dsbt_debug) 580 fprintf_unfiltered (gdb_stdlog, 581 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n"); 582 return 0; 583 } 584 addr += GOT_MODULE_OFFSET; 585 586 if (solib_dsbt_debug) 587 fprintf_unfiltered (gdb_stdlog, 588 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n", 589 GOT_MODULE_OFFSET, hex_string_custom (addr, 8)); 590 591 if (target_read_memory (addr, buf, sizeof buf) != 0) 592 return 0; 593 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order); 594 595 if (solib_dsbt_debug) 596 fprintf_unfiltered (gdb_stdlog, 597 "lm_base: lm_base_cache = %s\n", 598 hex_string_custom (info->lm_base_cache, 8)); 599 600 return info->lm_base_cache; 601 } 602 603 604 /* Build a list of `struct so_list' objects describing the shared 605 objects currently loaded in the inferior. This list does not 606 include an entry for the main executable file. 607 608 Note that we only gather information directly available from the 609 inferior --- we don't examine any of the shared library files 610 themselves. The declaration of `struct so_list' says which fields 611 we provide values for. */ 612 613 static struct so_list * 614 dsbt_current_sos (void) 615 { 616 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); 617 CORE_ADDR lm_addr; 618 struct so_list *sos_head = NULL; 619 struct so_list **sos_next_ptr = &sos_head; 620 struct dsbt_info *info = get_dsbt_info (); 621 622 /* Make sure that the main executable has been relocated. This is 623 required in order to find the address of the global offset table, 624 which in turn is used to find the link map info. (See lm_base 625 for details.) 626 627 Note that the relocation of the main executable is also performed 628 by solib_create_inferior_hook, however, in the case of core 629 files, this hook is called too late in order to be of benefit to 630 solib_add. solib_add eventually calls this function, 631 dsbt_current_sos, and also precedes the call to 632 solib_create_inferior_hook. (See post_create_inferior in 633 infcmd.c.) */ 634 if (info->main_executable_lm_info == 0 && core_bfd != NULL) 635 dsbt_relocate_main_executable (); 636 637 /* Locate the address of the first link map struct. */ 638 lm_addr = lm_base (); 639 640 /* We have at least one link map entry. Fetch the lot of them, 641 building the solist chain. */ 642 while (lm_addr) 643 { 644 struct ext_link_map lm_buf; 645 ext_Elf32_Word indexword; 646 CORE_ADDR map_addr; 647 int dsbt_index; 648 int ret; 649 650 if (solib_dsbt_debug) 651 fprintf_unfiltered (gdb_stdlog, 652 "current_sos: reading link_map entry at %s\n", 653 hex_string_custom (lm_addr, 8)); 654 655 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf)); 656 if (ret) 657 { 658 warning (_("dsbt_current_sos: Unable to read link map entry." 659 " Shared object chain may be incomplete.")); 660 break; 661 } 662 663 /* Fetch the load map address. */ 664 map_addr = extract_unsigned_integer (lm_buf.l_addr.map, 665 sizeof lm_buf.l_addr.map, 666 byte_order); 667 668 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword, 669 sizeof indexword); 670 if (ret) 671 { 672 warning (_("dsbt_current_sos: Unable to read dsbt index." 673 " Shared object chain may be incomplete.")); 674 break; 675 } 676 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword, 677 byte_order); 678 679 /* If the DSBT index is zero, then we're looking at the entry 680 for the main executable. By convention, we don't include 681 this in the list of shared objects. */ 682 if (dsbt_index != 0) 683 { 684 struct int_elf32_dsbt_loadmap *loadmap; 685 struct so_list *sop; 686 CORE_ADDR addr; 687 688 loadmap = fetch_loadmap (map_addr); 689 if (loadmap == NULL) 690 { 691 warning (_("dsbt_current_sos: Unable to fetch load map." 692 " Shared object chain may be incomplete.")); 693 break; 694 } 695 696 sop = XCNEW (struct so_list); 697 lm_info_dsbt *li = new lm_info_dsbt; 698 sop->lm_info = li; 699 li->map = loadmap; 700 /* Fetch the name. */ 701 addr = extract_unsigned_integer (lm_buf.l_name, 702 sizeof (lm_buf.l_name), 703 byte_order); 704 gdb::unique_xmalloc_ptr<char> name_buf 705 = target_read_string (addr, SO_NAME_MAX_PATH_SIZE - 1); 706 707 if (name_buf == nullptr) 708 warning (_("Can't read pathname for link map entry.")); 709 else 710 { 711 if (solib_dsbt_debug) 712 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n", 713 name_buf.get ()); 714 715 strncpy (sop->so_name, name_buf.get (), SO_NAME_MAX_PATH_SIZE - 1); 716 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; 717 strcpy (sop->so_original_name, sop->so_name); 718 } 719 720 *sos_next_ptr = sop; 721 sos_next_ptr = &sop->next; 722 } 723 else 724 { 725 info->main_lm_addr = lm_addr; 726 } 727 728 lm_addr = extract_unsigned_integer (lm_buf.l_next, 729 sizeof (lm_buf.l_next), byte_order); 730 } 731 732 return sos_head; 733 } 734 735 /* Return 1 if PC lies in the dynamic symbol resolution code of the 736 run time loader. */ 737 738 static int 739 dsbt_in_dynsym_resolve_code (CORE_ADDR pc) 740 { 741 struct dsbt_info *info = get_dsbt_info (); 742 743 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high) 744 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high) 745 || in_plt_section (pc)); 746 } 747 748 /* Print a warning about being unable to set the dynamic linker 749 breakpoint. */ 750 751 static void 752 enable_break_failure_warning (void) 753 { 754 warning (_("Unable to find dynamic linker breakpoint function.\n" 755 "GDB will be unable to debug shared library initializers\n" 756 "and track explicitly loaded dynamic code.")); 757 } 758 759 /* Helper function for gdb_bfd_lookup_symbol. */ 760 761 static int 762 cmp_name (const asymbol *sym, const void *data) 763 { 764 return (strcmp (sym->name, (const char *) data) == 0); 765 } 766 767 /* The dynamic linkers has, as part of its debugger interface, support 768 for arranging for the inferior to hit a breakpoint after mapping in 769 the shared libraries. This function enables that breakpoint. 770 771 On the TIC6X, using the shared library (DSBT), GDB can try to place 772 a breakpoint on '_dl_debug_state' to monitor the shared library 773 event. */ 774 775 static int 776 enable_break (void) 777 { 778 asection *interp_sect; 779 struct dsbt_info *info; 780 781 if (exec_bfd == NULL) 782 return 0; 783 784 if (!target_has_execution) 785 return 0; 786 787 info = get_dsbt_info (); 788 789 info->interp_text_sect_low = 0; 790 info->interp_text_sect_high = 0; 791 info->interp_plt_sect_low = 0; 792 info->interp_plt_sect_high = 0; 793 794 /* Find the .interp section; if not found, warn the user and drop 795 into the old breakpoint at symbol code. */ 796 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp"); 797 if (interp_sect) 798 { 799 unsigned int interp_sect_size; 800 char *buf; 801 CORE_ADDR addr; 802 struct int_elf32_dsbt_loadmap *ldm; 803 int ret; 804 805 /* Read the contents of the .interp section into a local buffer; 806 the contents specify the dynamic linker this program uses. */ 807 interp_sect_size = bfd_section_size (interp_sect); 808 buf = (char *) alloca (interp_sect_size); 809 bfd_get_section_contents (exec_bfd, interp_sect, 810 buf, 0, interp_sect_size); 811 812 /* Now we need to figure out where the dynamic linker was 813 loaded so that we can load its symbols and place a breakpoint 814 in the dynamic linker itself. */ 815 816 gdb_bfd_ref_ptr tmp_bfd; 817 try 818 { 819 tmp_bfd = solib_bfd_open (buf); 820 } 821 catch (const gdb_exception &ex) 822 { 823 } 824 825 if (tmp_bfd == NULL) 826 { 827 enable_break_failure_warning (); 828 return 0; 829 } 830 831 dsbt_get_initial_loadmaps (); 832 ldm = info->interp_loadmap; 833 834 /* Record the relocated start and end address of the dynamic linker 835 text and plt section for dsbt_in_dynsym_resolve_code. */ 836 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text"); 837 if (interp_sect) 838 { 839 info->interp_text_sect_low = bfd_section_vma (interp_sect); 840 info->interp_text_sect_low 841 += displacement_from_map (ldm, info->interp_text_sect_low); 842 info->interp_text_sect_high 843 = info->interp_text_sect_low + bfd_section_size (interp_sect); 844 } 845 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt"); 846 if (interp_sect) 847 { 848 info->interp_plt_sect_low = bfd_section_vma (interp_sect); 849 info->interp_plt_sect_low 850 += displacement_from_map (ldm, info->interp_plt_sect_low); 851 info->interp_plt_sect_high 852 = info->interp_plt_sect_low + bfd_section_size (interp_sect); 853 } 854 855 addr = gdb_bfd_lookup_symbol (tmp_bfd.get (), cmp_name, 856 "_dl_debug_state"); 857 if (addr != 0) 858 { 859 if (solib_dsbt_debug) 860 fprintf_unfiltered (gdb_stdlog, 861 "enable_break: _dl_debug_state (prior to relocation) = %s\n", 862 hex_string_custom (addr, 8)); 863 addr += displacement_from_map (ldm, addr); 864 865 if (solib_dsbt_debug) 866 fprintf_unfiltered (gdb_stdlog, 867 "enable_break: _dl_debug_state (after relocation) = %s\n", 868 hex_string_custom (addr, 8)); 869 870 /* Now (finally!) create the solib breakpoint. */ 871 create_solib_event_breakpoint (target_gdbarch (), addr); 872 873 ret = 1; 874 } 875 else 876 { 877 if (solib_dsbt_debug) 878 fprintf_unfiltered (gdb_stdlog, 879 "enable_break: _dl_debug_state is not found\n"); 880 ret = 0; 881 } 882 883 /* We're done with the loadmap. */ 884 xfree (ldm); 885 886 return ret; 887 } 888 889 /* Tell the user we couldn't set a dynamic linker breakpoint. */ 890 enable_break_failure_warning (); 891 892 /* Failure return. */ 893 return 0; 894 } 895 896 static void 897 dsbt_relocate_main_executable (void) 898 { 899 struct int_elf32_dsbt_loadmap *ldm; 900 int changed; 901 struct obj_section *osect; 902 struct dsbt_info *info = get_dsbt_info (); 903 904 dsbt_get_initial_loadmaps (); 905 ldm = info->exec_loadmap; 906 907 delete info->main_executable_lm_info; 908 info->main_executable_lm_info = new lm_info_dsbt; 909 info->main_executable_lm_info->map = ldm; 910 911 section_offsets new_offsets (symfile_objfile->section_offsets.size ()); 912 changed = 0; 913 914 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect) 915 { 916 CORE_ADDR orig_addr, addr, offset; 917 int osect_idx; 918 int seg; 919 920 osect_idx = osect - symfile_objfile->sections; 921 922 /* Current address of section. */ 923 addr = obj_section_addr (osect); 924 /* Offset from where this section started. */ 925 offset = symfile_objfile->section_offsets[osect_idx]; 926 /* Original address prior to any past relocations. */ 927 orig_addr = addr - offset; 928 929 for (seg = 0; seg < ldm->nsegs; seg++) 930 { 931 if (ldm->segs[seg].p_vaddr <= orig_addr 932 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz) 933 { 934 new_offsets[osect_idx] 935 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr; 936 937 if (new_offsets[osect_idx] != offset) 938 changed = 1; 939 break; 940 } 941 } 942 } 943 944 if (changed) 945 objfile_relocate (symfile_objfile, new_offsets); 946 947 /* Now that symfile_objfile has been relocated, we can compute the 948 GOT value and stash it away. */ 949 } 950 951 /* When gdb starts up the inferior, it nurses it along (through the 952 shell) until it is ready to execute it's first instruction. At this 953 point, this function gets called via solib_create_inferior_hook. 954 955 For the DSBT shared library, the main executable needs to be relocated. 956 The shared library breakpoints also need to be enabled. */ 957 958 static void 959 dsbt_solib_create_inferior_hook (int from_tty) 960 { 961 /* Relocate main executable. */ 962 dsbt_relocate_main_executable (); 963 964 /* Enable shared library breakpoints. */ 965 if (!enable_break ()) 966 { 967 warning (_("shared library handler failed to enable breakpoint")); 968 return; 969 } 970 } 971 972 static void 973 dsbt_clear_solib (void) 974 { 975 struct dsbt_info *info = get_dsbt_info (); 976 977 info->lm_base_cache = 0; 978 info->main_lm_addr = 0; 979 980 delete info->main_executable_lm_info; 981 info->main_executable_lm_info = NULL; 982 } 983 984 static void 985 dsbt_free_so (struct so_list *so) 986 { 987 lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info; 988 989 delete li; 990 } 991 992 static void 993 dsbt_relocate_section_addresses (struct so_list *so, 994 struct target_section *sec) 995 { 996 int seg; 997 lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info; 998 int_elf32_dsbt_loadmap *map = li->map; 999 1000 for (seg = 0; seg < map->nsegs; seg++) 1001 { 1002 if (map->segs[seg].p_vaddr <= sec->addr 1003 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz) 1004 { 1005 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr; 1006 1007 sec->addr += displ; 1008 sec->endaddr += displ; 1009 break; 1010 } 1011 } 1012 } 1013 static void 1014 show_dsbt_debug (struct ui_file *file, int from_tty, 1015 struct cmd_list_element *c, const char *value) 1016 { 1017 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value); 1018 } 1019 1020 struct target_so_ops dsbt_so_ops; 1021 1022 void _initialize_dsbt_solib (); 1023 void 1024 _initialize_dsbt_solib () 1025 { 1026 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses; 1027 dsbt_so_ops.free_so = dsbt_free_so; 1028 dsbt_so_ops.clear_solib = dsbt_clear_solib; 1029 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook; 1030 dsbt_so_ops.current_sos = dsbt_current_sos; 1031 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object; 1032 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code; 1033 dsbt_so_ops.bfd_open = solib_bfd_open; 1034 1035 /* Debug this file's internals. */ 1036 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance, 1037 &solib_dsbt_debug, _("\ 1038 Set internal debugging of shared library code for DSBT ELF."), _("\ 1039 Show internal debugging of shared library code for DSBT ELF."), _("\ 1040 When non-zero, DSBT solib specific internal debugging is enabled."), 1041 NULL, 1042 show_dsbt_debug, 1043 &setdebuglist, &showdebuglist); 1044 } 1045