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