gdb/
[deliverable/binutils-gdb.git] / gdb / solib-dsbt.c
1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2013 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 #include "gdb_bfd.h"
34
35 #define GOT_MODULE_OFFSET 4
36
37 /* Flag which indicates whether internal debug messages should be printed. */
38 static unsigned int solib_dsbt_debug = 0;
39
40 /* TIC6X pointers are four bytes wide. */
41 enum { TIC6X_PTR_SIZE = 4 };
42
43 /* Representation of loadmap and related structs for the TIC6X DSBT. */
44
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];
51
52 struct ext_elf32_dsbt_loadseg
53 {
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;
60 };
61
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 */];
74 };
75
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
80 {
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;
87 };
88
89 struct int_elf32_dsbt_loadmap
90 {
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 */];
101 };
102
103 /* External link_map and elf32_dsbt_loadaddr struct definitions. */
104
105 typedef gdb_byte ext_ptr[4];
106
107 struct ext_elf32_dsbt_loadaddr
108 {
109 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
110 };
111
112 struct ext_link_map
113 {
114 struct ext_elf32_dsbt_loadaddr l_addr;
115
116 /* Absolute file name object was found in. */
117 ext_ptr l_name; /* char *l_name; */
118
119 /* Dynamic section of the shared object. */
120 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
121
122 /* Chain of loaded objects. */
123 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
124 };
125
126 /* Link map info to include in an allocated so_list entry */
127
128 struct lm_info
129 {
130 /* The loadmap, digested into an easier to use form. */
131 struct int_elf32_dsbt_loadmap *map;
132 };
133
134 /* Per pspace dsbt specific data. */
135
136 struct dsbt_info
137 {
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;
143
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;
150
151 /* Cached value for lm_base, below. */
152 CORE_ADDR lm_base_cache;
153
154 /* Link map address for main module. */
155 CORE_ADDR main_lm_addr;
156
157 int enable_break2_done;
158
159 CORE_ADDR interp_text_sect_low;
160 CORE_ADDR interp_text_sect_high;
161 CORE_ADDR interp_plt_sect_low;
162 CORE_ADDR interp_plt_sect_high;
163 };
164
165 /* Per-program-space data key. */
166 static const struct program_space_data *solib_dsbt_pspace_data;
167
168 static void
169 dsbt_pspace_data_cleanup (struct program_space *pspace, void *arg)
170 {
171 struct dsbt_info *info;
172
173 info = program_space_data (pspace, solib_dsbt_pspace_data);
174 xfree (info);
175 }
176
177 /* Get the current dsbt data. If none is found yet, add it now. This
178 function always returns a valid object. */
179
180 static struct dsbt_info *
181 get_dsbt_info (void)
182 {
183 struct dsbt_info *info;
184
185 info = program_space_data (current_program_space, solib_dsbt_pspace_data);
186 if (info != NULL)
187 return info;
188
189 info = XZALLOC (struct dsbt_info);
190 set_program_space_data (current_program_space, solib_dsbt_pspace_data, info);
191
192 info->enable_break2_done = 0;
193 info->lm_base_cache = 0;
194 info->main_lm_addr = 0;
195
196 return info;
197 }
198
199
200 static void
201 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
202 {
203 int i;
204
205 if (map == NULL)
206 printf_filtered ("(null)\n");
207 else if (map->version != 0)
208 printf_filtered (_("Unsupported map version: %d\n"), map->version);
209 else
210 {
211 printf_filtered ("version %d\n", map->version);
212
213 for (i = 0; i < map->nsegs; i++)
214 printf_filtered ("%s:%s -> %s:%s\n",
215 print_core_address (target_gdbarch (),
216 map->segs[i].p_vaddr),
217 print_core_address (target_gdbarch (),
218 map->segs[i].p_vaddr
219 + map->segs[i].p_memsz),
220 print_core_address (target_gdbarch (), map->segs[i].addr),
221 print_core_address (target_gdbarch (), map->segs[i].addr
222 + map->segs[i].p_memsz));
223 }
224 }
225
226 /* Decode int_elf32_dsbt_loadmap from BUF. */
227
228 static struct int_elf32_dsbt_loadmap *
229 decode_loadmap (gdb_byte *buf)
230 {
231 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
232 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
233 struct int_elf32_dsbt_loadmap *int_ldmbuf;
234
235 int version, seg, nsegs;
236 int int_ldmbuf_size;
237
238 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
239
240 /* Extract the version. */
241 version = extract_unsigned_integer (ext_ldmbuf->version,
242 sizeof ext_ldmbuf->version,
243 byte_order);
244 if (version != 0)
245 {
246 /* We only handle version 0. */
247 return NULL;
248 }
249
250 /* Extract the number of segments. */
251 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
252 sizeof ext_ldmbuf->nsegs,
253 byte_order);
254
255 if (nsegs <= 0)
256 return NULL;
257
258 /* Allocate space into which to put information extract from the
259 external loadsegs. I.e, allocate the internal loadsegs. */
260 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
261 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
262 int_ldmbuf = xmalloc (int_ldmbuf_size);
263
264 /* Place extracted information in internal structs. */
265 int_ldmbuf->version = version;
266 int_ldmbuf->nsegs = nsegs;
267 for (seg = 0; seg < nsegs; seg++)
268 {
269 int_ldmbuf->segs[seg].addr
270 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
271 sizeof (ext_ldmbuf->segs[seg].addr),
272 byte_order);
273 int_ldmbuf->segs[seg].p_vaddr
274 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
275 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
276 byte_order);
277 int_ldmbuf->segs[seg].p_memsz
278 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
279 sizeof (ext_ldmbuf->segs[seg].p_memsz),
280 byte_order);
281 }
282
283 xfree (ext_ldmbuf);
284 return int_ldmbuf;
285 }
286
287
288 static struct dsbt_info *get_dsbt_info (void);
289
290 /* Interrogate the Linux kernel to find out where the program was loaded.
291 There are two load maps; one for the executable and one for the
292 interpreter (only in the case of a dynamically linked executable). */
293
294 static void
295 dsbt_get_initial_loadmaps (void)
296 {
297 gdb_byte *buf;
298 struct dsbt_info *info = get_dsbt_info ();
299
300 if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
301 "exec", &buf))
302 {
303 info->exec_loadmap = NULL;
304 error (_("Error reading DSBT exec loadmap"));
305 }
306 info->exec_loadmap = decode_loadmap (buf);
307 if (solib_dsbt_debug)
308 dsbt_print_loadmap (info->exec_loadmap);
309
310 if (0 >= target_read_alloc (&current_target, TARGET_OBJECT_FDPIC,
311 "interp", &buf))
312 {
313 info->interp_loadmap = NULL;
314 error (_("Error reading DSBT interp loadmap"));
315 }
316 info->interp_loadmap = decode_loadmap (buf);
317 if (solib_dsbt_debug)
318 dsbt_print_loadmap (info->interp_loadmap);
319 }
320
321 /* Given address LDMADDR, fetch and decode the loadmap at that address.
322 Return NULL if there is a problem reading the target memory or if
323 there doesn't appear to be a loadmap at the given address. The
324 allocated space (representing the loadmap) returned by this
325 function may be freed via a single call to xfree. */
326
327 static struct int_elf32_dsbt_loadmap *
328 fetch_loadmap (CORE_ADDR ldmaddr)
329 {
330 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
331 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
332 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
333 struct int_elf32_dsbt_loadmap *int_ldmbuf;
334 int ext_ldmbuf_size, int_ldmbuf_size;
335 int version, seg, nsegs;
336
337 /* Fetch initial portion of the loadmap. */
338 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
339 sizeof ext_ldmbuf_partial))
340 {
341 /* Problem reading the target's memory. */
342 return NULL;
343 }
344
345 /* Extract the version. */
346 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
347 sizeof ext_ldmbuf_partial.version,
348 byte_order);
349 if (version != 0)
350 {
351 /* We only handle version 0. */
352 return NULL;
353 }
354
355 /* Extract the number of segments. */
356 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
357 sizeof ext_ldmbuf_partial.nsegs,
358 byte_order);
359
360 if (nsegs <= 0)
361 return NULL;
362
363 /* Allocate space for the complete (external) loadmap. */
364 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
365 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
366 ext_ldmbuf = xmalloc (ext_ldmbuf_size);
367
368 /* Copy over the portion of the loadmap that's already been read. */
369 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
370
371 /* Read the rest of the loadmap from the target. */
372 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
373 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
374 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
375 {
376 /* Couldn't read rest of the loadmap. */
377 xfree (ext_ldmbuf);
378 return NULL;
379 }
380
381 /* Allocate space into which to put information extract from the
382 external loadsegs. I.e, allocate the internal loadsegs. */
383 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
384 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
385 int_ldmbuf = xmalloc (int_ldmbuf_size);
386
387 /* Place extracted information in internal structs. */
388 int_ldmbuf->version = version;
389 int_ldmbuf->nsegs = nsegs;
390 for (seg = 0; seg < nsegs; seg++)
391 {
392 int_ldmbuf->segs[seg].addr
393 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
394 sizeof (ext_ldmbuf->segs[seg].addr),
395 byte_order);
396 int_ldmbuf->segs[seg].p_vaddr
397 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
398 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
399 byte_order);
400 int_ldmbuf->segs[seg].p_memsz
401 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
402 sizeof (ext_ldmbuf->segs[seg].p_memsz),
403 byte_order);
404 }
405
406 xfree (ext_ldmbuf);
407 return int_ldmbuf;
408 }
409
410 static void dsbt_relocate_main_executable (void);
411 static int enable_break (void);
412
413 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
414 returned and the corresponding PTR is set. */
415
416 static int
417 scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
418 {
419 int arch_size, step, sect_size;
420 long dyn_tag;
421 CORE_ADDR dyn_ptr, dyn_addr;
422 gdb_byte *bufend, *bufstart, *buf;
423 Elf32_External_Dyn *x_dynp_32;
424 Elf64_External_Dyn *x_dynp_64;
425 struct bfd_section *sect;
426 struct target_section *target_section;
427
428 if (abfd == NULL)
429 return 0;
430
431 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
432 return 0;
433
434 arch_size = bfd_get_arch_size (abfd);
435 if (arch_size == -1)
436 return 0;
437
438 /* Find the start address of the .dynamic section. */
439 sect = bfd_get_section_by_name (abfd, ".dynamic");
440 if (sect == NULL)
441 return 0;
442
443 for (target_section = current_target_sections->sections;
444 target_section < current_target_sections->sections_end;
445 target_section++)
446 if (sect == target_section->the_bfd_section)
447 break;
448 if (target_section < current_target_sections->sections_end)
449 dyn_addr = target_section->addr;
450 else
451 {
452 /* ABFD may come from OBJFILE acting only as a symbol file without being
453 loaded into the target (see add_symbol_file_command). This case is
454 such fallback to the file VMA address without the possibility of
455 having the section relocated to its actual in-memory address. */
456
457 dyn_addr = bfd_section_vma (abfd, sect);
458 }
459
460 /* Read in .dynamic from the BFD. We will get the actual value
461 from memory later. */
462 sect_size = bfd_section_size (abfd, sect);
463 buf = bufstart = alloca (sect_size);
464 if (!bfd_get_section_contents (abfd, sect,
465 buf, 0, sect_size))
466 return 0;
467
468 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
469 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
470 : sizeof (Elf64_External_Dyn);
471 for (bufend = buf + sect_size;
472 buf < bufend;
473 buf += step)
474 {
475 if (arch_size == 32)
476 {
477 x_dynp_32 = (Elf32_External_Dyn *) buf;
478 dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_tag);
479 dyn_ptr = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp_32->d_un.d_ptr);
480 }
481 else
482 {
483 x_dynp_64 = (Elf64_External_Dyn *) buf;
484 dyn_tag = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_tag);
485 dyn_ptr = bfd_h_get_64 (abfd, (bfd_byte *) x_dynp_64->d_un.d_ptr);
486 }
487 if (dyn_tag == DT_NULL)
488 return 0;
489 if (dyn_tag == dyntag)
490 {
491 /* If requested, try to read the runtime value of this .dynamic
492 entry. */
493 if (ptr)
494 {
495 struct type *ptr_type;
496 gdb_byte ptr_buf[8];
497 CORE_ADDR ptr_addr;
498
499 ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
500 ptr_addr = dyn_addr + (buf - bufstart) + arch_size / 8;
501 if (target_read_memory (ptr_addr, ptr_buf, arch_size / 8) == 0)
502 dyn_ptr = extract_typed_address (ptr_buf, ptr_type);
503 *ptr = dyn_ptr;
504 }
505 return 1;
506 }
507 }
508
509 return 0;
510 }
511
512 /* If no open symbol file, attempt to locate and open the main symbol
513 file.
514
515 If FROM_TTYP dereferences to a non-zero integer, allow messages to
516 be printed. This parameter is a pointer rather than an int because
517 open_symbol_file_object is called via catch_errors and
518 catch_errors requires a pointer argument. */
519
520 static int
521 open_symbol_file_object (void *from_ttyp)
522 {
523 /* Unimplemented. */
524 return 0;
525 }
526
527 /* Given a loadmap and an address, return the displacement needed
528 to relocate the address. */
529
530 static CORE_ADDR
531 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
532 CORE_ADDR addr)
533 {
534 int seg;
535
536 for (seg = 0; seg < map->nsegs; seg++)
537 if (map->segs[seg].p_vaddr <= addr
538 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
539 return map->segs[seg].addr - map->segs[seg].p_vaddr;
540
541 return 0;
542 }
543
544 /* Return the address from which the link map chain may be found. On
545 DSBT, a pointer to the start of the link map will be located at the
546 word found at base of GOT + GOT_MODULE_OFFSET.
547
548 The base of GOT may be found in a number of ways. Assuming that the
549 main executable has already been relocated,
550 1 The easiest way to find this value is to look up the address of
551 _GLOBAL_OFFSET_TABLE_.
552 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
553 address of Global Offset Table. .*/
554
555 static CORE_ADDR
556 lm_base (void)
557 {
558 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
559 struct minimal_symbol *got_sym;
560 CORE_ADDR addr;
561 gdb_byte buf[TIC6X_PTR_SIZE];
562 struct dsbt_info *info = get_dsbt_info ();
563
564 /* One of our assumptions is that the main executable has been relocated.
565 Bail out if this has not happened. (Note that post_create_inferior
566 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
567 If we allow this to happen, lm_base_cache will be initialized with
568 a bogus value. */
569 if (info->main_executable_lm_info == 0)
570 return 0;
571
572 /* If we already have a cached value, return it. */
573 if (info->lm_base_cache)
574 return info->lm_base_cache;
575
576 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
577 symfile_objfile);
578
579 if (got_sym != 0)
580 {
581 addr = SYMBOL_VALUE_ADDRESS (got_sym);
582 if (solib_dsbt_debug)
583 fprintf_unfiltered (gdb_stdlog,
584 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
585 (unsigned int) addr);
586 }
587 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
588 {
589 struct int_elf32_dsbt_loadmap *ldm;
590
591 dsbt_get_initial_loadmaps ();
592 ldm = info->exec_loadmap;
593 addr += displacement_from_map (ldm, addr);
594 if (solib_dsbt_debug)
595 fprintf_unfiltered (gdb_stdlog,
596 "lm_base: get addr %x by DT_PLTGOT.\n",
597 (unsigned int) addr);
598 }
599 else
600 {
601 if (solib_dsbt_debug)
602 fprintf_unfiltered (gdb_stdlog,
603 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
604 return 0;
605 }
606 addr += GOT_MODULE_OFFSET;
607
608 if (solib_dsbt_debug)
609 fprintf_unfiltered (gdb_stdlog,
610 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
611 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
612
613 if (target_read_memory (addr, buf, sizeof buf) != 0)
614 return 0;
615 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
616
617 if (solib_dsbt_debug)
618 fprintf_unfiltered (gdb_stdlog,
619 "lm_base: lm_base_cache = %s\n",
620 hex_string_custom (info->lm_base_cache, 8));
621
622 return info->lm_base_cache;
623 }
624
625
626 /* Build a list of `struct so_list' objects describing the shared
627 objects currently loaded in the inferior. This list does not
628 include an entry for the main executable file.
629
630 Note that we only gather information directly available from the
631 inferior --- we don't examine any of the shared library files
632 themselves. The declaration of `struct so_list' says which fields
633 we provide values for. */
634
635 static struct so_list *
636 dsbt_current_sos (void)
637 {
638 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
639 CORE_ADDR lm_addr;
640 struct so_list *sos_head = NULL;
641 struct so_list **sos_next_ptr = &sos_head;
642 struct dsbt_info *info = get_dsbt_info ();
643
644 /* Make sure that the main executable has been relocated. This is
645 required in order to find the address of the global offset table,
646 which in turn is used to find the link map info. (See lm_base
647 for details.)
648
649 Note that the relocation of the main executable is also performed
650 by solib_create_inferior_hook, however, in the case of core
651 files, this hook is called too late in order to be of benefit to
652 solib_add. solib_add eventually calls this function,
653 dsbt_current_sos, and also precedes the call to
654 solib_create_inferior_hook. (See post_create_inferior in
655 infcmd.c.) */
656 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
657 dsbt_relocate_main_executable ();
658
659 /* Locate the address of the first link map struct. */
660 lm_addr = lm_base ();
661
662 /* We have at least one link map entry. Fetch the the lot of them,
663 building the solist chain. */
664 while (lm_addr)
665 {
666 struct ext_link_map lm_buf;
667 ext_Elf32_Word indexword;
668 CORE_ADDR map_addr;
669 int dsbt_index;
670 int ret;
671
672 if (solib_dsbt_debug)
673 fprintf_unfiltered (gdb_stdlog,
674 "current_sos: reading link_map entry at %s\n",
675 hex_string_custom (lm_addr, 8));
676
677 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
678 if (ret)
679 {
680 warning (_("dsbt_current_sos: Unable to read link map entry."
681 " Shared object chain may be incomplete."));
682 break;
683 }
684
685 /* Fetch the load map address. */
686 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
687 sizeof lm_buf.l_addr.map,
688 byte_order);
689
690 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
691 sizeof indexword);
692 if (ret)
693 {
694 warning (_("dsbt_current_sos: Unable to read dsbt index."
695 " Shared object chain may be incomplete."));
696 break;
697 }
698 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
699 byte_order);
700
701 /* If the DSBT index is zero, then we're looking at the entry
702 for the main executable. By convention, we don't include
703 this in the list of shared objects. */
704 if (dsbt_index != 0)
705 {
706 int errcode;
707 char *name_buf;
708 struct int_elf32_dsbt_loadmap *loadmap;
709 struct so_list *sop;
710 CORE_ADDR addr;
711
712 loadmap = fetch_loadmap (map_addr);
713 if (loadmap == NULL)
714 {
715 warning (_("dsbt_current_sos: Unable to fetch load map."
716 " Shared object chain may be incomplete."));
717 break;
718 }
719
720 sop = xcalloc (1, sizeof (struct so_list));
721 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
722 sop->lm_info->map = loadmap;
723 /* Fetch the name. */
724 addr = extract_unsigned_integer (lm_buf.l_name,
725 sizeof (lm_buf.l_name),
726 byte_order);
727 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
728 &errcode);
729
730 if (errcode != 0)
731 warning (_("Can't read pathname for link map entry: %s."),
732 safe_strerror (errcode));
733 else
734 {
735 if (solib_dsbt_debug)
736 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
737 name_buf);
738
739 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
740 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
741 xfree (name_buf);
742 strcpy (sop->so_original_name, sop->so_name);
743 }
744
745 *sos_next_ptr = sop;
746 sos_next_ptr = &sop->next;
747 }
748 else
749 {
750 info->main_lm_addr = lm_addr;
751 }
752
753 lm_addr = extract_unsigned_integer (lm_buf.l_next,
754 sizeof (lm_buf.l_next), byte_order);
755 }
756
757 return sos_head;
758 }
759
760 /* Return 1 if PC lies in the dynamic symbol resolution code of the
761 run time loader. */
762
763 static int
764 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
765 {
766 struct dsbt_info *info = get_dsbt_info ();
767
768 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
769 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
770 || in_plt_section (pc, NULL));
771 }
772
773 /* Print a warning about being unable to set the dynamic linker
774 breakpoint. */
775
776 static void
777 enable_break_failure_warning (void)
778 {
779 warning (_("Unable to find dynamic linker breakpoint function.\n"
780 "GDB will be unable to debug shared library initializers\n"
781 "and track explicitly loaded dynamic code."));
782 }
783
784 /* Helper function for gdb_bfd_lookup_symbol. */
785
786 static int
787 cmp_name (asymbol *sym, void *data)
788 {
789 return (strcmp (sym->name, (const char *) data) == 0);
790 }
791
792 /* The dynamic linkers has, as part of its debugger interface, support
793 for arranging for the inferior to hit a breakpoint after mapping in
794 the shared libraries. This function enables that breakpoint.
795
796 On the TIC6X, using the shared library (DSBT), GDB can try to place
797 a breakpoint on '_dl_debug_state' to monitor the shared library
798 event. */
799
800 static int
801 enable_break (void)
802 {
803 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
804 asection *interp_sect;
805 struct dsbt_info *info;
806
807 if (exec_bfd == NULL)
808 return 0;
809
810 if (!target_has_execution)
811 return 0;
812
813 info = get_dsbt_info ();
814
815 if (info->enable_break2_done)
816 return 1;
817
818 info->interp_text_sect_low = 0;
819 info->interp_text_sect_high = 0;
820 info->interp_plt_sect_low = 0;
821 info->interp_plt_sect_high = 0;
822
823 /* Find the .interp section; if not found, warn the user and drop
824 into the old breakpoint at symbol code. */
825 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
826 if (interp_sect)
827 {
828 unsigned int interp_sect_size;
829 char *buf;
830 bfd *tmp_bfd = NULL;
831 CORE_ADDR addr;
832 gdb_byte addr_buf[TIC6X_PTR_SIZE];
833 struct int_elf32_dsbt_loadmap *ldm;
834 volatile struct gdb_exception ex;
835 int ret;
836
837 /* Read the contents of the .interp section into a local buffer;
838 the contents specify the dynamic linker this program uses. */
839 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
840 buf = alloca (interp_sect_size);
841 bfd_get_section_contents (exec_bfd, interp_sect,
842 buf, 0, interp_sect_size);
843
844 /* Now we need to figure out where the dynamic linker was
845 loaded so that we can load its symbols and place a breakpoint
846 in the dynamic linker itself. */
847
848 TRY_CATCH (ex, RETURN_MASK_ALL)
849 {
850 tmp_bfd = solib_bfd_open (buf);
851 }
852 if (tmp_bfd == NULL)
853 {
854 enable_break_failure_warning ();
855 return 0;
856 }
857
858 dsbt_get_initial_loadmaps ();
859 ldm = info->interp_loadmap;
860
861 /* Record the relocated start and end address of the dynamic linker
862 text and plt section for dsbt_in_dynsym_resolve_code. */
863 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
864 if (interp_sect)
865 {
866 info->interp_text_sect_low
867 = bfd_section_vma (tmp_bfd, interp_sect);
868 info->interp_text_sect_low
869 += displacement_from_map (ldm, info->interp_text_sect_low);
870 info->interp_text_sect_high
871 = info->interp_text_sect_low
872 + bfd_section_size (tmp_bfd, interp_sect);
873 }
874 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
875 if (interp_sect)
876 {
877 info->interp_plt_sect_low =
878 bfd_section_vma (tmp_bfd, interp_sect);
879 info->interp_plt_sect_low
880 += displacement_from_map (ldm, info->interp_plt_sect_low);
881 info->interp_plt_sect_high =
882 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
883 }
884
885 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_state");
886 if (addr != 0)
887 {
888 if (solib_dsbt_debug)
889 fprintf_unfiltered (gdb_stdlog,
890 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
891 hex_string_custom (addr, 8));
892 addr += displacement_from_map (ldm, addr);
893
894 if (solib_dsbt_debug)
895 fprintf_unfiltered (gdb_stdlog,
896 "enable_break: _dl_debug_state (after relocation) = %s\n",
897 hex_string_custom (addr, 8));
898
899 /* Now (finally!) create the solib breakpoint. */
900 create_solib_event_breakpoint (target_gdbarch (), addr);
901
902 info->enable_break2_done = 1;
903 ret = 1;
904 }
905 else
906 {
907 if (solib_dsbt_debug)
908 fprintf_unfiltered (gdb_stdlog,
909 "enable_break: _dl_debug_state is not found\n");
910 ret = 0;
911 }
912
913 /* We're done with the temporary bfd. */
914 gdb_bfd_unref (tmp_bfd);
915
916 /* We're also done with the loadmap. */
917 xfree (ldm);
918
919 return ret;
920 }
921
922 /* Tell the user we couldn't set a dynamic linker breakpoint. */
923 enable_break_failure_warning ();
924
925 /* Failure return. */
926 return 0;
927 }
928
929 /* Once the symbols from a shared object have been loaded in the usual
930 way, we are called to do any system specific symbol handling that
931 is needed. */
932
933 static void
934 dsbt_special_symbol_handling (void)
935 {
936 }
937
938 static void
939 dsbt_relocate_main_executable (void)
940 {
941 struct int_elf32_dsbt_loadmap *ldm;
942 struct cleanup *old_chain;
943 struct section_offsets *new_offsets;
944 int changed;
945 struct obj_section *osect;
946 struct dsbt_info *info = get_dsbt_info ();
947
948 dsbt_get_initial_loadmaps ();
949 ldm = info->exec_loadmap;
950
951 xfree (info->main_executable_lm_info);
952 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
953 info->main_executable_lm_info->map = ldm;
954
955 new_offsets = xcalloc (symfile_objfile->num_sections,
956 sizeof (struct section_offsets));
957 old_chain = make_cleanup (xfree, new_offsets);
958 changed = 0;
959
960 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
961 {
962 CORE_ADDR orig_addr, addr, offset;
963 int osect_idx;
964 int seg;
965
966 osect_idx = osect - symfile_objfile->sections;
967
968 /* Current address of section. */
969 addr = obj_section_addr (osect);
970 /* Offset from where this section started. */
971 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
972 /* Original address prior to any past relocations. */
973 orig_addr = addr - offset;
974
975 for (seg = 0; seg < ldm->nsegs; seg++)
976 {
977 if (ldm->segs[seg].p_vaddr <= orig_addr
978 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
979 {
980 new_offsets->offsets[osect_idx]
981 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
982
983 if (new_offsets->offsets[osect_idx] != offset)
984 changed = 1;
985 break;
986 }
987 }
988 }
989
990 if (changed)
991 objfile_relocate (symfile_objfile, new_offsets);
992
993 do_cleanups (old_chain);
994
995 /* Now that symfile_objfile has been relocated, we can compute the
996 GOT value and stash it away. */
997 }
998
999 /* When gdb starts up the inferior, it nurses it along (through the
1000 shell) until it is ready to execute it's first instruction. At this
1001 point, this function gets called via solib_create_inferior_hook.
1002
1003 For the DSBT shared library, the main executable needs to be relocated.
1004 The shared library breakpoints also need to be enabled. */
1005
1006 static void
1007 dsbt_solib_create_inferior_hook (int from_tty)
1008 {
1009 /* Relocate main executable. */
1010 dsbt_relocate_main_executable ();
1011
1012 /* Enable shared library breakpoints. */
1013 if (!enable_break ())
1014 {
1015 warning (_("shared library handler failed to enable breakpoint"));
1016 return;
1017 }
1018 }
1019
1020 static void
1021 dsbt_clear_solib (void)
1022 {
1023 struct dsbt_info *info = get_dsbt_info ();
1024
1025 info->lm_base_cache = 0;
1026 info->enable_break2_done = 0;
1027 info->main_lm_addr = 0;
1028 if (info->main_executable_lm_info != 0)
1029 {
1030 xfree (info->main_executable_lm_info->map);
1031 xfree (info->main_executable_lm_info);
1032 info->main_executable_lm_info = 0;
1033 }
1034 }
1035
1036 static void
1037 dsbt_free_so (struct so_list *so)
1038 {
1039 xfree (so->lm_info->map);
1040 xfree (so->lm_info);
1041 }
1042
1043 static void
1044 dsbt_relocate_section_addresses (struct so_list *so,
1045 struct target_section *sec)
1046 {
1047 int seg;
1048 struct int_elf32_dsbt_loadmap *map;
1049
1050 map = so->lm_info->map;
1051
1052 for (seg = 0; seg < map->nsegs; seg++)
1053 {
1054 if (map->segs[seg].p_vaddr <= sec->addr
1055 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1056 {
1057 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1058
1059 sec->addr += displ;
1060 sec->endaddr += displ;
1061 break;
1062 }
1063 }
1064 }
1065 static void
1066 show_dsbt_debug (struct ui_file *file, int from_tty,
1067 struct cmd_list_element *c, const char *value)
1068 {
1069 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1070 }
1071
1072 struct target_so_ops dsbt_so_ops;
1073
1074 /* Provide a prototype to silence -Wmissing-prototypes. */
1075 extern initialize_file_ftype _initialize_dsbt_solib;
1076
1077 void
1078 _initialize_dsbt_solib (void)
1079 {
1080 solib_dsbt_pspace_data
1081 = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
1082
1083 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1084 dsbt_so_ops.free_so = dsbt_free_so;
1085 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1086 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1087 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1088 dsbt_so_ops.current_sos = dsbt_current_sos;
1089 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1090 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1091 dsbt_so_ops.bfd_open = solib_bfd_open;
1092
1093 /* Debug this file's internals. */
1094 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1095 &solib_dsbt_debug, _("\
1096 Set internal debugging of shared library code for DSBT ELF."), _("\
1097 Show internal debugging of shared library code for DSBT ELF."), _("\
1098 When non-zero, DSBT solib specific internal debugging is enabled."),
1099 NULL,
1100 show_dsbt_debug,
1101 &setdebuglist, &showdebuglist);
1102 }
This page took 0.050558 seconds and 5 git commands to generate.