1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2012 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"
34 #define GOT_MODULE_OFFSET 4
36 /* Flag which indicates whether internal debug messages should be printed. */
37 static int solib_dsbt_debug = 0;
39 /* TIC6X pointers are four bytes wide. */
40 enum { TIC6X_PTR_SIZE = 4 };
42 /* Representation of loadmap and related structs for the TIC6X DSBT. */
44 /* External versions; the size and alignment of the fields should be
45 the same as those on the target. When loaded, the placement of
46 the bits in each field will be the same as on the target. */
47 typedef gdb_byte ext_Elf32_Half[2];
48 typedef gdb_byte ext_Elf32_Addr[4];
49 typedef gdb_byte ext_Elf32_Word[4];
51 struct ext_elf32_dsbt_loadseg
53 /* Core address to which the segment is mapped. */
55 /* VMA recorded in the program header. */
56 ext_Elf32_Addr p_vaddr;
57 /* Size of this segment in memory. */
58 ext_Elf32_Word p_memsz;
61 struct ext_elf32_dsbt_loadmap {
62 /* Protocol version number, must be zero. */
63 ext_Elf32_Word version;
64 /* A pointer to the DSBT table; the DSBT size and the index of this
66 ext_Elf32_Word dsbt_table_ptr;
67 ext_Elf32_Word dsbt_size;
68 ext_Elf32_Word dsbt_index;
69 /* Number of segments in this map. */
71 /* The actual memory map. */
72 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
75 /* Internal versions; the types are GDB types and the data in each
76 of the fields is (or will be) decoded from the external struct
77 for ease of consumption. */
78 struct int_elf32_dsbt_loadseg
80 /* Core address to which the segment is mapped. */
82 /* VMA recorded in the program header. */
84 /* Size of this segment in memory. */
88 struct int_elf32_dsbt_loadmap
90 /* Protocol version number, must be zero. */
92 CORE_ADDR dsbt_table_ptr;
93 /* A pointer to the DSBT table; the DSBT size and the index of this
95 int dsbt_size, dsbt_index;
96 /* Number of segments in this map. */
98 /* The actual memory map. */
99 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
102 /* External link_map and elf32_dsbt_loadaddr struct definitions. */
104 typedef gdb_byte ext_ptr[4];
106 struct ext_elf32_dsbt_loadaddr
108 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
113 struct ext_elf32_dsbt_loadaddr l_addr;
115 /* Absolute file name object was found in. */
116 ext_ptr l_name; /* char *l_name; */
118 /* Dynamic section of the shared object. */
119 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
121 /* Chain of loaded objects. */
122 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
125 /* Link map info to include in an allocated so_list entry */
129 /* The loadmap, digested into an easier to use form. */
130 struct int_elf32_dsbt_loadmap *map;
133 /* Per pspace dsbt specific data. */
137 /* The load map, got value, etc. are not available from the chain
138 of loaded shared objects. ``main_executable_lm_info'' provides
139 a way to get at this information so that it doesn't need to be
140 frequently recomputed. Initialized by dsbt_relocate_main_executable. */
141 struct lm_info *main_executable_lm_info;
143 /* Load maps for the main executable and the interpreter. These are obtained
144 from ptrace. They are the starting point for getting into the program,
145 and are required to find the solib list with the individual load maps for
147 struct int_elf32_dsbt_loadmap *exec_loadmap;
148 struct int_elf32_dsbt_loadmap *interp_loadmap;
150 /* Cached value for lm_base, below. */
151 CORE_ADDR lm_base_cache;
153 /* Link map address for main module. */
154 CORE_ADDR main_lm_addr;
156 int enable_break2_done;
158 CORE_ADDR interp_text_sect_low;
159 CORE_ADDR interp_text_sect_high;
160 CORE_ADDR interp_plt_sect_low;
161 CORE_ADDR interp_plt_sect_high;
164 /* Per-program-space data key. */
165 static const struct program_space_data *solib_dsbt_pspace_data;
168 dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
170 struct dsbt_info *info;
172 info = program_space_data (pspace, solib_dsbt_pspace_data);
176 /* Get the current dsbt data. If none is found yet, add it now. This
177 function always returns a valid object. */
179 static struct dsbt_info *
182 struct dsbt_info *info;
184 info = program_space_data (current_program_space, solib_dsbt_pspace_data);
188 info = XZALLOC (struct dsbt_info);
189 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
191 info->enable_break2_done = 0;
192 info->lm_base_cache = 0;
193 info->main_lm_addr = 0;
200 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
205 printf_filtered ("(null)\n");
206 else if (map->version != 0)
207 printf_filtered (_("Unsupported map version: %d\n"), map->version);
210 printf_filtered ("version %d\n", map->version);
212 for (i = 0; i < map->nsegs; i++)
213 printf_filtered ("%s:%s -> %s:%s\n",
214 print_core_address (target_gdbarch,
215 map->segs[i].p_vaddr),
216 print_core_address (target_gdbarch,
218 + map->segs[i].p_memsz),
219 print_core_address (target_gdbarch, map->segs[i].addr),
220 print_core_address (target_gdbarch, map->segs[i].addr
221 + map->segs[i].p_memsz));
225 /* Decode int_elf32_dsbt_loadmap from BUF. */
227 static struct int_elf32_dsbt_loadmap *
228 decode_loadmap (gdb_byte *buf)
230 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
231 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
232 struct int_elf32_dsbt_loadmap *int_ldmbuf;
234 int version, seg, nsegs;
235 int ext_ldmbuf_size, int_ldmbuf_size;
237 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
239 /* Extract the version. */
240 version = extract_unsigned_integer (ext_ldmbuf->version,
241 sizeof ext_ldmbuf->version,
245 /* We only handle version 0. */
249 /* Extract the number of segments. */
250 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
251 sizeof ext_ldmbuf->nsegs,
257 /* Allocate space into which to put information extract from the
258 external loadsegs. I.e, allocate the internal loadsegs. */
259 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
260 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
261 int_ldmbuf = xmalloc (int_ldmbuf_size);
263 /* Place extracted information in internal structs. */
264 int_ldmbuf->version = version;
265 int_ldmbuf->nsegs = nsegs;
266 for (seg = 0; seg < nsegs; seg++)
268 int_ldmbuf->segs[seg].addr
269 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
270 sizeof (ext_ldmbuf->segs[seg].addr),
272 int_ldmbuf->segs[seg].p_vaddr
273 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
274 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
276 int_ldmbuf->segs[seg].p_memsz
277 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
278 sizeof (ext_ldmbuf->segs[seg].p_memsz),
287 static struct dsbt_info *get_dsbt_info (void);
289 /* Interrogate the Linux kernel to find out where the program was loaded.
290 There are two load maps; one for the executable and one for the
291 interpreter (only in the case of a dynamically linked executable). */
294 dsbt_get_initial_loadmaps (void)
297 struct dsbt_info *info = get_dsbt_info ();
299 if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC,
300 "exec", (gdb_byte**) &buf))
302 info->exec_loadmap = NULL;
303 error (_("Error reading DSBT exec loadmap"));
305 info->exec_loadmap = decode_loadmap (buf);
306 if (solib_dsbt_debug)
307 dsbt_print_loadmap (info->exec_loadmap);
309 if (0 >= target_read_alloc (¤t_target, TARGET_OBJECT_FDPIC,
310 "interp", (gdb_byte**)&buf))
312 info->interp_loadmap = NULL;
313 error (_("Error reading DSBT interp loadmap"));
315 info->interp_loadmap = decode_loadmap (buf);
316 if (solib_dsbt_debug)
317 dsbt_print_loadmap (info->interp_loadmap);
320 /* Given address LDMADDR, fetch and decode the loadmap at that address.
321 Return NULL if there is a problem reading the target memory or if
322 there doesn't appear to be a loadmap at the given address. The
323 allocated space (representing the loadmap) returned by this
324 function may be freed via a single call to xfree. */
326 static struct int_elf32_dsbt_loadmap *
327 fetch_loadmap (CORE_ADDR ldmaddr)
329 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
330 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
331 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
332 struct int_elf32_dsbt_loadmap *int_ldmbuf;
333 int ext_ldmbuf_size, int_ldmbuf_size;
334 int version, seg, nsegs;
336 /* Fetch initial portion of the loadmap. */
337 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
338 sizeof ext_ldmbuf_partial))
340 /* Problem reading the target's memory. */
344 /* Extract the version. */
345 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
346 sizeof ext_ldmbuf_partial.version,
350 /* We only handle version 0. */
354 /* Extract the number of segments. */
355 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
356 sizeof ext_ldmbuf_partial.nsegs,
362 /* Allocate space for the complete (external) loadmap. */
363 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
364 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
365 ext_ldmbuf = xmalloc (ext_ldmbuf_size);
367 /* Copy over the portion of the loadmap that's already been read. */
368 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
370 /* Read the rest of the loadmap from the target. */
371 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
372 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
373 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
375 /* Couldn't read rest of the loadmap. */
380 /* Allocate space into which to put information extract from the
381 external loadsegs. I.e, allocate the internal loadsegs. */
382 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
383 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
384 int_ldmbuf = xmalloc (int_ldmbuf_size);
386 /* Place extracted information in internal structs. */
387 int_ldmbuf->version = version;
388 int_ldmbuf->nsegs = nsegs;
389 for (seg = 0; seg < nsegs; seg++)
391 int_ldmbuf->segs[seg].addr
392 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
393 sizeof (ext_ldmbuf->segs[seg].addr),
395 int_ldmbuf->segs[seg].p_vaddr
396 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
397 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
399 int_ldmbuf->segs[seg].p_memsz
400 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
401 sizeof (ext_ldmbuf->segs[seg].p_memsz),
409 static void dsbt_relocate_main_executable (void);
410 static int enable_break2 (void);
412 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
413 returned and the corresponding PTR is set. */
416 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
418 int arch_size, step, sect_size;
420 CORE_ADDR dyn_ptr, dyn_addr;
421 gdb_byte *bufend, *bufstart, *buf;
422 Elf32_External_Dyn *x_dynp_32;
423 Elf64_External_Dyn *x_dynp_64;
424 struct bfd_section *sect;
425 struct target_section *target_section;
430 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
433 arch_size = bfd_get_arch_size (abfd);
437 /* Find the start address of the .dynamic section. */
438 sect = bfd_get_section_by_name (abfd, ".dynamic");
442 for (target_section = current_target_sections->sections;
443 target_section < current_target_sections->sections_end;
445 if (sect == target_section->the_bfd_section)
447 if (target_section < current_target_sections->sections_end)
448 dyn_addr = target_section->addr;
451 /* ABFD may come from OBJFILE acting only as a symbol file without being
452 loaded into the target (see add_symbol_file_command). This case is
453 such fallback to the file VMA address without the possibility of
454 having the section relocated to its actual in-memory address. */
456 dyn_addr = bfd_section_vma (abfd, sect);
459 /* Read in .dynamic from the BFD. We will get the actual value
460 from memory later. */
461 sect_size = bfd_section_size (abfd, sect);
462 buf = bufstart = alloca (sect_size);
463 if (!bfd_get_section_contents (abfd, sect,
467 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
468 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
469 : sizeof (Elf64_External_Dyn);
470 for (bufend = buf + sect_size;
476 x_dynp_32 = (Elf32_External_Dyn *) buf;
477 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
478 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
482 x_dynp_64 = (Elf64_External_Dyn *) buf;
483 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
484 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
486 if (dyn_tag == DT_NULL)
488 if (dyn_tag == dyntag)
490 /* If requested, try to read the runtime value of this .dynamic
494 struct type *ptr_type;
498 ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
499 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
500 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
501 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
511 /* If no open symbol file, attempt to locate and open the main symbol
514 If FROM_TTYP dereferences to a non-zero integer, allow messages to
515 be printed. This parameter is a pointer rather than an int because
516 open_symbol_file_object is called via catch_errors and
517 catch_errors requires a pointer argument. */
520 open_symbol_file_object (void *from_ttyp)
526 /* Given a loadmap and an address, return the displacement needed
527 to relocate the address. */
530 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
535 for (seg = 0; seg < map->nsegs; seg++)
536 if (map->segs[seg].p_vaddr <= addr
537 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
538 return map->segs[seg].addr - map->segs[seg].p_vaddr;
543 /* Return the address from which the link map chain may be found. On
544 DSBT, a pointer to the start of the link map will be located at the
545 word found at base of GOT + GOT_MODULE_OFFSET.
547 The base of GOT may be found in a number of ways. Assuming that the
548 main executable has already been relocated,
549 1 The easiest way to find this value is to look up the address of
550 _GLOBAL_OFFSET_TABLE_.
551 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
552 address of Global Offset Table. .*/
557 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
558 struct minimal_symbol *got_sym;
560 gdb_byte buf[TIC6X_PTR_SIZE];
561 struct dsbt_info *info = get_dsbt_info ();
563 /* One of our assumptions is that the main executable has been relocated.
564 Bail out if this has not happened. (Note that post_create_inferior
565 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
566 If we allow this to happen, lm_base_cache will be initialized with
568 if (info->main_executable_lm_info == 0)
571 /* If we already have a cached value, return it. */
572 if (info->lm_base_cache)
573 return info->lm_base_cache;
575 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
580 addr = SYMBOL_VALUE_ADDRESS (got_sym);
581 if (solib_dsbt_debug)
582 fprintf_unfiltered (gdb_stdlog,
583 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
584 (unsigned int) addr);
586 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
588 struct int_elf32_dsbt_loadmap *ldm;
590 dsbt_get_initial_loadmaps ();
591 ldm = info->exec_loadmap;
592 addr += displacement_from_map (ldm, addr);
593 if (solib_dsbt_debug)
594 fprintf_unfiltered (gdb_stdlog,
595 "lm_base: get addr %x by DT_PLTGOT.\n",
596 (unsigned int) addr);
600 if (solib_dsbt_debug)
601 fprintf_unfiltered (gdb_stdlog,
602 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
605 addr += GOT_MODULE_OFFSET;
607 if (solib_dsbt_debug)
608 fprintf_unfiltered (gdb_stdlog,
609 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
610 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
612 if (target_read_memory (addr, buf, sizeof buf) != 0)
614 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
616 if (solib_dsbt_debug)
617 fprintf_unfiltered (gdb_stdlog,
618 "lm_base: lm_base_cache = %s\n",
619 hex_string_custom (info->lm_base_cache, 8));
621 return info->lm_base_cache;
625 /* Build a list of `struct so_list' objects describing the shared
626 objects currently loaded in the inferior. This list does not
627 include an entry for the main executable file.
629 Note that we only gather information directly available from the
630 inferior --- we don't examine any of the shared library files
631 themselves. The declaration of `struct so_list' says which fields
632 we provide values for. */
634 static struct so_list *
635 dsbt_current_sos (void)
637 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
639 struct so_list *sos_head = NULL;
640 struct so_list **sos_next_ptr = &sos_head;
641 struct dsbt_info *info = get_dsbt_info ();
643 /* Make sure that the main executable has been relocated. This is
644 required in order to find the address of the global offset table,
645 which in turn is used to find the link map info. (See lm_base
648 Note that the relocation of the main executable is also performed
649 by SOLIB_CREATE_INFERIOR_HOOK, however, in the case of core
650 files, this hook is called too late in order to be of benefit to
651 SOLIB_ADD. SOLIB_ADD eventually calls this function,
652 dsbt_current_sos, and also precedes the call to
653 SOLIB_CREATE_INFERIOR_HOOK. (See post_create_inferior in
655 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
656 dsbt_relocate_main_executable ();
658 /* Locate the address of the first link map struct. */
659 lm_addr = lm_base ();
661 /* We have at least one link map entry. Fetch the the lot of them,
662 building the solist chain. */
665 struct ext_link_map lm_buf;
666 ext_Elf32_Word indexword;
671 if (solib_dsbt_debug)
672 fprintf_unfiltered (gdb_stdlog,
673 "current_sos: reading link_map entry at %s\n",
674 hex_string_custom (lm_addr, 8));
676 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
679 warning (_("dsbt_current_sos: Unable to read link map entry."
680 " Shared object chain may be incomplete."));
684 /* Fetch the load map address. */
685 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
686 sizeof lm_buf.l_addr.map,
689 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
693 warning (_("dsbt_current_sos: Unable to read dsbt index."
694 " Shared object chain may be incomplete."));
697 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
700 /* If the DSBT index is zero, then we're looking at the entry
701 for the main executable. By convention, we don't include
702 this in the list of shared objects. */
707 struct int_elf32_dsbt_loadmap *loadmap;
711 loadmap = fetch_loadmap (map_addr);
714 warning (_("dsbt_current_sos: Unable to fetch load map."
715 " Shared object chain may be incomplete."));
719 sop = xcalloc (1, sizeof (struct so_list));
720 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
721 sop->lm_info->map = loadmap;
722 /* Fetch the name. */
723 addr = extract_unsigned_integer (lm_buf.l_name,
724 sizeof (lm_buf.l_name),
726 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
730 warning (_("Can't read pathname for link map entry: %s."),
731 safe_strerror (errcode));
734 if (solib_dsbt_debug)
735 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
738 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
739 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
741 strcpy (sop->so_original_name, sop->so_name);
745 sos_next_ptr = &sop->next;
749 info->main_lm_addr = lm_addr;
752 lm_addr = extract_unsigned_integer (lm_buf.l_next,
753 sizeof (lm_buf.l_next), byte_order);
761 /* Return 1 if PC lies in the dynamic symbol resolution code of the
765 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
767 struct dsbt_info *info = get_dsbt_info ();
769 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
770 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
771 || in_plt_section (pc, NULL));
774 /* Print a warning about being unable to set the dynamic linker
778 enable_break_failure_warning (void)
780 warning (_("Unable to find dynamic linker breakpoint function.\n"
781 "GDB will be unable to debug shared library initializers\n"
782 "and track explicitly loaded dynamic code."));
785 /* Helper function for gdb_bfd_lookup_symbol. */
788 cmp_name (asymbol *sym, void *data)
790 return (strcmp (sym->name, (const char *) data) == 0);
793 /* The dynamic linkers has, as part of its debugger interface, support
794 for arranging for the inferior to hit a breakpoint after mapping in
795 the shared libraries. This function enables that breakpoint.
797 On the TIC6X, using the shared library (DSBT), the symbol
798 _dl_debug_addr points to the r_debug struct which contains
799 a field called r_brk. r_brk is the address of the function
800 descriptor upon which a breakpoint must be placed. Being a
801 function descriptor, we must extract the entry point in order
802 to set the breakpoint.
804 Our strategy will be to get the .interp section from the
805 executable. This section will provide us with the name of the
806 interpreter. We'll open the interpreter and then look up
807 the address of _dl_debug_addr. We then relocate this address
808 using the interpreter's loadmap. Once the relocated address
809 is known, we fetch the value (address) corresponding to r_brk
810 and then use that value to fetch the entry point of the function
811 we're interested in. */
816 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
819 asection *interp_sect;
820 struct dsbt_info *info = get_dsbt_info ();
822 if (exec_bfd == NULL)
825 if (!target_has_execution)
828 if (info->enable_break2_done)
831 info->interp_text_sect_low = 0;
832 info->interp_text_sect_high = 0;
833 info->interp_plt_sect_low = 0;
834 info->interp_plt_sect_high = 0;
836 /* Find the .interp section; if not found, warn the user and drop
837 into the old breakpoint at symbol code. */
838 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
841 unsigned int interp_sect_size;
845 CORE_ADDR addr, interp_loadmap_addr;
846 gdb_byte addr_buf[TIC6X_PTR_SIZE];
847 struct int_elf32_dsbt_loadmap *ldm;
848 volatile struct gdb_exception ex;
850 /* Read the contents of the .interp section into a local buffer;
851 the contents specify the dynamic linker this program uses. */
852 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
853 buf = alloca (interp_sect_size);
854 bfd_get_section_contents (exec_bfd, interp_sect,
855 buf, 0, interp_sect_size);
857 /* Now we need to figure out where the dynamic linker was
858 loaded so that we can load its symbols and place a breakpoint
859 in the dynamic linker itself. */
861 TRY_CATCH (ex, RETURN_MASK_ALL)
863 tmp_bfd = solib_bfd_open (buf);
867 enable_break_failure_warning ();
871 dsbt_get_initial_loadmaps ();
872 ldm = info->interp_loadmap;
874 /* Record the relocated start and end address of the dynamic linker
875 text and plt section for dsbt_in_dynsym_resolve_code. */
876 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
879 info->interp_text_sect_low
880 = bfd_section_vma (tmp_bfd, interp_sect);
881 info->interp_text_sect_low
882 += displacement_from_map (ldm, info->interp_text_sect_low);
883 info->interp_text_sect_high
884 = info->interp_text_sect_low
885 + bfd_section_size (tmp_bfd, interp_sect);
887 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
890 info->interp_plt_sect_low =
891 bfd_section_vma (tmp_bfd, interp_sect);
892 info->interp_plt_sect_low
893 += displacement_from_map (ldm, info->interp_plt_sect_low);
894 info->interp_plt_sect_high =
895 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
898 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_addr");
901 warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
902 enable_break_failure_warning ();
907 if (solib_dsbt_debug)
908 fprintf_unfiltered (gdb_stdlog,
909 "enable_break: _dl_debug_addr (prior to relocation) = %s\n",
910 hex_string_custom (addr, 8));
912 addr += displacement_from_map (ldm, addr);
914 if (solib_dsbt_debug)
915 fprintf_unfiltered (gdb_stdlog,
916 "enable_break: _dl_debug_addr (after relocation) = %s\n",
917 hex_string_custom (addr, 8));
919 /* Fetch the address of the r_debug struct. */
920 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
922 warning (_("Unable to fetch contents of _dl_debug_addr "
923 "(at address %s) from dynamic linker"),
924 hex_string_custom (addr, 8));
926 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
928 if (solib_dsbt_debug)
929 fprintf_unfiltered (gdb_stdlog,
930 "enable_break: _dl_debug_addr[0..3] = %s\n",
931 hex_string_custom (addr, 8));
933 /* If it's zero, then the ldso hasn't initialized yet, and so
934 there are no shared libs yet loaded. */
937 if (solib_dsbt_debug)
938 fprintf_unfiltered (gdb_stdlog,
939 "enable_break: ldso not yet initialized\n");
940 /* Do not warn, but mark to run again. */
944 /* Fetch the r_brk field. It's 8 bytes from the start of
946 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
948 warning (_("Unable to fetch _dl_debug_addr->r_brk "
949 "(at address %s) from dynamic linker"),
950 hex_string_custom (addr + 8, 8));
951 enable_break_failure_warning ();
955 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
957 /* We're done with the temporary bfd. */
960 /* We're also done with the loadmap. */
963 /* Remove all the solib event breakpoints. Their addresses
964 may have changed since the last time we ran the program. */
965 remove_solib_event_breakpoints ();
967 /* Now (finally!) create the solib breakpoint. */
968 create_solib_event_breakpoint (target_gdbarch, addr);
970 info->enable_break2_done = 1;
975 /* Tell the user we couldn't set a dynamic linker breakpoint. */
976 enable_break_failure_warning ();
978 /* Failure return. */
985 asection *interp_sect;
986 struct minimal_symbol *start;
988 /* Check for the presence of a .interp section. If there is no
989 such section, the executable is statically linked. */
991 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
993 if (interp_sect == NULL)
995 if (solib_dsbt_debug)
996 fprintf_unfiltered (gdb_stdlog,
997 "enable_break: No .interp section found.\n");
1001 start = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1004 if (solib_dsbt_debug)
1005 fprintf_unfiltered (gdb_stdlog,
1006 "enable_break: symbol _start is not found.\n");
1010 create_solib_event_breakpoint (target_gdbarch,
1011 SYMBOL_VALUE_ADDRESS (start));
1013 if (solib_dsbt_debug)
1014 fprintf_unfiltered (gdb_stdlog,
1015 "enable_break: solib event breakpoint placed at : %s\n",
1016 hex_string_custom (SYMBOL_VALUE_ADDRESS (start), 8));
1020 /* Once the symbols from a shared object have been loaded in the usual
1021 way, we are called to do any system specific symbol handling that
1025 dsbt_special_symbol_handling (void)
1030 dsbt_relocate_main_executable (void)
1033 CORE_ADDR exec_addr, interp_addr;
1034 struct int_elf32_dsbt_loadmap *ldm;
1035 struct cleanup *old_chain;
1036 struct section_offsets *new_offsets;
1038 struct obj_section *osect;
1039 struct dsbt_info *info = get_dsbt_info ();
1041 dsbt_get_initial_loadmaps ();
1042 ldm = info->exec_loadmap;
1044 xfree (info->main_executable_lm_info);
1045 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
1046 info->main_executable_lm_info->map = ldm;
1048 new_offsets = xcalloc (symfile_objfile->num_sections,
1049 sizeof (struct section_offsets));
1050 old_chain = make_cleanup (xfree, new_offsets);
1053 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
1055 CORE_ADDR orig_addr, addr, offset;
1059 osect_idx = osect->the_bfd_section->index;
1061 /* Current address of section. */
1062 addr = obj_section_addr (osect);
1063 /* Offset from where this section started. */
1064 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
1065 /* Original address prior to any past relocations. */
1066 orig_addr = addr - offset;
1068 for (seg = 0; seg < ldm->nsegs; seg++)
1070 if (ldm->segs[seg].p_vaddr <= orig_addr
1071 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
1073 new_offsets->offsets[osect_idx]
1074 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
1076 if (new_offsets->offsets[osect_idx] != offset)
1084 objfile_relocate (symfile_objfile, new_offsets);
1086 do_cleanups (old_chain);
1088 /* Now that symfile_objfile has been relocated, we can compute the
1089 GOT value and stash it away. */
1092 /* When gdb starts up the inferior, it nurses it along (through the
1093 shell) until it is ready to execute it's first instruction. At this
1094 point, this function gets called via expansion of the macro
1095 SOLIB_CREATE_INFERIOR_HOOK.
1097 For the DSBT shared library, the main executable needs to be relocated.
1098 The shared library breakpoints also need to be enabled.
1102 dsbt_solib_create_inferior_hook (int from_tty)
1104 /* Relocate main executable. */
1105 dsbt_relocate_main_executable ();
1107 /* Enable shared library breakpoints. */
1108 if (!enable_break ())
1110 warning (_("shared library handler failed to enable breakpoint"));
1116 dsbt_clear_solib (void)
1118 struct dsbt_info *info = get_dsbt_info ();
1120 info->lm_base_cache = 0;
1121 info->enable_break2_done = 0;
1122 info->main_lm_addr = 0;
1123 if (info->main_executable_lm_info != 0)
1125 xfree (info->main_executable_lm_info->map);
1126 xfree (info->main_executable_lm_info);
1127 info->main_executable_lm_info = 0;
1132 dsbt_free_so (struct so_list *so)
1134 xfree (so->lm_info->map);
1135 xfree (so->lm_info);
1139 dsbt_relocate_section_addresses (struct so_list *so,
1140 struct target_section *sec)
1143 struct int_elf32_dsbt_loadmap *map;
1145 map = so->lm_info->map;
1147 for (seg = 0; seg < map->nsegs; seg++)
1149 if (map->segs[seg].p_vaddr <= sec->addr
1150 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1152 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1155 sec->endaddr += displ;
1161 show_dsbt_debug (struct ui_file *file, int from_tty,
1162 struct cmd_list_element *c, const char *value)
1164 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1167 struct target_so_ops dsbt_so_ops;
1169 /* Provide a prototype to silence -Wmissing-prototypes. */
1170 extern initialize_file_ftype _initialize_dsbt_solib;
1173 _initialize_dsbt_solib (void)
1175 solib_dsbt_pspace_data
1176 = register_program_space_data_with_cleanup (dsbt_pspace_data_cleanup);
1178 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1179 dsbt_so_ops.free_so = dsbt_free_so;
1180 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1181 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1182 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1183 dsbt_so_ops.current_sos = dsbt_current_sos;
1184 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1185 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1186 dsbt_so_ops.bfd_open = solib_bfd_open;
1188 /* Debug this file's internals. */
1189 add_setshow_zinteger_cmd ("solib-dsbt", class_maintenance,
1190 &solib_dsbt_debug, _("\
1191 Set internal debugging of shared library code for DSBT ELF."), _("\
1192 Show internal debugging of shared library code for DSBT ELF."), _("\
1193 When non-zero, DSBT solib specific internal debugging is enabled."),
1196 &setdebuglist, &showdebuglist);