Automatic date update in version.in
[binutils-gdb.git] / gdb / solib-dsbt.c
blobb866a0b4517771ace75e66a01a65d849683197e2
1 /* Handle TIC6X (DSBT) shared libraries for GDB, the GNU Debugger.
2 Copyright (C) 2010-2022 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 "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 #include "solib-dsbt.h"
34 #define GOT_MODULE_OFFSET 4
36 /* Flag which indicates whether internal debug messages should be printed. */
37 static unsigned int solib_dsbt_debug = 0;
39 /* TIC6X pointers are four bytes wide. */
40 enum { TIC6X_PTR_SIZE = 4 };
42 /* Representation of loadmap and related structs for the TIC6X DSBT. */
44 /* External versions; the size and alignment of the fields should be
45 the same as those on the target. When loaded, the placement of
46 the bits in each field will be the same as on the target. */
47 typedef gdb_byte ext_Elf32_Half[2];
48 typedef gdb_byte ext_Elf32_Addr[4];
49 typedef gdb_byte ext_Elf32_Word[4];
51 struct ext_elf32_dsbt_loadseg
53 /* Core address to which the segment is mapped. */
54 ext_Elf32_Addr addr;
55 /* VMA recorded in the program header. */
56 ext_Elf32_Addr p_vaddr;
57 /* Size of this segment in memory. */
58 ext_Elf32_Word p_memsz;
61 struct ext_elf32_dsbt_loadmap {
62 /* Protocol version number, must be zero. */
63 ext_Elf32_Word version;
64 /* A pointer to the DSBT table; the DSBT size and the index of this
65 module. */
66 ext_Elf32_Word dsbt_table_ptr;
67 ext_Elf32_Word dsbt_size;
68 ext_Elf32_Word dsbt_index;
69 /* Number of segments in this map. */
70 ext_Elf32_Word nsegs;
71 /* The actual memory map. */
72 struct ext_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
75 /* Internal versions; the types are GDB types and the data in each
76 of the fields is (or will be) decoded from the external struct
77 for ease of consumption. */
78 struct int_elf32_dsbt_loadseg
80 /* Core address to which the segment is mapped. */
81 CORE_ADDR addr;
82 /* VMA recorded in the program header. */
83 CORE_ADDR p_vaddr;
84 /* Size of this segment in memory. */
85 long p_memsz;
88 struct int_elf32_dsbt_loadmap
90 /* Protocol version number, must be zero. */
91 int version;
92 CORE_ADDR dsbt_table_ptr;
93 /* A pointer to the DSBT table; the DSBT size and the index of this
94 module. */
95 int dsbt_size, dsbt_index;
96 /* Number of segments in this map. */
97 int nsegs;
98 /* The actual memory map. */
99 struct int_elf32_dsbt_loadseg segs[1 /* nsegs, actually */];
102 /* External link_map and elf32_dsbt_loadaddr struct definitions. */
104 typedef gdb_byte ext_ptr[4];
106 struct ext_elf32_dsbt_loadaddr
108 ext_ptr map; /* struct elf32_dsbt_loadmap *map; */
111 struct dbst_ext_link_map
113 struct ext_elf32_dsbt_loadaddr l_addr;
115 /* Absolute file name object was found in. */
116 ext_ptr l_name; /* char *l_name; */
118 /* Dynamic section of the shared object. */
119 ext_ptr l_ld; /* ElfW(Dyn) *l_ld; */
121 /* Chain of loaded objects. */
122 ext_ptr l_next, l_prev; /* struct link_map *l_next, *l_prev; */
125 /* Link map info to include in an allocated so_list entry */
127 struct lm_info_dsbt : public lm_info_base
129 ~lm_info_dsbt ()
131 xfree (this->map);
134 /* The loadmap, digested into an easier to use form. */
135 int_elf32_dsbt_loadmap *map = NULL;
138 /* Per pspace dsbt specific data. */
140 struct dsbt_info
142 /* The load map, got value, etc. are not available from the chain
143 of loaded shared objects. ``main_executable_lm_info'' provides
144 a way to get at this information so that it doesn't need to be
145 frequently recomputed. Initialized by dsbt_relocate_main_executable. */
146 struct lm_info_dsbt *main_executable_lm_info = nullptr;
148 /* Load maps for the main executable and the interpreter. These are obtained
149 from ptrace. They are the starting point for getting into the program,
150 and are required to find the solib list with the individual load maps for
151 each module. */
152 struct int_elf32_dsbt_loadmap *exec_loadmap = nullptr;
153 struct int_elf32_dsbt_loadmap *interp_loadmap = nullptr;
155 /* Cached value for lm_base, below. */
156 CORE_ADDR lm_base_cache = 0;
158 /* Link map address for main module. */
159 CORE_ADDR main_lm_addr = 0;
161 CORE_ADDR interp_text_sect_low = 0;
162 CORE_ADDR interp_text_sect_high = 0;
163 CORE_ADDR interp_plt_sect_low = 0;
164 CORE_ADDR interp_plt_sect_high = 0;
167 /* Per-program-space data key. */
168 static const registry<program_space>::key<dsbt_info> solib_dsbt_pspace_data;
170 /* Get the current dsbt data. If none is found yet, add it now. This
171 function always returns a valid object. */
173 static struct dsbt_info *
174 get_dsbt_info (void)
176 struct dsbt_info *info;
178 info = solib_dsbt_pspace_data.get (current_program_space);
179 if (info != NULL)
180 return info;
182 return solib_dsbt_pspace_data.emplace (current_program_space);
186 static void
187 dsbt_print_loadmap (struct int_elf32_dsbt_loadmap *map)
189 int i;
191 if (map == NULL)
192 gdb_printf ("(null)\n");
193 else if (map->version != 0)
194 gdb_printf (_("Unsupported map version: %d\n"), map->version);
195 else
197 gdb_printf ("version %d\n", map->version);
199 for (i = 0; i < map->nsegs; i++)
200 gdb_printf ("%s:%s -> %s:%s\n",
201 print_core_address (target_gdbarch (),
202 map->segs[i].p_vaddr),
203 print_core_address (target_gdbarch (),
204 map->segs[i].p_vaddr
205 + map->segs[i].p_memsz),
206 print_core_address (target_gdbarch (), map->segs[i].addr),
207 print_core_address (target_gdbarch (), map->segs[i].addr
208 + map->segs[i].p_memsz));
212 /* Decode int_elf32_dsbt_loadmap from BUF. */
214 static struct int_elf32_dsbt_loadmap *
215 decode_loadmap (const gdb_byte *buf)
217 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
218 const struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
219 struct int_elf32_dsbt_loadmap *int_ldmbuf;
221 int version, seg, nsegs;
222 int int_ldmbuf_size;
224 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) buf;
226 /* Extract the version. */
227 version = extract_unsigned_integer (ext_ldmbuf->version,
228 sizeof ext_ldmbuf->version,
229 byte_order);
230 if (version != 0)
232 /* We only handle version 0. */
233 return NULL;
236 /* Extract the number of segments. */
237 nsegs = extract_unsigned_integer (ext_ldmbuf->nsegs,
238 sizeof ext_ldmbuf->nsegs,
239 byte_order);
241 if (nsegs <= 0)
242 return NULL;
244 /* Allocate space into which to put information extract from the
245 external loadsegs. I.e, allocate the internal loadsegs. */
246 int_ldmbuf_size = (sizeof (struct int_elf32_dsbt_loadmap)
247 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg));
248 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
250 /* Place extracted information in internal structs. */
251 int_ldmbuf->version = version;
252 int_ldmbuf->nsegs = nsegs;
253 for (seg = 0; seg < nsegs; seg++)
255 int_ldmbuf->segs[seg].addr
256 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
257 sizeof (ext_ldmbuf->segs[seg].addr),
258 byte_order);
259 int_ldmbuf->segs[seg].p_vaddr
260 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
261 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
262 byte_order);
263 int_ldmbuf->segs[seg].p_memsz
264 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
265 sizeof (ext_ldmbuf->segs[seg].p_memsz),
266 byte_order);
269 return int_ldmbuf;
273 static struct dsbt_info *get_dsbt_info (void);
275 /* Interrogate the Linux kernel to find out where the program was loaded.
276 There are two load maps; one for the executable and one for the
277 interpreter (only in the case of a dynamically linked executable). */
279 static void
280 dsbt_get_initial_loadmaps (void)
282 struct dsbt_info *info = get_dsbt_info ();
283 gdb::optional<gdb::byte_vector> buf
284 = target_read_alloc (current_inferior ()->top_target (),
285 TARGET_OBJECT_FDPIC, "exec");
287 if (!buf || buf->empty ())
289 info->exec_loadmap = NULL;
290 error (_("Error reading DSBT exec loadmap"));
292 info->exec_loadmap = decode_loadmap (buf->data ());
293 if (solib_dsbt_debug)
294 dsbt_print_loadmap (info->exec_loadmap);
296 buf = target_read_alloc (current_inferior ()->top_target (),
297 TARGET_OBJECT_FDPIC, "exec");
298 if (!buf || buf->empty ())
300 info->interp_loadmap = NULL;
301 error (_("Error reading DSBT interp loadmap"));
303 info->interp_loadmap = decode_loadmap (buf->data ());
304 if (solib_dsbt_debug)
305 dsbt_print_loadmap (info->interp_loadmap);
308 /* Given address LDMADDR, fetch and decode the loadmap at that address.
309 Return NULL if there is a problem reading the target memory or if
310 there doesn't appear to be a loadmap at the given address. The
311 allocated space (representing the loadmap) returned by this
312 function may be freed via a single call to xfree. */
314 static struct int_elf32_dsbt_loadmap *
315 fetch_loadmap (CORE_ADDR ldmaddr)
317 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
318 struct ext_elf32_dsbt_loadmap ext_ldmbuf_partial;
319 struct ext_elf32_dsbt_loadmap *ext_ldmbuf;
320 struct int_elf32_dsbt_loadmap *int_ldmbuf;
321 int ext_ldmbuf_size, int_ldmbuf_size;
322 int version, seg, nsegs;
324 /* Fetch initial portion of the loadmap. */
325 if (target_read_memory (ldmaddr, (gdb_byte *) &ext_ldmbuf_partial,
326 sizeof ext_ldmbuf_partial))
328 /* Problem reading the target's memory. */
329 return NULL;
332 /* Extract the version. */
333 version = extract_unsigned_integer (ext_ldmbuf_partial.version,
334 sizeof ext_ldmbuf_partial.version,
335 byte_order);
336 if (version != 0)
338 /* We only handle version 0. */
339 return NULL;
342 /* Extract the number of segments. */
343 nsegs = extract_unsigned_integer (ext_ldmbuf_partial.nsegs,
344 sizeof ext_ldmbuf_partial.nsegs,
345 byte_order);
347 if (nsegs <= 0)
348 return NULL;
350 /* Allocate space for the complete (external) loadmap. */
351 ext_ldmbuf_size = sizeof (struct ext_elf32_dsbt_loadmap)
352 + (nsegs - 1) * sizeof (struct ext_elf32_dsbt_loadseg);
353 ext_ldmbuf = (struct ext_elf32_dsbt_loadmap *) xmalloc (ext_ldmbuf_size);
355 /* Copy over the portion of the loadmap that's already been read. */
356 memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial);
358 /* Read the rest of the loadmap from the target. */
359 if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial,
360 (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial,
361 ext_ldmbuf_size - sizeof ext_ldmbuf_partial))
363 /* Couldn't read rest of the loadmap. */
364 xfree (ext_ldmbuf);
365 return NULL;
368 /* Allocate space into which to put information extract from the
369 external loadsegs. I.e, allocate the internal loadsegs. */
370 int_ldmbuf_size = sizeof (struct int_elf32_dsbt_loadmap)
371 + (nsegs - 1) * sizeof (struct int_elf32_dsbt_loadseg);
372 int_ldmbuf = (struct int_elf32_dsbt_loadmap *) xmalloc (int_ldmbuf_size);
374 /* Place extracted information in internal structs. */
375 int_ldmbuf->version = version;
376 int_ldmbuf->nsegs = nsegs;
377 for (seg = 0; seg < nsegs; seg++)
379 int_ldmbuf->segs[seg].addr
380 = extract_unsigned_integer (ext_ldmbuf->segs[seg].addr,
381 sizeof (ext_ldmbuf->segs[seg].addr),
382 byte_order);
383 int_ldmbuf->segs[seg].p_vaddr
384 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_vaddr,
385 sizeof (ext_ldmbuf->segs[seg].p_vaddr),
386 byte_order);
387 int_ldmbuf->segs[seg].p_memsz
388 = extract_unsigned_integer (ext_ldmbuf->segs[seg].p_memsz,
389 sizeof (ext_ldmbuf->segs[seg].p_memsz),
390 byte_order);
393 xfree (ext_ldmbuf);
394 return int_ldmbuf;
397 static void dsbt_relocate_main_executable (void);
398 static int enable_break (void);
400 /* See solist.h. */
402 static int
403 open_symbol_file_object (int from_tty)
405 /* Unimplemented. */
406 return 0;
409 /* Given a loadmap and an address, return the displacement needed
410 to relocate the address. */
412 static CORE_ADDR
413 displacement_from_map (struct int_elf32_dsbt_loadmap *map,
414 CORE_ADDR addr)
416 int seg;
418 for (seg = 0; seg < map->nsegs; seg++)
419 if (map->segs[seg].p_vaddr <= addr
420 && addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
421 return map->segs[seg].addr - map->segs[seg].p_vaddr;
423 return 0;
426 /* Return the address from which the link map chain may be found. On
427 DSBT, a pointer to the start of the link map will be located at the
428 word found at base of GOT + GOT_MODULE_OFFSET.
430 The base of GOT may be found in a number of ways. Assuming that the
431 main executable has already been relocated,
432 1 The easiest way to find this value is to look up the address of
433 _GLOBAL_OFFSET_TABLE_.
434 2 The other way is to look for tag DT_PLTGOT, which contains the virtual
435 address of Global Offset Table. .*/
437 static CORE_ADDR
438 lm_base (void)
440 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
441 struct bound_minimal_symbol got_sym;
442 CORE_ADDR addr;
443 gdb_byte buf[TIC6X_PTR_SIZE];
444 struct dsbt_info *info = get_dsbt_info ();
446 /* One of our assumptions is that the main executable has been relocated.
447 Bail out if this has not happened. (Note that post_create_inferior
448 in infcmd.c will call solib_add prior to solib_create_inferior_hook.
449 If we allow this to happen, lm_base_cache will be initialized with
450 a bogus value. */
451 if (info->main_executable_lm_info == 0)
452 return 0;
454 /* If we already have a cached value, return it. */
455 if (info->lm_base_cache)
456 return info->lm_base_cache;
458 got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
459 current_program_space->symfile_object_file);
461 if (got_sym.minsym != 0)
463 addr = got_sym.value_address ();
464 if (solib_dsbt_debug)
465 gdb_printf (gdb_stdlog,
466 "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
467 (unsigned int) addr);
469 else if (gdb_bfd_scan_elf_dyntag (DT_PLTGOT,
470 current_program_space->exec_bfd (),
471 &addr, NULL))
473 struct int_elf32_dsbt_loadmap *ldm;
475 dsbt_get_initial_loadmaps ();
476 ldm = info->exec_loadmap;
477 addr += displacement_from_map (ldm, addr);
478 if (solib_dsbt_debug)
479 gdb_printf (gdb_stdlog,
480 "lm_base: get addr %x by DT_PLTGOT.\n",
481 (unsigned int) addr);
483 else
485 if (solib_dsbt_debug)
486 gdb_printf (gdb_stdlog,
487 "lm_base: _GLOBAL_OFFSET_TABLE_ not found.\n");
488 return 0;
490 addr += GOT_MODULE_OFFSET;
492 if (solib_dsbt_debug)
493 gdb_printf (gdb_stdlog,
494 "lm_base: _GLOBAL_OFFSET_TABLE_ + %d = %s\n",
495 GOT_MODULE_OFFSET, hex_string_custom (addr, 8));
497 if (target_read_memory (addr, buf, sizeof buf) != 0)
498 return 0;
499 info->lm_base_cache = extract_unsigned_integer (buf, sizeof buf, byte_order);
501 if (solib_dsbt_debug)
502 gdb_printf (gdb_stdlog,
503 "lm_base: lm_base_cache = %s\n",
504 hex_string_custom (info->lm_base_cache, 8));
506 return info->lm_base_cache;
510 /* Build a list of `struct so_list' objects describing the shared
511 objects currently loaded in the inferior. This list does not
512 include an entry for the main executable file.
514 Note that we only gather information directly available from the
515 inferior --- we don't examine any of the shared library files
516 themselves. The declaration of `struct so_list' says which fields
517 we provide values for. */
519 static struct so_list *
520 dsbt_current_sos (void)
522 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
523 CORE_ADDR lm_addr;
524 struct so_list *sos_head = NULL;
525 struct so_list **sos_next_ptr = &sos_head;
526 struct dsbt_info *info = get_dsbt_info ();
528 /* Make sure that the main executable has been relocated. This is
529 required in order to find the address of the global offset table,
530 which in turn is used to find the link map info. (See lm_base
531 for details.)
533 Note that the relocation of the main executable is also performed
534 by solib_create_inferior_hook, however, in the case of core
535 files, this hook is called too late in order to be of benefit to
536 solib_add. solib_add eventually calls this function,
537 dsbt_current_sos, and also precedes the call to
538 solib_create_inferior_hook. (See post_create_inferior in
539 infcmd.c.) */
540 if (info->main_executable_lm_info == 0 && core_bfd != NULL)
541 dsbt_relocate_main_executable ();
543 /* Locate the address of the first link map struct. */
544 lm_addr = lm_base ();
546 /* We have at least one link map entry. Fetch the lot of them,
547 building the solist chain. */
548 while (lm_addr)
550 struct dbst_ext_link_map lm_buf;
551 ext_Elf32_Word indexword;
552 CORE_ADDR map_addr;
553 int dsbt_index;
554 int ret;
556 if (solib_dsbt_debug)
557 gdb_printf (gdb_stdlog,
558 "current_sos: reading link_map entry at %s\n",
559 hex_string_custom (lm_addr, 8));
561 ret = target_read_memory (lm_addr, (gdb_byte *) &lm_buf, sizeof (lm_buf));
562 if (ret)
564 warning (_("dsbt_current_sos: Unable to read link map entry."
565 " Shared object chain may be incomplete."));
566 break;
569 /* Fetch the load map address. */
570 map_addr = extract_unsigned_integer (lm_buf.l_addr.map,
571 sizeof lm_buf.l_addr.map,
572 byte_order);
574 ret = target_read_memory (map_addr + 12, (gdb_byte *) &indexword,
575 sizeof indexword);
576 if (ret)
578 warning (_("dsbt_current_sos: Unable to read dsbt index."
579 " Shared object chain may be incomplete."));
580 break;
582 dsbt_index = extract_unsigned_integer (indexword, sizeof indexword,
583 byte_order);
585 /* If the DSBT index is zero, then we're looking at the entry
586 for the main executable. By convention, we don't include
587 this in the list of shared objects. */
588 if (dsbt_index != 0)
590 struct int_elf32_dsbt_loadmap *loadmap;
591 struct so_list *sop;
592 CORE_ADDR addr;
594 loadmap = fetch_loadmap (map_addr);
595 if (loadmap == NULL)
597 warning (_("dsbt_current_sos: Unable to fetch load map."
598 " Shared object chain may be incomplete."));
599 break;
602 sop = XCNEW (struct so_list);
603 lm_info_dsbt *li = new lm_info_dsbt;
604 sop->lm_info = li;
605 li->map = loadmap;
606 /* Fetch the name. */
607 addr = extract_unsigned_integer (lm_buf.l_name,
608 sizeof (lm_buf.l_name),
609 byte_order);
610 gdb::unique_xmalloc_ptr<char> name_buf
611 = target_read_string (addr, SO_NAME_MAX_PATH_SIZE - 1);
613 if (name_buf == nullptr)
614 warning (_("Can't read pathname for link map entry."));
615 else
617 if (solib_dsbt_debug)
618 gdb_printf (gdb_stdlog, "current_sos: name = %s\n",
619 name_buf.get ());
621 strncpy (sop->so_name, name_buf.get (), SO_NAME_MAX_PATH_SIZE - 1);
622 sop->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
623 strcpy (sop->so_original_name, sop->so_name);
626 *sos_next_ptr = sop;
627 sos_next_ptr = &sop->next;
629 else
631 info->main_lm_addr = lm_addr;
634 lm_addr = extract_unsigned_integer (lm_buf.l_next,
635 sizeof (lm_buf.l_next), byte_order);
638 return sos_head;
641 /* Return 1 if PC lies in the dynamic symbol resolution code of the
642 run time loader. */
644 static int
645 dsbt_in_dynsym_resolve_code (CORE_ADDR pc)
647 struct dsbt_info *info = get_dsbt_info ();
649 return ((pc >= info->interp_text_sect_low && pc < info->interp_text_sect_high)
650 || (pc >= info->interp_plt_sect_low && pc < info->interp_plt_sect_high)
651 || in_plt_section (pc));
654 /* Print a warning about being unable to set the dynamic linker
655 breakpoint. */
657 static void
658 enable_break_failure_warning (void)
660 warning (_("Unable to find dynamic linker breakpoint function.\n"
661 "GDB will be unable to debug shared library initializers\n"
662 "and track explicitly loaded dynamic code."));
665 /* Helper function for gdb_bfd_lookup_symbol. */
667 static int
668 cmp_name (const asymbol *sym, const void *data)
670 return (strcmp (sym->name, (const char *) data) == 0);
673 /* The dynamic linkers has, as part of its debugger interface, support
674 for arranging for the inferior to hit a breakpoint after mapping in
675 the shared libraries. This function enables that breakpoint.
677 On the TIC6X, using the shared library (DSBT), GDB can try to place
678 a breakpoint on '_dl_debug_state' to monitor the shared library
679 event. */
681 static int
682 enable_break (void)
684 asection *interp_sect;
685 struct dsbt_info *info;
687 if (current_program_space->exec_bfd () == NULL)
688 return 0;
690 if (!target_has_execution ())
691 return 0;
693 info = get_dsbt_info ();
695 info->interp_text_sect_low = 0;
696 info->interp_text_sect_high = 0;
697 info->interp_plt_sect_low = 0;
698 info->interp_plt_sect_high = 0;
700 /* Find the .interp section; if not found, warn the user and drop
701 into the old breakpoint at symbol code. */
702 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
703 ".interp");
704 if (interp_sect)
706 unsigned int interp_sect_size;
707 char *buf;
708 CORE_ADDR addr;
709 struct int_elf32_dsbt_loadmap *ldm;
710 int ret;
712 /* Read the contents of the .interp section into a local buffer;
713 the contents specify the dynamic linker this program uses. */
714 interp_sect_size = bfd_section_size (interp_sect);
715 buf = (char *) alloca (interp_sect_size);
716 bfd_get_section_contents (current_program_space->exec_bfd (),
717 interp_sect, buf, 0, interp_sect_size);
719 /* Now we need to figure out where the dynamic linker was
720 loaded so that we can load its symbols and place a breakpoint
721 in the dynamic linker itself. */
723 gdb_bfd_ref_ptr tmp_bfd;
726 tmp_bfd = solib_bfd_open (buf);
728 catch (const gdb_exception &ex)
732 if (tmp_bfd == NULL)
734 enable_break_failure_warning ();
735 return 0;
738 dsbt_get_initial_loadmaps ();
739 ldm = info->interp_loadmap;
741 /* Record the relocated start and end address of the dynamic linker
742 text and plt section for dsbt_in_dynsym_resolve_code. */
743 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
744 if (interp_sect)
746 info->interp_text_sect_low = bfd_section_vma (interp_sect);
747 info->interp_text_sect_low
748 += displacement_from_map (ldm, info->interp_text_sect_low);
749 info->interp_text_sect_high
750 = info->interp_text_sect_low + bfd_section_size (interp_sect);
752 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
753 if (interp_sect)
755 info->interp_plt_sect_low = bfd_section_vma (interp_sect);
756 info->interp_plt_sect_low
757 += displacement_from_map (ldm, info->interp_plt_sect_low);
758 info->interp_plt_sect_high
759 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
762 addr = gdb_bfd_lookup_symbol (tmp_bfd.get (), cmp_name,
763 "_dl_debug_state");
764 if (addr != 0)
766 if (solib_dsbt_debug)
767 gdb_printf (gdb_stdlog,
768 "enable_break: _dl_debug_state (prior to relocation) = %s\n",
769 hex_string_custom (addr, 8));
770 addr += displacement_from_map (ldm, addr);
772 if (solib_dsbt_debug)
773 gdb_printf (gdb_stdlog,
774 "enable_break: _dl_debug_state (after relocation) = %s\n",
775 hex_string_custom (addr, 8));
777 /* Now (finally!) create the solib breakpoint. */
778 create_solib_event_breakpoint (target_gdbarch (), addr);
780 ret = 1;
782 else
784 if (solib_dsbt_debug)
785 gdb_printf (gdb_stdlog,
786 "enable_break: _dl_debug_state is not found\n");
787 ret = 0;
790 /* We're done with the loadmap. */
791 xfree (ldm);
793 return ret;
796 /* Tell the user we couldn't set a dynamic linker breakpoint. */
797 enable_break_failure_warning ();
799 /* Failure return. */
800 return 0;
803 static void
804 dsbt_relocate_main_executable (void)
806 struct int_elf32_dsbt_loadmap *ldm;
807 int changed;
808 struct obj_section *osect;
809 struct dsbt_info *info = get_dsbt_info ();
811 dsbt_get_initial_loadmaps ();
812 ldm = info->exec_loadmap;
814 delete info->main_executable_lm_info;
815 info->main_executable_lm_info = new lm_info_dsbt;
816 info->main_executable_lm_info->map = ldm;
818 objfile *objf = current_program_space->symfile_object_file;
819 section_offsets new_offsets (objf->section_offsets.size ());
820 changed = 0;
822 ALL_OBJFILE_OSECTIONS (objf, osect)
824 CORE_ADDR orig_addr, addr, offset;
825 int osect_idx;
826 int seg;
828 osect_idx = osect - objf->sections;
830 /* Current address of section. */
831 addr = osect->addr ();
832 /* Offset from where this section started. */
833 offset = objf->section_offsets[osect_idx];
834 /* Original address prior to any past relocations. */
835 orig_addr = addr - offset;
837 for (seg = 0; seg < ldm->nsegs; seg++)
839 if (ldm->segs[seg].p_vaddr <= orig_addr
840 && orig_addr < ldm->segs[seg].p_vaddr + ldm->segs[seg].p_memsz)
842 new_offsets[osect_idx]
843 = ldm->segs[seg].addr - ldm->segs[seg].p_vaddr;
845 if (new_offsets[osect_idx] != offset)
846 changed = 1;
847 break;
852 if (changed)
853 objfile_relocate (objf, new_offsets);
855 /* Now that OBJF has been relocated, we can compute the GOT value
856 and stash it away. */
859 /* When gdb starts up the inferior, it nurses it along (through the
860 shell) until it is ready to execute it's first instruction. At this
861 point, this function gets called via solib_create_inferior_hook.
863 For the DSBT shared library, the main executable needs to be relocated.
864 The shared library breakpoints also need to be enabled. */
866 static void
867 dsbt_solib_create_inferior_hook (int from_tty)
869 /* Relocate main executable. */
870 dsbt_relocate_main_executable ();
872 /* Enable shared library breakpoints. */
873 if (!enable_break ())
875 warning (_("shared library handler failed to enable breakpoint"));
876 return;
880 static void
881 dsbt_clear_solib (void)
883 struct dsbt_info *info = get_dsbt_info ();
885 info->lm_base_cache = 0;
886 info->main_lm_addr = 0;
888 delete info->main_executable_lm_info;
889 info->main_executable_lm_info = NULL;
892 static void
893 dsbt_free_so (struct so_list *so)
895 lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
897 delete li;
900 static void
901 dsbt_relocate_section_addresses (struct so_list *so,
902 struct target_section *sec)
904 int seg;
905 lm_info_dsbt *li = (lm_info_dsbt *) so->lm_info;
906 int_elf32_dsbt_loadmap *map = li->map;
908 for (seg = 0; seg < map->nsegs; seg++)
910 if (map->segs[seg].p_vaddr <= sec->addr
911 && sec->addr < map->segs[seg].p_vaddr + map->segs[seg].p_memsz)
913 CORE_ADDR displ = map->segs[seg].addr - map->segs[seg].p_vaddr;
915 sec->addr += displ;
916 sec->endaddr += displ;
917 break;
921 static void
922 show_dsbt_debug (struct ui_file *file, int from_tty,
923 struct cmd_list_element *c, const char *value)
925 gdb_printf (file, _("solib-dsbt debugging is %s.\n"), value);
928 const struct target_so_ops dsbt_so_ops =
930 dsbt_relocate_section_addresses,
931 dsbt_free_so,
932 nullptr,
933 dsbt_clear_solib,
934 dsbt_solib_create_inferior_hook,
935 dsbt_current_sos,
936 open_symbol_file_object,
937 dsbt_in_dynsym_resolve_code,
938 solib_bfd_open,
941 void _initialize_dsbt_solib ();
942 void
943 _initialize_dsbt_solib ()
945 /* Debug this file's internals. */
946 add_setshow_zuinteger_cmd ("solib-dsbt", class_maintenance,
947 &solib_dsbt_debug, _("\
948 Set internal debugging of shared library code for DSBT ELF."), _("\
949 Show internal debugging of shared library code for DSBT ELF."), _("\
950 When non-zero, DSBT solib specific internal debugging is enabled."),
951 NULL,
952 show_dsbt_debug,
953 &setdebuglist, &showdebuglist);