gdb/testsuite/
[deliverable/binutils-gdb.git] / gdb / solib-dsbt.c
CommitLineData
8cd64e00 1/* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
0b302171 2 Copyright (C) 2010-2012 Free Software Foundation, Inc.
8cd64e00
YQ
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19
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
34#define GOT_MODULE_OFFSET 4
35
36/* Flag which indicates whether internal debug messages should be printed. */
37static int solib_dsbt_debug = 0;
38
39/* TIC6X pointers are four bytes wide. */
40enum { TIC6X_PTR_SIZE = 4 };
41
42/* Representation of loadmap and related structs for the TIC6X DSBT. */
43
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. */
47typedef gdb_byte ext_Elf32_Half[2];
48typedef gdb_byte ext_Elf32_Addr[4];
49typedef gdb_byte ext_Elf32_Word[4];
50
51struct ext_elf32_dsbt_loadseg
52{
53 /* Core address to which the segment is mapped. */
54 ext_Elf32_Addr addr;
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;
59};
60
61struct 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
65 module. */
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. */
70 ext_Elf32_Word nsegs;
71 /* The actual memory map. */
72 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
73};
74
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. */
78struct int_elf32_dsbt_loadseg
79{
80 /* Core address to which the segment is mapped. */
81 CORE_ADDR addr;
82 /* VMA recorded in the program header. */
83 CORE_ADDR p_vaddr;
84 /* Size of this segment in memory. */
85 long p_memsz;
86};
87
88struct int_elf32_dsbt_loadmap
89{
90 /* Protocol version number, must be zero. */
91 int version;
92 CORE_ADDR dsbt_table_ptr;
93 /* A pointer to the DSBT table; the DSBT size and the index of this
94 module. */
95 int dsbt_size, dsbt_index;
96 /* Number of segments in this map. */
97 int nsegs;
98 /* The actual memory map. */
99 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
100};
101
102/* External link_map and elf32_dsbt_loadaddr struct definitions. */
103
104typedef gdb_byte ext_ptr[4];
105
106struct ext_elf32_dsbt_loadaddr
107{
108 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
109};
110
111struct ext_link_map
112{
113 struct ext_elf32_dsbt_loadaddr l_addr;
114
115 /* Absolute file name object was found in. */
116 ext_ptr l_name; /* char *l_name; */
117
118 /* Dynamic section of the shared object. */
119 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
120
121 /* Chain of loaded objects. */
122 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
123};
124
125/* Link map info to include in an allocated so_list entry */
126
127struct lm_info
128{
129 /* The loadmap, digested into an easier to use form. */
130 struct int_elf32_dsbt_loadmap *map;
131};
132
133/* Per pspace dsbt specific data. */
134
135struct dsbt_info
136{
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;
142
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
146 each module. */
147 struct int_elf32_dsbt_loadmap *exec_loadmap;
148 struct int_elf32_dsbt_loadmap *interp_loadmap;
149
150 /* Cached value for lm_base, below. */
151 CORE_ADDR lm_base_cache;
152
153 /* Link map address for main module. */
154 CORE_ADDR main_lm_addr;
155
156 int enable_break2_done;
157
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;
162};
163
164/* Per-program-space data key. */
165static const struct program_space_data *solib_dsbt_pspace_data;
166
167static void
168dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
169{
170 struct dsbt_info *info;
171
172 info = program_space_data (pspace, solib_dsbt_pspace_data);
173 xfree (info);
174}
175
176/* Get the current dsbt data. If none is found yet, add it now. This
177 function always returns a valid object. */
178
179static struct dsbt_info *
180get_dsbt_info (void)
181{
182 struct dsbt_info *info;
183
184 info = program_space_data (current_program_space, solib_dsbt_pspace_data);
185 if (info != NULL)
186 return info;
187
188 info = XZALLOC (struct dsbt_info);
189 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
190
191 info->enable_break2_done = 0;
192 info->lm_base_cache = 0;
193 info->main_lm_addr = 0;
194
195 return info;
196}
197
198
199static void
200dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
201{
202 int i;
203
204 if (map == NULL)
205 printf_filtered ("(null)\n");
206 else if (map->version != 0)
207 printf_filtered (_("Unsupported map version: %d\n"), map->version);
208 else
209 {
210 printf_filtered ("version %d\n", map->version);
211
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,
217 map->segs[i].p_vaddr
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));
222 }
223}
224
225/* Decode int_elf32_dsbt_loadmap from BUF. */
226
227static struct int_elf32_dsbt_loadmap *
228decode_loadmap (gdb_byte *buf)
229{
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;
233
234 int version, seg, nsegs;
235 int ext_ldmbuf_size, int_ldmbuf_size;
236
237 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
238
239 /* Extract the version. */
240 version = extract_unsigned_integer (ext_ldmbuf->version,
241 sizeof ext_ldmbuf->version,
242 byte_order);
243 if (version != 0)
244 {
245 /* We only handle version 0. */
246 return NULL;
247 }
248
249 /* Extract the number of segments. */
250 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
251 sizeof ext_ldmbuf->nsegs,
252 byte_order);
253
254 if (nsegs <= 0)
255 return NULL;
256
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);
262
263 /* Place extracted information in internal structs. */
264 int_ldmbuf->version = version;
265 int_ldmbuf->nsegs = nsegs;
266 for (seg = 0; seg < nsegs; seg++)
267 {
268 int_ldmbuf->segs[seg].addr
269 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
270 sizeof (ext_ldmbuf->segs[seg].addr),
271 byte_order);
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),
275 byte_order);
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),
279 byte_order);
280 }
281
282 xfree (ext_ldmbuf);
283 return int_ldmbuf;
284}
285
286
287static struct dsbt_info *get_dsbt_info (void);
288
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). */
292
293static void
294dsbt_get_initial_loadmaps (void)
295{
296 gdb_byte *buf;
297 struct dsbt_info *info = get_dsbt_info ();
298
299 if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
300 "exec", (gdb_byte**) &buf))
301 {
302 info->exec_loadmap = NULL;
303 error (_("Error reading DSBT exec loadmap"));
304 }
305 info->exec_loadmap = decode_loadmap (buf);
306 if (solib_dsbt_debug)
307 dsbt_print_loadmap (info->exec_loadmap);
308
309 if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
310 "interp", (gdb_byte**)&buf))
311 {
312 info->interp_loadmap = NULL;
313 error (_("Error reading DSBT interp loadmap"));
314 }
315 info->interp_loadmap = decode_loadmap (buf);
316 if (solib_dsbt_debug)
317 dsbt_print_loadmap (info->interp_loadmap);
318}
319
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. */
325
326static struct int_elf32_dsbt_loadmap *
327fetch_loadmap (CORE_ADDR ldmaddr)
328{
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;
335
336 /* Fetch initial portion of the loadmap. */
337 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
338 sizeof ext_ldmbuf_partial))
339 {
340 /* Problem reading the target's memory. */
341 return NULL;
342 }
343
344 /* Extract the version. */
345 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
346 sizeof ext_ldmbuf_partial.version,
347 byte_order);
348 if (version != 0)
349 {
350 /* We only handle version 0. */
351 return NULL;
352 }
353
354 /* Extract the number of segments. */
355 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
356 sizeof ext_ldmbuf_partial.nsegs,
357 byte_order);
358
359 if (nsegs <= 0)
360 return NULL;
361
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);
366
367 /* Copy over the portion of the loadmap that's already been read. */
368 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
369
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))
374 {
375 /* Couldn't read rest of the loadmap. */
376 xfree (ext_ldmbuf);
377 return NULL;
378 }
379
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);
385
386 /* Place extracted information in internal structs. */
387 int_ldmbuf->version = version;
388 int_ldmbuf->nsegs = nsegs;
389 for (seg = 0; seg < nsegs; seg++)
390 {
391 int_ldmbuf->segs[seg].addr
392 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
393 sizeof (ext_ldmbuf->segs[seg].addr),
394 byte_order);
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),
398 byte_order);
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),
402 byte_order);
403 }
404
405 xfree (ext_ldmbuf);
406 return int_ldmbuf;
407}
408
409static void dsbt_relocate_main_executable (void);
410static int enable_break2 (void);
411
412/* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
413 returned and the corresponding PTR is set. */
414
415static int
416scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
417{
418 int arch_size, step, sect_size;
419 long dyn_tag;
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;
426
427 if (abfd == NULL)
428 return 0;
429
430 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
431 return 0;
432
433 arch_size = bfd_get_arch_size (abfd);
434 if (arch_size == -1)
435 return 0;
436
437 /* Find the start address of the .dynamic section. */
438 sect = bfd_get_section_by_name (abfd, ".dynamic");
439 if (sect == NULL)
440 return 0;
441
442 for (target_section = current_target_sections->sections;
443 target_section < current_target_sections->sections_end;
444 target_section++)
445 if (sect == target_section->the_bfd_section)
446 break;
447 if (target_section < current_target_sections->sections_end)
448 dyn_addr = target_section->addr;
449 else
450 {
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. */
455
456 dyn_addr = bfd_section_vma (abfd, sect);
457 }
458
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,
464 buf, 0, sect_size))
465 return 0;
466
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;
471 buf < bufend;
472 buf += step)
473 {
474 if (arch_size == 32)
475 {
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);
479 }
480 else
481 {
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);
485 }
486 if (dyn_tag == DT_NULL)
487 return 0;
488 if (dyn_tag == dyntag)
489 {
490 /* If requested, try to read the runtime value of this .dynamic
491 entry. */
492 if (ptr)
493 {
494 struct type *ptr_type;
495 gdb_byte ptr_buf[8];
496 CORE_ADDR ptr_addr;
497
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);
502 *ptr = dyn_ptr;
503 }
504 return 1;
505 }
506 }
507
508 return 0;
509}
510
8cd64e00
YQ
511/* If no open symbol file, attempt to locate and open the main symbol
512 file.
513
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. */
518
519static int
520open_symbol_file_object (void *from_ttyp)
521{
522 /* Unimplemented. */
523 return 0;
524}
525
526/* Given a loadmap and an address, return the displacement needed
527 to relocate the address. */
528
529static CORE_ADDR
530displacement_from_map (struct int_elf32_dsbt_loadmap *map,
531 CORE_ADDR addr)
532{
533 int seg;
534
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;
539
540 return 0;
541}
542
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.
546
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. .*/
553
554static CORE_ADDR
555lm_base (void)
556{
557 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
558 struct minimal_symbol *got_sym;
559 CORE_ADDR addr;
560 gdb_byte buf[TIC6X_PTR_SIZE];
561 struct dsbt_info *info = get_dsbt_info ();
562
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
567 a bogus value. */
568 if (info->main_executable_lm_info == 0)
569 return 0;
570
571 /* If we already have a cached value, return it. */
572 if (info->lm_base_cache)
573 return info->lm_base_cache;
574
575 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
576 symfile_objfile);
577
578 if (got_sym != 0)
579 {
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);
585 }
586 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
587 {
588 struct int_elf32_dsbt_loadmap *ldm;
589
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);
597 }
598 else
599 {
600 if (solib_dsbt_debug)
601 fprintf_unfiltered (gdb_stdlog,
602 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
603 return 0;
604 }
605 addr += GOT_MODULE_OFFSET;
606
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));
611
612 if (target_read_memory (addr, buf, sizeof buf) != 0)
613 return 0;
614 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
615
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));
620
621 return info->lm_base_cache;
622}
623
624
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.
628
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. */
633
634static struct so_list *
635dsbt_current_sos (void)
636{
637 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
638 CORE_ADDR lm_addr;
639 struct so_list *sos_head = NULL;
640 struct so_list **sos_next_ptr = &sos_head;
641 struct dsbt_info *info = get_dsbt_info ();
642
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
646 for details.)
647
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
654 infcmd.c.) */
655 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
656 dsbt_relocate_main_executable ();
657
658 /* Locate the address of the first link map struct. */
659 lm_addr = lm_base ();
660
661 /* We have at least one link map entry. Fetch the the lot of them,
662 building the solist chain. */
663 while (lm_addr)
664 {
665 struct ext_link_map lm_buf;
666 ext_Elf32_Word indexword;
667 CORE_ADDR map_addr;
668 int dsbt_index;
669 int ret;
670
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));
675
676 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
677 if (ret)
678 {
679 warning (_("dsbt_current_sos: Unable to read link map entry."
680 " Shared object chain may be incomplete."));
681 break;
682 }
683
684 /* Fetch the load map address. */
685 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
686 sizeof lm_buf.l_addr.map,
687 byte_order);
688
689 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
690 sizeof indexword);
691 if (ret)
692 {
693 warning (_("dsbt_current_sos: Unable to read dsbt index."
694 " Shared object chain may be incomplete."));
695 break;
696 }
697 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
698 byte_order);
699
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. */
703 if (dsbt_index != 0)
704 {
705 int errcode;
706 char *name_buf;
707 struct int_elf32_dsbt_loadmap *loadmap;
708 struct so_list *sop;
709 CORE_ADDR addr;
710
711 loadmap = fetch_loadmap (map_addr);
712 if (loadmap == NULL)
713 {
714 warning (_("dsbt_current_sos: Unable to fetch load map."
715 " Shared object chain may be incomplete."));
716 break;
717 }
718
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),
725 byte_order);
726 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
727 &errcode);
728
729 if (errcode != 0)
730 warning (_("Can't read pathname for link map entry: %s."),
731 safe_strerror (errcode));
732 else
733 {
734 if (solib_dsbt_debug)
735 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
736 name_buf);
737
738 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
739 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
740 xfree (name_buf);
741 strcpy (sop->so_original_name, sop->so_name);
742 }
743
744 *sos_next_ptr = sop;
745 sos_next_ptr = &sop->next;
746 }
747 else
748 {
749 info->main_lm_addr = lm_addr;
750 }
751
752 lm_addr = extract_unsigned_integer (lm_buf.l_next,
753 sizeof (lm_buf.l_next), byte_order);
754 }
755
756 enable_break2 ();
757
758 return sos_head;
759}
760
761/* Return 1 if PC lies in the dynamic symbol resolution code of the
762 run time loader. */
763
764static int
765dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
766{
767 struct dsbt_info *info = get_dsbt_info ();
768
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));
772}
773
774/* Print a warning about being unable to set the dynamic linker
775 breakpoint. */
776
777static void
778enable_break_failure_warning (void)
779{
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."));
783}
784
cb457ae2
YQ
785/* Helper function for gdb_bfd_lookup_symbol. */
786
787static int
788cmp_name (asymbol *sym, void *data)
789{
790 return (strcmp (sym->name, (const char *) data) == 0);
791}
792
8cd64e00
YQ
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.
796
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.
803
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. */
812
813static int
814enable_break2 (void)
815{
816 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
817 int success = 0;
818 char **bkpt_namep;
819 asection *interp_sect;
820 struct dsbt_info *info = get_dsbt_info ();
821
822 if (exec_bfd == NULL)
823 return 0;
824
825 if (!target_has_execution)
826 return 0;
827
828 if (info->enable_break2_done)
829 return 1;
830
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;
835
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");
839 if (interp_sect)
840 {
841 unsigned int interp_sect_size;
842 gdb_byte *buf;
843 bfd *tmp_bfd = NULL;
844 int status;
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;
849
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);
856
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. */
860
861 TRY_CATCH (ex, RETURN_MASK_ALL)
862 {
863 tmp_bfd = solib_bfd_open (buf);
864 }
865 if (tmp_bfd == NULL)
866 {
867 enable_break_failure_warning ();
868 return 0;
869 }
870
871 dsbt_get_initial_loadmaps ();
872 ldm = info->interp_loadmap;
873
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");
877 if (interp_sect)
878 {
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);
886 }
887 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
888 if (interp_sect)
889 {
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);
896 }
897
cb457ae2 898 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_addr");
8cd64e00
YQ
899 if (addr == 0)
900 {
901 warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
902 enable_break_failure_warning ();
903 bfd_close (tmp_bfd);
904 return 0;
905 }
906
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));
911
912 addr += displacement_from_map (ldm, addr);
913
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));
918
919 /* Fetch the address of the r_debug struct. */
920 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
921 {
922 warning (_("Unable to fetch contents of _dl_debug_addr "
923 "(at address %s) from dynamic linker"),
924 hex_string_custom (addr, 8));
925 }
926 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
927
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));
932
933 /* If it's zero, then the ldso hasn't initialized yet, and so
934 there are no shared libs yet loaded. */
935 if (addr == 0)
936 {
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. */
941 return 0;
942 }
943
944 /* Fetch the r_brk field. It's 8 bytes from the start of
945 _dl_debug_addr. */
946 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
947 {
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 ();
952 bfd_close (tmp_bfd);
953 return 0;
954 }
955 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
956
957 /* We're done with the temporary bfd. */
958 bfd_close (tmp_bfd);
959
960 /* We're also done with the loadmap. */
961 xfree (ldm);
962
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 ();
966
967 /* Now (finally!) create the solib breakpoint. */
968 create_solib_event_breakpoint (target_gdbarch, addr);
969
970 info->enable_break2_done = 1;
971
972 return 1;
973 }
974
975 /* Tell the user we couldn't set a dynamic linker breakpoint. */
976 enable_break_failure_warning ();
977
978 /* Failure return. */
979 return 0;
980}
981
982static int
983enable_break (void)
984{
985 asection *interp_sect;
986 struct minimal_symbol *start;
987
988 /* Check for the presence of a .interp section. If there is no
989 such section, the executable is statically linked. */
990
991 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
992
993 if (interp_sect == NULL)
994 {
995 if (solib_dsbt_debug)
996 fprintf_unfiltered (gdb_stdlog,
997 "enable_break: No .interp section found.\n");
998 return 0;
999 }
1000
1001 start = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1002 if (start == NULL)
1003 {
1004 if (solib_dsbt_debug)
1005 fprintf_unfiltered (gdb_stdlog,
1006 "enable_break: symbol _start is not found.\n");
1007 return 0;
1008 }
1009
1010 create_solib_event_breakpoint (target_gdbarch,
1011 SYMBOL_VALUE_ADDRESS (start));
1012
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));
1017 return 1;
1018}
1019
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
1022 is needed. */
1023
1024static void
1025dsbt_special_symbol_handling (void)
1026{
1027}
1028
1029static void
1030dsbt_relocate_main_executable (void)
1031{
1032 int status;
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;
1037 int changed;
1038 struct obj_section *osect;
1039 struct dsbt_info *info = get_dsbt_info ();
1040
1041 dsbt_get_initial_loadmaps ();
1042 ldm = info->exec_loadmap;
1043
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;
1047
1048 new_offsets = xcalloc (symfile_objfile->num_sections,
1049 sizeof (struct section_offsets));
1050 old_chain = make_cleanup (xfree, new_offsets);
1051 changed = 0;
1052
1053 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
1054 {
1055 CORE_ADDR orig_addr, addr, offset;
1056 int osect_idx;
1057 int seg;
1058
1059 osect_idx = osect->the_bfd_section->index;
1060
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;
1067
1068 for (seg = 0; seg < ldm->nsegs; seg++)
1069 {
1070 if (ldm->segs[seg].p_vaddr <= orig_addr
1071 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
1072 {
1073 new_offsets->offsets[osect_idx]
1074 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
1075
1076 if (new_offsets->offsets[osect_idx] != offset)
1077 changed = 1;
1078 break;
1079 }
1080 }
1081 }
1082
1083 if (changed)
1084 objfile_relocate (symfile_objfile, new_offsets);
1085
1086 do_cleanups (old_chain);
1087
1088 /* Now that symfile_objfile has been relocated, we can compute the
1089 GOT value and stash it away. */
1090}
1091
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.
1096
1097 For the DSBT shared library, the main executable needs to be relocated.
1098 The shared library breakpoints also need to be enabled.
1099 */
1100
1101static void
1102dsbt_solib_create_inferior_hook (int from_tty)
1103{
1104 /* Relocate main executable. */
1105 dsbt_relocate_main_executable ();
1106
1107 /* Enable shared library breakpoints. */
1108 if (!enable_break ())
1109 {
1110 warning (_("shared library handler failed to enable breakpoint"));
1111 return;
1112 }
1113}
1114
1115static void
1116dsbt_clear_solib (void)
1117{
1118 struct dsbt_info *info = get_dsbt_info ();
1119
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)
1124 {
1125 xfree (info->main_executable_lm_info->map);
1126 xfree (info->main_executable_lm_info);
1127 info->main_executable_lm_info = 0;
1128 }
1129}
1130
1131static void
1132dsbt_free_so (struct so_list *so)
1133{
1134 xfree (so->lm_info->map);
1135 xfree (so->lm_info);
1136}
1137
1138static void
1139dsbt_relocate_section_addresses (struct so_list *so,
1140 struct target_section *sec)
1141{
1142 int seg;
1143 struct int_elf32_dsbt_loadmap *map;
1144
1145 map = so->lm_info->map;
1146
1147 for (seg = 0; seg < map->nsegs; seg++)
1148 {
1149 if (map->segs[seg].p_vaddr <= sec->addr
1150 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1151 {
1152 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1153
1154 sec->addr += displ;
1155 sec->endaddr += displ;
1156 break;
1157 }
1158 }
1159}
1160static void
1161show_dsbt_debug (struct ui_file *file, int from_tty,
1162 struct cmd_list_element *c, const char *value)
1163{
1164 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1165}
1166
1167struct target_so_ops dsbt_so_ops;
1168
1169/* Provide a prototype to silence -Wmissing-prototypes. */
1170extern initialize_file_ftype _initialize_dsbt_solib;
1171
1172void
1173_initialize_dsbt_solib (void)
1174{
1175 solib_dsbt_pspace_data
1176 = register_program_space_data_with_cleanup (dsbt_pspace_data_cleanup);
1177
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;
1187
1188 /* Debug this file's internals. */
1189 add_setshow_zinteger_cmd ("solib-dsbt", class_maintenance,
1190 &solib_dsbt_debug, _("\
1191Set internal debugging of shared library code for DSBT ELF."), _("\
1192Show internal debugging of shared library code for DSBT ELF."), _("\
1193When non-zero, DSBT solib specific internal debugging is enabled."),
1194 NULL,
1195 show_dsbt_debug,
1196 &setdebuglist, &showdebuglist);
1197}
This page took 0.159832 seconds and 4 git commands to generate.