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