1 /* Handle AIX5 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
27 #include "gdb_string.h"
28 #include <sys/param.h>
30 #include <sys/procfs.h>
32 #include "elf/external.h"
42 #include "gdb_regex.h"
50 /* Link map info to include in an allocated so_list entry */
54 int nmappings
; /* number of mappings */
57 CORE_ADDR addr
; /* base address */
58 CORE_ADDR size
; /* size of mapped object */
59 CORE_ADDR offset
; /* offset into mapped object */
60 long flags
; /* MA_ protection and attribute flags */
61 CORE_ADDR gp
; /* global pointer value */
63 char *mapname
; /* name in /proc/pid/object */
64 char *pathname
; /* full pathname to object */
65 char *membername
; /* member name in archive file */
68 /* List of symbols in the dynamic linker where GDB can try to place
69 a breakpoint to monitor shared library events. */
71 static char *solib_break_names
[] =
77 static void aix5_relocate_main_executable (void);
83 bfd_lookup_symbol -- lookup the value for a specific symbol
87 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
91 An expensive way to lookup the value of a single symbol for
92 bfd's that are only temporary anyway. This is used by the
93 shared library support to find the address of the debugger
94 interface structures in the shared library.
96 Note that 0 is specifically allowed as an error return (no
101 bfd_lookup_symbol (bfd
*abfd
, char *symname
)
105 asymbol
**symbol_table
;
106 unsigned int number_of_symbols
;
108 struct cleanup
*back_to
;
109 CORE_ADDR symaddr
= 0;
111 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
113 if (storage_needed
> 0)
115 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
116 back_to
= make_cleanup (xfree
, symbol_table
);
117 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
119 for (i
= 0; i
< number_of_symbols
; i
++)
121 sym
= *symbol_table
++;
122 if (strcmp (sym
->name
, symname
) == 0)
124 /* Bfd symbols are section relative. */
125 symaddr
= sym
->value
+ sym
->section
->vma
;
129 do_cleanups (back_to
);
135 /* Look for the symbol in the dynamic string table too. */
137 storage_needed
= bfd_get_dynamic_symtab_upper_bound (abfd
);
139 if (storage_needed
> 0)
141 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
142 back_to
= make_cleanup (xfree
, symbol_table
);
143 number_of_symbols
= bfd_canonicalize_dynamic_symtab (abfd
, symbol_table
);
145 for (i
= 0; i
< number_of_symbols
; i
++)
147 sym
= *symbol_table
++;
148 if (strcmp (sym
->name
, symname
) == 0)
150 /* Bfd symbols are section relative. */
151 symaddr
= sym
->value
+ sym
->section
->vma
;
155 do_cleanups (back_to
);
162 /* Read /proc/PID/map and build a list of shared objects such that
163 the pr_mflags value AND'd with MATCH_MASK is equal to MATCH_VAL.
164 This gives us a convenient way to find all of the mappings that
165 don't belong to the main executable or vice versa. Here are
166 some of the possibilities:
168 - Fetch all mappings:
171 - Fetch all mappings except for main executable:
172 MATCH_MASK: MA_MAINEXEC
174 - Fetch only main executable:
175 MATCH_MASK: MA_MAINEXEC
176 MATCH_VAL: MA_MAINEXEC
178 A cleanup chain for the list allocations done by this function should
179 be established prior to calling build_so_list_from_mapfile(). */
181 static struct so_list
*
182 build_so_list_from_mapfile (int pid
, long match_mask
, long match_val
)
187 struct so_list
*sos
= NULL
;
190 int mapbuf_allocation_size
= 8192;
194 /* Open the map file */
196 xasprintf (&map_pathname
, "/proc/%d/map", pid
);
197 map_fd
= open (map_pathname
, O_RDONLY
);
198 xfree (map_pathname
);
202 /* Read the entire map file in */
208 mapbuf_allocation_size
*= 2;
209 lseek (map_fd
, 0, SEEK_SET
);
211 mapbuf
= xmalloc (mapbuf_allocation_size
);
212 mapbuf_size
= read (map_fd
, mapbuf
, mapbuf_allocation_size
);
216 /* FIXME: This warrants an error or a warning of some sort */
219 } while (mapbuf_size
== mapbuf_allocation_size
);
224 for (prmap
= (struct prmap
*) mapbuf
;
225 (char *) prmap
< mapbuf
+ mapbuf_size
;
228 char *mapname
, *pathname
, *membername
;
232 if (prmap
->pr_size
== 0)
235 /* Skip to the next entry if there's no path associated with the
236 map, unless we're looking for the kernel text region, in which
237 case it's okay if there's no path. */
238 if ((prmap
->pr_pathoff
== 0 || prmap
->pr_pathoff
>= mapbuf_size
)
239 && ((match_mask
& MA_KERNTEXT
) == 0))
242 /* Skip to the next entry if our match conditions don't hold. */
243 if ((prmap
->pr_mflags
& match_mask
) != match_val
)
246 mapname
= prmap
->pr_mapname
;
247 if (prmap
->pr_pathoff
== 0)
254 pathname
= mapbuf
+ prmap
->pr_pathoff
;
255 membername
= pathname
+ strlen (pathname
) + 1;
258 for (sop
= sos
; sop
!= NULL
; sop
= sop
->next
)
259 if (strcmp (pathname
, sop
->lm_info
->pathname
) == 0
260 && strcmp (membername
, sop
->lm_info
->membername
) == 0)
265 sop
= xcalloc (sizeof (struct so_list
), 1);
266 make_cleanup (xfree
, sop
);
267 sop
->lm_info
= xcalloc (sizeof (struct lm_info
), 1);
268 make_cleanup (xfree
, sop
->lm_info
);
269 sop
->lm_info
->mapname
= xstrdup (mapname
);
270 make_cleanup (xfree
, sop
->lm_info
->mapname
);
271 /* FIXME: Eliminate the pathname field once length restriction
272 is lifted on so_name and so_original_name. */
273 sop
->lm_info
->pathname
= xstrdup (pathname
);
274 make_cleanup (xfree
, sop
->lm_info
->pathname
);
275 sop
->lm_info
->membername
= xstrdup (membername
);
276 make_cleanup (xfree
, sop
->lm_info
->membername
);
278 strncpy (sop
->so_name
, pathname
, SO_NAME_MAX_PATH_SIZE
- 1);
279 sop
->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
280 strcpy (sop
->so_original_name
, sop
->so_name
);
286 mapidx
= sop
->lm_info
->nmappings
;
287 sop
->lm_info
->nmappings
+= 1;
288 sop
->lm_info
->mapping
289 = xrealloc (sop
->lm_info
->mapping
,
290 sop
->lm_info
->nmappings
* sizeof (struct lm_mapping
));
291 sop
->lm_info
->mapping
[mapidx
].addr
= (CORE_ADDR
) prmap
->pr_vaddr
;
292 sop
->lm_info
->mapping
[mapidx
].size
= prmap
->pr_size
;
293 sop
->lm_info
->mapping
[mapidx
].offset
= prmap
->pr_off
;
294 sop
->lm_info
->mapping
[mapidx
].flags
= prmap
->pr_mflags
;
295 sop
->lm_info
->mapping
[mapidx
].gp
= (CORE_ADDR
) prmap
->pr_gp
;
306 open_symbol_file_object
310 void open_symbol_file_object (void *from_tty)
314 If no open symbol file, attempt to locate and open the main symbol
317 If FROM_TTYP dereferences to a non-zero integer, allow messages to
318 be printed. This parameter is a pointer rather than an int because
319 open_symbol_file_object() is called via catch_errors() and
320 catch_errors() requires a pointer argument. */
323 open_symbol_file_object (void *from_ttyp
)
325 CORE_ADDR lm
, l_name
;
328 int from_tty
= *(int *)from_ttyp
;
329 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
332 sos
= build_so_list_from_mapfile (PIDGET (inferior_pid
),
333 MA_MAINEXEC
, MA_MAINEXEC
);
338 warning ("Could not find name of main executable in map file");
342 symbol_file_command (sos
->lm_info
->pathname
, from_tty
);
344 do_cleanups (old_chain
);
346 aix5_relocate_main_executable ();
353 aix5_current_sos -- build a list of currently loaded shared objects
357 struct so_list *aix5_current_sos ()
361 Build a list of `struct so_list' objects describing the shared
362 objects currently loaded in the inferior. This list does not
363 include an entry for the main executable file.
365 Note that we only gather information directly available from the
366 inferior --- we don't examine any of the shared library files
367 themselves. The declaration of `struct so_list' says which fields
368 we provide values for. */
370 static struct so_list
*
371 aix5_current_sos (void)
373 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
376 /* Fetch the list of mappings, excluding the main executable. */
377 sos
= build_so_list_from_mapfile (PIDGET (inferior_pid
), MA_MAINEXEC
, 0);
379 /* Reverse the list; it looks nicer when we print it if the mappings
380 are in the same order as in the map file. */
383 struct so_list
*next
= sos
->next
;
388 struct so_list
*prev
= sos
;
395 discard_cleanups (old_chain
);
400 /* Return 1 if PC lies in the dynamic symbol resolution code of the
403 static CORE_ADDR interp_text_sect_low
;
404 static CORE_ADDR interp_text_sect_high
;
405 static CORE_ADDR interp_plt_sect_low
;
406 static CORE_ADDR interp_plt_sect_high
;
409 aix5_in_dynsym_resolve_code (CORE_ADDR pc
)
411 return ((pc
>= interp_text_sect_low
&& pc
< interp_text_sect_high
)
412 || (pc
>= interp_plt_sect_low
&& pc
< interp_plt_sect_high
)
413 || in_plt_section (pc
, NULL
));
420 enable_break -- arrange for dynamic linker to hit breakpoint
424 int enable_break (void)
428 The dynamic linkers has, as part of its debugger interface, support
429 for arranging for the inferior to hit a breakpoint after mapping in
430 the shared libraries. This function enables that breakpoint.
439 struct minimal_symbol
*msymbol
;
441 asection
*interp_sect
;
443 /* First, remove all the solib event breakpoints. Their addresses
444 may have changed since the last time we ran the program. */
445 remove_solib_event_breakpoints ();
447 interp_text_sect_low
= interp_text_sect_high
= 0;
448 interp_plt_sect_low
= interp_plt_sect_high
= 0;
450 /* Find the .interp section; if not found, warn the user and drop
451 into the old breakpoint at symbol code. */
452 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
455 unsigned int interp_sect_size
;
459 CORE_ADDR sym_addr
= 0;
461 /* Read the contents of the .interp section into a local buffer;
462 the contents specify the dynamic linker this program uses. */
463 interp_sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
464 buf
= alloca (interp_sect_size
);
465 bfd_get_section_contents (exec_bfd
, interp_sect
,
466 buf
, 0, interp_sect_size
);
468 /* Now we need to figure out where the dynamic linker was
469 loaded so that we can load its symbols and place a breakpoint
470 in the dynamic linker itself.
472 This address is stored on the stack. However, I've been unable
473 to find any magic formula to find it for Solaris (appears to
474 be trivial on GNU/Linux). Therefore, we have to try an alternate
475 mechanism to find the dynamic linker's base address. */
476 tmp_bfd
= bfd_openr (buf
, gnutarget
);
480 /* Make sure the dynamic linker's really a useful object. */
481 if (!bfd_check_format (tmp_bfd
, bfd_object
))
483 warning ("Unable to grok dynamic linker %s as an object file", buf
);
488 /* We find the dynamic linker's base address by examining the
489 current pc (which point at the entry point for the dynamic
490 linker) and subtracting the offset of the entry point. */
491 load_addr
= read_pc () - tmp_bfd
->start_address
;
493 /* Record the relocated start and end address of the dynamic linker
494 text and plt section for aix5_in_dynsym_resolve_code. */
495 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".text");
498 interp_text_sect_low
=
499 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
500 interp_text_sect_high
=
501 interp_text_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
503 interp_sect
= bfd_get_section_by_name (tmp_bfd
, ".plt");
506 interp_plt_sect_low
=
507 bfd_section_vma (tmp_bfd
, interp_sect
) + load_addr
;
508 interp_plt_sect_high
=
509 interp_plt_sect_low
+ bfd_section_size (tmp_bfd
, interp_sect
);
512 /* Now try to set a breakpoint in the dynamic linker. */
513 for (bkpt_namep
= solib_break_names
; *bkpt_namep
!= NULL
; bkpt_namep
++)
515 sym_addr
= bfd_lookup_symbol (tmp_bfd
, *bkpt_namep
);
520 /* We're done with the temporary bfd. */
525 create_solib_event_breakpoint (load_addr
+ sym_addr
);
529 /* For whatever reason we couldn't set a breakpoint in the dynamic
530 linker. Warn and drop into the old code. */
532 warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
535 /* Nothing good happened. */
545 special_symbol_handling -- additional shared library symbol handling
549 void special_symbol_handling ()
553 Once the symbols from a shared object have been loaded in the usual
554 way, we are called to do any system specific symbol handling that
560 aix5_special_symbol_handling (void)
562 /* Nothing needed (yet) for AIX5. */
565 #define SECTMAPMASK (~ (CORE_ADDR) 0x03ffffff)
568 aix5_relocate_main_executable (void)
571 struct section_offsets
*new_offsets
;
574 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
576 /* Fetch the mappings for the main executable from the map file. */
577 so
= build_so_list_from_mapfile (PIDGET (inferior_pid
),
578 MA_MAINEXEC
, MA_MAINEXEC
);
580 /* Make sure we actually have some mappings to work with. */
583 warning ("Could not find main executable in map file");
584 do_cleanups (old_chain
);
588 /* Allocate the data structure which'll contain the new offsets to
589 relocate by. Initialize it so it contains the current offsets. */
590 new_offsets
= xcalloc (sizeof (struct section_offsets
),
591 symfile_objfile
->num_sections
);
592 make_cleanup (xfree
, new_offsets
);
593 for (i
= 0; i
< symfile_objfile
->num_sections
; i
++)
594 new_offsets
->offsets
[i
] = ANOFFSET (symfile_objfile
->section_offsets
, i
);
596 /* Iterate over the mappings in the main executable and compute
597 the new offset value as appropriate. */
598 for (i
= 0; i
< so
->lm_info
->nmappings
; i
++)
600 CORE_ADDR increment
= 0;
601 struct obj_section
*sect
;
602 bfd
*obfd
= symfile_objfile
->obfd
;
603 struct lm_mapping
*mapping
= &so
->lm_info
->mapping
[i
];
605 ALL_OBJFILE_OSECTIONS (symfile_objfile
, sect
)
607 int flags
= bfd_get_section_flags (obfd
, sect
->the_bfd_section
);
608 if (flags
& SEC_ALLOC
)
610 file_ptr filepos
= sect
->the_bfd_section
->filepos
;
611 if (mapping
->offset
<= filepos
612 && filepos
<= mapping
->offset
+ mapping
->size
)
614 int idx
= sect
->the_bfd_section
->index
;
617 increment
= mapping
->addr
618 - (bfd_section_vma (obfd
, sect
->the_bfd_section
)
621 if (increment
!= ANOFFSET (new_offsets
, idx
))
623 new_offsets
->offsets
[idx
] = increment
;
631 /* If any of the offsets have changed, then relocate the objfile. */
633 objfile_relocate (symfile_objfile
, new_offsets
);
635 /* Free up all the space we've allocated. */
636 do_cleanups (old_chain
);
643 aix5_solib_create_inferior_hook -- shared library startup support
647 void aix5_solib_create_inferior_hook()
651 When gdb starts up the inferior, it nurses it along (through the
652 shell) until it is ready to execute it's first instruction. At this
653 point, this function gets called via expansion of the macro
654 SOLIB_CREATE_INFERIOR_HOOK.
656 For AIX5 executables, this first instruction is the first
657 instruction in the dynamic linker (for dynamically linked
658 executables) or the instruction at "start" for statically linked
659 executables. For dynamically linked executables, the system
660 first exec's libc.so.N, which contains the dynamic linker,
661 and starts it running. The dynamic linker maps in any needed
662 shared libraries, maps in the actual user executable, and then
663 jumps to "start" in the user executable.
668 aix5_solib_create_inferior_hook (void)
670 aix5_relocate_main_executable ();
672 if (!enable_break ())
674 warning ("shared library handler failed to enable breakpoint");
680 aix5_clear_solib (void)
685 aix5_free_so (struct so_list
*so
)
687 xfree (so
->lm_info
->mapname
);
688 xfree (so
->lm_info
->pathname
);
689 xfree (so
->lm_info
->membername
);
694 aix5_relocate_section_addresses (struct so_list
*so
,
695 struct section_table
*sec
)
697 int flags
= bfd_get_section_flags (sec
->bfd
, sec
->the_bfd_section
);
698 file_ptr filepos
= sec
->the_bfd_section
->filepos
;
700 if (flags
& SEC_ALLOC
)
705 for (idx
= 0; idx
< so
->lm_info
->nmappings
; idx
++)
707 struct lm_mapping
*mapping
= &so
->lm_info
->mapping
[idx
];
708 if (mapping
->offset
<= filepos
709 && filepos
<= mapping
->offset
+ mapping
->size
)
713 if (idx
>= so
->lm_info
->nmappings
)
714 internal_error (__FILE__
, __LINE__
,
715 "aix_relocate_section_addresses: Can't find mapping for section %s",
716 bfd_get_section_name (sec
->bfd
, sec
->the_bfd_section
));
718 addr
= so
->lm_info
->mapping
[idx
].addr
;
721 sec
->endaddr
+= addr
;
725 /* Find the global pointer for the given function address ADDR. */
728 aix5_find_global_pointer (CORE_ADDR addr
)
730 struct so_list
*sos
, *so
;
731 CORE_ADDR global_pointer
= 0;
732 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
734 sos
= build_so_list_from_mapfile (PIDGET (inferior_pid
), 0, 0);
736 for (so
= sos
; so
!= NULL
; so
= so
->next
)
739 for (idx
= 0; idx
< so
->lm_info
->nmappings
; idx
++)
740 if (so
->lm_info
->mapping
[idx
].addr
<= addr
741 && addr
<= so
->lm_info
->mapping
[idx
].addr
742 + so
->lm_info
->mapping
[idx
].size
)
747 if (idx
< so
->lm_info
->nmappings
)
749 /* Look for a non-zero global pointer in the current set of
751 for (idx
= 0; idx
< so
->lm_info
->nmappings
; idx
++)
752 if (so
->lm_info
->mapping
[idx
].gp
!= 0)
754 global_pointer
= so
->lm_info
->mapping
[idx
].gp
;
757 /* Get out regardless of whether we found one or not. Mappings
758 don't overlap, so it would be pointless to continue. */
763 do_cleanups (old_chain
);
765 return global_pointer
;
768 /* Find the execute-only kernel region known as the gate page. This
769 page is where the signal trampoline lives. It may be found by
770 querying the map file and looking for the MA_KERNTEXT flag. */
772 aix5_find_gate_addresses (CORE_ADDR
*start
, CORE_ADDR
*end
)
775 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, 0);
777 /* Fetch the mappings for the main executable from the map file. */
778 so
= build_so_list_from_mapfile (PIDGET (inferior_pid
),
779 MA_KERNTEXT
, MA_KERNTEXT
);
781 /* Make sure we actually have some mappings to work with. */
784 warning ("Could not find gate page in map file");
787 do_cleanups (old_chain
);
791 /* There should only be on kernel mapping for the gate page and
792 it'll be in the read-only (even though it's execute-only)
793 mapping in the lm_info struct. */
795 *start
= so
->lm_info
->mapping
[0].addr
;
796 *end
= *start
+ so
->lm_info
->mapping
[0].size
;
798 /* Free up all the space we've allocated. */
799 do_cleanups (old_chain
);
802 /* From ia64-tdep.c. FIXME: If we end up using this for rs6000 too,
803 we'll need to make the names match. */
804 extern CORE_ADDR (*native_find_global_pointer
) (CORE_ADDR
);
806 /* From ia64-aix-tdep.c. Hook for finding the starting and
807 ending gate page addresses. The only reason that this hook
808 is in this file is because this is where the map file reading
810 extern void (*aix5_find_gate_addresses_hook
) (CORE_ADDR
*, CORE_ADDR
*);
812 static struct target_so_ops aix5_so_ops
;
815 _initialize_aix5_solib (void)
817 aix5_so_ops
.relocate_section_addresses
= aix5_relocate_section_addresses
;
818 aix5_so_ops
.free_so
= aix5_free_so
;
819 aix5_so_ops
.clear_solib
= aix5_clear_solib
;
820 aix5_so_ops
.solib_create_inferior_hook
= aix5_solib_create_inferior_hook
;
821 aix5_so_ops
.special_symbol_handling
= aix5_special_symbol_handling
;
822 aix5_so_ops
.current_sos
= aix5_current_sos
;
823 aix5_so_ops
.open_symbol_file_object
= open_symbol_file_object
;
824 aix5_so_ops
.in_dynsym_resolve_code
= aix5_in_dynsym_resolve_code
;
826 native_find_global_pointer
= aix5_find_global_pointer
;
827 aix5_find_gate_addresses_hook
= aix5_find_gate_addresses
;
829 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
830 current_target_so_ops
= &aix5_so_ops
;