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