1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "elf/external.h"
25 #include "elf/common.h"
36 #include "gdbthread.h"
39 #include "gdb_assert.h"
43 #include "solib-svr4.h"
45 #include "bfd-target.h"
49 #include "exceptions.h"
51 static struct link_map_offsets
*svr4_fetch_link_map_offsets (void);
52 static int svr4_have_link_map_offsets (void);
53 static void svr4_relocate_main_executable (void);
55 /* Link map info to include in an allocated so_list entry */
59 /* Pointer to copy of link map from inferior. The type is char *
60 rather than void *, so that we may use byte offsets to find the
61 various fields without the need for a cast. */
64 /* Amount by which addresses in the binary should be relocated to
65 match the inferior. This could most often be taken directly
66 from lm, but when prelinking is involved and the prelink base
67 address changes, we may need a different offset, we want to
68 warn about the difference and compute it only once. */
71 /* The target location of lm. */
75 /* On SVR4 systems, a list of symbols in the dynamic linker where
76 GDB can try to place a breakpoint to monitor shared library
79 If none of these symbols are found, or other errors occur, then
80 SVR4 systems will fall back to using a symbol as the "startup
81 mapping complete" breakpoint address. */
83 static char *solib_break_names
[] =
89 "__dl_rtld_db_dlactivity",
95 static char *bkpt_names
[] =
103 static char *main_name_list
[] =
109 /* Return non-zero if GDB_SO_NAME and INFERIOR_SO_NAME represent
110 the same shared library. */
113 svr4_same_1 (const char *gdb_so_name
, const char *inferior_so_name
)
115 if (strcmp (gdb_so_name
, inferior_so_name
) == 0)
118 /* On Solaris, when starting inferior we think that dynamic linker is
119 /usr/lib/ld.so.1, but later on, the table of loaded shared libraries
120 contains /lib/ld.so.1. Sometimes one file is a link to another, but
121 sometimes they have identical content, but are not linked to each
122 other. We don't restrict this check for Solaris, but the chances
123 of running into this situation elsewhere are very low. */
124 if (strcmp (gdb_so_name
, "/usr/lib/ld.so.1") == 0
125 && strcmp (inferior_so_name
, "/lib/ld.so.1") == 0)
128 /* Similarly, we observed the same issue with sparc64, but with
129 different locations. */
130 if (strcmp (gdb_so_name
, "/usr/lib/sparcv9/ld.so.1") == 0
131 && strcmp (inferior_so_name
, "/lib/sparcv9/ld.so.1") == 0)
138 svr4_same (struct so_list
*gdb
, struct so_list
*inferior
)
140 return (svr4_same_1 (gdb
->so_original_name
, inferior
->so_original_name
));
143 /* link map access functions */
146 LM_ADDR_FROM_LINK_MAP (struct so_list
*so
)
148 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
149 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
151 return extract_typed_address (so
->lm_info
->lm
+ lmo
->l_addr_offset
,
156 HAS_LM_DYNAMIC_FROM_LINK_MAP (void)
158 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
160 return lmo
->l_ld_offset
>= 0;
164 LM_DYNAMIC_FROM_LINK_MAP (struct so_list
*so
)
166 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
167 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
169 return extract_typed_address (so
->lm_info
->lm
+ lmo
->l_ld_offset
,
174 LM_ADDR_CHECK (struct so_list
*so
, bfd
*abfd
)
176 if (so
->lm_info
->l_addr
== (CORE_ADDR
)-1)
178 struct bfd_section
*dyninfo_sect
;
179 CORE_ADDR l_addr
, l_dynaddr
, dynaddr
;
181 l_addr
= LM_ADDR_FROM_LINK_MAP (so
);
183 if (! abfd
|| ! HAS_LM_DYNAMIC_FROM_LINK_MAP ())
186 l_dynaddr
= LM_DYNAMIC_FROM_LINK_MAP (so
);
188 dyninfo_sect
= bfd_get_section_by_name (abfd
, ".dynamic");
189 if (dyninfo_sect
== NULL
)
192 dynaddr
= bfd_section_vma (abfd
, dyninfo_sect
);
194 if (dynaddr
+ l_addr
!= l_dynaddr
)
196 CORE_ADDR align
= 0x1000;
198 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
200 Elf_Internal_Ehdr
*ehdr
= elf_tdata (abfd
)->elf_header
;
201 Elf_Internal_Phdr
*phdr
= elf_tdata (abfd
)->phdr
;
206 for (i
= 0; i
< ehdr
->e_phnum
; i
++)
207 if (phdr
[i
].p_type
== PT_LOAD
&& phdr
[i
].p_align
> align
)
208 align
= phdr
[i
].p_align
;
211 /* Turn it into a mask. */
214 /* If the changes match the alignment requirements, we
215 assume we're using a core file that was generated by the
216 same binary, just prelinked with a different base offset.
217 If it doesn't match, we may have a different binary, the
218 same binary with the dynamic table loaded at an unrelated
219 location, or anything, really. To avoid regressions,
220 don't adjust the base offset in the latter case, although
221 odds are that, if things really changed, debugging won't
224 One could expect more the condition
225 ((l_addr & align) == 0 && ((l_dynaddr - dynaddr) & align) == 0)
226 but the one below is relaxed for PPC. The PPC kernel supports
227 either 4k or 64k page sizes. To be prepared for 64k pages,
228 PPC ELF files are built using an alignment requirement of 64k.
229 However, when running on a kernel supporting 4k pages, the memory
230 mapping of the library may not actually happen on a 64k boundary!
232 (In the usual case where (l_addr & align) == 0, this check is
233 equivalent to the possibly expected check above.) */
235 if ((l_addr
& align
) == ((l_dynaddr
- dynaddr
) & align
))
237 l_addr
= l_dynaddr
- dynaddr
;
241 warning (_(".dynamic section for \"%s\" "
242 "is not at the expected address"), so
->so_name
);
243 warning (_("difference appears to be caused by prelink, "
244 "adjusting expectations"));
248 warning (_(".dynamic section for \"%s\" "
249 "is not at the expected address "
250 "(wrong library or version mismatch?)"), so
->so_name
);
254 so
->lm_info
->l_addr
= l_addr
;
257 return so
->lm_info
->l_addr
;
261 LM_NEXT (struct so_list
*so
)
263 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
264 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
266 return extract_typed_address (so
->lm_info
->lm
+ lmo
->l_next_offset
,
271 LM_NAME (struct so_list
*so
)
273 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
274 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
276 return extract_typed_address (so
->lm_info
->lm
+ lmo
->l_name_offset
,
281 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list
*so
)
283 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
284 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
286 /* Assume that everything is a library if the dynamic loader was loaded
287 late by a static executable. */
288 if (exec_bfd
&& bfd_get_section_by_name (exec_bfd
, ".dynamic") == NULL
)
291 return extract_typed_address (so
->lm_info
->lm
+ lmo
->l_prev_offset
,
295 /* Per pspace SVR4 specific data. */
299 CORE_ADDR debug_base
; /* Base of dynamic linker structures */
301 /* Validity flag for debug_loader_offset. */
302 int debug_loader_offset_p
;
304 /* Load address for the dynamic linker, inferred. */
305 CORE_ADDR debug_loader_offset
;
307 /* Name of the dynamic linker, valid if debug_loader_offset_p. */
308 char *debug_loader_name
;
310 /* Load map address for the main executable. */
311 CORE_ADDR main_lm_addr
;
313 CORE_ADDR interp_text_sect_low
;
314 CORE_ADDR interp_text_sect_high
;
315 CORE_ADDR interp_plt_sect_low
;
316 CORE_ADDR interp_plt_sect_high
;
319 /* Per-program-space data key. */
320 static const struct program_space_data
*solib_svr4_pspace_data
;
323 svr4_pspace_data_cleanup (struct program_space
*pspace
, void *arg
)
325 struct svr4_info
*info
;
327 info
= program_space_data (pspace
, solib_svr4_pspace_data
);
331 /* Get the current svr4 data. If none is found yet, add it now. This
332 function always returns a valid object. */
334 static struct svr4_info
*
337 struct svr4_info
*info
;
339 info
= program_space_data (current_program_space
, solib_svr4_pspace_data
);
343 info
= XZALLOC (struct svr4_info
);
344 set_program_space_data (current_program_space
, solib_svr4_pspace_data
, info
);
348 /* Local function prototypes */
350 static int match_main (char *);
352 static CORE_ADDR
bfd_lookup_symbol (bfd
*, char *);
358 bfd_lookup_symbol -- lookup the value for a specific symbol
362 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
366 An expensive way to lookup the value of a single symbol for
367 bfd's that are only temporary anyway. This is used by the
368 shared library support to find the address of the debugger
369 notification routine in the shared library.
371 The returned symbol may be in a code or data section; functions
372 will normally be in a code section, but may be in a data section
373 if this architecture uses function descriptors.
375 Note that 0 is specifically allowed as an error return (no
380 bfd_lookup_symbol (bfd
*abfd
, char *symname
)
384 asymbol
**symbol_table
;
385 unsigned int number_of_symbols
;
387 struct cleanup
*back_to
;
388 CORE_ADDR symaddr
= 0;
390 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
392 if (storage_needed
> 0)
394 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
395 back_to
= make_cleanup (xfree
, symbol_table
);
396 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
398 for (i
= 0; i
< number_of_symbols
; i
++)
400 sym
= *symbol_table
++;
401 if (strcmp (sym
->name
, symname
) == 0
402 && (sym
->section
->flags
& (SEC_CODE
| SEC_DATA
)) != 0)
404 /* BFD symbols are section relative. */
405 symaddr
= sym
->value
+ sym
->section
->vma
;
409 do_cleanups (back_to
);
415 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
416 have to check the dynamic string table too. */
418 storage_needed
= bfd_get_dynamic_symtab_upper_bound (abfd
);
420 if (storage_needed
> 0)
422 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
423 back_to
= make_cleanup (xfree
, symbol_table
);
424 number_of_symbols
= bfd_canonicalize_dynamic_symtab (abfd
, symbol_table
);
426 for (i
= 0; i
< number_of_symbols
; i
++)
428 sym
= *symbol_table
++;
430 if (strcmp (sym
->name
, symname
) == 0
431 && (sym
->section
->flags
& (SEC_CODE
| SEC_DATA
)) != 0)
433 /* BFD symbols are section relative. */
434 symaddr
= sym
->value
+ sym
->section
->vma
;
438 do_cleanups (back_to
);
445 /* Read program header TYPE from inferior memory. The header is found
446 by scanning the OS auxillary vector.
448 Return a pointer to allocated memory holding the program header contents,
449 or NULL on failure. If sucessful, and unless P_SECT_SIZE is NULL, the
450 size of those contents is returned to P_SECT_SIZE. Likewise, the target
451 architecture size (32-bit or 64-bit) is returned to P_ARCH_SIZE. */
454 read_program_header (int type
, int *p_sect_size
, int *p_arch_size
)
456 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
457 CORE_ADDR at_phdr
, at_phent
, at_phnum
;
458 int arch_size
, sect_size
;
462 /* Get required auxv elements from target. */
463 if (target_auxv_search (¤t_target
, AT_PHDR
, &at_phdr
) <= 0)
465 if (target_auxv_search (¤t_target
, AT_PHENT
, &at_phent
) <= 0)
467 if (target_auxv_search (¤t_target
, AT_PHNUM
, &at_phnum
) <= 0)
469 if (!at_phdr
|| !at_phnum
)
472 /* Determine ELF architecture type. */
473 if (at_phent
== sizeof (Elf32_External_Phdr
))
475 else if (at_phent
== sizeof (Elf64_External_Phdr
))
480 /* Find .dynamic section via the PT_DYNAMIC PHDR. */
483 Elf32_External_Phdr phdr
;
486 /* Search for requested PHDR. */
487 for (i
= 0; i
< at_phnum
; i
++)
489 if (target_read_memory (at_phdr
+ i
* sizeof (phdr
),
490 (gdb_byte
*)&phdr
, sizeof (phdr
)))
493 if (extract_unsigned_integer ((gdb_byte
*)phdr
.p_type
,
494 4, byte_order
) == type
)
501 /* Retrieve address and size. */
502 sect_addr
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_vaddr
,
504 sect_size
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_memsz
,
509 Elf64_External_Phdr phdr
;
512 /* Search for requested PHDR. */
513 for (i
= 0; i
< at_phnum
; i
++)
515 if (target_read_memory (at_phdr
+ i
* sizeof (phdr
),
516 (gdb_byte
*)&phdr
, sizeof (phdr
)))
519 if (extract_unsigned_integer ((gdb_byte
*)phdr
.p_type
,
520 4, byte_order
) == type
)
527 /* Retrieve address and size. */
528 sect_addr
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_vaddr
,
530 sect_size
= extract_unsigned_integer ((gdb_byte
*)phdr
.p_memsz
,
534 /* Read in requested program header. */
535 buf
= xmalloc (sect_size
);
536 if (target_read_memory (sect_addr
, buf
, sect_size
))
543 *p_arch_size
= arch_size
;
545 *p_sect_size
= sect_size
;
551 /* Return program interpreter string. */
553 find_program_interpreter (void)
555 gdb_byte
*buf
= NULL
;
557 /* If we have an exec_bfd, use its section table. */
559 && bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
561 struct bfd_section
*interp_sect
;
563 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
564 if (interp_sect
!= NULL
)
566 CORE_ADDR sect_addr
= bfd_section_vma (exec_bfd
, interp_sect
);
567 int sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
569 buf
= xmalloc (sect_size
);
570 bfd_get_section_contents (exec_bfd
, interp_sect
, buf
, 0, sect_size
);
574 /* If we didn't find it, use the target auxillary vector. */
576 buf
= read_program_header (PT_INTERP
, NULL
, NULL
);
582 /* Scan for DYNTAG in .dynamic section of ABFD. If DYNTAG is found 1 is
583 returned and the corresponding PTR is set. */
586 scan_dyntag (int dyntag
, bfd
*abfd
, CORE_ADDR
*ptr
)
588 int arch_size
, step
, sect_size
;
590 CORE_ADDR dyn_ptr
, dyn_addr
;
591 gdb_byte
*bufend
, *bufstart
, *buf
;
592 Elf32_External_Dyn
*x_dynp_32
;
593 Elf64_External_Dyn
*x_dynp_64
;
594 struct bfd_section
*sect
;
595 struct target_section
*target_section
;
600 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
603 arch_size
= bfd_get_arch_size (abfd
);
607 /* Find the start address of the .dynamic section. */
608 sect
= bfd_get_section_by_name (abfd
, ".dynamic");
612 for (target_section
= current_target_sections
->sections
;
613 target_section
< current_target_sections
->sections_end
;
615 if (sect
== target_section
->the_bfd_section
)
617 if (target_section
< current_target_sections
->sections_end
)
618 dyn_addr
= target_section
->addr
;
621 /* ABFD may come from OBJFILE acting only as a symbol file without being
622 loaded into the target (see add_symbol_file_command). This case is
623 such fallback to the file VMA address without the possibility of
624 having the section relocated to its actual in-memory address. */
626 dyn_addr
= bfd_section_vma (abfd
, sect
);
629 /* Read in .dynamic from the BFD. We will get the actual value
630 from memory later. */
631 sect_size
= bfd_section_size (abfd
, sect
);
632 buf
= bufstart
= alloca (sect_size
);
633 if (!bfd_get_section_contents (abfd
, sect
,
637 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
638 step
= (arch_size
== 32) ? sizeof (Elf32_External_Dyn
)
639 : sizeof (Elf64_External_Dyn
);
640 for (bufend
= buf
+ sect_size
;
646 x_dynp_32
= (Elf32_External_Dyn
*) buf
;
647 dyn_tag
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_dynp_32
->d_tag
);
648 dyn_ptr
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_dynp_32
->d_un
.d_ptr
);
652 x_dynp_64
= (Elf64_External_Dyn
*) buf
;
653 dyn_tag
= bfd_h_get_64 (abfd
, (bfd_byte
*) x_dynp_64
->d_tag
);
654 dyn_ptr
= bfd_h_get_64 (abfd
, (bfd_byte
*) x_dynp_64
->d_un
.d_ptr
);
656 if (dyn_tag
== DT_NULL
)
658 if (dyn_tag
== dyntag
)
660 /* If requested, try to read the runtime value of this .dynamic
664 struct type
*ptr_type
;
668 ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
669 ptr_addr
= dyn_addr
+ (buf
- bufstart
) + arch_size
/ 8;
670 if (target_read_memory (ptr_addr
, ptr_buf
, arch_size
/ 8) == 0)
671 dyn_ptr
= extract_typed_address (ptr_buf
, ptr_type
);
681 /* Scan for DYNTAG in .dynamic section of the target's main executable,
682 found by consulting the OS auxillary vector. If DYNTAG is found 1 is
683 returned and the corresponding PTR is set. */
686 scan_dyntag_auxv (int dyntag
, CORE_ADDR
*ptr
)
688 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
689 int sect_size
, arch_size
, step
;
692 gdb_byte
*bufend
, *bufstart
, *buf
;
694 /* Read in .dynamic section. */
695 buf
= bufstart
= read_program_header (PT_DYNAMIC
, §_size
, &arch_size
);
699 /* Iterate over BUF and scan for DYNTAG. If found, set PTR and return. */
700 step
= (arch_size
== 32) ? sizeof (Elf32_External_Dyn
)
701 : sizeof (Elf64_External_Dyn
);
702 for (bufend
= buf
+ sect_size
;
708 Elf32_External_Dyn
*dynp
= (Elf32_External_Dyn
*) buf
;
709 dyn_tag
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_tag
,
711 dyn_ptr
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_un
.d_ptr
,
716 Elf64_External_Dyn
*dynp
= (Elf64_External_Dyn
*) buf
;
717 dyn_tag
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_tag
,
719 dyn_ptr
= extract_unsigned_integer ((gdb_byte
*) dynp
->d_un
.d_ptr
,
722 if (dyn_tag
== DT_NULL
)
725 if (dyn_tag
== dyntag
)
744 elf_locate_base -- locate the base address of dynamic linker structs
745 for SVR4 elf targets.
749 CORE_ADDR elf_locate_base (void)
753 For SVR4 elf targets the address of the dynamic linker's runtime
754 structure is contained within the dynamic info section in the
755 executable file. The dynamic section is also mapped into the
756 inferior address space. Because the runtime loader fills in the
757 real address before starting the inferior, we have to read in the
758 dynamic info section from the inferior address space.
759 If there are any errors while trying to find the address, we
760 silently return 0, otherwise the found address is returned.
765 elf_locate_base (void)
767 struct minimal_symbol
*msymbol
;
770 /* Look for DT_MIPS_RLD_MAP first. MIPS executables use this
771 instead of DT_DEBUG, although they sometimes contain an unused
773 if (scan_dyntag (DT_MIPS_RLD_MAP
, exec_bfd
, &dyn_ptr
)
774 || scan_dyntag_auxv (DT_MIPS_RLD_MAP
, &dyn_ptr
))
776 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
778 int pbuf_size
= TYPE_LENGTH (ptr_type
);
779 pbuf
= alloca (pbuf_size
);
780 /* DT_MIPS_RLD_MAP contains a pointer to the address
781 of the dynamic link structure. */
782 if (target_read_memory (dyn_ptr
, pbuf
, pbuf_size
))
784 return extract_typed_address (pbuf
, ptr_type
);
788 if (scan_dyntag (DT_DEBUG
, exec_bfd
, &dyn_ptr
)
789 || scan_dyntag_auxv (DT_DEBUG
, &dyn_ptr
))
792 /* This may be a static executable. Look for the symbol
793 conventionally named _r_debug, as a last resort. */
794 msymbol
= lookup_minimal_symbol ("_r_debug", NULL
, symfile_objfile
);
796 return SYMBOL_VALUE_ADDRESS (msymbol
);
798 /* DT_DEBUG entry not found. */
806 locate_base -- locate the base address of dynamic linker structs
810 CORE_ADDR locate_base (struct svr4_info *)
814 For both the SunOS and SVR4 shared library implementations, if the
815 inferior executable has been linked dynamically, there is a single
816 address somewhere in the inferior's data space which is the key to
817 locating all of the dynamic linker's runtime structures. This
818 address is the value of the debug base symbol. The job of this
819 function is to find and return that address, or to return 0 if there
820 is no such address (the executable is statically linked for example).
822 For SunOS, the job is almost trivial, since the dynamic linker and
823 all of it's structures are statically linked to the executable at
824 link time. Thus the symbol for the address we are looking for has
825 already been added to the minimal symbol table for the executable's
826 objfile at the time the symbol file's symbols were read, and all we
827 have to do is look it up there. Note that we explicitly do NOT want
828 to find the copies in the shared library.
830 The SVR4 version is a bit more complicated because the address
831 is contained somewhere in the dynamic info section. We have to go
832 to a lot more work to discover the address of the debug base symbol.
833 Because of this complexity, we cache the value we find and return that
834 value on subsequent invocations. Note there is no copy in the
835 executable symbol tables.
840 locate_base (struct svr4_info
*info
)
842 /* Check to see if we have a currently valid address, and if so, avoid
843 doing all this work again and just return the cached address. If
844 we have no cached address, try to locate it in the dynamic info
845 section for ELF executables. There's no point in doing any of this
846 though if we don't have some link map offsets to work with. */
848 if (info
->debug_base
== 0 && svr4_have_link_map_offsets ())
849 info
->debug_base
= elf_locate_base ();
850 return info
->debug_base
;
853 /* Find the first element in the inferior's dynamic link map, and
854 return its address in the inferior.
856 FIXME: Perhaps we should validate the info somehow, perhaps by
857 checking r_version for a known version number, or r_state for
861 solib_svr4_r_map (struct svr4_info
*info
)
863 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
864 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
866 return read_memory_typed_address (info
->debug_base
+ lmo
->r_map_offset
,
870 /* Find r_brk from the inferior's debug base. */
873 solib_svr4_r_brk (struct svr4_info
*info
)
875 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
876 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
878 return read_memory_typed_address (info
->debug_base
+ lmo
->r_brk_offset
,
882 /* Find the link map for the dynamic linker (if it is not in the
883 normal list of loaded shared objects). */
886 solib_svr4_r_ldsomap (struct svr4_info
*info
)
888 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
889 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
890 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
893 /* Check version, and return zero if `struct r_debug' doesn't have
894 the r_ldsomap member. */
896 = read_memory_unsigned_integer (info
->debug_base
+ lmo
->r_version_offset
,
897 lmo
->r_version_size
, byte_order
);
898 if (version
< 2 || lmo
->r_ldsomap_offset
== -1)
901 return read_memory_typed_address (info
->debug_base
+ lmo
->r_ldsomap_offset
,
905 /* On Solaris systems with some versions of the dynamic linker,
906 ld.so's l_name pointer points to the SONAME in the string table
907 rather than into writable memory. So that GDB can find shared
908 libraries when loading a core file generated by gcore, ensure that
909 memory areas containing the l_name string are saved in the core
913 svr4_keep_data_in_core (CORE_ADDR vaddr
, unsigned long size
)
915 struct svr4_info
*info
;
918 struct cleanup
*old_chain
;
919 struct link_map_offsets
*lmo
;
922 info
= get_svr4_info ();
924 info
->debug_base
= 0;
926 if (!info
->debug_base
)
929 ldsomap
= solib_svr4_r_ldsomap (info
);
933 lmo
= svr4_fetch_link_map_offsets ();
934 new = XZALLOC (struct so_list
);
935 old_chain
= make_cleanup (xfree
, new);
936 new->lm_info
= xmalloc (sizeof (struct lm_info
));
937 make_cleanup (xfree
, new->lm_info
);
938 new->lm_info
->l_addr
= (CORE_ADDR
)-1;
939 new->lm_info
->lm_addr
= ldsomap
;
940 new->lm_info
->lm
= xzalloc (lmo
->link_map_size
);
941 make_cleanup (xfree
, new->lm_info
->lm
);
942 read_memory (ldsomap
, new->lm_info
->lm
, lmo
->link_map_size
);
943 lm_name
= LM_NAME (new);
944 do_cleanups (old_chain
);
946 return (lm_name
>= vaddr
&& lm_name
< vaddr
+ size
);
953 open_symbol_file_object
957 void open_symbol_file_object (void *from_tty)
961 If no open symbol file, attempt to locate and open the main symbol
962 file. On SVR4 systems, this is the first link map entry. If its
963 name is here, we can open it. Useful when attaching to a process
964 without first loading its symbol file.
966 If FROM_TTYP dereferences to a non-zero integer, allow messages to
967 be printed. This parameter is a pointer rather than an int because
968 open_symbol_file_object() is called via catch_errors() and
969 catch_errors() requires a pointer argument. */
972 open_symbol_file_object (void *from_ttyp
)
974 CORE_ADDR lm
, l_name
;
977 int from_tty
= *(int *)from_ttyp
;
978 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
979 struct type
*ptr_type
= builtin_type (target_gdbarch
)->builtin_data_ptr
;
980 int l_name_size
= TYPE_LENGTH (ptr_type
);
981 gdb_byte
*l_name_buf
= xmalloc (l_name_size
);
982 struct cleanup
*cleanups
= make_cleanup (xfree
, l_name_buf
);
983 struct svr4_info
*info
= get_svr4_info ();
986 if (!query (_("Attempt to reload symbols from process? ")))
989 /* Always locate the debug struct, in case it has moved. */
990 info
->debug_base
= 0;
991 if (locate_base (info
) == 0)
992 return 0; /* failed somehow... */
994 /* First link map member should be the executable. */
995 lm
= solib_svr4_r_map (info
);
997 return 0; /* failed somehow... */
999 /* Read address of name from target memory to GDB. */
1000 read_memory (lm
+ lmo
->l_name_offset
, l_name_buf
, l_name_size
);
1002 /* Convert the address to host format. */
1003 l_name
= extract_typed_address (l_name_buf
, ptr_type
);
1005 /* Free l_name_buf. */
1006 do_cleanups (cleanups
);
1009 return 0; /* No filename. */
1011 /* Now fetch the filename from target memory. */
1012 target_read_string (l_name
, &filename
, SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
1013 make_cleanup (xfree
, filename
);
1017 warning (_("failed to read exec filename from attached file: %s"),
1018 safe_strerror (errcode
));
1022 /* Have a pathname: read the symbol file. */
1023 symbol_file_add_main (filename
, from_tty
);
1028 /* If no shared library information is available from the dynamic
1029 linker, build a fallback list from other sources. */
1031 static struct so_list
*
1032 svr4_default_sos (void)
1034 struct svr4_info
*info
= get_svr4_info ();
1036 struct so_list
*head
= NULL
;
1037 struct so_list
**link_ptr
= &head
;
1039 if (info
->debug_loader_offset_p
)
1041 struct so_list
*new = XZALLOC (struct so_list
);
1043 new->lm_info
= xmalloc (sizeof (struct lm_info
));
1045 /* Nothing will ever check the cached copy of the link
1046 map if we set l_addr. */
1047 new->lm_info
->l_addr
= info
->debug_loader_offset
;
1048 new->lm_info
->lm_addr
= 0;
1049 new->lm_info
->lm
= NULL
;
1051 strncpy (new->so_name
, info
->debug_loader_name
,
1052 SO_NAME_MAX_PATH_SIZE
- 1);
1053 new->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
1054 strcpy (new->so_original_name
, new->so_name
);
1057 link_ptr
= &new->next
;
1065 current_sos -- build a list of currently loaded shared objects
1069 struct so_list *current_sos ()
1073 Build a list of `struct so_list' objects describing the shared
1074 objects currently loaded in the inferior. This list does not
1075 include an entry for the main executable file.
1077 Note that we only gather information directly available from the
1078 inferior --- we don't examine any of the shared library files
1079 themselves. The declaration of `struct so_list' says which fields
1080 we provide values for. */
1082 static struct so_list
*
1083 svr4_current_sos (void)
1086 struct so_list
*head
= 0;
1087 struct so_list
**link_ptr
= &head
;
1088 CORE_ADDR ldsomap
= 0;
1089 struct svr4_info
*info
;
1091 info
= get_svr4_info ();
1093 /* Always locate the debug struct, in case it has moved. */
1094 info
->debug_base
= 0;
1097 /* If we can't find the dynamic linker's base structure, this
1098 must not be a dynamically linked executable. Hmm. */
1099 if (! info
->debug_base
)
1100 return svr4_default_sos ();
1102 /* Walk the inferior's link map list, and build our list of
1103 `struct so_list' nodes. */
1104 lm
= solib_svr4_r_map (info
);
1108 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
1109 struct so_list
*new = XZALLOC (struct so_list
);
1110 struct cleanup
*old_chain
= make_cleanup (xfree
, new);
1112 new->lm_info
= xmalloc (sizeof (struct lm_info
));
1113 make_cleanup (xfree
, new->lm_info
);
1115 new->lm_info
->l_addr
= (CORE_ADDR
)-1;
1116 new->lm_info
->lm_addr
= lm
;
1117 new->lm_info
->lm
= xzalloc (lmo
->link_map_size
);
1118 make_cleanup (xfree
, new->lm_info
->lm
);
1120 read_memory (lm
, new->lm_info
->lm
, lmo
->link_map_size
);
1124 /* For SVR4 versions, the first entry in the link map is for the
1125 inferior executable, so we must ignore it. For some versions of
1126 SVR4, it has no name. For others (Solaris 2.3 for example), it
1127 does have a name, so we can no longer use a missing name to
1128 decide when to ignore it. */
1129 if (IGNORE_FIRST_LINK_MAP_ENTRY (new) && ldsomap
== 0)
1131 info
->main_lm_addr
= new->lm_info
->lm_addr
;
1139 /* Extract this shared object's name. */
1140 target_read_string (LM_NAME (new), &buffer
,
1141 SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
1143 warning (_("Can't read pathname for load map: %s."),
1144 safe_strerror (errcode
));
1147 strncpy (new->so_name
, buffer
, SO_NAME_MAX_PATH_SIZE
- 1);
1148 new->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
1149 strcpy (new->so_original_name
, new->so_name
);
1153 /* If this entry has no name, or its name matches the name
1154 for the main executable, don't include it in the list. */
1155 if (! new->so_name
[0]
1156 || match_main (new->so_name
))
1162 link_ptr
= &new->next
;
1166 /* On Solaris, the dynamic linker is not in the normal list of
1167 shared objects, so make sure we pick it up too. Having
1168 symbol information for the dynamic linker is quite crucial
1169 for skipping dynamic linker resolver code. */
1170 if (lm
== 0 && ldsomap
== 0)
1171 lm
= ldsomap
= solib_svr4_r_ldsomap (info
);
1173 discard_cleanups (old_chain
);
1177 return svr4_default_sos ();
1182 /* Get the address of the link_map for a given OBJFILE. */
1185 svr4_fetch_objfile_link_map (struct objfile
*objfile
)
1188 struct svr4_info
*info
= get_svr4_info ();
1190 /* Cause svr4_current_sos() to be run if it hasn't been already. */
1191 if (info
->main_lm_addr
== 0)
1192 solib_add (NULL
, 0, ¤t_target
, auto_solib_add
);
1194 /* svr4_current_sos() will set main_lm_addr for the main executable. */
1195 if (objfile
== symfile_objfile
)
1196 return info
->main_lm_addr
;
1198 /* The other link map addresses may be found by examining the list
1199 of shared libraries. */
1200 for (so
= master_so_list (); so
; so
= so
->next
)
1201 if (so
->objfile
== objfile
)
1202 return so
->lm_info
->lm_addr
;
1208 /* On some systems, the only way to recognize the link map entry for
1209 the main executable file is by looking at its name. Return
1210 non-zero iff SONAME matches one of the known main executable names. */
1213 match_main (char *soname
)
1217 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
1219 if (strcmp (soname
, *mainp
) == 0)
1226 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1227 SVR4 run time loader. */
1230 svr4_in_dynsym_resolve_code (CORE_ADDR pc
)
1232 struct svr4_info
*info
= get_svr4_info ();
1234 return ((pc
>= info
->interp_text_sect_low
1235 && pc
< info
->interp_text_sect_high
)
1236 || (pc
>= info
->interp_plt_sect_low
1237 && pc
< info
->interp_plt_sect_high
)
1238 || in_plt_section (pc
, NULL
));
1241 /* Given an executable's ABFD and target, compute the entry-point
1245 exec_entry_point (struct bfd
*abfd
, struct target_ops
*targ
)
1247 /* KevinB wrote ... for most targets, the address returned by
1248 bfd_get_start_address() is the entry point for the start
1249 function. But, for some targets, bfd_get_start_address() returns
1250 the address of a function descriptor from which the entry point
1251 address may be extracted. This address is extracted by
1252 gdbarch_convert_from_func_ptr_addr(). The method
1253 gdbarch_convert_from_func_ptr_addr() is the merely the identify
1254 function for targets which don't use function descriptors. */
1255 return gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
1256 bfd_get_start_address (abfd
),
1264 enable_break -- arrange for dynamic linker to hit breakpoint
1268 int enable_break (void)
1272 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1273 debugger interface, support for arranging for the inferior to hit
1274 a breakpoint after mapping in the shared libraries. This function
1275 enables that breakpoint.
1277 For SunOS, there is a special flag location (in_debugger) which we
1278 set to 1. When the dynamic linker sees this flag set, it will set
1279 a breakpoint at a location known only to itself, after saving the
1280 original contents of that place and the breakpoint address itself,
1281 in it's own internal structures. When we resume the inferior, it
1282 will eventually take a SIGTRAP when it runs into the breakpoint.
1283 We handle this (in a different place) by restoring the contents of
1284 the breakpointed location (which is only known after it stops),
1285 chasing around to locate the shared libraries that have been
1286 loaded, then resuming.
1288 For SVR4, the debugger interface structure contains a member (r_brk)
1289 which is statically initialized at the time the shared library is
1290 built, to the offset of a function (_r_debug_state) which is guaran-
1291 teed to be called once before mapping in a library, and again when
1292 the mapping is complete. At the time we are examining this member,
1293 it contains only the unrelocated offset of the function, so we have
1294 to do our own relocation. Later, when the dynamic linker actually
1295 runs, it relocates r_brk to be the actual address of _r_debug_state().
1297 The debugger interface structure also contains an enumeration which
1298 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1299 depending upon whether or not the library is being mapped or unmapped,
1300 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1304 enable_break (struct svr4_info
*info
, int from_tty
)
1306 struct minimal_symbol
*msymbol
;
1308 asection
*interp_sect
;
1309 gdb_byte
*interp_name
;
1312 /* First, remove all the solib event breakpoints. Their addresses
1313 may have changed since the last time we ran the program. */
1314 remove_solib_event_breakpoints ();
1316 info
->interp_text_sect_low
= info
->interp_text_sect_high
= 0;
1317 info
->interp_plt_sect_low
= info
->interp_plt_sect_high
= 0;
1319 /* If we already have a shared library list in the target, and
1320 r_debug contains r_brk, set the breakpoint there - this should
1321 mean r_brk has already been relocated. Assume the dynamic linker
1322 is the object containing r_brk. */
1324 solib_add (NULL
, from_tty
, ¤t_target
, auto_solib_add
);
1326 if (info
->debug_base
&& solib_svr4_r_map (info
) != 0)
1327 sym_addr
= solib_svr4_r_brk (info
);
1331 struct obj_section
*os
;
1333 sym_addr
= gdbarch_addr_bits_remove
1334 (target_gdbarch
, gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
1338 /* On at least some versions of Solaris there's a dynamic relocation
1339 on _r_debug.r_brk and SYM_ADDR may not be relocated yet, e.g., if
1340 we get control before the dynamic linker has self-relocated.
1341 Check if SYM_ADDR is in a known section, if it is assume we can
1342 trust its value. This is just a heuristic though, it could go away
1343 or be replaced if it's getting in the way.
1345 On ARM we need to know whether the ISA of rtld_db_dlactivity (or
1346 however it's spelled in your particular system) is ARM or Thumb.
1347 That knowledge is encoded in the address, if it's Thumb the low bit
1348 is 1. However, we've stripped that info above and it's not clear
1349 what all the consequences are of passing a non-addr_bits_remove'd
1350 address to create_solib_event_breakpoint. The call to
1351 find_pc_section verifies we know about the address and have some
1352 hope of computing the right kind of breakpoint to use (via
1353 symbol info). It does mean that GDB needs to be pointed at a
1354 non-stripped version of the dynamic linker in order to obtain
1355 information it already knows about. Sigh. */
1357 os
= find_pc_section (sym_addr
);
1360 /* Record the relocated start and end address of the dynamic linker
1361 text and plt section for svr4_in_dynsym_resolve_code. */
1363 CORE_ADDR load_addr
;
1365 tmp_bfd
= os
->objfile
->obfd
;
1366 load_addr
= ANOFFSET (os
->objfile
->section_offsets
,
1367 os
->objfile
->sect_index_text
);
1369 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
1372 info
->interp_text_sect_low
=
1373 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1374 info
->interp_text_sect_high
=
1375 info
->interp_text_sect_low
1376 + bfd_section_size (tmp_bfd
, interp_sect
);
1378 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
1381 info
->interp_plt_sect_low
=
1382 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1383 info
->interp_plt_sect_high
=
1384 info
->interp_plt_sect_low
1385 + bfd_section_size (tmp_bfd
, interp_sect
);
1388 create_solib_event_breakpoint (target_gdbarch
, sym_addr
);
1393 /* Find the program interpreter; if not found, warn the user and drop
1394 into the old breakpoint at symbol code. */
1395 interp_name
= find_program_interpreter ();
1398 CORE_ADDR load_addr
= 0;
1399 int load_addr_found
= 0;
1400 int loader_found_in_list
= 0;
1402 bfd
*tmp_bfd
= NULL
;
1403 struct target_ops
*tmp_bfd_target
;
1404 volatile struct gdb_exception ex
;
1408 /* Now we need to figure out where the dynamic linker was
1409 loaded so that we can load its symbols and place a breakpoint
1410 in the dynamic linker itself.
1412 This address is stored on the stack. However, I've been unable
1413 to find any magic formula to find it for Solaris (appears to
1414 be trivial on GNU/Linux). Therefore, we have to try an alternate
1415 mechanism to find the dynamic linker's base address. */
1417 TRY_CATCH (ex
, RETURN_MASK_ALL
)
1419 tmp_bfd
= solib_bfd_open (interp_name
);
1421 if (tmp_bfd
== NULL
)
1422 goto bkpt_at_symbol
;
1424 /* Now convert the TMP_BFD into a target. That way target, as
1425 well as BFD operations can be used. Note that closing the
1426 target will also close the underlying bfd. */
1427 tmp_bfd_target
= target_bfd_reopen (tmp_bfd
);
1429 /* On a running target, we can get the dynamic linker's base
1430 address from the shared library table. */
1431 so
= master_so_list ();
1434 if (svr4_same_1 (interp_name
, so
->so_original_name
))
1436 load_addr_found
= 1;
1437 loader_found_in_list
= 1;
1438 load_addr
= LM_ADDR_CHECK (so
, tmp_bfd
);
1444 /* If we were not able to find the base address of the loader
1445 from our so_list, then try using the AT_BASE auxilliary entry. */
1446 if (!load_addr_found
)
1447 if (target_auxv_search (¤t_target
, AT_BASE
, &load_addr
) > 0)
1448 load_addr_found
= 1;
1450 /* Otherwise we find the dynamic linker's base address by examining
1451 the current pc (which should point at the entry point for the
1452 dynamic linker) and subtracting the offset of the entry point.
1454 This is more fragile than the previous approaches, but is a good
1455 fallback method because it has actually been working well in
1457 if (!load_addr_found
)
1459 struct regcache
*regcache
1460 = get_thread_arch_regcache (inferior_ptid
, target_gdbarch
);
1461 load_addr
= (regcache_read_pc (regcache
)
1462 - exec_entry_point (tmp_bfd
, tmp_bfd_target
));
1465 if (!loader_found_in_list
)
1467 info
->debug_loader_name
= xstrdup (interp_name
);
1468 info
->debug_loader_offset_p
= 1;
1469 info
->debug_loader_offset
= load_addr
;
1470 solib_add (NULL
, from_tty
, ¤t_target
, auto_solib_add
);
1473 /* Record the relocated start and end address of the dynamic linker
1474 text and plt section for svr4_in_dynsym_resolve_code. */
1475 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
1478 info
->interp_text_sect_low
=
1479 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1480 info
->interp_text_sect_high
=
1481 info
->interp_text_sect_low
1482 + bfd_section_size (tmp_bfd
, interp_sect
);
1484 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
1487 info
->interp_plt_sect_low
=
1488 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1489 info
->interp_plt_sect_high
=
1490 info
->interp_plt_sect_low
1491 + bfd_section_size (tmp_bfd
, interp_sect
);
1494 /* Now try to set a breakpoint in the dynamic linker. */
1495 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1497 sym_addr
= bfd_lookup_symbol (tmp_bfd
, *bkpt_namep
);
1503 /* Convert 'sym_addr' from a function pointer to an address.
1504 Because we pass tmp_bfd_target instead of the current
1505 target, this will always produce an unrelocated value. */
1506 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
1510 /* We're done with both the temporary bfd and target. Remember,
1511 closing the target closes the underlying bfd. */
1512 target_close (tmp_bfd_target
, 0);
1516 create_solib_event_breakpoint (target_gdbarch
, load_addr
+ sym_addr
);
1517 xfree (interp_name
);
1521 /* For whatever reason we couldn't set a breakpoint in the dynamic
1522 linker. Warn and drop into the old code. */
1524 xfree (interp_name
);
1525 warning (_("Unable to find dynamic linker breakpoint function.\n"
1526 "GDB will be unable to debug shared library initializers\n"
1527 "and track explicitly loaded dynamic code."));
1530 /* Scan through the lists of symbols, trying to look up the symbol and
1531 set a breakpoint there. Terminate loop when we/if we succeed. */
1533 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1535 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
1536 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1538 sym_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1539 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
1542 create_solib_event_breakpoint (target_gdbarch
, sym_addr
);
1547 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1549 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
1550 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1552 sym_addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
1553 sym_addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
,
1556 create_solib_event_breakpoint (target_gdbarch
, sym_addr
);
1567 special_symbol_handling -- additional shared library symbol handling
1571 void special_symbol_handling ()
1575 Once the symbols from a shared object have been loaded in the usual
1576 way, we are called to do any system specific symbol handling that
1579 For SunOS4, this consisted of grunging around in the dynamic
1580 linkers structures to find symbol definitions for "common" symbols
1581 and adding them to the minimal symbol table for the runtime common
1584 However, for SVR4, there's nothing to do.
1589 svr4_special_symbol_handling (void)
1591 svr4_relocate_main_executable ();
1594 /* Decide if the objfile needs to be relocated. As indicated above,
1595 we will only be here when execution is stopped at the beginning
1596 of the program. Relocation is necessary if the address at which
1597 we are presently stopped differs from the start address stored in
1598 the executable AND there's no interpreter section. The condition
1599 regarding the interpreter section is very important because if
1600 there *is* an interpreter section, execution will begin there
1601 instead. When there is an interpreter section, the start address
1602 is (presumably) used by the interpreter at some point to start
1603 execution of the program.
1605 If there is an interpreter, it is normal for it to be set to an
1606 arbitrary address at the outset. The job of finding it is
1607 handled in enable_break().
1609 So, to summarize, relocations are necessary when there is no
1610 interpreter section and the start address obtained from the
1611 executable is different from the address at which GDB is
1614 [ The astute reader will note that we also test to make sure that
1615 the executable in question has the DYNAMIC flag set. It is my
1616 opinion that this test is unnecessary (undesirable even). It
1617 was added to avoid inadvertent relocation of an executable
1618 whose e_type member in the ELF header is not ET_DYN. There may
1619 be a time in the future when it is desirable to do relocations
1620 on other types of files as well in which case this condition
1621 should either be removed or modified to accomodate the new file
1622 type. (E.g, an ET_EXEC executable which has been built to be
1623 position-independent could safely be relocated by the OS if
1624 desired. It is true that this violates the ABI, but the ABI
1625 has been known to be bent from time to time.) - Kevin, Nov 2000. ]
1629 svr4_static_exec_displacement (void)
1631 asection
*interp_sect
;
1632 struct regcache
*regcache
1633 = get_thread_arch_regcache (inferior_ptid
, target_gdbarch
);
1634 CORE_ADDR pc
= regcache_read_pc (regcache
);
1636 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
1637 if (interp_sect
== NULL
1638 && (bfd_get_file_flags (exec_bfd
) & DYNAMIC
) != 0
1639 && (exec_entry_point (exec_bfd
, &exec_ops
) != pc
))
1640 return pc
- exec_entry_point (exec_bfd
, &exec_ops
);
1645 /* We relocate all of the sections by the same amount. This
1646 behavior is mandated by recent editions of the System V ABI.
1647 According to the System V Application Binary Interface,
1648 Edition 4.1, page 5-5:
1650 ... Though the system chooses virtual addresses for
1651 individual processes, it maintains the segments' relative
1652 positions. Because position-independent code uses relative
1653 addressesing between segments, the difference between
1654 virtual addresses in memory must match the difference
1655 between virtual addresses in the file. The difference
1656 between the virtual address of any segment in memory and
1657 the corresponding virtual address in the file is thus a
1658 single constant value for any one executable or shared
1659 object in a given process. This difference is the base
1660 address. One use of the base address is to relocate the
1661 memory image of the program during dynamic linking.
1663 The same language also appears in Edition 4.0 of the System V
1664 ABI and is left unspecified in some of the earlier editions. */
1667 svr4_exec_displacement (void)
1670 /* ENTRY_POINT is a possible function descriptor - before
1671 a call to gdbarch_convert_from_func_ptr_addr. */
1672 CORE_ADDR entry_point
;
1674 if (exec_bfd
== NULL
)
1677 if (target_auxv_search (¤t_target
, AT_ENTRY
, &entry_point
) == 1)
1678 return entry_point
- bfd_get_start_address (exec_bfd
);
1680 return svr4_static_exec_displacement ();
1683 /* Relocate the main executable. This function should be called upon
1684 stopping the inferior process at the entry point to the program.
1685 The entry point from BFD is compared to the AT_ENTRY of AUXV and if they are
1686 different, the main executable is relocated by the proper amount. */
1689 svr4_relocate_main_executable (void)
1691 CORE_ADDR displacement
= svr4_exec_displacement ();
1693 /* Even if DISPLACEMENT is 0 still try to relocate it as this is a new
1694 difference of in-memory vs. in-file addresses and we could already
1695 relocate the executable at this function to improper address before. */
1697 if (symfile_objfile
)
1699 struct section_offsets
*new_offsets
;
1702 new_offsets
= alloca (symfile_objfile
->num_sections
1703 * sizeof (*new_offsets
));
1705 for (i
= 0; i
< symfile_objfile
->num_sections
; i
++)
1706 new_offsets
->offsets
[i
] = displacement
;
1708 objfile_relocate (symfile_objfile
, new_offsets
);
1714 for (asect
= exec_bfd
->sections
; asect
!= NULL
; asect
= asect
->next
)
1715 exec_set_section_address (bfd_get_filename (exec_bfd
), asect
->index
,
1716 (bfd_section_vma (exec_bfd
, asect
)
1725 svr4_solib_create_inferior_hook -- shared library startup support
1729 void svr4_solib_create_inferior_hook (int from_tty)
1733 When gdb starts up the inferior, it nurses it along (through the
1734 shell) until it is ready to execute it's first instruction. At this
1735 point, this function gets called via expansion of the macro
1736 SOLIB_CREATE_INFERIOR_HOOK.
1738 For SunOS executables, this first instruction is typically the
1739 one at "_start", or a similar text label, regardless of whether
1740 the executable is statically or dynamically linked. The runtime
1741 startup code takes care of dynamically linking in any shared
1742 libraries, once gdb allows the inferior to continue.
1744 For SVR4 executables, this first instruction is either the first
1745 instruction in the dynamic linker (for dynamically linked
1746 executables) or the instruction at "start" for statically linked
1747 executables. For dynamically linked executables, the system
1748 first exec's /lib/libc.so.N, which contains the dynamic linker,
1749 and starts it running. The dynamic linker maps in any needed
1750 shared libraries, maps in the actual user executable, and then
1751 jumps to "start" in the user executable.
1753 For both SunOS shared libraries, and SVR4 shared libraries, we
1754 can arrange to cooperate with the dynamic linker to discover the
1755 names of shared libraries that are dynamically linked, and the
1756 base addresses to which they are linked.
1758 This function is responsible for discovering those names and
1759 addresses, and saving sufficient information about them to allow
1760 their symbols to be read at a later time.
1764 Between enable_break() and disable_break(), this code does not
1765 properly handle hitting breakpoints which the user might have
1766 set in the startup code or in the dynamic linker itself. Proper
1767 handling will probably have to wait until the implementation is
1768 changed to use the "breakpoint handler function" method.
1770 Also, what if child has exit()ed? Must exit loop somehow.
1774 svr4_solib_create_inferior_hook (int from_tty
)
1776 struct inferior
*inf
;
1777 struct thread_info
*tp
;
1778 struct svr4_info
*info
;
1780 info
= get_svr4_info ();
1782 /* Relocate the main executable if necessary. */
1783 if (current_inferior ()->attach_flag
== 0)
1784 svr4_relocate_main_executable ();
1786 if (!svr4_have_link_map_offsets ())
1789 if (!enable_break (info
, from_tty
))
1792 #if defined(_SCO_DS)
1793 /* SCO needs the loop below, other systems should be using the
1794 special shared library breakpoints and the shared library breakpoint
1797 Now run the target. It will eventually hit the breakpoint, at
1798 which point all of the libraries will have been mapped in and we
1799 can go groveling around in the dynamic linker structures to find
1800 out what we need to know about them. */
1802 inf
= current_inferior ();
1803 tp
= inferior_thread ();
1805 clear_proceed_status ();
1806 inf
->stop_soon
= STOP_QUIETLY
;
1807 tp
->stop_signal
= TARGET_SIGNAL_0
;
1810 target_resume (pid_to_ptid (-1), 0, tp
->stop_signal
);
1811 wait_for_inferior (0);
1813 while (tp
->stop_signal
!= TARGET_SIGNAL_TRAP
);
1814 inf
->stop_soon
= NO_STOP_QUIETLY
;
1815 #endif /* defined(_SCO_DS) */
1819 svr4_clear_solib (void)
1821 struct svr4_info
*info
;
1823 info
= get_svr4_info ();
1824 info
->debug_base
= 0;
1825 info
->debug_loader_offset_p
= 0;
1826 info
->debug_loader_offset
= 0;
1827 xfree (info
->debug_loader_name
);
1828 info
->debug_loader_name
= NULL
;
1832 svr4_free_so (struct so_list
*so
)
1834 xfree (so
->lm_info
->lm
);
1835 xfree (so
->lm_info
);
1839 /* Clear any bits of ADDR that wouldn't fit in a target-format
1840 data pointer. "Data pointer" here refers to whatever sort of
1841 address the dynamic linker uses to manage its sections. At the
1842 moment, we don't support shared libraries on any processors where
1843 code and data pointers are different sizes.
1845 This isn't really the right solution. What we really need here is
1846 a way to do arithmetic on CORE_ADDR values that respects the
1847 natural pointer/address correspondence. (For example, on the MIPS,
1848 converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
1849 sign-extend the value. There, simply truncating the bits above
1850 gdbarch_ptr_bit, as we do below, is no good.) This should probably
1851 be a new gdbarch method or something. */
1853 svr4_truncate_ptr (CORE_ADDR addr
)
1855 if (gdbarch_ptr_bit (target_gdbarch
) == sizeof (CORE_ADDR
) * 8)
1856 /* We don't need to truncate anything, and the bit twiddling below
1857 will fail due to overflow problems. */
1860 return addr
& (((CORE_ADDR
) 1 << gdbarch_ptr_bit (target_gdbarch
)) - 1);
1865 svr4_relocate_section_addresses (struct so_list
*so
,
1866 struct target_section
*sec
)
1868 sec
->addr
= svr4_truncate_ptr (sec
->addr
+ LM_ADDR_CHECK (so
,
1870 sec
->endaddr
= svr4_truncate_ptr (sec
->endaddr
+ LM_ADDR_CHECK (so
,
1875 /* Architecture-specific operations. */
1877 /* Per-architecture data key. */
1878 static struct gdbarch_data
*solib_svr4_data
;
1880 struct solib_svr4_ops
1882 /* Return a description of the layout of `struct link_map'. */
1883 struct link_map_offsets
*(*fetch_link_map_offsets
)(void);
1886 /* Return a default for the architecture-specific operations. */
1889 solib_svr4_init (struct obstack
*obstack
)
1891 struct solib_svr4_ops
*ops
;
1893 ops
= OBSTACK_ZALLOC (obstack
, struct solib_svr4_ops
);
1894 ops
->fetch_link_map_offsets
= NULL
;
1898 /* Set the architecture-specific `struct link_map_offsets' fetcher for
1899 GDBARCH to FLMO. Also, install SVR4 solib_ops into GDBARCH. */
1902 set_solib_svr4_fetch_link_map_offsets (struct gdbarch
*gdbarch
,
1903 struct link_map_offsets
*(*flmo
) (void))
1905 struct solib_svr4_ops
*ops
= gdbarch_data (gdbarch
, solib_svr4_data
);
1907 ops
->fetch_link_map_offsets
= flmo
;
1909 set_solib_ops (gdbarch
, &svr4_so_ops
);
1912 /* Fetch a link_map_offsets structure using the architecture-specific
1913 `struct link_map_offsets' fetcher. */
1915 static struct link_map_offsets
*
1916 svr4_fetch_link_map_offsets (void)
1918 struct solib_svr4_ops
*ops
= gdbarch_data (target_gdbarch
, solib_svr4_data
);
1920 gdb_assert (ops
->fetch_link_map_offsets
);
1921 return ops
->fetch_link_map_offsets ();
1924 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
1927 svr4_have_link_map_offsets (void)
1929 struct solib_svr4_ops
*ops
= gdbarch_data (target_gdbarch
, solib_svr4_data
);
1930 return (ops
->fetch_link_map_offsets
!= NULL
);
1934 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
1935 `struct r_debug' and a `struct link_map' that are binary compatible
1936 with the origional SVR4 implementation. */
1938 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1939 for an ILP32 SVR4 system. */
1941 struct link_map_offsets
*
1942 svr4_ilp32_fetch_link_map_offsets (void)
1944 static struct link_map_offsets lmo
;
1945 static struct link_map_offsets
*lmp
= NULL
;
1951 lmo
.r_version_offset
= 0;
1952 lmo
.r_version_size
= 4;
1953 lmo
.r_map_offset
= 4;
1954 lmo
.r_brk_offset
= 8;
1955 lmo
.r_ldsomap_offset
= 20;
1957 /* Everything we need is in the first 20 bytes. */
1958 lmo
.link_map_size
= 20;
1959 lmo
.l_addr_offset
= 0;
1960 lmo
.l_name_offset
= 4;
1961 lmo
.l_ld_offset
= 8;
1962 lmo
.l_next_offset
= 12;
1963 lmo
.l_prev_offset
= 16;
1969 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1970 for an LP64 SVR4 system. */
1972 struct link_map_offsets
*
1973 svr4_lp64_fetch_link_map_offsets (void)
1975 static struct link_map_offsets lmo
;
1976 static struct link_map_offsets
*lmp
= NULL
;
1982 lmo
.r_version_offset
= 0;
1983 lmo
.r_version_size
= 4;
1984 lmo
.r_map_offset
= 8;
1985 lmo
.r_brk_offset
= 16;
1986 lmo
.r_ldsomap_offset
= 40;
1988 /* Everything we need is in the first 40 bytes. */
1989 lmo
.link_map_size
= 40;
1990 lmo
.l_addr_offset
= 0;
1991 lmo
.l_name_offset
= 8;
1992 lmo
.l_ld_offset
= 16;
1993 lmo
.l_next_offset
= 24;
1994 lmo
.l_prev_offset
= 32;
2001 struct target_so_ops svr4_so_ops
;
2003 /* Lookup global symbol for ELF DSOs linked with -Bsymbolic. Those DSOs have a
2004 different rule for symbol lookup. The lookup begins here in the DSO, not in
2005 the main executable. */
2007 static struct symbol
*
2008 elf_lookup_lib_symbol (const struct objfile
*objfile
,
2010 const char *linkage_name
,
2011 const domain_enum domain
)
2015 if (objfile
== symfile_objfile
)
2019 /* OBJFILE should have been passed as the non-debug one. */
2020 gdb_assert (objfile
->separate_debug_objfile_backlink
== NULL
);
2022 abfd
= objfile
->obfd
;
2025 if (abfd
== NULL
|| scan_dyntag (DT_SYMBOLIC
, abfd
, NULL
) != 1)
2028 return lookup_global_symbol_from_objfile
2029 (objfile
, name
, linkage_name
, domain
);
2032 extern initialize_file_ftype _initialize_svr4_solib
; /* -Wmissing-prototypes */
2035 _initialize_svr4_solib (void)
2037 solib_svr4_data
= gdbarch_data_register_pre_init (solib_svr4_init
);
2038 solib_svr4_pspace_data
2039 = register_program_space_data_with_cleanup (svr4_pspace_data_cleanup
);
2041 svr4_so_ops
.relocate_section_addresses
= svr4_relocate_section_addresses
;
2042 svr4_so_ops
.free_so
= svr4_free_so
;
2043 svr4_so_ops
.clear_solib
= svr4_clear_solib
;
2044 svr4_so_ops
.solib_create_inferior_hook
= svr4_solib_create_inferior_hook
;
2045 svr4_so_ops
.special_symbol_handling
= svr4_special_symbol_handling
;
2046 svr4_so_ops
.current_sos
= svr4_current_sos
;
2047 svr4_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
2048 svr4_so_ops
.in_dynsym_resolve_code
= svr4_in_dynsym_resolve_code
;
2049 svr4_so_ops
.bfd_open
= solib_bfd_open
;
2050 svr4_so_ops
.lookup_lib_global_symbol
= elf_lookup_lib_symbol
;
2051 svr4_so_ops
.same
= svr4_same
;
2052 svr4_so_ops
.keep_data_in_core
= svr4_keep_data_in_core
;