1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
32 #include "exceptions.h"
35 #define GOT_MODULE_OFFSET 4
37 /* Flag which indicates whether internal debug messages should be printed. */
38 static unsigned int solib_dsbt_debug = 0;
40 /* TIC6X pointers are four bytes wide. */
41 enum { TIC6X_PTR_SIZE = 4 };
43 /* Representation of loadmap and related structs for the TIC6X DSBT. */
45 /* External versions; the size and alignment of the fields should be
46 the same as those on the target. When loaded, the placement of
47 the bits in each field will be the same as on the target. */
48 typedef gdb_byte ext_Elf32_Half[2];
49 typedef gdb_byte ext_Elf32_Addr[4];
50 typedef gdb_byte ext_Elf32_Word[4];
52 struct ext_elf32_dsbt_loadseg
54 /* Core address to which the segment is mapped. */
56 /* VMA recorded in the program header. */
57 ext_Elf32_Addr p_vaddr;
58 /* Size of this segment in memory. */
59 ext_Elf32_Word p_memsz;
62 struct ext_elf32_dsbt_loadmap {
63 /* Protocol version number, must be zero. */
64 ext_Elf32_Word version;
65 /* A pointer to the DSBT table; the DSBT size and the index of this
67 ext_Elf32_Word dsbt_table_ptr;
68 ext_Elf32_Word dsbt_size;
69 ext_Elf32_Word dsbt_index;
70 /* Number of segments in this map. */
72 /* The actual memory map. */
73 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
76 /* Internal versions; the types are GDB types and the data in each
77 of the fields is (or will be) decoded from the external struct
78 for ease of consumption. */
79 struct int_elf32_dsbt_loadseg
81 /* Core address to which the segment is mapped. */
83 /* VMA recorded in the program header. */
85 /* Size of this segment in memory. */
89 struct int_elf32_dsbt_loadmap
91 /* Protocol version number, must be zero. */
93 CORE_ADDR dsbt_table_ptr;
94 /* A pointer to the DSBT table; the DSBT size and the index of this
96 int dsbt_size, dsbt_index;
97 /* Number of segments in this map. */
99 /* The actual memory map. */
100 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
103 /* External link_map and elf32_dsbt_loadaddr struct definitions. */
105 typedef gdb_byte ext_ptr[4];
107 struct ext_elf32_dsbt_loadaddr
109 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
114 struct ext_elf32_dsbt_loadaddr l_addr;
116 /* Absolute file name object was found in. */
117 ext_ptr l_name; /* char *l_name; */
119 /* Dynamic section of the shared object. */
120 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
122 /* Chain of loaded objects. */
123 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
126 /* Link map info to include in an allocated so_list entry */
130 /* The loadmap, digested into an easier to use form. */
131 struct int_elf32_dsbt_loadmap *map;
134 /* Per pspace dsbt specific data. */
138 /* The load map, got value, etc. are not available from the chain
139 of loaded shared objects. ``main_executable_lm_info'' provides
140 a way to get at this information so that it doesn't need to be
141 frequently recomputed. Initialized by dsbt_relocate_main_executable. */
142 struct lm_info *main_executable_lm_info;
144 /* Load maps for the main executable and the interpreter. These are obtained
145 from ptrace. They are the starting point for getting into the program,
146 and are required to find the solib list with the individual load maps for
148 struct int_elf32_dsbt_loadmap *exec_loadmap;
149 struct int_elf32_dsbt_loadmap *interp_loadmap;
151 /* Cached value for lm_base, below. */
152 CORE_ADDR lm_base_cache;
154 /* Link map address for main module. */
155 CORE_ADDR main_lm_addr;
157 CORE_ADDR interp_text_sect_low;
158 CORE_ADDR interp_text_sect_high;
159 CORE_ADDR interp_plt_sect_low;
160 CORE_ADDR interp_plt_sect_high;
163 /* Per-program-space data key. */
164 static const struct program_space_data *solib_dsbt_pspace_data;
167 dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
172 /* Get the current dsbt data. If none is found yet, add it now. This
173 function always returns a valid object. */
175 static struct dsbt_info *
178 struct dsbt_info *info;
180 info = program_space_data (current_program_space, solib_dsbt_pspace_data);
184 info = XCNEW (struct dsbt_info);
185 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
187 info->lm_base_cache = 0;
188 info->main_lm_addr = 0;
195 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
200 printf_filtered ("(null)\n");
201 else if (map->version != 0)
202 printf_filtered (_("Unsupported map version: %d\n"), map->version);
205 printf_filtered ("version %d\n", map->version);
207 for (i = 0; i < map->nsegs; i++)
208 printf_filtered ("%s:%s -> %s:%s\n",
209 print_core_address (target_gdbarch (),
210 map->segs[i].p_vaddr),
211 print_core_address (target_gdbarch (),
213 + map->segs[i].p_memsz),
214 print_core_address (target_gdbarch (), map->segs[i].addr),
215 print_core_address (target_gdbarch (), map->segs[i].addr
216 + map->segs[i].p_memsz));
220 /* Decode int_elf32_dsbt_loadmap from BUF. */
222 static struct int_elf32_dsbt_loadmap *
223 decode_loadmap (gdb_byte *buf)
225 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
226 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
227 struct int_elf32_dsbt_loadmap *int_ldmbuf;
229 int version, seg, nsegs;
232 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
234 /* Extract the version. */
235 version = extract_unsigned_integer (ext_ldmbuf->version,
236 sizeof ext_ldmbuf->version,
240 /* We only handle version 0. */
244 /* Extract the number of segments. */
245 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
246 sizeof ext_ldmbuf->nsegs,
252 /* Allocate space into which to put information extract from the
253 external loadsegs. I.e, allocate the internal loadsegs. */
254 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
255 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
256 int_ldmbuf = xmalloc (int_ldmbuf_size);
258 /* Place extracted information in internal structs. */
259 int_ldmbuf->version = version;
260 int_ldmbuf->nsegs = nsegs;
261 for (seg = 0; seg < nsegs; seg++)
263 int_ldmbuf->segs[seg].addr
264 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
265 sizeof (ext_ldmbuf->segs[seg].addr),
267 int_ldmbuf->segs[seg].p_vaddr
268 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
269 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
271 int_ldmbuf->segs[seg].p_memsz
272 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
273 sizeof (ext_ldmbuf->segs[seg].p_memsz),
282 static struct dsbt_info *get_dsbt_info (void);
284 /* Interrogate the Linux kernel to find out where the program was loaded.
285 There are two load maps; one for the executable and one for the
286 interpreter (only in the case of a dynamically linked executable). */
289 dsbt_get_initial_loadmaps (void)
292 struct dsbt_info *info = get_dsbt_info ();
294 if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC,
297 info->exec_loadmap = NULL;
298 error (_("Error reading DSBT exec loadmap"));
300 info->exec_loadmap = decode_loadmap (buf);
301 if (solib_dsbt_debug)
302 dsbt_print_loadmap (info->exec_loadmap);
304 if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC,
307 info->interp_loadmap = NULL;
308 error (_("Error reading DSBT interp loadmap"));
310 info->interp_loadmap = decode_loadmap (buf);
311 if (solib_dsbt_debug)
312 dsbt_print_loadmap (info->interp_loadmap);
315 /* Given address LDMADDR, fetch and decode the loadmap at that address.
316 Return NULL if there is a problem reading the target memory or if
317 there doesn't appear to be a loadmap at the given address. The
318 allocated space (representing the loadmap) returned by this
319 function may be freed via a single call to xfree. */
321 static struct int_elf32_dsbt_loadmap *
322 fetch_loadmap (CORE_ADDR ldmaddr)
324 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
325 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
326 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
327 struct int_elf32_dsbt_loadmap *int_ldmbuf;
328 int ext_ldmbuf_size, int_ldmbuf_size;
329 int version, seg, nsegs;
331 /* Fetch initial portion of the loadmap. */
332 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
333 sizeof ext_ldmbuf_partial))
335 /* Problem reading the target's memory. */
339 /* Extract the version. */
340 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
341 sizeof ext_ldmbuf_partial.version,
345 /* We only handle version 0. */
349 /* Extract the number of segments. */
350 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
351 sizeof ext_ldmbuf_partial.nsegs,
357 /* Allocate space for the complete (external) loadmap. */
358 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
359 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
360 ext_ldmbuf = xmalloc (ext_ldmbuf_size);
362 /* Copy over the portion of the loadmap that's already been read. */
363 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
365 /* Read the rest of the loadmap from the target. */
366 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
367 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
368 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
370 /* Couldn't read rest of the loadmap. */
375 /* Allocate space into which to put information extract from the
376 external loadsegs. I.e, allocate the internal loadsegs. */
377 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
378 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
379 int_ldmbuf = xmalloc (int_ldmbuf_size);
381 /* Place extracted information in internal structs. */
382 int_ldmbuf->version = version;
383 int_ldmbuf->nsegs = nsegs;
384 for (seg = 0; seg < nsegs; seg++)
386 int_ldmbuf->segs[seg].addr
387 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
388 sizeof (ext_ldmbuf->segs[seg].addr),
390 int_ldmbuf->segs[seg].p_vaddr
391 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
392 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
394 int_ldmbuf->segs[seg].p_memsz
395 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
396 sizeof (ext_ldmbuf->segs[seg].p_memsz),
404 static void dsbt_relocate_main_executable (void);
405 static int enable_break (void);
407 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
408 returned and the corresponding PTR is set. */
411 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
413 int arch_size, step, sect_size;
415 CORE_ADDR dyn_ptr, dyn_addr;
416 gdb_byte *bufend, *bufstart, *buf;
417 Elf32_External_Dyn *x_dynp_32;
418 Elf64_External_Dyn *x_dynp_64;
419 struct bfd_section *sect;
420 struct target_section *target_section;
425 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
428 arch_size = bfd_get_arch_size (abfd);
432 /* Find the start address of the .dynamic section. */
433 sect = bfd_get_section_by_name (abfd, ".dynamic");
437 for (target_section = current_target_sections->sections;
438 target_section < current_target_sections->sections_end;
440 if (sect == target_section->the_bfd_section)
442 if (target_section < current_target_sections->sections_end)
443 dyn_addr = target_section->addr;
446 /* ABFD may come from OBJFILE acting only as a symbol file without being
447 loaded into the target (see add_symbol_file_command). This case is
448 such fallback to the file VMA address without the possibility of
449 having the section relocated to its actual in-memory address. */
451 dyn_addr = bfd_section_vma (abfd, sect);
454 /* Read in .dynamic from the BFD. We will get the actual value
455 from memory later. */
456 sect_size = bfd_section_size (abfd, sect);
457 buf = bufstart = alloca (sect_size);
458 if (!bfd_get_section_contents (abfd, sect,
462 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
463 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
464 : sizeof (Elf64_External_Dyn);
465 for (bufend = buf + sect_size;
471 x_dynp_32 = (Elf32_External_Dyn *) buf;
472 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
473 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
477 x_dynp_64 = (Elf64_External_Dyn *) buf;
478 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
479 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
481 if (dyn_tag == DT_NULL)
483 if (dyn_tag == dyntag)
485 /* If requested, try to read the runtime value of this .dynamic
489 struct type *ptr_type;
493 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
494 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
495 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
496 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
506 /* If no open symbol file, attempt to locate and open the main symbol
509 If FROM_TTYP dereferences to a non-zero integer, allow messages to
510 be printed. This parameter is a pointer rather than an int because
511 open_symbol_file_object is called via catch_errors and
512 catch_errors requires a pointer argument. */
515 open_symbol_file_object (void *from_ttyp)
521 /* Given a loadmap and an address, return the displacement needed
522 to relocate the address. */
525 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
530 for (seg = 0; seg < map->nsegs; seg++)
531 if (map->segs[seg].p_vaddr <= addr
532 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
533 return map->segs[seg].addr - map->segs[seg].p_vaddr;
538 /* Return the address from which the link map chain may be found. On
539 DSBT, a pointer to the start of the link map will be located at the
540 word found at base of GOT + GOT_MODULE_OFFSET.
542 The base of GOT may be found in a number of ways. Assuming that the
543 main executable has already been relocated,
544 1 The easiest way to find this value is to look up the address of
545 _GLOBAL_OFFSET_TABLE_.
546 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
547 address of Global Offset Table. .*/
552 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
553 struct bound_minimal_symbol got_sym;
555 gdb_byte buf[TIC6X_PTR_SIZE];
556 struct dsbt_info *info = get_dsbt_info ();
558 /* One of our assumptions is that the main executable has been relocated.
559 Bail out if this has not happened. (Note that post_create_inferior
560 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
561 If we allow this to happen, lm_base_cache will be initialized with
563 if (info->main_executable_lm_info == 0)
566 /* If we already have a cached value, return it. */
567 if (info->lm_base_cache)
568 return info->lm_base_cache;
570 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
573 if (got_sym.minsym != 0)
575 addr = BMSYMBOL_VALUE_ADDRESS (got_sym);
576 if (solib_dsbt_debug)
577 fprintf_unfiltered (gdb_stdlog,
578 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
579 (unsigned int) addr);
581 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
583 struct int_elf32_dsbt_loadmap *ldm;
585 dsbt_get_initial_loadmaps ();
586 ldm = info->exec_loadmap;
587 addr += displacement_from_map (ldm, addr);
588 if (solib_dsbt_debug)
589 fprintf_unfiltered (gdb_stdlog,
590 "lm_base: get addr %x by DT_PLTGOT.\n",
591 (unsigned int) addr);
595 if (solib_dsbt_debug)
596 fprintf_unfiltered (gdb_stdlog,
597 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
600 addr += GOT_MODULE_OFFSET;
602 if (solib_dsbt_debug)
603 fprintf_unfiltered (gdb_stdlog,
604 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
605 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
607 if (target_read_memory (addr, buf, sizeof buf) != 0)
609 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
611 if (solib_dsbt_debug)
612 fprintf_unfiltered (gdb_stdlog,
613 "lm_base: lm_base_cache = %s\n",
614 hex_string_custom (info->lm_base_cache, 8));
616 return info->lm_base_cache;
620 /* Build a list of `struct so_list' objects describing the shared
621 objects currently loaded in the inferior. This list does not
622 include an entry for the main executable file.
624 Note that we only gather information directly available from the
625 inferior --- we don't examine any of the shared library files
626 themselves. The declaration of `struct so_list' says which fields
627 we provide values for. */
629 static struct so_list *
630 dsbt_current_sos (void)
632 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
634 struct so_list *sos_head = NULL;
635 struct so_list **sos_next_ptr = &sos_head;
636 struct dsbt_info *info = get_dsbt_info ();
638 /* Make sure that the main executable has been relocated. This is
639 required in order to find the address of the global offset table,
640 which in turn is used to find the link map info. (See lm_base
643 Note that the relocation of the main executable is also performed
644 by solib_create_inferior_hook, however, in the case of core
645 files, this hook is called too late in order to be of benefit to
646 solib_add. solib_add eventually calls this function,
647 dsbt_current_sos, and also precedes the call to
648 solib_create_inferior_hook. (See post_create_inferior in
650 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
651 dsbt_relocate_main_executable ();
653 /* Locate the address of the first link map struct. */
654 lm_addr = lm_base ();
656 /* We have at least one link map entry. Fetch the the lot of them,
657 building the solist chain. */
660 struct ext_link_map lm_buf;
661 ext_Elf32_Word indexword;
666 if (solib_dsbt_debug)
667 fprintf_unfiltered (gdb_stdlog,
668 "current_sos: reading link_map entry at %s\n",
669 hex_string_custom (lm_addr, 8));
671 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
674 warning (_("dsbt_current_sos: Unable to read link map entry."
675 " Shared object chain may be incomplete."));
679 /* Fetch the load map address. */
680 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
681 sizeof lm_buf.l_addr.map,
684 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
688 warning (_("dsbt_current_sos: Unable to read dsbt index."
689 " Shared object chain may be incomplete."));
692 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
695 /* If the DSBT index is zero, then we're looking at the entry
696 for the main executable. By convention, we don't include
697 this in the list of shared objects. */
702 struct int_elf32_dsbt_loadmap *loadmap;
706 loadmap = fetch_loadmap (map_addr);
709 warning (_("dsbt_current_sos: Unable to fetch load map."
710 " Shared object chain may be incomplete."));
714 sop = xcalloc (1, sizeof (struct so_list));
715 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
716 sop->lm_info->map = loadmap;
717 /* Fetch the name. */
718 addr = extract_unsigned_integer (lm_buf.l_name,
719 sizeof (lm_buf.l_name),
721 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
725 warning (_("Can't read pathname for link map entry: %s."),
726 safe_strerror (errcode));
729 if (solib_dsbt_debug)
730 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
733 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
734 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
736 strcpy (sop->so_original_name, sop->so_name);
740 sos_next_ptr = &sop->next;
744 info->main_lm_addr = lm_addr;
747 lm_addr = extract_unsigned_integer (lm_buf.l_next,
748 sizeof (lm_buf.l_next), byte_order);
754 /* Return 1 if PC lies in the dynamic symbol resolution code of the
758 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
760 struct dsbt_info *info = get_dsbt_info ();
762 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
763 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
764 || in_plt_section (pc));
767 /* Print a warning about being unable to set the dynamic linker
771 enable_break_failure_warning (void)
773 warning (_("Unable to find dynamic linker breakpoint function.\n"
774 "GDB will be unable to debug shared library initializers\n"
775 "and track explicitly loaded dynamic code."));
778 /* Helper function for gdb_bfd_lookup_symbol. */
781 cmp_name (asymbol *sym, void *data)
783 return (strcmp (sym->name, (const char *) data) == 0);
786 /* The dynamic linkers has, as part of its debugger interface, support
787 for arranging for the inferior to hit a breakpoint after mapping in
788 the shared libraries. This function enables that breakpoint.
790 On the TIC6X, using the shared library (DSBT), GDB can try to place
791 a breakpoint on '_dl_debug_state' to monitor the shared library
797 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
798 asection *interp_sect;
799 struct dsbt_info *info;
801 if (exec_bfd == NULL)
804 if (!target_has_execution)
807 info = get_dsbt_info ();
809 info->interp_text_sect_low = 0;
810 info->interp_text_sect_high = 0;
811 info->interp_plt_sect_low = 0;
812 info->interp_plt_sect_high = 0;
814 /* Find the .interp section; if not found, warn the user and drop
815 into the old breakpoint at symbol code. */
816 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
819 unsigned int interp_sect_size;
823 gdb_byte addr_buf[TIC6X_PTR_SIZE];
824 struct int_elf32_dsbt_loadmap *ldm;
825 volatile struct gdb_exception ex;
828 /* Read the contents of the .interp section into a local buffer;
829 the contents specify the dynamic linker this program uses. */
830 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
831 buf = alloca (interp_sect_size);
832 bfd_get_section_contents (exec_bfd, interp_sect,
833 buf, 0, interp_sect_size);
835 /* Now we need to figure out where the dynamic linker was
836 loaded so that we can load its symbols and place a breakpoint
837 in the dynamic linker itself. */
839 TRY_CATCH (ex, RETURN_MASK_ALL)
841 tmp_bfd = solib_bfd_open (buf);
845 enable_break_failure_warning ();
849 dsbt_get_initial_loadmaps ();
850 ldm = info->interp_loadmap;
852 /* Record the relocated start and end address of the dynamic linker
853 text and plt section for dsbt_in_dynsym_resolve_code. */
854 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
857 info->interp_text_sect_low
858 = bfd_section_vma (tmp_bfd, interp_sect);
859 info->interp_text_sect_low
860 += displacement_from_map (ldm, info->interp_text_sect_low);
861 info->interp_text_sect_high
862 = info->interp_text_sect_low
863 + bfd_section_size (tmp_bfd, interp_sect);
865 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
868 info->interp_plt_sect_low =
869 bfd_section_vma (tmp_bfd, interp_sect);
870 info->interp_plt_sect_low
871 += displacement_from_map (ldm, info->interp_plt_sect_low);
872 info->interp_plt_sect_high =
873 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
876 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_state");
879 if (solib_dsbt_debug)
880 fprintf_unfiltered (gdb_stdlog,
881 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
882 hex_string_custom (addr, 8));
883 addr += displacement_from_map (ldm, addr);
885 if (solib_dsbt_debug)
886 fprintf_unfiltered (gdb_stdlog,
887 "enable_break: _dl_debug_state (after relocation) = %s\n",
888 hex_string_custom (addr, 8));
890 /* Now (finally!) create the solib breakpoint. */
891 create_solib_event_breakpoint (target_gdbarch (), addr);
897 if (solib_dsbt_debug)
898 fprintf_unfiltered (gdb_stdlog,
899 "enable_break: _dl_debug_state is not found\n");
903 /* We're done with the temporary bfd. */
904 gdb_bfd_unref (tmp_bfd);
906 /* We're also done with the loadmap. */
912 /* Tell the user we couldn't set a dynamic linker breakpoint. */
913 enable_break_failure_warning ();
915 /* Failure return. */
919 /* Once the symbols from a shared object have been loaded in the usual
920 way, we are called to do any system specific symbol handling that
924 dsbt_special_symbol_handling (void)
929 dsbt_relocate_main_executable (void)
931 struct int_elf32_dsbt_loadmap *ldm;
932 struct cleanup *old_chain;
933 struct section_offsets *new_offsets;
935 struct obj_section *osect;
936 struct dsbt_info *info = get_dsbt_info ();
938 dsbt_get_initial_loadmaps ();
939 ldm = info->exec_loadmap;
941 xfree (info->main_executable_lm_info);
942 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
943 info->main_executable_lm_info->map = ldm;
945 new_offsets = xcalloc (symfile_objfile->num_sections,
946 sizeof (struct section_offsets));
947 old_chain = make_cleanup (xfree, new_offsets);
950 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
952 CORE_ADDR orig_addr, addr, offset;
956 osect_idx = osect - symfile_objfile->sections;
958 /* Current address of section. */
959 addr = obj_section_addr (osect);
960 /* Offset from where this section started. */
961 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
962 /* Original address prior to any past relocations. */
963 orig_addr = addr - offset;
965 for (seg = 0; seg < ldm->nsegs; seg++)
967 if (ldm->segs[seg].p_vaddr <= orig_addr
968 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
970 new_offsets->offsets[osect_idx]
971 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
973 if (new_offsets->offsets[osect_idx] != offset)
981 objfile_relocate (symfile_objfile, new_offsets);
983 do_cleanups (old_chain);
985 /* Now that symfile_objfile has been relocated, we can compute the
986 GOT value and stash it away. */
989 /* When gdb starts up the inferior, it nurses it along (through the
990 shell) until it is ready to execute it's first instruction. At this
991 point, this function gets called via solib_create_inferior_hook.
993 For the DSBT shared library, the main executable needs to be relocated.
994 The shared library breakpoints also need to be enabled. */
997 dsbt_solib_create_inferior_hook (int from_tty)
999 /* Relocate main executable. */
1000 dsbt_relocate_main_executable ();
1002 /* Enable shared library breakpoints. */
1003 if (!enable_break ())
1005 warning (_("shared library handler failed to enable breakpoint"));
1011 dsbt_clear_solib (void)
1013 struct dsbt_info *info = get_dsbt_info ();
1015 info->lm_base_cache = 0;
1016 info->main_lm_addr = 0;
1017 if (info->main_executable_lm_info != 0)
1019 xfree (info->main_executable_lm_info->map);
1020 xfree (info->main_executable_lm_info);
1021 info->main_executable_lm_info = 0;
1026 dsbt_free_so (struct so_list *so)
1028 xfree (so->lm_info->map);
1029 xfree (so->lm_info);
1033 dsbt_relocate_section_addresses (struct so_list *so,
1034 struct target_section *sec)
1037 struct int_elf32_dsbt_loadmap *map;
1039 map = so->lm_info->map;
1041 for (seg = 0; seg < map->nsegs; seg++)
1043 if (map->segs[seg].p_vaddr <= sec->addr
1044 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1046 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1049 sec->endaddr += displ;
1055 show_dsbt_debug (struct ui_file *file, int from_tty,
1056 struct cmd_list_element *c, const char *value)
1058 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1061 struct target_so_ops dsbt_so_ops;
1063 /* Provide a prototype to silence -Wmissing-prototypes. */
1064 extern initialize_file_ftype _initialize_dsbt_solib;
1067 _initialize_dsbt_solib (void)
1069 solib_dsbt_pspace_data
1070 = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
1072 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1073 dsbt_so_ops.free_so = dsbt_free_so;
1074 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1075 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1076 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1077 dsbt_so_ops.current_sos = dsbt_current_sos;
1078 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1079 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1080 dsbt_so_ops.bfd_open = solib_bfd_open;
1082 /* Debug this file's internals. */
1083 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1084 &solib_dsbt_debug, _("\
1085 Set internal debugging of shared library code for DSBT ELF."), _("\
1086 Show internal debugging of shared library code for DSBT ELF."), _("\
1087 When non-zero, DSBT solib specific internal debugging is enabled."),
1090 &setdebuglist, &showdebuglist);