]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/solib-dsbt.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / solib-dsbt.c
1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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. */
37 static int solib_dsbt_debug = 0;
38
39 /* TIC6X pointers are four bytes wide. */
40 enum { 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. */
47 typedef gdb_byte ext_Elf32_Half[2];
48 typedef gdb_byte ext_Elf32_Addr[4];
49 typedef gdb_byte ext_Elf32_Word[4];
50
51 struct 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
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
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. */
78 struct 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
88 struct 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
104 typedef gdb_byte ext_ptr[4];
105
106 struct ext_elf32_dsbt_loadaddr
107 {
108 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
109 };
110
111 struct 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
127 struct 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
135 struct 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. */
165 static const struct program_space_data *solib_dsbt_pspace_data;
166
167 static void
168 dsbt_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
179 static struct dsbt_info *
180 get_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
199 static void
200 dsbt_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
227 static struct int_elf32_dsbt_loadmap *
228 decode_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
287 static 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
293 static void
294 dsbt_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
326 static struct int_elf32_dsbt_loadmap *
327 fetch_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
409 static void dsbt_relocate_main_executable (void);
410 static 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
415 static int
416 scan_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
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.
515
516 Note that 0 is specifically allowed as an error return (no
517 such symbol). */
518
519 static CORE_ADDR
520 bfd_lookup_symbol (bfd *abfd, char *symname)
521 {
522 long storage_needed;
523 asymbol *sym;
524 asymbol **symbol_table;
525 unsigned int number_of_symbols;
526 unsigned int i;
527 struct cleanup *back_to;
528 CORE_ADDR symaddr = 0;
529
530 storage_needed = bfd_get_symtab_upper_bound (abfd);
531
532 if (storage_needed > 0)
533 {
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);
537
538 for (i = 0; i < number_of_symbols; i++)
539 {
540 sym = *symbol_table++;
541 if (strcmp (sym->name, symname) == 0)
542 {
543 /* Bfd symbols are section relative. */
544 symaddr = sym->value + sym->section->vma;
545 break;
546 }
547 }
548 do_cleanups (back_to);
549 }
550
551 if (symaddr)
552 return symaddr;
553
554 /* Look for the symbol in the dynamic string table too. */
555
556 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
557
558 if (storage_needed > 0)
559 {
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);
563
564 for (i = 0; i < number_of_symbols; i++)
565 {
566 sym = *symbol_table++;
567 if (strcmp (sym->name, symname) == 0)
568 {
569 /* Bfd symbols are section relative. */
570 symaddr = sym->value + sym->section->vma;
571 break;
572 }
573 }
574 do_cleanups (back_to);
575 }
576
577 return symaddr;
578 }
579
580
581 /* If no open symbol file, attempt to locate and open the main symbol
582 file.
583
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. */
588
589 static int
590 open_symbol_file_object (void *from_ttyp)
591 {
592 /* Unimplemented. */
593 return 0;
594 }
595
596 /* Given a loadmap and an address, return the displacement needed
597 to relocate the address. */
598
599 static CORE_ADDR
600 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
601 CORE_ADDR addr)
602 {
603 int seg;
604
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;
609
610 return 0;
611 }
612
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.
616
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. .*/
623
624 static CORE_ADDR
625 lm_base (void)
626 {
627 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
628 struct minimal_symbol *got_sym;
629 CORE_ADDR addr;
630 gdb_byte buf[TIC6X_PTR_SIZE];
631 struct dsbt_info *info = get_dsbt_info ();
632
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
637 a bogus value. */
638 if (info->main_executable_lm_info == 0)
639 return 0;
640
641 /* If we already have a cached value, return it. */
642 if (info->lm_base_cache)
643 return info->lm_base_cache;
644
645 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
646 symfile_objfile);
647
648 if (got_sym != 0)
649 {
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);
655 }
656 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
657 {
658 struct int_elf32_dsbt_loadmap *ldm;
659
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);
667 }
668 else
669 {
670 if (solib_dsbt_debug)
671 fprintf_unfiltered (gdb_stdlog,
672 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
673 return 0;
674 }
675 addr += GOT_MODULE_OFFSET;
676
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));
681
682 if (target_read_memory (addr, buf, sizeof buf) != 0)
683 return 0;
684 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
685
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));
690
691 return info->lm_base_cache;
692 }
693
694
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.
698
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. */
703
704 static struct so_list *
705 dsbt_current_sos (void)
706 {
707 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
708 CORE_ADDR lm_addr;
709 struct so_list *sos_head = NULL;
710 struct so_list **sos_next_ptr = &sos_head;
711 struct dsbt_info *info = get_dsbt_info ();
712
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
716 for details.)
717
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
724 infcmd.c.) */
725 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
726 dsbt_relocate_main_executable ();
727
728 /* Locate the address of the first link map struct. */
729 lm_addr = lm_base ();
730
731 /* We have at least one link map entry. Fetch the the lot of them,
732 building the solist chain. */
733 while (lm_addr)
734 {
735 struct ext_link_map lm_buf;
736 ext_Elf32_Word indexword;
737 CORE_ADDR map_addr;
738 int dsbt_index;
739 int ret;
740
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));
745
746 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
747 if (ret)
748 {
749 warning (_("dsbt_current_sos: Unable to read link map entry."
750 " Shared object chain may be incomplete."));
751 break;
752 }
753
754 /* Fetch the load map address. */
755 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
756 sizeof lm_buf.l_addr.map,
757 byte_order);
758
759 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
760 sizeof indexword);
761 if (ret)
762 {
763 warning (_("dsbt_current_sos: Unable to read dsbt index."
764 " Shared object chain may be incomplete."));
765 break;
766 }
767 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
768 byte_order);
769
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. */
773 if (dsbt_index != 0)
774 {
775 int errcode;
776 char *name_buf;
777 struct int_elf32_dsbt_loadmap *loadmap;
778 struct so_list *sop;
779 CORE_ADDR addr;
780
781 loadmap = fetch_loadmap (map_addr);
782 if (loadmap == NULL)
783 {
784 warning (_("dsbt_current_sos: Unable to fetch load map."
785 " Shared object chain may be incomplete."));
786 break;
787 }
788
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),
795 byte_order);
796 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
797 &errcode);
798
799 if (errcode != 0)
800 warning (_("Can't read pathname for link map entry: %s."),
801 safe_strerror (errcode));
802 else
803 {
804 if (solib_dsbt_debug)
805 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
806 name_buf);
807
808 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
809 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
810 xfree (name_buf);
811 strcpy (sop->so_original_name, sop->so_name);
812 }
813
814 *sos_next_ptr = sop;
815 sos_next_ptr = &sop->next;
816 }
817 else
818 {
819 info->main_lm_addr = lm_addr;
820 }
821
822 lm_addr = extract_unsigned_integer (lm_buf.l_next,
823 sizeof (lm_buf.l_next), byte_order);
824 }
825
826 enable_break2 ();
827
828 return sos_head;
829 }
830
831 /* Return 1 if PC lies in the dynamic symbol resolution code of the
832 run time loader. */
833
834 static int
835 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
836 {
837 struct dsbt_info *info = get_dsbt_info ();
838
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));
842 }
843
844 /* Print a warning about being unable to set the dynamic linker
845 breakpoint. */
846
847 static void
848 enable_break_failure_warning (void)
849 {
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."));
853 }
854
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.
858
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.
865
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. */
874
875 static int
876 enable_break2 (void)
877 {
878 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
879 int success = 0;
880 char **bkpt_namep;
881 asection *interp_sect;
882 struct dsbt_info *info = get_dsbt_info ();
883
884 if (exec_bfd == NULL)
885 return 0;
886
887 if (!target_has_execution)
888 return 0;
889
890 if (info->enable_break2_done)
891 return 1;
892
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;
897
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");
901 if (interp_sect)
902 {
903 unsigned int interp_sect_size;
904 gdb_byte *buf;
905 bfd *tmp_bfd = NULL;
906 int status;
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;
911
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);
918
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. */
922
923 TRY_CATCH (ex, RETURN_MASK_ALL)
924 {
925 tmp_bfd = solib_bfd_open (buf);
926 }
927 if (tmp_bfd == NULL)
928 {
929 enable_break_failure_warning ();
930 return 0;
931 }
932
933 dsbt_get_initial_loadmaps ();
934 ldm = info->interp_loadmap;
935
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");
939 if (interp_sect)
940 {
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);
948 }
949 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
950 if (interp_sect)
951 {
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);
958 }
959
960 addr = bfd_lookup_symbol (tmp_bfd, "_dl_debug_addr");
961 if (addr == 0)
962 {
963 warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
964 enable_break_failure_warning ();
965 bfd_close (tmp_bfd);
966 return 0;
967 }
968
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));
973
974 addr += displacement_from_map (ldm, addr);
975
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));
980
981 /* Fetch the address of the r_debug struct. */
982 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
983 {
984 warning (_("Unable to fetch contents of _dl_debug_addr "
985 "(at address %s) from dynamic linker"),
986 hex_string_custom (addr, 8));
987 }
988 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
989
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));
994
995 /* If it's zero, then the ldso hasn't initialized yet, and so
996 there are no shared libs yet loaded. */
997 if (addr == 0)
998 {
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. */
1003 return 0;
1004 }
1005
1006 /* Fetch the r_brk field. It's 8 bytes from the start of
1007 _dl_debug_addr. */
1008 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
1009 {
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);
1015 return 0;
1016 }
1017 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
1018
1019 /* We're done with the temporary bfd. */
1020 bfd_close (tmp_bfd);
1021
1022 /* We're also done with the loadmap. */
1023 xfree (ldm);
1024
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 ();
1028
1029 /* Now (finally!) create the solib breakpoint. */
1030 create_solib_event_breakpoint (target_gdbarch, addr);
1031
1032 info->enable_break2_done = 1;
1033
1034 return 1;
1035 }
1036
1037 /* Tell the user we couldn't set a dynamic linker breakpoint. */
1038 enable_break_failure_warning ();
1039
1040 /* Failure return. */
1041 return 0;
1042 }
1043
1044 static int
1045 enable_break (void)
1046 {
1047 asection *interp_sect;
1048 struct minimal_symbol *start;
1049
1050 /* Check for the presence of a .interp section. If there is no
1051 such section, the executable is statically linked. */
1052
1053 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1054
1055 if (interp_sect == NULL)
1056 {
1057 if (solib_dsbt_debug)
1058 fprintf_unfiltered (gdb_stdlog,
1059 "enable_break: No .interp section found.\n");
1060 return 0;
1061 }
1062
1063 start = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1064 if (start == NULL)
1065 {
1066 if (solib_dsbt_debug)
1067 fprintf_unfiltered (gdb_stdlog,
1068 "enable_break: symbol _start is not found.\n");
1069 return 0;
1070 }
1071
1072 create_solib_event_breakpoint (target_gdbarch,
1073 SYMBOL_VALUE_ADDRESS (start));
1074
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));
1079 return 1;
1080 }
1081
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
1084 is needed. */
1085
1086 static void
1087 dsbt_special_symbol_handling (void)
1088 {
1089 }
1090
1091 static void
1092 dsbt_relocate_main_executable (void)
1093 {
1094 int status;
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;
1099 int changed;
1100 struct obj_section *osect;
1101 struct dsbt_info *info = get_dsbt_info ();
1102
1103 dsbt_get_initial_loadmaps ();
1104 ldm = info->exec_loadmap;
1105
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;
1109
1110 new_offsets = xcalloc (symfile_objfile->num_sections,
1111 sizeof (struct section_offsets));
1112 old_chain = make_cleanup (xfree, new_offsets);
1113 changed = 0;
1114
1115 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
1116 {
1117 CORE_ADDR orig_addr, addr, offset;
1118 int osect_idx;
1119 int seg;
1120
1121 osect_idx = osect->the_bfd_section->index;
1122
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;
1129
1130 for (seg = 0; seg < ldm->nsegs; seg++)
1131 {
1132 if (ldm->segs[seg].p_vaddr <= orig_addr
1133 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
1134 {
1135 new_offsets->offsets[osect_idx]
1136 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
1137
1138 if (new_offsets->offsets[osect_idx] != offset)
1139 changed = 1;
1140 break;
1141 }
1142 }
1143 }
1144
1145 if (changed)
1146 objfile_relocate (symfile_objfile, new_offsets);
1147
1148 do_cleanups (old_chain);
1149
1150 /* Now that symfile_objfile has been relocated, we can compute the
1151 GOT value and stash it away. */
1152 }
1153
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.
1158
1159 For the DSBT shared library, the main executable needs to be relocated.
1160 The shared library breakpoints also need to be enabled.
1161 */
1162
1163 static void
1164 dsbt_solib_create_inferior_hook (int from_tty)
1165 {
1166 /* Relocate main executable. */
1167 dsbt_relocate_main_executable ();
1168
1169 /* Enable shared library breakpoints. */
1170 if (!enable_break ())
1171 {
1172 warning (_("shared library handler failed to enable breakpoint"));
1173 return;
1174 }
1175 }
1176
1177 static void
1178 dsbt_clear_solib (void)
1179 {
1180 struct dsbt_info *info = get_dsbt_info ();
1181
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)
1186 {
1187 xfree (info->main_executable_lm_info->map);
1188 xfree (info->main_executable_lm_info);
1189 info->main_executable_lm_info = 0;
1190 }
1191 }
1192
1193 static void
1194 dsbt_free_so (struct so_list *so)
1195 {
1196 xfree (so->lm_info->map);
1197 xfree (so->lm_info);
1198 }
1199
1200 static void
1201 dsbt_relocate_section_addresses (struct so_list *so,
1202 struct target_section *sec)
1203 {
1204 int seg;
1205 struct int_elf32_dsbt_loadmap *map;
1206
1207 map = so->lm_info->map;
1208
1209 for (seg = 0; seg < map->nsegs; seg++)
1210 {
1211 if (map->segs[seg].p_vaddr <= sec->addr
1212 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1213 {
1214 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1215
1216 sec->addr += displ;
1217 sec->endaddr += displ;
1218 break;
1219 }
1220 }
1221 }
1222 static void
1223 show_dsbt_debug (struct ui_file *file, int from_tty,
1224 struct cmd_list_element *c, const char *value)
1225 {
1226 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1227 }
1228
1229 struct target_so_ops dsbt_so_ops;
1230
1231 /* Provide a prototype to silence -Wmissing-prototypes. */
1232 extern initialize_file_ftype _initialize_dsbt_solib;
1233
1234 void
1235 _initialize_dsbt_solib (void)
1236 {
1237 solib_dsbt_pspace_data
1238 = register_program_space_data_with_cleanup (dsbt_pspace_data_cleanup);
1239
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;
1249
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."),
1256 NULL,
1257 show_dsbt_debug,
1258 &setdebuglist, &showdebuglist);
1259 }