1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "elf/external.h"
23 #include "elf/common.h"
35 #include "gdbthread.h"
40 #include "solib-svr4.h"
42 #include "bfd-target.h"
50 #define NOTE_GNU_BUILD_ID_NAME ".note.gnu.build-id"
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
);
57 /* Link map info to include in an allocated so_list entry. */
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
68 CORE_ADDR l_addr
, l_addr_inferior
;
69 unsigned int l_addr_p
: 1;
71 /* The target location of lm. */
74 /* Values read in from inferior's fields of the same name. */
75 CORE_ADDR l_ld
, l_next
, l_prev
, l_name
;
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
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. */
86 static const char * const solib_break_names
[] =
92 "__dl_rtld_db_dlactivity",
98 static const char * const bkpt_names
[] =
106 static const char * const main_name_list
[] =
112 /* What to do when a probe stop occurs. */
116 /* Something went seriously wrong. Stop using probes and
117 revert to using the older interface. */
118 PROBES_INTERFACE_FAILED
,
120 /* No action is required. The shared object list is still
124 /* The shared object list should be reloaded entirely. */
127 /* Attempt to incrementally update the shared object list. If
128 the update fails or is not possible, fall back to reloading
133 /* A probe's name and its associated action. */
137 /* The name of the probe. */
140 /* What to do when a probe stop occurs. */
141 enum probe_action action
;
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. */
148 static const struct probe_info probe_info
[] =
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
},
159 #define NUM_PROBES ARRAY_SIZE (probe_info)
161 /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
162 the same shared library. */
165 svr4_same_1 (const char *gdb_so_name
, const char *inferior_so_name
)
167 if (strcmp (gdb_so_name
, inferior_so_name
) == 0)
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)
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)
190 svr4_same (struct so_list
*gdb
, struct so_list
*inferior
)
192 return (svr4_same_1 (gdb
->so_original_name
, inferior
->so_original_name
));
195 static struct lm_info
*
196 lm_info_read (CORE_ADDR lm_addr
)
198 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
200 struct lm_info
*lm_info
;
201 struct cleanup
*back_to
;
203 lm
= xmalloc (lmo
->link_map_size
);
204 back_to
= make_cleanup (xfree
, lm
);
206 if (target_read_memory (lm_addr
, lm
, lmo
->link_map_size
) != 0)
208 warning (_("Error reading shared library list entry at %s"),
209 paddress (target_gdbarch (), lm_addr
)),
214 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
216 lm_info
= xzalloc (sizeof (*lm_info
));
217 lm_info
->lm_addr
= lm_addr
;
219 lm_info
->l_addr_inferior
= extract_typed_address (&lm
[lmo
->l_addr_offset
],
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
],
224 lm_info
->l_prev
= extract_typed_address (&lm
[lmo
->l_prev_offset
],
226 lm_info
->l_name
= extract_typed_address (&lm
[lmo
->l_name_offset
],
230 do_cleanups (back_to
);
236 has_lm_dynamic_from_link_map (void)
238 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
240 return lmo
->l_ld_offset
>= 0;
244 lm_addr_check (const struct so_list
*so
, bfd
*abfd
)
246 if (!so
->lm_info
->l_addr_p
)
248 struct bfd_section
*dyninfo_sect
;
249 CORE_ADDR l_addr
, l_dynaddr
, dynaddr
;
251 l_addr
= so
->lm_info
->l_addr_inferior
;
253 if (! abfd
|| ! has_lm_dynamic_from_link_map ())
256 l_dynaddr
= so
->lm_info
->l_ld
;
258 dyninfo_sect
= bfd_get_section_by_name (abfd
, ".dynamic");
259 if (dyninfo_sect
== NULL
)
262 dynaddr
= bfd_section_vma (abfd
, dyninfo_sect
);
264 if (dynaddr
+ l_addr
!= l_dynaddr
)
266 CORE_ADDR align
= 0x1000;
267 CORE_ADDR minpagesize
= align
;
269 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
271 Elf_Internal_Ehdr
*ehdr
= elf_tdata (abfd
)->elf_header
;
272 Elf_Internal_Phdr
*phdr
= elf_tdata (abfd
)->phdr
;
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
;
281 minpagesize
= get_elf_backend_data (abfd
)->minpagesize
;
284 /* Turn it into a mask. */
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
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!
305 (In the usual case where (l_addr & align) == 0, this check is
306 equivalent to the possibly expected check above.)
308 Even on PPC it must be zero-aligned at least for MINPAGESIZE. */
310 l_addr
= l_dynaddr
- dynaddr
;
312 if ((l_addr
& (minpagesize
- 1)) == 0
313 && (l_addr
& align
) == ((l_dynaddr
- dynaddr
) & align
))
316 printf_unfiltered (_("Using PIC (Position Independent Code) "
317 "prelink displacement %s for \"%s\".\n"),
318 paddress (target_gdbarch (), l_addr
),
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. */
332 warning (_(".dynamic section for \"%s\" "
333 "is not at the expected address "
334 "(wrong library or version mismatch?)"), so
->so_name
);
339 so
->lm_info
->l_addr
= l_addr
;
340 so
->lm_info
->l_addr_p
= 1;
343 return so
->lm_info
->l_addr
;
346 /* Per pspace SVR4 specific data. */
350 CORE_ADDR debug_base
; /* Base of dynamic linker structures. */
352 /* Validity flag for debug_loader_offset. */
353 int debug_loader_offset_p
;
355 /* Load address for the dynamic linker, inferred. */
356 CORE_ADDR debug_loader_offset
;
358 /* Name of the dynamic linker, valid if debug_loader_offset_p. */
359 char *debug_loader_name
;
361 /* Load map address for the main executable. */
362 CORE_ADDR main_lm_addr
;
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
;
369 /* Nonzero if the list of objects was last obtained from the target
370 via qXfer:libraries-svr4:read. */
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. */
379 /* List of objects loaded into the inferior, used by the probes-
381 struct so_list
*solib_list
;
384 /* Per-program-space data key. */
385 static const struct program_space_data
*solib_svr4_pspace_data
;
387 /* Free the probes table. */
390 free_probes_table (struct svr4_info
*info
)
392 if (info
->probes_table
== NULL
)
395 htab_delete (info
->probes_table
);
396 info
->probes_table
= NULL
;
399 /* Free the solib list. */
402 free_solib_list (struct svr4_info
*info
)
404 svr4_free_library_list (&info
->solib_list
);
405 info
->solib_list
= NULL
;
409 svr4_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
411 struct svr4_info
*info
= arg
;
413 free_probes_table (info
);
414 free_solib_list (info
);
419 /* Get the current svr4 data. If none is found yet, add it now. This
420 function always returns a valid object. */
422 static struct svr4_info
*
425 struct svr4_info
*info
;
427 info
= program_space_data (current_program_space
, solib_svr4_pspace_data
);
431 info
= XCNEW (struct svr4_info
);
432 set_program_space_data (current_program_space
, solib_svr4_pspace_data
, info
);
436 /* Local function prototypes */
438 static int match_main (const char *);
440 /* Read program header TYPE from inferior memory. The header is found
441 by scanning the OS auxillary vector.
443 If TYPE == -1, return the program headers instead of the contents of
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. */
452 read_program_header (int type
, int *p_sect_size
, int *p_arch_size
)
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
;
461 /* Get required auxv elements from target. */
462 if (target_auxv_search (¤t_target
, AT_PHDR
, &at_phdr
) <= 0)
464 if (target_auxv_search (¤t_target
, AT_PHENT
, &at_phent
) <= 0)
466 if (target_auxv_search (¤t_target
, AT_PHNUM
, &at_phnum
) <= 0)
468 if (!at_phdr
|| !at_phnum
)
471 /* Determine ELF architecture type. */
472 if (at_phent
== sizeof (Elf32_External_Phdr
))
474 else if (at_phent
== sizeof (Elf64_External_Phdr
))
479 /* Find the requested segment. */
483 sect_size
= at_phent
* at_phnum
;
485 else if (arch_size
== 32)
487 Elf32_External_Phdr phdr
;
490 /* Search for requested PHDR. */
491 for (i
= 0; i
< at_phnum
; i
++)
495 if (target_read_memory (at_phdr
+ i
* sizeof (phdr
),
496 (gdb_byte
*)&phdr
, sizeof (phdr
)))
499 p_type
= extract_unsigned_integer ((gdb_byte
*) phdr
.p_type
,
502 if (p_type
== PT_PHDR
)
505 pt_phdr
= extract_unsigned_integer ((gdb_byte
*) phdr
.p_vaddr
,
516 /* Retrieve address and size. */
517 sect_addr
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_vaddr
,
519 sect_size
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_memsz
,
524 Elf64_External_Phdr phdr
;
527 /* Search for requested PHDR. */
528 for (i
= 0; i
< at_phnum
; i
++)
532 if (target_read_memory (at_phdr
+ i
* sizeof (phdr
),
533 (gdb_byte
*)&phdr
, sizeof (phdr
)))
536 p_type
= extract_unsigned_integer ((gdb_byte
*) phdr
.p_type
,
539 if (p_type
== PT_PHDR
)
542 pt_phdr
= extract_unsigned_integer ((gdb_byte
*) phdr
.p_vaddr
,
553 /* Retrieve address and size. */
554 sect_addr
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_vaddr
,
556 sect_size
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_memsz
,
560 /* PT_PHDR is optional, but we really need it
561 for PIE to make this work in general. */
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
);
570 /* Read in requested program header. */
571 buf
= xmalloc (sect_size
);
572 if (target_read_memory (sect_addr
, buf
, sect_size
))
579 *p_arch_size
= arch_size
;
581 *p_sect_size
= sect_size
;
587 /* Return program interpreter string. */
589 find_program_interpreter (void)
591 gdb_byte
*buf
= NULL
;
593 /* If we have an exec_bfd, use its section table. */
595 && bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
597 struct bfd_section
*interp_sect
;
599 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
600 if (interp_sect
!= NULL
)
602 int sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
604 buf
= xmalloc (sect_size
);
605 bfd_get_section_contents (exec_bfd
, interp_sect
, buf
, 0, sect_size
);
609 /* If we didn't find it, use the target auxillary vector. */
611 buf
= read_program_header (PT_INTERP
, NULL
, NULL
);
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. */
621 scan_dyntag (const int desired_dyntag
, bfd
*abfd
, CORE_ADDR
*ptr
)
623 int arch_size
, step
, sect_size
;
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
;
635 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
638 arch_size
= bfd_get_arch_size (abfd
);
642 /* Find the start address of the .dynamic section. */
643 sect
= bfd_get_section_by_name (abfd
, ".dynamic");
647 for (target_section
= current_target_sections
->sections
;
648 target_section
< current_target_sections
->sections_end
;
650 if (sect
== target_section
->the_bfd_section
)
652 if (target_section
< current_target_sections
->sections_end
)
653 dyn_addr
= target_section
->addr
;
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. */
661 dyn_addr
= bfd_section_vma (abfd
, sect
);
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
,
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
;
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
);
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
);
691 if (current_dyntag
== DT_NULL
)
693 if (current_dyntag
== desired_dyntag
)
695 /* If requested, try to read the runtime value of this .dynamic
699 struct type
*ptr_type
;
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
);
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. */
721 scan_dyntag_auxv (const int desired_dyntag
, CORE_ADDR
*ptr
)
723 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
724 int sect_size
, arch_size
, step
;
727 gdb_byte
*bufend
, *bufstart
, *buf
;
729 /* Read in .dynamic section. */
730 buf
= bufstart
= read_program_header (PT_DYNAMIC
, §_size
, &arch_size
);
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
;
743 Elf32_External_Dyn
*dynp
= (Elf32_External_Dyn
*) buf
;
745 current_dyntag
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_tag
,
747 dyn_ptr
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_un
.d_ptr
,
752 Elf64_External_Dyn
*dynp
= (Elf64_External_Dyn
*) buf
;
754 current_dyntag
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_tag
,
756 dyn_ptr
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_un
.d_ptr
,
759 if (current_dyntag
== DT_NULL
)
762 if (current_dyntag
== desired_dyntag
)
776 /* Locate the base address of dynamic linker structs for SVR4 elf
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. */
789 elf_locate_base (void)
791 struct bound_minimal_symbol msymbol
;
794 /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
795 instead of DT_DEBUG, although they sometimes contain an unused
797 if (scan_dyntag (DT_MIPS_RLD_MAP
, exec_bfd
, &dyn_ptr
)
798 || scan_dyntag_auxv (DT_MIPS_RLD_MAP
, &dyn_ptr
))
800 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
802 int pbuf_size
= TYPE_LENGTH (ptr_type
);
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
))
809 return extract_typed_address (pbuf
, ptr_type
);
813 if (scan_dyntag (DT_DEBUG
, exec_bfd
, &dyn_ptr
)
814 || scan_dyntag_auxv (DT_DEBUG
, &dyn_ptr
))
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
);
823 /* DT_DEBUG entry not found. */
827 /* Locate the base address of dynamic linker structs.
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).
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.
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. */
853 locate_base (struct svr4_info
*info
)
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. */
861 if (info
->debug_base
== 0 && svr4_have_link_map_offsets ())
862 info
->debug_base
= elf_locate_base ();
863 return info
->debug_base
;
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.
870 FIXME: Perhaps we should validate the info somehow, perhaps by
871 checking r_version for a known version number, or r_state for
875 solib_svr4_r_map (struct svr4_info
*info
)
877 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
878 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
883 addr
= read_memory_typed_address (info
->debug_base
+ lmo
->r_map_offset
,
886 CATCH (ex
, RETURN_MASK_ERROR
)
888 exception_print (gdb_stderr
, ex
);
895 /* Find r_brk from the inferior's debug base. */
898 solib_svr4_r_brk (struct svr4_info
*info
)
900 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
901 struct type
*ptr_type
= builtin_type (target_gdbarch ())->builtin_data_ptr
;
903 return read_memory_typed_address (info
->debug_base
+ lmo
->r_brk_offset
,
907 /* Find the link map for the dynamic linker (if it is not in the
908 normal list of loaded shared objects). */
911 solib_svr4_r_ldsomap (struct svr4_info
*info
)
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;
920 /* Check version, and return zero if `struct r_debug' doesn't have
921 the r_ldsomap member. */
923 = read_memory_unsigned_integer (info
->debug_base
+ lmo
->r_version_offset
,
924 lmo
->r_version_size
, byte_order
);
926 CATCH (ex
, RETURN_MASK_ERROR
)
928 exception_print (gdb_stderr
, ex
);
932 if (version
< 2 || lmo
->r_ldsomap_offset
== -1)
935 return read_memory_typed_address (info
->debug_base
+ lmo
->r_ldsomap_offset
,
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
947 svr4_keep_data_in_core (CORE_ADDR vaddr
, unsigned long size
)
949 struct svr4_info
*info
;
951 struct so_list
*newobj
;
952 struct cleanup
*old_chain
;
955 info
= get_svr4_info ();
957 info
->debug_base
= 0;
959 if (!info
->debug_base
)
962 ldsomap
= solib_svr4_r_ldsomap (info
);
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
);
973 return (name_lm
>= vaddr
&& name_lm
< vaddr
+ size
);
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. */
981 svr4_validate (const struct so_list
*const so
)
983 const bfd_byte
*local_id
;
986 gdb_assert (so
!= NULL
);
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
)
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. */
996 || !bfd_check_format (so
->abfd
, bfd_object
))
999 /* GDB has verified the local file really does not contain the build ID. */
1000 if (so
->abfd
->build_id
== NULL
)
1004 remote_hex
= alloca (so
->build_idsz
* 2 + 1);
1005 bin2hex (so
->build_id
, remote_hex
, so
->build_idsz
);
1007 return xstrprintf (_("remote build ID is %s "
1008 "but local file does not have build ID"),
1012 local_id
= so
->abfd
->build_id
->data
;
1013 local_idsz
= so
->abfd
->build_id
->size
;
1015 if (so
->build_idsz
!= local_idsz
1016 || memcmp (so
->build_id
, local_id
, so
->build_idsz
) != 0)
1018 char *remote_hex
, *local_hex
;
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
);
1025 return xstrprintf (_("remote build ID %s "
1026 "does not match local build ID %s"),
1027 remote_hex
, local_hex
);
1030 /* Both build IDs are present and they match. */
1034 /* Implement the "open_symbol_file_object" target_so_ops method.
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. */
1042 open_symbol_file_object (void *from_ttyp
)
1044 CORE_ADDR lm
, l_name
;
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 ();
1055 if (symfile_objfile
)
1056 if (!query (_("Attempt to reload symbols from process? ")))
1058 do_cleanups (cleanups
);
1062 /* Always locate the debug struct, in case it has moved. */
1063 info
->debug_base
= 0;
1064 if (locate_base (info
) == 0)
1066 do_cleanups (cleanups
);
1067 return 0; /* failed somehow... */
1070 /* First link map member should be the executable. */
1071 lm
= solib_svr4_r_map (info
);
1074 do_cleanups (cleanups
);
1075 return 0; /* failed somehow... */
1078 /* Read address of name from target memory to GDB. */
1079 read_memory (lm
+ lmo
->l_name_offset
, l_name_buf
, l_name_size
);
1081 /* Convert the address to host format. */
1082 l_name
= extract_typed_address (l_name_buf
, ptr_type
);
1086 do_cleanups (cleanups
);
1087 return 0; /* No filename. */
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
);
1096 warning (_("failed to read exec filename from attached file: %s"),
1097 safe_strerror (errcode
));
1098 do_cleanups (cleanups
);
1102 /* Have a pathname: read the symbol file. */
1103 symbol_file_add_main (filename
, from_tty
);
1105 do_cleanups (cleanups
);
1109 /* Data exchange structure for the XML parser as returned by
1110 svr4_current_sos_via_xfer_libraries. */
1112 struct svr4_library_list
1114 struct so_list
*head
, **tailp
;
1116 /* Inferior address of struct link_map used for the main executable. It is
1117 NULL if not known. */
1121 /* Implementation for target_so_ops.free_so. */
1124 svr4_free_so (struct so_list
*so
)
1126 xfree (so
->lm_info
);
1129 /* Implement target_so_ops.clear_so. */
1132 svr4_clear_so (struct so_list
*so
)
1134 if (so
->lm_info
!= NULL
)
1135 so
->lm_info
->l_addr_p
= 0;
1138 /* Free so_list built so far (called via cleanup). */
1141 svr4_free_library_list (void *p_list
)
1143 struct so_list
*list
= *(struct so_list
**) p_list
;
1145 while (list
!= NULL
)
1147 struct so_list
*next
= list
->next
;
1154 /* Copy library list. */
1156 static struct so_list
*
1157 svr4_copy_library_list (struct so_list
*src
)
1159 struct so_list
*dst
= NULL
;
1160 struct so_list
**link
= &dst
;
1164 struct so_list
*newobj
;
1166 newobj
= xmalloc (sizeof (struct so_list
));
1167 memcpy (newobj
, src
, sizeof (struct so_list
));
1169 newobj
->lm_info
= xmalloc (sizeof (struct lm_info
));
1170 memcpy (newobj
->lm_info
, src
->lm_info
, sizeof (struct lm_info
));
1172 if (newobj
->build_id
!= NULL
)
1174 newobj
->build_id
= xmalloc (src
->build_idsz
);
1175 memcpy (newobj
->build_id
, src
->build_id
, src
->build_idsz
);
1178 newobj
->next
= NULL
;
1180 link
= &newobj
->next
;
1188 #ifdef HAVE_LIBEXPAT
1190 #include "xml-support.h"
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. */
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
)
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
;
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
;
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
)
1221 const size_t hex_build_id_len
= strlen (hex_build_id
);
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
);
1232 const size_t build_idsz
= hex_build_id_len
/ 2;
1234 new_elem
->build_id
= xmalloc (build_idsz
);
1235 new_elem
->build_idsz
= hex2bin (hex_build_id
, new_elem
->build_id
,
1237 if (new_elem
->build_idsz
!= build_idsz
)
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;
1251 *list
->tailp
= new_elem
;
1252 list
->tailp
= &new_elem
->next
;
1255 /* Handle the start of a <library-list-svr4> element. */
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
)
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");
1266 if (strcmp (version
, "1.0") != 0)
1267 gdb_xml_error (parser
,
1268 _("SVR4 Library list has unsupported version \"%s\""),
1272 list
->main_lm
= *(ULONGEST
*) main_lm
->value
;
1275 /* The allowed elements and attributes for an XML library list.
1276 The root element is a <library-list>. */
1278 static const struct gdb_xml_attribute svr4_library_attributes
[] =
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
}
1288 static const struct gdb_xml_element svr4_library_list_children
[] =
1291 "library", svr4_library_attributes
, NULL
,
1292 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
1293 library_list_start_library
, NULL
1295 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
1298 static const struct gdb_xml_attribute svr4_library_list_attributes
[] =
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
}
1305 static const struct gdb_xml_element svr4_library_list_elements
[] =
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
}
1312 /* Parse qXfer:libraries:read packet into *SO_LIST_RETURN. Return 1 if
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. */
1319 svr4_parse_libraries (const char *document
, struct svr4_library_list
*list
)
1321 struct cleanup
*back_to
= make_cleanup (svr4_free_library_list
,
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)
1329 /* Parsed successfully, keep the result. */
1330 discard_cleanups (back_to
);
1334 do_cleanups (back_to
);
1338 /* Attempt to get so_list from target via qXfer:libraries-svr4:read packet.
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.
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 (). */
1349 svr4_current_sos_via_xfer_libraries (struct svr4_library_list
*list
,
1352 char *svr4_library_document
;
1354 struct cleanup
*back_to
;
1356 gdb_assert (annex
== NULL
|| target_augmented_libraries_svr4_read ());
1358 /* Fetch the list of shared libraries. */
1359 svr4_library_document
= target_read_stralloc (¤t_target
,
1360 TARGET_OBJECT_LIBRARIES_SVR4
,
1362 if (svr4_library_document
== NULL
)
1365 back_to
= make_cleanup (xfree
, svr4_library_document
);
1366 result
= svr4_parse_libraries (svr4_library_document
, list
);
1367 do_cleanups (back_to
);
1375 svr4_current_sos_via_xfer_libraries (struct svr4_library_list
*list
,
1383 /* If no shared library information is available from the dynamic
1384 linker, build a fallback list from other sources. */
1386 static struct so_list
*
1387 svr4_default_sos (void)
1389 struct svr4_info
*info
= get_svr4_info ();
1390 struct so_list
*newobj
;
1392 if (!info
->debug_loader_offset_p
)
1395 newobj
= XCNEW (struct so_list
);
1397 newobj
->lm_info
= xzalloc (sizeof (struct lm_info
));
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;
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
);
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. */
1419 svr4_read_so_list (CORE_ADDR lm
, CORE_ADDR prev_lm
,
1420 struct so_list
***link_ptr_ptr
, int ignore_first
)
1422 CORE_ADDR first_l_name
= 0;
1425 for (; lm
!= 0; prev_lm
= lm
, lm
= next_lm
)
1427 struct so_list
*newobj
;
1428 struct cleanup
*old_chain
;
1432 newobj
= XCNEW (struct so_list
);
1433 old_chain
= make_cleanup_free_so (newobj
);
1435 newobj
->lm_info
= lm_info_read (lm
);
1436 if (newobj
->lm_info
== NULL
)
1438 do_cleanups (old_chain
);
1442 next_lm
= newobj
->lm_info
->l_next
;
1444 if (newobj
->lm_info
->l_prev
!= prev_lm
)
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
);
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)
1460 struct svr4_info
*info
= get_svr4_info ();
1462 first_l_name
= newobj
->lm_info
->l_name
;
1463 info
->main_lm_addr
= newobj
->lm_info
->lm_addr
;
1464 do_cleanups (old_chain
);
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
);
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
);
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
);
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
))
1493 do_cleanups (old_chain
);
1497 discard_cleanups (old_chain
);
1499 **link_ptr_ptr
= newobj
;
1500 *link_ptr_ptr
= &newobj
->next
;
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. */
1511 static struct so_list
*
1512 svr4_current_sos_direct (struct svr4_info
*info
)
1515 struct so_list
*head
= NULL
;
1516 struct so_list
**link_ptr
= &head
;
1517 struct cleanup
*back_to
;
1519 struct svr4_library_list library_list
;
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.
1526 Unfortunately statically linked inferiors will also fall back through this
1527 suboptimal code path. */
1529 info
->using_xfer
= svr4_current_sos_via_xfer_libraries (&library_list
,
1531 if (info
->using_xfer
)
1533 if (library_list
.main_lm
)
1534 info
->main_lm_addr
= library_list
.main_lm
;
1536 return library_list
.head
? library_list
.head
: svr4_default_sos ();
1539 /* Always locate the debug struct, in case it has moved. */
1540 info
->debug_base
= 0;
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 ();
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
)
1555 back_to
= make_cleanup (svr4_free_library_list
, &head
);
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
);
1561 svr4_read_so_list (lm
, 0, &link_ptr
, ignore_first
);
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
);
1569 svr4_read_so_list (lm
, 0, &link_ptr
, 0);
1571 discard_cleanups (back_to
);
1574 return svr4_default_sos ();
1579 /* Implement the main part of the "current_sos" target_so_ops
1582 static struct so_list
*
1583 svr4_current_sos_1 (void)
1585 struct svr4_info
*info
= get_svr4_info ();
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
);
1592 /* Otherwise obtain the solib list directly from the inferior. */
1593 return svr4_current_sos_direct (info
);
1596 /* Implement the "current_sos" target_so_ops method. */
1598 static struct so_list
*
1599 svr4_current_sos (void)
1601 struct so_list
*so_head
= svr4_current_sos_1 ();
1602 struct mem_range vsyscall_range
;
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)
1610 struct so_list
**sop
;
1613 while (*sop
!= NULL
)
1615 struct so_list
*so
= *sop
;
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
1626 E.g., on Linux 3.16 (x86_64) the vDSO is a regular
1627 0-based ELF, and we see:
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, ...}
1634 And on Linux 2.6.32 (x86_64) we see:
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, ... }
1641 Dumping that vDSO shows:
1643 (gdb) info proc mappings
1644 0x7ffff7ffe000 0x7ffff7fff000 0x1000 0 [vdso]
1645 (gdb) dump memory vdso.bin 0x7ffff7ffe000 0x7ffff7fff000
1646 # readelf -Wa vdso.bin
1648 Entry point address: 0xffffffffff700700
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
1656 [ 9] .dynamic DYNAMIC ffffffffff700580 000580 0000f0
1658 if (address_in_mem_range (so
->lm_info
->l_ld
, &vsyscall_range
))
1672 /* Get the address of the link_map for a given OBJFILE. */
1675 svr4_fetch_objfile_link_map (struct objfile
*objfile
)
1678 struct svr4_info
*info
= get_svr4_info ();
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, ¤t_target
, auto_solib_add
);
1684 /* svr4_current_sos() will set main_lm_addr for the main executable. */
1685 if (objfile
== symfile_objfile
)
1686 return info
->main_lm_addr
;
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
;
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. */
1703 match_main (const char *soname
)
1705 const char * const *mainp
;
1707 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
1709 if (strcmp (soname
, *mainp
) == 0)
1716 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1717 SVR4 run time loader. */
1720 svr4_in_dynsym_resolve_code (CORE_ADDR pc
)
1722 struct svr4_info
*info
= get_svr4_info ();
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
));
1732 /* Given an executable's ABFD and target, compute the entry-point
1736 exec_entry_point (struct bfd
*abfd
, struct target_ops
*targ
)
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
),
1751 return gdbarch_addr_bits_remove (target_gdbarch (), addr
);
1754 /* A probe and its associated action. */
1756 struct probe_and_action
1759 struct probe
*probe
;
1761 /* The relocated address of the probe. */
1765 enum probe_action action
;
1768 /* Returns a hash code for the probe_and_action referenced by p. */
1771 hash_probe_and_action (const void *p
)
1773 const struct probe_and_action
*pa
= p
;
1775 return (hashval_t
) pa
->address
;
1778 /* Returns non-zero if the probe_and_actions referenced by p1 and p2
1782 equal_probe_and_action (const void *p1
, const void *p2
)
1784 const struct probe_and_action
*pa1
= p1
;
1785 const struct probe_and_action
*pa2
= p2
;
1787 return pa1
->address
== pa2
->address
;
1790 /* Register a solib event probe and its associated action in the
1794 register_solib_event_probe (struct probe
*probe
, CORE_ADDR address
,
1795 enum probe_action action
)
1797 struct svr4_info
*info
= get_svr4_info ();
1798 struct probe_and_action lookup
, *pa
;
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
);
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
);
1812 pa
= XCNEW (struct probe_and_action
);
1814 pa
->address
= address
;
1815 pa
->action
= action
;
1820 /* Get the solib event probe at the specified location, and the
1821 action associated with it. Returns NULL if no solib event probe
1824 static struct probe_and_action
*
1825 solib_event_probe_at (struct svr4_info
*info
, CORE_ADDR address
)
1827 struct probe_and_action lookup
;
1830 lookup
.address
= address
;
1831 slot
= htab_find_slot (info
->probes_table
, &lookup
, NO_INSERT
);
1836 return (struct probe_and_action
*) *slot
;
1839 /* Decide what action to take when the specified solib event probe is
1842 static enum probe_action
1843 solib_event_probe_action (struct probe_and_action
*pa
)
1845 enum probe_action action
;
1846 unsigned probe_argc
;
1847 struct frame_info
*frame
= get_current_frame ();
1849 action
= pa
->action
;
1850 if (action
== DO_NOTHING
|| action
== PROBES_INTERFACE_FAILED
)
1853 gdb_assert (action
== FULL_RELOAD
|| action
== UPDATE_OR_RELOAD
);
1855 /* Check that an appropriate number of arguments has been supplied.
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
;
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. */
1874 solist_update_full (struct svr4_info
*info
)
1876 free_solib_list (info
);
1877 info
->solib_list
= svr4_current_sos_direct (info
);
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
1888 solist_update_incremental (struct svr4_info
*info
, CORE_ADDR lm
)
1890 struct so_list
*tail
;
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
)
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 ())
1905 /* Walk to the end of the list. */
1906 for (tail
= info
->solib_list
; tail
->next
!= NULL
; tail
= tail
->next
)
1908 prev_lm
= tail
->lm_info
->lm_addr
;
1910 /* Read the new objects. */
1911 if (info
->using_xfer
)
1913 struct svr4_library_list library_list
;
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
))
1922 tail
->next
= library_list
.head
;
1926 struct so_list
**link
= &tail
->next
;
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
1932 if (!svr4_read_so_list (lm
, prev_lm
, &link
, 0))
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. */
1944 disable_probes_interface_cleanup (void *arg
)
1946 struct svr4_info
*info
= get_svr4_info ();
1948 warning (_("Probes-based dynamic linker interface failed.\n"
1949 "Reverting to original interface.\n"));
1951 free_probes_table (info
);
1952 free_solib_list (info
);
1955 /* Update the solib list as appropriate when using the
1956 probes-based linker interface. Do nothing if using the
1957 standard interface. */
1960 svr4_handle_solib_event (void)
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
;
1967 CORE_ADDR pc
, debug_base
, lm
= 0;
1969 struct frame_info
*frame
= get_current_frame ();
1971 /* Do nothing if not using the probes interface. */
1972 if (info
->probes_table
== NULL
)
1975 /* If anything goes wrong we revert to the original linker
1977 old_chain
= make_cleanup (disable_probes_interface_cleanup
, NULL
);
1979 pc
= regcache_read_pc (get_current_regcache ());
1980 pa
= solib_event_probe_at (info
, pc
);
1983 do_cleanups (old_chain
);
1987 action
= solib_event_probe_action (pa
);
1988 if (action
== PROBES_INTERFACE_FAILED
)
1990 do_cleanups (old_chain
);
1994 if (action
== DO_NOTHING
)
1996 discard_cleanups (old_chain
);
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
);
2014 val
= evaluate_probe_argument (pa
->probe
, 1, frame
);
2017 do_cleanups (old_chain
);
2021 debug_base
= value_as_address (val
);
2022 if (debug_base
== 0)
2024 do_cleanups (old_chain
);
2028 /* Always locate the debug struct, in case it moved. */
2029 info
->debug_base
= 0;
2030 if (locate_base (info
) == 0)
2032 do_cleanups (old_chain
);
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
;
2043 if (action
== UPDATE_OR_RELOAD
)
2045 val
= evaluate_probe_argument (pa
->probe
, 2, frame
);
2047 lm
= value_as_address (val
);
2050 action
= FULL_RELOAD
;
2053 /* Resume section map updates. */
2054 do_cleanups (usm_chain
);
2056 if (action
== UPDATE_OR_RELOAD
)
2058 if (!solist_update_incremental (info
, lm
))
2059 action
= FULL_RELOAD
;
2062 if (action
== FULL_RELOAD
)
2064 if (!solist_update_full (info
))
2066 do_cleanups (old_chain
);
2071 discard_cleanups (old_chain
);
2074 /* Helper function for svr4_update_solib_event_breakpoints. */
2077 svr4_update_solib_event_breakpoint (struct breakpoint
*b
, void *arg
)
2079 struct bp_location
*loc
;
2081 if (b
->type
!= bp_shlib_event
)
2083 /* Continue iterating. */
2087 for (loc
= b
->loc
; loc
!= NULL
; loc
= loc
->next
)
2089 struct svr4_info
*info
;
2090 struct probe_and_action
*pa
;
2092 info
= program_space_data (loc
->pspace
, solib_svr4_pspace_data
);
2093 if (info
== NULL
|| info
->probes_table
== NULL
)
2096 pa
= solib_event_probe_at (info
, loc
->address
);
2100 if (pa
->action
== DO_NOTHING
)
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
);
2111 /* Continue iterating. */
2115 /* Enable or disable optional solib event breakpoints as appropriate.
2116 Called whenever stop_on_solib_events is changed. */
2119 svr4_update_solib_event_breakpoints (void)
2121 iterate_over_breakpoints (svr4_update_solib_event_breakpoint
, NULL
);
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
2130 svr4_create_probe_breakpoints (struct gdbarch
*gdbarch
,
2131 VEC (probe_p
) **probes
,
2132 struct objfile
*objfile
)
2136 for (i
= 0; i
< NUM_PROBES
; i
++)
2138 enum probe_action action
= probe_info
[i
].action
;
2139 struct probe
*probe
;
2143 VEC_iterate (probe_p
, probes
[i
], ix
, probe
);
2146 CORE_ADDR address
= get_probe_address (probe
, objfile
);
2148 create_solib_event_breakpoint (gdbarch
, address
);
2149 register_solib_event_probe (probe
, address
, action
);
2153 svr4_update_solib_event_breakpoints ();
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.
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
2169 svr4_create_solib_event_breakpoints (struct gdbarch
*gdbarch
,
2172 struct obj_section
*os
;
2174 os
= find_pc_section (address
);
2179 for (with_prefix
= 0; with_prefix
<= 1; with_prefix
++)
2181 VEC (probe_p
) *probes
[NUM_PROBES
];
2182 int all_probes_found
= 1;
2183 int checked_can_use_probe_arguments
= 0;
2186 memset (probes
, 0, sizeof (probes
));
2187 for (i
= 0; i
< NUM_PROBES
; i
++)
2189 const char *name
= probe_info
[i
].name
;
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. */
2202 xsnprintf (buf
, sizeof (buf
), "rtld_%s", name
);
2206 probes
[i
] = find_probes_in_objfile (os
->objfile
, "rtld", name
);
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)
2214 if (VEC_empty (probe_p
, probes
[i
]))
2216 all_probes_found
= 0;
2220 /* Ensure probe arguments can be evaluated. */
2221 if (!checked_can_use_probe_arguments
)
2223 p
= VEC_index (probe_p
, probes
[i
], 0);
2224 if (!can_evaluate_probe_arguments (p
))
2226 all_probes_found
= 0;
2229 checked_can_use_probe_arguments
= 1;
2233 if (all_probes_found
)
2234 svr4_create_probe_breakpoints (gdbarch
, probes
, os
->objfile
);
2236 for (i
= 0; i
< NUM_PROBES
; i
++)
2237 VEC_free (probe_p
, probes
[i
]);
2239 if (all_probes_found
)
2244 create_solib_event_breakpoint (gdbarch
, address
);
2247 /* Helper function for gdb_bfd_lookup_symbol. */
2250 cmp_name_and_sec_flags (asymbol
*sym
, void *data
)
2252 return (strcmp (sym
->name
, (const char *) data
) == 0
2253 && (sym
->section
->flags
& (SEC_CODE
| SEC_DATA
)) != 0);
2255 /* Arrange for dynamic linker to hit breakpoint.
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.
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.
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().
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. */
2288 enable_break (struct svr4_info
*info
, int from_tty
)
2290 struct bound_minimal_symbol msymbol
;
2291 const char * const *bkpt_namep
;
2292 asection
*interp_sect
;
2296 info
->interp_text_sect_low
= info
->interp_text_sect_high
= 0;
2297 info
->interp_plt_sect_low
= info
->interp_plt_sect_high
= 0;
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. */
2304 solib_add (NULL
, from_tty
, ¤t_target
, auto_solib_add
);
2306 if (info
->debug_base
&& solib_svr4_r_map (info
) != 0)
2307 sym_addr
= solib_svr4_r_brk (info
);
2311 struct obj_section
*os
;
2313 sym_addr
= gdbarch_addr_bits_remove
2314 (target_gdbarch (), gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
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.
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. */
2337 os
= find_pc_section (sym_addr
);
2340 /* Record the relocated start and end address of the dynamic linker
2341 text and plt section for svr4_in_dynsym_resolve_code. */
2343 CORE_ADDR load_addr
;
2345 tmp_bfd
= os
->objfile
->obfd
;
2346 load_addr
= ANOFFSET (os
->objfile
->section_offsets
,
2347 SECT_OFF_TEXT (os
->objfile
));
2349 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
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
);
2358 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
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
);
2368 svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr
);
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 ();
2378 CORE_ADDR load_addr
= 0;
2379 int load_addr_found
= 0;
2380 int loader_found_in_list
= 0;
2382 bfd
*tmp_bfd
= NULL
;
2383 struct target_ops
*tmp_bfd_target
;
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.
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. */
2398 tmp_bfd
= solib_bfd_open (interp_name
);
2400 CATCH (ex
, RETURN_MASK_ALL
)
2405 if (tmp_bfd
== NULL
)
2406 goto bkpt_at_symbol
;
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
);
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 ();
2420 if (svr4_same_1 (interp_name
, so
->so_original_name
))
2422 load_addr_found
= 1;
2423 loader_found_in_list
= 1;
2424 load_addr
= lm_addr_check (so
, tmp_bfd
);
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 (¤t_target
, AT_BASE
, &load_addr
) > 0)
2435 int addr_bit
= gdbarch_addr_bit (target_gdbarch ());
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
2442 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
2444 CORE_ADDR space_size
= (CORE_ADDR
) 1 << addr_bit
;
2445 CORE_ADDR tmp_entry_point
= exec_entry_point (tmp_bfd
,
2448 gdb_assert (load_addr
< space_size
);
2450 /* TMP_ENTRY_POINT exceeding SPACE_SIZE would be for prelinked
2451 64bit ld.so with 32bit executable, it should not happen. */
2453 if (tmp_entry_point
< space_size
2454 && tmp_entry_point
+ load_addr
>= space_size
)
2455 load_addr
-= space_size
;
2458 load_addr_found
= 1;
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.
2465 This is more fragile than the previous approaches, but is a good
2466 fallback method because it has actually been working well in
2468 if (!load_addr_found
)
2470 struct regcache
*regcache
2471 = get_thread_arch_regcache (inferior_ptid
, target_gdbarch ());
2473 load_addr
= (regcache_read_pc (regcache
)
2474 - exec_entry_point (tmp_bfd
, tmp_bfd_target
));
2477 if (!loader_found_in_list
)
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
, ¤t_target
, auto_solib_add
);
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");
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
);
2496 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
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
);
2506 /* Now try to set a breakpoint in the dynamic linker. */
2507 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
2509 sym_addr
= gdb_bfd_lookup_symbol (tmp_bfd
, cmp_name_and_sec_flags
,
2510 (void *) *bkpt_namep
);
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 (),
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
);
2530 svr4_create_solib_event_breakpoints (target_gdbarch (),
2531 load_addr
+ sym_addr
);
2532 xfree (interp_name
);
2536 /* For whatever reason we couldn't set a breakpoint in the dynamic
2537 linker. Warn and drop into the old code. */
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."));
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. */
2548 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
2550 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
2551 if ((msymbol
.minsym
!= NULL
)
2552 && (BMSYMBOL_VALUE_ADDRESS (msymbol
) != 0))
2554 sym_addr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
2555 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2558 svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr
);
2563 if (interp_name
!= NULL
&& !current_inferior ()->attach_flag
)
2565 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
2567 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
2568 if ((msymbol
.minsym
!= NULL
)
2569 && (BMSYMBOL_VALUE_ADDRESS (msymbol
) != 0))
2571 sym_addr
= BMSYMBOL_VALUE_ADDRESS (msymbol
);
2572 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch (),
2575 svr4_create_solib_event_breakpoints (target_gdbarch (), sym_addr
);
2583 /* Implement the "special_symbol_handling" target_so_ops method. */
2586 svr4_special_symbol_handling (void)
2588 /* Nothing to do. */
2591 /* Read the ELF program headers from ABFD. Return the contents and
2592 set *PHDRS_SIZE to the size of the program headers. */
2595 read_program_headers_from_bfd (bfd
*abfd
, int *phdrs_size
)
2597 Elf_Internal_Ehdr
*ehdr
;
2600 ehdr
= elf_elfheader (abfd
);
2602 *phdrs_size
= ehdr
->e_phnum
* ehdr
->e_phentsize
;
2603 if (*phdrs_size
== 0)
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
)
2617 /* Return 1 and fill *DISPLACEMENTP with detected PIE offset of inferior
2618 exec_bfd. Otherwise return 0.
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:
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.
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.
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.
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.
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. ] */
2661 svr4_exec_displacement (CORE_ADDR
*displacementp
)
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
;
2667 if (exec_bfd
== NULL
)
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. */
2674 if ((bfd_get_file_flags (exec_bfd
) & DYNAMIC
) == 0)
2677 if (target_auxv_search (¤t_target
, AT_ENTRY
, &entry_point
) <= 0)
2680 exec_displacement
= entry_point
- bfd_get_start_address (exec_bfd
);
2682 /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
2683 alignment. It is cheaper than the program headers comparison below. */
2685 if (bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
2687 const struct elf_backend_data
*elf
= get_elf_backend_data (exec_bfd
);
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. */
2694 if ((exec_displacement
& (elf
->minpagesize
- 1)) != 0)
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". */
2704 if (bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
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
;
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
)
2716 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch ());
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.
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. */
2731 if (phdrs_size
!= phdrs2_size
2732 || bfd_get_arch_size (exec_bfd
) != arch_size
)
2734 else if (arch_size
== 32
2735 && phdrs_size
>= sizeof (Elf32_External_Phdr
)
2736 && phdrs_size
% sizeof (Elf32_External_Phdr
) == 0)
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;
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. */
2748 for (i
= 0; i
< ehdr2
->e_phnum
; i
++)
2749 if (phdr2
[i
].p_type
== PT_LOAD
)
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;
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
;
2761 vaddr
= extract_unsigned_integer (buf_vaddr_p
, 4,
2763 displacement_vaddr
= vaddr
- phdr2
[i
].p_vaddr
;
2765 paddr
= extract_unsigned_integer (buf_paddr_p
, 4,
2767 displacement_paddr
= paddr
- phdr2
[i
].p_paddr
;
2769 if (displacement_vaddr
== displacement_paddr
)
2770 displacement
= displacement_vaddr
;
2775 /* Now compare BUF and BUF2 with optional DISPLACEMENT. */
2777 for (i
= 0; i
< phdrs_size
/ sizeof (Elf32_External_Phdr
); i
++)
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
;
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
];
2790 /* PT_GNU_STACK is an exception by being never relocated by
2791 prelink as its addresses are always zero. */
2793 if (memcmp (phdrp
, phdr2p
, sizeof (*phdrp
)) == 0)
2796 /* Check also other adjustment combinations - PR 11786. */
2798 vaddr
= extract_unsigned_integer (buf_vaddr_p
, 4,
2800 vaddr
-= displacement
;
2801 store_unsigned_integer (buf_vaddr_p
, 4, byte_order
, vaddr
);
2803 paddr
= extract_unsigned_integer (buf_paddr_p
, 4,
2805 paddr
-= displacement
;
2806 store_unsigned_integer (buf_paddr_p
, 4, byte_order
, paddr
);
2808 if (memcmp (phdrp
, phdr2p
, sizeof (*phdrp
)) == 0)
2811 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2812 CentOS-5 has problems with filesz, memsz as well.
2814 if (phdr2
[i
].p_type
== PT_GNU_RELRO
)
2816 Elf32_External_Phdr tmp_phdr
= *phdrp
;
2817 Elf32_External_Phdr tmp_phdr2
= *phdr2p
;
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);
2828 if (memcmp (&tmp_phdr
, &tmp_phdr2
, sizeof (tmp_phdr
))
2833 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2834 plt2_asect
= bfd_get_section_by_name (exec_bfd
, ".plt");
2838 gdb_byte
*buf_filesz_p
= (gdb_byte
*) &phdrp
->p_filesz
;
2841 content2
= (bfd_get_section_flags (exec_bfd
, plt2_asect
)
2842 & SEC_HAS_CONTENTS
) != 0;
2844 filesz
= extract_unsigned_integer (buf_filesz_p
, 4,
2847 /* PLT2_ASECT is from on-disk file (exec_bfd) while
2848 FILESZ is from the in-memory image. */
2850 filesz
+= bfd_get_section_size (plt2_asect
);
2852 filesz
-= bfd_get_section_size (plt2_asect
);
2854 store_unsigned_integer (buf_filesz_p
, 4, byte_order
,
2857 if (memcmp (phdrp
, phdr2p
, sizeof (*phdrp
)) == 0)
2865 else if (arch_size
== 64
2866 && phdrs_size
>= sizeof (Elf64_External_Phdr
)
2867 && phdrs_size
% sizeof (Elf64_External_Phdr
) == 0)
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;
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. */
2879 for (i
= 0; i
< ehdr2
->e_phnum
; i
++)
2880 if (phdr2
[i
].p_type
== PT_LOAD
)
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;
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
;
2892 vaddr
= extract_unsigned_integer (buf_vaddr_p
, 8,
2894 displacement_vaddr
= vaddr
- phdr2
[i
].p_vaddr
;
2896 paddr
= extract_unsigned_integer (buf_paddr_p
, 8,
2898 displacement_paddr
= paddr
- phdr2
[i
].p_paddr
;
2900 if (displacement_vaddr
== displacement_paddr
)
2901 displacement
= displacement_vaddr
;
2906 /* Now compare BUF and BUF2 with optional DISPLACEMENT. */
2908 for (i
= 0; i
< phdrs_size
/ sizeof (Elf64_External_Phdr
); i
++)
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
;
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
];
2921 /* PT_GNU_STACK is an exception by being never relocated by
2922 prelink as its addresses are always zero. */
2924 if (memcmp (phdrp
, phdr2p
, sizeof (*phdrp
)) == 0)
2927 /* Check also other adjustment combinations - PR 11786. */
2929 vaddr
= extract_unsigned_integer (buf_vaddr_p
, 8,
2931 vaddr
-= displacement
;
2932 store_unsigned_integer (buf_vaddr_p
, 8, byte_order
, vaddr
);
2934 paddr
= extract_unsigned_integer (buf_paddr_p
, 8,
2936 paddr
-= displacement
;
2937 store_unsigned_integer (buf_paddr_p
, 8, byte_order
, paddr
);
2939 if (memcmp (phdrp
, phdr2p
, sizeof (*phdrp
)) == 0)
2942 /* Strip modifies the flags and alignment of PT_GNU_RELRO.
2943 CentOS-5 has problems with filesz, memsz as well.
2945 if (phdr2
[i
].p_type
== PT_GNU_RELRO
)
2947 Elf64_External_Phdr tmp_phdr
= *phdrp
;
2948 Elf64_External_Phdr tmp_phdr2
= *phdr2p
;
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);
2959 if (memcmp (&tmp_phdr
, &tmp_phdr2
, sizeof (tmp_phdr
))
2964 /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS. */
2965 plt2_asect
= bfd_get_section_by_name (exec_bfd
, ".plt");
2969 gdb_byte
*buf_filesz_p
= (gdb_byte
*) &phdrp
->p_filesz
;
2972 content2
= (bfd_get_section_flags (exec_bfd
, plt2_asect
)
2973 & SEC_HAS_CONTENTS
) != 0;
2975 filesz
= extract_unsigned_integer (buf_filesz_p
, 8,
2978 /* PLT2_ASECT is from on-disk file (exec_bfd) while
2979 FILESZ is from the in-memory image. */
2981 filesz
+= bfd_get_section_size (plt2_asect
);
2983 filesz
-= bfd_get_section_size (plt2_asect
);
2985 store_unsigned_integer (buf_filesz_p
, 8, byte_order
,
2988 if (memcmp (phdrp
, phdr2p
, sizeof (*phdrp
)) == 0)
3009 /* It can be printed repeatedly as there is no easy way to check
3010 the executable symbols/file has been already relocated to
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
));
3019 *displacementp
= exec_displacement
;
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. */
3029 svr4_relocate_main_executable (void)
3031 CORE_ADDR displacement
;
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.
3040 If we cannot compute the PIE displacement, either:
3042 - The executable is not PIE.
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.
3048 Then we leave the section offsets untouched and use them as is for
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.
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. */
3059 if (! svr4_exec_displacement (&displacement
))
3062 /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
3065 if (symfile_objfile
)
3067 struct section_offsets
*new_offsets
;
3070 new_offsets
= alloca (symfile_objfile
->num_sections
3071 * sizeof (*new_offsets
));
3073 for (i
= 0; i
< symfile_objfile
->num_sections
; i
++)
3074 new_offsets
->offsets
[i
] = displacement
;
3076 objfile_relocate (symfile_objfile
, new_offsets
);
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
)
3089 /* Implement the "create_inferior_hook" target_solib_ops method.
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.
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.
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. */
3109 svr4_solib_create_inferior_hook (int from_tty
)
3111 struct svr4_info
*info
;
3113 info
= get_svr4_info ();
3115 /* Clear the probes-based interface's state. */
3116 free_probes_table (info
);
3117 free_solib_list (info
);
3119 /* Relocate the main executable if necessary. */
3120 svr4_relocate_main_executable ();
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
)
3127 if (!svr4_have_link_map_offsets ())
3130 if (!enable_break (info
, from_tty
))
3135 svr4_clear_solib (void)
3137 struct svr4_info
*info
;
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
;
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.
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. */
3161 svr4_truncate_ptr (CORE_ADDR addr
)
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. */
3168 return addr
& (((CORE_ADDR
) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
3173 svr4_relocate_section_addresses (struct so_list
*so
,
3174 struct target_section
*sec
)
3176 bfd
*abfd
= sec
->the_bfd_section
->owner
;
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
));
3183 /* Architecture-specific operations. */
3185 /* Per-architecture data key. */
3186 static struct gdbarch_data
*solib_svr4_data
;
3188 struct solib_svr4_ops
3190 /* Return a description of the layout of `struct link_map'. */
3191 struct link_map_offsets
*(*fetch_link_map_offsets
)(void);
3194 /* Return a default for the architecture-specific operations. */
3197 solib_svr4_init (struct obstack
*obstack
)
3199 struct solib_svr4_ops
*ops
;
3201 ops
= OBSTACK_ZALLOC (obstack
, struct solib_svr4_ops
);
3202 ops
->fetch_link_map_offsets
= NULL
;
3206 /* Set the architecture-specific `struct link_map_offsets' fetcher for
3207 GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
3210 set_solib_svr4_fetch_link_map_offsets (struct gdbarch
*gdbarch
,
3211 struct link_map_offsets
*(*flmo
) (void))
3213 struct solib_svr4_ops
*ops
= gdbarch_data (gdbarch
, solib_svr4_data
);
3215 ops
->fetch_link_map_offsets
= flmo
;
3217 set_solib_ops (gdbarch
, &svr4_so_ops
);
3220 /* Fetch a link_map_offsets structure using the architecture-specific
3221 `struct link_map_offsets' fetcher. */
3223 static struct link_map_offsets
*
3224 svr4_fetch_link_map_offsets (void)
3226 struct solib_svr4_ops
*ops
= gdbarch_data (target_gdbarch (), solib_svr4_data
);
3228 gdb_assert (ops
->fetch_link_map_offsets
);
3229 return ops
->fetch_link_map_offsets ();
3232 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
3235 svr4_have_link_map_offsets (void)
3237 struct solib_svr4_ops
*ops
= gdbarch_data (target_gdbarch (), solib_svr4_data
);
3239 return (ops
->fetch_link_map_offsets
!= NULL
);
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. */
3247 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3248 for an ILP32 SVR4 system. */
3250 struct link_map_offsets
*
3251 svr4_ilp32_fetch_link_map_offsets (void)
3253 static struct link_map_offsets lmo
;
3254 static struct link_map_offsets
*lmp
= NULL
;
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;
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;
3278 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
3279 for an LP64 SVR4 system. */
3281 struct link_map_offsets
*
3282 svr4_lp64_fetch_link_map_offsets (void)
3284 static struct link_map_offsets lmo
;
3285 static struct link_map_offsets
*lmp
= NULL
;
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;
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;
3310 struct target_so_ops svr4_so_ops
;
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. */
3316 static struct symbol
*
3317 elf_lookup_lib_symbol (struct objfile
*objfile
,
3319 const domain_enum domain
)
3323 if (objfile
== symfile_objfile
)
3327 /* OBJFILE should have been passed as the non-debug one. */
3328 gdb_assert (objfile
->separate_debug_objfile_backlink
== NULL
);
3330 abfd
= objfile
->obfd
;
3333 if (abfd
== NULL
|| scan_dyntag (DT_SYMBOLIC
, abfd
, NULL
) != 1)
3336 return lookup_global_symbol_from_objfile (objfile
, name
, domain
);
3339 extern initialize_file_ftype _initialize_svr4_solib
; /* -Wmissing-prototypes */
3342 _initialize_svr4_solib (void)
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
);
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
;