1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010, 2011 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 /* An expensive way to lookup the value of a single symbol for
512 bfd's that are only temporary anyway. This is used by the
513 shared library support to find the address of the debugger
514 interface structures in the shared library.
516 Note that 0 is specifically allowed as an error return (no
520 bfd_lookup_symbol (bfd *abfd, char *symname)
524 asymbol **symbol_table;
525 unsigned int number_of_symbols;
527 struct cleanup *back_to;
528 CORE_ADDR symaddr = 0;
530 storage_needed = bfd_get_symtab_upper_bound (abfd);
532 if (storage_needed > 0)
534 symbol_table = (asymbol **) xmalloc (storage_needed);
535 back_to = make_cleanup (xfree, symbol_table);
536 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
538 for (i = 0; i < number_of_symbols; i++)
540 sym = *symbol_table++;
541 if (strcmp (sym->name, symname) == 0)
543 /* Bfd symbols are section relative. */
544 symaddr = sym->value + sym->section->vma;
548 do_cleanups (back_to);
554 /* Look for the symbol in the dynamic string table too. */
556 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
558 if (storage_needed > 0)
560 symbol_table = (asymbol **) xmalloc (storage_needed);
561 back_to = make_cleanup (xfree, symbol_table);
562 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
564 for (i = 0; i < number_of_symbols; i++)
566 sym = *symbol_table++;
567 if (strcmp (sym->name, symname) == 0)
569 /* Bfd symbols are section relative. */
570 symaddr = sym->value + sym->section->vma;
574 do_cleanups (back_to);
581 /* If no open symbol file, attempt to locate and open the main symbol
584 If FROM_TTYP dereferences to a non-zero integer, allow messages to
585 be printed. This parameter is a pointer rather than an int because
586 open_symbol_file_object is called via catch_errors and
587 catch_errors requires a pointer argument. */
590 open_symbol_file_object (void *from_ttyp)
596 /* Given a loadmap and an address, return the displacement needed
597 to relocate the address. */
600 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
605 for (seg = 0; seg < map->nsegs; seg++)
606 if (map->segs[seg].p_vaddr <= addr
607 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
608 return map->segs[seg].addr - map->segs[seg].p_vaddr;
613 /* Return the address from which the link map chain may be found. On
614 DSBT, a pointer to the start of the link map will be located at the
615 word found at base of GOT + GOT_MODULE_OFFSET.
617 The base of GOT may be found in a number of ways. Assuming that the
618 main executable has already been relocated,
619 1 The easiest way to find this value is to look up the address of
620 _GLOBAL_OFFSET_TABLE_.
621 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
622 address of Global Offset Table. .*/
627 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
628 struct minimal_symbol *got_sym;
630 gdb_byte buf[TIC6X_PTR_SIZE];
631 struct dsbt_info *info = get_dsbt_info ();
633 /* One of our assumptions is that the main executable has been relocated.
634 Bail out if this has not happened. (Note that post_create_inferior
635 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
636 If we allow this to happen, lm_base_cache will be initialized with
638 if (info->main_executable_lm_info == 0)
641 /* If we already have a cached value, return it. */
642 if (info->lm_base_cache)
643 return info->lm_base_cache;
645 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
650 addr = SYMBOL_VALUE_ADDRESS (got_sym);
651 if (solib_dsbt_debug)
652 fprintf_unfiltered (gdb_stdlog,
653 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
654 (unsigned int) addr);
656 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
658 struct int_elf32_dsbt_loadmap *ldm;
660 dsbt_get_initial_loadmaps ();
661 ldm = info->exec_loadmap;
662 addr += displacement_from_map (ldm, addr);
663 if (solib_dsbt_debug)
664 fprintf_unfiltered (gdb_stdlog,
665 "lm_base: get addr %x by DT_PLTGOT.\n",
666 (unsigned int) addr);
670 if (solib_dsbt_debug)
671 fprintf_unfiltered (gdb_stdlog,
672 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
675 addr += GOT_MODULE_OFFSET;
677 if (solib_dsbt_debug)
678 fprintf_unfiltered (gdb_stdlog,
679 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
680 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
682 if (target_read_memory (addr, buf, sizeof buf) != 0)
684 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
686 if (solib_dsbt_debug)
687 fprintf_unfiltered (gdb_stdlog,
688 "lm_base: lm_base_cache = %s\n",
689 hex_string_custom (info->lm_base_cache, 8));
691 return info->lm_base_cache;
695 /* Build a list of `struct so_list' objects describing the shared
696 objects currently loaded in the inferior. This list does not
697 include an entry for the main executable file.
699 Note that we only gather information directly available from the
700 inferior --- we don't examine any of the shared library files
701 themselves. The declaration of `struct so_list' says which fields
702 we provide values for. */
704 static struct so_list *
705 dsbt_current_sos (void)
707 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
709 struct so_list *sos_head = NULL;
710 struct so_list **sos_next_ptr = &sos_head;
711 struct dsbt_info *info = get_dsbt_info ();
713 /* Make sure that the main executable has been relocated. This is
714 required in order to find the address of the global offset table,
715 which in turn is used to find the link map info. (See lm_base
718 Note that the relocation of the main executable is also performed
719 by SOLIB_CREATE_INFERIOR_HOOK, however, in the case of core
720 files, this hook is called too late in order to be of benefit to
721 SOLIB_ADD. SOLIB_ADD eventually calls this function,
722 dsbt_current_sos, and also precedes the call to
723 SOLIB_CREATE_INFERIOR_HOOK. (See post_create_inferior in
725 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
726 dsbt_relocate_main_executable ();
728 /* Locate the address of the first link map struct. */
729 lm_addr = lm_base ();
731 /* We have at least one link map entry. Fetch the the lot of them,
732 building the solist chain. */
735 struct ext_link_map lm_buf;
736 ext_Elf32_Word indexword;
741 if (solib_dsbt_debug)
742 fprintf_unfiltered (gdb_stdlog,
743 "current_sos: reading link_map entry at %s\n",
744 hex_string_custom (lm_addr, 8));
746 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
749 warning (_("dsbt_current_sos: Unable to read link map entry."
750 " Shared object chain may be incomplete."));
754 /* Fetch the load map address. */
755 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
756 sizeof lm_buf.l_addr.map,
759 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
763 warning (_("dsbt_current_sos: Unable to read dsbt index."
764 " Shared object chain may be incomplete."));
767 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
770 /* If the DSBT index is zero, then we're looking at the entry
771 for the main executable. By convention, we don't include
772 this in the list of shared objects. */
777 struct int_elf32_dsbt_loadmap *loadmap;
781 loadmap = fetch_loadmap (map_addr);
784 warning (_("dsbt_current_sos: Unable to fetch load map."
785 " Shared object chain may be incomplete."));
789 sop = xcalloc (1, sizeof (struct so_list));
790 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
791 sop->lm_info->map = loadmap;
792 /* Fetch the name. */
793 addr = extract_unsigned_integer (lm_buf.l_name,
794 sizeof (lm_buf.l_name),
796 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
800 warning (_("Can't read pathname for link map entry: %s."),
801 safe_strerror (errcode));
804 if (solib_dsbt_debug)
805 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
808 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
809 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
811 strcpy (sop->so_original_name, sop->so_name);
815 sos_next_ptr = &sop->next;
819 info->main_lm_addr = lm_addr;
822 lm_addr = extract_unsigned_integer (lm_buf.l_next,
823 sizeof (lm_buf.l_next), byte_order);
831 /* Return 1 if PC lies in the dynamic symbol resolution code of the
835 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
837 struct dsbt_info *info = get_dsbt_info ();
839 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
840 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
841 || in_plt_section (pc, NULL));
844 /* Print a warning about being unable to set the dynamic linker
848 enable_break_failure_warning (void)
850 warning (_("Unable to find dynamic linker breakpoint function.\n"
851 "GDB will be unable to debug shared library initializers\n"
852 "and track explicitly loaded dynamic code."));
855 /* The dynamic linkers has, as part of its debugger interface, support
856 for arranging for the inferior to hit a breakpoint after mapping in
857 the shared libraries. This function enables that breakpoint.
859 On the TIC6X, using the shared library (DSBT), the symbol
860 _dl_debug_addr points to the r_debug struct which contains
861 a field called r_brk. r_brk is the address of the function
862 descriptor upon which a breakpoint must be placed. Being a
863 function descriptor, we must extract the entry point in order
864 to set the breakpoint.
866 Our strategy will be to get the .interp section from the
867 executable. This section will provide us with the name of the
868 interpreter. We'll open the interpreter and then look up
869 the address of _dl_debug_addr. We then relocate this address
870 using the interpreter's loadmap. Once the relocated address
871 is known, we fetch the value (address) corresponding to r_brk
872 and then use that value to fetch the entry point of the function
873 we're interested in. */
878 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
881 asection *interp_sect;
882 struct dsbt_info *info = get_dsbt_info ();
884 if (exec_bfd == NULL)
887 if (!target_has_execution)
890 if (info->enable_break2_done)
893 info->interp_text_sect_low = 0;
894 info->interp_text_sect_high = 0;
895 info->interp_plt_sect_low = 0;
896 info->interp_plt_sect_high = 0;
898 /* Find the .interp section; if not found, warn the user and drop
899 into the old breakpoint at symbol code. */
900 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
903 unsigned int interp_sect_size;
907 CORE_ADDR addr, interp_loadmap_addr;
908 gdb_byte addr_buf[TIC6X_PTR_SIZE];
909 struct int_elf32_dsbt_loadmap *ldm;
910 volatile struct gdb_exception ex;
912 /* Read the contents of the .interp section into a local buffer;
913 the contents specify the dynamic linker this program uses. */
914 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
915 buf = alloca (interp_sect_size);
916 bfd_get_section_contents (exec_bfd, interp_sect,
917 buf, 0, interp_sect_size);
919 /* Now we need to figure out where the dynamic linker was
920 loaded so that we can load its symbols and place a breakpoint
921 in the dynamic linker itself. */
923 TRY_CATCH (ex, RETURN_MASK_ALL)
925 tmp_bfd = solib_bfd_open (buf);
929 enable_break_failure_warning ();
933 dsbt_get_initial_loadmaps ();
934 ldm = info->interp_loadmap;
936 /* Record the relocated start and end address of the dynamic linker
937 text and plt section for dsbt_in_dynsym_resolve_code. */
938 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
941 info->interp_text_sect_low
942 = bfd_section_vma (tmp_bfd, interp_sect);
943 info->interp_text_sect_low
944 += displacement_from_map (ldm, info->interp_text_sect_low);
945 info->interp_text_sect_high
946 = info->interp_text_sect_low
947 + bfd_section_size (tmp_bfd, interp_sect);
949 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
952 info->interp_plt_sect_low =
953 bfd_section_vma (tmp_bfd, interp_sect);
954 info->interp_plt_sect_low
955 += displacement_from_map (ldm, info->interp_plt_sect_low);
956 info->interp_plt_sect_high =
957 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
960 addr = bfd_lookup_symbol (tmp_bfd, "_dl_debug_addr");
963 warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
964 enable_break_failure_warning ();
969 if (solib_dsbt_debug)
970 fprintf_unfiltered (gdb_stdlog,
971 "enable_break: _dl_debug_addr (prior to relocation) = %s\n",
972 hex_string_custom (addr, 8));
974 addr += displacement_from_map (ldm, addr);
976 if (solib_dsbt_debug)
977 fprintf_unfiltered (gdb_stdlog,
978 "enable_break: _dl_debug_addr (after relocation) = %s\n",
979 hex_string_custom (addr, 8));
981 /* Fetch the address of the r_debug struct. */
982 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
984 warning (_("Unable to fetch contents of _dl_debug_addr "
985 "(at address %s) from dynamic linker"),
986 hex_string_custom (addr, 8));
988 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
990 if (solib_dsbt_debug)
991 fprintf_unfiltered (gdb_stdlog,
992 "enable_break: _dl_debug_addr[0..3] = %s\n",
993 hex_string_custom (addr, 8));
995 /* If it's zero, then the ldso hasn't initialized yet, and so
996 there are no shared libs yet loaded. */
999 if (solib_dsbt_debug)
1000 fprintf_unfiltered (gdb_stdlog,
1001 "enable_break: ldso not yet initialized\n");
1002 /* Do not warn, but mark to run again. */
1006 /* Fetch the r_brk field. It's 8 bytes from the start of
1008 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
1010 warning (_("Unable to fetch _dl_debug_addr->r_brk "
1011 "(at address %s) from dynamic linker"),
1012 hex_string_custom (addr + 8, 8));
1013 enable_break_failure_warning ();
1014 bfd_close (tmp_bfd);
1017 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
1019 /* We're done with the temporary bfd. */
1020 bfd_close (tmp_bfd);
1022 /* We're also done with the loadmap. */
1025 /* Remove all the solib event breakpoints. Their addresses
1026 may have changed since the last time we ran the program. */
1027 remove_solib_event_breakpoints ();
1029 /* Now (finally!) create the solib breakpoint. */
1030 create_solib_event_breakpoint (target_gdbarch, addr);
1032 info->enable_break2_done = 1;
1037 /* Tell the user we couldn't set a dynamic linker breakpoint. */
1038 enable_break_failure_warning ();
1040 /* Failure return. */
1047 asection *interp_sect;
1048 struct minimal_symbol *start;
1050 /* Check for the presence of a .interp section. If there is no
1051 such section, the executable is statically linked. */
1053 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1055 if (interp_sect == NULL)
1057 if (solib_dsbt_debug)
1058 fprintf_unfiltered (gdb_stdlog,
1059 "enable_break: No .interp section found.\n");
1063 start = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1066 if (solib_dsbt_debug)
1067 fprintf_unfiltered (gdb_stdlog,
1068 "enable_break: symbol _start is not found.\n");
1072 create_solib_event_breakpoint (target_gdbarch,
1073 SYMBOL_VALUE_ADDRESS (start));
1075 if (solib_dsbt_debug)
1076 fprintf_unfiltered (gdb_stdlog,
1077 "enable_break: solib event breakpoint placed at : %s\n",
1078 hex_string_custom (SYMBOL_VALUE_ADDRESS (start), 8));
1082 /* Once the symbols from a shared object have been loaded in the usual
1083 way, we are called to do any system specific symbol handling that
1087 dsbt_special_symbol_handling (void)
1092 dsbt_relocate_main_executable (void)
1095 CORE_ADDR exec_addr, interp_addr;
1096 struct int_elf32_dsbt_loadmap *ldm;
1097 struct cleanup *old_chain;
1098 struct section_offsets *new_offsets;
1100 struct obj_section *osect;
1101 struct dsbt_info *info = get_dsbt_info ();
1103 dsbt_get_initial_loadmaps ();
1104 ldm = info->exec_loadmap;
1106 xfree (info->main_executable_lm_info);
1107 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
1108 info->main_executable_lm_info->map = ldm;
1110 new_offsets = xcalloc (symfile_objfile->num_sections,
1111 sizeof (struct section_offsets));
1112 old_chain = make_cleanup (xfree, new_offsets);
1115 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
1117 CORE_ADDR orig_addr, addr, offset;
1121 osect_idx = osect->the_bfd_section->index;
1123 /* Current address of section. */
1124 addr = obj_section_addr (osect);
1125 /* Offset from where this section started. */
1126 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
1127 /* Original address prior to any past relocations. */
1128 orig_addr = addr - offset;
1130 for (seg = 0; seg < ldm->nsegs; seg++)
1132 if (ldm->segs[seg].p_vaddr <= orig_addr
1133 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
1135 new_offsets->offsets[osect_idx]
1136 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
1138 if (new_offsets->offsets[osect_idx] != offset)
1146 objfile_relocate (symfile_objfile, new_offsets);
1148 do_cleanups (old_chain);
1150 /* Now that symfile_objfile has been relocated, we can compute the
1151 GOT value and stash it away. */
1154 /* When gdb starts up the inferior, it nurses it along (through the
1155 shell) until it is ready to execute it's first instruction. At this
1156 point, this function gets called via expansion of the macro
1157 SOLIB_CREATE_INFERIOR_HOOK.
1159 For the DSBT shared library, the main executable needs to be relocated.
1160 The shared library breakpoints also need to be enabled.
1164 dsbt_solib_create_inferior_hook (int from_tty)
1166 /* Relocate main executable. */
1167 dsbt_relocate_main_executable ();
1169 /* Enable shared library breakpoints. */
1170 if (!enable_break ())
1172 warning (_("shared library handler failed to enable breakpoint"));
1178 dsbt_clear_solib (void)
1180 struct dsbt_info *info = get_dsbt_info ();
1182 info->lm_base_cache = 0;
1183 info->enable_break2_done = 0;
1184 info->main_lm_addr = 0;
1185 if (info->main_executable_lm_info != 0)
1187 xfree (info->main_executable_lm_info->map);
1188 xfree (info->main_executable_lm_info);
1189 info->main_executable_lm_info = 0;
1194 dsbt_free_so (struct so_list *so)
1196 xfree (so->lm_info->map);
1197 xfree (so->lm_info);
1201 dsbt_relocate_section_addresses (struct so_list *so,
1202 struct target_section *sec)
1205 struct int_elf32_dsbt_loadmap *map;
1207 map = so->lm_info->map;
1209 for (seg = 0; seg < map->nsegs; seg++)
1211 if (map->segs[seg].p_vaddr <= sec->addr
1212 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1214 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1217 sec->endaddr += displ;
1223 show_dsbt_debug (struct ui_file *file, int from_tty,
1224 struct cmd_list_element *c, const char *value)
1226 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1229 struct target_so_ops dsbt_so_ops;
1231 /* Provide a prototype to silence -Wmissing-prototypes. */
1232 extern initialize_file_ftype _initialize_dsbt_solib;
1235 _initialize_dsbt_solib (void)
1237 solib_dsbt_pspace_data
1238 = register_program_space_data_with_cleanup (dsbt_pspace_data_cleanup);
1240 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1241 dsbt_so_ops.free_so = dsbt_free_so;
1242 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1243 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1244 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1245 dsbt_so_ops.current_sos = dsbt_current_sos;
1246 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1247 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1248 dsbt_so_ops.bfd_open = solib_bfd_open;
1250 /* Debug this file's internals. */
1251 add_setshow_zinteger_cmd ("solib-dsbt", class_maintenance,
1252 &solib_dsbt_debug, _("\
1253 Set internal debugging of shared library code for DSBT ELF."), _("\
1254 Show internal debugging of shared library code for DSBT ELF."), _("\
1255 When non-zero, DSBT solib specific internal debugging is enabled."),
1258 &setdebuglist, &showdebuglist);