* gas/cgen feature
[binutils-gdb.git] / gdb / solib-svr4.c
blob603c69c490865c9f8f35a6700bb124992ad9895b
1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 98, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #define _SYSCALL32 /* for Sparc64 cross Sparc32 */
23 #include "defs.h"
26 #include <sys/types.h>
27 #include <signal.h>
28 #include "gdb_string.h"
29 #include <sys/param.h>
30 #include <fcntl.h>
32 #ifndef SVR4_SHARED_LIBS
33 /* SunOS shared libs need the nlist structure. */
34 #include <a.out.h>
35 #else
36 #include "elf/external.h"
37 #endif
39 #ifdef HAVE_LINK_H
40 #include <link.h>
41 #endif
43 #include "symtab.h"
44 #include "bfd.h"
45 #include "symfile.h"
46 #include "objfiles.h"
47 #include "gdbcore.h"
48 #include "command.h"
49 #include "target.h"
50 #include "frame.h"
51 #include "gdb_regex.h"
52 #include "inferior.h"
53 #include "environ.h"
54 #include "language.h"
55 #include "gdbcmd.h"
57 #include "solist.h"
58 #include "solib-svr4.h"
60 /* Link map info to include in an allocated so_list entry */
62 struct lm_info
64 /* Pointer to copy of link map from inferior. The type is char *
65 rather than void *, so that we may use byte offsets to find the
66 various fields without the need for a cast. */
67 char *lm;
70 /* On SVR4 systems, a list of symbols in the dynamic linker where
71 GDB can try to place a breakpoint to monitor shared library
72 events.
74 If none of these symbols are found, or other errors occur, then
75 SVR4 systems will fall back to using a symbol as the "startup
76 mapping complete" breakpoint address. */
78 #ifdef SVR4_SHARED_LIBS
79 static char *solib_break_names[] =
81 "r_debug_state",
82 "_r_debug_state",
83 "_dl_debug_state",
84 "rtld_db_dlactivity",
85 "_rtld_debug_state",
86 NULL
88 #endif
90 #define BKPT_AT_SYMBOL 1
92 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
93 static char *bkpt_names[] =
95 #ifdef SOLIB_BKPT_NAME
96 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
97 #endif
98 "_start",
99 "main",
100 NULL
102 #endif
104 /* Symbols which are used to locate the base of the link map structures. */
106 #ifndef SVR4_SHARED_LIBS
107 static char *debug_base_symbols[] =
109 "_DYNAMIC",
110 "_DYNAMIC__MGC",
111 NULL
113 #endif
115 static char *main_name_list[] =
117 "main_$main",
118 NULL
122 /* Fetch (and possibly build) an appropriate link_map_offsets structure
123 for native targets using struct definitions from link.h. */
125 struct link_map_offsets *
126 default_svr4_fetch_link_map_offsets (void)
128 #ifdef HAVE_LINK_H
129 static struct link_map_offsets lmo;
130 static struct link_map_offsets *lmp = 0;
131 #if defined (HAVE_STRUCT_LINK_MAP32)
132 static struct link_map_offsets lmo32;
133 static struct link_map_offsets *lmp32 = 0;
134 #endif
136 #ifndef offsetof
137 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
138 #endif
139 #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER))
141 if (lmp == 0)
143 lmp = &lmo;
145 #ifdef SVR4_SHARED_LIBS
146 lmo.r_debug_size = sizeof (struct r_debug);
148 lmo.r_map_offset = offsetof (struct r_debug, r_map);
149 lmo.r_map_size = fieldsize (struct r_debug, r_map);
151 lmo.link_map_size = sizeof (struct link_map);
153 lmo.l_addr_offset = offsetof (struct link_map, l_addr);
154 lmo.l_addr_size = fieldsize (struct link_map, l_addr);
156 lmo.l_next_offset = offsetof (struct link_map, l_next);
157 lmo.l_next_size = fieldsize (struct link_map, l_next);
159 lmo.l_prev_offset = offsetof (struct link_map, l_prev);
160 lmo.l_prev_size = fieldsize (struct link_map, l_prev);
162 lmo.l_name_offset = offsetof (struct link_map, l_name);
163 lmo.l_name_size = fieldsize (struct link_map, l_name);
164 #else /* !SVR4_SHARED_LIBS */
165 lmo.link_map_size = sizeof (struct link_map);
167 lmo.l_addr_offset = offsetof (struct link_map, lm_addr);
168 lmo.l_addr_size = fieldsize (struct link_map, lm_addr);
170 lmo.l_next_offset = offsetof (struct link_map, lm_next);
171 lmo.l_next_size = fieldsize (struct link_map, lm_next);
173 lmo.l_name_offset = offsetof (struct link_map, lm_name);
174 lmo.l_name_size = fieldsize (struct link_map, lm_name);
175 #endif /* SVR4_SHARED_LIBS */
178 #if defined (HAVE_STRUCT_LINK_MAP32)
179 if (lmp32 == 0)
181 lmp32 = &lmo32;
183 lmo32.r_debug_size = sizeof (struct r_debug32);
185 lmo32.r_map_offset = offsetof (struct r_debug32, r_map);
186 lmo32.r_map_size = fieldsize (struct r_debug32, r_map);
188 lmo32.link_map_size = sizeof (struct link_map32);
190 lmo32.l_addr_offset = offsetof (struct link_map32, l_addr);
191 lmo32.l_addr_size = fieldsize (struct link_map32, l_addr);
193 lmo32.l_next_offset = offsetof (struct link_map32, l_next);
194 lmo32.l_next_size = fieldsize (struct link_map32, l_next);
196 lmo32.l_prev_offset = offsetof (struct link_map32, l_prev);
197 lmo32.l_prev_size = fieldsize (struct link_map32, l_prev);
199 lmo32.l_name_offset = offsetof (struct link_map32, l_name);
200 lmo32.l_name_size = fieldsize (struct link_map32, l_name);
202 #endif /* defined (HAVE_STRUCT_LINK_MAP32) */
204 #if defined (HAVE_STRUCT_LINK_MAP32)
205 if (bfd_get_arch_size (exec_bfd) == 32)
206 return lmp32;
207 else
208 #endif
209 return lmp;
211 #else
213 internal_error ("default_svr4_fetch_link_map_offsets called without HAVE_LINK_H defined.");
214 return 0;
216 #endif /* HAVE_LINK_H */
219 /* Macro to extract an address from a solib structure.
220 When GDB is configured for some 32-bit targets (e.g. Solaris 2.7
221 sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is
222 64 bits. We have to extract only the significant bits of addresses
223 to get the right address when accessing the core file BFD. */
225 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
226 extract_address (&(MEMBER), sizeof (MEMBER))
228 /* local data declarations */
230 #ifndef SVR4_SHARED_LIBS
232 /* NOTE: converted the macros LM_ADDR, LM_NEXT, LM_NAME and
233 IGNORE_FIRST_LINK_MAP_ENTRY into functions (see below).
234 MVS, June 2000 */
236 static struct link_dynamic dynamic_copy;
237 static struct link_dynamic_2 ld_2_copy;
238 static struct ld_debug debug_copy;
239 static CORE_ADDR debug_addr;
240 static CORE_ADDR flag_addr;
242 #endif /* !SVR4_SHARED_LIBS */
244 /* link map access functions */
246 static CORE_ADDR
247 LM_ADDR (struct so_list *so)
249 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
251 return extract_address (so->lm_info->lm + lmo->l_addr_offset, lmo->l_addr_size);
254 static CORE_ADDR
255 LM_NEXT (struct so_list *so)
257 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
259 return extract_address (so->lm_info->lm + lmo->l_next_offset, lmo->l_next_size);
262 static CORE_ADDR
263 LM_NAME (struct so_list *so)
265 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
267 return extract_address (so->lm_info->lm + lmo->l_name_offset, lmo->l_name_size);
270 #ifndef SVR4_SHARED_LIBS
272 static int
273 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
275 return 0;
278 #else /* SVR4_SHARED_LIBS */
280 static int
281 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
283 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
285 return extract_address (so->lm_info->lm + lmo->l_prev_offset,
286 lmo->l_prev_size) == 0;
289 #endif /* !SVR4_SHARED_LIBS */
291 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
292 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
294 /* Local function prototypes */
296 static int match_main (char *);
298 #ifndef SVR4_SHARED_LIBS
300 /* Allocate the runtime common object file. */
302 static void
303 allocate_rt_common_objfile (void)
305 struct objfile *objfile;
306 struct objfile *last_one;
308 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
309 memset (objfile, 0, sizeof (struct objfile));
310 objfile->md = NULL;
311 obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0,
312 xmalloc, xfree);
313 obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc,
314 xfree);
315 obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc,
316 xfree);
317 obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc,
318 xfree);
319 objfile->name = mstrsave (objfile->md, "rt_common");
321 /* Add this file onto the tail of the linked list of other such files. */
323 objfile->next = NULL;
324 if (object_files == NULL)
325 object_files = objfile;
326 else
328 for (last_one = object_files;
329 last_one->next;
330 last_one = last_one->next);
331 last_one->next = objfile;
334 rt_common_objfile = objfile;
337 /* Read all dynamically loaded common symbol definitions from the inferior
338 and put them into the minimal symbol table for the runtime common
339 objfile. */
341 static void
342 solib_add_common_symbols (CORE_ADDR rtc_symp)
344 struct rtc_symb inferior_rtc_symb;
345 struct nlist inferior_rtc_nlist;
346 int len;
347 char *name;
349 /* Remove any runtime common symbols from previous runs. */
351 if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count)
353 obstack_free (&rt_common_objfile->symbol_obstack, 0);
354 obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0,
355 xmalloc, xfree);
356 rt_common_objfile->minimal_symbol_count = 0;
357 rt_common_objfile->msymbols = NULL;
360 init_minimal_symbol_collection ();
361 make_cleanup_discard_minimal_symbols ();
363 while (rtc_symp)
365 read_memory (rtc_symp,
366 (char *) &inferior_rtc_symb,
367 sizeof (inferior_rtc_symb));
368 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp),
369 (char *) &inferior_rtc_nlist,
370 sizeof (inferior_rtc_nlist));
371 if (inferior_rtc_nlist.n_type == N_COMM)
373 /* FIXME: The length of the symbol name is not available, but in the
374 current implementation the common symbol is allocated immediately
375 behind the name of the symbol. */
376 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
378 name = xmalloc (len);
379 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name),
380 name, len);
382 /* Allocate the runtime common objfile if necessary. */
383 if (rt_common_objfile == NULL)
384 allocate_rt_common_objfile ();
386 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
387 mst_bss, rt_common_objfile);
388 xfree (name);
390 rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next);
393 /* Install any minimal symbols that have been collected as the current
394 minimal symbols for the runtime common objfile. */
396 install_minimal_symbols (rt_common_objfile);
399 #endif /* SVR4_SHARED_LIBS */
402 #ifdef SVR4_SHARED_LIBS
404 static CORE_ADDR bfd_lookup_symbol (bfd *, char *);
408 LOCAL FUNCTION
410 bfd_lookup_symbol -- lookup the value for a specific symbol
412 SYNOPSIS
414 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
416 DESCRIPTION
418 An expensive way to lookup the value of a single symbol for
419 bfd's that are only temporary anyway. This is used by the
420 shared library support to find the address of the debugger
421 interface structures in the shared library.
423 Note that 0 is specifically allowed as an error return (no
424 such symbol).
427 static CORE_ADDR
428 bfd_lookup_symbol (bfd *abfd, char *symname)
430 unsigned int storage_needed;
431 asymbol *sym;
432 asymbol **symbol_table;
433 unsigned int number_of_symbols;
434 unsigned int i;
435 struct cleanup *back_to;
436 CORE_ADDR symaddr = 0;
438 storage_needed = bfd_get_symtab_upper_bound (abfd);
440 if (storage_needed > 0)
442 symbol_table = (asymbol **) xmalloc (storage_needed);
443 back_to = make_cleanup (xfree, (PTR) symbol_table);
444 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
446 for (i = 0; i < number_of_symbols; i++)
448 sym = *symbol_table++;
449 if (STREQ (sym->name, symname))
451 /* Bfd symbols are section relative. */
452 symaddr = sym->value + sym->section->vma;
453 break;
456 do_cleanups (back_to);
459 if (symaddr)
460 return symaddr;
462 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
463 have to check the dynamic string table too. */
465 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
467 if (storage_needed > 0)
469 symbol_table = (asymbol **) xmalloc (storage_needed);
470 back_to = make_cleanup (xfree, (PTR) symbol_table);
471 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
473 for (i = 0; i < number_of_symbols; i++)
475 sym = *symbol_table++;
476 if (STREQ (sym->name, symname))
478 /* Bfd symbols are section relative. */
479 symaddr = sym->value + sym->section->vma;
480 break;
483 do_cleanups (back_to);
486 return symaddr;
489 #ifdef HANDLE_SVR4_EXEC_EMULATORS
492 Solaris BCP (the part of Solaris which allows it to run SunOS4
493 a.out files) throws in another wrinkle. Solaris does not fill
494 in the usual a.out link map structures when running BCP programs,
495 the only way to get at them is via groping around in the dynamic
496 linker.
497 The dynamic linker and it's structures are located in the shared
498 C library, which gets run as the executable's "interpreter" by
499 the kernel.
501 Note that we can assume nothing about the process state at the time
502 we need to find these structures. We may be stopped on the first
503 instruction of the interpreter (C shared library), the first
504 instruction of the executable itself, or somewhere else entirely
505 (if we attached to the process for example).
508 static char *debug_base_symbols[] =
510 "r_debug", /* Solaris 2.3 */
511 "_r_debug", /* Solaris 2.1, 2.2 */
512 NULL
515 static int look_for_base (int, CORE_ADDR);
519 LOCAL FUNCTION
521 look_for_base -- examine file for each mapped address segment
523 SYNOPSYS
525 static int look_for_base (int fd, CORE_ADDR baseaddr)
527 DESCRIPTION
529 This function is passed to proc_iterate_over_mappings, which
530 causes it to get called once for each mapped address space, with
531 an open file descriptor for the file mapped to that space, and the
532 base address of that mapped space.
534 Our job is to find the debug base symbol in the file that this
535 fd is open on, if it exists, and if so, initialize the dynamic
536 linker structure base address debug_base.
538 Note that this is a computationally expensive proposition, since
539 we basically have to open a bfd on every call, so we specifically
540 avoid opening the exec file.
543 static int
544 look_for_base (int fd, CORE_ADDR baseaddr)
546 bfd *interp_bfd;
547 CORE_ADDR address = 0;
548 char **symbolp;
550 /* If the fd is -1, then there is no file that corresponds to this
551 mapped memory segment, so skip it. Also, if the fd corresponds
552 to the exec file, skip it as well. */
554 if (fd == -1
555 || (exec_bfd != NULL
556 && fdmatch (fileno ((FILE *) (exec_bfd->iostream)), fd)))
558 return (0);
561 /* Try to open whatever random file this fd corresponds to. Note that
562 we have no way currently to find the filename. Don't gripe about
563 any problems we might have, just fail. */
565 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
567 return (0);
569 if (!bfd_check_format (interp_bfd, bfd_object))
571 /* FIXME-leak: on failure, might not free all memory associated with
572 interp_bfd. */
573 bfd_close (interp_bfd);
574 return (0);
577 /* Now try to find our debug base symbol in this file, which we at
578 least know to be a valid ELF executable or shared library. */
580 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
582 address = bfd_lookup_symbol (interp_bfd, *symbolp);
583 if (address != 0)
585 break;
588 if (address == 0)
590 /* FIXME-leak: on failure, might not free all memory associated with
591 interp_bfd. */
592 bfd_close (interp_bfd);
593 return (0);
596 /* Eureka! We found the symbol. But now we may need to relocate it
597 by the base address. If the symbol's value is less than the base
598 address of the shared library, then it hasn't yet been relocated
599 by the dynamic linker, and we have to do it ourself. FIXME: Note
600 that we make the assumption that the first segment that corresponds
601 to the shared library has the base address to which the library
602 was relocated. */
604 if (address < baseaddr)
606 address += baseaddr;
608 debug_base = address;
609 /* FIXME-leak: on failure, might not free all memory associated with
610 interp_bfd. */
611 bfd_close (interp_bfd);
612 return (1);
614 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
618 LOCAL FUNCTION
620 elf_locate_base -- locate the base address of dynamic linker structs
621 for SVR4 elf targets.
623 SYNOPSIS
625 CORE_ADDR elf_locate_base (void)
627 DESCRIPTION
629 For SVR4 elf targets the address of the dynamic linker's runtime
630 structure is contained within the dynamic info section in the
631 executable file. The dynamic section is also mapped into the
632 inferior address space. Because the runtime loader fills in the
633 real address before starting the inferior, we have to read in the
634 dynamic info section from the inferior address space.
635 If there are any errors while trying to find the address, we
636 silently return 0, otherwise the found address is returned.
640 static CORE_ADDR
641 elf_locate_base (void)
643 sec_ptr dyninfo_sect;
644 int dyninfo_sect_size;
645 CORE_ADDR dyninfo_addr;
646 char *buf;
647 char *bufend;
648 int arch_size;
650 /* Find the start address of the .dynamic section. */
651 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
652 if (dyninfo_sect == NULL)
653 return 0;
654 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
656 /* Read in .dynamic section, silently ignore errors. */
657 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
658 buf = alloca (dyninfo_sect_size);
659 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
660 return 0;
662 /* Find the DT_DEBUG entry in the the .dynamic section.
663 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
664 no DT_DEBUG entries. */
666 arch_size = bfd_get_arch_size (exec_bfd);
667 if (arch_size == -1) /* failure */
668 return 0;
670 if (arch_size == 32)
671 { /* 32-bit elf */
672 for (bufend = buf + dyninfo_sect_size;
673 buf < bufend;
674 buf += sizeof (Elf32_External_Dyn))
676 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *) buf;
677 long dyn_tag;
678 CORE_ADDR dyn_ptr;
680 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
681 if (dyn_tag == DT_NULL)
682 break;
683 else if (dyn_tag == DT_DEBUG)
685 dyn_ptr = bfd_h_get_32 (exec_bfd,
686 (bfd_byte *) x_dynp->d_un.d_ptr);
687 return dyn_ptr;
689 #ifdef DT_MIPS_RLD_MAP
690 else if (dyn_tag == DT_MIPS_RLD_MAP)
692 char *pbuf;
694 pbuf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
695 /* DT_MIPS_RLD_MAP contains a pointer to the address
696 of the dynamic link structure. */
697 dyn_ptr = bfd_h_get_32 (exec_bfd,
698 (bfd_byte *) x_dynp->d_un.d_ptr);
699 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
700 return 0;
701 return extract_unsigned_integer (pbuf, sizeof (pbuf));
703 #endif
706 else /* 64-bit elf */
708 for (bufend = buf + dyninfo_sect_size;
709 buf < bufend;
710 buf += sizeof (Elf64_External_Dyn))
712 Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *) buf;
713 long dyn_tag;
714 CORE_ADDR dyn_ptr;
716 dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
717 if (dyn_tag == DT_NULL)
718 break;
719 else if (dyn_tag == DT_DEBUG)
721 dyn_ptr = bfd_h_get_64 (exec_bfd,
722 (bfd_byte *) x_dynp->d_un.d_ptr);
723 return dyn_ptr;
728 /* DT_DEBUG entry not found. */
729 return 0;
732 #endif /* SVR4_SHARED_LIBS */
736 LOCAL FUNCTION
738 locate_base -- locate the base address of dynamic linker structs
740 SYNOPSIS
742 CORE_ADDR locate_base (void)
744 DESCRIPTION
746 For both the SunOS and SVR4 shared library implementations, if the
747 inferior executable has been linked dynamically, there is a single
748 address somewhere in the inferior's data space which is the key to
749 locating all of the dynamic linker's runtime structures. This
750 address is the value of the debug base symbol. The job of this
751 function is to find and return that address, or to return 0 if there
752 is no such address (the executable is statically linked for example).
754 For SunOS, the job is almost trivial, since the dynamic linker and
755 all of it's structures are statically linked to the executable at
756 link time. Thus the symbol for the address we are looking for has
757 already been added to the minimal symbol table for the executable's
758 objfile at the time the symbol file's symbols were read, and all we
759 have to do is look it up there. Note that we explicitly do NOT want
760 to find the copies in the shared library.
762 The SVR4 version is a bit more complicated because the address
763 is contained somewhere in the dynamic info section. We have to go
764 to a lot more work to discover the address of the debug base symbol.
765 Because of this complexity, we cache the value we find and return that
766 value on subsequent invocations. Note there is no copy in the
767 executable symbol tables.
771 static CORE_ADDR
772 locate_base (void)
775 #ifndef SVR4_SHARED_LIBS
777 struct minimal_symbol *msymbol;
778 CORE_ADDR address = 0;
779 char **symbolp;
781 /* For SunOS, we want to limit the search for the debug base symbol to the
782 executable being debugged, since there is a duplicate named symbol in the
783 shared library. We don't want the shared library versions. */
785 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
787 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
788 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
790 address = SYMBOL_VALUE_ADDRESS (msymbol);
791 return (address);
794 return (0);
796 #else /* SVR4_SHARED_LIBS */
798 /* Check to see if we have a currently valid address, and if so, avoid
799 doing all this work again and just return the cached address. If
800 we have no cached address, try to locate it in the dynamic info
801 section for ELF executables. */
803 if (debug_base == 0)
805 if (exec_bfd != NULL
806 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
807 debug_base = elf_locate_base ();
808 #ifdef HANDLE_SVR4_EXEC_EMULATORS
809 /* Try it the hard way for emulated executables. */
810 else if (inferior_pid != 0 && target_has_execution)
811 proc_iterate_over_mappings (look_for_base);
812 #endif
814 return (debug_base);
816 #endif /* !SVR4_SHARED_LIBS */
822 LOCAL FUNCTION
824 first_link_map_member -- locate first member in dynamic linker's map
826 SYNOPSIS
828 static CORE_ADDR first_link_map_member (void)
830 DESCRIPTION
832 Find the first element in the inferior's dynamic link map, and
833 return its address in the inferior. This function doesn't copy the
834 link map entry itself into our address space; current_sos actually
835 does the reading. */
837 static CORE_ADDR
838 first_link_map_member (void)
840 CORE_ADDR lm = 0;
842 #ifndef SVR4_SHARED_LIBS
844 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
845 if (dynamic_copy.ld_version >= 2)
847 /* It is a version that we can deal with, so read in the secondary
848 structure and find the address of the link map list from it. */
849 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2),
850 (char *) &ld_2_copy, sizeof (struct link_dynamic_2));
851 lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded);
854 #else /* SVR4_SHARED_LIBS */
855 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
856 char *r_map_buf = xmalloc (lmo->r_map_size);
857 struct cleanup *cleanups = make_cleanup (xfree, r_map_buf);
859 read_memory (debug_base + lmo->r_map_offset, r_map_buf, lmo->r_map_size);
861 lm = extract_address (r_map_buf, lmo->r_map_size);
863 /* FIXME: Perhaps we should validate the info somehow, perhaps by
864 checking r_version for a known version number, or r_state for
865 RT_CONSISTENT. */
867 do_cleanups (cleanups);
869 #endif /* !SVR4_SHARED_LIBS */
871 return (lm);
874 #ifdef SVR4_SHARED_LIBS
877 LOCAL FUNCTION
879 open_symbol_file_object
881 SYNOPSIS
883 void open_symbol_file_object (void *from_tty)
885 DESCRIPTION
887 If no open symbol file, attempt to locate and open the main symbol
888 file. On SVR4 systems, this is the first link map entry. If its
889 name is here, we can open it. Useful when attaching to a process
890 without first loading its symbol file.
892 If FROM_TTYP dereferences to a non-zero integer, allow messages to
893 be printed. This parameter is a pointer rather than an int because
894 open_symbol_file_object() is called via catch_errors() and
895 catch_errors() requires a pointer argument. */
897 static int
898 open_symbol_file_object (void *from_ttyp)
900 CORE_ADDR lm, l_name;
901 char *filename;
902 int errcode;
903 int from_tty = *(int *)from_ttyp;
904 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
905 char *l_name_buf = xmalloc (lmo->l_name_size);
906 struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
908 if (symfile_objfile)
909 if (!query ("Attempt to reload symbols from process? "))
910 return 0;
912 if ((debug_base = locate_base ()) == 0)
913 return 0; /* failed somehow... */
915 /* First link map member should be the executable. */
916 if ((lm = first_link_map_member ()) == 0)
917 return 0; /* failed somehow... */
919 /* Read address of name from target memory to GDB. */
920 read_memory (lm + lmo->l_name_offset, l_name_buf, lmo->l_name_size);
922 /* Convert the address to host format. */
923 l_name = extract_address (l_name_buf, lmo->l_name_size);
925 /* Free l_name_buf. */
926 do_cleanups (cleanups);
928 if (l_name == 0)
929 return 0; /* No filename. */
931 /* Now fetch the filename from target memory. */
932 target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
934 if (errcode)
936 warning ("failed to read exec filename from attached file: %s",
937 safe_strerror (errcode));
938 return 0;
941 make_cleanup (xfree, filename);
942 /* Have a pathname: read the symbol file. */
943 symbol_file_command (filename, from_tty);
945 return 1;
947 #else
949 static int
950 open_symbol_file_object (int *from_ttyp)
952 return 1;
955 #endif /* SVR4_SHARED_LIBS */
958 /* LOCAL FUNCTION
960 current_sos -- build a list of currently loaded shared objects
962 SYNOPSIS
964 struct so_list *current_sos ()
966 DESCRIPTION
968 Build a list of `struct so_list' objects describing the shared
969 objects currently loaded in the inferior. This list does not
970 include an entry for the main executable file.
972 Note that we only gather information directly available from the
973 inferior --- we don't examine any of the shared library files
974 themselves. The declaration of `struct so_list' says which fields
975 we provide values for. */
977 static struct so_list *
978 svr4_current_sos (void)
980 CORE_ADDR lm;
981 struct so_list *head = 0;
982 struct so_list **link_ptr = &head;
984 /* Make sure we've looked up the inferior's dynamic linker's base
985 structure. */
986 if (! debug_base)
988 debug_base = locate_base ();
990 /* If we can't find the dynamic linker's base structure, this
991 must not be a dynamically linked executable. Hmm. */
992 if (! debug_base)
993 return 0;
996 /* Walk the inferior's link map list, and build our list of
997 `struct so_list' nodes. */
998 lm = first_link_map_member ();
999 while (lm)
1001 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
1002 struct so_list *new
1003 = (struct so_list *) xmalloc (sizeof (struct so_list));
1004 struct cleanup *old_chain = make_cleanup (xfree, new);
1006 memset (new, 0, sizeof (*new));
1008 new->lm_info = xmalloc (sizeof (struct lm_info));
1009 make_cleanup (xfree, new->lm_info);
1011 new->lm_info->lm = xmalloc (lmo->link_map_size);
1012 make_cleanup (xfree, new->lm_info->lm);
1013 memset (new->lm_info->lm, 0, lmo->link_map_size);
1015 read_memory (lm, new->lm_info->lm, lmo->link_map_size);
1017 lm = LM_NEXT (new);
1019 /* For SVR4 versions, the first entry in the link map is for the
1020 inferior executable, so we must ignore it. For some versions of
1021 SVR4, it has no name. For others (Solaris 2.3 for example), it
1022 does have a name, so we can no longer use a missing name to
1023 decide when to ignore it. */
1024 if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
1025 free_so (new);
1026 else
1028 int errcode;
1029 char *buffer;
1031 /* Extract this shared object's name. */
1032 target_read_string (LM_NAME (new), &buffer,
1033 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
1034 if (errcode != 0)
1036 warning ("current_sos: Can't read pathname for load map: %s\n",
1037 safe_strerror (errcode));
1039 else
1041 strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
1042 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
1043 xfree (buffer);
1044 strcpy (new->so_original_name, new->so_name);
1047 /* If this entry has no name, or its name matches the name
1048 for the main executable, don't include it in the list. */
1049 if (! new->so_name[0]
1050 || match_main (new->so_name))
1051 free_so (new);
1052 else
1054 new->next = 0;
1055 *link_ptr = new;
1056 link_ptr = &new->next;
1060 discard_cleanups (old_chain);
1063 return head;
1067 /* On some systems, the only way to recognize the link map entry for
1068 the main executable file is by looking at its name. Return
1069 non-zero iff SONAME matches one of the known main executable names. */
1071 static int
1072 match_main (char *soname)
1074 char **mainp;
1076 for (mainp = main_name_list; *mainp != NULL; mainp++)
1078 if (strcmp (soname, *mainp) == 0)
1079 return (1);
1082 return (0);
1086 #ifdef SVR4_SHARED_LIBS
1088 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1089 SVR4 run time loader. */
1091 static CORE_ADDR interp_text_sect_low;
1092 static CORE_ADDR interp_text_sect_high;
1093 static CORE_ADDR interp_plt_sect_low;
1094 static CORE_ADDR interp_plt_sect_high;
1097 in_svr4_dynsym_resolve_code (CORE_ADDR pc)
1099 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
1100 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
1101 || in_plt_section (pc, NULL));
1103 #endif
1107 LOCAL FUNCTION
1109 disable_break -- remove the "mapping changed" breakpoint
1111 SYNOPSIS
1113 static int disable_break ()
1115 DESCRIPTION
1117 Removes the breakpoint that gets hit when the dynamic linker
1118 completes a mapping change.
1122 #ifndef SVR4_SHARED_LIBS
1124 static int
1125 disable_break (void)
1127 int status = 1;
1129 int in_debugger = 0;
1131 /* Read the debugger structure from the inferior to retrieve the
1132 address of the breakpoint and the original contents of the
1133 breakpoint address. Remove the breakpoint by writing the original
1134 contents back. */
1136 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1138 /* Set `in_debugger' to zero now. */
1140 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1142 breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr);
1143 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1144 sizeof (debug_copy.ldd_bp_inst));
1146 /* For the SVR4 version, we always know the breakpoint address. For the
1147 SunOS version we don't know it until the above code is executed.
1148 Grumble if we are stopped anywhere besides the breakpoint address. */
1150 if (stop_pc != breakpoint_addr)
1152 warning ("stopped at unknown breakpoint while handling shared libraries");
1155 return (status);
1158 #endif /* #ifdef SVR4_SHARED_LIBS */
1162 LOCAL FUNCTION
1164 enable_break -- arrange for dynamic linker to hit breakpoint
1166 SYNOPSIS
1168 int enable_break (void)
1170 DESCRIPTION
1172 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1173 debugger interface, support for arranging for the inferior to hit
1174 a breakpoint after mapping in the shared libraries. This function
1175 enables that breakpoint.
1177 For SunOS, there is a special flag location (in_debugger) which we
1178 set to 1. When the dynamic linker sees this flag set, it will set
1179 a breakpoint at a location known only to itself, after saving the
1180 original contents of that place and the breakpoint address itself,
1181 in it's own internal structures. When we resume the inferior, it
1182 will eventually take a SIGTRAP when it runs into the breakpoint.
1183 We handle this (in a different place) by restoring the contents of
1184 the breakpointed location (which is only known after it stops),
1185 chasing around to locate the shared libraries that have been
1186 loaded, then resuming.
1188 For SVR4, the debugger interface structure contains a member (r_brk)
1189 which is statically initialized at the time the shared library is
1190 built, to the offset of a function (_r_debug_state) which is guaran-
1191 teed to be called once before mapping in a library, and again when
1192 the mapping is complete. At the time we are examining this member,
1193 it contains only the unrelocated offset of the function, so we have
1194 to do our own relocation. Later, when the dynamic linker actually
1195 runs, it relocates r_brk to be the actual address of _r_debug_state().
1197 The debugger interface structure also contains an enumeration which
1198 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1199 depending upon whether or not the library is being mapped or unmapped,
1200 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1203 static int
1204 enable_break (void)
1206 int success = 0;
1208 #ifndef SVR4_SHARED_LIBS
1210 int j;
1211 int in_debugger;
1213 /* Get link_dynamic structure */
1215 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1216 sizeof (dynamic_copy));
1217 if (j)
1219 /* unreadable */
1220 return (0);
1223 /* Calc address of debugger interface structure */
1225 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1227 /* Calc address of `in_debugger' member of debugger interface structure */
1229 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1230 (char *) &debug_copy);
1232 /* Write a value of 1 to this member. */
1234 in_debugger = 1;
1235 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1236 success = 1;
1238 #else /* SVR4_SHARED_LIBS */
1240 #ifdef BKPT_AT_SYMBOL
1242 struct minimal_symbol *msymbol;
1243 char **bkpt_namep;
1244 asection *interp_sect;
1246 /* First, remove all the solib event breakpoints. Their addresses
1247 may have changed since the last time we ran the program. */
1248 remove_solib_event_breakpoints ();
1250 #ifdef SVR4_SHARED_LIBS
1251 interp_text_sect_low = interp_text_sect_high = 0;
1252 interp_plt_sect_low = interp_plt_sect_high = 0;
1254 /* Find the .interp section; if not found, warn the user and drop
1255 into the old breakpoint at symbol code. */
1256 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1257 if (interp_sect)
1259 unsigned int interp_sect_size;
1260 char *buf;
1261 CORE_ADDR load_addr;
1262 bfd *tmp_bfd = NULL;
1263 int tmp_fd = -1;
1264 char *tmp_pathname = NULL;
1265 CORE_ADDR sym_addr = 0;
1267 /* Read the contents of the .interp section into a local buffer;
1268 the contents specify the dynamic linker this program uses. */
1269 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1270 buf = alloca (interp_sect_size);
1271 bfd_get_section_contents (exec_bfd, interp_sect,
1272 buf, 0, interp_sect_size);
1274 /* Now we need to figure out where the dynamic linker was
1275 loaded so that we can load its symbols and place a breakpoint
1276 in the dynamic linker itself.
1278 This address is stored on the stack. However, I've been unable
1279 to find any magic formula to find it for Solaris (appears to
1280 be trivial on GNU/Linux). Therefore, we have to try an alternate
1281 mechanism to find the dynamic linker's base address. */
1283 tmp_fd = solib_open (buf, &tmp_pathname);
1284 if (tmp_fd >= 0)
1285 tmp_bfd = bfd_fdopenr (tmp_pathname, gnutarget, tmp_fd);
1287 if (tmp_bfd == NULL)
1288 goto bkpt_at_symbol;
1290 /* Make sure the dynamic linker's really a useful object. */
1291 if (!bfd_check_format (tmp_bfd, bfd_object))
1293 warning ("Unable to grok dynamic linker %s as an object file", buf);
1294 bfd_close (tmp_bfd);
1295 goto bkpt_at_symbol;
1298 /* We find the dynamic linker's base address by examining the
1299 current pc (which point at the entry point for the dynamic
1300 linker) and subtracting the offset of the entry point. */
1301 load_addr = read_pc () - tmp_bfd->start_address;
1303 /* Record the relocated start and end address of the dynamic linker
1304 text and plt section for in_svr4_dynsym_resolve_code. */
1305 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
1306 if (interp_sect)
1308 interp_text_sect_low =
1309 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1310 interp_text_sect_high =
1311 interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1313 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1314 if (interp_sect)
1316 interp_plt_sect_low =
1317 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1318 interp_plt_sect_high =
1319 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1322 /* Now try to set a breakpoint in the dynamic linker. */
1323 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1325 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1326 if (sym_addr != 0)
1327 break;
1330 /* We're done with the temporary bfd. */
1331 bfd_close (tmp_bfd);
1333 if (sym_addr != 0)
1335 create_solib_event_breakpoint (load_addr + sym_addr);
1336 return 1;
1339 /* For whatever reason we couldn't set a breakpoint in the dynamic
1340 linker. Warn and drop into the old code. */
1341 bkpt_at_symbol:
1342 warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
1344 #endif
1346 /* Scan through the list of symbols, trying to look up the symbol and
1347 set a breakpoint there. Terminate loop when we/if we succeed. */
1349 breakpoint_addr = 0;
1350 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1352 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1353 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1355 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1356 return 1;
1360 /* Nothing good happened. */
1361 success = 0;
1363 #endif /* BKPT_AT_SYMBOL */
1365 #endif /* !SVR4_SHARED_LIBS */
1367 return (success);
1372 LOCAL FUNCTION
1374 special_symbol_handling -- additional shared library symbol handling
1376 SYNOPSIS
1378 void special_symbol_handling ()
1380 DESCRIPTION
1382 Once the symbols from a shared object have been loaded in the usual
1383 way, we are called to do any system specific symbol handling that
1384 is needed.
1386 For SunOS4, this consists of grunging around in the dynamic
1387 linkers structures to find symbol definitions for "common" symbols
1388 and adding them to the minimal symbol table for the runtime common
1389 objfile.
1393 static void
1394 svr4_special_symbol_handling (void)
1396 #ifndef SVR4_SHARED_LIBS
1397 int j;
1399 if (debug_addr == 0)
1401 /* Get link_dynamic structure */
1403 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1404 sizeof (dynamic_copy));
1405 if (j)
1407 /* unreadable */
1408 return;
1411 /* Calc address of debugger interface structure */
1412 /* FIXME, this needs work for cross-debugging of core files
1413 (byteorder, size, alignment, etc). */
1415 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1418 /* Read the debugger structure from the inferior, just to make sure
1419 we have a current copy. */
1421 j = target_read_memory (debug_addr, (char *) &debug_copy,
1422 sizeof (debug_copy));
1423 if (j)
1424 return; /* unreadable */
1426 /* Get common symbol definitions for the loaded object. */
1428 if (debug_copy.ldd_cp)
1430 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp));
1433 #endif /* !SVR4_SHARED_LIBS */
1436 /* Relocate the main executable. This function should be called upon
1437 stopping the inferior process at the entry point to the program.
1438 The entry point from BFD is compared to the PC and if they are
1439 different, the main executable is relocated by the proper amount.
1441 As written it will only attempt to relocate executables which
1442 lack interpreter sections. It seems likely that only dynamic
1443 linker executables will get relocated, though it should work
1444 properly for a position-independent static executable as well. */
1446 static void
1447 svr4_relocate_main_executable (void)
1449 asection *interp_sect;
1450 CORE_ADDR pc = read_pc ();
1452 /* Decide if the objfile needs to be relocated. As indicated above,
1453 we will only be here when execution is stopped at the beginning
1454 of the program. Relocation is necessary if the address at which
1455 we are presently stopped differs from the start address stored in
1456 the executable AND there's no interpreter section. The condition
1457 regarding the interpreter section is very important because if
1458 there *is* an interpreter section, execution will begin there
1459 instead. When there is an interpreter section, the start address
1460 is (presumably) used by the interpreter at some point to start
1461 execution of the program.
1463 If there is an interpreter, it is normal for it to be set to an
1464 arbitrary address at the outset. The job of finding it is
1465 handled in enable_break().
1467 So, to summarize, relocations are necessary when there is no
1468 interpreter section and the start address obtained from the
1469 executable is different from the address at which GDB is
1470 currently stopped.
1472 [ The astute reader will note that we also test to make sure that
1473 the executable in question has the DYNAMIC flag set. It is my
1474 opinion that this test is unnecessary (undesirable even). It
1475 was added to avoid inadvertent relocation of an executable
1476 whose e_type member in the ELF header is not ET_DYN. There may
1477 be a time in the future when it is desirable to do relocations
1478 on other types of files as well in which case this condition
1479 should either be removed or modified to accomodate the new file
1480 type. (E.g, an ET_EXEC executable which has been built to be
1481 position-independent could safely be relocated by the OS if
1482 desired. It is true that this violates the ABI, but the ABI
1483 has been known to be bent from time to time.) - Kevin, Nov 2000. ]
1486 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1487 if (interp_sect == NULL
1488 && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0
1489 && bfd_get_start_address (exec_bfd) != pc)
1491 struct cleanup *old_chain;
1492 struct section_offsets *new_offsets;
1493 int i, changed;
1494 CORE_ADDR displacement;
1496 /* It is necessary to relocate the objfile. The amount to
1497 relocate by is simply the address at which we are stopped
1498 minus the starting address from the executable.
1500 We relocate all of the sections by the same amount. This
1501 behavior is mandated by recent editions of the System V ABI.
1502 According to the System V Application Binary Interface,
1503 Edition 4.1, page 5-5:
1505 ... Though the system chooses virtual addresses for
1506 individual processes, it maintains the segments' relative
1507 positions. Because position-independent code uses relative
1508 addressesing between segments, the difference between
1509 virtual addresses in memory must match the difference
1510 between virtual addresses in the file. The difference
1511 between the virtual address of any segment in memory and
1512 the corresponding virtual address in the file is thus a
1513 single constant value for any one executable or shared
1514 object in a given process. This difference is the base
1515 address. One use of the base address is to relocate the
1516 memory image of the program during dynamic linking.
1518 The same language also appears in Edition 4.0 of the System V
1519 ABI and is left unspecified in some of the earlier editions. */
1521 displacement = pc - bfd_get_start_address (exec_bfd);
1522 changed = 0;
1524 new_offsets = xcalloc (sizeof (struct section_offsets),
1525 symfile_objfile->num_sections);
1526 old_chain = make_cleanup (xfree, new_offsets);
1528 for (i = 0; i < symfile_objfile->num_sections; i++)
1530 if (displacement != ANOFFSET (symfile_objfile->section_offsets, i))
1531 changed = 1;
1532 new_offsets->offsets[i] = displacement;
1535 if (changed)
1536 objfile_relocate (symfile_objfile, new_offsets);
1538 do_cleanups (old_chain);
1544 GLOBAL FUNCTION
1546 svr4_solib_create_inferior_hook -- shared library startup support
1548 SYNOPSIS
1550 void svr4_solib_create_inferior_hook()
1552 DESCRIPTION
1554 When gdb starts up the inferior, it nurses it along (through the
1555 shell) until it is ready to execute it's first instruction. At this
1556 point, this function gets called via expansion of the macro
1557 SOLIB_CREATE_INFERIOR_HOOK.
1559 For SunOS executables, this first instruction is typically the
1560 one at "_start", or a similar text label, regardless of whether
1561 the executable is statically or dynamically linked. The runtime
1562 startup code takes care of dynamically linking in any shared
1563 libraries, once gdb allows the inferior to continue.
1565 For SVR4 executables, this first instruction is either the first
1566 instruction in the dynamic linker (for dynamically linked
1567 executables) or the instruction at "start" for statically linked
1568 executables. For dynamically linked executables, the system
1569 first exec's /lib/libc.so.N, which contains the dynamic linker,
1570 and starts it running. The dynamic linker maps in any needed
1571 shared libraries, maps in the actual user executable, and then
1572 jumps to "start" in the user executable.
1574 For both SunOS shared libraries, and SVR4 shared libraries, we
1575 can arrange to cooperate with the dynamic linker to discover the
1576 names of shared libraries that are dynamically linked, and the
1577 base addresses to which they are linked.
1579 This function is responsible for discovering those names and
1580 addresses, and saving sufficient information about them to allow
1581 their symbols to be read at a later time.
1583 FIXME
1585 Between enable_break() and disable_break(), this code does not
1586 properly handle hitting breakpoints which the user might have
1587 set in the startup code or in the dynamic linker itself. Proper
1588 handling will probably have to wait until the implementation is
1589 changed to use the "breakpoint handler function" method.
1591 Also, what if child has exit()ed? Must exit loop somehow.
1594 static void
1595 svr4_solib_create_inferior_hook (void)
1597 /* Relocate the main executable if necessary. */
1598 svr4_relocate_main_executable ();
1600 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1601 yet. In fact, in the case of a SunOS4 executable being run on
1602 Solaris, we can't get it yet. current_sos will get it when it needs
1603 it. */
1604 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1605 if ((debug_base = locate_base ()) == 0)
1607 /* Can't find the symbol or the executable is statically linked. */
1608 return;
1610 #endif
1612 if (!enable_break ())
1614 warning ("shared library handler failed to enable breakpoint");
1615 return;
1618 #if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS)
1619 /* SCO and SunOS need the loop below, other systems should be using the
1620 special shared library breakpoints and the shared library breakpoint
1621 service routine.
1623 Now run the target. It will eventually hit the breakpoint, at
1624 which point all of the libraries will have been mapped in and we
1625 can go groveling around in the dynamic linker structures to find
1626 out what we need to know about them. */
1628 clear_proceed_status ();
1629 stop_soon_quietly = 1;
1630 stop_signal = TARGET_SIGNAL_0;
1633 target_resume (-1, 0, stop_signal);
1634 wait_for_inferior ();
1636 while (stop_signal != TARGET_SIGNAL_TRAP);
1637 stop_soon_quietly = 0;
1639 #if !defined(_SCO_DS)
1640 /* We are now either at the "mapping complete" breakpoint (or somewhere
1641 else, a condition we aren't prepared to deal with anyway), so adjust
1642 the PC as necessary after a breakpoint, disable the breakpoint, and
1643 add any shared libraries that were mapped in. */
1645 if (DECR_PC_AFTER_BREAK)
1647 stop_pc -= DECR_PC_AFTER_BREAK;
1648 write_register (PC_REGNUM, stop_pc);
1651 if (!disable_break ())
1653 warning ("shared library handler failed to disable breakpoint");
1656 if (auto_solib_add)
1657 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1658 #endif /* ! _SCO_DS */
1659 #endif
1662 static void
1663 svr4_clear_solib (void)
1665 debug_base = 0;
1668 static void
1669 svr4_free_so (struct so_list *so)
1671 xfree (so->lm_info->lm);
1672 xfree (so->lm_info);
1675 static void
1676 svr4_relocate_section_addresses (struct so_list *so,
1677 struct section_table *sec)
1679 sec->addr += LM_ADDR (so);
1680 sec->endaddr += LM_ADDR (so);
1683 static struct target_so_ops svr4_so_ops;
1685 void
1686 _initialize_svr4_solib (void)
1688 svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
1689 svr4_so_ops.free_so = svr4_free_so;
1690 svr4_so_ops.clear_solib = svr4_clear_solib;
1691 svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
1692 svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
1693 svr4_so_ops.current_sos = svr4_current_sos;
1694 svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
1696 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
1697 current_target_so_ops = &svr4_so_ops;