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