Updated Malay translation for the bfd sub-directory
[binutils-gdb.git] / gdb / solib-dsbt.c
blob360aede8f301d457f066a6958a70711a5dd88ac8
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 "extract-store-integer.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 "command.h"
28 #include "gdb_bfd.h"
29 #include "solib-dsbt.h"
30 #include "elf/common.h"
31 #include "cli/cli-cmds.h"
33 #define GOT_MODULE_OFFSET 4
35 /* Flag which indicates whether internal debug messages should be printed. */
36 static unsigned int solib_dsbt_debug = 0;
38 /* TIC6X pointers are four bytes wide. */
39 enum { TIC6X_PTR_SIZE = 4 };
41 /* Representation of loadmap and related structs for the TIC6X DSBT. */
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];
50 struct ext_elf32_dsbt_loadseg
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;
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 */];
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
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;
87 struct int_elf32_dsbt_loadmap
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 */];
101 /* External link_map and elf32_dsbt_loadaddr struct definitions. */
103 typedef gdb_byte ext_ptr[4];
105 struct ext_elf32_dsbt_loadaddr
107 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
110 struct dbst_ext_link_map
112 struct ext_elf32_dsbt_loadaddr l_addr;
114 /* Absolute file name object was found in. */
115 ext_ptr l_name; /* char *l_name; */
117 /* Dynamic section of the shared object. */
118 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
120 /* Chain of loaded objects. */
121 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
124 /* Link map info to include in an allocated so_list entry */
126 struct lm_info_dsbt final : public lm_info
128 ~lm_info_dsbt ()
130 xfree (this->map);
133 /* The loadmap, digested into an easier to use form. */
134 int_elf32_dsbt_loadmap *map = NULL;
137 /* Per pspace dsbt specific data. */
139 struct dsbt_info
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;
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;
154 /* Cached value for lm_base, below. */
155 CORE_ADDR lm_base_cache = 0;
157 /* Link map address for main module. */
158 CORE_ADDR main_lm_addr = 0;
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;
166 /* Per-program-space data key. */
167 static const registry<program_space>::key<dsbt_info> solib_dsbt_pspace_data;
169 /* Get the dsbt solib data for PSPACE. If none is found yet, add it now. This
170 function always returns a valid object. */
172 static dsbt_info *
173 get_dsbt_info (program_space *pspace)
175 dsbt_info *info = solib_dsbt_pspace_data.get (pspace);
176 if (info != nullptr)
177 return info;
179 return solib_dsbt_pspace_data.emplace (pspace);
183 static void
184 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
186 int i;
188 if (map == NULL)
189 gdb_printf ("(null)\n");
190 else if (map->version != 0)
191 gdb_printf (_("Unsupported map version: %d\n"), map->version);
192 else
194 gdb_printf ("version %d\n", map->version);
196 for (i = 0; i < map->nsegs; i++)
197 gdb_printf ("%s:%s -> %s:%s\n",
198 print_core_address (current_inferior ()->arch (),
199 map->segs[i].p_vaddr),
200 print_core_address (current_inferior ()->arch (),
201 (map->segs[i].p_vaddr
202 + map->segs[i].p_memsz)),
203 print_core_address (current_inferior ()->arch (),
204 map->segs[i].addr),
205 print_core_address (current_inferior ()->arch (),
206 (map->segs[i].addr
207 + map->segs[i].p_memsz)));
211 /* Decode int_elf32_dsbt_loadmap from BUF. */
213 static struct int_elf32_dsbt_loadmap *
214 decode_loadmap (const gdb_byte *buf)
216 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
217 const struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
218 struct int_elf32_dsbt_loadmap *int_ldmbuf;
220 int version, seg, nsegs;
221 int int_ldmbuf_size;
223 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
225 /* Extract the version. */
226 version = extract_unsigned_integer (ext_ldmbuf->version,
227 sizeof ext_ldmbuf->version,
228 byte_order);
229 if (version != 0)
231 /* We only handle version 0. */
232 return NULL;
235 /* Extract the number of segments. */
236 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
237 sizeof ext_ldmbuf->nsegs,
238 byte_order);
240 if (nsegs <= 0)
241 return NULL;
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);
249 /* Place extracted information in internal structs. */
250 int_ldmbuf->version = version;
251 int_ldmbuf->nsegs = nsegs;
252 for (seg = 0; seg < nsegs; seg++)
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);
268 return int_ldmbuf;
271 /* Interrogate the Linux kernel to find out where the program was loaded.
272 There are two load maps; one for the executable and one for the
273 interpreter (only in the case of a dynamically linked executable). */
275 static void
276 dsbt_get_initial_loadmaps (void)
278 dsbt_info *info = get_dsbt_info (current_program_space);
279 std::optional<gdb::byte_vector> buf
280 = target_read_alloc (current_inferior ()->top_target (),
281 TARGET_OBJECT_FDPIC, "exec");
283 if (!buf || buf->empty ())
285 info->exec_loadmap = NULL;
286 error (_("Error reading DSBT exec loadmap"));
288 info->exec_loadmap = decode_loadmap (buf->data ());
289 if (solib_dsbt_debug)
290 dsbt_print_loadmap (info->exec_loadmap);
292 buf = target_read_alloc (current_inferior ()->top_target (),
293 TARGET_OBJECT_FDPIC, "exec");
294 if (!buf || buf->empty ())
296 info->interp_loadmap = NULL;
297 error (_("Error reading DSBT interp loadmap"));
299 info->interp_loadmap = decode_loadmap (buf->data ());
300 if (solib_dsbt_debug)
301 dsbt_print_loadmap (info->interp_loadmap);
304 /* Given address LDMADDR, fetch and decode the loadmap at that address.
305 Return NULL if there is a problem reading the target memory or if
306 there doesn't appear to be a loadmap at the given address. The
307 allocated space (representing the loadmap) returned by this
308 function may be freed via a single call to xfree. */
310 static struct int_elf32_dsbt_loadmap *
311 fetch_loadmap (CORE_ADDR ldmaddr)
313 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
314 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
315 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
316 struct int_elf32_dsbt_loadmap *int_ldmbuf;
317 int ext_ldmbuf_size, int_ldmbuf_size;
318 int version, seg, nsegs;
320 /* Fetch initial portion of the loadmap. */
321 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
322 sizeof ext_ldmbuf_partial))
324 /* Problem reading the target's memory. */
325 return NULL;
328 /* Extract the version. */
329 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
330 sizeof ext_ldmbuf_partial.version,
331 byte_order);
332 if (version != 0)
334 /* We only handle version 0. */
335 return NULL;
338 /* Extract the number of segments. */
339 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
340 sizeof ext_ldmbuf_partial.nsegs,
341 byte_order);
343 if (nsegs <= 0)
344 return NULL;
346 /* Allocate space for the complete (external) loadmap. */
347 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
348 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
349 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size);
351 /* Copy over the portion of the loadmap that's already been read. */
352 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
354 /* Read the rest of the loadmap from the target. */
355 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
356 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
357 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
359 /* Couldn't read rest of the loadmap. */
360 xfree (ext_ldmbuf);
361 return NULL;
364 /* Allocate space into which to put information extract from the
365 external loadsegs. I.e, allocate the internal loadsegs. */
366 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
367 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
368 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
370 /* Place extracted information in internal structs. */
371 int_ldmbuf->version = version;
372 int_ldmbuf->nsegs = nsegs;
373 for (seg = 0; seg < nsegs; seg++)
375 int_ldmbuf->segs[seg].addr
376 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
377 sizeof (ext_ldmbuf->segs[seg].addr),
378 byte_order);
379 int_ldmbuf->segs[seg].p_vaddr
380 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
381 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
382 byte_order);
383 int_ldmbuf->segs[seg].p_memsz
384 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
385 sizeof (ext_ldmbuf->segs[seg].p_memsz),
386 byte_order);
389 xfree (ext_ldmbuf);
390 return int_ldmbuf;
393 static void dsbt_relocate_main_executable (void);
394 static int enable_break (void);
396 /* See solist.h. */
398 static int
399 open_symbol_file_object (int from_tty)
401 /* Unimplemented. */
402 return 0;
405 /* Given a loadmap and an address, return the displacement needed
406 to relocate the address. */
408 static CORE_ADDR
409 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
410 CORE_ADDR addr)
412 int seg;
414 for (seg = 0; seg < map->nsegs; seg++)
415 if (map->segs[seg].p_vaddr <= addr
416 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
417 return map->segs[seg].addr - map->segs[seg].p_vaddr;
419 return 0;
422 /* Return the address from which the link map chain may be found. On
423 DSBT, a pointer to the start of the link map will be located at the
424 word found at base of GOT + GOT_MODULE_OFFSET.
426 The base of GOT may be found in a number of ways. Assuming that the
427 main executable has already been relocated,
428 1 The easiest way to find this value is to look up the address of
429 _GLOBAL_OFFSET_TABLE_.
430 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
431 address of Global Offset Table. .*/
433 static CORE_ADDR
434 lm_base (void)
436 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
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 bound_minimal_symbol got_sym
454 = lookup_minimal_symbol (current_program_space, "_GLOBAL_OFFSET_TABLE_",
455 current_program_space->symfile_object_file);
457 if (got_sym.minsym != 0)
459 addr = got_sym.value_address ();
460 if (solib_dsbt_debug)
461 gdb_printf (gdb_stdlog,
462 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
463 (unsigned int) addr);
465 else if (gdb_bfd_scan_elf_dyntag (DT_PLTGOT,
466 current_program_space->exec_bfd (),
467 &addr, NULL))
469 struct int_elf32_dsbt_loadmap *ldm;
471 dsbt_get_initial_loadmaps ();
472 ldm = info->exec_loadmap;
473 addr += displacement_from_map (ldm, addr);
474 if (solib_dsbt_debug)
475 gdb_printf (gdb_stdlog,
476 "lm_base: get addr %x by DT_PLTGOT.\n",
477 (unsigned int) addr);
479 else
481 if (solib_dsbt_debug)
482 gdb_printf (gdb_stdlog,
483 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
484 return 0;
486 addr += GOT_MODULE_OFFSET;
488 if (solib_dsbt_debug)
489 gdb_printf (gdb_stdlog,
490 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
491 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
493 if (target_read_memory (addr, buf, sizeof buf) != 0)
494 return 0;
495 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
497 if (solib_dsbt_debug)
498 gdb_printf (gdb_stdlog,
499 "lm_base: lm_base_cache = %s\n",
500 hex_string_custom (info->lm_base_cache, 8));
502 return info->lm_base_cache;
506 /* Build a list of `struct solib' objects describing the shared
507 objects currently loaded in the inferior. This list does not
508 include an entry for the main executable file.
510 Note that we only gather information directly available from the
511 inferior --- we don't examine any of the shared library files
512 themselves. The declaration of `struct solib' says which fields
513 we provide values for. */
515 static owning_intrusive_list<solib>
516 dsbt_current_sos (void)
518 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
519 CORE_ADDR lm_addr;
520 dsbt_info *info = get_dsbt_info (current_program_space);
521 owning_intrusive_list<solib> sos;
523 /* Make sure that the main executable has been relocated. This is
524 required in order to find the address of the global offset table,
525 which in turn is used to find the link map info. (See lm_base
526 for details.)
528 Note that the relocation of the main executable is also performed
529 by solib_create_inferior_hook, however, in the case of core
530 files, this hook is called too late in order to be of benefit to
531 solib_add. solib_add eventually calls this function,
532 dsbt_current_sos, and also precedes the call to
533 solib_create_inferior_hook. (See post_create_inferior in
534 infcmd.c.) */
535 if (info->main_executable_lm_info == 0
536 && current_program_space->core_bfd () != nullptr)
537 dsbt_relocate_main_executable ();
539 /* Locate the address of the first link map struct. */
540 lm_addr = lm_base ();
542 /* We have at least one link map entry. Fetch the lot of them,
543 building the solist chain. */
544 while (lm_addr)
546 struct dbst_ext_link_map lm_buf;
547 ext_Elf32_Word indexword;
548 CORE_ADDR map_addr;
549 int dsbt_index;
550 int ret;
552 if (solib_dsbt_debug)
553 gdb_printf (gdb_stdlog,
554 "current_sos: reading link_map entry at %s\n",
555 hex_string_custom (lm_addr, 8));
557 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
558 if (ret)
560 warning (_("dsbt_current_sos: Unable to read link map entry."
561 " Shared object chain may be incomplete."));
562 break;
565 /* Fetch the load map address. */
566 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
567 sizeof lm_buf.l_addr.map,
568 byte_order);
570 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
571 sizeof indexword);
572 if (ret)
574 warning (_("dsbt_current_sos: Unable to read dsbt index."
575 " Shared object chain may be incomplete."));
576 break;
578 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
579 byte_order);
581 /* If the DSBT index is zero, then we're looking at the entry
582 for the main executable. By convention, we don't include
583 this in the list of shared objects. */
584 if (dsbt_index != 0)
586 struct int_elf32_dsbt_loadmap *loadmap;
587 CORE_ADDR addr;
589 loadmap = fetch_loadmap (map_addr);
590 if (loadmap == NULL)
592 warning (_("dsbt_current_sos: Unable to fetch load map."
593 " Shared object chain may be incomplete."));
594 break;
597 auto &sop = sos.emplace_back ();
598 auto li = std::make_unique<lm_info_dsbt> ();
599 li->map = loadmap;
600 /* Fetch the name. */
601 addr = extract_unsigned_integer (lm_buf.l_name,
602 sizeof (lm_buf.l_name),
603 byte_order);
604 gdb::unique_xmalloc_ptr<char> name_buf
605 = target_read_string (addr, SO_NAME_MAX_PATH_SIZE - 1);
607 if (name_buf == nullptr)
608 warning (_("Can't read pathname for link map entry."));
609 else
611 if (solib_dsbt_debug)
612 gdb_printf (gdb_stdlog, "current_sos: name = %s\n",
613 name_buf.get ());
615 sop.so_name = name_buf.get ();
616 sop.so_original_name = sop.so_name;
619 sop.lm_info = std::move (li);
621 else
623 info->main_lm_addr = lm_addr;
626 lm_addr = extract_unsigned_integer (lm_buf.l_next,
627 sizeof (lm_buf.l_next), byte_order);
630 return sos;
633 /* Return 1 if PC lies in the dynamic symbol resolution code of the
634 run time loader. */
636 static int
637 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
639 dsbt_info *info = get_dsbt_info (current_program_space);
641 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
642 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
643 || in_plt_section (pc));
646 /* Print a warning about being unable to set the dynamic linker
647 breakpoint. */
649 static void
650 enable_break_failure_warning (void)
652 warning (_("Unable to find dynamic linker breakpoint function.\n"
653 "GDB will be unable to debug shared library initializers\n"
654 "and track explicitly loaded dynamic code."));
657 /* The dynamic linkers has, as part of its debugger interface, support
658 for arranging for the inferior to hit a breakpoint after mapping in
659 the shared libraries. This function enables that breakpoint.
661 On the TIC6X, using the shared library (DSBT), GDB can try to place
662 a breakpoint on '_dl_debug_state' to monitor the shared library
663 event. */
665 static int
666 enable_break (void)
668 asection *interp_sect;
670 if (current_program_space->exec_bfd () == NULL)
671 return 0;
673 if (!target_has_execution ())
674 return 0;
676 dsbt_info *info = get_dsbt_info (current_program_space);
678 info->interp_text_sect_low = 0;
679 info->interp_text_sect_high = 0;
680 info->interp_plt_sect_low = 0;
681 info->interp_plt_sect_high = 0;
683 /* Find the .interp section; if not found, warn the user and drop
684 into the old breakpoint at symbol code. */
685 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
686 ".interp");
687 if (interp_sect)
689 unsigned int interp_sect_size;
690 char *buf;
691 CORE_ADDR addr;
692 struct int_elf32_dsbt_loadmap *ldm;
693 int ret;
695 /* Read the contents of the .interp section into a local buffer;
696 the contents specify the dynamic linker this program uses. */
697 interp_sect_size = bfd_section_size (interp_sect);
698 buf = (char *) alloca (interp_sect_size);
699 bfd_get_section_contents (current_program_space->exec_bfd (),
700 interp_sect, buf, 0, interp_sect_size);
702 /* Now we need to figure out where the dynamic linker was
703 loaded so that we can load its symbols and place a breakpoint
704 in the dynamic linker itself. */
706 gdb_bfd_ref_ptr tmp_bfd;
709 tmp_bfd = solib_bfd_open (buf);
711 catch (const gdb_exception &ex)
715 if (tmp_bfd == NULL)
717 enable_break_failure_warning ();
718 return 0;
721 dsbt_get_initial_loadmaps ();
722 ldm = info->interp_loadmap;
724 /* Record the relocated start and end address of the dynamic linker
725 text and plt section for dsbt_in_dynsym_resolve_code. */
726 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
727 if (interp_sect)
729 info->interp_text_sect_low = bfd_section_vma (interp_sect);
730 info->interp_text_sect_low
731 += displacement_from_map (ldm, info->interp_text_sect_low);
732 info->interp_text_sect_high
733 = info->interp_text_sect_low + bfd_section_size (interp_sect);
735 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
736 if (interp_sect)
738 info->interp_plt_sect_low = bfd_section_vma (interp_sect);
739 info->interp_plt_sect_low
740 += displacement_from_map (ldm, info->interp_plt_sect_low);
741 info->interp_plt_sect_high
742 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
745 addr = (gdb_bfd_lookup_symbol
746 (tmp_bfd.get (),
747 [] (const asymbol *sym)
749 return strcmp (sym->name, "_dl_debug_state") == 0;
750 }));
752 if (addr != 0)
754 if (solib_dsbt_debug)
755 gdb_printf (gdb_stdlog,
756 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
757 hex_string_custom (addr, 8));
758 addr += displacement_from_map (ldm, addr);
760 if (solib_dsbt_debug)
761 gdb_printf (gdb_stdlog,
762 "enable_break: _dl_debug_state (after relocation) = %s\n",
763 hex_string_custom (addr, 8));
765 /* Now (finally!) create the solib breakpoint. */
766 create_solib_event_breakpoint (current_inferior ()->arch (), addr);
768 ret = 1;
770 else
772 if (solib_dsbt_debug)
773 gdb_printf (gdb_stdlog,
774 "enable_break: _dl_debug_state is not found\n");
775 ret = 0;
778 /* We're done with the loadmap. */
779 xfree (ldm);
781 return ret;
784 /* Tell the user we couldn't set a dynamic linker breakpoint. */
785 enable_break_failure_warning ();
787 /* Failure return. */
788 return 0;
791 static void
792 dsbt_relocate_main_executable (void)
794 struct int_elf32_dsbt_loadmap *ldm;
795 int changed;
796 dsbt_info *info = get_dsbt_info (current_program_space);
798 dsbt_get_initial_loadmaps ();
799 ldm = info->exec_loadmap;
801 delete info->main_executable_lm_info;
802 info->main_executable_lm_info = new lm_info_dsbt;
803 info->main_executable_lm_info->map = ldm;
805 objfile *objf = current_program_space->symfile_object_file;
806 section_offsets new_offsets (objf->section_offsets.size ());
807 changed = 0;
809 for (obj_section *osect : objf->sections ())
811 CORE_ADDR orig_addr, addr, offset;
812 int osect_idx;
813 int seg;
815 osect_idx = osect - objf->sections_start;
817 /* Current address of section. */
818 addr = osect->addr ();
819 /* Offset from where this section started. */
820 offset = objf->section_offsets[osect_idx];
821 /* Original address prior to any past relocations. */
822 orig_addr = addr - offset;
824 for (seg = 0; seg < ldm->nsegs; seg++)
826 if (ldm->segs[seg].p_vaddr <= orig_addr
827 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
829 new_offsets[osect_idx]
830 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
832 if (new_offsets[osect_idx] != offset)
833 changed = 1;
834 break;
839 if (changed)
840 objfile_relocate (objf, new_offsets);
842 /* Now that OBJF has been relocated, we can compute the GOT value
843 and stash it away. */
846 /* When gdb starts up the inferior, it nurses it along (through the
847 shell) until it is ready to execute its first instruction. At this
848 point, this function gets called via solib_create_inferior_hook.
850 For the DSBT shared library, the main executable needs to be relocated.
851 The shared library breakpoints also need to be enabled. */
853 static void
854 dsbt_solib_create_inferior_hook (int from_tty)
856 /* Relocate main executable. */
857 dsbt_relocate_main_executable ();
859 /* Enable shared library breakpoints. */
860 if (!enable_break ())
862 warning (_("shared library handler failed to enable breakpoint"));
863 return;
867 static void
868 dsbt_clear_solib (program_space *pspace)
870 dsbt_info *info = get_dsbt_info (pspace);
872 info->lm_base_cache = 0;
873 info->main_lm_addr = 0;
875 delete info->main_executable_lm_info;
876 info->main_executable_lm_info = NULL;
879 static void
880 dsbt_relocate_section_addresses (solib &so, target_section *sec)
882 int seg;
883 auto *li = gdb::checked_static_cast<lm_info_dsbt *> (so.lm_info.get ());
884 int_elf32_dsbt_loadmap *map = li->map;
886 for (seg = 0; seg < map->nsegs; seg++)
888 if (map->segs[seg].p_vaddr <= sec->addr
889 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
891 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
893 sec->addr += displ;
894 sec->endaddr += displ;
895 break;
899 static void
900 show_dsbt_debug (struct ui_file *file, int from_tty,
901 struct cmd_list_element *c, const char *value)
903 gdb_printf (file, _("solib-dsbt debugging is %s.\n"), value);
906 const solib_ops dsbt_so_ops =
908 dsbt_relocate_section_addresses,
909 nullptr,
910 dsbt_clear_solib,
911 dsbt_solib_create_inferior_hook,
912 dsbt_current_sos,
913 open_symbol_file_object,
914 dsbt_in_dynsym_resolve_code,
915 solib_bfd_open,
916 nullptr,
917 nullptr,
918 nullptr,
919 nullptr,
920 default_find_solib_addr,
923 void _initialize_dsbt_solib ();
924 void
925 _initialize_dsbt_solib ()
927 /* Debug this file's internals. */
928 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
929 &solib_dsbt_debug, _("\
930 Set internal debugging of shared library code for DSBT ELF."), _("\
931 Show internal debugging of shared library code for DSBT ELF."), _("\
932 When non-zero, DSBT solib specific internal debugging is enabled."),
933 NULL,
934 show_dsbt_debug,
935 &setdebuglist, &showdebuglist);