GDB (xrefs)
Loading...
Searching...
No Matches
solib-svr4.c
Go to the documentation of this file.
1/* Handle SVR4 shared libraries for GDB, the GNU Debugger.
2
3 Copyright (C) 1990-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21
22#include "elf/external.h"
23#include "elf/common.h"
24#include "elf/mips.h"
25
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 "observable.h"
36
37#include "solist.h"
38#include "solib.h"
39#include "solib-svr4.h"
40
41#include "bfd-target.h"
42#include "elf-bfd.h"
43#include "exec.h"
44#include "auxv.h"
45#include "gdb_bfd.h"
46#include "probe.h"
47
48#include <map>
49
51static int svr4_have_link_map_offsets (void);
52static void svr4_relocate_main_executable (void);
53static void svr4_free_library_list (so_list *solist);
57 objfile *current_objfile);
58
59
60/* On SVR4 systems, a list of symbols in the dynamic linker where
61 GDB can try to place a breakpoint to monitor shared library
62 events.
63
64 If none of these symbols are found, or other errors occur, then
65 SVR4 systems will fall back to using a symbol as the "startup
66 mapping complete" breakpoint address. */
67
68static const char * const solib_break_names[] =
69{
70 "r_debug_state",
71 "_r_debug_state",
72 "_dl_debug_state",
73 "rtld_db_dlactivity",
74 "__dl_rtld_db_dlactivity",
75 "_rtld_debug_state",
76
77 NULL
78};
79
80static const char * const bkpt_names[] =
81{
82 "_start",
83 "__start",
84 "main",
85 NULL
86};
87
88static const char * const main_name_list[] =
89{
90 "main_$main",
91 NULL
92};
93
94/* What to do when a probe stop occurs. */
95
97{
98 /* Something went seriously wrong. Stop using probes and
99 revert to using the older interface. */
101
102 /* No action is required. The shared object list is still
103 valid. */
105
106 /* The shared object list should be reloaded entirely. */
108
109 /* Attempt to incrementally update the shared object list. If
110 the update fails or is not possible, fall back to reloading
111 the list in full. */
113};
114
115/* A probe's name and its associated action. */
116
118{
119 /* The name of the probe. */
120 const char *name;
121
122 /* What to do when a probe stop occurs. */
124};
125
126/* A list of named probes and their associated actions. If all
127 probes are present in the dynamic linker then the probes-based
128 interface will be used. */
129
130static const struct probe_info probe_info[] =
131{
132 { "init_start", DO_NOTHING },
133 { "init_complete", FULL_RELOAD },
134 { "map_start", DO_NOTHING },
135 { "map_failed", DO_NOTHING },
136 { "reloc_complete", UPDATE_OR_RELOAD },
137 { "unmap_start", DO_NOTHING },
138 { "unmap_complete", FULL_RELOAD },
139};
140
141#define NUM_PROBES ARRAY_SIZE (probe_info)
142
143/* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
144 the same shared library. */
145
146static int
147svr4_same_1 (const char *gdb_so_name, const char *inferior_so_name)
148{
149 if (strcmp (gdb_so_name, inferior_so_name) == 0)
150 return 1;
151
152 /* On Solaris, when starting inferior we think that dynamic linker is
153 /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
154 contains /lib/ld.so.1. Sometimes one file is a link to another, but
155 sometimes they have identical content, but are not linked to each
156 other. We don't restrict this check for Solaris, but the chances
157 of running into this situation elsewhere are very low. */
158 if (strcmp (gdb_so_name, "/usr/lib/ld.so.1") == 0
159 && strcmp (inferior_so_name, "/lib/ld.so.1") == 0)
160 return 1;
161
162 /* Similarly, we observed the same issue with amd64 and sparcv9, but with
163 different locations. */
164 if (strcmp (gdb_so_name, "/usr/lib/amd64/ld.so.1") == 0
165 && strcmp (inferior_so_name, "/lib/amd64/ld.so.1") == 0)
166 return 1;
167
168 if (strcmp (gdb_so_name, "/usr/lib/sparcv9/ld.so.1") == 0
169 && strcmp (inferior_so_name, "/lib/sparcv9/ld.so.1") == 0)
170 return 1;
171
172 return 0;
173}
174
175static int
177{
178 if (!svr4_same_1 (gdb->so_original_name, inferior->so_original_name))
179 return false;
180
181 /* There may be different instances of the same library, in different
182 namespaces. Each instance, however, must have been loaded at a
183 different address so its relocation offset would be different. */
184 const lm_info_svr4 *lmg = (const lm_info_svr4 *) gdb->lm_info;
185 const lm_info_svr4 *lmi = (const lm_info_svr4 *) inferior->lm_info;
186
187 return (lmg->l_addr_inferior == lmi->l_addr_inferior);
188}
189
190static std::unique_ptr<lm_info_svr4>
192{
194 std::unique_ptr<lm_info_svr4> lm_info;
195
196 gdb::byte_vector lm (lmo->link_map_size);
197
198 if (target_read_memory (lm_addr, lm.data (), lmo->link_map_size) != 0)
199 warning (_("Error reading shared library list entry at %s"),
201 else
202 {
203 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
204
205 lm_info.reset (new lm_info_svr4);
206 lm_info->lm_addr = lm_addr;
207
208 lm_info->l_addr_inferior = extract_typed_address (&lm[lmo->l_addr_offset],
209 ptr_type);
210 lm_info->l_ld = extract_typed_address (&lm[lmo->l_ld_offset], ptr_type);
211 lm_info->l_next = extract_typed_address (&lm[lmo->l_next_offset],
212 ptr_type);
213 lm_info->l_prev = extract_typed_address (&lm[lmo->l_prev_offset],
214 ptr_type);
215 lm_info->l_name = extract_typed_address (&lm[lmo->l_name_offset],
216 ptr_type);
217 }
218
219 return lm_info;
220}
221
222static int
224{
226
227 return lmo->l_ld_offset >= 0;
228}
229
230static CORE_ADDR
231lm_addr_check (const struct so_list *so, bfd *abfd)
232{
233 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
234
235 if (!li->l_addr_p)
236 {
237 struct bfd_section *dyninfo_sect;
238 CORE_ADDR l_addr, l_dynaddr, dynaddr;
239
240 l_addr = li->l_addr_inferior;
241
242 if (! abfd || ! has_lm_dynamic_from_link_map ())
243 goto set_addr;
244
245 l_dynaddr = li->l_ld;
246
247 dyninfo_sect = bfd_get_section_by_name (abfd, ".dynamic");
248 if (dyninfo_sect == NULL)
249 goto set_addr;
250
251 dynaddr = bfd_section_vma (dyninfo_sect);
252
253 if (dynaddr + l_addr != l_dynaddr)
254 {
255 CORE_ADDR align = 0x1000;
256 CORE_ADDR minpagesize = align;
257
258 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
259 {
260 Elf_Internal_Ehdr *ehdr = elf_tdata (abfd)->elf_header;
261 Elf_Internal_Phdr *phdr = elf_tdata (abfd)->phdr;
262 int i;
263
264 align = 1;
265
266 for (i = 0; i < ehdr->e_phnum; i++)
267 if (phdr[i].p_type == PT_LOAD && phdr[i].p_align > align)
268 align = phdr[i].p_align;
269
270 minpagesize = get_elf_backend_data (abfd)->minpagesize;
271 }
272
273 /* Turn it into a mask. */
274 align--;
275
276 /* If the changes match the alignment requirements, we
277 assume we're using a core file that was generated by the
278 same binary, just prelinked with a different base offset.
279 If it doesn't match, we may have a different binary, the
280 same binary with the dynamic table loaded at an unrelated
281 location, or anything, really. To avoid regressions,
282 don't adjust the base offset in the latter case, although
283 odds are that, if things really changed, debugging won't
284 quite work.
285
286 One could expect more the condition
287 ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
288 but the one below is relaxed for PPC. The PPC kernel supports
289 either 4k or 64k page sizes. To be prepared for 64k pages,
290 PPC ELF files are built using an alignment requirement of 64k.
291 However, when running on a kernel supporting 4k pages, the memory
292 mapping of the library may not actually happen on a 64k boundary!
293
294 (In the usual case where (l_addr & align) == 0, this check is
295 equivalent to the possibly expected check above.)
296
297 Even on PPC it must be zero-aligned at least for MINPAGESIZE. */
298
299 l_addr = l_dynaddr - dynaddr;
300
301 if ((l_addr & (minpagesize - 1)) == 0
302 && (l_addr & align) == ((l_dynaddr - dynaddr) & align))
303 {
304 if (info_verbose)
305 gdb_printf (_("Using PIC (Position Independent Code) "
306 "prelink displacement %s for \"%s\".\n"),
307 paddress (target_gdbarch (), l_addr),
308 so->so_name);
309 }
310 else
311 {
312 /* There is no way to verify the library file matches. prelink
313 can during prelinking of an unprelinked file (or unprelinking
314 of a prelinked file) shift the DYNAMIC segment by arbitrary
315 offset without any page size alignment. There is no way to
316 find out the ELF header and/or Program Headers for a limited
317 verification if it they match. One could do a verification
318 of the DYNAMIC segment. Still the found address is the best
319 one GDB could find. */
320
321 warning (_(".dynamic section for \"%s\" "
322 "is not at the expected address "
323 "(wrong library or version mismatch?)"), so->so_name);
324 }
325 }
326
327 set_addr:
328 li->l_addr = l_addr;
329 li->l_addr_p = 1;
330 }
331
332 return li->l_addr;
333}
334
335/* Per pspace SVR4 specific data. */
336
338{
339 svr4_info () = default;
340 ~svr4_info ();
341
342 /* Base of dynamic linker structures in default namespace. */
343 CORE_ADDR debug_base = 0;
344
345 /* Validity flag for debug_loader_offset. */
347
348 /* Load address for the dynamic linker, inferred. */
349 CORE_ADDR debug_loader_offset = 0;
350
351 /* Name of the dynamic linker, valid if debug_loader_offset_p. */
352 char *debug_loader_name = nullptr;
353
354 /* Load map address for the main executable in default namespace. */
355 CORE_ADDR main_lm_addr = 0;
356
357 CORE_ADDR interp_text_sect_low = 0;
359 CORE_ADDR interp_plt_sect_low = 0;
360 CORE_ADDR interp_plt_sect_high = 0;
361
362 /* True if the list of objects was last obtained from the target
363 via qXfer:libraries-svr4:read. */
364 bool using_xfer = false;
365
366 /* Table of struct probe_and_action instances, used by the
367 probes-based interface to map breakpoint addresses to probes
368 and their associated actions. Lookup is performed using
369 probe_and_action->prob->address. */
371
372 /* List of objects loaded into the inferior per namespace, used by the
373 probes-based interface.
374
375 The namespace is represented by the address of its corresponding
376 r_debug[_ext] object. We get the namespace id as argument to the
377 'reloc_complete' probe but we don't get it when scanning the load map
378 on attach.
379
380 The r_debug[_ext] objects may move when ld.so itself moves. In that
381 case, we expect also the global _r_debug to move so we can detect
382 this and reload everything. The r_debug[_ext] objects are not
383 expected to move individually.
384
385 The special entry zero is reserved for a linear list to support
386 gdbstubs that do not support namespaces. */
387 std::map<CORE_ADDR, so_list *> solib_lists;
388};
389
390/* Per-program-space data key. */
392
393/* Return whether DEBUG_BASE is the default namespace of INFO. */
394
395static bool
396svr4_is_default_namespace (const svr4_info *info, CORE_ADDR debug_base)
397{
398 return (debug_base == info->debug_base);
399}
400
401/* Free the probes table. */
402
403static void
405{
406 info->probes_table.reset (nullptr);
407}
408
409/* Free the solib lists for all namespaces. */
410
411static void
413{
414 for (const std::pair<CORE_ADDR, so_list *> tuple
415 : info->solib_lists)
416 svr4_free_library_list (tuple.second);
417
418 info->solib_lists.clear ();
419}
420
425
426/* Get the svr4 data for program space PSPACE. If none is found yet, add it now.
427 This function always returns a valid object. */
428
429static struct svr4_info *
431{
432 struct svr4_info *info = solib_svr4_pspace_data.get (pspace);
433
434 if (info == NULL)
435 info = solib_svr4_pspace_data.emplace (pspace);
436
437 return info;
438}
439
440/* Local function prototypes */
441
442static int match_main (const char *);
443
444/* Read program header TYPE from inferior memory. The header is found
445 by scanning the OS auxiliary vector.
446
447 If TYPE == -1, return the program headers instead of the contents of
448 one program header.
449
450 Return vector of bytes holding the program header contents, or an empty
451 optional on failure. If successful and P_ARCH_SIZE is non-NULL, the target
452 architecture size (32-bit or 64-bit) is returned to *P_ARCH_SIZE. Likewise,
453 the base address of the section is returned in *BASE_ADDR. */
454
455static gdb::optional<gdb::byte_vector>
456read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
457{
458 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
459 CORE_ADDR at_phdr, at_phent, at_phnum, pt_phdr = 0;
460 int arch_size, sect_size;
461 CORE_ADDR sect_addr;
462 int pt_phdr_p = 0;
463
464 /* Get required auxv elements from target. */
465 if (target_auxv_search (AT_PHDR, &at_phdr) <= 0)
466 return {};
467 if (target_auxv_search (AT_PHENT, &at_phent) <= 0)
468 return {};
469 if (target_auxv_search (AT_PHNUM, &at_phnum) <= 0)
470 return {};
471 if (!at_phdr || !at_phnum)
472 return {};
473
474 /* Determine ELF architecture type. */
475 if (at_phent == sizeof (Elf32_External_Phdr))
476 arch_size = 32;
477 else if (at_phent == sizeof (Elf64_External_Phdr))
478 arch_size = 64;
479 else
480 return {};
481
482 /* Find the requested segment. */
483 if (type == -1)
484 {
485 sect_addr = at_phdr;
486 sect_size = at_phent * at_phnum;
487 }
488 else if (arch_size == 32)
489 {
490 Elf32_External_Phdr phdr;
491 int i;
492
493 /* Search for requested PHDR. */
494 for (i = 0; i < at_phnum; i++)
495 {
496 int p_type;
497
498 if (target_read_memory (at_phdr + i * sizeof (phdr),
499 (gdb_byte *)&phdr, sizeof (phdr)))
500 return {};
501
502 p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
503 4, byte_order);
504
505 if (p_type == PT_PHDR)
506 {
507 pt_phdr_p = 1;
508 pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
509 4, byte_order);
510 }
511
512 if (p_type == type)
513 break;
514 }
515
516 if (i == at_phnum)
517 return {};
518
519 /* Retrieve address and size. */
520 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
521 4, byte_order);
522 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
523 4, byte_order);
524 }
525 else
526 {
527 Elf64_External_Phdr phdr;
528 int i;
529
530 /* Search for requested PHDR. */
531 for (i = 0; i < at_phnum; i++)
532 {
533 int p_type;
534
535 if (target_read_memory (at_phdr + i * sizeof (phdr),
536 (gdb_byte *)&phdr, sizeof (phdr)))
537 return {};
538
539 p_type = extract_unsigned_integer ((gdb_byte *) phdr.p_type,
540 4, byte_order);
541
542 if (p_type == PT_PHDR)
543 {
544 pt_phdr_p = 1;
545 pt_phdr = extract_unsigned_integer ((gdb_byte *) phdr.p_vaddr,
546 8, byte_order);
547 }
548
549 if (p_type == type)
550 break;
551 }
552
553 if (i == at_phnum)
554 return {};
555
556 /* Retrieve address and size. */
557 sect_addr = extract_unsigned_integer ((gdb_byte *)phdr.p_vaddr,
558 8, byte_order);
559 sect_size = extract_unsigned_integer ((gdb_byte *)phdr.p_memsz,
560 8, byte_order);
561 }
562
563 /* PT_PHDR is optional, but we really need it
564 for PIE to make this work in general. */
565
566 if (pt_phdr_p)
567 {
568 /* at_phdr is real address in memory. pt_phdr is what pheader says it is.
569 Relocation offset is the difference between the two. */
570 sect_addr = sect_addr + (at_phdr - pt_phdr);
571 }
572
573 /* Read in requested program header. */
574 gdb::byte_vector buf (sect_size);
575 if (target_read_memory (sect_addr, buf.data (), sect_size))
576 return {};
577
578 if (p_arch_size)
579 *p_arch_size = arch_size;
580 if (base_addr)
581 *base_addr = sect_addr;
582
583 return buf;
584}
585
586
587/* Return program interpreter string. */
588static gdb::optional<gdb::byte_vector>
590{
591 /* If we have a current exec_bfd, use its section table. */
593 && (bfd_get_flavour (current_program_space->exec_bfd ())
594 == bfd_target_elf_flavour))
595 {
596 struct bfd_section *interp_sect;
597
598 interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
599 ".interp");
600 if (interp_sect != NULL)
601 {
602 int sect_size = bfd_section_size (interp_sect);
603
604 gdb::byte_vector buf (sect_size);
605 bool res
606 = bfd_get_section_contents (current_program_space->exec_bfd (),
607 interp_sect, buf.data (), 0, sect_size);
608 if (res)
609 return buf;
610 }
611 }
612
613 /* If we didn't find it, use the target auxiliary vector. */
614 return read_program_header (PT_INTERP, NULL, NULL);
615}
616
617
618/* Scan for DESIRED_DYNTAG in .dynamic section of the target's main executable,
619 found by consulting the OS auxillary vector. If DESIRED_DYNTAG is found, 1
620 is returned and the corresponding PTR is set. */
621
622static int
623scan_dyntag_auxv (const int desired_dyntag, CORE_ADDR *ptr,
624 CORE_ADDR *ptr_addr)
625{
626 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
627 int arch_size, step;
628 long current_dyntag;
629 CORE_ADDR dyn_ptr;
630 CORE_ADDR base_addr;
631
632 /* Read in .dynamic section. */
633 gdb::optional<gdb::byte_vector> ph_data
634 = read_program_header (PT_DYNAMIC, &arch_size, &base_addr);
635 if (!ph_data)
636 return 0;
637
638 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
639 step = (arch_size == 32) ? sizeof (Elf32_External_Dyn)
640 : sizeof (Elf64_External_Dyn);
641 for (gdb_byte *buf = ph_data->data (), *bufend = buf + ph_data->size ();
642 buf < bufend; buf += step)
643 {
644 if (arch_size == 32)
645 {
646 Elf32_External_Dyn *dynp = (Elf32_External_Dyn *) buf;
647
648 current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
649 4, byte_order);
650 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
651 4, byte_order);
652 }
653 else
654 {
655 Elf64_External_Dyn *dynp = (Elf64_External_Dyn *) buf;
656
657 current_dyntag = extract_unsigned_integer ((gdb_byte *) dynp->d_tag,
658 8, byte_order);
659 dyn_ptr = extract_unsigned_integer ((gdb_byte *) dynp->d_un.d_ptr,
660 8, byte_order);
661 }
662 if (current_dyntag == DT_NULL)
663 break;
664
665 if (current_dyntag == desired_dyntag)
666 {
667 if (ptr)
668 *ptr = dyn_ptr;
669
670 if (ptr_addr)
671 *ptr_addr = base_addr + buf - ph_data->data ();
672
673 return 1;
674 }
675 }
676
677 return 0;
678}
679
680/* Locate the base address of dynamic linker structs for SVR4 elf
681 targets.
682
683 For SVR4 elf targets the address of the dynamic linker's runtime
684 structure is contained within the dynamic info section in the
685 executable file. The dynamic section is also mapped into the
686 inferior address space. Because the runtime loader fills in the
687 real address before starting the inferior, we have to read in the
688 dynamic info section from the inferior address space.
689 If there are any errors while trying to find the address, we
690 silently return 0, otherwise the found address is returned. */
691
692static CORE_ADDR
694{
695 struct bound_minimal_symbol msymbol;
696 CORE_ADDR dyn_ptr, dyn_ptr_addr;
697
699 return 0;
700
701 /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
702 instead of DT_DEBUG, although they sometimes contain an unused
703 DT_DEBUG. */
704 if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP,
706 &dyn_ptr, NULL)
707 || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
708 {
709 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
710 gdb_byte *pbuf;
711 int pbuf_size = ptr_type->length ();
712
713 pbuf = (gdb_byte *) alloca (pbuf_size);
714 /* DT_MIPS_RLD_MAP contains a pointer to the address
715 of the dynamic link structure. */
716 if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
717 return 0;
718 return extract_typed_address (pbuf, ptr_type);
719 }
720
721 /* Then check DT_MIPS_RLD_MAP_REL. MIPS executables now use this form
722 because of needing to support PIE. DT_MIPS_RLD_MAP will also exist
723 in non-PIE. */
724 if (gdb_bfd_scan_elf_dyntag (DT_MIPS_RLD_MAP_REL,
726 &dyn_ptr, &dyn_ptr_addr)
727 || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
728 {
729 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
730 gdb_byte *pbuf;
731 int pbuf_size = ptr_type->length ();
732
733 pbuf = (gdb_byte *) alloca (pbuf_size);
734 /* DT_MIPS_RLD_MAP_REL contains an offset from the address of the
735 DT slot to the address of the dynamic link structure. */
736 if (target_read_memory (dyn_ptr + dyn_ptr_addr, pbuf, pbuf_size))
737 return 0;
738 return extract_typed_address (pbuf, ptr_type);
739 }
740
741 /* Find DT_DEBUG. */
743 &dyn_ptr, NULL)
744 || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
745 return dyn_ptr;
746
747 /* This may be a static executable. Look for the symbol
748 conventionally named _r_debug, as a last resort. */
749 msymbol = lookup_minimal_symbol ("_r_debug", NULL,
751 if (msymbol.minsym != NULL)
752 return msymbol.value_address ();
753
754 /* DT_DEBUG entry not found. */
755 return 0;
756}
757
758/* Find the first element in the inferior's dynamic link map, and
759 return its address in the inferior. Return zero if the address
760 could not be determined.
761
762 FIXME: Perhaps we should validate the info somehow, perhaps by
763 checking r_version for a known version number, or r_state for
764 RT_CONSISTENT. */
765
766static CORE_ADDR
767solib_svr4_r_map (CORE_ADDR debug_base)
768{
770 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
771 CORE_ADDR addr = 0;
772
773 try
774 {
775 addr = read_memory_typed_address (debug_base + lmo->r_map_offset,
776 ptr_type);
777 }
778 catch (const gdb_exception_error &ex)
779 {
781 }
782
783 return addr;
784}
785
786/* Find r_brk from the inferior's debug base. */
787
788static CORE_ADDR
790{
792 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
793
794 return read_memory_typed_address (info->debug_base + lmo->r_brk_offset,
795 ptr_type);
796}
797
798/* Find the link map for the dynamic linker (if it is not in the
799 normal list of loaded shared objects). */
800
801static CORE_ADDR
803{
805 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
806 enum bfd_endian byte_order = type_byte_order (ptr_type);
807 ULONGEST version = 0;
808
809 try
810 {
811 /* Check version, and return zero if `struct r_debug' doesn't have
812 the r_ldsomap member. */
813 version
814 = read_memory_unsigned_integer (info->debug_base + lmo->r_version_offset,
815 lmo->r_version_size, byte_order);
816 }
817 catch (const gdb_exception_error &ex)
818 {
820 }
821
822 if (version < 2 || lmo->r_ldsomap_offset == -1)
823 return 0;
824
825 return read_memory_typed_address (info->debug_base + lmo->r_ldsomap_offset,
826 ptr_type);
827}
828
829/* Find the next namespace from the r_next field. */
830
831static CORE_ADDR
832solib_svr4_r_next (CORE_ADDR debug_base)
833{
836 bfd_endian byte_order = type_byte_order (ptr_type);
837 ULONGEST version = 0;
838
839 try
840 {
841 version
843 lmo->r_version_size, byte_order);
844 }
845 catch (const gdb_exception_error &ex)
846 {
848 }
849
850 /* The r_next field is added with r_version == 2. */
851 if (version < 2 || lmo->r_next_offset == -1)
852 return 0;
853
854 return read_memory_typed_address (debug_base + lmo->r_next_offset,
855 ptr_type);
856}
857
858/* On Solaris systems with some versions of the dynamic linker,
859 ld.so's l_name pointer points to the SONAME in the string table
860 rather than into writable memory. So that GDB can find shared
861 libraries when loading a core file generated by gcore, ensure that
862 memory areas containing the l_name string are saved in the core
863 file. */
864
865static int
866svr4_keep_data_in_core (CORE_ADDR vaddr, unsigned long size)
867{
868 struct svr4_info *info;
869 CORE_ADDR ldsomap;
870 CORE_ADDR name_lm;
871
873
874 info->debug_base = elf_locate_base ();
875 if (info->debug_base == 0)
876 return 0;
877
878 ldsomap = solib_svr4_r_ldsomap (info);
879 if (!ldsomap)
880 return 0;
881
882 std::unique_ptr<lm_info_svr4> li = lm_info_read (ldsomap);
883 name_lm = li != NULL ? li->l_name : 0;
884
885 return (name_lm >= vaddr && name_lm < vaddr + size);
886}
887
888/* See solist.h. */
889
890static int
892{
893 CORE_ADDR lm, l_name;
895 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
896 int l_name_size = ptr_type->length ();
897 gdb::byte_vector l_name_buf (l_name_size);
899 symfile_add_flags add_flags = 0;
900
901 if (from_tty)
902 add_flags |= SYMFILE_VERBOSE;
903
905 if (!query (_("Attempt to reload symbols from process? ")))
906 return 0;
907
908 /* Always locate the debug struct, in case it has moved. */
909 info->debug_base = elf_locate_base ();
910 if (info->debug_base == 0)
911 return 0; /* failed somehow... */
912
913 /* First link map member should be the executable. */
914 lm = solib_svr4_r_map (info->debug_base);
915 if (lm == 0)
916 return 0; /* failed somehow... */
917
918 /* Read address of name from target memory to GDB. */
919 read_memory (lm + lmo->l_name_offset, l_name_buf.data (), l_name_size);
920
921 /* Convert the address to host format. */
922 l_name = extract_typed_address (l_name_buf.data (), ptr_type);
923
924 if (l_name == 0)
925 return 0; /* No filename. */
926
927 /* Now fetch the filename from target memory. */
928 gdb::unique_xmalloc_ptr<char> filename
930
931 if (filename == nullptr)
932 {
933 warning (_("failed to read exec filename from attached file"));
934 return 0;
935 }
936
937 /* Have a pathname: read the symbol file. */
938 symbol_file_add_main (filename.get (), add_flags);
939
940 return 1;
941}
942
943/* Data exchange structure for the XML parser as returned by
944 svr4_current_sos_via_xfer_libraries. */
945
947{
948 /* The tail pointer of the current namespace. This is internal to XML
949 parsing. */
951
952 /* Inferior address of struct link_map used for the main executable. It is
953 NULL if not known. */
954 CORE_ADDR main_lm;
955
956 /* List of objects loaded into the inferior per namespace. This does
957 not include any default sos.
958
959 See comment on struct svr4_info.solib_lists. */
960 std::map<CORE_ADDR, so_list *> solib_lists;
961};
962
963/* This module's 'free_objfile' observer. */
964
965static void
970
971/* Implementation for target_so_ops.free_so. */
972
973static void
975{
976 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
977
978 delete li;
979}
980
981/* Implement target_so_ops.clear_so. */
982
983static void
985{
986 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
987
988 if (li != NULL)
989 li->l_addr_p = 0;
990}
991
992/* Free so_list built so far. */
993
994static void
996{
997 while (list != NULL)
998 {
999 struct so_list *next = list->next;
1000
1001 free_so (list);
1002 list = next;
1003 }
1004}
1005
1006/* Copy library list. */
1007
1008static struct so_list *
1010{
1011 struct so_list *dst = NULL;
1012 struct so_list **link = &dst;
1013
1014 while (src != NULL)
1015 {
1016 struct so_list *newobj;
1017
1018 newobj = XNEW (struct so_list);
1019 memcpy (newobj, src, sizeof (struct so_list));
1020
1021 lm_info_svr4 *src_li = (lm_info_svr4 *) src->lm_info;
1022 newobj->lm_info = new lm_info_svr4 (*src_li);
1023
1024 newobj->next = NULL;
1025 *link = newobj;
1026 link = &newobj->next;
1027
1028 src = src->next;
1029 }
1030
1031 return dst;
1032}
1033
1034#ifdef HAVE_LIBEXPAT
1035
1036#include "xml-support.h"
1037
1038/* Handle the start of a <library> element. Note: new elements are added
1039 at the tail of the list, keeping the list in order. */
1040
1041static void
1042library_list_start_library (struct gdb_xml_parser *parser,
1043 const struct gdb_xml_element *element,
1044 void *user_data,
1045 std::vector<gdb_xml_value> &attributes)
1046{
1047 struct svr4_library_list *list = (struct svr4_library_list *) user_data;
1048 const char *name
1049 = (const char *) xml_find_attribute (attributes, "name")->value.get ();
1050 ULONGEST *lmp
1051 = (ULONGEST *) xml_find_attribute (attributes, "lm")->value.get ();
1052 ULONGEST *l_addrp
1053 = (ULONGEST *) xml_find_attribute (attributes, "l_addr")->value.get ();
1054 ULONGEST *l_ldp
1055 = (ULONGEST *) xml_find_attribute (attributes, "l_ld")->value.get ();
1056 struct so_list *new_elem;
1057
1058 new_elem = XCNEW (struct so_list);
1059 lm_info_svr4 *li = new lm_info_svr4;
1060 new_elem->lm_info = li;
1061 li->lm_addr = *lmp;
1062 li->l_addr_inferior = *l_addrp;
1063 li->l_ld = *l_ldp;
1064
1065 strncpy (new_elem->so_name, name, sizeof (new_elem->so_name) - 1);
1066 new_elem->so_name[sizeof (new_elem->so_name) - 1] = 0;
1067 strcpy (new_elem->so_original_name, new_elem->so_name);
1068
1069 /* Older versions did not supply lmid. Put the element into the flat
1070 list of the special namespace zero in that case. */
1071 gdb_xml_value *at_lmid = xml_find_attribute (attributes, "lmid");
1072 if (at_lmid == nullptr)
1073 {
1074 *list->tailp = new_elem;
1075 list->tailp = &new_elem->next;
1076 }
1077 else
1078 {
1079 ULONGEST lmid = *(ULONGEST *) at_lmid->value.get ();
1080
1081 /* Ensure that the element is actually initialized. */
1082 if (list->solib_lists.find (lmid) == list->solib_lists.end ())
1083 list->solib_lists[lmid] = nullptr;
1084
1085 so_list **psolist = &list->solib_lists[lmid];
1086 so_list **pnext = psolist;
1087
1088 /* Walk to the end of the list if we have one. */
1089 so_list *solist = *psolist;
1090 if (solist != nullptr)
1091 {
1092 for (; solist->next != nullptr; solist = solist->next)
1093 /* Nothing. */;
1094
1095 pnext = &solist->next;
1096 }
1097
1098 *pnext = new_elem;
1099 }
1100}
1101
1102/* Handle the start of a <library-list-svr4> element. */
1103
1104static void
1105svr4_library_list_start_list (struct gdb_xml_parser *parser,
1106 const struct gdb_xml_element *element,
1107 void *user_data,
1108 std::vector<gdb_xml_value> &attributes)
1109{
1110 struct svr4_library_list *list = (struct svr4_library_list *) user_data;
1111 const char *version
1112 = (const char *) xml_find_attribute (attributes, "version")->value.get ();
1113 struct gdb_xml_value *main_lm = xml_find_attribute (attributes, "main-lm");
1114
1115 if (strcmp (version, "1.0") != 0)
1116 gdb_xml_error (parser,
1117 _("SVR4 Library list has unsupported version \"%s\""),
1118 version);
1119
1120 if (main_lm)
1121 list->main_lm = *(ULONGEST *) main_lm->value.get ();
1122
1123 /* Older gdbserver do not support namespaces. We use the special
1124 namespace zero for a linear list of libraries. */
1125 so_list **solist = &list->solib_lists[0];
1126 *solist = nullptr;
1127 list->tailp = solist;
1128}
1129
1130/* The allowed elements and attributes for an XML library list.
1131 The root element is a <library-list>. */
1132
1133static const struct gdb_xml_attribute svr4_library_attributes[] =
1134{
1135 { "name", GDB_XML_AF_NONE, NULL, NULL },
1137 { "l_addr", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
1140 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1141};
1142
1143static const struct gdb_xml_element svr4_library_list_children[] =
1144{
1145 {
1146 "library", svr4_library_attributes, NULL,
1148 library_list_start_library, NULL
1149 },
1150 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1151};
1152
1153static const struct gdb_xml_attribute svr4_library_list_attributes[] =
1154{
1155 { "version", GDB_XML_AF_NONE, NULL, NULL },
1157 { NULL, GDB_XML_AF_NONE, NULL, NULL }
1158};
1159
1160static const struct gdb_xml_element svr4_library_list_elements[] =
1161{
1162 { "library-list-svr4", svr4_library_list_attributes, svr4_library_list_children,
1163 GDB_XML_EF_NONE, svr4_library_list_start_list, NULL },
1164 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
1165};
1166
1167/* Parse qXfer:libraries:read packet into *SO_LIST_RETURN. Return 1 if
1168
1169 Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
1170 case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
1171 empty, caller is responsible for freeing all its entries. */
1172
1173static int
1174svr4_parse_libraries (const char *document, struct svr4_library_list *list)
1175{
1176 auto cleanup = make_scope_exit ([list] ()
1177 {
1178 for (const std::pair<CORE_ADDR, so_list *> tuple
1179 : list->solib_lists)
1180 svr4_free_library_list (tuple.second);
1181 });
1182
1183 list->tailp = nullptr;
1184 list->main_lm = 0;
1185 list->solib_lists.clear ();
1186 if (gdb_xml_parse_quick (_("target library list"), "library-list-svr4.dtd",
1187 svr4_library_list_elements, document, list) == 0)
1188 {
1189 /* Parsed successfully, keep the result. */
1190 cleanup.release ();
1191 return 1;
1192 }
1193
1194 return 0;
1195}
1196
1197/* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
1198
1199 Return 0 if packet not supported, *SO_LIST_RETURN is not modified in such
1200 case. Return 1 if *SO_LIST_RETURN contains the library list, it may be
1201 empty, caller is responsible for freeing all its entries.
1202
1203 Note that ANNEX must be NULL if the remote does not explicitly allow
1204 qXfer:libraries-svr4:read packets with non-empty annexes. Support for
1205 this can be checked using target_augmented_libraries_svr4_read (). */
1206
1207static int
1209 const char *annex)
1210{
1211 gdb_assert (annex == NULL || target_augmented_libraries_svr4_read ());
1212
1213 /* Fetch the list of shared libraries. */
1214 gdb::optional<gdb::char_vector> svr4_library_document
1215 = target_read_stralloc (current_inferior ()->top_target (),
1217 annex);
1218 if (!svr4_library_document)
1219 return 0;
1220
1221 return svr4_parse_libraries (svr4_library_document->data (), list);
1222}
1223
1224#else
1225
1226static int
1228 const char *annex)
1229{
1230 return 0;
1231}
1232
1233#endif
1234
1235/* If no shared library information is available from the dynamic
1236 linker, build a fallback list from other sources. */
1237
1238static struct so_list *
1240{
1241 struct so_list *newobj;
1242
1243 if (!info->debug_loader_offset_p)
1244 return NULL;
1245
1246 newobj = XCNEW (struct so_list);
1247 lm_info_svr4 *li = new lm_info_svr4;
1248 newobj->lm_info = li;
1249
1250 /* Nothing will ever check the other fields if we set l_addr_p. */
1251 li->l_addr = li->l_addr_inferior = info->debug_loader_offset;
1252 li->l_addr_p = 1;
1253
1254 strncpy (newobj->so_name, info->debug_loader_name, SO_NAME_MAX_PATH_SIZE - 1);
1255 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1256 strcpy (newobj->so_original_name, newobj->so_name);
1257
1258 return newobj;
1259}
1260
1261/* Read the whole inferior libraries chain starting at address LM.
1262 Expect the first entry in the chain's previous entry to be PREV_LM.
1263 Add the entries to the tail referenced by LINK_PTR_PTR. Ignore the
1264 first entry if IGNORE_FIRST and set global MAIN_LM_ADDR according
1265 to it. Returns nonzero upon success. If zero is returned the
1266 entries stored to LINK_PTR_PTR are still valid although they may
1267 represent only part of the inferior library list. */
1268
1269static int
1270svr4_read_so_list (svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm,
1271 struct so_list ***link_ptr_ptr, int ignore_first)
1272{
1273 CORE_ADDR first_l_name = 0;
1274 CORE_ADDR next_lm;
1275
1276 for (; lm != 0; prev_lm = lm, lm = next_lm)
1277 {
1278 so_list_up newobj (XCNEW (struct so_list));
1279
1280 lm_info_svr4 *li = lm_info_read (lm).release ();
1281 newobj->lm_info = li;
1282 if (li == NULL)
1283 return 0;
1284
1285 next_lm = li->l_next;
1286
1287 if (li->l_prev != prev_lm)
1288 {
1289 warning (_("Corrupted shared library list: %s != %s"),
1290 paddress (target_gdbarch (), prev_lm),
1291 paddress (target_gdbarch (), li->l_prev));
1292 return 0;
1293 }
1294
1295 /* For SVR4 versions, the first entry in the link map is for the
1296 inferior executable, so we must ignore it. For some versions of
1297 SVR4, it has no name. For others (Solaris 2.3 for example), it
1298 does have a name, so we can no longer use a missing name to
1299 decide when to ignore it. */
1300 if (ignore_first && li->l_prev == 0)
1301 {
1302 first_l_name = li->l_name;
1303 info->main_lm_addr = li->lm_addr;
1304 continue;
1305 }
1306
1307 /* Extract this shared object's name. */
1308 gdb::unique_xmalloc_ptr<char> buffer
1310 if (buffer == nullptr)
1311 {
1312 /* If this entry's l_name address matches that of the
1313 inferior executable, then this is not a normal shared
1314 object, but (most likely) a vDSO. In this case, silently
1315 skip it; otherwise emit a warning. */
1316 if (first_l_name == 0 || li->l_name != first_l_name)
1317 warning (_("Can't read pathname for load map."));
1318 continue;
1319 }
1320
1321 strncpy (newobj->so_name, buffer.get (), SO_NAME_MAX_PATH_SIZE - 1);
1322 newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1323 strcpy (newobj->so_original_name, newobj->so_name);
1324
1325 /* If this entry has no name, or its name matches the name
1326 for the main executable, don't include it in the list. */
1327 if (! newobj->so_name[0] || match_main (newobj->so_name))
1328 continue;
1329
1330 newobj->next = 0;
1331 /* Don't free it now. */
1332 **link_ptr_ptr = newobj.release ();
1333 *link_ptr_ptr = &(**link_ptr_ptr)->next;
1334 }
1335
1336 return 1;
1337}
1338
1339/* Read the full list of currently loaded shared objects directly
1340 from the inferior, without referring to any libraries read and
1341 stored by the probes interface. Handle special cases relating
1342 to the first elements of the list in default namespace. */
1343
1344static void
1346{
1347 CORE_ADDR lm;
1348 bool ignore_first;
1349 struct svr4_library_list library_list;
1350
1351 /* Remove any old libraries. We're going to read them back in again. */
1352 free_solib_lists (info);
1353
1354 /* Fall back to manual examination of the target if the packet is not
1355 supported or gdbserver failed to find DT_DEBUG. gdb.server/solib-list.exp
1356 tests a case where gdbserver cannot find the shared libraries list while
1357 GDB itself is able to find it via SYMFILE_OBJFILE.
1358
1359 Unfortunately statically linked inferiors will also fall back through this
1360 suboptimal code path. */
1361
1362 info->using_xfer = svr4_current_sos_via_xfer_libraries (&library_list,
1363 NULL);
1364 if (info->using_xfer)
1365 {
1366 if (library_list.main_lm)
1367 info->main_lm_addr = library_list.main_lm;
1368
1369 /* Remove an empty special zero namespace so we know that when there
1370 is one, it is actually used, and we have a flat list without
1371 namespace information. */
1372 if ((library_list.solib_lists.find (0)
1373 != library_list.solib_lists.end ())
1374 && (library_list.solib_lists[0] == nullptr))
1375 library_list.solib_lists.erase (0);
1376
1377 /* Replace the (empty) solib_lists in INFO with the one generated
1378 from the target. We don't want to copy it on assignment and then
1379 delete the original afterwards, so let's just swap the
1380 internals. */
1381 std::swap (info->solib_lists, library_list.solib_lists);
1382 return;
1383 }
1384
1385 /* If we can't find the dynamic linker's base structure, this
1386 must not be a dynamically linked executable. Hmm. */
1387 info->debug_base = elf_locate_base ();
1388 if (info->debug_base == 0)
1389 return;
1390
1391 /* Assume that everything is a library if the dynamic loader was loaded
1392 late by a static executable. */
1394 && bfd_get_section_by_name (current_program_space->exec_bfd (),
1395 ".dynamic") == NULL)
1396 ignore_first = false;
1397 else
1398 ignore_first = true;
1399
1400 auto cleanup = make_scope_exit ([info] ()
1401 {
1402 free_solib_lists (info);
1403 });
1404
1405 /* Collect the sos in each namespace. */
1406 CORE_ADDR debug_base = info->debug_base;
1407 for (; debug_base != 0;
1408 ignore_first = false, debug_base = solib_svr4_r_next (debug_base))
1409 {
1410 /* Walk the inferior's link map list, and build our so_list list. */
1411 lm = solib_svr4_r_map (debug_base);
1412 if (lm != 0)
1413 {
1414 so_list **sos = &info->solib_lists[debug_base];
1415 *sos = nullptr;
1416
1417 svr4_read_so_list (info, lm, 0, &sos, ignore_first);
1418 }
1419 }
1420
1421 /* On Solaris, the dynamic linker is not in the normal list of
1422 shared objects, so make sure we pick it up too. Having
1423 symbol information for the dynamic linker is quite crucial
1424 for skipping dynamic linker resolver code.
1425
1426 Note that we interpret the ldsomap load map address as 'virtual'
1427 r_debug object. If we added it to the default namespace (as it was),
1428 we would probably run into inconsistencies with the load map's
1429 prev/next links (I wonder if we did). */
1430 debug_base = solib_svr4_r_ldsomap (info);
1431 if (debug_base != 0)
1432 {
1433 /* Add the dynamic linker's namespace unless we already did. */
1434 if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
1435 {
1436 so_list **sos = &info->solib_lists[debug_base];
1437 *sos = nullptr;
1438 svr4_read_so_list (info, debug_base, 0, &sos, 0);
1439 }
1440 }
1441
1442 cleanup.release ();
1443}
1444
1445/* Collect sos read and stored by the probes interface. */
1446
1447static so_list *
1449{
1450 so_list *sos = nullptr;
1451 so_list **pnext = &sos;
1452
1453 for (const std::pair<CORE_ADDR, so_list *> tuple
1454 : info->solib_lists)
1455 {
1456 so_list *solist = tuple.second;
1457
1458 /* Allow the linker to report empty namespaces. */
1459 if (solist == nullptr)
1460 continue;
1461
1462 *pnext = svr4_copy_library_list (solist);
1463
1464 /* Update PNEXT to point to the next member of the last element. */
1465 gdb_assert (*pnext != nullptr);
1466 for (;;)
1467 {
1468 so_list *next = *pnext;
1469 if (next == nullptr)
1470 break;
1471
1472 pnext = &next->next;
1473 }
1474 }
1475
1476 return sos;
1477}
1478
1479/* Implement the main part of the "current_sos" target_so_ops
1480 method. */
1481
1482static struct so_list *
1484{
1485 so_list *sos = nullptr;
1486
1487 /* If we're using the probes interface, we can use the cache as it will
1488 be maintained by probe update/reload actions. */
1489 if (info->probes_table != nullptr)
1490 sos = svr4_collect_probes_sos (info);
1491
1492 /* If we're not using the probes interface or if we didn't cache
1493 anything, read the sos to fill the cache, then collect them from the
1494 cache. */
1495 if (sos == nullptr)
1496 {
1498
1499 sos = svr4_collect_probes_sos (info);
1500 if (sos == nullptr)
1501 sos = svr4_default_sos (info);
1502 }
1503
1504 return sos;
1505}
1506
1507/* Implement the "current_sos" target_so_ops method. */
1508
1509static struct so_list *
1511{
1513 struct so_list *so_head = svr4_current_sos_1 (info);
1514 struct mem_range vsyscall_range;
1515
1516 /* Filter out the vDSO module, if present. Its symbol file would
1517 not be found on disk. The vDSO/vsyscall's OBJFILE is instead
1518 managed by symfile-mem.c:add_vsyscall_page. */
1519 if (gdbarch_vsyscall_range (target_gdbarch (), &vsyscall_range)
1520 && vsyscall_range.length != 0)
1521 {
1522 struct so_list **sop;
1523
1524 sop = &so_head;
1525 while (*sop != NULL)
1526 {
1527 struct so_list *so = *sop;
1528
1529 /* We can't simply match the vDSO by starting address alone,
1530 because lm_info->l_addr_inferior (and also l_addr) do not
1531 necessarily represent the real starting address of the
1532 ELF if the vDSO's ELF itself is "prelinked". The l_ld
1533 field (the ".dynamic" section of the shared object)
1534 always points at the absolute/resolved address though.
1535 So check whether that address is inside the vDSO's
1536 mapping instead.
1537
1538 E.g., on Linux 3.16 (x86_64) the vDSO is a regular
1539 0-based ELF, and we see:
1540
1541 (gdb) info auxv
1542 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffb000
1543 (gdb) p/x *_r_debug.r_map.l_next
1544 $1 = {l_addr = 0x7ffff7ffb000, ..., l_ld = 0x7ffff7ffb318, ...}
1545
1546 And on Linux 2.6.32 (x86_64) we see:
1547
1548 (gdb) info auxv
1549 33 AT_SYSINFO_EHDR System-supplied DSO's ELF header 0x7ffff7ffe000
1550 (gdb) p/x *_r_debug.r_map.l_next
1551 $5 = {l_addr = 0x7ffff88fe000, ..., l_ld = 0x7ffff7ffe580, ... }
1552
1553 Dumping that vDSO shows:
1554
1555 (gdb) info proc mappings
1556 0x7ffff7ffe000 0x7ffff7fff000 0x1000 0 [vdso]
1557 (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
1558 # readelf -Wa vdso.bin
1559 [...]
1560 Entry point address: 0xffffffffff700700
1561 [...]
1562 Section Headers:
1563 [Nr] Name Type Address Off Size
1564 [ 0] NULL 0000000000000000 000000 000000
1565 [ 1] .hash HASH ffffffffff700120 000120 000038
1566 [ 2] .dynsym DYNSYM ffffffffff700158 000158 0000d8
1567 [...]
1568 [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
1569 */
1570
1571 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1572
1573 if (address_in_mem_range (li->l_ld, &vsyscall_range))
1574 {
1575 *sop = so->next;
1576 free_so (so);
1577 break;
1578 }
1579
1580 sop = &so->next;
1581 }
1582 }
1583
1584 return so_head;
1585}
1586
1587/* Get the address of the link_map for a given OBJFILE. */
1588
1589CORE_ADDR
1591{
1592 struct svr4_info *info = get_svr4_info (objfile->pspace);
1593
1594 /* Cause svr4_current_sos() to be run if it hasn't been already. */
1595 if (info->main_lm_addr == 0)
1596 solib_add (NULL, 0, auto_solib_add);
1597
1598 /* svr4_current_sos() will set main_lm_addr for the main executable. */
1600 return info->main_lm_addr;
1601
1602 /* The other link map addresses may be found by examining the list
1603 of shared libraries. */
1604 for (struct so_list *so : current_program_space->solibs ())
1605 if (so->objfile == objfile)
1606 {
1607 lm_info_svr4 *li = (lm_info_svr4 *) so->lm_info;
1608
1609 return li->lm_addr;
1610 }
1611
1612 /* Not found! */
1613 return 0;
1614}
1615
1616/* On some systems, the only way to recognize the link map entry for
1617 the main executable file is by looking at its name. Return
1618 non-zero iff SONAME matches one of the known main executable names. */
1619
1620static int
1621match_main (const char *soname)
1622{
1623 const char * const *mainp;
1624
1625 for (mainp = main_name_list; *mainp != NULL; mainp++)
1626 {
1627 if (strcmp (soname, *mainp) == 0)
1628 return (1);
1629 }
1630
1631 return (0);
1632}
1633
1634/* Return 1 if PC lies in the dynamic symbol resolution code of the
1635 SVR4 run time loader. */
1636
1637int
1639{
1641
1642 return ((pc >= info->interp_text_sect_low
1643 && pc < info->interp_text_sect_high)
1644 || (pc >= info->interp_plt_sect_low
1645 && pc < info->interp_plt_sect_high)
1646 || in_plt_section (pc)
1647 || in_gnu_ifunc_stub (pc));
1648}
1649
1650/* Given an executable's ABFD and target, compute the entry-point
1651 address. */
1652
1653static CORE_ADDR
1654exec_entry_point (struct bfd *abfd, struct target_ops *targ)
1655{
1656 CORE_ADDR addr;
1657
1658 /* KevinB wrote ... for most targets, the address returned by
1659 bfd_get_start_address() is the entry point for the start
1660 function. But, for some targets, bfd_get_start_address() returns
1661 the address of a function descriptor from which the entry point
1662 address may be extracted. This address is extracted by
1663 gdbarch_convert_from_func_ptr_addr(). The method
1664 gdbarch_convert_from_func_ptr_addr() is the merely the identify
1665 function for targets which don't use function descriptors. */
1667 bfd_get_start_address (abfd),
1668 targ);
1669 return gdbarch_addr_bits_remove (target_gdbarch (), addr);
1670}
1671
1672/* A probe and its associated action. */
1673
1675{
1676 /* The probe. */
1678
1679 /* The relocated address of the probe. */
1680 CORE_ADDR address;
1681
1682 /* The action. */
1684
1685 /* The objfile where this probe was found. */
1687};
1688
1689/* Returns a hash code for the probe_and_action referenced by p. */
1690
1691static hashval_t
1693{
1694 const struct probe_and_action *pa = (const struct probe_and_action *) p;
1695
1696 return (hashval_t) pa->address;
1697}
1698
1699/* Returns non-zero if the probe_and_actions referenced by p1 and p2
1700 are equal. */
1701
1702static int
1703equal_probe_and_action (const void *p1, const void *p2)
1704{
1705 const struct probe_and_action *pa1 = (const struct probe_and_action *) p1;
1706 const struct probe_and_action *pa2 = (const struct probe_and_action *) p2;
1707
1708 return pa1->address == pa2->address;
1709}
1710
1711/* Traversal function for probes_table_remove_objfile_probes. */
1712
1713static int
1715{
1716 probe_and_action *pa = (probe_and_action *) *slot;
1717 struct objfile *objfile = (struct objfile *) info;
1718
1719 if (pa->objfile == objfile)
1720 htab_clear_slot (get_svr4_info (objfile->pspace)->probes_table.get (),
1721 slot);
1722
1723 return 1;
1724}
1725
1726/* Remove all probes that belong to OBJFILE from the probes table. */
1727
1728static void
1730{
1732 if (info->probes_table != nullptr)
1733 htab_traverse_noresize (info->probes_table.get (),
1735}
1736
1737/* Register a solib event probe and its associated action in the
1738 probes table. */
1739
1740static void
1742 probe *prob, CORE_ADDR address,
1743 enum probe_action action)
1744{
1745 struct probe_and_action lookup, *pa;
1746 void **slot;
1747
1748 /* Create the probes table, if necessary. */
1749 if (info->probes_table == NULL)
1750 info->probes_table.reset (htab_create_alloc (1, hash_probe_and_action,
1752 xfree, xcalloc, xfree));
1753
1754 lookup.address = address;
1755 slot = htab_find_slot (info->probes_table.get (), &lookup, INSERT);
1756 gdb_assert (*slot == HTAB_EMPTY_ENTRY);
1757
1758 pa = XCNEW (struct probe_and_action);
1759 pa->prob = prob;
1760 pa->address = address;
1761 pa->action = action;
1762 pa->objfile = objfile;
1763
1764 *slot = pa;
1765}
1766
1767/* Get the solib event probe at the specified location, and the
1768 action associated with it. Returns NULL if no solib event probe
1769 was found. */
1770
1771static struct probe_and_action *
1772solib_event_probe_at (struct svr4_info *info, CORE_ADDR address)
1773{
1774 struct probe_and_action lookup;
1775 void **slot;
1776
1777 lookup.address = address;
1778 slot = htab_find_slot (info->probes_table.get (), &lookup, NO_INSERT);
1779
1780 if (slot == NULL)
1781 return NULL;
1782
1783 return (struct probe_and_action *) *slot;
1784}
1785
1786/* Decide what action to take when the specified solib event probe is
1787 hit. */
1788
1789static enum probe_action
1791{
1792 enum probe_action action;
1793 unsigned probe_argc = 0;
1795
1796 action = pa->action;
1798 return action;
1799
1800 gdb_assert (action == FULL_RELOAD || action == UPDATE_OR_RELOAD);
1801
1802 /* Check that an appropriate number of arguments has been supplied.
1803 We expect:
1804 arg0: Lmid_t lmid (mandatory)
1805 arg1: struct r_debug *debug_base (mandatory)
1806 arg2: struct link_map *new (optional, for incremental updates) */
1807 try
1808 {
1809 probe_argc = pa->prob->get_argument_count (get_frame_arch (frame));
1810 }
1811 catch (const gdb_exception_error &ex)
1812 {
1814 probe_argc = 0;
1815 }
1816
1817 /* If get_argument_count throws an exception, probe_argc will be set
1818 to zero. However, if pa->prob does not have arguments, then
1819 get_argument_count will succeed but probe_argc will also be zero.
1820 Both cases happen because of different things, but they are
1821 treated equally here: action will be set to
1822 PROBES_INTERFACE_FAILED. */
1823 if (probe_argc == 2)
1825 else if (probe_argc < 2)
1827
1828 return action;
1829}
1830
1831/* Populate the shared object list by reading the entire list of
1832 shared objects from the inferior. Handle special cases relating
1833 to the first elements of the list. Returns nonzero on success. */
1834
1835static int
1837{
1839
1840 return 1;
1841}
1842
1843/* Update the shared object list starting from the link-map entry
1844 passed by the linker in the probe's third argument. Returns
1845 nonzero if the list was successfully updated, or zero to indicate
1846 failure. */
1847
1848static int
1849solist_update_incremental (svr4_info *info, CORE_ADDR debug_base,
1850 CORE_ADDR lm)
1851{
1852 /* Fall back to a full update if we are using a remote target
1853 that does not support incremental transfers. */
1854 if (info->using_xfer && !target_augmented_libraries_svr4_read ())
1855 return 0;
1856
1857 /* Fall back to a full update if we used the special namespace zero. We
1858 wouldn't be able to find the last item in the DEBUG_BASE namespace
1859 and hence get the prev link wrong. */
1860 if (info->solib_lists.find (0) != info->solib_lists.end ())
1861 return 0;
1862
1863 /* Ensure that the element is actually initialized. */
1864 if (info->solib_lists.find (debug_base) == info->solib_lists.end ())
1865 info->solib_lists[debug_base] = nullptr;
1866
1867 so_list **psolist = &info->solib_lists[debug_base];
1868 so_list **pnext = nullptr;
1869 so_list *solist = *psolist;
1870 CORE_ADDR prev_lm;
1871
1872 if (solist == nullptr)
1873 {
1874 /* svr4_current_sos_direct contains logic to handle a number of
1875 special cases relating to the first elements of the list in
1876 default namespace. To avoid duplicating this logic we defer to
1877 solist_update_full in this case. */
1878 if (svr4_is_default_namespace (info, debug_base))
1879 return 0;
1880
1881 prev_lm = 0;
1882 pnext = psolist;
1883 }
1884 else
1885 {
1886 /* Walk to the end of the list. */
1887 for (; solist->next != nullptr; solist = solist->next)
1888 /* Nothing. */;
1889
1890 lm_info_svr4 *li = (lm_info_svr4 *) solist->lm_info;
1891 prev_lm = li->lm_addr;
1892 pnext = &solist->next;
1893 }
1894
1895 /* Read the new objects. */
1896 if (info->using_xfer)
1897 {
1898 struct svr4_library_list library_list;
1899 char annex[64];
1900
1901 /* Unknown key=value pairs are ignored by the gdbstub. */
1902 xsnprintf (annex, sizeof (annex), "lmid=%s;start=%s;prev=%s",
1903 phex_nz (debug_base, sizeof (debug_base)),
1904 phex_nz (lm, sizeof (lm)),
1905 phex_nz (prev_lm, sizeof (prev_lm)));
1906 if (!svr4_current_sos_via_xfer_libraries (&library_list, annex))
1907 return 0;
1908
1909 /* Get the so list from the target. We replace the list in the
1910 target response so we can easily check that the response only
1911 covers one namespace.
1912
1913 We expect gdbserver to provide updates for the namespace that
1914 contains LM, which would be this namespace... */
1915 so_list *sos = nullptr;
1916 if (library_list.solib_lists.find (debug_base)
1917 != library_list.solib_lists.end ())
1918 std::swap (sos, library_list.solib_lists[debug_base]);
1919 if (sos == nullptr)
1920 {
1921 /* ...or for the special zero namespace for earlier versions... */
1922 if (library_list.solib_lists.find (0)
1923 != library_list.solib_lists.end ())
1924 std::swap (sos, library_list.solib_lists[0]);
1925 }
1926
1927 /* ...but nothing else. */
1928 for (const std::pair<CORE_ADDR, so_list *> tuple
1929 : library_list.solib_lists)
1930 gdb_assert (tuple.second == nullptr);
1931
1932 *pnext = sos;
1933 }
1934 else
1935 {
1936 /* IGNORE_FIRST may safely be set to zero here because the
1937 above check and deferral to solist_update_full ensures
1938 that this call to svr4_read_so_list will never see the
1939 first element. */
1940 if (!svr4_read_so_list (info, lm, prev_lm, &pnext, 0))
1941 return 0;
1942 }
1943
1944 return 1;
1945}
1946
1947/* Disable the probes-based linker interface and revert to the
1948 original interface. We don't reset the breakpoints as the
1949 ones set up for the probes-based interface are adequate. */
1950
1951static void
1953{
1954 warning (_("Probes-based dynamic linker interface failed.\n"
1955 "Reverting to original interface."));
1956
1957 free_probes_table (info);
1958 free_solib_lists (info);
1959}
1960
1961/* Update the solib list as appropriate when using the
1962 probes-based linker interface. Do nothing if using the
1963 standard interface. */
1964
1965static void
1967{
1969 struct probe_and_action *pa;
1970 enum probe_action action;
1971 struct value *val = NULL;
1972 CORE_ADDR pc, debug_base, lm = 0;
1974
1975 /* Do nothing if not using the probes interface. */
1976 if (info->probes_table == NULL)
1977 return;
1978
1980 pa = solib_event_probe_at (info, pc);
1981 if (pa == nullptr)
1982 {
1983 /* When some solib ops sits above us, it can respond to a solib event
1984 by calling in here. This is done assuming that if the current event
1985 is not an SVR4 solib event, calling here should be a no-op. */
1986 return;
1987 }
1988
1989 /* If anything goes wrong we revert to the original linker
1990 interface. */
1991 auto cleanup = make_scope_exit ([info] ()
1992 {
1994 });
1995
1996 action = solib_event_probe_action (pa);
1997 if (action == PROBES_INTERFACE_FAILED)
1998 return;
1999
2000 if (action == DO_NOTHING)
2001 {
2002 cleanup.release ();
2003 return;
2004 }
2005
2006 /* evaluate_argument looks up symbols in the dynamic linker
2007 using find_pc_section. find_pc_section is accelerated by a cache
2008 called the section map. The section map is invalidated every
2009 time a shared library is loaded or unloaded, and if the inferior
2010 is generating a lot of shared library events then the section map
2011 will be updated every time svr4_handle_solib_event is called.
2012 We called find_pc_section in svr4_create_solib_event_breakpoints,
2013 so we can guarantee that the dynamic linker's sections are in the
2014 section map. We can therefore inhibit section map updates across
2015 these calls to evaluate_argument and save a lot of time. */
2016 {
2017 scoped_restore inhibit_updates
2019
2020 try
2021 {
2022 val = pa->prob->evaluate_argument (1, frame);
2023 }
2024 catch (const gdb_exception_error &ex)
2025 {
2027 val = NULL;
2028 }
2029
2030 if (val == NULL)
2031 return;
2032
2033 debug_base = value_as_address (val);
2034 if (debug_base == 0)
2035 return;
2036
2037 /* If the global _r_debug object moved, we need to reload everything
2038 since we cannot identify namespaces (by the location of their
2039 r_debug_ext object) anymore. */
2040 CORE_ADDR global_debug_base = elf_locate_base ();
2041 if (global_debug_base != info->debug_base)
2042 {
2043 info->debug_base = global_debug_base;
2044 action = FULL_RELOAD;
2045 }
2046
2047 if (info->debug_base == 0)
2048 {
2049 /* It's possible for the reloc_complete probe to be triggered before
2050 the linker has set the DT_DEBUG pointer (for example, when the
2051 linker has finished relocating an LD_AUDIT library or its
2052 dependencies). Since we can't yet handle libraries from other link
2053 namespaces, we don't lose anything by ignoring them here. */
2054 struct value *link_map_id_val;
2055 try
2056 {
2057 link_map_id_val = pa->prob->evaluate_argument (0, frame);
2058 }
2059 catch (const gdb_exception_error)
2060 {
2061 link_map_id_val = NULL;
2062 }
2063 /* glibc and illumos' libc both define LM_ID_BASE as zero. */
2064 if (link_map_id_val != NULL && value_as_long (link_map_id_val) != 0)
2065 action = DO_NOTHING;
2066 else
2067 return;
2068 }
2069
2070 if (action == UPDATE_OR_RELOAD)
2071 {
2072 try
2073 {
2074 val = pa->prob->evaluate_argument (2, frame);
2075 }
2076 catch (const gdb_exception_error &ex)
2077 {
2079 return;
2080 }
2081
2082 if (val != NULL)
2083 lm = value_as_address (val);
2084
2085 if (lm == 0)
2086 action = FULL_RELOAD;
2087 }
2088
2089 /* Resume section map updates. Closing the scope is
2090 sufficient. */
2091 }
2092
2093 if (action == UPDATE_OR_RELOAD)
2094 {
2095 if (!solist_update_incremental (info, debug_base, lm))
2096 action = FULL_RELOAD;
2097 }
2098
2099 if (action == FULL_RELOAD)
2100 {
2101 if (!solist_update_full (info))
2102 return;
2103 }
2104
2105 cleanup.release ();
2106}
2107
2108/* Helper function for svr4_update_solib_event_breakpoints. */
2109
2110static bool
2112{
2113 if (b->type != bp_shlib_event)
2114 {
2115 /* Continue iterating. */
2116 return false;
2117 }
2118
2119 for (bp_location &loc : b->locations ())
2120 {
2121 struct svr4_info *info;
2122 struct probe_and_action *pa;
2123
2124 info = solib_svr4_pspace_data.get (loc.pspace);
2125 if (info == NULL || info->probes_table == NULL)
2126 continue;
2127
2128 pa = solib_event_probe_at (info, loc.address);
2129 if (pa == NULL)
2130 continue;
2131
2132 if (pa->action == DO_NOTHING)
2133 {
2136 else if (b->enable_state == bp_enabled && !stop_on_solib_events)
2138 }
2139
2140 break;
2141 }
2142
2143 /* Continue iterating. */
2144 return false;
2145}
2146
2147/* Enable or disable optional solib event breakpoints as appropriate.
2148 Called whenever stop_on_solib_events is changed. */
2149
2150static void
2156
2157/* Create and register solib event breakpoints. PROBES is an array
2158 of NUM_PROBES elements, each of which is vector of probes. A
2159 solib event breakpoint will be created and registered for each
2160 probe. */
2161
2162static void
2164 const std::vector<probe *> *probes,
2165 struct objfile *objfile)
2166{
2167 for (int i = 0; i < NUM_PROBES; i++)
2168 {
2170
2171 for (probe *p : probes[i])
2172 {
2173 CORE_ADDR address = p->get_relocated_address (objfile);
2174
2175 solib_debug_printf ("name=%s, addr=%s", probe_info[i].name,
2177
2180 }
2181 }
2182
2184}
2185
2186/* Find all the glibc named probes. Only if all of the probes are found, then
2187 create them and return true. Otherwise return false. If WITH_PREFIX is set
2188 then add "rtld" to the front of the probe names. */
2189static bool
2191 struct gdbarch *gdbarch,
2192 struct obj_section *os,
2193 bool with_prefix)
2194{
2195 SOLIB_SCOPED_DEBUG_START_END ("objfile=%s, with_prefix=%d",
2196 os->objfile->original_name, with_prefix);
2197
2198 std::vector<probe *> probes[NUM_PROBES];
2199
2200 for (int i = 0; i < NUM_PROBES; i++)
2201 {
2202 const char *name = probe_info[i].name;
2203 char buf[32];
2204
2205 /* Fedora 17 and Red Hat Enterprise Linux 6.2-6.4 shipped with an early
2206 version of the probes code in which the probes' names were prefixed
2207 with "rtld_" and the "map_failed" probe did not exist. The locations
2208 of the probes are otherwise the same, so we check for probes with
2209 prefixed names if probes with unprefixed names are not present. */
2210 if (with_prefix)
2211 {
2212 xsnprintf (buf, sizeof (buf), "rtld_%s", name);
2213 name = buf;
2214 }
2215
2216 probes[i] = find_probes_in_objfile (os->objfile, "rtld", name);
2217 solib_debug_printf ("probe=%s, num found=%zu", name, probes[i].size ());
2218
2219 /* Ensure at least one probe for the current name was found. */
2220 if (probes[i].empty ())
2221 {
2222 /* The "map_failed" probe did not exist in early versions of the
2223 probes code in which the probes' names were prefixed with
2224 "rtld_".
2225
2226 Additionally, the "map_failed" probe was accidentally removed
2227 from glibc 2.35 and 2.36, when changes in glibc meant the
2228 probe could no longer be reached, and the compiler optimized
2229 the probe away. In this case the probe name doesn't have the
2230 "rtld_" prefix.
2231
2232 To handle this, and give GDB as much flexibility as possible,
2233 we make the rule that, if a probe isn't required for the
2234 correct operation of GDB (i.e. its action is DO_NOTHING), then
2235 we will still use the probes interface, even if that probe is
2236 missing.
2237
2238 The only (possible) downside of this is that, if the user has
2239 'set stop-on-solib-events on' in effect, then they might get
2240 fewer events using the probes interface than with the classic
2241 non-probes interface. */
2242 if (probe_info[i].action == DO_NOTHING)
2243 continue;
2244 else
2245 return false;
2246 }
2247
2248 /* Ensure probe arguments can be evaluated. */
2249 for (probe *p : probes[i])
2250 {
2251 if (!p->can_evaluate_arguments ())
2252 return false;
2253 /* This will fail if the probe is invalid. This has been seen on Arm
2254 due to references to symbols that have been resolved away. */
2255 try
2256 {
2257 p->get_argument_count (gdbarch);
2258 }
2259 catch (const gdb_exception_error &ex)
2260 {
2262 warning (_("Initializing probes-based dynamic linker interface "
2263 "failed.\nReverting to original interface."));
2264 return false;
2265 }
2266 }
2267 }
2268
2269 /* All probes found. Now create them. */
2270 solib_debug_printf ("using probes interface");
2271 svr4_create_probe_breakpoints (info, gdbarch, probes, os->objfile);
2272 return true;
2273}
2274
2275/* Both the SunOS and the SVR4 dynamic linkers call a marker function
2276 before and after mapping and unmapping shared libraries. The sole
2277 purpose of this method is to allow debuggers to set a breakpoint so
2278 they can track these changes.
2279
2280 Some versions of the glibc dynamic linker contain named probes
2281 to allow more fine grained stopping. Given the address of the
2282 original marker function, this function attempts to find these
2283 probes, and if found, sets breakpoints on those instead. If the
2284 probes aren't found, a single breakpoint is set on the original
2285 marker function. */
2286
2287static void
2289 CORE_ADDR address)
2290{
2291 struct obj_section *os = find_pc_section (address);
2292
2293 if (os == nullptr
2294 || (!svr4_find_and_create_probe_breakpoints (info, gdbarch, os, false)
2295 && !svr4_find_and_create_probe_breakpoints (info, gdbarch, os, true)))
2296 {
2297 solib_debug_printf ("falling back to r_brk breakpoint: addr=%s",
2298 paddress (gdbarch, address));
2300 }
2301}
2302
2303/* Arrange for dynamic linker to hit breakpoint.
2304
2305 Both the SunOS and the SVR4 dynamic linkers have, as part of their
2306 debugger interface, support for arranging for the inferior to hit
2307 a breakpoint after mapping in the shared libraries. This function
2308 enables that breakpoint.
2309
2310 For SunOS, there is a special flag location (in_debugger) which we
2311 set to 1. When the dynamic linker sees this flag set, it will set
2312 a breakpoint at a location known only to itself, after saving the
2313 original contents of that place and the breakpoint address itself,
2314 in its own internal structures. When we resume the inferior, it
2315 will eventually take a SIGTRAP when it runs into the breakpoint.
2316 We handle this (in a different place) by restoring the contents of
2317 the breakpointed location (which is only known after it stops),
2318 chasing around to locate the shared libraries that have been
2319 loaded, then resuming.
2320
2321 For SVR4, the debugger interface structure contains a member (r_brk)
2322 which is statically initialized at the time the shared library is
2323 built, to the offset of a function (_r_debug_state) which is guaran-
2324 teed to be called once before mapping in a library, and again when
2325 the mapping is complete. At the time we are examining this member,
2326 it contains only the unrelocated offset of the function, so we have
2327 to do our own relocation. Later, when the dynamic linker actually
2328 runs, it relocates r_brk to be the actual address of _r_debug_state().
2329
2330 The debugger interface structure also contains an enumeration which
2331 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
2332 depending upon whether or not the library is being mapped or unmapped,
2333 and then set to RT_CONSISTENT after the library is mapped/unmapped. */
2334
2335static int
2336enable_break (struct svr4_info *info, int from_tty)
2337{
2338 struct bound_minimal_symbol msymbol;
2339 const char * const *bkpt_namep;
2340 asection *interp_sect;
2341 CORE_ADDR sym_addr;
2342
2343 info->interp_text_sect_low = info->interp_text_sect_high = 0;
2344 info->interp_plt_sect_low = info->interp_plt_sect_high = 0;
2345
2346 /* If we already have a shared library list in the target, and
2347 r_debug contains r_brk, set the breakpoint there - this should
2348 mean r_brk has already been relocated. Assume the dynamic linker
2349 is the object containing r_brk. */
2350
2351 solib_add (NULL, from_tty, auto_solib_add);
2352 sym_addr = 0;
2353 if (info->debug_base && solib_svr4_r_map (info->debug_base) != 0)
2354 sym_addr = solib_svr4_r_brk (info);
2355
2356 if (sym_addr != 0)
2357 {
2358 struct obj_section *os;
2359
2360 sym_addr = gdbarch_addr_bits_remove
2361 (target_gdbarch (),
2363 (target_gdbarch (), sym_addr, current_inferior ()->top_target ()));
2364
2365 /* On at least some versions of Solaris there's a dynamic relocation
2366 on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
2367 we get control before the dynamic linker has self-relocated.
2368 Check if SYM_ADDR is in a known section, if it is assume we can
2369 trust its value. This is just a heuristic though, it could go away
2370 or be replaced if it's getting in the way.
2371
2372 On ARM we need to know whether the ISA of rtld_db_dlactivity (or
2373 however it's spelled in your particular system) is ARM or Thumb.
2374 That knowledge is encoded in the address, if it's Thumb the low bit
2375 is 1. However, we've stripped that info above and it's not clear
2376 what all the consequences are of passing a non-addr_bits_remove'd
2377 address to svr4_create_solib_event_breakpoints. The call to
2378 find_pc_section verifies we know about the address and have some
2379 hope of computing the right kind of breakpoint to use (via
2380 symbol info). It does mean that GDB needs to be pointed at a
2381 non-stripped version of the dynamic linker in order to obtain
2382 information it already knows about. Sigh. */
2383
2384 os = find_pc_section (sym_addr);
2385 if (os != NULL)
2386 {
2387 /* Record the relocated start and end address of the dynamic linker
2388 text and plt section for svr4_in_dynsym_resolve_code. */
2389 bfd *tmp_bfd;
2390 CORE_ADDR load_addr;
2391
2392 tmp_bfd = os->objfile->obfd.get ();
2393 load_addr = os->objfile->text_section_offset ();
2394
2395 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
2396 if (interp_sect)
2397 {
2398 info->interp_text_sect_low
2399 = bfd_section_vma (interp_sect) + load_addr;
2400 info->interp_text_sect_high
2401 = info->interp_text_sect_low + bfd_section_size (interp_sect);
2402 }
2403 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
2404 if (interp_sect)
2405 {
2406 info->interp_plt_sect_low
2407 = bfd_section_vma (interp_sect) + load_addr;
2408 info->interp_plt_sect_high
2409 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
2410 }
2411
2413 return 1;
2414 }
2415 }
2416
2417 /* Find the program interpreter; if not found, warn the user and drop
2418 into the old breakpoint at symbol code. */
2419 gdb::optional<gdb::byte_vector> interp_name_holder
2421 if (interp_name_holder)
2422 {
2423 const char *interp_name = (const char *) interp_name_holder->data ();
2424 CORE_ADDR load_addr = 0;
2425 int load_addr_found = 0;
2426 int loader_found_in_list = 0;
2427 target_ops_up tmp_bfd_target;
2428
2429 sym_addr = 0;
2430
2431 /* Now we need to figure out where the dynamic linker was
2432 loaded so that we can load its symbols and place a breakpoint
2433 in the dynamic linker itself.
2434
2435 This address is stored on the stack. However, I've been unable
2436 to find any magic formula to find it for Solaris (appears to
2437 be trivial on GNU/Linux). Therefore, we have to try an alternate
2438 mechanism to find the dynamic linker's base address. */
2439
2440 gdb_bfd_ref_ptr tmp_bfd;
2441 try
2442 {
2443 tmp_bfd = solib_bfd_open (interp_name);
2444 }
2445 catch (const gdb_exception &ex)
2446 {
2447 }
2448
2449 if (tmp_bfd == NULL)
2450 goto bkpt_at_symbol;
2451
2452 /* Now convert the TMP_BFD into a target. That way target, as
2453 well as BFD operations can be used. */
2454 tmp_bfd_target = target_bfd_reopen (tmp_bfd);
2455
2456 /* On a running target, we can get the dynamic linker's base
2457 address from the shared library table. */
2458 for (struct so_list *so : current_program_space->solibs ())
2459 {
2460 if (svr4_same_1 (interp_name, so->so_original_name))
2461 {
2462 load_addr_found = 1;
2463 loader_found_in_list = 1;
2464 load_addr = lm_addr_check (so, tmp_bfd.get ());
2465 break;
2466 }
2467 }
2468
2469 /* If we were not able to find the base address of the loader
2470 from our so_list, then try using the AT_BASE auxilliary entry. */
2471 if (!load_addr_found)
2472 if (target_auxv_search (AT_BASE, &load_addr) > 0)
2473 {
2474 int addr_bit = gdbarch_addr_bit (target_gdbarch ());
2475
2476 /* Ensure LOAD_ADDR has proper sign in its possible upper bits so
2477 that `+ load_addr' will overflow CORE_ADDR width not creating
2478 invalid addresses like 0x101234567 for 32bit inferiors on 64bit
2479 GDB. */
2480
2481 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
2482 {
2483 CORE_ADDR space_size = (CORE_ADDR) 1 << addr_bit;
2484 CORE_ADDR tmp_entry_point
2485 = exec_entry_point (tmp_bfd.get (), tmp_bfd_target.get ());
2486
2487 gdb_assert (load_addr < space_size);
2488
2489 /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
2490 64bit ld.so with 32bit executable, it should not happen. */
2491
2492 if (tmp_entry_point < space_size
2493 && tmp_entry_point + load_addr >= space_size)
2494 load_addr -= space_size;
2495 }
2496
2497 load_addr_found = 1;
2498 }
2499
2500 /* Otherwise we find the dynamic linker's base address by examining
2501 the current pc (which should point at the entry point for the
2502 dynamic linker) and subtracting the offset of the entry point.
2503
2504 This is more fragile than the previous approaches, but is a good
2505 fallback method because it has actually been working well in
2506 most cases. */
2507 if (!load_addr_found)
2508 {
2509 struct regcache *regcache
2510 = get_thread_arch_regcache (current_inferior ()->process_target (),
2512
2513 load_addr = (regcache_read_pc (regcache)
2514 - exec_entry_point (tmp_bfd.get (),
2515 tmp_bfd_target.get ()));
2516 }
2517
2518 if (!loader_found_in_list)
2519 {
2520 info->debug_loader_name = xstrdup (interp_name);
2521 info->debug_loader_offset_p = 1;
2522 info->debug_loader_offset = load_addr;
2523 solib_add (NULL, from_tty, auto_solib_add);
2524 }
2525
2526 /* Record the relocated start and end address of the dynamic linker
2527 text and plt section for svr4_in_dynsym_resolve_code. */
2528 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".text");
2529 if (interp_sect)
2530 {
2531 info->interp_text_sect_low
2532 = bfd_section_vma (interp_sect) + load_addr;
2533 info->interp_text_sect_high
2534 = info->interp_text_sect_low + bfd_section_size (interp_sect);
2535 }
2536 interp_sect = bfd_get_section_by_name (tmp_bfd.get (), ".plt");
2537 if (interp_sect)
2538 {
2539 info->interp_plt_sect_low
2540 = bfd_section_vma (interp_sect) + load_addr;
2541 info->interp_plt_sect_high
2542 = info->interp_plt_sect_low + bfd_section_size (interp_sect);
2543 }
2544
2545 /* Now try to set a breakpoint in the dynamic linker. */
2546 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2547 {
2548 sym_addr
2550 (tmp_bfd.get (),
2551 [=] (const asymbol *sym)
2552 {
2553 return (strcmp (sym->name, *bkpt_namep) == 0
2554 && ((sym->section->flags & (SEC_CODE | SEC_DATA))
2555 != 0));
2556 }));
2557 if (sym_addr != 0)
2558 break;
2559 }
2560
2561 if (sym_addr != 0)
2562 /* Convert 'sym_addr' from a function pointer to an address.
2563 Because we pass tmp_bfd_target instead of the current
2564 target, this will always produce an unrelocated value. */
2566 sym_addr,
2567 tmp_bfd_target.get ());
2568
2569 if (sym_addr != 0)
2570 {
2572 load_addr + sym_addr);
2573 return 1;
2574 }
2575
2576 /* For whatever reason we couldn't set a breakpoint in the dynamic
2577 linker. Warn and drop into the old code. */
2578 bkpt_at_symbol:
2579 warning (_("Unable to find dynamic linker breakpoint function.\n"
2580 "GDB will be unable to debug shared library initializers\n"
2581 "and track explicitly loaded dynamic code."));
2582 }
2583
2584 /* Scan through the lists of symbols, trying to look up the symbol and
2585 set a breakpoint there. Terminate loop when we/if we succeed. */
2586
2588 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
2589 {
2590 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
2591 if ((msymbol.minsym != NULL)
2592 && (msymbol.value_address () != 0))
2593 {
2594 sym_addr = msymbol.value_address ();
2596 (target_gdbarch (), sym_addr, current_inferior ()->top_target ());
2598 sym_addr);
2599 return 1;
2600 }
2601 }
2602
2603 if (interp_name_holder && !current_inferior ()->attach_flag)
2604 {
2605 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
2606 {
2607 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
2608 if ((msymbol.minsym != NULL)
2609 && (msymbol.value_address () != 0))
2610 {
2611 sym_addr = msymbol.value_address ();
2613 (target_gdbarch (), sym_addr,
2614 current_inferior ()->top_target ());
2616 sym_addr);
2617 return 1;
2618 }
2619 }
2620 }
2621 return 0;
2622}
2623
2624/* Read the ELF program headers from ABFD. */
2625
2626static gdb::optional<gdb::byte_vector>
2628{
2629 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
2630 int phdrs_size = ehdr->e_phnum * ehdr->e_phentsize;
2631 if (phdrs_size == 0)
2632 return {};
2633
2634 gdb::byte_vector buf (phdrs_size);
2635 if (bfd_seek (abfd, ehdr->e_phoff, SEEK_SET) != 0
2636 || bfd_read (buf.data (), phdrs_size, abfd) != phdrs_size)
2637 return {};
2638
2639 return buf;
2640}
2641
2642/* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
2643 exec_bfd. Otherwise return 0.
2644
2645 We relocate all of the sections by the same amount. This
2646 behavior is mandated by recent editions of the System V ABI.
2647 According to the System V Application Binary Interface,
2648 Edition 4.1, page 5-5:
2649
2650 ... Though the system chooses virtual addresses for
2651 individual processes, it maintains the segments' relative
2652 positions. Because position-independent code uses relative
2653 addressing between segments, the difference between
2654 virtual addresses in memory must match the difference
2655 between virtual addresses in the file. The difference
2656 between the virtual address of any segment in memory and
2657 the corresponding virtual address in the file is thus a
2658 single constant value for any one executable or shared
2659 object in a given process. This difference is the base
2660 address. One use of the base address is to relocate the
2661 memory image of the program during dynamic linking.
2662
2663 The same language also appears in Edition 4.0 of the System V
2664 ABI and is left unspecified in some of the earlier editions.
2665
2666 Decide if the objfile needs to be relocated. As indicated above, we will
2667 only be here when execution is stopped. But during attachment PC can be at
2668 arbitrary address therefore regcache_read_pc can be misleading (contrary to
2669 the auxv AT_ENTRY value). Moreover for executable with interpreter section
2670 regcache_read_pc would point to the interpreter and not the main executable.
2671
2672 So, to summarize, relocations are necessary when the start address obtained
2673 from the executable is different from the address in auxv AT_ENTRY entry.
2674
2675 [ The astute reader will note that we also test to make sure that
2676 the executable in question has the DYNAMIC flag set. It is my
2677 opinion that this test is unnecessary (undesirable even). It
2678 was added to avoid inadvertent relocation of an executable
2679 whose e_type member in the ELF header is not ET_DYN. There may
2680 be a time in the future when it is desirable to do relocations
2681 on other types of files as well in which case this condition
2682 should either be removed or modified to accommodate the new file
2683 type. - Kevin, Nov 2000. ] */
2684
2685static int
2686svr4_exec_displacement (CORE_ADDR *displacementp)
2687{
2688 /* ENTRY_POINT is a possible function descriptor - before
2689 a call to gdbarch_convert_from_func_ptr_addr. */
2690 CORE_ADDR entry_point, exec_displacement;
2691
2692 if (current_program_space->exec_bfd () == NULL)
2693 return 0;
2694
2695 /* Therefore for ELF it is ET_EXEC and not ET_DYN. Both shared libraries
2696 being executed themselves and PIE (Position Independent Executable)
2697 executables are ET_DYN. */
2698
2699 if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0)
2700 return 0;
2701
2702 if (target_auxv_search (AT_ENTRY, &entry_point) <= 0)
2703 return 0;
2704
2705 exec_displacement
2706 = entry_point - bfd_get_start_address (current_program_space->exec_bfd ());
2707
2708 /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
2709 alignment. It is cheaper than the program headers comparison below. */
2710
2711 if (bfd_get_flavour (current_program_space->exec_bfd ())
2712 == bfd_target_elf_flavour)
2713 {
2714 const struct elf_backend_data *elf
2715 = get_elf_backend_data (current_program_space->exec_bfd ());
2716
2717 /* p_align of PT_LOAD segments does not specify any alignment but
2718 only congruency of addresses:
2719 p_offset % p_align == p_vaddr % p_align
2720 Kernel is free to load the executable with lower alignment. */
2721
2722 if ((exec_displacement & (elf->minpagesize - 1)) != 0)
2723 return 0;
2724 }
2725
2726 /* Verify that the auxilliary vector describes the same file as exec_bfd, by
2727 comparing their program headers. If the program headers in the auxilliary
2728 vector do not match the program headers in the executable, then we are
2729 looking at a different file than the one used by the kernel - for
2730 instance, "gdb program" connected to "gdbserver :PORT ld.so program". */
2731
2732 if (bfd_get_flavour (current_program_space->exec_bfd ())
2733 == bfd_target_elf_flavour)
2734 {
2735 /* Be optimistic and return 0 only if GDB was able to verify the headers
2736 really do not match. */
2737 int arch_size;
2738
2739 gdb::optional<gdb::byte_vector> phdrs_target
2740 = read_program_header (-1, &arch_size, NULL);
2741 gdb::optional<gdb::byte_vector> phdrs_binary
2743 if (phdrs_target && phdrs_binary)
2744 {
2745 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
2746
2747 /* We are dealing with three different addresses. EXEC_BFD
2748 represents current address in on-disk file. target memory content
2749 may be different from EXEC_BFD as the file may have been prelinked
2750 to a different address after the executable has been loaded.
2751 Moreover the address of placement in target memory can be
2752 different from what the program headers in target memory say -
2753 this is the goal of PIE.
2754
2755 Detected DISPLACEMENT covers both the offsets of PIE placement and
2756 possible new prelink performed after start of the program. Here
2757 relocate BUF and BUF2 just by the EXEC_BFD vs. target memory
2758 content offset for the verification purpose. */
2759
2760 if (phdrs_target->size () != phdrs_binary->size ()
2761 || bfd_get_arch_size (current_program_space->exec_bfd ()) != arch_size)
2762 return 0;
2763 else if (arch_size == 32
2764 && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
2765 && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
2766 {
2767 Elf_Internal_Ehdr *ehdr2
2768 = elf_tdata (current_program_space->exec_bfd ())->elf_header;
2769 Elf_Internal_Phdr *phdr2
2770 = elf_tdata (current_program_space->exec_bfd ())->phdr;
2771 CORE_ADDR displacement = 0;
2772 int i;
2773
2774 /* DISPLACEMENT could be found more easily by the difference of
2775 ehdr2->e_entry. But we haven't read the ehdr yet, and we
2776 already have enough information to compute that displacement
2777 with what we've read. */
2778
2779 for (i = 0; i < ehdr2->e_phnum; i++)
2780 if (phdr2[i].p_type == PT_LOAD)
2781 {
2782 Elf32_External_Phdr *phdrp;
2783 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2784 CORE_ADDR vaddr, paddr;
2785 CORE_ADDR displacement_vaddr = 0;
2786 CORE_ADDR displacement_paddr = 0;
2787
2788 phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
2789 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2790 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2791
2792 vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
2793 byte_order);
2794 displacement_vaddr = vaddr - phdr2[i].p_vaddr;
2795
2796 paddr = extract_unsigned_integer (buf_paddr_p, 4,
2797 byte_order);
2798 displacement_paddr = paddr - phdr2[i].p_paddr;
2799
2800 if (displacement_vaddr == displacement_paddr)
2801 displacement = displacement_vaddr;
2802
2803 break;
2804 }
2805
2806 /* Now compare program headers from the target and the binary
2807 with optional DISPLACEMENT. */
2808
2809 for (i = 0;
2810 i < phdrs_target->size () / sizeof (Elf32_External_Phdr);
2811 i++)
2812 {
2813 Elf32_External_Phdr *phdrp;
2814 Elf32_External_Phdr *phdr2p;
2815 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2816 CORE_ADDR vaddr, paddr;
2817 asection *plt2_asect;
2818
2819 phdrp = &((Elf32_External_Phdr *) phdrs_target->data ())[i];
2820 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2821 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2822 phdr2p = &((Elf32_External_Phdr *) phdrs_binary->data ())[i];
2823
2824 /* PT_GNU_STACK is an exception by being never relocated by
2825 prelink as its addresses are always zero. */
2826
2827 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2828 continue;
2829
2830 /* Check also other adjustment combinations - PR 11786. */
2831
2832 vaddr = extract_unsigned_integer (buf_vaddr_p, 4,
2833 byte_order);
2834 vaddr -= displacement;
2835 store_unsigned_integer (buf_vaddr_p, 4, byte_order, vaddr);
2836
2837 paddr = extract_unsigned_integer (buf_paddr_p, 4,
2838 byte_order);
2839 paddr -= displacement;
2840 store_unsigned_integer (buf_paddr_p, 4, byte_order, paddr);
2841
2842 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2843 continue;
2844
2845 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2846 CentOS-5 has problems with filesz, memsz as well.
2847 Strip also modifies memsz of PT_TLS.
2848 See PR 11786. */
2849 if (phdr2[i].p_type == PT_GNU_RELRO
2850 || phdr2[i].p_type == PT_TLS)
2851 {
2852 Elf32_External_Phdr tmp_phdr = *phdrp;
2853 Elf32_External_Phdr tmp_phdr2 = *phdr2p;
2854
2855 memset (tmp_phdr.p_filesz, 0, 4);
2856 memset (tmp_phdr.p_memsz, 0, 4);
2857 memset (tmp_phdr.p_flags, 0, 4);
2858 memset (tmp_phdr.p_align, 0, 4);
2859 memset (tmp_phdr2.p_filesz, 0, 4);
2860 memset (tmp_phdr2.p_memsz, 0, 4);
2861 memset (tmp_phdr2.p_flags, 0, 4);
2862 memset (tmp_phdr2.p_align, 0, 4);
2863
2864 if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
2865 == 0)
2866 continue;
2867 }
2868
2869 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2870 bfd *exec_bfd = current_program_space->exec_bfd ();
2871 plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
2872 if (plt2_asect)
2873 {
2874 int content2;
2875 gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
2876 CORE_ADDR filesz;
2877
2878 content2 = (bfd_section_flags (plt2_asect)
2879 & SEC_HAS_CONTENTS) != 0;
2880
2881 filesz = extract_unsigned_integer (buf_filesz_p, 4,
2882 byte_order);
2883
2884 /* PLT2_ASECT is from on-disk file (exec_bfd) while
2885 FILESZ is from the in-memory image. */
2886 if (content2)
2887 filesz += bfd_section_size (plt2_asect);
2888 else
2889 filesz -= bfd_section_size (plt2_asect);
2890
2891 store_unsigned_integer (buf_filesz_p, 4, byte_order,
2892 filesz);
2893
2894 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2895 continue;
2896 }
2897
2898 return 0;
2899 }
2900 }
2901 else if (arch_size == 64
2902 && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
2903 && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
2904 {
2905 Elf_Internal_Ehdr *ehdr2
2906 = elf_tdata (current_program_space->exec_bfd ())->elf_header;
2907 Elf_Internal_Phdr *phdr2
2908 = elf_tdata (current_program_space->exec_bfd ())->phdr;
2909 CORE_ADDR displacement = 0;
2910 int i;
2911
2912 /* DISPLACEMENT could be found more easily by the difference of
2913 ehdr2->e_entry. But we haven't read the ehdr yet, and we
2914 already have enough information to compute that displacement
2915 with what we've read. */
2916
2917 for (i = 0; i < ehdr2->e_phnum; i++)
2918 if (phdr2[i].p_type == PT_LOAD)
2919 {
2920 Elf64_External_Phdr *phdrp;
2921 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2922 CORE_ADDR vaddr, paddr;
2923 CORE_ADDR displacement_vaddr = 0;
2924 CORE_ADDR displacement_paddr = 0;
2925
2926 phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
2927 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2928 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2929
2930 vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
2931 byte_order);
2932 displacement_vaddr = vaddr - phdr2[i].p_vaddr;
2933
2934 paddr = extract_unsigned_integer (buf_paddr_p, 8,
2935 byte_order);
2936 displacement_paddr = paddr - phdr2[i].p_paddr;
2937
2938 if (displacement_vaddr == displacement_paddr)
2939 displacement = displacement_vaddr;
2940
2941 break;
2942 }
2943
2944 /* Now compare BUF and BUF2 with optional DISPLACEMENT. */
2945
2946 for (i = 0;
2947 i < phdrs_target->size () / sizeof (Elf64_External_Phdr);
2948 i++)
2949 {
2950 Elf64_External_Phdr *phdrp;
2951 Elf64_External_Phdr *phdr2p;
2952 gdb_byte *buf_vaddr_p, *buf_paddr_p;
2953 CORE_ADDR vaddr, paddr;
2954 asection *plt2_asect;
2955
2956 phdrp = &((Elf64_External_Phdr *) phdrs_target->data ())[i];
2957 buf_vaddr_p = (gdb_byte *) &phdrp->p_vaddr;
2958 buf_paddr_p = (gdb_byte *) &phdrp->p_paddr;
2959 phdr2p = &((Elf64_External_Phdr *) phdrs_binary->data ())[i];
2960
2961 /* PT_GNU_STACK is an exception by being never relocated by
2962 prelink as its addresses are always zero. */
2963
2964 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2965 continue;
2966
2967 /* Check also other adjustment combinations - PR 11786. */
2968
2969 vaddr = extract_unsigned_integer (buf_vaddr_p, 8,
2970 byte_order);
2971 vaddr -= displacement;
2972 store_unsigned_integer (buf_vaddr_p, 8, byte_order, vaddr);
2973
2974 paddr = extract_unsigned_integer (buf_paddr_p, 8,
2975 byte_order);
2976 paddr -= displacement;
2977 store_unsigned_integer (buf_paddr_p, 8, byte_order, paddr);
2978
2979 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
2980 continue;
2981
2982 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2983 CentOS-5 has problems with filesz, memsz as well.
2984 Strip also modifies memsz of PT_TLS.
2985 See PR 11786. */
2986 if (phdr2[i].p_type == PT_GNU_RELRO
2987 || phdr2[i].p_type == PT_TLS)
2988 {
2989 Elf64_External_Phdr tmp_phdr = *phdrp;
2990 Elf64_External_Phdr tmp_phdr2 = *phdr2p;
2991
2992 memset (tmp_phdr.p_filesz, 0, 8);
2993 memset (tmp_phdr.p_memsz, 0, 8);
2994 memset (tmp_phdr.p_flags, 0, 4);
2995 memset (tmp_phdr.p_align, 0, 8);
2996 memset (tmp_phdr2.p_filesz, 0, 8);
2997 memset (tmp_phdr2.p_memsz, 0, 8);
2998 memset (tmp_phdr2.p_flags, 0, 4);
2999 memset (tmp_phdr2.p_align, 0, 8);
3000
3001 if (memcmp (&tmp_phdr, &tmp_phdr2, sizeof (tmp_phdr))
3002 == 0)
3003 continue;
3004 }
3005
3006 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
3007 plt2_asect
3008 = bfd_get_section_by_name (current_program_space->exec_bfd (),
3009 ".plt");
3010 if (plt2_asect)
3011 {
3012 int content2;
3013 gdb_byte *buf_filesz_p = (gdb_byte *) &phdrp->p_filesz;
3014 CORE_ADDR filesz;
3015
3016 content2 = (bfd_section_flags (plt2_asect)
3017 & SEC_HAS_CONTENTS) != 0;
3018
3019 filesz = extract_unsigned_integer (buf_filesz_p, 8,
3020 byte_order);
3021
3022 /* PLT2_ASECT is from on-disk file (current
3023 exec_bfd) while FILESZ is from the in-memory
3024 image. */
3025 if (content2)
3026 filesz += bfd_section_size (plt2_asect);
3027 else
3028 filesz -= bfd_section_size (plt2_asect);
3029
3030 store_unsigned_integer (buf_filesz_p, 8, byte_order,
3031 filesz);
3032
3033 if (memcmp (phdrp, phdr2p, sizeof (*phdrp)) == 0)
3034 continue;
3035 }
3036
3037 return 0;
3038 }
3039 }
3040 else
3041 return 0;
3042 }
3043 }
3044
3045 if (info_verbose)
3046 {
3047 /* It can be printed repeatedly as there is no easy way to check
3048 the executable symbols/file has been already relocated to
3049 displacement. */
3050
3051 gdb_printf (_("Using PIE (Position Independent Executable) "
3052 "displacement %s for \"%s\".\n"),
3053 paddress (target_gdbarch (), exec_displacement),
3054 bfd_get_filename (current_program_space->exec_bfd ()));
3055 }
3056
3057 *displacementp = exec_displacement;
3058 return 1;
3059}
3060
3061/* Relocate the main executable. This function should be called upon
3062 stopping the inferior process at the entry point to the program.
3063 The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
3064 different, the main executable is relocated by the proper amount. */
3065
3066static void
3068{
3069 CORE_ADDR displacement;
3070
3071 /* If we are re-running this executable, SYMFILE_OBJFILE->SECTION_OFFSETS
3072 probably contains the offsets computed using the PIE displacement
3073 from the previous run, which of course are irrelevant for this run.
3074 So we need to determine the new PIE displacement and recompute the
3075 section offsets accordingly, even if SYMFILE_OBJFILE->SECTION_OFFSETS
3076 already contains pre-computed offsets.
3077
3078 If we cannot compute the PIE displacement, either:
3079
3080 - The executable is not PIE.
3081
3082 - SYMFILE_OBJFILE does not match the executable started in the target.
3083 This can happen for main executable symbols loaded at the host while
3084 `ld.so --ld-args main-executable' is loaded in the target.
3085
3086 Then we leave the section offsets untouched and use them as is for
3087 this run. Either:
3088
3089 - These section offsets were properly reset earlier, and thus
3090 already contain the correct values. This can happen for instance
3091 when reconnecting via the remote protocol to a target that supports
3092 the `qOffsets' packet.
3093
3094 - The section offsets were not reset earlier, and the best we can
3095 hope is that the old offsets are still applicable to the new run. */
3096
3097 if (! svr4_exec_displacement (&displacement))
3098 return;
3099
3100 /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
3101 addresses. */
3102
3104 if (objf)
3105 {
3106 section_offsets new_offsets (objf->section_offsets.size (),
3107 displacement);
3108 objfile_relocate (objf, new_offsets);
3109 }
3110 else if (current_program_space->exec_bfd ())
3111 {
3112 asection *asect;
3113
3114 bfd *exec_bfd = current_program_space->exec_bfd ();
3115 for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
3116 exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
3117 bfd_section_vma (asect) + displacement);
3118 }
3119}
3120
3121/* Implement the "create_inferior_hook" target_solib_ops method.
3122
3123 For SVR4 executables, this first instruction is either the first
3124 instruction in the dynamic linker (for dynamically linked
3125 executables) or the instruction at "start" for statically linked
3126 executables. For dynamically linked executables, the system
3127 first exec's /lib/libc.so.N, which contains the dynamic linker,
3128 and starts it running. The dynamic linker maps in any needed
3129 shared libraries, maps in the actual user executable, and then
3130 jumps to "start" in the user executable.
3131
3132 We can arrange to cooperate with the dynamic linker to discover the
3133 names of shared libraries that are dynamically linked, and the base
3134 addresses to which they are linked.
3135
3136 This function is responsible for discovering those names and
3137 addresses, and saving sufficient information about them to allow
3138 their symbols to be read at a later time. */
3139
3140static void
3142{
3143 struct svr4_info *info;
3144
3146
3147 /* Clear the probes-based interface's state. */
3148 free_probes_table (info);
3149 free_solib_lists (info);
3150
3151 /* Relocate the main executable if necessary. */
3153
3154 /* No point setting a breakpoint in the dynamic linker if we can't
3155 hit it (e.g., a core file, or a trace file). */
3156 if (!target_has_execution ())
3157 return;
3158
3160 return;
3161
3162 if (!enable_break (info, from_tty))
3163 return;
3164}
3165
3166static void
3168{
3169 struct svr4_info *info;
3170
3172 info->debug_base = 0;
3173 info->debug_loader_offset_p = 0;
3174 info->debug_loader_offset = 0;
3175 xfree (info->debug_loader_name);
3176 info->debug_loader_name = NULL;
3177}
3178
3179/* Clear any bits of ADDR that wouldn't fit in a target-format
3180 data pointer. "Data pointer" here refers to whatever sort of
3181 address the dynamic linker uses to manage its sections. At the
3182 moment, we don't support shared libraries on any processors where
3183 code and data pointers are different sizes.
3184
3185 This isn't really the right solution. What we really need here is
3186 a way to do arithmetic on CORE_ADDR values that respects the
3187 natural pointer/address correspondence. (For example, on the MIPS,
3188 converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
3189 sign-extend the value. There, simply truncating the bits above
3190 gdbarch_ptr_bit, as we do below, is no good.) This should probably
3191 be a new gdbarch method or something. */
3192static CORE_ADDR
3193svr4_truncate_ptr (CORE_ADDR addr)
3194{
3195 if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
3196 /* We don't need to truncate anything, and the bit twiddling below
3197 will fail due to overflow problems. */
3198 return addr;
3199 else
3200 return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
3201}
3202
3203
3204static void
3206 struct target_section *sec)
3207{
3208 bfd *abfd = sec->the_bfd_section->owner;
3209
3210 sec->addr = svr4_truncate_ptr (sec->addr + lm_addr_check (so, abfd));
3211 sec->endaddr = svr4_truncate_ptr (sec->endaddr + lm_addr_check (so, abfd));
3212}
3213
3214
3215/* Architecture-specific operations. */
3216
3218{
3219 /* Return a description of the layout of `struct link_map'. */
3220 struct link_map_offsets *(*fetch_link_map_offsets)(void) = nullptr;
3221};
3222
3223/* Per-architecture data key. */
3225
3226/* Return a default for the architecture-specific operations. */
3227
3228static struct solib_svr4_ops *
3230{
3231 struct solib_svr4_ops *ops = solib_svr4_data.get (gdbarch);
3232 if (ops == nullptr)
3233 ops = solib_svr4_data.emplace (gdbarch);
3234 return ops;
3235}
3236
3237/* Set the architecture-specific `struct link_map_offsets' fetcher for
3238 GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
3239
3240void
3252
3253/* Fetch a link_map_offsets structure using the architecture-specific
3254 `struct link_map_offsets' fetcher. */
3255
3256static struct link_map_offsets *
3258{
3259 struct solib_svr4_ops *ops = get_ops (target_gdbarch ());
3260
3261 gdb_assert (ops->fetch_link_map_offsets);
3262 return ops->fetch_link_map_offsets ();
3263}
3264
3265/* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
3266
3267static int
3269{
3270 struct solib_svr4_ops *ops = get_ops (target_gdbarch ());
3271
3272 return (ops->fetch_link_map_offsets != NULL);
3273}
3274
3275
3276/* Most OS'es that have SVR4-style ELF dynamic libraries define a
3277 `struct r_debug' and a `struct link_map' that are binary compatible
3278 with the original SVR4 implementation. */
3279
3280/* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3281 for an ILP32 SVR4 system. */
3282
3283struct link_map_offsets *
3285{
3286 static struct link_map_offsets lmo;
3287 static struct link_map_offsets *lmp = NULL;
3288
3289 if (lmp == NULL)
3290 {
3291 lmp = &lmo;
3292
3293 lmo.r_version_offset = 0;
3294 lmo.r_version_size = 4;
3295 lmo.r_map_offset = 4;
3296 lmo.r_brk_offset = 8;
3297 lmo.r_ldsomap_offset = 20;
3298 lmo.r_next_offset = -1;
3299
3300 /* Everything we need is in the first 20 bytes. */
3301 lmo.link_map_size = 20;
3302 lmo.l_addr_offset = 0;
3303 lmo.l_name_offset = 4;
3304 lmo.l_ld_offset = 8;
3305 lmo.l_next_offset = 12;
3306 lmo.l_prev_offset = 16;
3307 }
3308
3309 return lmp;
3310}
3311
3312/* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3313 for an LP64 SVR4 system. */
3314
3315struct link_map_offsets *
3317{
3318 static struct link_map_offsets lmo;
3319 static struct link_map_offsets *lmp = NULL;
3320
3321 if (lmp == NULL)
3322 {
3323 lmp = &lmo;
3324
3325 lmo.r_version_offset = 0;
3326 lmo.r_version_size = 4;
3327 lmo.r_map_offset = 8;
3328 lmo.r_brk_offset = 16;
3329 lmo.r_ldsomap_offset = 40;
3330 lmo.r_next_offset = -1;
3331
3332 /* Everything we need is in the first 40 bytes. */
3333 lmo.link_map_size = 40;
3334 lmo.l_addr_offset = 0;
3335 lmo.l_name_offset = 8;
3336 lmo.l_ld_offset = 16;
3337 lmo.l_next_offset = 24;
3338 lmo.l_prev_offset = 32;
3339 }
3340
3341 return lmp;
3342}
3343
3344
3345/* Return the DSO matching OBJFILE or nullptr if none can be found. */
3346
3347static so_list *
3349{
3350 if (objfile == nullptr)
3351 return nullptr;
3352
3353 /* If OBJFILE is a separate debug object file, look for the original
3354 object file. */
3357
3358 for (so_list *so : current_program_space->solibs ())
3359 if (so->objfile == objfile)
3360 return so;
3361
3362 return nullptr;
3363}
3364
3365/* Return the address of the r_debug object for the namespace containing
3366 SOLIB or zero if it cannot be found. This may happen when symbol files
3367 are added manually, for example, or with the main executable.
3368
3369 Current callers treat zero as initial namespace so they are doing the
3370 right thing for the main executable. */
3371
3372static CORE_ADDR
3374{
3375 if (solib == nullptr)
3376 return 0;
3377
3379 gdb_assert (info != nullptr);
3380 for (const std::pair<CORE_ADDR, so_list *> tuple
3381 : info->solib_lists)
3382 {
3383 CORE_ADDR debug_base = tuple.first;
3384 so_list *solist = tuple.second;
3385
3386 for (; solist != nullptr; solist = solist->next)
3387 if (svr4_same (solib, solist))
3388 return debug_base;
3389 }
3390
3391 return 0;
3392}
3393
3394/* Search order for ELF DSOs linked with -Bsymbolic. Those DSOs have a
3395 different rule for symbol lookup. The lookup begins here in the DSO,
3396 not in the main executable. When starting from CURRENT_OBJFILE, we
3397 stay in the same namespace as that file. Otherwise, we only consider
3398 the initial namespace. */
3399
3400static void
3403 objfile *current_objfile)
3404{
3405 bool checked_current_objfile = false;
3406 if (current_objfile != nullptr)
3407 {
3408 bfd *abfd;
3409
3410 if (current_objfile->separate_debug_objfile_backlink != nullptr)
3411 current_objfile = current_objfile->separate_debug_objfile_backlink;
3412
3413 if (current_objfile == current_program_space->symfile_object_file)
3415 else
3416 abfd = current_objfile->obfd.get ();
3417
3418 if (abfd != nullptr
3419 && gdb_bfd_scan_elf_dyntag (DT_SYMBOLIC, abfd, nullptr, nullptr) == 1)
3420 {
3421 checked_current_objfile = true;
3422 if (cb (current_objfile))
3423 return;
3424 }
3425 }
3426
3427 /* The linker namespace to iterate identified by the address of its
3428 r_debug object, defaulting to the initial namespace. */
3429 CORE_ADDR initial = elf_locate_base ();
3430 so_list *curr_solib = find_solib_for_objfile (current_objfile);
3431 CORE_ADDR debug_base = find_debug_base_for_solib (curr_solib);
3432 if (debug_base == 0)
3433 debug_base = initial;
3434
3436 {
3437 if (checked_current_objfile && objfile == current_objfile)
3438 continue;
3439
3440 /* Try to determine the namespace into which objfile was loaded.
3441
3442 If we fail, e.g. for manually added symbol files or for the main
3443 executable, we assume that they were added to the initial
3444 namespace. */
3446 CORE_ADDR solib_base = find_debug_base_for_solib (solib);
3447 if (solib_base == 0)
3448 solib_base = initial;
3449
3450 /* Ignore objfiles that were added to a different namespace. */
3451 if (solib_base != debug_base)
3452 continue;
3453
3454 if (cb (objfile))
3455 return;
3456 }
3457}
3458
3476
3478void
const char *const name
void xfree(void *)
static struct @5 attributes[]
void * xcalloc(size_t number, size_t size)
Definition alloc.c:85
struct gdbarch * target_gdbarch(void)
int target_auxv_search(const gdb::byte_vector &auxv, target_ops *ops, gdbarch *gdbarch, CORE_ADDR match, CORE_ADDR *valp)
Definition auxv.c:385
target_ops_up target_bfd_reopen(const gdb_bfd_ref_ptr &abfd)
Definition bfd-target.c:98
void enable_breakpoint(struct breakpoint *bpt)
struct breakpoint * create_solib_event_breakpoint(struct gdbarch *gdbarch, CORE_ADDR address)
void disable_breakpoint(struct breakpoint *bpt)
breakpoint_safe_range all_breakpoints_safe()
Definition breakpoint.c:712
@ bp_shlib_event
Definition breakpoint.h:149
@ bp_disabled
Definition breakpoint.h:218
@ bp_enabled
Definition breakpoint.h:220
Definition probe.h:115
virtual struct value * evaluate_argument(unsigned n, frame_info_ptr frame)=0
virtual unsigned get_argument_count(struct gdbarch *gdbarch)=0
void * get(unsigned key)
Definition registry.h:211
CORE_ADDR read_memory_typed_address(CORE_ADDR addr, struct type *type)
Definition corefile.c:336
ULONGEST read_memory_unsigned_integer(CORE_ADDR memaddr, int len, enum bfd_endian byte_order)
Definition corefile.c:306
void read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition corefile.c:238
#define SEEK_SET
Definition defs.h:101
static void store_unsigned_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, ULONGEST val)
Definition defs.h:515
bool info_verbose
Definition top.c:1941
CORE_ADDR extract_typed_address(const gdb_byte *buf, struct type *type)
Definition findvar.c:152
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:480
void exception_print(struct ui_file *file, const struct gdb_exception &e)
Definition exceptions.c:106
void exec_set_section_address(const char *filename, int index, CORE_ADDR address)
Definition exec.c:1030
struct gdbarch * get_frame_arch(frame_info_ptr this_frame)
Definition frame.c:3027
frame_info_ptr get_current_frame(void)
Definition frame.c:1670
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
Definition gdb_bfd.h:79
int gdbarch_vsyscall_range(struct gdbarch *gdbarch, struct mem_range *range)
Definition gdbarch.c:5215
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition gdbarch.c:1396
int gdbarch_addr_bit(struct gdbarch *gdbarch)
Definition gdbarch.c:1739
void set_gdbarch_iterate_over_objfiles_in_search_order(struct gdbarch *gdbarch, gdbarch_iterate_over_objfiles_in_search_order_ftype *iterate_over_objfiles_in_search_order)
CORE_ADDR gdbarch_addr_bits_remove(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition gdbarch.c:3152
CORE_ADDR gdbarch_convert_from_func_ptr_addr(struct gdbarch *gdbarch, CORE_ADDR addr, struct target_ops *targ)
Definition gdbarch.c:3135
int gdbarch_ptr_bit(struct gdbarch *gdbarch)
Definition gdbarch.c:1722
void set_gdbarch_so_ops(struct gdbarch *gdbarch, const struct target_so_ops *so_ops)
Definition gdbarch.c:3380
gdb::function_view< bool(objfile *)> iterate_over_objfiles_in_search_order_cb_ftype
Definition gdbarch.h:93
enum bfd_endian type_byte_order(const struct type *type)
Definition gdbtypes.c:3900
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
size_t size
Definition go32-nat.c:239
ptid_t inferior_ptid
Definition infcmd.c:74
struct inferior * current_inferior(void)
Definition inferior.c:55
int stop_on_solib_events
Definition infrun.c:375
int address_in_mem_range(CORE_ADDR address, const struct mem_range *r)
Definition memrange.c:38
struct bound_minimal_symbol lookup_minimal_symbol(const char *name, const char *sfile, struct objfile *objf)
Definition minsyms.c:363
bool in_gnu_ifunc_stub(CORE_ADDR pc)
Definition minsyms.c:1004
observable< struct objfile * > free_objfile
static CORE_ADDR lm_addr(struct so_list *so)
Definition nto-tdep.c:246
scoped_restore_tmpl< int > inhibit_section_map_updates(struct program_space *pspace)
Definition objfiles.c:1197
void objfile_relocate(struct objfile *objfile, const section_offsets &new_offsets)
Definition objfiles.c:676
struct obj_section * find_pc_section(CORE_ADDR pc)
Definition objfiles.c:1128
static int in_plt_section(CORE_ADDR pc)
Definition objfiles.h:972
std::vector< probe * > find_probes_in_objfile(struct objfile *objfile, const char *provider, const char *name)
Definition probe.c:216
struct program_space * current_program_space
Definition progspace.c:40
CORE_ADDR regcache_read_pc(struct regcache *regcache)
Definition regcache.c:1333
struct regcache * get_thread_arch_regcache(process_stratum_target *target, ptid_t ptid, struct gdbarch *gdbarch)
Definition regcache.c:384
struct regcache * get_current_regcache(void)
Definition regcache.c:429
static int enable_break(void)
Definition solib-dsbt.c:673
static CORE_ADDR interp_text_sect_high
Definition solib-frv.c:427
static CORE_ADDR interp_plt_sect_high
Definition solib-frv.c:429
static struct so_list * svr4_default_sos(svr4_info *info)
static void svr4_create_probe_breakpoints(svr4_info *info, struct gdbarch *gdbarch, const std::vector< probe * > *probes, struct objfile *objfile)
static struct so_list * svr4_current_sos(void)
static so_list * find_solib_for_objfile(struct objfile *objfile)
static int svr4_read_so_list(svr4_info *info, CORE_ADDR lm, CORE_ADDR prev_lm, struct so_list ***link_ptr_ptr, int ignore_first)
static bool svr4_find_and_create_probe_breakpoints(svr4_info *info, struct gdbarch *gdbarch, struct obj_section *os, bool with_prefix)
static bool svr4_update_solib_event_breakpoint(struct breakpoint *b)
static void svr4_create_solib_event_breakpoints(svr4_info *info, struct gdbarch *gdbarch, CORE_ADDR address)
static CORE_ADDR find_debug_base_for_solib(so_list *solib)
void set_solib_svr4_fetch_link_map_offsets(struct gdbarch *gdbarch, struct link_map_offsets *(*flmo)(void))
static CORE_ADDR solib_svr4_r_ldsomap(struct svr4_info *info)
Definition solib-svr4.c:802
static enum probe_action solib_event_probe_action(struct probe_and_action *pa)
static void svr4_relocate_main_executable(void)
const struct target_so_ops svr4_so_ops
static so_list * svr4_collect_probes_sos(svr4_info *info)
static hashval_t hash_probe_and_action(const void *p)
static void free_solib_lists(svr4_info *info)
Definition solib-svr4.c:412
static CORE_ADDR elf_locate_base(void)
Definition solib-svr4.c:693
#define NUM_PROBES
Definition solib-svr4.c:141
static struct svr4_info * get_svr4_info(program_space *pspace)
Definition solib-svr4.c:430
static int svr4_current_sos_via_xfer_libraries(struct svr4_library_list *list, const char *annex)
probe_action
Definition solib-svr4.c:97
@ FULL_RELOAD
Definition solib-svr4.c:107
@ UPDATE_OR_RELOAD
Definition solib-svr4.c:112
@ DO_NOTHING
Definition solib-svr4.c:104
@ PROBES_INTERFACE_FAILED
Definition solib-svr4.c:100
static CORE_ADDR solib_svr4_r_map(CORE_ADDR debug_base)
Definition solib-svr4.c:767
static CORE_ADDR lm_addr_check(const struct so_list *so, bfd *abfd)
Definition solib-svr4.c:231
static void probes_table_remove_objfile_probes(struct objfile *objfile)
static const registry< gdbarch >::key< struct solib_svr4_ops > solib_svr4_data
static void svr4_iterate_over_objfiles_in_search_order(gdbarch *gdbarch, iterate_over_objfiles_in_search_order_cb_ftype cb, objfile *current_objfile)
static void svr4_handle_solib_event(void)
int svr4_in_dynsym_resolve_code(CORE_ADDR pc)
static int svr4_exec_displacement(CORE_ADDR *displacementp)
static const registry< program_space >::key< svr4_info > solib_svr4_pspace_data
Definition solib-svr4.c:391
static gdb::optional< gdb::byte_vector > find_program_interpreter(void)
Definition solib-svr4.c:589
static void svr4_update_solib_event_breakpoints(void)
static struct so_list * svr4_current_sos_1(svr4_info *info)
static int solist_update_full(struct svr4_info *info)
struct link_map_offsets * svr4_lp64_fetch_link_map_offsets(void)
static int svr4_have_link_map_offsets(void)
static void free_probes_table(struct svr4_info *info)
Definition solib-svr4.c:404
static void svr4_current_sos_direct(struct svr4_info *info)
static void svr4_relocate_section_addresses(struct so_list *so, struct target_section *sec)
static struct so_list * svr4_copy_library_list(struct so_list *src)
CORE_ADDR svr4_fetch_objfile_link_map(struct objfile *objfile)
static int svr4_keep_data_in_core(CORE_ADDR vaddr, unsigned long size)
Definition solib-svr4.c:866
static struct solib_svr4_ops * get_ops(struct gdbarch *gdbarch)
static int open_symbol_file_object(int from_tty)
Definition solib-svr4.c:891
static gdb::optional< gdb::byte_vector > read_program_header(int type, int *p_arch_size, CORE_ADDR *base_addr)
Definition solib-svr4.c:456
static void svr4_free_so(struct so_list *so)
Definition solib-svr4.c:974
static void svr4_free_objfile_observer(struct objfile *objfile)
Definition solib-svr4.c:966
static void svr4_solib_create_inferior_hook(int from_tty)
static void svr4_clear_so(struct so_list *so)
Definition solib-svr4.c:984
static int scan_dyntag_auxv(const int desired_dyntag, CORE_ADDR *ptr, CORE_ADDR *ptr_addr)
Definition solib-svr4.c:623
static CORE_ADDR solib_svr4_r_next(CORE_ADDR debug_base)
Definition solib-svr4.c:832
static const char *const bkpt_names[]
Definition solib-svr4.c:80
static int probes_table_htab_remove_objfile_probes(void **slot, void *info)
static void disable_probes_interface(svr4_info *info)
static void svr4_free_library_list(so_list *solist)
Definition solib-svr4.c:995
static bool svr4_is_default_namespace(const svr4_info *info, CORE_ADDR debug_base)
Definition solib-svr4.c:396
static std::unique_ptr< lm_info_svr4 > lm_info_read(CORE_ADDR lm_addr)
Definition solib-svr4.c:191
static void svr4_clear_solib(void)
static int has_lm_dynamic_from_link_map(void)
Definition solib-svr4.c:223
static struct link_map_offsets * svr4_fetch_link_map_offsets(void)
static CORE_ADDR solib_svr4_r_brk(struct svr4_info *info)
Definition solib-svr4.c:789
static int svr4_same(struct so_list *gdb, struct so_list *inferior)
Definition solib-svr4.c:176
static const char *const main_name_list[]
Definition solib-svr4.c:88
static int match_main(const char *)
static CORE_ADDR exec_entry_point(struct bfd *abfd, struct target_ops *targ)
static int svr4_same_1(const char *gdb_so_name, const char *inferior_so_name)
Definition solib-svr4.c:147
void _initialize_svr4_solib()
static int solist_update_incremental(svr4_info *info, CORE_ADDR debug_base, CORE_ADDR lm)
static gdb::optional< gdb::byte_vector > read_program_headers_from_bfd(bfd *abfd)
static CORE_ADDR svr4_truncate_ptr(CORE_ADDR addr)
static struct probe_and_action * solib_event_probe_at(struct svr4_info *info, CORE_ADDR address)
static int equal_probe_and_action(const void *p1, const void *p2)
static void register_solib_event_probe(svr4_info *info, struct objfile *objfile, probe *prob, CORE_ADDR address, enum probe_action action)
struct link_map_offsets * svr4_ilp32_fetch_link_map_offsets(void)
static const char *const solib_break_names[]
Definition solib-svr4.c:68
void free_so(struct so_list *so)
Definition solib.c:671
void solib_add(const char *pattern, int from_tty, int readsyms)
Definition solib.c:990
gdb_bfd_ref_ptr solib_bfd_open(const char *pathname)
Definition solib.c:440
CORE_ADDR gdb_bfd_lookup_symbol(bfd *abfd, gdb::function_view< bool(const asymbol *)> match_sym)
Definition solib.c:1720
int gdb_bfd_scan_elf_dyntag(const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr, CORE_ADDR *ptr_addr)
Definition solib.c:1541
#define solib_debug_printf(fmt,...)
Definition solib.h:39
#define SOLIB_SCOPED_DEBUG_START_END(fmt,...)
Definition solib.h:42
std::unique_ptr< so_list, so_deleter > so_list_up
Definition solist.h:178
#define SO_NAME_MAX_PATH_SIZE
Definition solist.h:22
CORE_ADDR value_address() const
Definition minsyms.h:41
struct minimal_symbol * minsym
Definition minsyms.h:49
bptype type
Definition breakpoint.h:798
bp_location_range locations() const
enum enable_state enable_state
Definition breakpoint.h:800
struct type * builtin_data_ptr
Definition gdbtypes.h:2135
gdb::unique_xmalloc_ptr< void > value
Definition xml-support.h:78
CORE_ADDR l_ld
Definition solib-svr4.h:48
CORE_ADDR l_prev
Definition solib-svr4.h:48
CORE_ADDR l_addr
Definition solib-svr4.h:41
CORE_ADDR l_name
Definition solib-svr4.h:48
CORE_ADDR l_addr_inferior
Definition solib-svr4.h:41
CORE_ADDR l_next
Definition solib-svr4.h:48
CORE_ADDR lm_addr
Definition solib-svr4.h:45
int length
Definition memrange.h:48
struct objfile * separate_debug_objfile_backlink
Definition objfiles.h:830
struct program_space * pspace
Definition objfiles.h:728
gdb_bfd_ref_ptr obfd
Definition objfiles.h:740
::section_offsets section_offsets
Definition objfiles.h:786
CORE_ADDR address
struct objfile * objfile
enum probe_action action
enum probe_action action
Definition solib-svr4.c:123
const char * name
Definition solib-svr4.c:120
objfiles_range objfiles()
Definition progspace.h:209
bfd * exec_bfd() const
Definition progspace.h:268
struct objfile * symfile_object_file
Definition progspace.h:357
so_list_range solibs() const
Definition progspace.h:260
char so_name[SO_NAME_MAX_PATH_SIZE]
Definition solist.h:56
struct so_list * next
Definition solist.h:40
char so_original_name[SO_NAME_MAX_PATH_SIZE]
Definition solist.h:53
lm_info_base * lm_info
Definition solist.h:46
struct link_map_offsets *(* fetch_link_map_offsets)(void)
CORE_ADDR debug_loader_offset
Definition solib-svr4.c:349
svr4_info()=default
char * debug_loader_name
Definition solib-svr4.c:352
bool using_xfer
Definition solib-svr4.c:364
int debug_loader_offset_p
Definition solib-svr4.c:346
CORE_ADDR main_lm_addr
Definition solib-svr4.c:355
CORE_ADDR debug_base
Definition solib-svr4.c:343
std::map< CORE_ADDR, so_list * > solib_lists
Definition solib-svr4.c:387
CORE_ADDR interp_text_sect_high
Definition solib-svr4.c:358
CORE_ADDR interp_text_sect_low
Definition solib-svr4.c:357
CORE_ADDR interp_plt_sect_high
Definition solib-svr4.c:360
htab_up probes_table
Definition solib-svr4.c:370
CORE_ADDR interp_plt_sect_low
Definition solib-svr4.c:359
CORE_ADDR main_lm
Definition solib-svr4.c:954
std::map< CORE_ADDR, so_list * > solib_lists
Definition solib-svr4.c:960
so_list ** tailp
Definition solib-svr4.c:950
CORE_ADDR endaddr
struct bfd_section * the_bfd_section
ULONGEST length() const
Definition gdbtypes.h:983
Definition value.h:130
@ SYMFILE_VERBOSE
bool auto_solib_add
Definition symfile.c:149
void symbol_file_add_main(const char *args, symfile_add_flags add_flags)
Definition symfile.c:1186
std::vector< CORE_ADDR > section_offsets
Definition symtab.h:1669
int target_read_string(CORE_ADDR addr, int len, int width, unsigned int fetchlimit, gdb::unique_xmalloc_ptr< gdb_byte > *buffer, int *bytes_read)
Definition target.c:65
gdb::optional< gdb::char_vector > target_read_stralloc(struct target_ops *ops, enum target_object object, const char *annex)
Definition target.c:2321
bool target_has_execution(inferior *inf)
Definition target.c:201
int target_read_memory(CORE_ADDR memaddr, gdb_byte *myaddr, ssize_t len)
Definition target.c:1785
bool target_augmented_libraries_svr4_read()
Definition target.c:809
@ TARGET_OBJECT_LIBRARIES_SVR4
Definition target.h:178
std::unique_ptr< target_ops, target_ops_deleter > target_ops_up
Definition target.h:1347
int query(const char *ctlstr,...)
Definition utils.c:943
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition utils.c:3166
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stderr
Definition utils.h:187
CORE_ADDR value_as_address(struct value *val)
Definition value.c:2636
LONGEST value_as_long(struct value *val)
Definition value.c:2554
const char version[]
Definition version.c:2
@ GDB_XML_EF_NONE
@ GDB_XML_EF_REPEATABLE
@ GDB_XML_EF_OPTIONAL
void void gdb_xml_error(struct gdb_xml_parser *parser, const char *format,...) ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF(2
void void struct gdb_xml_value * xml_find_attribute(std::vector< gdb_xml_value > &attributes, const char *name)
int gdb_xml_parse_quick(const char *name, const char *dtd_name, const struct gdb_xml_element *elements, const char *document, void *user_data)
gdb_xml_attribute_handler gdb_xml_parse_attr_ulongest
@ GDB_XML_AF_OPTIONAL
@ GDB_XML_AF_NONE