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