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