Automatic date update in version.in
[binutils-gdb.git] / gdb / solib-dsbt.c
blob0208ed13effc3a920f7029e8e44d0e3bd44a92d4
1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
20 #include "inferior.h"
21 #include "gdbcore.h"
22 #include "solib.h"
23 #include "solist.h"
24 #include "objfiles.h"
25 #include "symtab.h"
26 #include "command.h"
27 #include "gdb_bfd.h"
28 #include "solib-dsbt.h"
29 #include "elf/common.h"
30 #include "cli/cli-cmds.h"
32 #define GOT_MODULE_OFFSET 4
34 /* Flag which indicates whether internal debug messages should be printed. */
35 static unsigned int solib_dsbt_debug = 0;
37 /* TIC6X pointers are four bytes wide. */
38 enum { TIC6X_PTR_SIZE = 4 };
40 /* Representation of loadmap and related structs for the TIC6X DSBT. */
42 /* External versions; the size and alignment of the fields should be
43 the same as those on the target. When loaded, the placement of
44 the bits in each field will be the same as on the target. */
45 typedef gdb_byte ext_Elf32_Half[2];
46 typedef gdb_byte ext_Elf32_Addr[4];
47 typedef gdb_byte ext_Elf32_Word[4];
49 struct ext_elf32_dsbt_loadseg
51 /* Core address to which the segment is mapped. */
52 ext_Elf32_Addr addr;
53 /* VMA recorded in the program header. */
54 ext_Elf32_Addr p_vaddr;
55 /* Size of this segment in memory. */
56 ext_Elf32_Word p_memsz;
59 struct ext_elf32_dsbt_loadmap {
60 /* Protocol version number, must be zero. */
61 ext_Elf32_Word version;
62 /* A pointer to the DSBT table; the DSBT size and the index of this
63 module. */
64 ext_Elf32_Word dsbt_table_ptr;
65 ext_Elf32_Word dsbt_size;
66 ext_Elf32_Word dsbt_index;
67 /* Number of segments in this map. */
68 ext_Elf32_Word nsegs;
69 /* The actual memory map. */
70 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
73 /* Internal versions; the types are GDB types and the data in each
74 of the fields is (or will be) decoded from the external struct
75 for ease of consumption. */
76 struct int_elf32_dsbt_loadseg
78 /* Core address to which the segment is mapped. */
79 CORE_ADDR addr;
80 /* VMA recorded in the program header. */
81 CORE_ADDR p_vaddr;
82 /* Size of this segment in memory. */
83 long p_memsz;
86 struct int_elf32_dsbt_loadmap
88 /* Protocol version number, must be zero. */
89 int version;
90 CORE_ADDR dsbt_table_ptr;
91 /* A pointer to the DSBT table; the DSBT size and the index of this
92 module. */
93 int dsbt_size, dsbt_index;
94 /* Number of segments in this map. */
95 int nsegs;
96 /* The actual memory map. */
97 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
100 /* External link_map and elf32_dsbt_loadaddr struct definitions. */
102 typedef gdb_byte ext_ptr[4];
104 struct ext_elf32_dsbt_loadaddr
106 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
109 struct dbst_ext_link_map
111 struct ext_elf32_dsbt_loadaddr l_addr;
113 /* Absolute file name object was found in. */
114 ext_ptr l_name; /* char *l_name; */
116 /* Dynamic section of the shared object. */
117 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
119 /* Chain of loaded objects. */
120 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
123 /* Link map info to include in an allocated so_list entry */
125 struct lm_info_dsbt final : public lm_info
127 ~lm_info_dsbt ()
129 xfree (this->map);
132 /* The loadmap, digested into an easier to use form. */
133 int_elf32_dsbt_loadmap *map = NULL;
136 /* Per pspace dsbt specific data. */
138 struct dsbt_info
140 /* The load map, got value, etc. are not available from the chain
141 of loaded shared objects. ``main_executable_lm_info'' provides
142 a way to get at this information so that it doesn't need to be
143 frequently recomputed. Initialized by dsbt_relocate_main_executable. */
144 struct lm_info_dsbt *main_executable_lm_info = nullptr;
146 /* Load maps for the main executable and the interpreter. These are obtained
147 from ptrace. They are the starting point for getting into the program,
148 and are required to find the solib list with the individual load maps for
149 each module. */
150 struct int_elf32_dsbt_loadmap *exec_loadmap = nullptr;
151 struct int_elf32_dsbt_loadmap *interp_loadmap = nullptr;
153 /* Cached value for lm_base, below. */
154 CORE_ADDR lm_base_cache = 0;
156 /* Link map address for main module. */
157 CORE_ADDR main_lm_addr = 0;
159 CORE_ADDR interp_text_sect_low = 0;
160 CORE_ADDR interp_text_sect_high = 0;
161 CORE_ADDR interp_plt_sect_low = 0;
162 CORE_ADDR interp_plt_sect_high = 0;
165 /* Per-program-space data key. */
166 static const registry<program_space>::key<dsbt_info> solib_dsbt_pspace_data;
168 /* Get the dsbt solib data for PSPACE. If none is found yet, add it now. This
169 function always returns a valid object. */
171 static dsbt_info *
172 get_dsbt_info (program_space *pspace)
174 dsbt_info *info = solib_dsbt_pspace_data.get (pspace);
175 if (info != nullptr)
176 return info;
178 return solib_dsbt_pspace_data.emplace (pspace);
182 static void
183 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
185 int i;
187 if (map == NULL)
188 gdb_printf ("(null)\n");
189 else if (map->version != 0)
190 gdb_printf (_("Unsupported map version: %d\n"), map->version);
191 else
193 gdb_printf ("version %d\n", map->version);
195 for (i = 0; i < map->nsegs; i++)
196 gdb_printf ("%s:%s -> %s:%s\n",
197 print_core_address (current_inferior ()->arch (),
198 map->segs[i].p_vaddr),
199 print_core_address (current_inferior ()->arch (),
200 (map->segs[i].p_vaddr
201 + map->segs[i].p_memsz)),
202 print_core_address (current_inferior ()->arch (),
203 map->segs[i].addr),
204 print_core_address (current_inferior ()->arch (),
205 (map->segs[i].addr
206 + map->segs[i].p_memsz)));
210 /* Decode int_elf32_dsbt_loadmap from BUF. */
212 static struct int_elf32_dsbt_loadmap *
213 decode_loadmap (const gdb_byte *buf)
215 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
216 const struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
217 struct int_elf32_dsbt_loadmap *int_ldmbuf;
219 int version, seg, nsegs;
220 int int_ldmbuf_size;
222 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
224 /* Extract the version. */
225 version = extract_unsigned_integer (ext_ldmbuf->version,
226 sizeof ext_ldmbuf->version,
227 byte_order);
228 if (version != 0)
230 /* We only handle version 0. */
231 return NULL;
234 /* Extract the number of segments. */
235 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
236 sizeof ext_ldmbuf->nsegs,
237 byte_order);
239 if (nsegs <= 0)
240 return NULL;
242 /* Allocate space into which to put information extract from the
243 external loadsegs. I.e, allocate the internal loadsegs. */
244 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
245 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
246 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
248 /* Place extracted information in internal structs. */
249 int_ldmbuf->version = version;
250 int_ldmbuf->nsegs = nsegs;
251 for (seg = 0; seg < nsegs; seg++)
253 int_ldmbuf->segs[seg].addr
254 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
255 sizeof (ext_ldmbuf->segs[seg].addr),
256 byte_order);
257 int_ldmbuf->segs[seg].p_vaddr
258 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
259 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
260 byte_order);
261 int_ldmbuf->segs[seg].p_memsz
262 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
263 sizeof (ext_ldmbuf->segs[seg].p_memsz),
264 byte_order);
267 return int_ldmbuf;
270 /* Interrogate the Linux kernel to find out where the program was loaded.
271 There are two load maps; one for the executable and one for the
272 interpreter (only in the case of a dynamically linked executable). */
274 static void
275 dsbt_get_initial_loadmaps (void)
277 dsbt_info *info = get_dsbt_info (current_program_space);
278 std::optional<gdb::byte_vector> buf
279 = target_read_alloc (current_inferior ()->top_target (),
280 TARGET_OBJECT_FDPIC, "exec");
282 if (!buf || buf->empty ())
284 info->exec_loadmap = NULL;
285 error (_("Error reading DSBT exec loadmap"));
287 info->exec_loadmap = decode_loadmap (buf->data ());
288 if (solib_dsbt_debug)
289 dsbt_print_loadmap (info->exec_loadmap);
291 buf = target_read_alloc (current_inferior ()->top_target (),
292 TARGET_OBJECT_FDPIC, "exec");
293 if (!buf || buf->empty ())
295 info->interp_loadmap = NULL;
296 error (_("Error reading DSBT interp loadmap"));
298 info->interp_loadmap = decode_loadmap (buf->data ());
299 if (solib_dsbt_debug)
300 dsbt_print_loadmap (info->interp_loadmap);
303 /* Given address LDMADDR, fetch and decode the loadmap at that address.
304 Return NULL if there is a problem reading the target memory or if
305 there doesn't appear to be a loadmap at the given address. The
306 allocated space (representing the loadmap) returned by this
307 function may be freed via a single call to xfree. */
309 static struct int_elf32_dsbt_loadmap *
310 fetch_loadmap (CORE_ADDR ldmaddr)
312 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
313 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
314 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
315 struct int_elf32_dsbt_loadmap *int_ldmbuf;
316 int ext_ldmbuf_size, int_ldmbuf_size;
317 int version, seg, nsegs;
319 /* Fetch initial portion of the loadmap. */
320 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
321 sizeof ext_ldmbuf_partial))
323 /* Problem reading the target's memory. */
324 return NULL;
327 /* Extract the version. */
328 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
329 sizeof ext_ldmbuf_partial.version,
330 byte_order);
331 if (version != 0)
333 /* We only handle version 0. */
334 return NULL;
337 /* Extract the number of segments. */
338 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
339 sizeof ext_ldmbuf_partial.nsegs,
340 byte_order);
342 if (nsegs <= 0)
343 return NULL;
345 /* Allocate space for the complete (external) loadmap. */
346 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
347 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
348 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size);
350 /* Copy over the portion of the loadmap that's already been read. */
351 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
353 /* Read the rest of the loadmap from the target. */
354 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
355 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
356 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
358 /* Couldn't read rest of the loadmap. */
359 xfree (ext_ldmbuf);
360 return NULL;
363 /* Allocate space into which to put information extract from the
364 external loadsegs. I.e, allocate the internal loadsegs. */
365 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
366 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
367 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
369 /* Place extracted information in internal structs. */
370 int_ldmbuf->version = version;
371 int_ldmbuf->nsegs = nsegs;
372 for (seg = 0; seg < nsegs; seg++)
374 int_ldmbuf->segs[seg].addr
375 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
376 sizeof (ext_ldmbuf->segs[seg].addr),
377 byte_order);
378 int_ldmbuf->segs[seg].p_vaddr
379 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
380 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
381 byte_order);
382 int_ldmbuf->segs[seg].p_memsz
383 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
384 sizeof (ext_ldmbuf->segs[seg].p_memsz),
385 byte_order);
388 xfree (ext_ldmbuf);
389 return int_ldmbuf;
392 static void dsbt_relocate_main_executable (void);
393 static int enable_break (void);
395 /* See solist.h. */
397 static int
398 open_symbol_file_object (int from_tty)
400 /* Unimplemented. */
401 return 0;
404 /* Given a loadmap and an address, return the displacement needed
405 to relocate the address. */
407 static CORE_ADDR
408 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
409 CORE_ADDR addr)
411 int seg;
413 for (seg = 0; seg < map->nsegs; seg++)
414 if (map->segs[seg].p_vaddr <= addr
415 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
416 return map->segs[seg].addr - map->segs[seg].p_vaddr;
418 return 0;
421 /* Return the address from which the link map chain may be found. On
422 DSBT, a pointer to the start of the link map will be located at the
423 word found at base of GOT + GOT_MODULE_OFFSET.
425 The base of GOT may be found in a number of ways. Assuming that the
426 main executable has already been relocated,
427 1 The easiest way to find this value is to look up the address of
428 _GLOBAL_OFFSET_TABLE_.
429 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
430 address of Global Offset Table. .*/
432 static CORE_ADDR
433 lm_base (void)
435 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
436 struct bound_minimal_symbol got_sym;
437 CORE_ADDR addr;
438 gdb_byte buf[TIC6X_PTR_SIZE];
439 dsbt_info *info = get_dsbt_info (current_program_space);
441 /* One of our assumptions is that the main executable has been relocated.
442 Bail out if this has not happened. (Note that post_create_inferior
443 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
444 If we allow this to happen, lm_base_cache will be initialized with
445 a bogus value. */
446 if (info->main_executable_lm_info == 0)
447 return 0;
449 /* If we already have a cached value, return it. */
450 if (info->lm_base_cache)
451 return info->lm_base_cache;
453 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
454 current_program_space->symfile_object_file);
456 if (got_sym.minsym != 0)
458 addr = got_sym.value_address ();
459 if (solib_dsbt_debug)
460 gdb_printf (gdb_stdlog,
461 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
462 (unsigned int) addr);
464 else if (gdb_bfd_scan_elf_dyntag (DT_PLTGOT,
465 current_program_space->exec_bfd (),
466 &addr, NULL))
468 struct int_elf32_dsbt_loadmap *ldm;
470 dsbt_get_initial_loadmaps ();
471 ldm = info->exec_loadmap;
472 addr += displacement_from_map (ldm, addr);
473 if (solib_dsbt_debug)
474 gdb_printf (gdb_stdlog,
475 "lm_base: get addr %x by DT_PLTGOT.\n",
476 (unsigned int) addr);
478 else
480 if (solib_dsbt_debug)
481 gdb_printf (gdb_stdlog,
482 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
483 return 0;
485 addr += GOT_MODULE_OFFSET;
487 if (solib_dsbt_debug)
488 gdb_printf (gdb_stdlog,
489 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
490 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
492 if (target_read_memory (addr, buf, sizeof buf) != 0)
493 return 0;
494 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
496 if (solib_dsbt_debug)
497 gdb_printf (gdb_stdlog,
498 "lm_base: lm_base_cache = %s\n",
499 hex_string_custom (info->lm_base_cache, 8));
501 return info->lm_base_cache;
505 /* Build a list of `struct solib' objects describing the shared
506 objects currently loaded in the inferior. This list does not
507 include an entry for the main executable file.
509 Note that we only gather information directly available from the
510 inferior --- we don't examine any of the shared library files
511 themselves. The declaration of `struct solib' says which fields
512 we provide values for. */
514 static intrusive_list<solib>
515 dsbt_current_sos (void)
517 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
518 CORE_ADDR lm_addr;
519 dsbt_info *info = get_dsbt_info (current_program_space);
520 intrusive_list<solib> sos;
522 /* Make sure that the main executable has been relocated. This is
523 required in order to find the address of the global offset table,
524 which in turn is used to find the link map info. (See lm_base
525 for details.)
527 Note that the relocation of the main executable is also performed
528 by solib_create_inferior_hook, however, in the case of core
529 files, this hook is called too late in order to be of benefit to
530 solib_add. solib_add eventually calls this function,
531 dsbt_current_sos, and also precedes the call to
532 solib_create_inferior_hook. (See post_create_inferior in
533 infcmd.c.) */
534 if (info->main_executable_lm_info == 0
535 && current_program_space->core_bfd () != nullptr)
536 dsbt_relocate_main_executable ();
538 /* Locate the address of the first link map struct. */
539 lm_addr = lm_base ();
541 /* We have at least one link map entry. Fetch the lot of them,
542 building the solist chain. */
543 while (lm_addr)
545 struct dbst_ext_link_map lm_buf;
546 ext_Elf32_Word indexword;
547 CORE_ADDR map_addr;
548 int dsbt_index;
549 int ret;
551 if (solib_dsbt_debug)
552 gdb_printf (gdb_stdlog,
553 "current_sos: reading link_map entry at %s\n",
554 hex_string_custom (lm_addr, 8));
556 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
557 if (ret)
559 warning (_("dsbt_current_sos: Unable to read link map entry."
560 " Shared object chain may be incomplete."));
561 break;
564 /* Fetch the load map address. */
565 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
566 sizeof lm_buf.l_addr.map,
567 byte_order);
569 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
570 sizeof indexword);
571 if (ret)
573 warning (_("dsbt_current_sos: Unable to read dsbt index."
574 " Shared object chain may be incomplete."));
575 break;
577 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
578 byte_order);
580 /* If the DSBT index is zero, then we're looking at the entry
581 for the main executable. By convention, we don't include
582 this in the list of shared objects. */
583 if (dsbt_index != 0)
585 struct int_elf32_dsbt_loadmap *loadmap;
586 CORE_ADDR addr;
588 loadmap = fetch_loadmap (map_addr);
589 if (loadmap == NULL)
591 warning (_("dsbt_current_sos: Unable to fetch load map."
592 " Shared object chain may be incomplete."));
593 break;
596 solib *sop = new solib;
597 auto li = std::make_unique<lm_info_dsbt> ();
598 li->map = loadmap;
599 /* Fetch the name. */
600 addr = extract_unsigned_integer (lm_buf.l_name,
601 sizeof (lm_buf.l_name),
602 byte_order);
603 gdb::unique_xmalloc_ptr<char> name_buf
604 = target_read_string (addr, SO_NAME_MAX_PATH_SIZE - 1);
606 if (name_buf == nullptr)
607 warning (_("Can't read pathname for link map entry."));
608 else
610 if (solib_dsbt_debug)
611 gdb_printf (gdb_stdlog, "current_sos: name = %s\n",
612 name_buf.get ());
614 sop->so_name = name_buf.get ();
615 sop->so_original_name = sop->so_name;
618 sos.push_back (*sop);
620 else
622 info->main_lm_addr = lm_addr;
625 lm_addr = extract_unsigned_integer (lm_buf.l_next,
626 sizeof (lm_buf.l_next), byte_order);
629 return sos;
632 /* Return 1 if PC lies in the dynamic symbol resolution code of the
633 run time loader. */
635 static int
636 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
638 dsbt_info *info = get_dsbt_info (current_program_space);
640 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
641 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
642 || in_plt_section (pc));
645 /* Print a warning about being unable to set the dynamic linker
646 breakpoint. */
648 static void
649 enable_break_failure_warning (void)
651 warning (_("Unable to find dynamic linker breakpoint function.\n"
652 "GDB will be unable to debug shared library initializers\n"
653 "and track explicitly loaded dynamic code."));
656 /* The dynamic linkers has, as part of its debugger interface, support
657 for arranging for the inferior to hit a breakpoint after mapping in
658 the shared libraries. This function enables that breakpoint.
660 On the TIC6X, using the shared library (DSBT), GDB can try to place
661 a breakpoint on '_dl_debug_state' to monitor the shared library
662 event. */
664 static int
665 enable_break (void)
667 asection *interp_sect;
669 if (current_program_space->exec_bfd () == NULL)
670 return 0;
672 if (!target_has_execution ())
673 return 0;
675 dsbt_info *info = get_dsbt_info (current_program_space);
677 info->interp_text_sect_low = 0;
678 info->interp_text_sect_high = 0;
679 info->interp_plt_sect_low = 0;
680 info->interp_plt_sect_high = 0;
682 /* Find the .interp section; if not found, warn the user and drop
683 into the old breakpoint at symbol code. */
684 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
685 ".interp");
686 if (interp_sect)
688 unsigned int interp_sect_size;
689 char *buf;
690 CORE_ADDR addr;
691 struct int_elf32_dsbt_loadmap *ldm;
692 int ret;
694 /* Read the contents of the .interp section into a local buffer;
695 the contents specify the dynamic linker this program uses. */
696 interp_sect_size = bfd_section_size (interp_sect);
697 buf = (char *) alloca (interp_sect_size);
698 bfd_get_section_contents (current_program_space->exec_bfd (),
699 interp_sect, buf, 0, interp_sect_size);
701 /* Now we need to figure out where the dynamic linker was
702 loaded so that we can load its symbols and place a breakpoint
703 in the dynamic linker itself. */
705 gdb_bfd_ref_ptr tmp_bfd;
708 tmp_bfd = solib_bfd_open (buf);
710 catch (const gdb_exception &ex)
714 if (tmp_bfd == NULL)
716 enable_break_failure_warning ();
717 return 0;
720 dsbt_get_initial_loadmaps ();
721 ldm = info->interp_loadmap;
723 /* Record the relocated start and end address of the dynamic linker
724 text and plt section for dsbt_in_dynsym_resolve_code. */
725 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
726 if (interp_sect)
728 info->interp_text_sect_low = bfd_section_vma (interp_sect);
729 info->interp_text_sect_low
730 += displacement_from_map (ldm, info->interp_text_sect_low);
731 info->interp_text_sect_high
732 = info->interp_text_sect_low + bfd_section_size (interp_sect);
734 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
735 if (interp_sect)
737 info->interp_plt_sect_low = bfd_section_vma (interp_sect);
738 info->interp_plt_sect_low
739 += displacement_from_map (ldm, info->interp_plt_sect_low);
740 info->interp_plt_sect_high
741 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
744 addr = (gdb_bfd_lookup_symbol
745 (tmp_bfd.get (),
746 [] (const asymbol *sym)
748 return strcmp (sym->name, "_dl_debug_state") == 0;
749 }));
751 if (addr != 0)
753 if (solib_dsbt_debug)
754 gdb_printf (gdb_stdlog,
755 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
756 hex_string_custom (addr, 8));
757 addr += displacement_from_map (ldm, addr);
759 if (solib_dsbt_debug)
760 gdb_printf (gdb_stdlog,
761 "enable_break: _dl_debug_state (after relocation) = %s\n",
762 hex_string_custom (addr, 8));
764 /* Now (finally!) create the solib breakpoint. */
765 create_solib_event_breakpoint (current_inferior ()->arch (), addr);
767 ret = 1;
769 else
771 if (solib_dsbt_debug)
772 gdb_printf (gdb_stdlog,
773 "enable_break: _dl_debug_state is not found\n");
774 ret = 0;
777 /* We're done with the loadmap. */
778 xfree (ldm);
780 return ret;
783 /* Tell the user we couldn't set a dynamic linker breakpoint. */
784 enable_break_failure_warning ();
786 /* Failure return. */
787 return 0;
790 static void
791 dsbt_relocate_main_executable (void)
793 struct int_elf32_dsbt_loadmap *ldm;
794 int changed;
795 dsbt_info *info = get_dsbt_info (current_program_space);
797 dsbt_get_initial_loadmaps ();
798 ldm = info->exec_loadmap;
800 delete info->main_executable_lm_info;
801 info->main_executable_lm_info = new lm_info_dsbt;
802 info->main_executable_lm_info->map = ldm;
804 objfile *objf = current_program_space->symfile_object_file;
805 section_offsets new_offsets (objf->section_offsets.size ());
806 changed = 0;
808 for (obj_section *osect : objf->sections ())
810 CORE_ADDR orig_addr, addr, offset;
811 int osect_idx;
812 int seg;
814 osect_idx = osect - objf->sections_start;
816 /* Current address of section. */
817 addr = osect->addr ();
818 /* Offset from where this section started. */
819 offset = objf->section_offsets[osect_idx];
820 /* Original address prior to any past relocations. */
821 orig_addr = addr - offset;
823 for (seg = 0; seg < ldm->nsegs; seg++)
825 if (ldm->segs[seg].p_vaddr <= orig_addr
826 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
828 new_offsets[osect_idx]
829 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
831 if (new_offsets[osect_idx] != offset)
832 changed = 1;
833 break;
838 if (changed)
839 objfile_relocate (objf, new_offsets);
841 /* Now that OBJF has been relocated, we can compute the GOT value
842 and stash it away. */
845 /* When gdb starts up the inferior, it nurses it along (through the
846 shell) until it is ready to execute its first instruction. At this
847 point, this function gets called via solib_create_inferior_hook.
849 For the DSBT shared library, the main executable needs to be relocated.
850 The shared library breakpoints also need to be enabled. */
852 static void
853 dsbt_solib_create_inferior_hook (int from_tty)
855 /* Relocate main executable. */
856 dsbt_relocate_main_executable ();
858 /* Enable shared library breakpoints. */
859 if (!enable_break ())
861 warning (_("shared library handler failed to enable breakpoint"));
862 return;
866 static void
867 dsbt_clear_solib (program_space *pspace)
869 dsbt_info *info = get_dsbt_info (pspace);
871 info->lm_base_cache = 0;
872 info->main_lm_addr = 0;
874 delete info->main_executable_lm_info;
875 info->main_executable_lm_info = NULL;
878 static void
879 dsbt_relocate_section_addresses (solib &so, target_section *sec)
881 int seg;
882 auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info.get ());
883 int_elf32_dsbt_loadmap *map = li->map;
885 for (seg = 0; seg < map->nsegs; seg++)
887 if (map->segs[seg].p_vaddr <= sec->addr
888 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
890 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
892 sec->addr += displ;
893 sec->endaddr += displ;
894 break;
898 static void
899 show_dsbt_debug (struct ui_file *file, int from_tty,
900 struct cmd_list_element *c, const char *value)
902 gdb_printf (file, _("solib-dsbt debugging is %s.\n"), value);
905 const solib_ops dsbt_so_ops =
907 dsbt_relocate_section_addresses,
908 nullptr,
909 dsbt_clear_solib,
910 dsbt_solib_create_inferior_hook,
911 dsbt_current_sos,
912 open_symbol_file_object,
913 dsbt_in_dynsym_resolve_code,
914 solib_bfd_open,
917 void _initialize_dsbt_solib ();
918 void
919 _initialize_dsbt_solib ()
921 /* Debug this file's internals. */
922 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
923 &solib_dsbt_debug, _("\
924 Set internal debugging of shared library code for DSBT ELF."), _("\
925 Show internal debugging of shared library code for DSBT ELF."), _("\
926 When non-zero, DSBT solib specific internal debugging is enabled."),
927 NULL,
928 show_dsbt_debug,
929 &setdebuglist, &showdebuglist);