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