2012-05-18 Sergio Durigan Junior <sergiodj@redhat.com>
[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
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 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 /* If no open symbol file, attempt to locate and open the main symbol
512 file.
513
514 If FROM_TTYP dereferences to a non-zero integer, allow messages to
515 be printed. This parameter is a pointer rather than an int because
516 open_symbol_file_object is called via catch_errors and
517 catch_errors requires a pointer argument. */
518
519 static int
520 open_symbol_file_object (void *from_ttyp)
521 {
522 /* Unimplemented. */
523 return 0;
524 }
525
526 /* Given a loadmap and an address, return the displacement needed
527 to relocate the address. */
528
529 static CORE_ADDR
530 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
531 CORE_ADDR addr)
532 {
533 int seg;
534
535 for (seg = 0; seg < map->nsegs; seg++)
536 if (map->segs[seg].p_vaddr <= addr
537 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
538 return map->segs[seg].addr - map->segs[seg].p_vaddr;
539
540 return 0;
541 }
542
543 /* Return the address from which the link map chain may be found. On
544 DSBT, a pointer to the start of the link map will be located at the
545 word found at base of GOT + GOT_MODULE_OFFSET.
546
547 The base of GOT may be found in a number of ways. Assuming that the
548 main executable has already been relocated,
549 1 The easiest way to find this value is to look up the address of
550 _GLOBAL_OFFSET_TABLE_.
551 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
552 address of Global Offset Table. .*/
553
554 static CORE_ADDR
555 lm_base (void)
556 {
557 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
558 struct minimal_symbol *got_sym;
559 CORE_ADDR addr;
560 gdb_byte buf[TIC6X_PTR_SIZE];
561 struct dsbt_info *info = get_dsbt_info ();
562
563 /* One of our assumptions is that the main executable has been relocated.
564 Bail out if this has not happened. (Note that post_create_inferior
565 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
566 If we allow this to happen, lm_base_cache will be initialized with
567 a bogus value. */
568 if (info->main_executable_lm_info == 0)
569 return 0;
570
571 /* If we already have a cached value, return it. */
572 if (info->lm_base_cache)
573 return info->lm_base_cache;
574
575 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
576 symfile_objfile);
577
578 if (got_sym != 0)
579 {
580 addr = SYMBOL_VALUE_ADDRESS (got_sym);
581 if (solib_dsbt_debug)
582 fprintf_unfiltered (gdb_stdlog,
583 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
584 (unsigned int) addr);
585 }
586 else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
587 {
588 struct int_elf32_dsbt_loadmap *ldm;
589
590 dsbt_get_initial_loadmaps ();
591 ldm = info->exec_loadmap;
592 addr += displacement_from_map (ldm, addr);
593 if (solib_dsbt_debug)
594 fprintf_unfiltered (gdb_stdlog,
595 "lm_base: get addr %x by DT_PLTGOT.\n",
596 (unsigned int) addr);
597 }
598 else
599 {
600 if (solib_dsbt_debug)
601 fprintf_unfiltered (gdb_stdlog,
602 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
603 return 0;
604 }
605 addr += GOT_MODULE_OFFSET;
606
607 if (solib_dsbt_debug)
608 fprintf_unfiltered (gdb_stdlog,
609 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
610 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
611
612 if (target_read_memory (addr, buf, sizeof buf) != 0)
613 return 0;
614 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
615
616 if (solib_dsbt_debug)
617 fprintf_unfiltered (gdb_stdlog,
618 "lm_base: lm_base_cache = %s\n",
619 hex_string_custom (info->lm_base_cache, 8));
620
621 return info->lm_base_cache;
622 }
623
624
625 /* Build a list of `struct so_list' objects describing the shared
626 objects currently loaded in the inferior. This list does not
627 include an entry for the main executable file.
628
629 Note that we only gather information directly available from the
630 inferior --- we don't examine any of the shared library files
631 themselves. The declaration of `struct so_list' says which fields
632 we provide values for. */
633
634 static struct so_list *
635 dsbt_current_sos (void)
636 {
637 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
638 CORE_ADDR lm_addr;
639 struct so_list *sos_head = NULL;
640 struct so_list **sos_next_ptr = &sos_head;
641 struct dsbt_info *info = get_dsbt_info ();
642
643 /* Make sure that the main executable has been relocated. This is
644 required in order to find the address of the global offset table,
645 which in turn is used to find the link map info. (See lm_base
646 for details.)
647
648 Note that the relocation of the main executable is also performed
649 by SOLIB_CREATE_INFERIOR_HOOK, however, in the case of core
650 files, this hook is called too late in order to be of benefit to
651 SOLIB_ADD. SOLIB_ADD eventually calls this function,
652 dsbt_current_sos, and also precedes the call to
653 SOLIB_CREATE_INFERIOR_HOOK. (See post_create_inferior in
654 infcmd.c.) */
655 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
656 dsbt_relocate_main_executable ();
657
658 /* Locate the address of the first link map struct. */
659 lm_addr = lm_base ();
660
661 /* We have at least one link map entry. Fetch the the lot of them,
662 building the solist chain. */
663 while (lm_addr)
664 {
665 struct ext_link_map lm_buf;
666 ext_Elf32_Word indexword;
667 CORE_ADDR map_addr;
668 int dsbt_index;
669 int ret;
670
671 if (solib_dsbt_debug)
672 fprintf_unfiltered (gdb_stdlog,
673 "current_sos: reading link_map entry at %s\n",
674 hex_string_custom (lm_addr, 8));
675
676 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
677 if (ret)
678 {
679 warning (_("dsbt_current_sos: Unable to read link map entry."
680 " Shared object chain may be incomplete."));
681 break;
682 }
683
684 /* Fetch the load map address. */
685 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
686 sizeof lm_buf.l_addr.map,
687 byte_order);
688
689 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
690 sizeof indexword);
691 if (ret)
692 {
693 warning (_("dsbt_current_sos: Unable to read dsbt index."
694 " Shared object chain may be incomplete."));
695 break;
696 }
697 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
698 byte_order);
699
700 /* If the DSBT index is zero, then we're looking at the entry
701 for the main executable. By convention, we don't include
702 this in the list of shared objects. */
703 if (dsbt_index != 0)
704 {
705 int errcode;
706 char *name_buf;
707 struct int_elf32_dsbt_loadmap *loadmap;
708 struct so_list *sop;
709 CORE_ADDR addr;
710
711 loadmap = fetch_loadmap (map_addr);
712 if (loadmap == NULL)
713 {
714 warning (_("dsbt_current_sos: Unable to fetch load map."
715 " Shared object chain may be incomplete."));
716 break;
717 }
718
719 sop = xcalloc (1, sizeof (struct so_list));
720 sop->lm_info = xcalloc (1, sizeof (struct lm_info));
721 sop->lm_info->map = loadmap;
722 /* Fetch the name. */
723 addr = extract_unsigned_integer (lm_buf.l_name,
724 sizeof (lm_buf.l_name),
725 byte_order);
726 target_read_string (addr, &name_buf, SO_NAME_MAX_PATH_SIZE - 1,
727 &errcode);
728
729 if (errcode != 0)
730 warning (_("Can't read pathname for link map entry: %s."),
731 safe_strerror (errcode));
732 else
733 {
734 if (solib_dsbt_debug)
735 fprintf_unfiltered (gdb_stdlog, "current_sos: name = %s\n",
736 name_buf);
737
738 strncpy (sop->so_name, name_buf, SO_NAME_MAX_PATH_SIZE - 1);
739 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
740 xfree (name_buf);
741 strcpy (sop->so_original_name, sop->so_name);
742 }
743
744 *sos_next_ptr = sop;
745 sos_next_ptr = &sop->next;
746 }
747 else
748 {
749 info->main_lm_addr = lm_addr;
750 }
751
752 lm_addr = extract_unsigned_integer (lm_buf.l_next,
753 sizeof (lm_buf.l_next), byte_order);
754 }
755
756 enable_break2 ();
757
758 return sos_head;
759 }
760
761 /* Return 1 if PC lies in the dynamic symbol resolution code of the
762 run time loader. */
763
764 static int
765 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
766 {
767 struct dsbt_info *info = get_dsbt_info ();
768
769 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
770 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
771 || in_plt_section (pc, NULL));
772 }
773
774 /* Print a warning about being unable to set the dynamic linker
775 breakpoint. */
776
777 static void
778 enable_break_failure_warning (void)
779 {
780 warning (_("Unable to find dynamic linker breakpoint function.\n"
781 "GDB will be unable to debug shared library initializers\n"
782 "and track explicitly loaded dynamic code."));
783 }
784
785 /* Helper function for gdb_bfd_lookup_symbol. */
786
787 static int
788 cmp_name (asymbol *sym, void *data)
789 {
790 return (strcmp (sym->name, (const char *) data) == 0);
791 }
792
793 /* The dynamic linkers has, as part of its debugger interface, support
794 for arranging for the inferior to hit a breakpoint after mapping in
795 the shared libraries. This function enables that breakpoint.
796
797 On the TIC6X, using the shared library (DSBT), the symbol
798 _dl_debug_addr points to the r_debug struct which contains
799 a field called r_brk. r_brk is the address of the function
800 descriptor upon which a breakpoint must be placed. Being a
801 function descriptor, we must extract the entry point in order
802 to set the breakpoint.
803
804 Our strategy will be to get the .interp section from the
805 executable. This section will provide us with the name of the
806 interpreter. We'll open the interpreter and then look up
807 the address of _dl_debug_addr. We then relocate this address
808 using the interpreter's loadmap. Once the relocated address
809 is known, we fetch the value (address) corresponding to r_brk
810 and then use that value to fetch the entry point of the function
811 we're interested in. */
812
813 static int
814 enable_break2 (void)
815 {
816 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
817 int success = 0;
818 char **bkpt_namep;
819 asection *interp_sect;
820 struct dsbt_info *info = get_dsbt_info ();
821
822 if (exec_bfd == NULL)
823 return 0;
824
825 if (!target_has_execution)
826 return 0;
827
828 if (info->enable_break2_done)
829 return 1;
830
831 info->interp_text_sect_low = 0;
832 info->interp_text_sect_high = 0;
833 info->interp_plt_sect_low = 0;
834 info->interp_plt_sect_high = 0;
835
836 /* Find the .interp section; if not found, warn the user and drop
837 into the old breakpoint at symbol code. */
838 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
839 if (interp_sect)
840 {
841 unsigned int interp_sect_size;
842 gdb_byte *buf;
843 bfd *tmp_bfd = NULL;
844 CORE_ADDR addr;
845 gdb_byte addr_buf[TIC6X_PTR_SIZE];
846 struct int_elf32_dsbt_loadmap *ldm;
847 volatile struct gdb_exception ex;
848
849 /* Read the contents of the .interp section into a local buffer;
850 the contents specify the dynamic linker this program uses. */
851 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
852 buf = alloca (interp_sect_size);
853 bfd_get_section_contents (exec_bfd, interp_sect,
854 buf, 0, interp_sect_size);
855
856 /* Now we need to figure out where the dynamic linker was
857 loaded so that we can load its symbols and place a breakpoint
858 in the dynamic linker itself. */
859
860 TRY_CATCH (ex, RETURN_MASK_ALL)
861 {
862 tmp_bfd = solib_bfd_open (buf);
863 }
864 if (tmp_bfd == NULL)
865 {
866 enable_break_failure_warning ();
867 return 0;
868 }
869
870 dsbt_get_initial_loadmaps ();
871 ldm = info->interp_loadmap;
872
873 /* Record the relocated start and end address of the dynamic linker
874 text and plt section for dsbt_in_dynsym_resolve_code. */
875 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
876 if (interp_sect)
877 {
878 info->interp_text_sect_low
879 = bfd_section_vma (tmp_bfd, interp_sect);
880 info->interp_text_sect_low
881 += displacement_from_map (ldm, info->interp_text_sect_low);
882 info->interp_text_sect_high
883 = info->interp_text_sect_low
884 + bfd_section_size (tmp_bfd, interp_sect);
885 }
886 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
887 if (interp_sect)
888 {
889 info->interp_plt_sect_low =
890 bfd_section_vma (tmp_bfd, interp_sect);
891 info->interp_plt_sect_low
892 += displacement_from_map (ldm, info->interp_plt_sect_low);
893 info->interp_plt_sect_high =
894 info->interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
895 }
896
897 addr = gdb_bfd_lookup_symbol (tmp_bfd, cmp_name, "_dl_debug_addr");
898 if (addr == 0)
899 {
900 warning (_("Could not find symbol _dl_debug_addr in dynamic linker"));
901 enable_break_failure_warning ();
902 bfd_close (tmp_bfd);
903 return 0;
904 }
905
906 if (solib_dsbt_debug)
907 fprintf_unfiltered (gdb_stdlog,
908 "enable_break: _dl_debug_addr (prior to relocation) = %s\n",
909 hex_string_custom (addr, 8));
910
911 addr += displacement_from_map (ldm, addr);
912
913 if (solib_dsbt_debug)
914 fprintf_unfiltered (gdb_stdlog,
915 "enable_break: _dl_debug_addr (after relocation) = %s\n",
916 hex_string_custom (addr, 8));
917
918 /* Fetch the address of the r_debug struct. */
919 if (target_read_memory (addr, addr_buf, sizeof addr_buf) != 0)
920 {
921 warning (_("Unable to fetch contents of _dl_debug_addr "
922 "(at address %s) from dynamic linker"),
923 hex_string_custom (addr, 8));
924 }
925 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
926
927 if (solib_dsbt_debug)
928 fprintf_unfiltered (gdb_stdlog,
929 "enable_break: _dl_debug_addr[0..3] = %s\n",
930 hex_string_custom (addr, 8));
931
932 /* If it's zero, then the ldso hasn't initialized yet, and so
933 there are no shared libs yet loaded. */
934 if (addr == 0)
935 {
936 if (solib_dsbt_debug)
937 fprintf_unfiltered (gdb_stdlog,
938 "enable_break: ldso not yet initialized\n");
939 /* Do not warn, but mark to run again. */
940 return 0;
941 }
942
943 /* Fetch the r_brk field. It's 8 bytes from the start of
944 _dl_debug_addr. */
945 if (target_read_memory (addr + 8, addr_buf, sizeof addr_buf) != 0)
946 {
947 warning (_("Unable to fetch _dl_debug_addr->r_brk "
948 "(at address %s) from dynamic linker"),
949 hex_string_custom (addr + 8, 8));
950 enable_break_failure_warning ();
951 bfd_close (tmp_bfd);
952 return 0;
953 }
954 addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order);
955
956 /* We're done with the temporary bfd. */
957 bfd_close (tmp_bfd);
958
959 /* We're also done with the loadmap. */
960 xfree (ldm);
961
962 /* Remove all the solib event breakpoints. Their addresses
963 may have changed since the last time we ran the program. */
964 remove_solib_event_breakpoints ();
965
966 /* Now (finally!) create the solib breakpoint. */
967 create_solib_event_breakpoint (target_gdbarch, addr);
968
969 info->enable_break2_done = 1;
970
971 return 1;
972 }
973
974 /* Tell the user we couldn't set a dynamic linker breakpoint. */
975 enable_break_failure_warning ();
976
977 /* Failure return. */
978 return 0;
979 }
980
981 static int
982 enable_break (void)
983 {
984 asection *interp_sect;
985 struct minimal_symbol *start;
986
987 /* Check for the presence of a .interp section. If there is no
988 such section, the executable is statically linked. */
989
990 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
991
992 if (interp_sect == NULL)
993 {
994 if (solib_dsbt_debug)
995 fprintf_unfiltered (gdb_stdlog,
996 "enable_break: No .interp section found.\n");
997 return 0;
998 }
999
1000 start = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1001 if (start == NULL)
1002 {
1003 if (solib_dsbt_debug)
1004 fprintf_unfiltered (gdb_stdlog,
1005 "enable_break: symbol _start is not found.\n");
1006 return 0;
1007 }
1008
1009 create_solib_event_breakpoint (target_gdbarch,
1010 SYMBOL_VALUE_ADDRESS (start));
1011
1012 if (solib_dsbt_debug)
1013 fprintf_unfiltered (gdb_stdlog,
1014 "enable_break: solib event breakpoint placed at : %s\n",
1015 hex_string_custom (SYMBOL_VALUE_ADDRESS (start), 8));
1016 return 1;
1017 }
1018
1019 /* Once the symbols from a shared object have been loaded in the usual
1020 way, we are called to do any system specific symbol handling that
1021 is needed. */
1022
1023 static void
1024 dsbt_special_symbol_handling (void)
1025 {
1026 }
1027
1028 static void
1029 dsbt_relocate_main_executable (void)
1030 {
1031 struct int_elf32_dsbt_loadmap *ldm;
1032 struct cleanup *old_chain;
1033 struct section_offsets *new_offsets;
1034 int changed;
1035 struct obj_section *osect;
1036 struct dsbt_info *info = get_dsbt_info ();
1037
1038 dsbt_get_initial_loadmaps ();
1039 ldm = info->exec_loadmap;
1040
1041 xfree (info->main_executable_lm_info);
1042 info->main_executable_lm_info = xcalloc (1, sizeof (struct lm_info));
1043 info->main_executable_lm_info->map = ldm;
1044
1045 new_offsets = xcalloc (symfile_objfile->num_sections,
1046 sizeof (struct section_offsets));
1047 old_chain = make_cleanup (xfree, new_offsets);
1048 changed = 0;
1049
1050 ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
1051 {
1052 CORE_ADDR orig_addr, addr, offset;
1053 int osect_idx;
1054 int seg;
1055
1056 osect_idx = osect->the_bfd_section->index;
1057
1058 /* Current address of section. */
1059 addr = obj_section_addr (osect);
1060 /* Offset from where this section started. */
1061 offset = ANOFFSET (symfile_objfile->section_offsets, osect_idx);
1062 /* Original address prior to any past relocations. */
1063 orig_addr = addr - offset;
1064
1065 for (seg = 0; seg < ldm->nsegs; seg++)
1066 {
1067 if (ldm->segs[seg].p_vaddr <= orig_addr
1068 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
1069 {
1070 new_offsets->offsets[osect_idx]
1071 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
1072
1073 if (new_offsets->offsets[osect_idx] != offset)
1074 changed = 1;
1075 break;
1076 }
1077 }
1078 }
1079
1080 if (changed)
1081 objfile_relocate (symfile_objfile, new_offsets);
1082
1083 do_cleanups (old_chain);
1084
1085 /* Now that symfile_objfile has been relocated, we can compute the
1086 GOT value and stash it away. */
1087 }
1088
1089 /* When gdb starts up the inferior, it nurses it along (through the
1090 shell) until it is ready to execute it's first instruction. At this
1091 point, this function gets called via expansion of the macro
1092 SOLIB_CREATE_INFERIOR_HOOK.
1093
1094 For the DSBT shared library, the main executable needs to be relocated.
1095 The shared library breakpoints also need to be enabled.
1096 */
1097
1098 static void
1099 dsbt_solib_create_inferior_hook (int from_tty)
1100 {
1101 /* Relocate main executable. */
1102 dsbt_relocate_main_executable ();
1103
1104 /* Enable shared library breakpoints. */
1105 if (!enable_break ())
1106 {
1107 warning (_("shared library handler failed to enable breakpoint"));
1108 return;
1109 }
1110 }
1111
1112 static void
1113 dsbt_clear_solib (void)
1114 {
1115 struct dsbt_info *info = get_dsbt_info ();
1116
1117 info->lm_base_cache = 0;
1118 info->enable_break2_done = 0;
1119 info->main_lm_addr = 0;
1120 if (info->main_executable_lm_info != 0)
1121 {
1122 xfree (info->main_executable_lm_info->map);
1123 xfree (info->main_executable_lm_info);
1124 info->main_executable_lm_info = 0;
1125 }
1126 }
1127
1128 static void
1129 dsbt_free_so (struct so_list *so)
1130 {
1131 xfree (so->lm_info->map);
1132 xfree (so->lm_info);
1133 }
1134
1135 static void
1136 dsbt_relocate_section_addresses (struct so_list *so,
1137 struct target_section *sec)
1138 {
1139 int seg;
1140 struct int_elf32_dsbt_loadmap *map;
1141
1142 map = so->lm_info->map;
1143
1144 for (seg = 0; seg < map->nsegs; seg++)
1145 {
1146 if (map->segs[seg].p_vaddr <= sec->addr
1147 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
1148 {
1149 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
1150
1151 sec->addr += displ;
1152 sec->endaddr += displ;
1153 break;
1154 }
1155 }
1156 }
1157 static void
1158 show_dsbt_debug (struct ui_file *file, int from_tty,
1159 struct cmd_list_element *c, const char *value)
1160 {
1161 fprintf_filtered (file, _("solib-dsbt debugging is %s.\n"), value);
1162 }
1163
1164 struct target_so_ops dsbt_so_ops;
1165
1166 /* Provide a prototype to silence -Wmissing-prototypes. */
1167 extern initialize_file_ftype _initialize_dsbt_solib;
1168
1169 void
1170 _initialize_dsbt_solib (void)
1171 {
1172 solib_dsbt_pspace_data
1173 = register_program_space_data_with_cleanup (dsbt_pspace_data_cleanup);
1174
1175 dsbt_so_ops.relocate_section_addresses = dsbt_relocate_section_addresses;
1176 dsbt_so_ops.free_so = dsbt_free_so;
1177 dsbt_so_ops.clear_solib = dsbt_clear_solib;
1178 dsbt_so_ops.solib_create_inferior_hook = dsbt_solib_create_inferior_hook;
1179 dsbt_so_ops.special_symbol_handling = dsbt_special_symbol_handling;
1180 dsbt_so_ops.current_sos = dsbt_current_sos;
1181 dsbt_so_ops.open_symbol_file_object = open_symbol_file_object;
1182 dsbt_so_ops.in_dynsym_resolve_code = dsbt_in_dynsym_resolve_code;
1183 dsbt_so_ops.bfd_open = solib_bfd_open;
1184
1185 /* Debug this file's internals. */
1186 add_setshow_zinteger_cmd ("solib-dsbt", class_maintenance,
1187 &solib_dsbt_debug, _("\
1188 Set internal debugging of shared library code for DSBT ELF."), _("\
1189 Show internal debugging of shared library code for DSBT ELF."), _("\
1190 When non-zero, DSBT solib specific internal debugging is enabled."),
1191 NULL,
1192 show_dsbt_debug,
1193 &setdebuglist, &showdebuglist);
1194 }