gdb/gdbserver:
[deliverable/binutils-gdb.git] / gdb / solib-dsbt.c
1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2012 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", (gdb_byte**) &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", (gdb_byte**)&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_break2 (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 enable_break2 ();
758
759 return sos_head;
760 }
761
762 /* Return 1 if PC lies in the dynamic symbol resolution code of the
763 run time loader. */
764
765 static int
766 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
767 {
768 struct dsbt_info *info = get_dsbt_info ();
769
770 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
771 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
772 || in_plt_section (pc, NULL));
773 }
774
775 /* Print a warning about being unable to set the dynamic linker
776 breakpoint. */
777
778 static void
779 enable_break_failure_warning (void)
780 {
781 warning (_("Unable to find dynamic linker breakpoint function.\n"
782 "GDB will be unable to debug shared library initializers\n"
783 "and track explicitly loaded dynamic code."));
784 }
785
786 /* Helper function for gdb_bfd_lookup_symbol. */
787
788 static int
789 cmp_name (asymbol *sym, void *data)
790 {
791 return (strcmp (sym->name, (const char *) data) == 0);
792 }
793
794 /* The dynamic linkers has, as part of its debugger interface, support
795 for arranging for the inferior to hit a breakpoint after mapping in
796 the shared libraries. This function enables that breakpoint.
797
798 On the TIC6X, using the shared library (DSBT), the symbol
799 _dl_debug_addr points to the r_debug struct which contains
800 a field called r_brk. r_brk is the address of the function
801 descriptor upon which a breakpoint must be placed. Being a
802 function descriptor, we must extract the entry point in order
803 to set the breakpoint.
804
805 Our strategy will be to get the .interp section from the
806 executable. This section will provide us with the name of the
807 interpreter. We'll open the interpreter and then look up
808 the address of _dl_debug_addr. We then relocate this address
809 using the interpreter's loadmap. Once the relocated address
810 is known, we fetch the value (address) corresponding to r_brk
811 and then use that value to fetch the entry point of the function
812 we're interested in. */
813
814 static int
815 enable_break2 (void)
816 {
817 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
818 int success = 0;
819 char **bkpt_namep;
820 asection *interp_sect;
821 struct dsbt_info *info = get_dsbt_info ();
822
823 if (exec_bfd == NULL)
824 return 0;
825
826 if (!target_has_execution)
827 return 0;
828
829 if (info->enable_break2_done)
830 return 1;
831
832 info->interp_text_sect_low = 0;
833 info->interp_text_sect_high = 0;
834 info->interp_plt_sect_low = 0;
835 info->interp_plt_sect_high = 0;
836
837 /* Find the .interp section; if not found, warn the user and drop
838 into the old breakpoint at symbol code. */
839 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
840 if (interp_sect)
841 {
842 unsigned int interp_sect_size;
843 gdb_byte *buf;
844 bfd *tmp_bfd = NULL;
845 CORE_ADDR 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
898 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_addr");
899 if (addr == 0)
900 {
901 warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
902 enable_break_failure_warning ();
903 gdb_bfd_unref (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 gdb_bfd_unref (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 gdb_bfd_unref (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
982 static int
983 enable_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
1024 static void
1025 dsbt_special_symbol_handling (void)
1026 {
1027 }
1028
1029 static void
1030 dsbt_relocate_main_executable (void)
1031 {
1032 struct int_elf32_dsbt_loadmap *ldm;
1033 struct cleanup *old_chain;
1034 struct section_offsets *new_offsets;
1035 int changed;
1036 struct obj_section *osect;
1037 struct dsbt_info *info = get_dsbt_info ();
1038
1039 dsbt_get_initial_loadmaps ();
1040 ldm = info->exec_loadmap;
1041
1042 xfree (info->main_executable_lm_info);
1043 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
1044 info->main_executable_lm_info->map = ldm;
1045
1046 new_offsets = xcalloc (symfile_objfile->num_sections,
1047 sizeof (struct section_offsets));
1048 old_chain = make_cleanup (xfree, new_offsets);
1049 changed = 0;
1050
1051 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
1052 {
1053 CORE_ADDR orig_addr, addr, offset;
1054 int osect_idx;
1055 int seg;
1056
1057 osect_idx = osect->the_bfd_section->index;
1058
1059 /* Current address of section. */
1060 addr = obj_section_addr (osect);
1061 /* Offset from where this section started. */
1062 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
1063 /* Original address prior to any past relocations. */
1064 orig_addr = addr - offset;
1065
1066 for (seg = 0; seg < ldm->nsegs; seg++)
1067 {
1068 if (ldm->segs[seg].p_vaddr <= orig_addr
1069 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
1070 {
1071 new_offsets->offsets[osect_idx]
1072 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
1073
1074 if (new_offsets->offsets[osect_idx] != offset)
1075 changed = 1;
1076 break;
1077 }
1078 }
1079 }
1080
1081 if (changed)
1082 objfile_relocate (symfile_objfile, new_offsets);
1083
1084 do_cleanups (old_chain);
1085
1086 /* Now that symfile_objfile has been relocated, we can compute the
1087 GOT value and stash it away. */
1088 }
1089
1090 /* When gdb starts up the inferior, it nurses it along (through the
1091 shell) until it is ready to execute it's first instruction. At this
1092 point, this function gets called via expansion of the macro
1093 SOLIB_CREATE_INFERIOR_HOOK.
1094
1095 For the DSBT shared library, the main executable needs to be relocated.
1096 The shared library breakpoints also need to be enabled.
1097 */
1098
1099 static void
1100 dsbt_solib_create_inferior_hook (int from_tty)
1101 {
1102 /* Relocate main executable. */
1103 dsbt_relocate_main_executable ();
1104
1105 /* Enable shared library breakpoints. */
1106 if (!enable_break ())
1107 {
1108 warning (_("shared library handler failed to enable breakpoint"));
1109 return;
1110 }
1111 }
1112
1113 static void
1114 dsbt_clear_solib (void)
1115 {
1116 struct dsbt_info *info = get_dsbt_info ();
1117
1118 info->lm_base_cache = 0;
1119 info->enable_break2_done = 0;
1120 info->main_lm_addr = 0;
1121 if (info->main_executable_lm_info != 0)
1122 {
1123 xfree (info->main_executable_lm_info->map);
1124 xfree (info->main_executable_lm_info);
1125 info->main_executable_lm_info = 0;
1126 }
1127 }
1128
1129 static void
1130 dsbt_free_so (struct so_list *so)
1131 {
1132 xfree (so->lm_info->map);
1133 xfree (so->lm_info);
1134 }
1135
1136 static void
1137 dsbt_relocate_section_addresses (struct so_list *so,
1138 struct target_section *sec)
1139 {
1140 int seg;
1141 struct int_elf32_dsbt_loadmap *map;
1142
1143 map = so->lm_info->map;
1144
1145 for (seg = 0; seg < map->nsegs; seg++)
1146 {
1147 if (map->segs[seg].p_vaddr <= sec->addr
1148 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1149 {
1150 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1151
1152 sec->addr += displ;
1153 sec->endaddr += displ;
1154 break;
1155 }
1156 }
1157 }
1158 static void
1159 show_dsbt_debug (struct ui_file *file, int from_tty,
1160 struct cmd_list_element *c, const char *value)
1161 {
1162 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1163 }
1164
1165 struct target_so_ops dsbt_so_ops;
1166
1167 /* Provide a prototype to silence -Wmissing-prototypes. */
1168 extern initialize_file_ftype _initialize_dsbt_solib;
1169
1170 void
1171 _initialize_dsbt_solib (void)
1172 {
1173 solib_dsbt_pspace_data
1174 = register_program_space_data_with_cleanup (NULL, dsbt_pspace_data_cleanup);
1175
1176 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1177 dsbt_so_ops.free_so = dsbt_free_so;
1178 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1179 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1180 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1181 dsbt_so_ops.current_sos = dsbt_current_sos;
1182 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1183 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1184 dsbt_so_ops.bfd_open = solib_bfd_open;
1185
1186 /* Debug this file's internals. */
1187 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
1188 &solib_dsbt_debug, _("\
1189 Set internal debugging of shared library code for DSBT ELF."), _("\
1190 Show internal debugging of shared library code for DSBT ELF."), _("\
1191 When non-zero, DSBT solib specific internal debugging is enabled."),
1192 NULL,
1193 show_dsbt_debug,
1194 &setdebuglist, &showdebuglist);
1195 }
This page took 0.058016 seconds and 4 git commands to generate.