1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2013 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/>. */
21 #include "gdb_string.h"
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 int enable_break2_done;
159 CORE_ADDR interp_text_sect_low;
160 CORE_ADDR interp_text_sect_high;
161 CORE_ADDR interp_plt_sect_low;
162 CORE_ADDR interp_plt_sect_high;
165 /* Per-program-space data key. */
166 static const struct program_space_data *solib_dsbt_pspace_data;
169 dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
171 struct dsbt_info *info;
173 info = program_space_data (pspace, solib_dsbt_pspace_data);
177 /* Get the current dsbt data. If none is found yet, add it now. This
178 function always returns a valid object. */
180 static struct dsbt_info *
183 struct dsbt_info *info;
185 info = program_space_data (current_program_space, solib_dsbt_pspace_data);
189 info = XZALLOC (struct dsbt_info);
190 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
192 info->enable_break2_done = 0;
193 info->lm_base_cache = 0;
194 info->main_lm_addr = 0;
201 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
206 printf_filtered ("(null)\n");
207 else if (map->version != 0)
208 printf_filtered (_("Unsupported map version: %d\n"), map->version);
211 printf_filtered ("version %d\n", map->version);
213 for (i = 0; i < map->nsegs; i++)
214 printf_filtered ("%s:%s -> %s:%s\n",
215 print_core_address (target_gdbarch (),
216 map->segs[i].p_vaddr),
217 print_core_address (target_gdbarch (),
219 + map->segs[i].p_memsz),
220 print_core_address (target_gdbarch (), map->segs[i].addr),
221 print_core_address (target_gdbarch (), map->segs[i].addr
222 + map->segs[i].p_memsz));
226 /* Decode int_elf32_dsbt_loadmap from BUF. */
228 static struct int_elf32_dsbt_loadmap *
229 decode_loadmap (gdb_byte *buf)
231 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
232 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
233 struct int_elf32_dsbt_loadmap *int_ldmbuf;
235 int version, seg, nsegs;
238 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
240 /* Extract the version. */
241 version = extract_unsigned_integer (ext_ldmbuf->version,
242 sizeof ext_ldmbuf->version,
246 /* We only handle version 0. */
250 /* Extract the number of segments. */
251 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
252 sizeof ext_ldmbuf->nsegs,
258 /* Allocate space into which to put information extract from the
259 external loadsegs. I.e, allocate the internal loadsegs. */
260 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
261 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
262 int_ldmbuf = xmalloc (int_ldmbuf_size);
264 /* Place extracted information in internal structs. */
265 int_ldmbuf->version = version;
266 int_ldmbuf->nsegs = nsegs;
267 for (seg = 0; seg < nsegs; seg++)
269 int_ldmbuf->segs[seg].addr
270 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
271 sizeof (ext_ldmbuf->segs[seg].addr),
273 int_ldmbuf->segs[seg].p_vaddr
274 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
275 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
277 int_ldmbuf->segs[seg].p_memsz
278 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
279 sizeof (ext_ldmbuf->segs[seg].p_memsz),
288 static struct dsbt_info *get_dsbt_info (void);
290 /* Interrogate the Linux kernel to find out where the program was loaded.
291 There are two load maps; one for the executable and one for the
292 interpreter (only in the case of a dynamically linked executable). */
295 dsbt_get_initial_loadmaps (void)
298 struct dsbt_info *info = get_dsbt_info ();
300 if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC,
303 info->exec_loadmap = NULL;
304 error (_("Error reading DSBT exec loadmap"));
306 info->exec_loadmap = decode_loadmap (buf);
307 if (solib_dsbt_debug)
308 dsbt_print_loadmap (info->exec_loadmap);
310 if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC,
313 info->interp_loadmap = NULL;
314 error (_("Error reading DSBT interp loadmap"));
316 info->interp_loadmap = decode_loadmap (buf);
317 if (solib_dsbt_debug)
318 dsbt_print_loadmap (info->interp_loadmap);
321 /* Given address LDMADDR, fetch and decode the loadmap at that address.
322 Return NULL if there is a problem reading the target memory or if
323 there doesn't appear to be a loadmap at the given address. The
324 allocated space (representing the loadmap) returned by this
325 function may be freed via a single call to xfree. */
327 static struct int_elf32_dsbt_loadmap *
328 fetch_loadmap (CORE_ADDR ldmaddr)
330 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
331 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
332 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
333 struct int_elf32_dsbt_loadmap *int_ldmbuf;
334 int ext_ldmbuf_size, int_ldmbuf_size;
335 int version, seg, nsegs;
337 /* Fetch initial portion of the loadmap. */
338 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
339 sizeof ext_ldmbuf_partial))
341 /* Problem reading the target's memory. */
345 /* Extract the version. */
346 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
347 sizeof ext_ldmbuf_partial.version,
351 /* We only handle version 0. */
355 /* Extract the number of segments. */
356 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
357 sizeof ext_ldmbuf_partial.nsegs,
363 /* Allocate space for the complete (external) loadmap. */
364 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
365 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
366 ext_ldmbuf = xmalloc (ext_ldmbuf_size);
368 /* Copy over the portion of the loadmap that's already been read. */
369 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
371 /* Read the rest of the loadmap from the target. */
372 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
373 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
374 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
376 /* Couldn't read rest of the loadmap. */
381 /* Allocate space into which to put information extract from the
382 external loadsegs. I.e, allocate the internal loadsegs. */
383 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
384 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
385 int_ldmbuf = xmalloc (int_ldmbuf_size);
387 /* Place extracted information in internal structs. */
388 int_ldmbuf->version = version;
389 int_ldmbuf->nsegs = nsegs;
390 for (seg = 0; seg < nsegs; seg++)
392 int_ldmbuf->segs[seg].addr
393 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
394 sizeof (ext_ldmbuf->segs[seg].addr),
396 int_ldmbuf->segs[seg].p_vaddr
397 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
398 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
400 int_ldmbuf->segs[seg].p_memsz
401 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
402 sizeof (ext_ldmbuf->segs[seg].p_memsz),
410 static void dsbt_relocate_main_executable (void);
411 static int enable_break (void);
413 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
414 returned and the corresponding PTR is set. */
417 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
419 int arch_size, step, sect_size;
421 CORE_ADDR dyn_ptr, dyn_addr;
422 gdb_byte *bufend, *bufstart, *buf;
423 Elf32_External_Dyn *x_dynp_32;
424 Elf64_External_Dyn *x_dynp_64;
425 struct bfd_section *sect;
426 struct target_section *target_section;
431 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
434 arch_size = bfd_get_arch_size (abfd);
438 /* Find the start address of the .dynamic section. */
439 sect = bfd_get_section_by_name (abfd, ".dynamic");
443 for (target_section = current_target_sections->sections;
444 target_section < current_target_sections->sections_end;
446 if (sect == target_section->the_bfd_section)
448 if (target_section < current_target_sections->sections_end)
449 dyn_addr = target_section->addr;
452 /* ABFD may come from OBJFILE acting only as a symbol file without being
453 loaded into the target (see add_symbol_file_command). This case is
454 such fallback to the file VMA address without the possibility of
455 having the section relocated to its actual in-memory address. */
457 dyn_addr = bfd_section_vma (abfd, sect);
460 /* Read in .dynamic from the BFD. We will get the actual value
461 from memory later. */
462 sect_size = bfd_section_size (abfd, sect);
463 buf = bufstart = alloca (sect_size);
464 if (!bfd_get_section_contents (abfd, sect,
468 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
469 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
470 : sizeof (Elf64_External_Dyn);
471 for (bufend = buf + sect_size;
477 x_dynp_32 = (Elf32_External_Dyn *) buf;
478 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
479 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
483 x_dynp_64 = (Elf64_External_Dyn *) buf;
484 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
485 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
487 if (dyn_tag == DT_NULL)
489 if (dyn_tag == dyntag)
491 /* If requested, try to read the runtime value of this .dynamic
495 struct type *ptr_type;
499 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
500 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
501 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
502 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
512 /* If no open symbol file, attempt to locate and open the main symbol
515 If FROM_TTYP dereferences to a non-zero integer, allow messages to
516 be printed. This parameter is a pointer rather than an int because
517 open_symbol_file_object is called via catch_errors and
518 catch_errors requires a pointer argument. */
521 open_symbol_file_object (void *from_ttyp)
527 /* Given a loadmap and an address, return the displacement needed
528 to relocate the address. */
531 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
536 for (seg = 0; seg < map->nsegs; seg++)
537 if (map->segs[seg].p_vaddr <= addr
538 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
539 return map->segs[seg].addr - map->segs[seg].p_vaddr;
544 /* Return the address from which the link map chain may be found. On
545 DSBT, a pointer to the start of the link map will be located at the
546 word found at base of GOT + GOT_MODULE_OFFSET.
548 The base of GOT may be found in a number of ways. Assuming that the
549 main executable has already been relocated,
550 1 The easiest way to find this value is to look up the address of
551 _GLOBAL_OFFSET_TABLE_.
552 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
553 address of Global Offset Table. .*/
558 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
559 struct minimal_symbol *got_sym;
561 gdb_byte buf[TIC6X_PTR_SIZE];
562 struct dsbt_info *info = get_dsbt_info ();
564 /* One of our assumptions is that the main executable has been relocated.
565 Bail out if this has not happened. (Note that post_create_inferior
566 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
567 If we allow this to happen, lm_base_cache will be initialized with
569 if (info->main_executable_lm_info == 0)
572 /* If we already have a cached value, return it. */
573 if (info->lm_base_cache)
574 return info->lm_base_cache;
576 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
581 addr = SYMBOL_VALUE_ADDRESS (got_sym);
582 if (solib_dsbt_debug)
583 fprintf_unfiltered (gdb_stdlog,
584 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
585 (unsigned int) addr);
587 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
589 struct int_elf32_dsbt_loadmap *ldm;
591 dsbt_get_initial_loadmaps ();
592 ldm = info->exec_loadmap;
593 addr += displacement_from_map (ldm, addr);
594 if (solib_dsbt_debug)
595 fprintf_unfiltered (gdb_stdlog,
596 "lm_base: get addr %x by DT_PLTGOT.\n",
597 (unsigned int) addr);
601 if (solib_dsbt_debug)
602 fprintf_unfiltered (gdb_stdlog,
603 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
606 addr += GOT_MODULE_OFFSET;
608 if (solib_dsbt_debug)
609 fprintf_unfiltered (gdb_stdlog,
610 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
611 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
613 if (target_read_memory (addr, buf, sizeof buf) != 0)
615 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
617 if (solib_dsbt_debug)
618 fprintf_unfiltered (gdb_stdlog,
619 "lm_base: lm_base_cache = %s\n",
620 hex_string_custom (info->lm_base_cache, 8));
622 return info->lm_base_cache;
626 /* Build a list of `struct so_list' objects describing the shared
627 objects currently loaded in the inferior. This list does not
628 include an entry for the main executable file.
630 Note that we only gather information directly available from the
631 inferior --- we don't examine any of the shared library files
632 themselves. The declaration of `struct so_list' says which fields
633 we provide values for. */
635 static struct so_list *
636 dsbt_current_sos (void)
638 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
640 struct so_list *sos_head = NULL;
641 struct so_list **sos_next_ptr = &sos_head;
642 struct dsbt_info *info = get_dsbt_info ();
644 /* Make sure that the main executable has been relocated. This is
645 required in order to find the address of the global offset table,
646 which in turn is used to find the link map info. (See lm_base
649 Note that the relocation of the main executable is also performed
650 by solib_create_inferior_hook, however, in the case of core
651 files, this hook is called too late in order to be of benefit to
652 solib_add. solib_add eventually calls this function,
653 dsbt_current_sos, and also precedes the call to
654 solib_create_inferior_hook. (See post_create_inferior in
656 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
657 dsbt_relocate_main_executable ();
659 /* Locate the address of the first link map struct. */
660 lm_addr = lm_base ();
662 /* We have at least one link map entry. Fetch the the lot of them,
663 building the solist chain. */
666 struct ext_link_map lm_buf;
667 ext_Elf32_Word indexword;
672 if (solib_dsbt_debug)
673 fprintf_unfiltered (gdb_stdlog,
674 "current_sos: reading link_map entry at %s\n",
675 hex_string_custom (lm_addr, 8));
677 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
680 warning (_("dsbt_current_sos: Unable to read link map entry."
681 " Shared object chain may be incomplete."));
685 /* Fetch the load map address. */
686 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
687 sizeof lm_buf.l_addr.map,
690 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
694 warning (_("dsbt_current_sos: Unable to read dsbt index."
695 " Shared object chain may be incomplete."));
698 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
701 /* If the DSBT index is zero, then we're looking at the entry
702 for the main executable. By convention, we don't include
703 this in the list of shared objects. */
708 struct int_elf32_dsbt_loadmap *loadmap;
712 loadmap = fetch_loadmap (map_addr);
715 warning (_("dsbt_current_sos: Unable to fetch load map."
716 " Shared object chain may be incomplete."));
720 sop = xcalloc (1, sizeof (struct so_list));
721 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
722 sop->lm_info->map = loadmap;
723 /* Fetch the name. */
724 addr = extract_unsigned_integer (lm_buf.l_name,
725 sizeof (lm_buf.l_name),
727 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
731 warning (_("Can't read pathname for link map entry: %s."),
732 safe_strerror (errcode));
735 if (solib_dsbt_debug)
736 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
739 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
740 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
742 strcpy (sop->so_original_name, sop->so_name);
746 sos_next_ptr = &sop->next;
750 info->main_lm_addr = lm_addr;
753 lm_addr = extract_unsigned_integer (lm_buf.l_next,
754 sizeof (lm_buf.l_next), byte_order);
760 /* Return 1 if PC lies in the dynamic symbol resolution code of the
764 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
766 struct dsbt_info *info = get_dsbt_info ();
768 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
769 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
770 || in_plt_section (pc, NULL));
773 /* Print a warning about being unable to set the dynamic linker
777 enable_break_failure_warning (void)
779 warning (_("Unable to find dynamic linker breakpoint function.\n"
780 "GDB will be unable to debug shared library initializers\n"
781 "and track explicitly loaded dynamic code."));
784 /* Helper function for gdb_bfd_lookup_symbol. */
787 cmp_name (asymbol *sym, void *data)
789 return (strcmp (sym->name, (const char *) data) == 0);
792 /* The dynamic linkers has, as part of its debugger interface, support
793 for arranging for the inferior to hit a breakpoint after mapping in
794 the shared libraries. This function enables that breakpoint.
796 On the TIC6X, using the shared library (DSBT), GDB can try to place
797 a breakpoint on '_dl_debug_state' to monitor the shared library
803 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
804 asection *interp_sect;
805 struct dsbt_info *info;
807 if (exec_bfd == NULL)
810 if (!target_has_execution)
813 info = get_dsbt_info ();
815 if (info->enable_break2_done)
818 info->interp_text_sect_low = 0;
819 info->interp_text_sect_high = 0;
820 info->interp_plt_sect_low = 0;
821 info->interp_plt_sect_high = 0;
823 /* Find the .interp section; if not found, warn the user and drop
824 into the old breakpoint at symbol code. */
825 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
828 unsigned int interp_sect_size;
832 gdb_byte addr_buf[TIC6X_PTR_SIZE];
833 struct int_elf32_dsbt_loadmap *ldm;
834 volatile struct gdb_exception ex;
837 /* Read the contents of the .interp section into a local buffer;
838 the contents specify the dynamic linker this program uses. */
839 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
840 buf = alloca (interp_sect_size);
841 bfd_get_section_contents (exec_bfd, interp_sect,
842 buf, 0, interp_sect_size);
844 /* Now we need to figure out where the dynamic linker was
845 loaded so that we can load its symbols and place a breakpoint
846 in the dynamic linker itself. */
848 TRY_CATCH (ex, RETURN_MASK_ALL)
850 tmp_bfd = solib_bfd_open (buf);
854 enable_break_failure_warning ();
858 dsbt_get_initial_loadmaps ();
859 ldm = info->interp_loadmap;
861 /* Record the relocated start and end address of the dynamic linker
862 text and plt section for dsbt_in_dynsym_resolve_code. */
863 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
866 info->interp_text_sect_low
867 = bfd_section_vma (tmp_bfd, interp_sect);
868 info->interp_text_sect_low
869 += displacement_from_map (ldm, info->interp_text_sect_low);
870 info->interp_text_sect_high
871 = info->interp_text_sect_low
872 + bfd_section_size (tmp_bfd, interp_sect);
874 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
877 info->interp_plt_sect_low =
878 bfd_section_vma (tmp_bfd, interp_sect);
879 info->interp_plt_sect_low
880 += displacement_from_map (ldm, info->interp_plt_sect_low);
881 info->interp_plt_sect_high =
882 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
885 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_state");
888 if (solib_dsbt_debug)
889 fprintf_unfiltered (gdb_stdlog,
890 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
891 hex_string_custom (addr, 8));
892 addr += displacement_from_map (ldm, addr);
894 if (solib_dsbt_debug)
895 fprintf_unfiltered (gdb_stdlog,
896 "enable_break: _dl_debug_state (after relocation) = %s\n",
897 hex_string_custom (addr, 8));
899 /* Now (finally!) create the solib breakpoint. */
900 create_solib_event_breakpoint (target_gdbarch (), addr);
902 info->enable_break2_done = 1;
907 if (solib_dsbt_debug)
908 fprintf_unfiltered (gdb_stdlog,
909 "enable_break: _dl_debug_state is not found\n");
913 /* We're done with the temporary bfd. */
914 gdb_bfd_unref (tmp_bfd);
916 /* We're also done with the loadmap. */
922 /* Tell the user we couldn't set a dynamic linker breakpoint. */
923 enable_break_failure_warning ();
925 /* Failure return. */
929 /* Once the symbols from a shared object have been loaded in the usual
930 way, we are called to do any system specific symbol handling that
934 dsbt_special_symbol_handling (void)
939 dsbt_relocate_main_executable (void)
941 struct int_elf32_dsbt_loadmap *ldm;
942 struct cleanup *old_chain;
943 struct section_offsets *new_offsets;
945 struct obj_section *osect;
946 struct dsbt_info *info = get_dsbt_info ();
948 dsbt_get_initial_loadmaps ();
949 ldm = info->exec_loadmap;
951 xfree (info->main_executable_lm_info);
952 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
953 info->main_executable_lm_info->map = ldm;
955 new_offsets = xcalloc (symfile_objfile->num_sections,
956 sizeof (struct section_offsets));
957 old_chain = make_cleanup (xfree, new_offsets);
960 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
962 CORE_ADDR orig_addr, addr, offset;
966 osect_idx = osect - symfile_objfile->sections;
968 /* Current address of section. */
969 addr = obj_section_addr (osect);
970 /* Offset from where this section started. */
971 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
972 /* Original address prior to any past relocations. */
973 orig_addr = addr - offset;
975 for (seg = 0; seg < ldm->nsegs; seg++)
977 if (ldm->segs[seg].p_vaddr <= orig_addr
978 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
980 new_offsets->offsets[osect_idx]
981 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
983 if (new_offsets->offsets[osect_idx] != offset)
991 objfile_relocate (symfile_objfile, new_offsets);
993 do_cleanups (old_chain);
995 /* Now that symfile_objfile has been relocated, we can compute the
996 GOT value and stash it away. */
999 /* When gdb starts up the inferior, it nurses it along (through the
1000 shell) until it is ready to execute it's first instruction. At this
1001 point, this function gets called via solib_create_inferior_hook.
1003 For the DSBT shared library, the main executable needs to be relocated.
1004 The shared library breakpoints also need to be enabled. */
1007 dsbt_solib_create_inferior_hook (int from_tty)
1009 /* Relocate main executable. */
1010 dsbt_relocate_main_executable ();
1012 /* Enable shared library breakpoints. */
1013 if (!enable_break ())
1015 warning (_("shared library handler failed to enable breakpoint"));
1021 dsbt_clear_solib (void)
1023 struct dsbt_info *info = get_dsbt_info ();
1025 info->lm_base_cache = 0;
1026 info->enable_break2_done = 0;
1027 info->main_lm_addr = 0;
1028 if (info->main_executable_lm_info != 0)
1030 xfree (info->main_executable_lm_info->map);
1031 xfree (info->main_executable_lm_info);
1032 info->main_executable_lm_info = 0;
1037 dsbt_free_so (struct so_list *so)
1039 xfree (so->lm_info->map);
1040 xfree (so->lm_info);
1044 dsbt_relocate_section_addresses (struct so_list *so,
1045 struct target_section *sec)
1048 struct int_elf32_dsbt_loadmap *map;
1050 map = so->lm_info->map;
1052 for (seg = 0; seg < map->nsegs; seg++)
1054 if (map->segs[seg].p_vaddr <= sec->addr
1055 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1057 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1060 sec->endaddr += displ;
1066 show_dsbt_debug (struct ui_file *file, int from_tty,
1067 struct cmd_list_element *c, const char *value)
1069 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1072 struct target_so_ops dsbt_so_ops;
1074 /* Provide a prototype to silence -Wmissing-prototypes. */
1075 extern initialize_file_ftype _initialize_dsbt_solib;
1078 _initialize_dsbt_solib (void)
1080 solib_dsbt_pspace_data
1081 = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
1083 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1084 dsbt_so_ops.free_so = dsbt_free_so;
1085 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1086 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1087 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1088 dsbt_so_ops.current_sos = dsbt_current_sos;
1089 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1090 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1091 dsbt_so_ops.bfd_open = solib_bfd_open;
1093 /* Debug this file's internals. */
1094 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1095 &solib_dsbt_debug, _("\
1096 Set internal debugging of shared library code for DSBT ELF."), _("\
1097 Show internal debugging of shared library code for DSBT ELF."), _("\
1098 When non-zero, DSBT solib specific internal debugging is enabled."),
1101 &setdebuglist, &showdebuglist);