Automatic date update in version.in
[binutils-gdb.git] / gdb / solib-svr4.c
bloba6a9ec5c86b3badd204288967608f4a72ed70033
1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
22 #include "elf/external.h"
23 #include "elf/common.h"
24 #include "elf/mips.h"
26 #include "symtab.h"
27 #include "bfd.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "gdbcore.h"
31 #include "target.h"
32 #include "inferior.h"
33 #include "infrun.h"
34 #include "regcache.h"
35 #include "gdbthread.h"
36 #include "observable.h"
38 #include "solist.h"
39 #include "solib.h"
40 #include "solib-svr4.h"
42 #include "bfd-target.h"
43 #include "elf-bfd.h"
44 #include "exec.h"
45 #include "auxv.h"
46 #include "gdb_bfd.h"
47 #include "probe.h"
49 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
50 static int svr4_have_link_map_offsets (void);
51 static void svr4_relocate_main_executable (void);
52 static void svr4_free_library_list (void *p_list);
53 static void probes_table_remove_objfile_probes (struct objfile *objfile);
54 static void svr4_iterate_over_objfiles_in_search_order
55 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
56 objfile *current_objfile);
59 /* On SVR4 systems, a list of symbols in the dynamic linker where
60 GDB can try to place a breakpoint to monitor shared library
61 events.
63 If none of these symbols are found, or other errors occur, then
64 SVR4 systems will fall back to using a symbol as the "startup
65 mapping complete" breakpoint address. */
67 static const char * const solib_break_names[] =
69 "r_debug_state",
70 "_r_debug_state",
71 "_dl_debug_state",
72 "rtld_db_dlactivity",
73 "__dl_rtld_db_dlactivity",
74 "_rtld_debug_state",
76 NULL
79 static const char * const bkpt_names[] =
81 "_start",
82 "__start",
83 "main",
84 NULL
87 static const char * const main_name_list[] =
89 "main_$main",
90 NULL
93 /* What to do when a probe stop occurs. */
95 enum probe_action
97 /* Something went seriously wrong. Stop using probes and
98 revert to using the older interface. */
99 PROBES_INTERFACE_FAILED,
101 /* No action is required. The shared object list is still
102 valid. */
103 DO_NOTHING,
105 /* The shared object list should be reloaded entirely. */
106 FULL_RELOAD,
108 /* Attempt to incrementally update the shared object list. If
109 the update fails or is not possible, fall back to reloading
110 the list in full. */
111 UPDATE_OR_RELOAD,
114 /* A probe's name and its associated action. */
116 struct probe_info
118 /* The name of the probe. */
119 const char *name;
121 /* What to do when a probe stop occurs. */
122 enum probe_action action;
125 /* A list of named probes and their associated actions. If all
126 probes are present in the dynamic linker then the probes-based
127 interface will be used. */
129 static const struct probe_info probe_info[] =
131 { "init_start", DO_NOTHING },
132 { "init_complete", FULL_RELOAD },
133 { "map_start", DO_NOTHING },
134 { "map_failed", DO_NOTHING },
135 { "reloc_complete", UPDATE_OR_RELOAD },
136 { "unmap_start", DO_NOTHING },
137 { "unmap_complete", FULL_RELOAD },
140 #define NUM_PROBES ARRAY_SIZE (probe_info)
142 /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
143 the same shared library. */
145 static int
146 svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
148 if (strcmp (gdb_so_name, inferior_so_name) == 0)
149 return 1;
151 /* On Solaris, when starting inferior we think that dynamic linker is
152 /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
153 contains /lib/ld.so.1. Sometimes one file is a link to another, but
154 sometimes they have identical content, but are not linked to each
155 other. We don't restrict this check for Solaris, but the chances
156 of running into this situation elsewhere are very low. */
157 if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
158 && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
159 return 1;
161 /* Similarly, we observed the same issue with amd64 and sparcv9, but with
162 different locations. */
163 if (strcmp (gdb_so_name, "/usr/lib/amd64/ld.so.1") == 0
164 && strcmp (inferior_so_name, "/lib/amd64/ld.so.1") == 0)
165 return 1;
167 if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
168 && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
169 return 1;
171 return 0;
174 static int
175 svr4_same (struct so_list *gdb, struct so_list *inferior)
177 return (svr4_same_1 (gdb->so_original_name, inferior->so_original_name));
180 static std::unique_ptr<lm_info_svr4>
181 lm_info_read (CORE_ADDR lm_addr)
183 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
184 std::unique_ptr<lm_info_svr4> lm_info;
186 gdb::byte_vector lm (lmo->link_map_size);
188 if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
189 warning (_("Error reading shared library list entry at %s"),
190 paddress (target_gdbarch (), lm_addr));
191 else
193 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
195 lm_info.reset (new lm_info_svr4);
196 lm_info->lm_addr = lm_addr;
198 lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
199 ptr_type);
200 lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
201 lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
202 ptr_type);
203 lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
204 ptr_type);
205 lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
206 ptr_type);
209 return lm_info;
212 static int
213 has_lm_dynamic_from_link_map (void)
215 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
217 return lmo->l_ld_offset >= 0;
220 static CORE_ADDR
221 lm_addr_check (const struct so_list *so, bfd *abfd)
223 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
225 if (!li->l_addr_p)
227 struct bfd_section *dyninfo_sect;
228 CORE_ADDR l_addr, l_dynaddr, dynaddr;
230 l_addr = li->l_addr_inferior;
232 if (! abfd || ! has_lm_dynamic_from_link_map ())
233 goto set_addr;
235 l_dynaddr = li->l_ld;
237 dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
238 if (dyninfo_sect == NULL)
239 goto set_addr;
241 dynaddr = bfd_section_vma (dyninfo_sect);
243 if (dynaddr + l_addr != l_dynaddr)
245 CORE_ADDR align = 0x1000;
246 CORE_ADDR minpagesize = align;
248 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
250 Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
251 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
252 int i;
254 align = 1;
256 for (i = 0; i < ehdr->e_phnum; i++)
257 if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
258 align = phdr[i].p_align;
260 minpagesize = get_elf_backend_data (abfd)->minpagesize;
263 /* Turn it into a mask. */
264 align--;
266 /* If the changes match the alignment requirements, we
267 assume we're using a core file that was generated by the
268 same binary, just prelinked with a different base offset.
269 If it doesn't match, we may have a different binary, the
270 same binary with the dynamic table loaded at an unrelated
271 location, or anything, really. To avoid regressions,
272 don't adjust the base offset in the latter case, although
273 odds are that, if things really changed, debugging won't
274 quite work.
276 One could expect more the condition
277 ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
278 but the one below is relaxed for PPC. The PPC kernel supports
279 either 4k or 64k page sizes. To be prepared for 64k pages,
280 PPC ELF files are built using an alignment requirement of 64k.
281 However, when running on a kernel supporting 4k pages, the memory
282 mapping of the library may not actually happen on a 64k boundary!
284 (In the usual case where (l_addr & align) == 0, this check is
285 equivalent to the possibly expected check above.)
287 Even on PPC it must be zero-aligned at least for MINPAGESIZE. */
289 l_addr = l_dynaddr - dynaddr;
291 if ((l_addr & (minpagesize - 1)) == 0
292 && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
294 if (info_verbose)
295 gdb_printf (_("Using PIC (Position Independent Code) "
296 "prelink displacement %s for \"%s\".\n"),
297 paddress (target_gdbarch (), l_addr),
298 so->so_name);
300 else
302 /* There is no way to verify the library file matches. prelink
303 can during prelinking of an unprelinked file (or unprelinking
304 of a prelinked file) shift the DYNAMIC segment by arbitrary
305 offset without any page size alignment. There is no way to
306 find out the ELF header and/or Program Headers for a limited
307 verification if it they match. One could do a verification
308 of the DYNAMIC segment. Still the found address is the best
309 one GDB could find. */
311 warning (_(".dynamic section for \"%s\" "
312 "is not at the expected address "
313 "(wrong library or version mismatch?)"), so->so_name);
317 set_addr:
318 li->l_addr = l_addr;
319 li->l_addr_p = 1;
322 return li->l_addr;
325 /* Per pspace SVR4 specific data. */
327 struct svr4_info
329 svr4_info () = default;
330 ~svr4_info ();
332 /* Base of dynamic linker structures. */
333 CORE_ADDR debug_base = 0;
335 /* Validity flag for debug_loader_offset. */
336 int debug_loader_offset_p = 0;
338 /* Load address for the dynamic linker, inferred. */
339 CORE_ADDR debug_loader_offset = 0;
341 /* Name of the dynamic linker, valid if debug_loader_offset_p. */
342 char *debug_loader_name = nullptr;
344 /* Load map address for the main executable. */
345 CORE_ADDR main_lm_addr = 0;
347 CORE_ADDR interp_text_sect_low = 0;
348 CORE_ADDR interp_text_sect_high = 0;
349 CORE_ADDR interp_plt_sect_low = 0;
350 CORE_ADDR interp_plt_sect_high = 0;
352 /* Nonzero if the list of objects was last obtained from the target
353 via qXfer:libraries-svr4:read. */
354 int using_xfer = 0;
356 /* Table of struct probe_and_action instances, used by the
357 probes-based interface to map breakpoint addresses to probes
358 and their associated actions. Lookup is performed using
359 probe_and_action->prob->address. */
360 htab_up probes_table;
362 /* List of objects loaded into the inferior, used by the probes-
363 based interface. */
364 struct so_list *solib_list = nullptr;
367 /* Per-program-space data key. */
368 static const registry<program_space>::key<svr4_info> solib_svr4_pspace_data;
370 /* Free the probes table. */
372 static void
373 free_probes_table (struct svr4_info *info)
375 info->probes_table.reset (nullptr);
378 /* Free the solib list. */
380 static void
381 free_solib_list (struct svr4_info *info)
383 svr4_free_library_list (&info->solib_list);
384 info->solib_list = NULL;
387 svr4_info::~svr4_info ()
389 free_solib_list (this);
392 /* Get the svr4 data for program space PSPACE. If none is found yet, add it now.
393 This function always returns a valid object. */
395 static struct svr4_info *
396 get_svr4_info (program_space *pspace)
398 struct svr4_info *info = solib_svr4_pspace_data.get (pspace);
400 if (info == NULL)
401 info = solib_svr4_pspace_data.emplace (pspace);
403 return info;
406 /* Local function prototypes */
408 static int match_main (const char *);
410 /* Read program header TYPE from inferior memory. The header is found
411 by scanning the OS auxiliary vector.
413 If TYPE == -1, return the program headers instead of the contents of
414 one program header.
416 Return vector of bytes holding the program header contents, or an empty
417 optional on failure. If successful and P_ARCH_SIZE is non-NULL, the target
418 architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE. Likewise,
419 the base address of the section is returned in *BASE_ADDR. */
421 static gdb::optional<gdb::byte_vector>
422 read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
424 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
425 CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
426 int arch_size, sect_size;
427 CORE_ADDR sect_addr;
428 int pt_phdr_p = 0;
430 /* Get required auxv elements from target. */
431 if (target_auxv_search (current_inferior ()->top_target (),
432 AT_PHDR, &at_phdr) <= 0)
433 return {};
434 if (target_auxv_search (current_inferior ()->top_target (),
435 AT_PHENT, &at_phent) <= 0)
436 return {};
437 if (target_auxv_search (current_inferior ()->top_target (),
438 AT_PHNUM, &at_phnum) <= 0)
439 return {};
440 if (!at_phdr || !at_phnum)
441 return {};
443 /* Determine ELF architecture type. */
444 if (at_phent == sizeof (Elf32_External_Phdr))
445 arch_size = 32;
446 else if (at_phent == sizeof (Elf64_External_Phdr))
447 arch_size = 64;
448 else
449 return {};
451 /* Find the requested segment. */
452 if (type == -1)
454 sect_addr = at_phdr;
455 sect_size = at_phent * at_phnum;
457 else if (arch_size == 32)
459 Elf32_External_Phdr phdr;
460 int i;
462 /* Search for requested PHDR. */
463 for (i = 0; i < at_phnum; i++)
465 int p_type;
467 if (target_read_memory (at_phdr + i * sizeof (phdr),
468 (gdb_byte *)&phdr, sizeof (phdr)))
469 return {};
471 p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
472 4, byte_order);
474 if (p_type == PT_PHDR)
476 pt_phdr_p = 1;
477 pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
478 4, byte_order);
481 if (p_type == type)
482 break;
485 if (i == at_phnum)
486 return {};
488 /* Retrieve address and size. */
489 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
490 4, byte_order);
491 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
492 4, byte_order);
494 else
496 Elf64_External_Phdr phdr;
497 int i;
499 /* Search for requested PHDR. */
500 for (i = 0; i < at_phnum; i++)
502 int p_type;
504 if (target_read_memory (at_phdr + i * sizeof (phdr),
505 (gdb_byte *)&phdr, sizeof (phdr)))
506 return {};
508 p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
509 4, byte_order);
511 if (p_type == PT_PHDR)
513 pt_phdr_p = 1;
514 pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
515 8, byte_order);
518 if (p_type == type)
519 break;
522 if (i == at_phnum)
523 return {};
525 /* Retrieve address and size. */
526 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
527 8, byte_order);
528 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
529 8, byte_order);
532 /* PT_PHDR is optional, but we really need it
533 for PIE to make this work in general. */
535 if (pt_phdr_p)
537 /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
538 Relocation offset is the difference between the two. */
539 sect_addr = sect_addr + (at_phdr - pt_phdr);
542 /* Read in requested program header. */
543 gdb::byte_vector buf (sect_size);
544 if (target_read_memory (sect_addr, buf.data (), sect_size))
545 return {};
547 if (p_arch_size)
548 *p_arch_size = arch_size;
549 if (base_addr)
550 *base_addr = sect_addr;
552 return buf;
556 /* Return program interpreter string. */
557 static gdb::optional<gdb::byte_vector>
558 find_program_interpreter (void)
560 /* If we have a current exec_bfd, use its section table. */
561 if (current_program_space->exec_bfd ()
562 && (bfd_get_flavour (current_program_space->exec_bfd ())
563 == bfd_target_elf_flavour))
565 struct bfd_section *interp_sect;
567 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
568 ".interp");
569 if (interp_sect != NULL)
571 int sect_size = bfd_section_size (interp_sect);
573 gdb::byte_vector buf (sect_size);
574 bfd_get_section_contents (current_program_space->exec_bfd (),
575 interp_sect, buf.data (), 0, sect_size);
576 return buf;
580 /* If we didn't find it, use the target auxiliary vector. */
581 return read_program_header (PT_INTERP, NULL, NULL);
585 /* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
586 found by consulting the OS auxillary vector. If DESIRED_DYNTAG is found, 1
587 is returned and the corresponding PTR is set. */
589 static int
590 scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr,
591 CORE_ADDR *ptr_addr)
593 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
594 int arch_size, step;
595 long current_dyntag;
596 CORE_ADDR dyn_ptr;
597 CORE_ADDR base_addr;
599 /* Read in .dynamic section. */
600 gdb::optional<gdb::byte_vector> ph_data
601 = read_program_header (PT_DYNAMIC, &arch_size, &base_addr);
602 if (!ph_data)
603 return 0;
605 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
606 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
607 : sizeof (Elf64_External_Dyn);
608 for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size ();
609 buf < bufend; buf += step)
611 if (arch_size == 32)
613 Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
615 current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
616 4, byte_order);
617 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
618 4, byte_order);
620 else
622 Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
624 current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
625 8, byte_order);
626 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
627 8, byte_order);
629 if (current_dyntag == DT_NULL)
630 break;
632 if (current_dyntag == desired_dyntag)
634 if (ptr)
635 *ptr = dyn_ptr;
637 if (ptr_addr)
638 *ptr_addr = base_addr + buf - ph_data->data ();
640 return 1;
644 return 0;
647 /* Locate the base address of dynamic linker structs for SVR4 elf
648 targets.
650 For SVR4 elf targets the address of the dynamic linker's runtime
651 structure is contained within the dynamic info section in the
652 executable file. The dynamic section is also mapped into the
653 inferior address space. Because the runtime loader fills in the
654 real address before starting the inferior, we have to read in the
655 dynamic info section from the inferior address space.
656 If there are any errors while trying to find the address, we
657 silently return 0, otherwise the found address is returned. */
659 static CORE_ADDR
660 elf_locate_base (void)
662 struct bound_minimal_symbol msymbol;
663 CORE_ADDR dyn_ptr, dyn_ptr_addr;
665 /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
666 instead of DT_DEBUG, although they sometimes contain an unused
667 DT_DEBUG. */
668 if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP,
669 current_program_space->exec_bfd (),
670 &dyn_ptr, NULL)
671 || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
673 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
674 gdb_byte *pbuf;
675 int pbuf_size = ptr_type->length ();
677 pbuf = (gdb_byte *) alloca (pbuf_size);
678 /* DT_MIPS_RLD_MAP contains a pointer to the address
679 of the dynamic link structure. */
680 if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
681 return 0;
682 return extract_typed_address (pbuf, ptr_type);
685 /* Then check DT_MIPS_RLD_MAP_REL. MIPS executables now use this form
686 because of needing to support PIE. DT_MIPS_RLD_MAP will also exist
687 in non-PIE. */
688 if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP_REL,
689 current_program_space->exec_bfd (),
690 &dyn_ptr, &dyn_ptr_addr)
691 || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
693 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
694 gdb_byte *pbuf;
695 int pbuf_size = ptr_type->length ();
697 pbuf = (gdb_byte *) alloca (pbuf_size);
698 /* DT_MIPS_RLD_MAP_REL contains an offset from the address of the
699 DT slot to the address of the dynamic link structure. */
700 if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size))
701 return 0;
702 return extract_typed_address (pbuf, ptr_type);
705 /* Find DT_DEBUG. */
706 if (gdb_bfd_scan_elf_dyntag (DT_DEBUG, current_program_space->exec_bfd (),
707 &dyn_ptr, NULL)
708 || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
709 return dyn_ptr;
711 /* This may be a static executable. Look for the symbol
712 conventionally named _r_debug, as a last resort. */
713 msymbol = lookup_minimal_symbol ("_r_debug", NULL,
714 current_program_space->symfile_object_file);
715 if (msymbol.minsym != NULL)
716 return msymbol.value_address ();
718 /* DT_DEBUG entry not found. */
719 return 0;
722 /* Locate the base address of dynamic linker structs.
724 For both the SunOS and SVR4 shared library implementations, if the
725 inferior executable has been linked dynamically, there is a single
726 address somewhere in the inferior's data space which is the key to
727 locating all of the dynamic linker's runtime structures. This
728 address is the value of the debug base symbol. The job of this
729 function is to find and return that address, or to return 0 if there
730 is no such address (the executable is statically linked for example).
732 For SunOS, the job is almost trivial, since the dynamic linker and
733 all of it's structures are statically linked to the executable at
734 link time. Thus the symbol for the address we are looking for has
735 already been added to the minimal symbol table for the executable's
736 objfile at the time the symbol file's symbols were read, and all we
737 have to do is look it up there. Note that we explicitly do NOT want
738 to find the copies in the shared library.
740 The SVR4 version is a bit more complicated because the address
741 is contained somewhere in the dynamic info section. We have to go
742 to a lot more work to discover the address of the debug base symbol.
743 Because of this complexity, we cache the value we find and return that
744 value on subsequent invocations. Note there is no copy in the
745 executable symbol tables. */
747 static CORE_ADDR
748 locate_base (struct svr4_info *info)
750 /* Check to see if we have a currently valid address, and if so, avoid
751 doing all this work again and just return the cached address. If
752 we have no cached address, try to locate it in the dynamic info
753 section for ELF executables. There's no point in doing any of this
754 though if we don't have some link map offsets to work with. */
756 if (info->debug_base == 0 && svr4_have_link_map_offsets ())
757 info->debug_base = elf_locate_base ();
758 return info->debug_base;
761 /* Find the first element in the inferior's dynamic link map, and
762 return its address in the inferior. Return zero if the address
763 could not be determined.
765 FIXME: Perhaps we should validate the info somehow, perhaps by
766 checking r_version for a known version number, or r_state for
767 RT_CONSISTENT. */
769 static CORE_ADDR
770 solib_svr4_r_map (struct svr4_info *info)
772 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
773 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
774 CORE_ADDR addr = 0;
778 addr = read_memory_typed_address (info->debug_base + lmo->r_map_offset,
779 ptr_type);
781 catch (const gdb_exception_error &ex)
783 exception_print (gdb_stderr, ex);
786 return addr;
789 /* Find r_brk from the inferior's debug base. */
791 static CORE_ADDR
792 solib_svr4_r_brk (struct svr4_info *info)
794 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
795 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
797 return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
798 ptr_type);
801 /* Find the link map for the dynamic linker (if it is not in the
802 normal list of loaded shared objects). */
804 static CORE_ADDR
805 solib_svr4_r_ldsomap (struct svr4_info *info)
807 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
808 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
809 enum bfd_endian byte_order = type_byte_order (ptr_type);
810 ULONGEST version = 0;
814 /* Check version, and return zero if `struct r_debug' doesn't have
815 the r_ldsomap member. */
816 version
817 = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
818 lmo->r_version_size, byte_order);
820 catch (const gdb_exception_error &ex)
822 exception_print (gdb_stderr, ex);
825 if (version < 2 || lmo->r_ldsomap_offset == -1)
826 return 0;
828 return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
829 ptr_type);
832 /* On Solaris systems with some versions of the dynamic linker,
833 ld.so's l_name pointer points to the SONAME in the string table
834 rather than into writable memory. So that GDB can find shared
835 libraries when loading a core file generated by gcore, ensure that
836 memory areas containing the l_name string are saved in the core
837 file. */
839 static int
840 svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
842 struct svr4_info *info;
843 CORE_ADDR ldsomap;
844 CORE_ADDR name_lm;
846 info = get_svr4_info (current_program_space);
848 info->debug_base = 0;
849 locate_base (info);
850 if (!info->debug_base)
851 return 0;
853 ldsomap = solib_svr4_r_ldsomap (info);
854 if (!ldsomap)
855 return 0;
857 std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
858 name_lm = li != NULL ? li->l_name : 0;
860 return (name_lm >= vaddr && name_lm < vaddr + size);
863 /* See solist.h. */
865 static int
866 open_symbol_file_object (int from_tty)
868 CORE_ADDR lm, l_name;
869 struct link_map_offsets *lmo = svr4_fetch_link_map_offsets ();
870 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
871 int l_name_size = ptr_type->length ();
872 gdb::byte_vector l_name_buf (l_name_size);
873 struct svr4_info *info = get_svr4_info (current_program_space);
874 symfile_add_flags add_flags = 0;
876 if (from_tty)
877 add_flags |= SYMFILE_VERBOSE;
879 if (current_program_space->symfile_object_file)
880 if (!query (_("Attempt to reload symbols from process? ")))
881 return 0;
883 /* Always locate the debug struct, in case it has moved. */
884 info->debug_base = 0;
885 if (locate_base (info) == 0)
886 return 0; /* failed somehow... */
888 /* First link map member should be the executable. */
889 lm = solib_svr4_r_map (info);
890 if (lm == 0)
891 return 0; /* failed somehow... */
893 /* Read address of name from target memory to GDB. */
894 read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
896 /* Convert the address to host format. */
897 l_name = extract_typed_address (l_name_buf.data (), ptr_type);
899 if (l_name == 0)
900 return 0; /* No filename. */
902 /* Now fetch the filename from target memory. */
903 gdb::unique_xmalloc_ptr<char> filename
904 = target_read_string (l_name, SO_NAME_MAX_PATH_SIZE - 1);
906 if (filename == nullptr)
908 warning (_("failed to read exec filename from attached file"));
909 return 0;
912 /* Have a pathname: read the symbol file. */
913 symbol_file_add_main (filename.get (), add_flags);
915 return 1;
918 /* Data exchange structure for the XML parser as returned by
919 svr4_current_sos_via_xfer_libraries. */
921 struct svr4_library_list
923 struct so_list *head, **tailp;
925 /* Inferior address of struct link_map used for the main executable. It is
926 NULL if not known. */
927 CORE_ADDR main_lm;
930 /* This module's 'free_objfile' observer. */
932 static void
933 svr4_free_objfile_observer (struct objfile *objfile)
935 probes_table_remove_objfile_probes (objfile);
938 /* Implementation for target_so_ops.free_so. */
940 static void
941 svr4_free_so (struct so_list *so)
943 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
945 delete li;
948 /* Implement target_so_ops.clear_so. */
950 static void
951 svr4_clear_so (struct so_list *so)
953 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
955 if (li != NULL)
956 li->l_addr_p = 0;
959 /* Free so_list built so far (called via cleanup). */
961 static void
962 svr4_free_library_list (void *p_list)
964 struct so_list *list = *(struct so_list **) p_list;
966 while (list != NULL)
968 struct so_list *next = list->next;
970 free_so (list);
971 list = next;
975 /* Copy library list. */
977 static struct so_list *
978 svr4_copy_library_list (struct so_list *src)
980 struct so_list *dst = NULL;
981 struct so_list **link = &dst;
983 while (src != NULL)
985 struct so_list *newobj;
987 newobj = XNEW (struct so_list);
988 memcpy (newobj, src, sizeof (struct so_list));
990 lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
991 newobj->lm_info = new lm_info_svr4 (*src_li);
993 newobj->next = NULL;
994 *link = newobj;
995 link = &newobj->next;
997 src = src->next;
1000 return dst;
1003 #ifdef HAVE_LIBEXPAT
1005 #include "xml-support.h"
1007 /* Handle the start of a <library> element. Note: new elements are added
1008 at the tail of the list, keeping the list in order. */
1010 static void
1011 library_list_start_library (struct gdb_xml_parser *parser,
1012 const struct gdb_xml_element *element,
1013 void *user_data,
1014 std::vector<gdb_xml_value> &attributes)
1016 struct svr4_library_list *list = (struct svr4_library_list *) user_data;
1017 const char *name
1018 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
1019 ULONGEST *lmp
1020 = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get ();
1021 ULONGEST *l_addrp
1022 = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
1023 ULONGEST *l_ldp
1024 = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
1025 struct so_list *new_elem;
1027 new_elem = XCNEW (struct so_list);
1028 lm_info_svr4 *li = new lm_info_svr4;
1029 new_elem->lm_info = li;
1030 li->lm_addr = *lmp;
1031 li->l_addr_inferior = *l_addrp;
1032 li->l_ld = *l_ldp;
1034 strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
1035 new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
1036 strcpy (new_elem->so_original_name, new_elem->so_name);
1038 *list->tailp = new_elem;
1039 list->tailp = &new_elem->next;
1042 /* Handle the start of a <library-list-svr4> element. */
1044 static void
1045 svr4_library_list_start_list (struct gdb_xml_parser *parser,
1046 const struct gdb_xml_element *element,
1047 void *user_data,
1048 std::vector<gdb_xml_value> &attributes)
1050 struct svr4_library_list *list = (struct svr4_library_list *) user_data;
1051 const char *version
1052 = (const char *) xml_find_attribute (attributes, "version")->value.get ();
1053 struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
1055 if (strcmp (version, "1.0") != 0)
1056 gdb_xml_error (parser,
1057 _("SVR4 Library list has unsupported version \"%s\""),
1058 version);
1060 if (main_lm)
1061 list->main_lm = *(ULONGEST *) main_lm->value.get ();
1064 /* The allowed elements and attributes for an XML library list.
1065 The root element is a <library-list>. */
1067 static const struct gdb_xml_attribute svr4_library_attributes[] =
1069 { "name", GDB_XML_AF_NONE, NULL, NULL },
1070 { "lm", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1071 { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1072 { "l_ld", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1073 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1076 static const struct gdb_xml_element svr4_library_list_children[] =
1079 "library", svr4_library_attributes, NULL,
1080 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
1081 library_list_start_library, NULL
1083 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1086 static const struct gdb_xml_attribute svr4_library_list_attributes[] =
1088 { "version", GDB_XML_AF_NONE, NULL, NULL },
1089 { "main-lm", GDB_XML_AF_OPTIONAL, gdb_xml_parse_attr_ulongest, NULL },
1090 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1093 static const struct gdb_xml_element svr4_library_list_elements[] =
1095 { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
1096 GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
1097 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1100 /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN. Return 1 if
1102 Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
1103 case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
1104 empty, caller is responsible for freeing all its entries. */
1106 static int
1107 svr4_parse_libraries (const char *document, struct svr4_library_list *list)
1109 auto cleanup = make_scope_exit ([&] ()
1111 svr4_free_library_list (&list->head);
1114 memset (list, 0, sizeof (*list));
1115 list->tailp = &list->head;
1116 if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
1117 svr4_library_list_elements, document, list) == 0)
1119 /* Parsed successfully, keep the result. */
1120 cleanup.release ();
1121 return 1;
1124 return 0;
1127 /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
1129 Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
1130 case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
1131 empty, caller is responsible for freeing all its entries.
1133 Note that ANNEX must be NULL if the remote does not explicitly allow
1134 qXfer:libraries-svr4:read packets with non-empty annexes. Support for
1135 this can be checked using target_augmented_libraries_svr4_read (). */
1137 static int
1138 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
1139 const char *annex)
1141 gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
1143 /* Fetch the list of shared libraries. */
1144 gdb::optional<gdb::char_vector> svr4_library_document
1145 = target_read_stralloc (current_inferior ()->top_target (),
1146 TARGET_OBJECT_LIBRARIES_SVR4,
1147 annex);
1148 if (!svr4_library_document)
1149 return 0;
1151 return svr4_parse_libraries (svr4_library_document->data (), list);
1154 #else
1156 static int
1157 svr4_current_sos_via_xfer_libraries (struct svr4_library_list *list,
1158 const char *annex)
1160 return 0;
1163 #endif
1165 /* If no shared library information is available from the dynamic
1166 linker, build a fallback list from other sources. */
1168 static struct so_list *
1169 svr4_default_sos (svr4_info *info)
1171 struct so_list *newobj;
1173 if (!info->debug_loader_offset_p)
1174 return NULL;
1176 newobj = XCNEW (struct so_list);
1177 lm_info_svr4 *li = new lm_info_svr4;
1178 newobj->lm_info = li;
1180 /* Nothing will ever check the other fields if we set l_addr_p. */
1181 li->l_addr = info->debug_loader_offset;
1182 li->l_addr_p = 1;
1184 strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
1185 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1186 strcpy (newobj->so_original_name, newobj->so_name);
1188 return newobj;
1191 /* Read the whole inferior libraries chain starting at address LM.
1192 Expect the first entry in the chain's previous entry to be PREV_LM.
1193 Add the entries to the tail referenced by LINK_PTR_PTR. Ignore the
1194 first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
1195 to it. Returns nonzero upon success. If zero is returned the
1196 entries stored to LINK_PTR_PTR are still valid although they may
1197 represent only part of the inferior library list. */
1199 static int
1200 svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
1201 struct so_list ***link_ptr_ptr, int ignore_first)
1203 CORE_ADDR first_l_name = 0;
1204 CORE_ADDR next_lm;
1206 for (; lm != 0; prev_lm = lm, lm = next_lm)
1208 so_list_up newobj (XCNEW (struct so_list));
1210 lm_info_svr4 *li = lm_info_read (lm).release ();
1211 newobj->lm_info = li;
1212 if (li == NULL)
1213 return 0;
1215 next_lm = li->l_next;
1217 if (li->l_prev != prev_lm)
1219 warning (_("Corrupted shared library list: %s != %s"),
1220 paddress (target_gdbarch (), prev_lm),
1221 paddress (target_gdbarch (), li->l_prev));
1222 return 0;
1225 /* For SVR4 versions, the first entry in the link map is for the
1226 inferior executable, so we must ignore it. For some versions of
1227 SVR4, it has no name. For others (Solaris 2.3 for example), it
1228 does have a name, so we can no longer use a missing name to
1229 decide when to ignore it. */
1230 if (ignore_first && li->l_prev == 0)
1232 first_l_name = li->l_name;
1233 info->main_lm_addr = li->lm_addr;
1234 continue;
1237 /* Extract this shared object's name. */
1238 gdb::unique_xmalloc_ptr<char> buffer
1239 = target_read_string (li->l_name, SO_NAME_MAX_PATH_SIZE - 1);
1240 if (buffer == nullptr)
1242 /* If this entry's l_name address matches that of the
1243 inferior executable, then this is not a normal shared
1244 object, but (most likely) a vDSO. In this case, silently
1245 skip it; otherwise emit a warning. */
1246 if (first_l_name == 0 || li->l_name != first_l_name)
1247 warning (_("Can't read pathname for load map."));
1248 continue;
1251 strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
1252 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1253 strcpy (newobj->so_original_name, newobj->so_name);
1255 /* If this entry has no name, or its name matches the name
1256 for the main executable, don't include it in the list. */
1257 if (! newobj->so_name[0] || match_main (newobj->so_name))
1258 continue;
1260 newobj->next = 0;
1261 /* Don't free it now. */
1262 **link_ptr_ptr = newobj.release ();
1263 *link_ptr_ptr = &(**link_ptr_ptr)->next;
1266 return 1;
1269 /* Read the full list of currently loaded shared objects directly
1270 from the inferior, without referring to any libraries read and
1271 stored by the probes interface. Handle special cases relating
1272 to the first elements of the list. */
1274 static struct so_list *
1275 svr4_current_sos_direct (struct svr4_info *info)
1277 CORE_ADDR lm;
1278 struct so_list *head = NULL;
1279 struct so_list **link_ptr = &head;
1280 int ignore_first;
1281 struct svr4_library_list library_list;
1283 /* Fall back to manual examination of the target if the packet is not
1284 supported or gdbserver failed to find DT_DEBUG. gdb.server/solib-list.exp
1285 tests a case where gdbserver cannot find the shared libraries list while
1286 GDB itself is able to find it via SYMFILE_OBJFILE.
1288 Unfortunately statically linked inferiors will also fall back through this
1289 suboptimal code path. */
1291 info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
1292 NULL);
1293 if (info->using_xfer)
1295 if (library_list.main_lm)
1296 info->main_lm_addr = library_list.main_lm;
1298 return library_list.head ? library_list.head : svr4_default_sos (info);
1301 /* Always locate the debug struct, in case it has moved. */
1302 info->debug_base = 0;
1303 locate_base (info);
1305 /* If we can't find the dynamic linker's base structure, this
1306 must not be a dynamically linked executable. Hmm. */
1307 if (! info->debug_base)
1308 return svr4_default_sos (info);
1310 /* Assume that everything is a library if the dynamic loader was loaded
1311 late by a static executable. */
1312 if (current_program_space->exec_bfd ()
1313 && bfd_get_section_by_name (current_program_space->exec_bfd (),
1314 ".dynamic") == NULL)
1315 ignore_first = 0;
1316 else
1317 ignore_first = 1;
1319 auto cleanup = make_scope_exit ([&] ()
1321 svr4_free_library_list (&head);
1324 /* Walk the inferior's link map list, and build our list of
1325 `struct so_list' nodes. */
1326 lm = solib_svr4_r_map (info);
1327 if (lm)
1328 svr4_read_so_list (info, lm, 0, &link_ptr, ignore_first);
1330 /* On Solaris, the dynamic linker is not in the normal list of
1331 shared objects, so make sure we pick it up too. Having
1332 symbol information for the dynamic linker is quite crucial
1333 for skipping dynamic linker resolver code. */
1334 lm = solib_svr4_r_ldsomap (info);
1335 if (lm)
1336 svr4_read_so_list (info, lm, 0, &link_ptr, 0);
1338 cleanup.release ();
1340 if (head == NULL)
1341 return svr4_default_sos (info);
1343 return head;
1346 /* Implement the main part of the "current_sos" target_so_ops
1347 method. */
1349 static struct so_list *
1350 svr4_current_sos_1 (svr4_info *info)
1352 /* If the solib list has been read and stored by the probes
1353 interface then we return a copy of the stored list. */
1354 if (info->solib_list != NULL)
1355 return svr4_copy_library_list (info->solib_list);
1357 /* Otherwise obtain the solib list directly from the inferior. */
1358 return svr4_current_sos_direct (info);
1361 /* Implement the "current_sos" target_so_ops method. */
1363 static struct so_list *
1364 svr4_current_sos (void)
1366 svr4_info *info = get_svr4_info (current_program_space);
1367 struct so_list *so_head = svr4_current_sos_1 (info);
1368 struct mem_range vsyscall_range;
1370 /* Filter out the vDSO module, if present. Its symbol file would
1371 not be found on disk. The vDSO/vsyscall's OBJFILE is instead
1372 managed by symfile-mem.c:add_vsyscall_page. */
1373 if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range)
1374 && vsyscall_range.length != 0)
1376 struct so_list **sop;
1378 sop = &so_head;
1379 while (*sop != NULL)
1381 struct so_list *so = *sop;
1383 /* We can't simply match the vDSO by starting address alone,
1384 because lm_info->l_addr_inferior (and also l_addr) do not
1385 necessarily represent the real starting address of the
1386 ELF if the vDSO's ELF itself is "prelinked". The l_ld
1387 field (the ".dynamic" section of the shared object)
1388 always points at the absolute/resolved address though.
1389 So check whether that address is inside the vDSO's
1390 mapping instead.
1392 E.g., on Linux 3.16 (x86_64) the vDSO is a regular
1393 0-based ELF, and we see:
1395 (gdb) info auxv
1396 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffb000
1397 (gdb) p/x *_r_debug.r_map.l_next
1398 $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...}
1400 And on Linux 2.6.32 (x86_64) we see:
1402 (gdb) info auxv
1403 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffe000
1404 (gdb) p/x *_r_debug.r_map.l_next
1405 $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... }
1407 Dumping that vDSO shows:
1409 (gdb) info proc mappings
1410 0x7ffff7ffe000 0x7ffff7fff000 0x1000 0 [vdso]
1411 (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
1412 # readelf -Wa vdso.bin
1413 [...]
1414 Entry point address: 0xffffffffff700700
1415 [...]
1416 Section Headers:
1417 [Nr] Name Type Address Off Size
1418 [ 0] NULL 0000000000000000 000000 000000
1419 [ 1] .hash HASH ffffffffff700120 000120 000038
1420 [ 2] .dynsym DYNSYM ffffffffff700158 000158 0000d8
1421 [...]
1422 [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
1425 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1427 if (address_in_mem_range (li->l_ld, &vsyscall_range))
1429 *sop = so->next;
1430 free_so (so);
1431 break;
1434 sop = &so->next;
1438 return so_head;
1441 /* Get the address of the link_map for a given OBJFILE. */
1443 CORE_ADDR
1444 svr4_fetch_objfile_link_map (struct objfile *objfile)
1446 struct svr4_info *info = get_svr4_info (objfile->pspace);
1448 /* Cause svr4_current_sos() to be run if it hasn't been already. */
1449 if (info->main_lm_addr == 0)
1450 solib_add (NULL, 0, auto_solib_add);
1452 /* svr4_current_sos() will set main_lm_addr for the main executable. */
1453 if (objfile == current_program_space->symfile_object_file)
1454 return info->main_lm_addr;
1456 /* The other link map addresses may be found by examining the list
1457 of shared libraries. */
1458 for (struct so_list *so : current_program_space->solibs ())
1459 if (so->objfile == objfile)
1461 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1463 return li->lm_addr;
1466 /* Not found! */
1467 return 0;
1470 /* On some systems, the only way to recognize the link map entry for
1471 the main executable file is by looking at its name. Return
1472 non-zero iff SONAME matches one of the known main executable names. */
1474 static int
1475 match_main (const char *soname)
1477 const char * const *mainp;
1479 for (mainp = main_name_list; *mainp != NULL; mainp++)
1481 if (strcmp (soname, *mainp) == 0)
1482 return (1);
1485 return (0);
1488 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1489 SVR4 run time loader. */
1492 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
1494 struct svr4_info *info = get_svr4_info (current_program_space);
1496 return ((pc >= info->interp_text_sect_low
1497 && pc < info->interp_text_sect_high)
1498 || (pc >= info->interp_plt_sect_low
1499 && pc < info->interp_plt_sect_high)
1500 || in_plt_section (pc)
1501 || in_gnu_ifunc_stub (pc));
1504 /* Given an executable's ABFD and target, compute the entry-point
1505 address. */
1507 static CORE_ADDR
1508 exec_entry_point (struct bfd *abfd, struct target_ops *targ)
1510 CORE_ADDR addr;
1512 /* KevinB wrote ... for most targets, the address returned by
1513 bfd_get_start_address() is the entry point for the start
1514 function. But, for some targets, bfd_get_start_address() returns
1515 the address of a function descriptor from which the entry point
1516 address may be extracted. This address is extracted by
1517 gdbarch_convert_from_func_ptr_addr(). The method
1518 gdbarch_convert_from_func_ptr_addr() is the merely the identify
1519 function for targets which don't use function descriptors. */
1520 addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
1521 bfd_get_start_address (abfd),
1522 targ);
1523 return gdbarch_addr_bits_remove (target_gdbarch (), addr);
1526 /* A probe and its associated action. */
1528 struct probe_and_action
1530 /* The probe. */
1531 probe *prob;
1533 /* The relocated address of the probe. */
1534 CORE_ADDR address;
1536 /* The action. */
1537 enum probe_action action;
1539 /* The objfile where this probe was found. */
1540 struct objfile *objfile;
1543 /* Returns a hash code for the probe_and_action referenced by p. */
1545 static hashval_t
1546 hash_probe_and_action (const void *p)
1548 const struct probe_and_action *pa = (const struct probe_and_action *) p;
1550 return (hashval_t) pa->address;
1553 /* Returns non-zero if the probe_and_actions referenced by p1 and p2
1554 are equal. */
1556 static int
1557 equal_probe_and_action (const void *p1, const void *p2)
1559 const struct probe_and_action *pa1 = (const struct probe_and_action *) p1;
1560 const struct probe_and_action *pa2 = (const struct probe_and_action *) p2;
1562 return pa1->address == pa2->address;
1565 /* Traversal function for probes_table_remove_objfile_probes. */
1567 static int
1568 probes_table_htab_remove_objfile_probes (void **slot, void *info)
1570 probe_and_action *pa = (probe_and_action *) *slot;
1571 struct objfile *objfile = (struct objfile *) info;
1573 if (pa->objfile == objfile)
1574 htab_clear_slot (get_svr4_info (objfile->pspace)->probes_table.get (),
1575 slot);
1577 return 1;
1580 /* Remove all probes that belong to OBJFILE from the probes table. */
1582 static void
1583 probes_table_remove_objfile_probes (struct objfile *objfile)
1585 svr4_info *info = get_svr4_info (objfile->pspace);
1586 if (info->probes_table != nullptr)
1587 htab_traverse_noresize (info->probes_table.get (),
1588 probes_table_htab_remove_objfile_probes, objfile);
1591 /* Register a solib event probe and its associated action in the
1592 probes table. */
1594 static void
1595 register_solib_event_probe (svr4_info *info, struct objfile *objfile,
1596 probe *prob, CORE_ADDR address,
1597 enum probe_action action)
1599 struct probe_and_action lookup, *pa;
1600 void **slot;
1602 /* Create the probes table, if necessary. */
1603 if (info->probes_table == NULL)
1604 info->probes_table.reset (htab_create_alloc (1, hash_probe_and_action,
1605 equal_probe_and_action,
1606 xfree, xcalloc, xfree));
1608 lookup.address = address;
1609 slot = htab_find_slot (info->probes_table.get (), &lookup, INSERT);
1610 gdb_assert (*slot == HTAB_EMPTY_ENTRY);
1612 pa = XCNEW (struct probe_and_action);
1613 pa->prob = prob;
1614 pa->address = address;
1615 pa->action = action;
1616 pa->objfile = objfile;
1618 *slot = pa;
1621 /* Get the solib event probe at the specified location, and the
1622 action associated with it. Returns NULL if no solib event probe
1623 was found. */
1625 static struct probe_and_action *
1626 solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
1628 struct probe_and_action lookup;
1629 void **slot;
1631 lookup.address = address;
1632 slot = htab_find_slot (info->probes_table.get (), &lookup, NO_INSERT);
1634 if (slot == NULL)
1635 return NULL;
1637 return (struct probe_and_action *) *slot;
1640 /* Decide what action to take when the specified solib event probe is
1641 hit. */
1643 static enum probe_action
1644 solib_event_probe_action (struct probe_and_action *pa)
1646 enum probe_action action;
1647 unsigned probe_argc = 0;
1648 struct frame_info *frame = get_current_frame ();
1650 action = pa->action;
1651 if (action == DO_NOTHING || action == PROBES_INTERFACE_FAILED)
1652 return action;
1654 gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
1656 /* Check that an appropriate number of arguments has been supplied.
1657 We expect:
1658 arg0: Lmid_t lmid (mandatory)
1659 arg1: struct r_debug *debug_base (mandatory)
1660 arg2: struct link_map *new (optional, for incremental updates) */
1663 probe_argc = pa->prob->get_argument_count (get_frame_arch (frame));
1665 catch (const gdb_exception_error &ex)
1667 exception_print (gdb_stderr, ex);
1668 probe_argc = 0;
1671 /* If get_argument_count throws an exception, probe_argc will be set
1672 to zero. However, if pa->prob does not have arguments, then
1673 get_argument_count will succeed but probe_argc will also be zero.
1674 Both cases happen because of different things, but they are
1675 treated equally here: action will be set to
1676 PROBES_INTERFACE_FAILED. */
1677 if (probe_argc == 2)
1678 action = FULL_RELOAD;
1679 else if (probe_argc < 2)
1680 action = PROBES_INTERFACE_FAILED;
1682 return action;
1685 /* Populate the shared object list by reading the entire list of
1686 shared objects from the inferior. Handle special cases relating
1687 to the first elements of the list. Returns nonzero on success. */
1689 static int
1690 solist_update_full (struct svr4_info *info)
1692 free_solib_list (info);
1693 info->solib_list = svr4_current_sos_direct (info);
1695 return 1;
1698 /* Update the shared object list starting from the link-map entry
1699 passed by the linker in the probe's third argument. Returns
1700 nonzero if the list was successfully updated, or zero to indicate
1701 failure. */
1703 static int
1704 solist_update_incremental (struct svr4_info *info, CORE_ADDR lm)
1706 struct so_list *tail;
1707 CORE_ADDR prev_lm;
1709 /* svr4_current_sos_direct contains logic to handle a number of
1710 special cases relating to the first elements of the list. To
1711 avoid duplicating this logic we defer to solist_update_full
1712 if the list is empty. */
1713 if (info->solib_list == NULL)
1714 return 0;
1716 /* Fall back to a full update if we are using a remote target
1717 that does not support incremental transfers. */
1718 if (info->using_xfer && !target_augmented_libraries_svr4_read ())
1719 return 0;
1721 /* Walk to the end of the list. */
1722 for (tail = info->solib_list; tail->next != NULL; tail = tail->next)
1723 /* Nothing. */;
1725 lm_info_svr4 *li = (lm_info_svr4 *) tail->lm_info;
1726 prev_lm = li->lm_addr;
1728 /* Read the new objects. */
1729 if (info->using_xfer)
1731 struct svr4_library_list library_list;
1732 char annex[64];
1734 xsnprintf (annex, sizeof (annex), "start=%s;prev=%s",
1735 phex_nz (lm, sizeof (lm)),
1736 phex_nz (prev_lm, sizeof (prev_lm)));
1737 if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
1738 return 0;
1740 tail->next = library_list.head;
1742 else
1744 struct so_list **link = &tail->next;
1746 /* IGNORE_FIRST may safely be set to zero here because the
1747 above check and deferral to solist_update_full ensures
1748 that this call to svr4_read_so_list will never see the
1749 first element. */
1750 if (!svr4_read_so_list (info, lm, prev_lm, &link, 0))
1751 return 0;
1754 return 1;
1757 /* Disable the probes-based linker interface and revert to the
1758 original interface. We don't reset the breakpoints as the
1759 ones set up for the probes-based interface are adequate. */
1761 static void
1762 disable_probes_interface (svr4_info *info)
1764 warning (_("Probes-based dynamic linker interface failed.\n"
1765 "Reverting to original interface."));
1767 free_probes_table (info);
1768 free_solib_list (info);
1771 /* Update the solib list as appropriate when using the
1772 probes-based linker interface. Do nothing if using the
1773 standard interface. */
1775 static void
1776 svr4_handle_solib_event (void)
1778 struct svr4_info *info = get_svr4_info (current_program_space);
1779 struct probe_and_action *pa;
1780 enum probe_action action;
1781 struct value *val = NULL;
1782 CORE_ADDR pc, debug_base, lm = 0;
1783 struct frame_info *frame = get_current_frame ();
1785 /* Do nothing if not using the probes interface. */
1786 if (info->probes_table == NULL)
1787 return;
1789 /* If anything goes wrong we revert to the original linker
1790 interface. */
1791 auto cleanup = make_scope_exit ([info] ()
1793 disable_probes_interface (info);
1796 pc = regcache_read_pc (get_current_regcache ());
1797 pa = solib_event_probe_at (info, pc);
1798 if (pa == NULL)
1799 return;
1801 action = solib_event_probe_action (pa);
1802 if (action == PROBES_INTERFACE_FAILED)
1803 return;
1805 if (action == DO_NOTHING)
1807 cleanup.release ();
1808 return;
1811 /* evaluate_argument looks up symbols in the dynamic linker
1812 using find_pc_section. find_pc_section is accelerated by a cache
1813 called the section map. The section map is invalidated every
1814 time a shared library is loaded or unloaded, and if the inferior
1815 is generating a lot of shared library events then the section map
1816 will be updated every time svr4_handle_solib_event is called.
1817 We called find_pc_section in svr4_create_solib_event_breakpoints,
1818 so we can guarantee that the dynamic linker's sections are in the
1819 section map. We can therefore inhibit section map updates across
1820 these calls to evaluate_argument and save a lot of time. */
1822 scoped_restore inhibit_updates
1823 = inhibit_section_map_updates (current_program_space);
1827 val = pa->prob->evaluate_argument (1, frame);
1829 catch (const gdb_exception_error &ex)
1831 exception_print (gdb_stderr, ex);
1832 val = NULL;
1835 if (val == NULL)
1836 return;
1838 debug_base = value_as_address (val);
1839 if (debug_base == 0)
1840 return;
1842 /* Always locate the debug struct, in case it moved. */
1843 info->debug_base = 0;
1844 if (locate_base (info) == 0)
1846 /* It's possible for the reloc_complete probe to be triggered before
1847 the linker has set the DT_DEBUG pointer (for example, when the
1848 linker has finished relocating an LD_AUDIT library or its
1849 dependencies). Since we can't yet handle libraries from other link
1850 namespaces, we don't lose anything by ignoring them here. */
1851 struct value *link_map_id_val;
1854 link_map_id_val = pa->prob->evaluate_argument (0, frame);
1856 catch (const gdb_exception_error)
1858 link_map_id_val = NULL;
1860 /* glibc and illumos' libc both define LM_ID_BASE as zero. */
1861 if (link_map_id_val != NULL && value_as_long (link_map_id_val) != 0)
1862 action = DO_NOTHING;
1863 else
1864 return;
1867 /* GDB does not currently support libraries loaded via dlmopen
1868 into namespaces other than the initial one. We must ignore
1869 any namespace other than the initial namespace here until
1870 support for this is added to GDB. */
1871 if (debug_base != info->debug_base)
1872 action = DO_NOTHING;
1874 if (action == UPDATE_OR_RELOAD)
1878 val = pa->prob->evaluate_argument (2, frame);
1880 catch (const gdb_exception_error &ex)
1882 exception_print (gdb_stderr, ex);
1883 return;
1886 if (val != NULL)
1887 lm = value_as_address (val);
1889 if (lm == 0)
1890 action = FULL_RELOAD;
1893 /* Resume section map updates. Closing the scope is
1894 sufficient. */
1897 if (action == UPDATE_OR_RELOAD)
1899 if (!solist_update_incremental (info, lm))
1900 action = FULL_RELOAD;
1903 if (action == FULL_RELOAD)
1905 if (!solist_update_full (info))
1906 return;
1909 cleanup.release ();
1912 /* Helper function for svr4_update_solib_event_breakpoints. */
1914 static bool
1915 svr4_update_solib_event_breakpoint (struct breakpoint *b)
1917 if (b->type != bp_shlib_event)
1919 /* Continue iterating. */
1920 return false;
1923 for (bp_location *loc : b->locations ())
1925 struct svr4_info *info;
1926 struct probe_and_action *pa;
1928 info = solib_svr4_pspace_data.get (loc->pspace);
1929 if (info == NULL || info->probes_table == NULL)
1930 continue;
1932 pa = solib_event_probe_at (info, loc->address);
1933 if (pa == NULL)
1934 continue;
1936 if (pa->action == DO_NOTHING)
1938 if (b->enable_state == bp_disabled && stop_on_solib_events)
1939 enable_breakpoint (b);
1940 else if (b->enable_state == bp_enabled && !stop_on_solib_events)
1941 disable_breakpoint (b);
1944 break;
1947 /* Continue iterating. */
1948 return false;
1951 /* Enable or disable optional solib event breakpoints as appropriate.
1952 Called whenever stop_on_solib_events is changed. */
1954 static void
1955 svr4_update_solib_event_breakpoints (void)
1957 for (breakpoint *bp : all_breakpoints_safe ())
1958 svr4_update_solib_event_breakpoint (bp);
1961 /* Create and register solib event breakpoints. PROBES is an array
1962 of NUM_PROBES elements, each of which is vector of probes. A
1963 solib event breakpoint will be created and registered for each
1964 probe. */
1966 static void
1967 svr4_create_probe_breakpoints (svr4_info *info, struct gdbarch *gdbarch,
1968 const std::vector<probe *> *probes,
1969 struct objfile *objfile)
1971 for (int i = 0; i < NUM_PROBES; i++)
1973 enum probe_action action = probe_info[i].action;
1975 for (probe *p : probes[i])
1977 CORE_ADDR address = p->get_relocated_address (objfile);
1979 create_solib_event_breakpoint (gdbarch, address);
1980 register_solib_event_probe (info, objfile, p, address, action);
1984 svr4_update_solib_event_breakpoints ();
1987 /* Find all the glibc named probes. Only if all of the probes are found, then
1988 create them and return true. Otherwise return false. If WITH_PREFIX is set
1989 then add "rtld" to the front of the probe names. */
1990 static bool
1991 svr4_find_and_create_probe_breakpoints (svr4_info *info,
1992 struct gdbarch *gdbarch,
1993 struct obj_section *os,
1994 bool with_prefix)
1996 std::vector<probe *> probes[NUM_PROBES];
1998 for (int i = 0; i < NUM_PROBES; i++)
2000 const char *name = probe_info[i].name;
2001 char buf[32];
2003 /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4 shipped with an early
2004 version of the probes code in which the probes' names were prefixed
2005 with "rtld_" and the "map_failed" probe did not exist. The locations
2006 of the probes are otherwise the same, so we check for probes with
2007 prefixed names if probes with unprefixed names are not present. */
2008 if (with_prefix)
2010 xsnprintf (buf, sizeof (buf), "rtld_%s", name);
2011 name = buf;
2014 probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
2016 /* The "map_failed" probe did not exist in early
2017 versions of the probes code in which the probes'
2018 names were prefixed with "rtld_". */
2019 if (with_prefix && streq (name, "rtld_map_failed"))
2020 continue;
2022 /* Ensure at least one probe for the current name was found. */
2023 if (probes[i].empty ())
2024 return false;
2026 /* Ensure probe arguments can be evaluated. */
2027 for (probe *p : probes[i])
2029 if (!p->can_evaluate_arguments ())
2030 return false;
2031 /* This will fail if the probe is invalid. This has been seen on Arm
2032 due to references to symbols that have been resolved away. */
2035 p->get_argument_count (gdbarch);
2037 catch (const gdb_exception_error &ex)
2039 exception_print (gdb_stderr, ex);
2040 warning (_("Initializing probes-based dynamic linker interface "
2041 "failed.\nReverting to original interface."));
2042 return false;
2047 /* All probes found. Now create them. */
2048 svr4_create_probe_breakpoints (info, gdbarch, probes, os->objfile);
2049 return true;
2052 /* Both the SunOS and the SVR4 dynamic linkers call a marker function
2053 before and after mapping and unmapping shared libraries. The sole
2054 purpose of this method is to allow debuggers to set a breakpoint so
2055 they can track these changes.
2057 Some versions of the glibc dynamic linker contain named probes
2058 to allow more fine grained stopping. Given the address of the
2059 original marker function, this function attempts to find these
2060 probes, and if found, sets breakpoints on those instead. If the
2061 probes aren't found, a single breakpoint is set on the original
2062 marker function. */
2064 static void
2065 svr4_create_solib_event_breakpoints (svr4_info *info, struct gdbarch *gdbarch,
2066 CORE_ADDR address)
2068 struct obj_section *os = find_pc_section (address);
2070 if (os == nullptr
2071 || (!svr4_find_and_create_probe_breakpoints (info, gdbarch, os, false)
2072 && !svr4_find_and_create_probe_breakpoints (info, gdbarch, os, true)))
2073 create_solib_event_breakpoint (gdbarch, address);
2076 /* Helper function for gdb_bfd_lookup_symbol. */
2078 static int
2079 cmp_name_and_sec_flags (const asymbol *sym, const void *data)
2081 return (strcmp (sym->name, (const char *) data) == 0
2082 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0);
2084 /* Arrange for dynamic linker to hit breakpoint.
2086 Both the SunOS and the SVR4 dynamic linkers have, as part of their
2087 debugger interface, support for arranging for the inferior to hit
2088 a breakpoint after mapping in the shared libraries. This function
2089 enables that breakpoint.
2091 For SunOS, there is a special flag location (in_debugger) which we
2092 set to 1. When the dynamic linker sees this flag set, it will set
2093 a breakpoint at a location known only to itself, after saving the
2094 original contents of that place and the breakpoint address itself,
2095 in it's own internal structures. When we resume the inferior, it
2096 will eventually take a SIGTRAP when it runs into the breakpoint.
2097 We handle this (in a different place) by restoring the contents of
2098 the breakpointed location (which is only known after it stops),
2099 chasing around to locate the shared libraries that have been
2100 loaded, then resuming.
2102 For SVR4, the debugger interface structure contains a member (r_brk)
2103 which is statically initialized at the time the shared library is
2104 built, to the offset of a function (_r_debug_state) which is guaran-
2105 teed to be called once before mapping in a library, and again when
2106 the mapping is complete. At the time we are examining this member,
2107 it contains only the unrelocated offset of the function, so we have
2108 to do our own relocation. Later, when the dynamic linker actually
2109 runs, it relocates r_brk to be the actual address of _r_debug_state().
2111 The debugger interface structure also contains an enumeration which
2112 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
2113 depending upon whether or not the library is being mapped or unmapped,
2114 and then set to RT_CONSISTENT after the library is mapped/unmapped. */
2116 static int
2117 enable_break (struct svr4_info *info, int from_tty)
2119 struct bound_minimal_symbol msymbol;
2120 const char * const *bkpt_namep;
2121 asection *interp_sect;
2122 CORE_ADDR sym_addr;
2124 info->interp_text_sect_low = info->interp_text_sect_high = 0;
2125 info->interp_plt_sect_low = info->interp_plt_sect_high = 0;
2127 /* If we already have a shared library list in the target, and
2128 r_debug contains r_brk, set the breakpoint there - this should
2129 mean r_brk has already been relocated. Assume the dynamic linker
2130 is the object containing r_brk. */
2132 solib_add (NULL, from_tty, auto_solib_add);
2133 sym_addr = 0;
2134 if (info->debug_base && solib_svr4_r_map (info) != 0)
2135 sym_addr = solib_svr4_r_brk (info);
2137 if (sym_addr != 0)
2139 struct obj_section *os;
2141 sym_addr = gdbarch_addr_bits_remove
2142 (target_gdbarch (),
2143 gdbarch_convert_from_func_ptr_addr
2144 (target_gdbarch (), sym_addr, current_inferior ()->top_target ()));
2146 /* On at least some versions of Solaris there's a dynamic relocation
2147 on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
2148 we get control before the dynamic linker has self-relocated.
2149 Check if SYM_ADDR is in a known section, if it is assume we can
2150 trust its value. This is just a heuristic though, it could go away
2151 or be replaced if it's getting in the way.
2153 On ARM we need to know whether the ISA of rtld_db_dlactivity (or
2154 however it's spelled in your particular system) is ARM or Thumb.
2155 That knowledge is encoded in the address, if it's Thumb the low bit
2156 is 1. However, we've stripped that info above and it's not clear
2157 what all the consequences are of passing a non-addr_bits_remove'd
2158 address to svr4_create_solib_event_breakpoints. The call to
2159 find_pc_section verifies we know about the address and have some
2160 hope of computing the right kind of breakpoint to use (via
2161 symbol info). It does mean that GDB needs to be pointed at a
2162 non-stripped version of the dynamic linker in order to obtain
2163 information it already knows about. Sigh. */
2165 os = find_pc_section (sym_addr);
2166 if (os != NULL)
2168 /* Record the relocated start and end address of the dynamic linker
2169 text and plt section for svr4_in_dynsym_resolve_code. */
2170 bfd *tmp_bfd;
2171 CORE_ADDR load_addr;
2173 tmp_bfd = os->objfile->obfd.get ();
2174 load_addr = os->objfile->text_section_offset ();
2176 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
2177 if (interp_sect)
2179 info->interp_text_sect_low
2180 = bfd_section_vma (interp_sect) + load_addr;
2181 info->interp_text_sect_high
2182 = info->interp_text_sect_low + bfd_section_size (interp_sect);
2184 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
2185 if (interp_sect)
2187 info->interp_plt_sect_low
2188 = bfd_section_vma (interp_sect) + load_addr;
2189 info->interp_plt_sect_high
2190 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
2193 svr4_create_solib_event_breakpoints (info, target_gdbarch (), sym_addr);
2194 return 1;
2198 /* Find the program interpreter; if not found, warn the user and drop
2199 into the old breakpoint at symbol code. */
2200 gdb::optional<gdb::byte_vector> interp_name_holder
2201 = find_program_interpreter ();
2202 if (interp_name_holder)
2204 const char *interp_name = (const char *) interp_name_holder->data ();
2205 CORE_ADDR load_addr = 0;
2206 int load_addr_found = 0;
2207 int loader_found_in_list = 0;
2208 struct target_ops *tmp_bfd_target;
2210 sym_addr = 0;
2212 /* Now we need to figure out where the dynamic linker was
2213 loaded so that we can load its symbols and place a breakpoint
2214 in the dynamic linker itself.
2216 This address is stored on the stack. However, I've been unable
2217 to find any magic formula to find it for Solaris (appears to
2218 be trivial on GNU/Linux). Therefore, we have to try an alternate
2219 mechanism to find the dynamic linker's base address. */
2221 gdb_bfd_ref_ptr tmp_bfd;
2224 tmp_bfd = solib_bfd_open (interp_name);
2226 catch (const gdb_exception &ex)
2230 if (tmp_bfd == NULL)
2231 goto bkpt_at_symbol;
2233 /* Now convert the TMP_BFD into a target. That way target, as
2234 well as BFD operations can be used. */
2235 tmp_bfd_target = target_bfd_reopen (tmp_bfd);
2237 /* On a running target, we can get the dynamic linker's base
2238 address from the shared library table. */
2239 for (struct so_list *so : current_program_space->solibs ())
2241 if (svr4_same_1 (interp_name, so->so_original_name))
2243 load_addr_found = 1;
2244 loader_found_in_list = 1;
2245 load_addr = lm_addr_check (so, tmp_bfd.get ());
2246 break;
2250 /* If we were not able to find the base address of the loader
2251 from our so_list, then try using the AT_BASE auxilliary entry. */
2252 if (!load_addr_found)
2253 if (target_auxv_search (current_inferior ()->top_target (),
2254 AT_BASE, &load_addr) > 0)
2256 int addr_bit = gdbarch_addr_bit (target_gdbarch ());
2258 /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
2259 that `+ load_addr' will overflow CORE_ADDR width not creating
2260 invalid addresses like 0x101234567 for 32bit inferiors on 64bit
2261 GDB. */
2263 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
2265 CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
2266 CORE_ADDR tmp_entry_point = exec_entry_point (tmp_bfd.get (),
2267 tmp_bfd_target);
2269 gdb_assert (load_addr < space_size);
2271 /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
2272 64bit ld.so with 32bit executable, it should not happen. */
2274 if (tmp_entry_point < space_size
2275 && tmp_entry_point + load_addr >= space_size)
2276 load_addr -= space_size;
2279 load_addr_found = 1;
2282 /* Otherwise we find the dynamic linker's base address by examining
2283 the current pc (which should point at the entry point for the
2284 dynamic linker) and subtracting the offset of the entry point.
2286 This is more fragile than the previous approaches, but is a good
2287 fallback method because it has actually been working well in
2288 most cases. */
2289 if (!load_addr_found)
2291 struct regcache *regcache
2292 = get_thread_arch_regcache (current_inferior ()->process_target (),
2293 inferior_ptid, target_gdbarch ());
2295 load_addr = (regcache_read_pc (regcache)
2296 - exec_entry_point (tmp_bfd.get (), tmp_bfd_target));
2299 if (!loader_found_in_list)
2301 info->debug_loader_name = xstrdup (interp_name);
2302 info->debug_loader_offset_p = 1;
2303 info->debug_loader_offset = load_addr;
2304 solib_add (NULL, from_tty, auto_solib_add);
2307 /* Record the relocated start and end address of the dynamic linker
2308 text and plt section for svr4_in_dynsym_resolve_code. */
2309 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
2310 if (interp_sect)
2312 info->interp_text_sect_low
2313 = bfd_section_vma (interp_sect) + load_addr;
2314 info->interp_text_sect_high
2315 = info->interp_text_sect_low + bfd_section_size (interp_sect);
2317 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
2318 if (interp_sect)
2320 info->interp_plt_sect_low
2321 = bfd_section_vma (interp_sect) + load_addr;
2322 info->interp_plt_sect_high
2323 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
2326 /* Now try to set a breakpoint in the dynamic linker. */
2327 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2329 sym_addr = gdb_bfd_lookup_symbol (tmp_bfd.get (),
2330 cmp_name_and_sec_flags,
2331 *bkpt_namep);
2332 if (sym_addr != 0)
2333 break;
2336 if (sym_addr != 0)
2337 /* Convert 'sym_addr' from a function pointer to an address.
2338 Because we pass tmp_bfd_target instead of the current
2339 target, this will always produce an unrelocated value. */
2340 sym_addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2341 sym_addr,
2342 tmp_bfd_target);
2344 /* We're done with both the temporary bfd and target. Closing
2345 the target closes the underlying bfd, because it holds the
2346 only remaining reference. */
2347 target_close (tmp_bfd_target);
2349 if (sym_addr != 0)
2351 svr4_create_solib_event_breakpoints (info, target_gdbarch (),
2352 load_addr + sym_addr);
2353 return 1;
2356 /* For whatever reason we couldn't set a breakpoint in the dynamic
2357 linker. Warn and drop into the old code. */
2358 bkpt_at_symbol:
2359 warning (_("Unable to find dynamic linker breakpoint function.\n"
2360 "GDB will be unable to debug shared library initializers\n"
2361 "and track explicitly loaded dynamic code."));
2364 /* Scan through the lists of symbols, trying to look up the symbol and
2365 set a breakpoint there. Terminate loop when we/if we succeed. */
2367 objfile *objf = current_program_space->symfile_object_file;
2368 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2370 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
2371 if ((msymbol.minsym != NULL)
2372 && (msymbol.value_address () != 0))
2374 sym_addr = msymbol.value_address ();
2375 sym_addr = gdbarch_convert_from_func_ptr_addr
2376 (target_gdbarch (), sym_addr, current_inferior ()->top_target ());
2377 svr4_create_solib_event_breakpoints (info, target_gdbarch (),
2378 sym_addr);
2379 return 1;
2383 if (interp_name_holder && !current_inferior ()->attach_flag)
2385 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
2387 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
2388 if ((msymbol.minsym != NULL)
2389 && (msymbol.value_address () != 0))
2391 sym_addr = msymbol.value_address ();
2392 sym_addr = gdbarch_convert_from_func_ptr_addr
2393 (target_gdbarch (), sym_addr,
2394 current_inferior ()->top_target ());
2395 svr4_create_solib_event_breakpoints (info, target_gdbarch (),
2396 sym_addr);
2397 return 1;
2401 return 0;
2404 /* Read the ELF program headers from ABFD. */
2406 static gdb::optional<gdb::byte_vector>
2407 read_program_headers_from_bfd (bfd *abfd)
2409 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
2410 int phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
2411 if (phdrs_size == 0)
2412 return {};
2414 gdb::byte_vector buf (phdrs_size);
2415 if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
2416 || bfd_bread (buf.data (), phdrs_size, abfd) != phdrs_size)
2417 return {};
2419 return buf;
2422 /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
2423 exec_bfd. Otherwise return 0.
2425 We relocate all of the sections by the same amount. This
2426 behavior is mandated by recent editions of the System V ABI.
2427 According to the System V Application Binary Interface,
2428 Edition 4.1, page 5-5:
2430 ... Though the system chooses virtual addresses for
2431 individual processes, it maintains the segments' relative
2432 positions. Because position-independent code uses relative
2433 addressing between segments, the difference between
2434 virtual addresses in memory must match the difference
2435 between virtual addresses in the file. The difference
2436 between the virtual address of any segment in memory and
2437 the corresponding virtual address in the file is thus a
2438 single constant value for any one executable or shared
2439 object in a given process. This difference is the base
2440 address. One use of the base address is to relocate the
2441 memory image of the program during dynamic linking.
2443 The same language also appears in Edition 4.0 of the System V
2444 ABI and is left unspecified in some of the earlier editions.
2446 Decide if the objfile needs to be relocated. As indicated above, we will
2447 only be here when execution is stopped. But during attachment PC can be at
2448 arbitrary address therefore regcache_read_pc can be misleading (contrary to
2449 the auxv AT_ENTRY value). Moreover for executable with interpreter section
2450 regcache_read_pc would point to the interpreter and not the main executable.
2452 So, to summarize, relocations are necessary when the start address obtained
2453 from the executable is different from the address in auxv AT_ENTRY entry.
2455 [ The astute reader will note that we also test to make sure that
2456 the executable in question has the DYNAMIC flag set. It is my
2457 opinion that this test is unnecessary (undesirable even). It
2458 was added to avoid inadvertent relocation of an executable
2459 whose e_type member in the ELF header is not ET_DYN. There may
2460 be a time in the future when it is desirable to do relocations
2461 on other types of files as well in which case this condition
2462 should either be removed or modified to accomodate the new file
2463 type. - Kevin, Nov 2000. ] */
2465 static int
2466 svr4_exec_displacement (CORE_ADDR *displacementp)
2468 /* ENTRY_POINT is a possible function descriptor - before
2469 a call to gdbarch_convert_from_func_ptr_addr. */
2470 CORE_ADDR entry_point, exec_displacement;
2472 if (current_program_space->exec_bfd () == NULL)
2473 return 0;
2475 /* Therefore for ELF it is ET_EXEC and not ET_DYN. Both shared libraries
2476 being executed themselves and PIE (Position Independent Executable)
2477 executables are ET_DYN. */
2479 if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0)
2480 return 0;
2482 if (target_auxv_search (current_inferior ()->top_target (),
2483 AT_ENTRY, &entry_point) <= 0)
2484 return 0;
2486 exec_displacement
2487 = entry_point - bfd_get_start_address (current_program_space->exec_bfd ());
2489 /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
2490 alignment. It is cheaper than the program headers comparison below. */
2492 if (bfd_get_flavour (current_program_space->exec_bfd ())
2493 == bfd_target_elf_flavour)
2495 const struct elf_backend_data *elf
2496 = get_elf_backend_data (current_program_space->exec_bfd ());
2498 /* p_align of PT_LOAD segments does not specify any alignment but
2499 only congruency of addresses:
2500 p_offset % p_align == p_vaddr % p_align
2501 Kernel is free to load the executable with lower alignment. */
2503 if ((exec_displacement & (elf->minpagesize - 1)) != 0)
2504 return 0;
2507 /* Verify that the auxilliary vector describes the same file as exec_bfd, by
2508 comparing their program headers. If the program headers in the auxilliary
2509 vector do not match the program headers in the executable, then we are
2510 looking at a different file than the one used by the kernel - for
2511 instance, "gdb program" connected to "gdbserver :PORT ld.so program". */
2513 if (bfd_get_flavour (current_program_space->exec_bfd ())
2514 == bfd_target_elf_flavour)
2516 /* Be optimistic and return 0 only if GDB was able to verify the headers
2517 really do not match. */
2518 int arch_size;
2520 gdb::optional<gdb::byte_vector> phdrs_target
2521 = read_program_header (-1, &arch_size, NULL);
2522 gdb::optional<gdb::byte_vector> phdrs_binary
2523 = read_program_headers_from_bfd (current_program_space->exec_bfd ());
2524 if (phdrs_target && phdrs_binary)
2526 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
2528 /* We are dealing with three different addresses. EXEC_BFD
2529 represents current address in on-disk file. target memory content
2530 may be different from EXEC_BFD as the file may have been prelinked
2531 to a different address after the executable has been loaded.
2532 Moreover the address of placement in target memory can be
2533 different from what the program headers in target memory say -
2534 this is the goal of PIE.
2536 Detected DISPLACEMENT covers both the offsets of PIE placement and
2537 possible new prelink performed after start of the program. Here
2538 relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
2539 content offset for the verification purpose. */
2541 if (phdrs_target->size () != phdrs_binary->size ()
2542 || bfd_get_arch_size (current_program_space->exec_bfd ()) != arch_size)
2543 return 0;
2544 else if (arch_size == 32
2545 && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
2546 && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
2548 Elf_Internal_Ehdr *ehdr2
2549 = elf_tdata (current_program_space->exec_bfd ())->elf_header;
2550 Elf_Internal_Phdr *phdr2
2551 = elf_tdata (current_program_space->exec_bfd ())->phdr;
2552 CORE_ADDR displacement = 0;
2553 int i;
2555 /* DISPLACEMENT could be found more easily by the difference of
2556 ehdr2->e_entry. But we haven't read the ehdr yet, and we
2557 already have enough information to compute that displacement
2558 with what we've read. */
2560 for (i = 0; i < ehdr2->e_phnum; i++)
2561 if (phdr2[i].p_type == PT_LOAD)
2563 Elf32_External_Phdr *phdrp;
2564 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2565 CORE_ADDR vaddr, paddr;
2566 CORE_ADDR displacement_vaddr = 0;
2567 CORE_ADDR displacement_paddr = 0;
2569 phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
2570 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2571 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2573 vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
2574 byte_order);
2575 displacement_vaddr = vaddr - phdr2[i].p_vaddr;
2577 paddr = extract_unsigned_integer (buf_paddr_p, 4,
2578 byte_order);
2579 displacement_paddr = paddr - phdr2[i].p_paddr;
2581 if (displacement_vaddr == displacement_paddr)
2582 displacement = displacement_vaddr;
2584 break;
2587 /* Now compare program headers from the target and the binary
2588 with optional DISPLACEMENT. */
2590 for (i = 0;
2591 i < phdrs_target->size () / sizeof (Elf32_External_Phdr);
2592 i++)
2594 Elf32_External_Phdr *phdrp;
2595 Elf32_External_Phdr *phdr2p;
2596 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2597 CORE_ADDR vaddr, paddr;
2598 asection *plt2_asect;
2600 phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
2601 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2602 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2603 phdr2p = &((Elf32_External_Phdr *) phdrs_binary->data ())[i];
2605 /* PT_GNU_STACK is an exception by being never relocated by
2606 prelink as its addresses are always zero. */
2608 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2609 continue;
2611 /* Check also other adjustment combinations - PR 11786. */
2613 vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
2614 byte_order);
2615 vaddr -= displacement;
2616 store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);
2618 paddr = extract_unsigned_integer (buf_paddr_p, 4,
2619 byte_order);
2620 paddr -= displacement;
2621 store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);
2623 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2624 continue;
2626 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2627 CentOS-5 has problems with filesz, memsz as well.
2628 Strip also modifies memsz of PT_TLS.
2629 See PR 11786. */
2630 if (phdr2[i].p_type == PT_GNU_RELRO
2631 || phdr2[i].p_type == PT_TLS)
2633 Elf32_External_Phdr tmp_phdr = *phdrp;
2634 Elf32_External_Phdr tmp_phdr2 = *phdr2p;
2636 memset (tmp_phdr.p_filesz, 0, 4);
2637 memset (tmp_phdr.p_memsz, 0, 4);
2638 memset (tmp_phdr.p_flags, 0, 4);
2639 memset (tmp_phdr.p_align, 0, 4);
2640 memset (tmp_phdr2.p_filesz, 0, 4);
2641 memset (tmp_phdr2.p_memsz, 0, 4);
2642 memset (tmp_phdr2.p_flags, 0, 4);
2643 memset (tmp_phdr2.p_align, 0, 4);
2645 if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
2646 == 0)
2647 continue;
2650 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2651 bfd *exec_bfd = current_program_space->exec_bfd ();
2652 plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
2653 if (plt2_asect)
2655 int content2;
2656 gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
2657 CORE_ADDR filesz;
2659 content2 = (bfd_section_flags (plt2_asect)
2660 & SEC_HAS_CONTENTS) != 0;
2662 filesz = extract_unsigned_integer (buf_filesz_p, 4,
2663 byte_order);
2665 /* PLT2_ASECT is from on-disk file (exec_bfd) while
2666 FILESZ is from the in-memory image. */
2667 if (content2)
2668 filesz += bfd_section_size (plt2_asect);
2669 else
2670 filesz -= bfd_section_size (plt2_asect);
2672 store_unsigned_integer (buf_filesz_p, 4, byte_order,
2673 filesz);
2675 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2676 continue;
2679 return 0;
2682 else if (arch_size == 64
2683 && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
2684 && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
2686 Elf_Internal_Ehdr *ehdr2
2687 = elf_tdata (current_program_space->exec_bfd ())->elf_header;
2688 Elf_Internal_Phdr *phdr2
2689 = elf_tdata (current_program_space->exec_bfd ())->phdr;
2690 CORE_ADDR displacement = 0;
2691 int i;
2693 /* DISPLACEMENT could be found more easily by the difference of
2694 ehdr2->e_entry. But we haven't read the ehdr yet, and we
2695 already have enough information to compute that displacement
2696 with what we've read. */
2698 for (i = 0; i < ehdr2->e_phnum; i++)
2699 if (phdr2[i].p_type == PT_LOAD)
2701 Elf64_External_Phdr *phdrp;
2702 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2703 CORE_ADDR vaddr, paddr;
2704 CORE_ADDR displacement_vaddr = 0;
2705 CORE_ADDR displacement_paddr = 0;
2707 phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
2708 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2709 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2711 vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
2712 byte_order);
2713 displacement_vaddr = vaddr - phdr2[i].p_vaddr;
2715 paddr = extract_unsigned_integer (buf_paddr_p, 8,
2716 byte_order);
2717 displacement_paddr = paddr - phdr2[i].p_paddr;
2719 if (displacement_vaddr == displacement_paddr)
2720 displacement = displacement_vaddr;
2722 break;
2725 /* Now compare BUF and BUF2 with optional DISPLACEMENT. */
2727 for (i = 0;
2728 i < phdrs_target->size () / sizeof (Elf64_External_Phdr);
2729 i++)
2731 Elf64_External_Phdr *phdrp;
2732 Elf64_External_Phdr *phdr2p;
2733 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2734 CORE_ADDR vaddr, paddr;
2735 asection *plt2_asect;
2737 phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
2738 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2739 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2740 phdr2p = &((Elf64_External_Phdr *) phdrs_binary->data ())[i];
2742 /* PT_GNU_STACK is an exception by being never relocated by
2743 prelink as its addresses are always zero. */
2745 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2746 continue;
2748 /* Check also other adjustment combinations - PR 11786. */
2750 vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
2751 byte_order);
2752 vaddr -= displacement;
2753 store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);
2755 paddr = extract_unsigned_integer (buf_paddr_p, 8,
2756 byte_order);
2757 paddr -= displacement;
2758 store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);
2760 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2761 continue;
2763 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2764 CentOS-5 has problems with filesz, memsz as well.
2765 Strip also modifies memsz of PT_TLS.
2766 See PR 11786. */
2767 if (phdr2[i].p_type == PT_GNU_RELRO
2768 || phdr2[i].p_type == PT_TLS)
2770 Elf64_External_Phdr tmp_phdr = *phdrp;
2771 Elf64_External_Phdr tmp_phdr2 = *phdr2p;
2773 memset (tmp_phdr.p_filesz, 0, 8);
2774 memset (tmp_phdr.p_memsz, 0, 8);
2775 memset (tmp_phdr.p_flags, 0, 4);
2776 memset (tmp_phdr.p_align, 0, 8);
2777 memset (tmp_phdr2.p_filesz, 0, 8);
2778 memset (tmp_phdr2.p_memsz, 0, 8);
2779 memset (tmp_phdr2.p_flags, 0, 4);
2780 memset (tmp_phdr2.p_align, 0, 8);
2782 if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
2783 == 0)
2784 continue;
2787 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2788 plt2_asect
2789 = bfd_get_section_by_name (current_program_space->exec_bfd (),
2790 ".plt");
2791 if (plt2_asect)
2793 int content2;
2794 gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
2795 CORE_ADDR filesz;
2797 content2 = (bfd_section_flags (plt2_asect)
2798 & SEC_HAS_CONTENTS) != 0;
2800 filesz = extract_unsigned_integer (buf_filesz_p, 8,
2801 byte_order);
2803 /* PLT2_ASECT is from on-disk file (current
2804 exec_bfd) while FILESZ is from the in-memory
2805 image. */
2806 if (content2)
2807 filesz += bfd_section_size (plt2_asect);
2808 else
2809 filesz -= bfd_section_size (plt2_asect);
2811 store_unsigned_integer (buf_filesz_p, 8, byte_order,
2812 filesz);
2814 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2815 continue;
2818 return 0;
2821 else
2822 return 0;
2826 if (info_verbose)
2828 /* It can be printed repeatedly as there is no easy way to check
2829 the executable symbols/file has been already relocated to
2830 displacement. */
2832 gdb_printf (_("Using PIE (Position Independent Executable) "
2833 "displacement %s for \"%s\".\n"),
2834 paddress (target_gdbarch (), exec_displacement),
2835 bfd_get_filename (current_program_space->exec_bfd ()));
2838 *displacementp = exec_displacement;
2839 return 1;
2842 /* Relocate the main executable. This function should be called upon
2843 stopping the inferior process at the entry point to the program.
2844 The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
2845 different, the main executable is relocated by the proper amount. */
2847 static void
2848 svr4_relocate_main_executable (void)
2850 CORE_ADDR displacement;
2852 /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
2853 probably contains the offsets computed using the PIE displacement
2854 from the previous run, which of course are irrelevant for this run.
2855 So we need to determine the new PIE displacement and recompute the
2856 section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
2857 already contains pre-computed offsets.
2859 If we cannot compute the PIE displacement, either:
2861 - The executable is not PIE.
2863 - SYMFILE_OBJFILE does not match the executable started in the target.
2864 This can happen for main executable symbols loaded at the host while
2865 `ld.so --ld-args main-executable' is loaded in the target.
2867 Then we leave the section offsets untouched and use them as is for
2868 this run. Either:
2870 - These section offsets were properly reset earlier, and thus
2871 already contain the correct values. This can happen for instance
2872 when reconnecting via the remote protocol to a target that supports
2873 the `qOffsets' packet.
2875 - The section offsets were not reset earlier, and the best we can
2876 hope is that the old offsets are still applicable to the new run. */
2878 if (! svr4_exec_displacement (&displacement))
2879 return;
2881 /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
2882 addresses. */
2884 objfile *objf = current_program_space->symfile_object_file;
2885 if (objf)
2887 section_offsets new_offsets (objf->section_offsets.size (),
2888 displacement);
2889 objfile_relocate (objf, new_offsets);
2891 else if (current_program_space->exec_bfd ())
2893 asection *asect;
2895 bfd *exec_bfd = current_program_space->exec_bfd ();
2896 for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
2897 exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
2898 bfd_section_vma (asect) + displacement);
2902 /* Implement the "create_inferior_hook" target_solib_ops method.
2904 For SVR4 executables, this first instruction is either the first
2905 instruction in the dynamic linker (for dynamically linked
2906 executables) or the instruction at "start" for statically linked
2907 executables. For dynamically linked executables, the system
2908 first exec's /lib/libc.so.N, which contains the dynamic linker,
2909 and starts it running. The dynamic linker maps in any needed
2910 shared libraries, maps in the actual user executable, and then
2911 jumps to "start" in the user executable.
2913 We can arrange to cooperate with the dynamic linker to discover the
2914 names of shared libraries that are dynamically linked, and the base
2915 addresses to which they are linked.
2917 This function is responsible for discovering those names and
2918 addresses, and saving sufficient information about them to allow
2919 their symbols to be read at a later time. */
2921 static void
2922 svr4_solib_create_inferior_hook (int from_tty)
2924 struct svr4_info *info;
2926 info = get_svr4_info (current_program_space);
2928 /* Clear the probes-based interface's state. */
2929 free_probes_table (info);
2930 free_solib_list (info);
2932 /* Relocate the main executable if necessary. */
2933 svr4_relocate_main_executable ();
2935 /* No point setting a breakpoint in the dynamic linker if we can't
2936 hit it (e.g., a core file, or a trace file). */
2937 if (!target_has_execution ())
2938 return;
2940 if (!svr4_have_link_map_offsets ())
2941 return;
2943 if (!enable_break (info, from_tty))
2944 return;
2947 static void
2948 svr4_clear_solib (void)
2950 struct svr4_info *info;
2952 info = get_svr4_info (current_program_space);
2953 info->debug_base = 0;
2954 info->debug_loader_offset_p = 0;
2955 info->debug_loader_offset = 0;
2956 xfree (info->debug_loader_name);
2957 info->debug_loader_name = NULL;
2960 /* Clear any bits of ADDR that wouldn't fit in a target-format
2961 data pointer. "Data pointer" here refers to whatever sort of
2962 address the dynamic linker uses to manage its sections. At the
2963 moment, we don't support shared libraries on any processors where
2964 code and data pointers are different sizes.
2966 This isn't really the right solution. What we really need here is
2967 a way to do arithmetic on CORE_ADDR values that respects the
2968 natural pointer/address correspondence. (For example, on the MIPS,
2969 converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
2970 sign-extend the value. There, simply truncating the bits above
2971 gdbarch_ptr_bit, as we do below, is no good.) This should probably
2972 be a new gdbarch method or something. */
2973 static CORE_ADDR
2974 svr4_truncate_ptr (CORE_ADDR addr)
2976 if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
2977 /* We don't need to truncate anything, and the bit twiddling below
2978 will fail due to overflow problems. */
2979 return addr;
2980 else
2981 return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
2985 static void
2986 svr4_relocate_section_addresses (struct so_list *so,
2987 struct target_section *sec)
2989 bfd *abfd = sec->the_bfd_section->owner;
2991 sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
2992 sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
2996 /* Architecture-specific operations. */
2998 struct solib_svr4_ops
3000 /* Return a description of the layout of `struct link_map'. */
3001 struct link_map_offsets *(*fetch_link_map_offsets)(void) = nullptr;
3004 /* Per-architecture data key. */
3005 static const registry<gdbarch>::key<struct solib_svr4_ops> solib_svr4_data;
3007 /* Return a default for the architecture-specific operations. */
3009 static struct solib_svr4_ops *
3010 get_ops (struct gdbarch *gdbarch)
3012 struct solib_svr4_ops *ops = solib_svr4_data.get (gdbarch);
3013 if (ops == nullptr)
3014 ops = solib_svr4_data.emplace (gdbarch);
3015 return ops;
3018 /* Set the architecture-specific `struct link_map_offsets' fetcher for
3019 GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
3021 void
3022 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
3023 struct link_map_offsets *(*flmo) (void))
3025 struct solib_svr4_ops *ops = get_ops (gdbarch);
3027 ops->fetch_link_map_offsets = flmo;
3029 set_gdbarch_so_ops (gdbarch, &svr4_so_ops);
3030 set_gdbarch_iterate_over_objfiles_in_search_order
3031 (gdbarch, svr4_iterate_over_objfiles_in_search_order);
3034 /* Fetch a link_map_offsets structure using the architecture-specific
3035 `struct link_map_offsets' fetcher. */
3037 static struct link_map_offsets *
3038 svr4_fetch_link_map_offsets (void)
3040 struct solib_svr4_ops *ops = get_ops (target_gdbarch ());
3042 gdb_assert (ops->fetch_link_map_offsets);
3043 return ops->fetch_link_map_offsets ();
3046 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
3048 static int
3049 svr4_have_link_map_offsets (void)
3051 struct solib_svr4_ops *ops = get_ops (target_gdbarch ());
3053 return (ops->fetch_link_map_offsets != NULL);
3057 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
3058 `struct r_debug' and a `struct link_map' that are binary compatible
3059 with the original SVR4 implementation. */
3061 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3062 for an ILP32 SVR4 system. */
3064 struct link_map_offsets *
3065 svr4_ilp32_fetch_link_map_offsets (void)
3067 static struct link_map_offsets lmo;
3068 static struct link_map_offsets *lmp = NULL;
3070 if (lmp == NULL)
3072 lmp = &lmo;
3074 lmo.r_version_offset = 0;
3075 lmo.r_version_size = 4;
3076 lmo.r_map_offset = 4;
3077 lmo.r_brk_offset = 8;
3078 lmo.r_ldsomap_offset = 20;
3080 /* Everything we need is in the first 20 bytes. */
3081 lmo.link_map_size = 20;
3082 lmo.l_addr_offset = 0;
3083 lmo.l_name_offset = 4;
3084 lmo.l_ld_offset = 8;
3085 lmo.l_next_offset = 12;
3086 lmo.l_prev_offset = 16;
3089 return lmp;
3092 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3093 for an LP64 SVR4 system. */
3095 struct link_map_offsets *
3096 svr4_lp64_fetch_link_map_offsets (void)
3098 static struct link_map_offsets lmo;
3099 static struct link_map_offsets *lmp = NULL;
3101 if (lmp == NULL)
3103 lmp = &lmo;
3105 lmo.r_version_offset = 0;
3106 lmo.r_version_size = 4;
3107 lmo.r_map_offset = 8;
3108 lmo.r_brk_offset = 16;
3109 lmo.r_ldsomap_offset = 40;
3111 /* Everything we need is in the first 40 bytes. */
3112 lmo.link_map_size = 40;
3113 lmo.l_addr_offset = 0;
3114 lmo.l_name_offset = 8;
3115 lmo.l_ld_offset = 16;
3116 lmo.l_next_offset = 24;
3117 lmo.l_prev_offset = 32;
3120 return lmp;
3124 /* Search order for ELF DSOs linked with -Bsymbolic. Those DSOs have a
3125 different rule for symbol lookup. The lookup begins here in the DSO, not in
3126 the main executable. */
3128 static void
3129 svr4_iterate_over_objfiles_in_search_order
3130 (gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb,
3131 objfile *current_objfile)
3133 bool checked_current_objfile = false;
3134 if (current_objfile != nullptr)
3136 bfd *abfd;
3138 if (current_objfile->separate_debug_objfile_backlink != nullptr)
3139 current_objfile = current_objfile->separate_debug_objfile_backlink;
3141 if (current_objfile == current_program_space->symfile_object_file)
3142 abfd = current_program_space->exec_bfd ();
3143 else
3144 abfd = current_objfile->obfd.get ();
3146 if (abfd != nullptr
3147 && gdb_bfd_scan_elf_dyntag (DT_SYMBOLIC, abfd, nullptr, nullptr) == 1)
3149 checked_current_objfile = true;
3150 if (cb (current_objfile))
3151 return;
3155 for (objfile *objfile : current_program_space->objfiles ())
3157 if (checked_current_objfile && objfile == current_objfile)
3158 continue;
3159 if (cb (objfile))
3160 return;
3164 const struct target_so_ops svr4_so_ops =
3166 svr4_relocate_section_addresses,
3167 svr4_free_so,
3168 svr4_clear_so,
3169 svr4_clear_solib,
3170 svr4_solib_create_inferior_hook,
3171 svr4_current_sos,
3172 open_symbol_file_object,
3173 svr4_in_dynsym_resolve_code,
3174 solib_bfd_open,
3175 nullptr,
3176 svr4_same,
3177 svr4_keep_data_in_core,
3178 svr4_update_solib_event_breakpoints,
3179 svr4_handle_solib_event,
3182 void _initialize_svr4_solib ();
3183 void
3184 _initialize_svr4_solib ()
3186 gdb::observers::free_objfile.attach (svr4_free_objfile_observer,
3187 "solib-svr4");