1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 2000, 2001, 2003, 2004, 2005, 2006
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 2 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, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
26 #include "elf/external.h"
27 #include "elf/common.h"
38 #include "gdb_assert.h"
42 #include "solib-svr4.h"
44 #include "bfd-target.h"
48 static struct link_map_offsets
*svr4_fetch_link_map_offsets (void);
49 static int svr4_have_link_map_offsets (void);
51 /* This hook is set to a function that provides native link map
52 offsets if the code in solib-legacy.c is linked in. */
53 struct link_map_offsets
*(*legacy_svr4_fetch_link_map_offsets_hook
) (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. */
72 /* On SVR4 systems, a list of symbols in the dynamic linker where
73 GDB can try to place a breakpoint to monitor shared library
76 If none of these symbols are found, or other errors occur, then
77 SVR4 systems will fall back to using a symbol as the "startup
78 mapping complete" breakpoint address. */
80 static char *solib_break_names
[] =
88 /* On the 64-bit PowerPC, the linker symbol with the same name as
89 the C function points to a function descriptor, not to the entry
90 point. The linker symbol whose name is the C function name
91 prefixed with a '.' points to the function's entry point. So
92 when we look through this table, we ignore symbols that point
93 into the data section (thus skipping the descriptor's symbol),
94 and eventually try this one, giving us the real entry point
101 #define BKPT_AT_SYMBOL 1
103 #if defined (BKPT_AT_SYMBOL)
104 static char *bkpt_names
[] =
106 #ifdef SOLIB_BKPT_NAME
107 SOLIB_BKPT_NAME
, /* Prefer configured name if it exists. */
116 static char *main_name_list
[] =
122 /* Macro to extract an address from a solib structure. When GDB is
123 configured for some 32-bit targets (e.g. Solaris 2.7 sparc), BFD is
124 configured to handle 64-bit targets, so CORE_ADDR is 64 bits. We
125 have to extract only the significant bits of addresses to get the
126 right address when accessing the core file BFD.
128 Assume that the address is unsigned. */
130 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
131 extract_unsigned_integer (&(MEMBER), sizeof (MEMBER))
133 /* local data declarations */
135 /* link map access functions */
138 LM_ADDR_FROM_LINK_MAP (struct so_list
*so
)
140 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
142 return (CORE_ADDR
) extract_signed_integer (so
->lm_info
->lm
143 + lmo
->l_addr_offset
,
148 HAS_LM_DYNAMIC_FROM_LINK_MAP ()
150 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
152 return (lmo
->l_ld_size
!= 0);
156 LM_DYNAMIC_FROM_LINK_MAP (struct so_list
*so
)
158 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
160 gdb_assert (lmo
->l_ld_size
!= 0);
162 return (CORE_ADDR
) extract_signed_integer (so
->lm_info
->lm
168 LM_ADDR_CHECK (struct so_list
*so
, bfd
*abfd
)
170 if (so
->lm_info
->l_addr
== (CORE_ADDR
)-1)
172 struct bfd_section
*dyninfo_sect
;
173 CORE_ADDR l_addr
, l_dynaddr
, dynaddr
, align
= 0x1000;
175 l_addr
= LM_ADDR_FROM_LINK_MAP (so
);
177 if (! abfd
|| ! HAS_LM_DYNAMIC_FROM_LINK_MAP ())
180 l_dynaddr
= LM_DYNAMIC_FROM_LINK_MAP (so
);
182 dyninfo_sect
= bfd_get_section_by_name (abfd
, ".dynamic");
183 if (dyninfo_sect
== NULL
)
186 dynaddr
= bfd_section_vma (abfd
, dyninfo_sect
);
188 if (dynaddr
+ l_addr
!= l_dynaddr
)
190 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
192 Elf_Internal_Ehdr
*ehdr
= elf_tdata (abfd
)->elf_header
;
193 Elf_Internal_Phdr
*phdr
= elf_tdata (abfd
)->phdr
;
198 for (i
= 0; i
< ehdr
->e_phnum
; i
++)
199 if (phdr
[i
].p_type
== PT_LOAD
&& phdr
[i
].p_align
> align
)
200 align
= phdr
[i
].p_align
;
203 /* Turn it into a mask. */
206 /* If the changes match the alignment requirements, we
207 assume we're using a core file that was generated by the
208 same binary, just prelinked with a different base offset.
209 If it doesn't match, we may have a different binary, the
210 same binary with the dynamic table loaded at an unrelated
211 location, or anything, really. To avoid regressions,
212 don't adjust the base offset in the latter case, although
213 odds are that, if things really changed, debugging won't
215 if ((l_addr
& align
) == 0 && ((dynaddr
- l_dynaddr
) & align
) == 0)
217 l_addr
= l_dynaddr
- dynaddr
;
219 warning (_(".dynamic section for \"%s\" "
220 "is not at the expected address"), so
->so_name
);
221 warning (_("difference appears to be caused by prelink, "
222 "adjusting expectations"));
225 warning (_(".dynamic section for \"%s\" "
226 "is not at the expected address "
227 "(wrong library or version mismatch?)"), so
->so_name
);
231 so
->lm_info
->l_addr
= l_addr
;
234 return so
->lm_info
->l_addr
;
238 LM_NEXT (struct so_list
*so
)
240 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
242 /* Assume that the address is unsigned. */
243 return extract_unsigned_integer (so
->lm_info
->lm
+ lmo
->l_next_offset
,
248 LM_NAME (struct so_list
*so
)
250 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
252 /* Assume that the address is unsigned. */
253 return extract_unsigned_integer (so
->lm_info
->lm
+ lmo
->l_name_offset
,
258 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list
*so
)
260 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
262 /* Assume that the address is unsigned. */
263 return extract_unsigned_integer (so
->lm_info
->lm
+ lmo
->l_prev_offset
,
264 lmo
->l_prev_size
) == 0;
267 static CORE_ADDR debug_base
; /* Base of dynamic linker structures */
268 static CORE_ADDR breakpoint_addr
; /* Address where end bkpt is set */
270 /* Local function prototypes */
272 static int match_main (char *);
274 static CORE_ADDR
bfd_lookup_symbol (bfd
*, char *, flagword
);
280 bfd_lookup_symbol -- lookup the value for a specific symbol
284 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname, flagword sect_flags)
288 An expensive way to lookup the value of a single symbol for
289 bfd's that are only temporary anyway. This is used by the
290 shared library support to find the address of the debugger
291 interface structures in the shared library.
293 If SECT_FLAGS is non-zero, only match symbols in sections whose
294 flags include all those in SECT_FLAGS.
296 Note that 0 is specifically allowed as an error return (no
301 bfd_lookup_symbol (bfd
*abfd
, char *symname
, flagword sect_flags
)
305 asymbol
**symbol_table
;
306 unsigned int number_of_symbols
;
308 struct cleanup
*back_to
;
309 CORE_ADDR symaddr
= 0;
311 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
313 if (storage_needed
> 0)
315 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
316 back_to
= make_cleanup (xfree
, symbol_table
);
317 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
319 for (i
= 0; i
< number_of_symbols
; i
++)
321 sym
= *symbol_table
++;
322 if (strcmp (sym
->name
, symname
) == 0
323 && (sym
->section
->flags
& sect_flags
) == sect_flags
)
325 /* Bfd symbols are section relative. */
326 symaddr
= sym
->value
+ sym
->section
->vma
;
330 do_cleanups (back_to
);
336 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
337 have to check the dynamic string table too. */
339 storage_needed
= bfd_get_dynamic_symtab_upper_bound (abfd
);
341 if (storage_needed
> 0)
343 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
344 back_to
= make_cleanup (xfree
, symbol_table
);
345 number_of_symbols
= bfd_canonicalize_dynamic_symtab (abfd
, symbol_table
);
347 for (i
= 0; i
< number_of_symbols
; i
++)
349 sym
= *symbol_table
++;
351 if (strcmp (sym
->name
, symname
) == 0
352 && (sym
->section
->flags
& sect_flags
) == sect_flags
)
354 /* Bfd symbols are section relative. */
355 symaddr
= sym
->value
+ sym
->section
->vma
;
359 do_cleanups (back_to
);
369 elf_locate_base -- locate the base address of dynamic linker structs
370 for SVR4 elf targets.
374 CORE_ADDR elf_locate_base (void)
378 For SVR4 elf targets the address of the dynamic linker's runtime
379 structure is contained within the dynamic info section in the
380 executable file. The dynamic section is also mapped into the
381 inferior address space. Because the runtime loader fills in the
382 real address before starting the inferior, we have to read in the
383 dynamic info section from the inferior address space.
384 If there are any errors while trying to find the address, we
385 silently return 0, otherwise the found address is returned.
390 elf_locate_base (void)
392 struct bfd_section
*dyninfo_sect
;
393 int dyninfo_sect_size
;
394 CORE_ADDR dyninfo_addr
;
399 /* Find the start address of the .dynamic section. */
400 dyninfo_sect
= bfd_get_section_by_name (exec_bfd
, ".dynamic");
401 if (dyninfo_sect
== NULL
)
403 dyninfo_addr
= bfd_section_vma (exec_bfd
, dyninfo_sect
);
405 /* Read in .dynamic section, silently ignore errors. */
406 dyninfo_sect_size
= bfd_section_size (exec_bfd
, dyninfo_sect
);
407 buf
= alloca (dyninfo_sect_size
);
408 if (target_read_memory (dyninfo_addr
, buf
, dyninfo_sect_size
))
411 /* Find the DT_DEBUG entry in the the .dynamic section.
412 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
413 no DT_DEBUG entries. */
415 arch_size
= bfd_get_arch_size (exec_bfd
);
416 if (arch_size
== -1) /* failure */
421 for (bufend
= buf
+ dyninfo_sect_size
;
423 buf
+= sizeof (Elf32_External_Dyn
))
425 Elf32_External_Dyn
*x_dynp
= (Elf32_External_Dyn
*) buf
;
429 dyn_tag
= bfd_h_get_32 (exec_bfd
, (bfd_byte
*) x_dynp
->d_tag
);
430 if (dyn_tag
== DT_NULL
)
432 else if (dyn_tag
== DT_DEBUG
)
434 dyn_ptr
= bfd_h_get_32 (exec_bfd
,
435 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
438 else if (dyn_tag
== DT_MIPS_RLD_MAP
)
441 int pbuf_size
= TARGET_PTR_BIT
/ HOST_CHAR_BIT
;
443 pbuf
= alloca (pbuf_size
);
444 /* DT_MIPS_RLD_MAP contains a pointer to the address
445 of the dynamic link structure. */
446 dyn_ptr
= bfd_h_get_32 (exec_bfd
,
447 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
448 if (target_read_memory (dyn_ptr
, pbuf
, pbuf_size
))
450 return extract_unsigned_integer (pbuf
, pbuf_size
);
454 else /* 64-bit elf */
456 for (bufend
= buf
+ dyninfo_sect_size
;
458 buf
+= sizeof (Elf64_External_Dyn
))
460 Elf64_External_Dyn
*x_dynp
= (Elf64_External_Dyn
*) buf
;
464 dyn_tag
= bfd_h_get_64 (exec_bfd
, (bfd_byte
*) x_dynp
->d_tag
);
465 if (dyn_tag
== DT_NULL
)
467 else if (dyn_tag
== DT_DEBUG
)
469 dyn_ptr
= bfd_h_get_64 (exec_bfd
,
470 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
473 else if (dyn_tag
== DT_MIPS_RLD_MAP
)
476 int pbuf_size
= TARGET_PTR_BIT
/ HOST_CHAR_BIT
;
478 pbuf
= alloca (pbuf_size
);
479 /* DT_MIPS_RLD_MAP contains a pointer to the address
480 of the dynamic link structure. */
481 dyn_ptr
= bfd_h_get_64 (exec_bfd
,
482 (bfd_byte
*) x_dynp
->d_un
.d_ptr
);
483 if (target_read_memory (dyn_ptr
, pbuf
, pbuf_size
))
485 return extract_unsigned_integer (pbuf
, pbuf_size
);
490 /* DT_DEBUG entry not found. */
498 locate_base -- locate the base address of dynamic linker structs
502 CORE_ADDR locate_base (void)
506 For both the SunOS and SVR4 shared library implementations, if the
507 inferior executable has been linked dynamically, there is a single
508 address somewhere in the inferior's data space which is the key to
509 locating all of the dynamic linker's runtime structures. This
510 address is the value of the debug base symbol. The job of this
511 function is to find and return that address, or to return 0 if there
512 is no such address (the executable is statically linked for example).
514 For SunOS, the job is almost trivial, since the dynamic linker and
515 all of it's structures are statically linked to the executable at
516 link time. Thus the symbol for the address we are looking for has
517 already been added to the minimal symbol table for the executable's
518 objfile at the time the symbol file's symbols were read, and all we
519 have to do is look it up there. Note that we explicitly do NOT want
520 to find the copies in the shared library.
522 The SVR4 version is a bit more complicated because the address
523 is contained somewhere in the dynamic info section. We have to go
524 to a lot more work to discover the address of the debug base symbol.
525 Because of this complexity, we cache the value we find and return that
526 value on subsequent invocations. Note there is no copy in the
527 executable symbol tables.
534 /* Check to see if we have a currently valid address, and if so, avoid
535 doing all this work again and just return the cached address. If
536 we have no cached address, try to locate it in the dynamic info
537 section for ELF executables. There's no point in doing any of this
538 though if we don't have some link map offsets to work with. */
540 if (debug_base
== 0 && svr4_have_link_map_offsets ())
543 && bfd_get_flavour (exec_bfd
) == bfd_target_elf_flavour
)
544 debug_base
= elf_locate_base ();
549 /* Find the first element in the inferior's dynamic link map, and
550 return its address in the inferior.
552 FIXME: Perhaps we should validate the info somehow, perhaps by
553 checking r_version for a known version number, or r_state for
557 solib_svr4_r_map (void)
559 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
561 return read_memory_typed_address (debug_base
+ lmo
->r_map_offset
,
562 builtin_type_void_data_ptr
);
565 /* Find the link map for the dynamic linker (if it is not in the
566 normal list of loaded shared objects). */
569 solib_svr4_r_ldsomap (void)
571 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
574 /* Check version, and return zero if `struct r_debug' doesn't have
575 the r_ldsomap member. */
576 version
= read_memory_unsigned_integer (debug_base
+ lmo
->r_version_offset
,
577 lmo
->r_version_size
);
578 if (version
< 2 || lmo
->r_ldsomap_offset
== -1)
581 return read_memory_typed_address (debug_base
+ lmo
->r_ldsomap_offset
,
582 builtin_type_void_data_ptr
);
589 open_symbol_file_object
593 void open_symbol_file_object (void *from_tty)
597 If no open symbol file, attempt to locate and open the main symbol
598 file. On SVR4 systems, this is the first link map entry. If its
599 name is here, we can open it. Useful when attaching to a process
600 without first loading its symbol file.
602 If FROM_TTYP dereferences to a non-zero integer, allow messages to
603 be printed. This parameter is a pointer rather than an int because
604 open_symbol_file_object() is called via catch_errors() and
605 catch_errors() requires a pointer argument. */
608 open_symbol_file_object (void *from_ttyp
)
610 CORE_ADDR lm
, l_name
;
613 int from_tty
= *(int *)from_ttyp
;
614 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
615 gdb_byte
*l_name_buf
= xmalloc (lmo
->l_name_size
);
616 struct cleanup
*cleanups
= make_cleanup (xfree
, l_name_buf
);
619 if (!query ("Attempt to reload symbols from process? "))
622 if ((debug_base
= locate_base ()) == 0)
623 return 0; /* failed somehow... */
625 /* First link map member should be the executable. */
626 lm
= solib_svr4_r_map ();
628 return 0; /* failed somehow... */
630 /* Read address of name from target memory to GDB. */
631 read_memory (lm
+ lmo
->l_name_offset
, l_name_buf
, lmo
->l_name_size
);
633 /* Convert the address to host format. Assume that the address is
635 l_name
= extract_unsigned_integer (l_name_buf
, lmo
->l_name_size
);
637 /* Free l_name_buf. */
638 do_cleanups (cleanups
);
641 return 0; /* No filename. */
643 /* Now fetch the filename from target memory. */
644 target_read_string (l_name
, &filename
, SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
648 warning (_("failed to read exec filename from attached file: %s"),
649 safe_strerror (errcode
));
653 make_cleanup (xfree
, filename
);
654 /* Have a pathname: read the symbol file. */
655 symbol_file_add_main (filename
, from_tty
);
662 current_sos -- build a list of currently loaded shared objects
666 struct so_list *current_sos ()
670 Build a list of `struct so_list' objects describing the shared
671 objects currently loaded in the inferior. This list does not
672 include an entry for the main executable file.
674 Note that we only gather information directly available from the
675 inferior --- we don't examine any of the shared library files
676 themselves. The declaration of `struct so_list' says which fields
677 we provide values for. */
679 static struct so_list
*
680 svr4_current_sos (void)
683 struct so_list
*head
= 0;
684 struct so_list
**link_ptr
= &head
;
685 CORE_ADDR ldsomap
= 0;
687 /* Make sure we've looked up the inferior's dynamic linker's base
691 debug_base
= locate_base ();
693 /* If we can't find the dynamic linker's base structure, this
694 must not be a dynamically linked executable. Hmm. */
699 /* Walk the inferior's link map list, and build our list of
700 `struct so_list' nodes. */
701 lm
= solib_svr4_r_map ();
704 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
705 struct so_list
*new = XZALLOC (struct so_list
);
706 struct cleanup
*old_chain
= make_cleanup (xfree
, new);
708 new->lm_info
= xmalloc (sizeof (struct lm_info
));
709 make_cleanup (xfree
, new->lm_info
);
711 new->lm_info
->l_addr
= (CORE_ADDR
)-1;
712 new->lm_info
->lm
= xzalloc (lmo
->link_map_size
);
713 make_cleanup (xfree
, new->lm_info
->lm
);
715 read_memory (lm
, new->lm_info
->lm
, lmo
->link_map_size
);
719 /* For SVR4 versions, the first entry in the link map is for the
720 inferior executable, so we must ignore it. For some versions of
721 SVR4, it has no name. For others (Solaris 2.3 for example), it
722 does have a name, so we can no longer use a missing name to
723 decide when to ignore it. */
724 if (IGNORE_FIRST_LINK_MAP_ENTRY (new) && ldsomap
== 0)
731 /* Extract this shared object's name. */
732 target_read_string (LM_NAME (new), &buffer
,
733 SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
735 warning (_("Can't read pathname for load map: %s."),
736 safe_strerror (errcode
));
739 strncpy (new->so_name
, buffer
, SO_NAME_MAX_PATH_SIZE
- 1);
740 new->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
742 strcpy (new->so_original_name
, new->so_name
);
745 /* If this entry has no name, or its name matches the name
746 for the main executable, don't include it in the list. */
747 if (! new->so_name
[0]
748 || match_main (new->so_name
))
754 link_ptr
= &new->next
;
758 /* On Solaris, the dynamic linker is not in the normal list of
759 shared objects, so make sure we pick it up too. Having
760 symbol information for the dynamic linker is quite crucial
761 for skipping dynamic linker resolver code. */
762 if (lm
== 0 && ldsomap
== 0)
763 lm
= ldsomap
= solib_svr4_r_ldsomap ();
765 discard_cleanups (old_chain
);
771 /* Get the address of the link_map for a given OBJFILE. Loop through
772 the link maps, and return the address of the one corresponding to
773 the given objfile. Note that this function takes into account that
774 objfile can be the main executable, not just a shared library. The
775 main executable has always an empty name field in the linkmap. */
778 svr4_fetch_objfile_link_map (struct objfile
*objfile
)
782 if ((debug_base
= locate_base ()) == 0)
783 return 0; /* failed somehow... */
785 /* Position ourselves on the first link map. */
786 lm
= solib_svr4_r_map ();
789 /* Get info on the layout of the r_debug and link_map structures. */
790 struct link_map_offsets
*lmo
= svr4_fetch_link_map_offsets ();
793 struct lm_info objfile_lm_info
;
794 struct cleanup
*old_chain
;
795 CORE_ADDR name_address
;
796 gdb_byte
*l_name_buf
= xmalloc (lmo
->l_name_size
);
797 old_chain
= make_cleanup (xfree
, l_name_buf
);
799 /* Set up the buffer to contain the portion of the link_map
800 structure that gdb cares about. Note that this is not the
801 whole link_map structure. */
802 objfile_lm_info
.lm
= xzalloc (lmo
->link_map_size
);
803 make_cleanup (xfree
, objfile_lm_info
.lm
);
805 /* Read the link map into our internal structure. */
806 read_memory (lm
, objfile_lm_info
.lm
, lmo
->link_map_size
);
808 /* Read address of name from target memory to GDB. */
809 read_memory (lm
+ lmo
->l_name_offset
, l_name_buf
, lmo
->l_name_size
);
811 /* Extract this object's name. Assume that the address is
813 name_address
= extract_unsigned_integer (l_name_buf
, lmo
->l_name_size
);
814 target_read_string (name_address
, &buffer
,
815 SO_NAME_MAX_PATH_SIZE
- 1, &errcode
);
816 make_cleanup (xfree
, buffer
);
818 warning (_("Can't read pathname for load map: %s."),
819 safe_strerror (errcode
));
822 /* Is this the linkmap for the file we want? */
823 /* If the file is not a shared library and has no name,
824 we are sure it is the main executable, so we return that. */
825 if ((buffer
&& strcmp (buffer
, objfile
->name
) == 0)
826 || (!(objfile
->flags
& OBJF_SHARED
) && (strcmp (buffer
, "") == 0)))
828 do_cleanups (old_chain
);
832 /* Not the file we wanted, continue checking. Assume that the
833 address is unsigned. */
834 lm
= extract_unsigned_integer (objfile_lm_info
.lm
+ lmo
->l_next_offset
,
836 do_cleanups (old_chain
);
841 /* On some systems, the only way to recognize the link map entry for
842 the main executable file is by looking at its name. Return
843 non-zero iff SONAME matches one of the known main executable names. */
846 match_main (char *soname
)
850 for (mainp
= main_name_list
; *mainp
!= NULL
; mainp
++)
852 if (strcmp (soname
, *mainp
) == 0)
859 /* Return 1 if PC lies in the dynamic symbol resolution code of the
860 SVR4 run time loader. */
861 static CORE_ADDR interp_text_sect_low
;
862 static CORE_ADDR interp_text_sect_high
;
863 static CORE_ADDR interp_plt_sect_low
;
864 static CORE_ADDR interp_plt_sect_high
;
867 svr4_in_dynsym_resolve_code (CORE_ADDR pc
)
869 return ((pc
>= interp_text_sect_low
&& pc
< interp_text_sect_high
)
870 || (pc
>= interp_plt_sect_low
&& pc
< interp_plt_sect_high
)
871 || in_plt_section (pc
, NULL
));
874 /* Given an executable's ABFD and target, compute the entry-point
878 exec_entry_point (struct bfd
*abfd
, struct target_ops
*targ
)
880 /* KevinB wrote ... for most targets, the address returned by
881 bfd_get_start_address() is the entry point for the start
882 function. But, for some targets, bfd_get_start_address() returns
883 the address of a function descriptor from which the entry point
884 address may be extracted. This address is extracted by
885 gdbarch_convert_from_func_ptr_addr(). The method
886 gdbarch_convert_from_func_ptr_addr() is the merely the identify
887 function for targets which don't use function descriptors. */
888 return gdbarch_convert_from_func_ptr_addr (current_gdbarch
,
889 bfd_get_start_address (abfd
),
897 enable_break -- arrange for dynamic linker to hit breakpoint
901 int enable_break (void)
905 Both the SunOS and the SVR4 dynamic linkers have, as part of their
906 debugger interface, support for arranging for the inferior to hit
907 a breakpoint after mapping in the shared libraries. This function
908 enables that breakpoint.
910 For SunOS, there is a special flag location (in_debugger) which we
911 set to 1. When the dynamic linker sees this flag set, it will set
912 a breakpoint at a location known only to itself, after saving the
913 original contents of that place and the breakpoint address itself,
914 in it's own internal structures. When we resume the inferior, it
915 will eventually take a SIGTRAP when it runs into the breakpoint.
916 We handle this (in a different place) by restoring the contents of
917 the breakpointed location (which is only known after it stops),
918 chasing around to locate the shared libraries that have been
919 loaded, then resuming.
921 For SVR4, the debugger interface structure contains a member (r_brk)
922 which is statically initialized at the time the shared library is
923 built, to the offset of a function (_r_debug_state) which is guaran-
924 teed to be called once before mapping in a library, and again when
925 the mapping is complete. At the time we are examining this member,
926 it contains only the unrelocated offset of the function, so we have
927 to do our own relocation. Later, when the dynamic linker actually
928 runs, it relocates r_brk to be the actual address of _r_debug_state().
930 The debugger interface structure also contains an enumeration which
931 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
932 depending upon whether or not the library is being mapped or unmapped,
933 and then set to RT_CONSISTENT after the library is mapped/unmapped.
941 #ifdef BKPT_AT_SYMBOL
943 struct minimal_symbol
*msymbol
;
945 asection
*interp_sect
;
947 /* First, remove all the solib event breakpoints. Their addresses
948 may have changed since the last time we ran the program. */
949 remove_solib_event_breakpoints ();
951 interp_text_sect_low
= interp_text_sect_high
= 0;
952 interp_plt_sect_low
= interp_plt_sect_high
= 0;
954 /* Find the .interp section; if not found, warn the user and drop
955 into the old breakpoint at symbol code. */
956 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
959 unsigned int interp_sect_size
;
961 CORE_ADDR load_addr
= 0;
962 int load_addr_found
= 0;
965 struct target_ops
*tmp_bfd_target
;
967 char *tmp_pathname
= NULL
;
968 CORE_ADDR sym_addr
= 0;
970 /* Read the contents of the .interp section into a local buffer;
971 the contents specify the dynamic linker this program uses. */
972 interp_sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
973 buf
= alloca (interp_sect_size
);
974 bfd_get_section_contents (exec_bfd
, interp_sect
,
975 buf
, 0, interp_sect_size
);
977 /* Now we need to figure out where the dynamic linker was
978 loaded so that we can load its symbols and place a breakpoint
979 in the dynamic linker itself.
981 This address is stored on the stack. However, I've been unable
982 to find any magic formula to find it for Solaris (appears to
983 be trivial on GNU/Linux). Therefore, we have to try an alternate
984 mechanism to find the dynamic linker's base address. */
986 tmp_fd
= solib_open (buf
, &tmp_pathname
);
988 tmp_bfd
= bfd_fopen (tmp_pathname
, gnutarget
, FOPEN_RB
, tmp_fd
);
993 /* Make sure the dynamic linker's really a useful object. */
994 if (!bfd_check_format (tmp_bfd
, bfd_object
))
996 warning (_("Unable to grok dynamic linker %s as an object file"), buf
);
1001 /* Now convert the TMP_BFD into a target. That way target, as
1002 well as BFD operations can be used. Note that closing the
1003 target will also close the underlying bfd. */
1004 tmp_bfd_target
= target_bfd_reopen (tmp_bfd
);
1006 /* On a running target, we can get the dynamic linker's base
1007 address from the shared library table. */
1008 solib_add (NULL
, 0, NULL
, auto_solib_add
);
1009 so
= master_so_list ();
1012 if (strcmp (buf
, so
->so_original_name
) == 0)
1014 load_addr_found
= 1;
1015 load_addr
= LM_ADDR_CHECK (so
, tmp_bfd
);
1021 /* Otherwise we find the dynamic linker's base address by examining
1022 the current pc (which should point at the entry point for the
1023 dynamic linker) and subtracting the offset of the entry point. */
1024 if (!load_addr_found
)
1025 load_addr
= (read_pc ()
1026 - exec_entry_point (tmp_bfd
, tmp_bfd_target
));
1028 /* Record the relocated start and end address of the dynamic linker
1029 text and plt section for svr4_in_dynsym_resolve_code. */
1030 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
1033 interp_text_sect_low
=
1034 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1035 interp_text_sect_high
=
1036 interp_text_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
1038 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
1041 interp_plt_sect_low
=
1042 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
1043 interp_plt_sect_high
=
1044 interp_plt_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
1047 /* Now try to set a breakpoint in the dynamic linker. */
1048 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1050 /* On ABI's that use function descriptors, there are usually
1051 two linker symbols associated with each C function: one
1052 pointing at the actual entry point of the machine code,
1053 and one pointing at the function's descriptor. The
1054 latter symbol has the same name as the C function.
1056 What we're looking for here is the machine code entry
1057 point, so we are only interested in symbols in code
1059 sym_addr
= bfd_lookup_symbol (tmp_bfd
, *bkpt_namep
, SEC_CODE
);
1064 /* We're done with both the temporary bfd and target. Remember,
1065 closing the target closes the underlying bfd. */
1066 target_close (tmp_bfd_target
, 0);
1070 create_solib_event_breakpoint (load_addr
+ sym_addr
);
1074 /* For whatever reason we couldn't set a breakpoint in the dynamic
1075 linker. Warn and drop into the old code. */
1077 warning (_("Unable to find dynamic linker breakpoint function.\n"
1078 "GDB will be unable to debug shared library initializers\n"
1079 "and track explicitly loaded dynamic code."));
1082 /* Scan through the list of symbols, trying to look up the symbol and
1083 set a breakpoint there. Terminate loop when we/if we succeed. */
1085 breakpoint_addr
= 0;
1086 for (bkpt_namep
= bkpt_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
1088 msymbol
= lookup_minimal_symbol (*bkpt_namep
, NULL
, symfile_objfile
);
1089 if ((msymbol
!= NULL
) && (SYMBOL_VALUE_ADDRESS (msymbol
) != 0))
1091 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol
));
1096 /* Nothing good happened. */
1099 #endif /* BKPT_AT_SYMBOL */
1108 special_symbol_handling -- additional shared library symbol handling
1112 void special_symbol_handling ()
1116 Once the symbols from a shared object have been loaded in the usual
1117 way, we are called to do any system specific symbol handling that
1120 For SunOS4, this consisted of grunging around in the dynamic
1121 linkers structures to find symbol definitions for "common" symbols
1122 and adding them to the minimal symbol table for the runtime common
1125 However, for SVR4, there's nothing to do.
1130 svr4_special_symbol_handling (void)
1134 /* Relocate the main executable. This function should be called upon
1135 stopping the inferior process at the entry point to the program.
1136 The entry point from BFD is compared to the PC and if they are
1137 different, the main executable is relocated by the proper amount.
1139 As written it will only attempt to relocate executables which
1140 lack interpreter sections. It seems likely that only dynamic
1141 linker executables will get relocated, though it should work
1142 properly for a position-independent static executable as well. */
1145 svr4_relocate_main_executable (void)
1147 asection
*interp_sect
;
1148 CORE_ADDR pc
= read_pc ();
1150 /* Decide if the objfile needs to be relocated. As indicated above,
1151 we will only be here when execution is stopped at the beginning
1152 of the program. Relocation is necessary if the address at which
1153 we are presently stopped differs from the start address stored in
1154 the executable AND there's no interpreter section. The condition
1155 regarding the interpreter section is very important because if
1156 there *is* an interpreter section, execution will begin there
1157 instead. When there is an interpreter section, the start address
1158 is (presumably) used by the interpreter at some point to start
1159 execution of the program.
1161 If there is an interpreter, it is normal for it to be set to an
1162 arbitrary address at the outset. The job of finding it is
1163 handled in enable_break().
1165 So, to summarize, relocations are necessary when there is no
1166 interpreter section and the start address obtained from the
1167 executable is different from the address at which GDB is
1170 [ The astute reader will note that we also test to make sure that
1171 the executable in question has the DYNAMIC flag set. It is my
1172 opinion that this test is unnecessary (undesirable even). It
1173 was added to avoid inadvertent relocation of an executable
1174 whose e_type member in the ELF header is not ET_DYN. There may
1175 be a time in the future when it is desirable to do relocations
1176 on other types of files as well in which case this condition
1177 should either be removed or modified to accomodate the new file
1178 type. (E.g, an ET_EXEC executable which has been built to be
1179 position-independent could safely be relocated by the OS if
1180 desired. It is true that this violates the ABI, but the ABI
1181 has been known to be bent from time to time.) - Kevin, Nov 2000. ]
1184 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
1185 if (interp_sect
== NULL
1186 && (bfd_get_file_flags (exec_bfd
) & DYNAMIC
) != 0
1187 && (exec_entry_point (exec_bfd
, &exec_ops
) != pc
))
1189 struct cleanup
*old_chain
;
1190 struct section_offsets
*new_offsets
;
1192 CORE_ADDR displacement
;
1194 /* It is necessary to relocate the objfile. The amount to
1195 relocate by is simply the address at which we are stopped
1196 minus the starting address from the executable.
1198 We relocate all of the sections by the same amount. This
1199 behavior is mandated by recent editions of the System V ABI.
1200 According to the System V Application Binary Interface,
1201 Edition 4.1, page 5-5:
1203 ... Though the system chooses virtual addresses for
1204 individual processes, it maintains the segments' relative
1205 positions. Because position-independent code uses relative
1206 addressesing between segments, the difference between
1207 virtual addresses in memory must match the difference
1208 between virtual addresses in the file. The difference
1209 between the virtual address of any segment in memory and
1210 the corresponding virtual address in the file is thus a
1211 single constant value for any one executable or shared
1212 object in a given process. This difference is the base
1213 address. One use of the base address is to relocate the
1214 memory image of the program during dynamic linking.
1216 The same language also appears in Edition 4.0 of the System V
1217 ABI and is left unspecified in some of the earlier editions. */
1219 displacement
= pc
- exec_entry_point (exec_bfd
, &exec_ops
);
1222 new_offsets
= xcalloc (symfile_objfile
->num_sections
,
1223 sizeof (struct section_offsets
));
1224 old_chain
= make_cleanup (xfree
, new_offsets
);
1226 for (i
= 0; i
< symfile_objfile
->num_sections
; i
++)
1228 if (displacement
!= ANOFFSET (symfile_objfile
->section_offsets
, i
))
1230 new_offsets
->offsets
[i
] = displacement
;
1234 objfile_relocate (symfile_objfile
, new_offsets
);
1236 do_cleanups (old_chain
);
1244 svr4_solib_create_inferior_hook -- shared library startup support
1248 void svr4_solib_create_inferior_hook ()
1252 When gdb starts up the inferior, it nurses it along (through the
1253 shell) until it is ready to execute it's first instruction. At this
1254 point, this function gets called via expansion of the macro
1255 SOLIB_CREATE_INFERIOR_HOOK.
1257 For SunOS executables, this first instruction is typically the
1258 one at "_start", or a similar text label, regardless of whether
1259 the executable is statically or dynamically linked. The runtime
1260 startup code takes care of dynamically linking in any shared
1261 libraries, once gdb allows the inferior to continue.
1263 For SVR4 executables, this first instruction is either the first
1264 instruction in the dynamic linker (for dynamically linked
1265 executables) or the instruction at "start" for statically linked
1266 executables. For dynamically linked executables, the system
1267 first exec's /lib/libc.so.N, which contains the dynamic linker,
1268 and starts it running. The dynamic linker maps in any needed
1269 shared libraries, maps in the actual user executable, and then
1270 jumps to "start" in the user executable.
1272 For both SunOS shared libraries, and SVR4 shared libraries, we
1273 can arrange to cooperate with the dynamic linker to discover the
1274 names of shared libraries that are dynamically linked, and the
1275 base addresses to which they are linked.
1277 This function is responsible for discovering those names and
1278 addresses, and saving sufficient information about them to allow
1279 their symbols to be read at a later time.
1283 Between enable_break() and disable_break(), this code does not
1284 properly handle hitting breakpoints which the user might have
1285 set in the startup code or in the dynamic linker itself. Proper
1286 handling will probably have to wait until the implementation is
1287 changed to use the "breakpoint handler function" method.
1289 Also, what if child has exit()ed? Must exit loop somehow.
1293 svr4_solib_create_inferior_hook (void)
1295 /* Relocate the main executable if necessary. */
1296 svr4_relocate_main_executable ();
1298 if (!svr4_have_link_map_offsets ())
1300 warning (_("no shared library support for this OS / ABI"));
1305 if (!enable_break ())
1307 warning (_("shared library handler failed to enable breakpoint"));
1311 #if defined(_SCO_DS)
1312 /* SCO needs the loop below, other systems should be using the
1313 special shared library breakpoints and the shared library breakpoint
1316 Now run the target. It will eventually hit the breakpoint, at
1317 which point all of the libraries will have been mapped in and we
1318 can go groveling around in the dynamic linker structures to find
1319 out what we need to know about them. */
1321 clear_proceed_status ();
1322 stop_soon
= STOP_QUIETLY
;
1323 stop_signal
= TARGET_SIGNAL_0
;
1326 target_resume (pid_to_ptid (-1), 0, stop_signal
);
1327 wait_for_inferior ();
1329 while (stop_signal
!= TARGET_SIGNAL_TRAP
);
1330 stop_soon
= NO_STOP_QUIETLY
;
1331 #endif /* defined(_SCO_DS) */
1335 svr4_clear_solib (void)
1341 svr4_free_so (struct so_list
*so
)
1343 xfree (so
->lm_info
->lm
);
1344 xfree (so
->lm_info
);
1348 /* Clear any bits of ADDR that wouldn't fit in a target-format
1349 data pointer. "Data pointer" here refers to whatever sort of
1350 address the dynamic linker uses to manage its sections. At the
1351 moment, we don't support shared libraries on any processors where
1352 code and data pointers are different sizes.
1354 This isn't really the right solution. What we really need here is
1355 a way to do arithmetic on CORE_ADDR values that respects the
1356 natural pointer/address correspondence. (For example, on the MIPS,
1357 converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
1358 sign-extend the value. There, simply truncating the bits above
1359 TARGET_PTR_BIT, as we do below, is no good.) This should probably
1360 be a new gdbarch method or something. */
1362 svr4_truncate_ptr (CORE_ADDR addr
)
1364 if (TARGET_PTR_BIT
== sizeof (CORE_ADDR
) * 8)
1365 /* We don't need to truncate anything, and the bit twiddling below
1366 will fail due to overflow problems. */
1369 return addr
& (((CORE_ADDR
) 1 << TARGET_PTR_BIT
) - 1);
1374 svr4_relocate_section_addresses (struct so_list
*so
,
1375 struct section_table
*sec
)
1377 sec
->addr
= svr4_truncate_ptr (sec
->addr
+ LM_ADDR_CHECK (so
,
1379 sec
->endaddr
= svr4_truncate_ptr (sec
->endaddr
+ LM_ADDR_CHECK (so
,
1384 /* Architecture-specific operations. */
1386 /* Per-architecture data key. */
1387 static struct gdbarch_data
*solib_svr4_data
;
1389 struct solib_svr4_ops
1391 /* Return a description of the layout of `struct link_map'. */
1392 struct link_map_offsets
*(*fetch_link_map_offsets
)(void);
1395 /* Return a default for the architecture-specific operations. */
1398 solib_svr4_init (struct obstack
*obstack
)
1400 struct solib_svr4_ops
*ops
;
1402 ops
= OBSTACK_ZALLOC (obstack
, struct solib_svr4_ops
);
1403 ops
->fetch_link_map_offsets
= legacy_svr4_fetch_link_map_offsets_hook
;
1407 /* Set the architecture-specific `struct link_map_offsets' fetcher for
1411 set_solib_svr4_fetch_link_map_offsets (struct gdbarch
*gdbarch
,
1412 struct link_map_offsets
*(*flmo
) (void))
1414 struct solib_svr4_ops
*ops
= gdbarch_data (gdbarch
, solib_svr4_data
);
1416 ops
->fetch_link_map_offsets
= flmo
;
1419 /* Fetch a link_map_offsets structure using the architecture-specific
1420 `struct link_map_offsets' fetcher. */
1422 static struct link_map_offsets
*
1423 svr4_fetch_link_map_offsets (void)
1425 struct solib_svr4_ops
*ops
= gdbarch_data (current_gdbarch
, solib_svr4_data
);
1427 gdb_assert (ops
->fetch_link_map_offsets
);
1428 return ops
->fetch_link_map_offsets ();
1431 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise. */
1434 svr4_have_link_map_offsets (void)
1436 struct solib_svr4_ops
*ops
= gdbarch_data (current_gdbarch
, solib_svr4_data
);
1437 return (ops
->fetch_link_map_offsets
!= NULL
);
1441 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
1442 `struct r_debug' and a `struct link_map' that are binary compatible
1443 with the origional SVR4 implementation. */
1445 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1446 for an ILP32 SVR4 system. */
1448 struct link_map_offsets
*
1449 svr4_ilp32_fetch_link_map_offsets (void)
1451 static struct link_map_offsets lmo
;
1452 static struct link_map_offsets
*lmp
= NULL
;
1458 lmo
.r_version_offset
= 0;
1459 lmo
.r_version_size
= 4;
1460 lmo
.r_map_offset
= 4;
1461 lmo
.r_ldsomap_offset
= 20;
1463 /* Everything we need is in the first 20 bytes. */
1464 lmo
.link_map_size
= 20;
1465 lmo
.l_addr_offset
= 0;
1466 lmo
.l_addr_size
= 4;
1467 lmo
.l_name_offset
= 4;
1468 lmo
.l_name_size
= 4;
1469 lmo
.l_ld_offset
= 8;
1471 lmo
.l_next_offset
= 12;
1472 lmo
.l_next_size
= 4;
1473 lmo
.l_prev_offset
= 16;
1474 lmo
.l_prev_size
= 4;
1480 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1481 for an LP64 SVR4 system. */
1483 struct link_map_offsets
*
1484 svr4_lp64_fetch_link_map_offsets (void)
1486 static struct link_map_offsets lmo
;
1487 static struct link_map_offsets
*lmp
= NULL
;
1493 lmo
.r_version_offset
= 0;
1494 lmo
.r_version_size
= 4;
1495 lmo
.r_map_offset
= 8;
1496 lmo
.r_ldsomap_offset
= 40;
1498 /* Everything we need is in the first 40 bytes. */
1499 lmo
.link_map_size
= 40;
1500 lmo
.l_addr_offset
= 0;
1501 lmo
.l_addr_size
= 8;
1502 lmo
.l_name_offset
= 8;
1503 lmo
.l_name_size
= 8;
1504 lmo
.l_ld_offset
= 16;
1506 lmo
.l_next_offset
= 24;
1507 lmo
.l_next_size
= 8;
1508 lmo
.l_prev_offset
= 32;
1509 lmo
.l_prev_size
= 8;
1516 static struct target_so_ops svr4_so_ops
;
1518 extern initialize_file_ftype _initialize_svr4_solib
; /* -Wmissing-prototypes */
1521 _initialize_svr4_solib (void)
1523 solib_svr4_data
= gdbarch_data_register_pre_init (solib_svr4_init
);
1525 svr4_so_ops
.relocate_section_addresses
= svr4_relocate_section_addresses
;
1526 svr4_so_ops
.free_so
= svr4_free_so
;
1527 svr4_so_ops
.clear_solib
= svr4_clear_solib
;
1528 svr4_so_ops
.solib_create_inferior_hook
= svr4_solib_create_inferior_hook
;
1529 svr4_so_ops
.special_symbol_handling
= svr4_special_symbol_handling
;
1530 svr4_so_ops
.current_sos
= svr4_current_sos
;
1531 svr4_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
1532 svr4_so_ops
.in_dynsym_resolve_code
= svr4_in_dynsym_resolve_code
;
1534 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
1535 current_target_so_ops
= &svr4_so_ops
;