merge from gcc
[gdb/gnu.git] / gdb / solib-dsbt.c
blob4fe24f82e6e98002642f2a99eea10cbafc436695
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/>. */
20 #include "defs.h"
21 #include "gdb_string.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "solib.h"
25 #include "solist.h"
26 #include "objfiles.h"
27 #include "symtab.h"
28 #include "language.h"
29 #include "command.h"
30 #include "gdbcmd.h"
31 #include "elf-bfd.h"
32 #include "exceptions.h"
33 #include "gdb_bfd.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. */
55 ext_Elf32_Addr addr;
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
66 module. */
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. */
71 ext_Elf32_Word nsegs;
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. */
82 CORE_ADDR addr;
83 /* VMA recorded in the program header. */
84 CORE_ADDR p_vaddr;
85 /* Size of this segment in memory. */
86 long p_memsz;
89 struct int_elf32_dsbt_loadmap
91 /* Protocol version number, must be zero. */
92 int version;
93 CORE_ADDR dsbt_table_ptr;
94 /* A pointer to the DSBT table; the DSBT size and the index of this
95 module. */
96 int dsbt_size, dsbt_index;
97 /* Number of segments in this map. */
98 int nsegs;
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; */
112 struct ext_link_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 */
128 struct lm_info
130 /* The loadmap, digested into an easier to use form. */
131 struct int_elf32_dsbt_loadmap *map;
134 /* Per pspace dsbt specific data. */
136 struct dsbt_info
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
147 each module. */
148 struct int_elf32_dsbt_loadmap *exec_loadmap;
149 struct int_elf32_dsbt_loadmap *interp_loadmap;
151 /* Cached value for lm_base, below. */
152 CORE_ADDR lm_base_cache;
154 /* Link map address for main module. */
155 CORE_ADDR main_lm_addr;
157 CORE_ADDR interp_text_sect_low;
158 CORE_ADDR interp_text_sect_high;
159 CORE_ADDR interp_plt_sect_low;
160 CORE_ADDR interp_plt_sect_high;
163 /* Per-program-space data key. */
164 static const struct program_space_data *solib_dsbt_pspace_data;
166 static void
167 dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
169 struct dsbt_info *info;
171 info = program_space_data (pspace, solib_dsbt_pspace_data);
172 xfree (info);
175 /* Get the current dsbt data. If none is found yet, add it now. This
176 function always returns a valid object. */
178 static struct dsbt_info *
179 get_dsbt_info (void)
181 struct dsbt_info *info;
183 info = program_space_data (current_program_space, solib_dsbt_pspace_data);
184 if (info != NULL)
185 return info;
187 info = XZALLOC (struct dsbt_info);
188 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
190 info->lm_base_cache = 0;
191 info->main_lm_addr = 0;
193 return info;
197 static void
198 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
200 int i;
202 if (map == NULL)
203 printf_filtered ("(null)\n");
204 else if (map->version != 0)
205 printf_filtered (_("Unsupported map version: %d\n"), map->version);
206 else
208 printf_filtered ("version %d\n", map->version);
210 for (i = 0; i < map->nsegs; i++)
211 printf_filtered ("%s:%s -> %s:%s\n",
212 print_core_address (target_gdbarch (),
213 map->segs[i].p_vaddr),
214 print_core_address (target_gdbarch (),
215 map->segs[i].p_vaddr
216 + map->segs[i].p_memsz),
217 print_core_address (target_gdbarch (), map->segs[i].addr),
218 print_core_address (target_gdbarch (), map->segs[i].addr
219 + map->segs[i].p_memsz));
223 /* Decode int_elf32_dsbt_loadmap from BUF. */
225 static struct int_elf32_dsbt_loadmap *
226 decode_loadmap (gdb_byte *buf)
228 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
229 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
230 struct int_elf32_dsbt_loadmap *int_ldmbuf;
232 int version, seg, nsegs;
233 int int_ldmbuf_size;
235 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
237 /* Extract the version. */
238 version = extract_unsigned_integer (ext_ldmbuf->version,
239 sizeof ext_ldmbuf->version,
240 byte_order);
241 if (version != 0)
243 /* We only handle version 0. */
244 return NULL;
247 /* Extract the number of segments. */
248 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
249 sizeof ext_ldmbuf->nsegs,
250 byte_order);
252 if (nsegs <= 0)
253 return NULL;
255 /* Allocate space into which to put information extract from the
256 external loadsegs. I.e, allocate the internal loadsegs. */
257 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
258 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
259 int_ldmbuf = xmalloc (int_ldmbuf_size);
261 /* Place extracted information in internal structs. */
262 int_ldmbuf->version = version;
263 int_ldmbuf->nsegs = nsegs;
264 for (seg = 0; seg < nsegs; seg++)
266 int_ldmbuf->segs[seg].addr
267 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
268 sizeof (ext_ldmbuf->segs[seg].addr),
269 byte_order);
270 int_ldmbuf->segs[seg].p_vaddr
271 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
272 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
273 byte_order);
274 int_ldmbuf->segs[seg].p_memsz
275 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
276 sizeof (ext_ldmbuf->segs[seg].p_memsz),
277 byte_order);
280 xfree (ext_ldmbuf);
281 return int_ldmbuf;
285 static struct dsbt_info *get_dsbt_info (void);
287 /* Interrogate the Linux kernel to find out where the program was loaded.
288 There are two load maps; one for the executable and one for the
289 interpreter (only in the case of a dynamically linked executable). */
291 static void
292 dsbt_get_initial_loadmaps (void)
294 gdb_byte *buf;
295 struct dsbt_info *info = get_dsbt_info ();
297 if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
298 "exec", &buf))
300 info->exec_loadmap = NULL;
301 error (_("Error reading DSBT exec loadmap"));
303 info->exec_loadmap = decode_loadmap (buf);
304 if (solib_dsbt_debug)
305 dsbt_print_loadmap (info->exec_loadmap);
307 if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
308 "interp", &buf))
310 info->interp_loadmap = NULL;
311 error (_("Error reading DSBT interp loadmap"));
313 info->interp_loadmap = decode_loadmap (buf);
314 if (solib_dsbt_debug)
315 dsbt_print_loadmap (info->interp_loadmap);
318 /* Given address LDMADDR, fetch and decode the loadmap at that address.
319 Return NULL if there is a problem reading the target memory or if
320 there doesn't appear to be a loadmap at the given address. The
321 allocated space (representing the loadmap) returned by this
322 function may be freed via a single call to xfree. */
324 static struct int_elf32_dsbt_loadmap *
325 fetch_loadmap (CORE_ADDR ldmaddr)
327 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
328 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
329 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
330 struct int_elf32_dsbt_loadmap *int_ldmbuf;
331 int ext_ldmbuf_size, int_ldmbuf_size;
332 int version, seg, nsegs;
334 /* Fetch initial portion of the loadmap. */
335 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
336 sizeof ext_ldmbuf_partial))
338 /* Problem reading the target's memory. */
339 return NULL;
342 /* Extract the version. */
343 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
344 sizeof ext_ldmbuf_partial.version,
345 byte_order);
346 if (version != 0)
348 /* We only handle version 0. */
349 return NULL;
352 /* Extract the number of segments. */
353 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
354 sizeof ext_ldmbuf_partial.nsegs,
355 byte_order);
357 if (nsegs <= 0)
358 return NULL;
360 /* Allocate space for the complete (external) loadmap. */
361 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
362 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
363 ext_ldmbuf = xmalloc (ext_ldmbuf_size);
365 /* Copy over the portion of the loadmap that's already been read. */
366 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
368 /* Read the rest of the loadmap from the target. */
369 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
370 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
371 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
373 /* Couldn't read rest of the loadmap. */
374 xfree (ext_ldmbuf);
375 return NULL;
378 /* Allocate space into which to put information extract from the
379 external loadsegs. I.e, allocate the internal loadsegs. */
380 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
381 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
382 int_ldmbuf = xmalloc (int_ldmbuf_size);
384 /* Place extracted information in internal structs. */
385 int_ldmbuf->version = version;
386 int_ldmbuf->nsegs = nsegs;
387 for (seg = 0; seg < nsegs; seg++)
389 int_ldmbuf->segs[seg].addr
390 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
391 sizeof (ext_ldmbuf->segs[seg].addr),
392 byte_order);
393 int_ldmbuf->segs[seg].p_vaddr
394 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
395 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
396 byte_order);
397 int_ldmbuf->segs[seg].p_memsz
398 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
399 sizeof (ext_ldmbuf->segs[seg].p_memsz),
400 byte_order);
403 xfree (ext_ldmbuf);
404 return int_ldmbuf;
407 static void dsbt_relocate_main_executable (void);
408 static int enable_break (void);
410 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
411 returned and the corresponding PTR is set. */
413 static int
414 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
416 int arch_size, step, sect_size;
417 long dyn_tag;
418 CORE_ADDR dyn_ptr, dyn_addr;
419 gdb_byte *bufend, *bufstart, *buf;
420 Elf32_External_Dyn *x_dynp_32;
421 Elf64_External_Dyn *x_dynp_64;
422 struct bfd_section *sect;
423 struct target_section *target_section;
425 if (abfd == NULL)
426 return 0;
428 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
429 return 0;
431 arch_size = bfd_get_arch_size (abfd);
432 if (arch_size == -1)
433 return 0;
435 /* Find the start address of the .dynamic section. */
436 sect = bfd_get_section_by_name (abfd, ".dynamic");
437 if (sect == NULL)
438 return 0;
440 for (target_section = current_target_sections->sections;
441 target_section < current_target_sections->sections_end;
442 target_section++)
443 if (sect == target_section->the_bfd_section)
444 break;
445 if (target_section < current_target_sections->sections_end)
446 dyn_addr = target_section->addr;
447 else
449 /* ABFD may come from OBJFILE acting only as a symbol file without being
450 loaded into the target (see add_symbol_file_command). This case is
451 such fallback to the file VMA address without the possibility of
452 having the section relocated to its actual in-memory address. */
454 dyn_addr = bfd_section_vma (abfd, sect);
457 /* Read in .dynamic from the BFD. We will get the actual value
458 from memory later. */
459 sect_size = bfd_section_size (abfd, sect);
460 buf = bufstart = alloca (sect_size);
461 if (!bfd_get_section_contents (abfd, sect,
462 buf, 0, sect_size))
463 return 0;
465 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
466 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
467 : sizeof (Elf64_External_Dyn);
468 for (bufend = buf + sect_size;
469 buf < bufend;
470 buf += step)
472 if (arch_size == 32)
474 x_dynp_32 = (Elf32_External_Dyn *) buf;
475 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
476 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
478 else
480 x_dynp_64 = (Elf64_External_Dyn *) buf;
481 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
482 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
484 if (dyn_tag == DT_NULL)
485 return 0;
486 if (dyn_tag == dyntag)
488 /* If requested, try to read the runtime value of this .dynamic
489 entry. */
490 if (ptr)
492 struct type *ptr_type;
493 gdb_byte ptr_buf[8];
494 CORE_ADDR ptr_addr;
496 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
497 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
498 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
499 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
500 *ptr = dyn_ptr;
502 return 1;
506 return 0;
509 /* If no open symbol file, attempt to locate and open the main symbol
510 file.
512 If FROM_TTYP dereferences to a non-zero integer, allow messages to
513 be printed. This parameter is a pointer rather than an int because
514 open_symbol_file_object is called via catch_errors and
515 catch_errors requires a pointer argument. */
517 static int
518 open_symbol_file_object (void *from_ttyp)
520 /* Unimplemented. */
521 return 0;
524 /* Given a loadmap and an address, return the displacement needed
525 to relocate the address. */
527 static CORE_ADDR
528 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
529 CORE_ADDR addr)
531 int seg;
533 for (seg = 0; seg < map->nsegs; seg++)
534 if (map->segs[seg].p_vaddr <= addr
535 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
536 return map->segs[seg].addr - map->segs[seg].p_vaddr;
538 return 0;
541 /* Return the address from which the link map chain may be found. On
542 DSBT, a pointer to the start of the link map will be located at the
543 word found at base of GOT + GOT_MODULE_OFFSET.
545 The base of GOT may be found in a number of ways. Assuming that the
546 main executable has already been relocated,
547 1 The easiest way to find this value is to look up the address of
548 _GLOBAL_OFFSET_TABLE_.
549 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
550 address of Global Offset Table. .*/
552 static CORE_ADDR
553 lm_base (void)
555 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
556 struct minimal_symbol *got_sym;
557 CORE_ADDR addr;
558 gdb_byte buf[TIC6X_PTR_SIZE];
559 struct dsbt_info *info = get_dsbt_info ();
561 /* One of our assumptions is that the main executable has been relocated.
562 Bail out if this has not happened. (Note that post_create_inferior
563 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
564 If we allow this to happen, lm_base_cache will be initialized with
565 a bogus value. */
566 if (info->main_executable_lm_info == 0)
567 return 0;
569 /* If we already have a cached value, return it. */
570 if (info->lm_base_cache)
571 return info->lm_base_cache;
573 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
574 symfile_objfile);
576 if (got_sym != 0)
578 addr = SYMBOL_VALUE_ADDRESS (got_sym);
579 if (solib_dsbt_debug)
580 fprintf_unfiltered (gdb_stdlog,
581 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
582 (unsigned int) addr);
584 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
586 struct int_elf32_dsbt_loadmap *ldm;
588 dsbt_get_initial_loadmaps ();
589 ldm = info->exec_loadmap;
590 addr += displacement_from_map (ldm, addr);
591 if (solib_dsbt_debug)
592 fprintf_unfiltered (gdb_stdlog,
593 "lm_base: get addr %x by DT_PLTGOT.\n",
594 (unsigned int) addr);
596 else
598 if (solib_dsbt_debug)
599 fprintf_unfiltered (gdb_stdlog,
600 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
601 return 0;
603 addr += GOT_MODULE_OFFSET;
605 if (solib_dsbt_debug)
606 fprintf_unfiltered (gdb_stdlog,
607 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
608 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
610 if (target_read_memory (addr, buf, sizeof buf) != 0)
611 return 0;
612 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
614 if (solib_dsbt_debug)
615 fprintf_unfiltered (gdb_stdlog,
616 "lm_base: lm_base_cache = %s\n",
617 hex_string_custom (info->lm_base_cache, 8));
619 return info->lm_base_cache;
623 /* Build a list of `struct so_list' objects describing the shared
624 objects currently loaded in the inferior. This list does not
625 include an entry for the main executable file.
627 Note that we only gather information directly available from the
628 inferior --- we don't examine any of the shared library files
629 themselves. The declaration of `struct so_list' says which fields
630 we provide values for. */
632 static struct so_list *
633 dsbt_current_sos (void)
635 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
636 CORE_ADDR lm_addr;
637 struct so_list *sos_head = NULL;
638 struct so_list **sos_next_ptr = &sos_head;
639 struct dsbt_info *info = get_dsbt_info ();
641 /* Make sure that the main executable has been relocated. This is
642 required in order to find the address of the global offset table,
643 which in turn is used to find the link map info. (See lm_base
644 for details.)
646 Note that the relocation of the main executable is also performed
647 by solib_create_inferior_hook, however, in the case of core
648 files, this hook is called too late in order to be of benefit to
649 solib_add. solib_add eventually calls this function,
650 dsbt_current_sos, and also precedes the call to
651 solib_create_inferior_hook. (See post_create_inferior in
652 infcmd.c.) */
653 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
654 dsbt_relocate_main_executable ();
656 /* Locate the address of the first link map struct. */
657 lm_addr = lm_base ();
659 /* We have at least one link map entry. Fetch the the lot of them,
660 building the solist chain. */
661 while (lm_addr)
663 struct ext_link_map lm_buf;
664 ext_Elf32_Word indexword;
665 CORE_ADDR map_addr;
666 int dsbt_index;
667 int ret;
669 if (solib_dsbt_debug)
670 fprintf_unfiltered (gdb_stdlog,
671 "current_sos: reading link_map entry at %s\n",
672 hex_string_custom (lm_addr, 8));
674 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
675 if (ret)
677 warning (_("dsbt_current_sos: Unable to read link map entry."
678 " Shared object chain may be incomplete."));
679 break;
682 /* Fetch the load map address. */
683 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
684 sizeof lm_buf.l_addr.map,
685 byte_order);
687 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
688 sizeof indexword);
689 if (ret)
691 warning (_("dsbt_current_sos: Unable to read dsbt index."
692 " Shared object chain may be incomplete."));
693 break;
695 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
696 byte_order);
698 /* If the DSBT index is zero, then we're looking at the entry
699 for the main executable. By convention, we don't include
700 this in the list of shared objects. */
701 if (dsbt_index != 0)
703 int errcode;
704 char *name_buf;
705 struct int_elf32_dsbt_loadmap *loadmap;
706 struct so_list *sop;
707 CORE_ADDR addr;
709 loadmap = fetch_loadmap (map_addr);
710 if (loadmap == NULL)
712 warning (_("dsbt_current_sos: Unable to fetch load map."
713 " Shared object chain may be incomplete."));
714 break;
717 sop = xcalloc (1, sizeof (struct so_list));
718 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
719 sop->lm_info->map = loadmap;
720 /* Fetch the name. */
721 addr = extract_unsigned_integer (lm_buf.l_name,
722 sizeof (lm_buf.l_name),
723 byte_order);
724 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
725 &errcode);
727 if (errcode != 0)
728 warning (_("Can't read pathname for link map entry: %s."),
729 safe_strerror (errcode));
730 else
732 if (solib_dsbt_debug)
733 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
734 name_buf);
736 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
737 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
738 xfree (name_buf);
739 strcpy (sop->so_original_name, sop->so_name);
742 *sos_next_ptr = sop;
743 sos_next_ptr = &sop->next;
745 else
747 info->main_lm_addr = lm_addr;
750 lm_addr = extract_unsigned_integer (lm_buf.l_next,
751 sizeof (lm_buf.l_next), byte_order);
754 return sos_head;
757 /* Return 1 if PC lies in the dynamic symbol resolution code of the
758 run time loader. */
760 static int
761 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
763 struct dsbt_info *info = get_dsbt_info ();
765 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
766 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
767 || in_plt_section (pc));
770 /* Print a warning about being unable to set the dynamic linker
771 breakpoint. */
773 static void
774 enable_break_failure_warning (void)
776 warning (_("Unable to find dynamic linker breakpoint function.\n"
777 "GDB will be unable to debug shared library initializers\n"
778 "and track explicitly loaded dynamic code."));
781 /* Helper function for gdb_bfd_lookup_symbol. */
783 static int
784 cmp_name (asymbol *sym, void *data)
786 return (strcmp (sym->name, (const char *) data) == 0);
789 /* The dynamic linkers has, as part of its debugger interface, support
790 for arranging for the inferior to hit a breakpoint after mapping in
791 the shared libraries. This function enables that breakpoint.
793 On the TIC6X, using the shared library (DSBT), GDB can try to place
794 a breakpoint on '_dl_debug_state' to monitor the shared library
795 event. */
797 static int
798 enable_break (void)
800 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
801 asection *interp_sect;
802 struct dsbt_info *info;
804 if (exec_bfd == NULL)
805 return 0;
807 if (!target_has_execution)
808 return 0;
810 info = get_dsbt_info ();
812 info->interp_text_sect_low = 0;
813 info->interp_text_sect_high = 0;
814 info->interp_plt_sect_low = 0;
815 info->interp_plt_sect_high = 0;
817 /* Find the .interp section; if not found, warn the user and drop
818 into the old breakpoint at symbol code. */
819 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
820 if (interp_sect)
822 unsigned int interp_sect_size;
823 char *buf;
824 bfd *tmp_bfd = NULL;
825 CORE_ADDR addr;
826 gdb_byte addr_buf[TIC6X_PTR_SIZE];
827 struct int_elf32_dsbt_loadmap *ldm;
828 volatile struct gdb_exception ex;
829 int ret;
831 /* Read the contents of the .interp section into a local buffer;
832 the contents specify the dynamic linker this program uses. */
833 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
834 buf = alloca (interp_sect_size);
835 bfd_get_section_contents (exec_bfd, interp_sect,
836 buf, 0, interp_sect_size);
838 /* Now we need to figure out where the dynamic linker was
839 loaded so that we can load its symbols and place a breakpoint
840 in the dynamic linker itself. */
842 TRY_CATCH (ex, RETURN_MASK_ALL)
844 tmp_bfd = solib_bfd_open (buf);
846 if (tmp_bfd == NULL)
848 enable_break_failure_warning ();
849 return 0;
852 dsbt_get_initial_loadmaps ();
853 ldm = info->interp_loadmap;
855 /* Record the relocated start and end address of the dynamic linker
856 text and plt section for dsbt_in_dynsym_resolve_code. */
857 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
858 if (interp_sect)
860 info->interp_text_sect_low
861 = bfd_section_vma (tmp_bfd, interp_sect);
862 info->interp_text_sect_low
863 += displacement_from_map (ldm, info->interp_text_sect_low);
864 info->interp_text_sect_high
865 = info->interp_text_sect_low
866 + bfd_section_size (tmp_bfd, interp_sect);
868 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
869 if (interp_sect)
871 info->interp_plt_sect_low =
872 bfd_section_vma (tmp_bfd, interp_sect);
873 info->interp_plt_sect_low
874 += displacement_from_map (ldm, info->interp_plt_sect_low);
875 info->interp_plt_sect_high =
876 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
879 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_state");
880 if (addr != 0)
882 if (solib_dsbt_debug)
883 fprintf_unfiltered (gdb_stdlog,
884 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
885 hex_string_custom (addr, 8));
886 addr += displacement_from_map (ldm, addr);
888 if (solib_dsbt_debug)
889 fprintf_unfiltered (gdb_stdlog,
890 "enable_break: _dl_debug_state (after relocation) = %s\n",
891 hex_string_custom (addr, 8));
893 /* Now (finally!) create the solib breakpoint. */
894 create_solib_event_breakpoint (target_gdbarch (), addr);
896 ret = 1;
898 else
900 if (solib_dsbt_debug)
901 fprintf_unfiltered (gdb_stdlog,
902 "enable_break: _dl_debug_state is not found\n");
903 ret = 0;
906 /* We're done with the temporary bfd. */
907 gdb_bfd_unref (tmp_bfd);
909 /* We're also done with the loadmap. */
910 xfree (ldm);
912 return ret;
915 /* Tell the user we couldn't set a dynamic linker breakpoint. */
916 enable_break_failure_warning ();
918 /* Failure return. */
919 return 0;
922 /* Once the symbols from a shared object have been loaded in the usual
923 way, we are called to do any system specific symbol handling that
924 is needed. */
926 static void
927 dsbt_special_symbol_handling (void)
931 static void
932 dsbt_relocate_main_executable (void)
934 struct int_elf32_dsbt_loadmap *ldm;
935 struct cleanup *old_chain;
936 struct section_offsets *new_offsets;
937 int changed;
938 struct obj_section *osect;
939 struct dsbt_info *info = get_dsbt_info ();
941 dsbt_get_initial_loadmaps ();
942 ldm = info->exec_loadmap;
944 xfree (info->main_executable_lm_info);
945 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
946 info->main_executable_lm_info->map = ldm;
948 new_offsets = xcalloc (symfile_objfile->num_sections,
949 sizeof (struct section_offsets));
950 old_chain = make_cleanup (xfree, new_offsets);
951 changed = 0;
953 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
955 CORE_ADDR orig_addr, addr, offset;
956 int osect_idx;
957 int seg;
959 osect_idx = osect - symfile_objfile->sections;
961 /* Current address of section. */
962 addr = obj_section_addr (osect);
963 /* Offset from where this section started. */
964 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
965 /* Original address prior to any past relocations. */
966 orig_addr = addr - offset;
968 for (seg = 0; seg < ldm->nsegs; seg++)
970 if (ldm->segs[seg].p_vaddr <= orig_addr
971 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
973 new_offsets->offsets[osect_idx]
974 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
976 if (new_offsets->offsets[osect_idx] != offset)
977 changed = 1;
978 break;
983 if (changed)
984 objfile_relocate (symfile_objfile, new_offsets);
986 do_cleanups (old_chain);
988 /* Now that symfile_objfile has been relocated, we can compute the
989 GOT value and stash it away. */
992 /* When gdb starts up the inferior, it nurses it along (through the
993 shell) until it is ready to execute it's first instruction. At this
994 point, this function gets called via solib_create_inferior_hook.
996 For the DSBT shared library, the main executable needs to be relocated.
997 The shared library breakpoints also need to be enabled. */
999 static void
1000 dsbt_solib_create_inferior_hook (int from_tty)
1002 /* Relocate main executable. */
1003 dsbt_relocate_main_executable ();
1005 /* Enable shared library breakpoints. */
1006 if (!enable_break ())
1008 warning (_("shared library handler failed to enable breakpoint"));
1009 return;
1013 static void
1014 dsbt_clear_solib (void)
1016 struct dsbt_info *info = get_dsbt_info ();
1018 info->lm_base_cache = 0;
1019 info->main_lm_addr = 0;
1020 if (info->main_executable_lm_info != 0)
1022 xfree (info->main_executable_lm_info->map);
1023 xfree (info->main_executable_lm_info);
1024 info->main_executable_lm_info = 0;
1028 static void
1029 dsbt_free_so (struct so_list *so)
1031 xfree (so->lm_info->map);
1032 xfree (so->lm_info);
1035 static void
1036 dsbt_relocate_section_addresses (struct so_list *so,
1037 struct target_section *sec)
1039 int seg;
1040 struct int_elf32_dsbt_loadmap *map;
1042 map = so->lm_info->map;
1044 for (seg = 0; seg < map->nsegs; seg++)
1046 if (map->segs[seg].p_vaddr <= sec->addr
1047 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1049 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1051 sec->addr += displ;
1052 sec->endaddr += displ;
1053 break;
1057 static void
1058 show_dsbt_debug (struct ui_file *file, int from_tty,
1059 struct cmd_list_element *c, const char *value)
1061 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1064 struct target_so_ops dsbt_so_ops;
1066 /* Provide a prototype to silence -Wmissing-prototypes. */
1067 extern initialize_file_ftype _initialize_dsbt_solib;
1069 void
1070 _initialize_dsbt_solib (void)
1072 solib_dsbt_pspace_data
1073 = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
1075 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1076 dsbt_so_ops.free_so = dsbt_free_so;
1077 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1078 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1079 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1080 dsbt_so_ops.current_sos = dsbt_current_sos;
1081 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1082 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1083 dsbt_so_ops.bfd_open = solib_bfd_open;
1085 /* Debug this file's internals. */
1086 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1087 &solib_dsbt_debug, _("\
1088 Set internal debugging of shared library code for DSBT ELF."), _("\
1089 Show internal debugging of shared library code for DSBT ELF."), _("\
1090 When non-zero, DSBT solib specific internal debugging is enabled."),
1091 NULL,
1092 show_dsbt_debug,
1093 &setdebuglist, &showdebuglist);