* thread-db.c (check_event): Don't report an error if we encounter
[deliverable/binutils-gdb.git] / gdb / solib-svr4.c
1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
3 2001
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "regcache.h"
25
26
27 #include <sys/types.h>
28 #include <signal.h>
29 #include "gdb_string.h"
30 #include <sys/param.h>
31 #include <fcntl.h>
32
33 #ifndef SVR4_SHARED_LIBS
34 /* SunOS shared libs need the nlist structure. */
35 #include <a.out.h>
36 #include <link.h>
37 #else
38 #include "elf/external.h"
39 #include "elf/common.h"
40 #endif
41
42 #include "symtab.h"
43 #include "bfd.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "gdbcore.h"
47 #include "command.h"
48 #include "target.h"
49 #include "frame.h"
50 #include "gdb_regex.h"
51 #include "inferior.h"
52 #include "environ.h"
53 #include "language.h"
54 #include "gdbcmd.h"
55
56 #include "solist.h"
57 #include "solib-svr4.h"
58
59 #ifndef SVR4_FETCH_LINK_MAP_OFFSETS
60 #define SVR4_FETCH_LINK_MAP_OFFSETS() fetch_link_map_offsets ()
61 #endif
62
63 static struct link_map_offsets *default_svr4_fetch_link_map_offsets (void);
64 static struct link_map_offsets *(*fetch_link_map_offsets)(void) =
65 default_svr4_fetch_link_map_offsets;
66
67 /* legacy_svr4_fetch_link_map_offsets_hook is a pointer to a function
68 which is used to fetch link map offsets. It will only be set
69 by solib-legacy.c, if at all. */
70 struct link_map_offsets *(*legacy_svr4_fetch_link_map_offsets_hook)(void) = 0;
71
72 /* Link map info to include in an allocated so_list entry */
73
74 struct lm_info
75 {
76 /* Pointer to copy of link map from inferior. The type is char *
77 rather than void *, so that we may use byte offsets to find the
78 various fields without the need for a cast. */
79 char *lm;
80 };
81
82 /* On SVR4 systems, a list of symbols in the dynamic linker where
83 GDB can try to place a breakpoint to monitor shared library
84 events.
85
86 If none of these symbols are found, or other errors occur, then
87 SVR4 systems will fall back to using a symbol as the "startup
88 mapping complete" breakpoint address. */
89
90 #ifdef SVR4_SHARED_LIBS
91 static char *solib_break_names[] =
92 {
93 "r_debug_state",
94 "_r_debug_state",
95 "_dl_debug_state",
96 "rtld_db_dlactivity",
97 "_rtld_debug_state",
98 NULL
99 };
100 #endif
101
102 #define BKPT_AT_SYMBOL 1
103
104 #if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
105 static char *bkpt_names[] =
106 {
107 #ifdef SOLIB_BKPT_NAME
108 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
109 #endif
110 "_start",
111 "main",
112 NULL
113 };
114 #endif
115
116 /* Symbols which are used to locate the base of the link map structures. */
117
118 #ifndef SVR4_SHARED_LIBS
119 static char *debug_base_symbols[] =
120 {
121 "_DYNAMIC",
122 "_DYNAMIC__MGC",
123 NULL
124 };
125 #endif
126
127 static char *main_name_list[] =
128 {
129 "main_$main",
130 NULL
131 };
132
133
134 /* Fetch (and possibly build) an appropriate link_map_offsets structure
135 for native targets using struct definitions from link.h.
136
137 Note: For non-native targets (i.e. cross-debugging situations),
138 you need to define a target specific fetch_link_map_offsets()
139 function and call set_solib_svr4_fetch_link_map_offsets () to
140 register this function. */
141
142 static struct link_map_offsets *
143 default_svr4_fetch_link_map_offsets (void)
144 {
145 if (legacy_svr4_fetch_link_map_offsets_hook)
146 return legacy_svr4_fetch_link_map_offsets_hook ();
147 else
148 {
149 internal_error (__FILE__, __LINE__,
150 "default_svr4_fetch_link_map_offsets called without legacy link_map support enabled.");
151 return 0;
152 }
153 }
154
155 /* Macro to extract an address from a solib structure.
156 When GDB is configured for some 32-bit targets (e.g. Solaris 2.7
157 sparc), BFD is configured to handle 64-bit targets, so CORE_ADDR is
158 64 bits. We have to extract only the significant bits of addresses
159 to get the right address when accessing the core file BFD. */
160
161 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
162 extract_address (&(MEMBER), sizeof (MEMBER))
163
164 /* local data declarations */
165
166 #ifndef SVR4_SHARED_LIBS
167
168 /* NOTE: converted the macros LM_ADDR, LM_NEXT, LM_NAME and
169 IGNORE_FIRST_LINK_MAP_ENTRY into functions (see below).
170 MVS, June 2000 */
171
172 static struct link_dynamic dynamic_copy;
173 static struct link_dynamic_2 ld_2_copy;
174 static struct ld_debug debug_copy;
175 static CORE_ADDR debug_addr;
176 static CORE_ADDR flag_addr;
177
178 #endif /* !SVR4_SHARED_LIBS */
179
180 /* link map access functions */
181
182 static CORE_ADDR
183 LM_ADDR (struct so_list *so)
184 {
185 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
186
187 return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lmo->l_addr_offset,
188 lmo->l_addr_size);
189 }
190
191 static CORE_ADDR
192 LM_NEXT (struct so_list *so)
193 {
194 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
195
196 return extract_address (so->lm_info->lm + lmo->l_next_offset, lmo->l_next_size);
197 }
198
199 static CORE_ADDR
200 LM_NAME (struct so_list *so)
201 {
202 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
203
204 return extract_address (so->lm_info->lm + lmo->l_name_offset, lmo->l_name_size);
205 }
206
207 #ifndef SVR4_SHARED_LIBS
208
209 static int
210 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
211 {
212 return 0;
213 }
214
215 #else /* SVR4_SHARED_LIBS */
216
217 static int
218 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
219 {
220 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
221
222 return extract_address (so->lm_info->lm + lmo->l_prev_offset,
223 lmo->l_prev_size) == 0;
224 }
225
226 #endif /* !SVR4_SHARED_LIBS */
227
228 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
229 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
230
231 /* Local function prototypes */
232
233 static int match_main (char *);
234
235 #ifndef SVR4_SHARED_LIBS
236
237 /* Allocate the runtime common object file. */
238
239 static void
240 allocate_rt_common_objfile (void)
241 {
242 struct objfile *objfile;
243 struct objfile *last_one;
244
245 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
246 memset (objfile, 0, sizeof (struct objfile));
247 objfile->md = NULL;
248 obstack_specify_allocation (&objfile->psymbol_cache.cache, 0, 0,
249 xmalloc, xfree);
250 obstack_specify_allocation (&objfile->psymbol_obstack, 0, 0, xmalloc,
251 xfree);
252 obstack_specify_allocation (&objfile->symbol_obstack, 0, 0, xmalloc,
253 xfree);
254 obstack_specify_allocation (&objfile->type_obstack, 0, 0, xmalloc,
255 xfree);
256 objfile->name = mstrsave (objfile->md, "rt_common");
257
258 /* Add this file onto the tail of the linked list of other such files. */
259
260 objfile->next = NULL;
261 if (object_files == NULL)
262 object_files = objfile;
263 else
264 {
265 for (last_one = object_files;
266 last_one->next;
267 last_one = last_one->next);
268 last_one->next = objfile;
269 }
270
271 rt_common_objfile = objfile;
272 }
273
274 /* Read all dynamically loaded common symbol definitions from the inferior
275 and put them into the minimal symbol table for the runtime common
276 objfile. */
277
278 static void
279 solib_add_common_symbols (CORE_ADDR rtc_symp)
280 {
281 struct rtc_symb inferior_rtc_symb;
282 struct nlist inferior_rtc_nlist;
283 int len;
284 char *name;
285
286 /* Remove any runtime common symbols from previous runs. */
287
288 if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count)
289 {
290 obstack_free (&rt_common_objfile->symbol_obstack, 0);
291 obstack_specify_allocation (&rt_common_objfile->symbol_obstack, 0, 0,
292 xmalloc, xfree);
293 rt_common_objfile->minimal_symbol_count = 0;
294 rt_common_objfile->msymbols = NULL;
295 }
296
297 init_minimal_symbol_collection ();
298 make_cleanup_discard_minimal_symbols ();
299
300 while (rtc_symp)
301 {
302 read_memory (rtc_symp,
303 (char *) &inferior_rtc_symb,
304 sizeof (inferior_rtc_symb));
305 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp),
306 (char *) &inferior_rtc_nlist,
307 sizeof (inferior_rtc_nlist));
308 if (inferior_rtc_nlist.n_type == N_COMM)
309 {
310 /* FIXME: The length of the symbol name is not available, but in the
311 current implementation the common symbol is allocated immediately
312 behind the name of the symbol. */
313 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
314
315 name = xmalloc (len);
316 read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name),
317 name, len);
318
319 /* Allocate the runtime common objfile if necessary. */
320 if (rt_common_objfile == NULL)
321 allocate_rt_common_objfile ();
322
323 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
324 mst_bss, rt_common_objfile);
325 xfree (name);
326 }
327 rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next);
328 }
329
330 /* Install any minimal symbols that have been collected as the current
331 minimal symbols for the runtime common objfile. */
332
333 install_minimal_symbols (rt_common_objfile);
334 }
335
336 #endif /* SVR4_SHARED_LIBS */
337
338
339 #ifdef SVR4_SHARED_LIBS
340
341 static CORE_ADDR bfd_lookup_symbol (bfd *, char *);
342
343 /*
344
345 LOCAL FUNCTION
346
347 bfd_lookup_symbol -- lookup the value for a specific symbol
348
349 SYNOPSIS
350
351 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
352
353 DESCRIPTION
354
355 An expensive way to lookup the value of a single symbol for
356 bfd's that are only temporary anyway. This is used by the
357 shared library support to find the address of the debugger
358 interface structures in the shared library.
359
360 Note that 0 is specifically allowed as an error return (no
361 such symbol).
362 */
363
364 static CORE_ADDR
365 bfd_lookup_symbol (bfd *abfd, char *symname)
366 {
367 long storage_needed;
368 asymbol *sym;
369 asymbol **symbol_table;
370 unsigned int number_of_symbols;
371 unsigned int i;
372 struct cleanup *back_to;
373 CORE_ADDR symaddr = 0;
374
375 storage_needed = bfd_get_symtab_upper_bound (abfd);
376
377 if (storage_needed > 0)
378 {
379 symbol_table = (asymbol **) xmalloc (storage_needed);
380 back_to = make_cleanup (xfree, (PTR) symbol_table);
381 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
382
383 for (i = 0; i < number_of_symbols; i++)
384 {
385 sym = *symbol_table++;
386 if (STREQ (sym->name, symname))
387 {
388 /* Bfd symbols are section relative. */
389 symaddr = sym->value + sym->section->vma;
390 break;
391 }
392 }
393 do_cleanups (back_to);
394 }
395
396 if (symaddr)
397 return symaddr;
398
399 /* On FreeBSD, the dynamic linker is stripped by default. So we'll
400 have to check the dynamic string table too. */
401
402 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
403
404 if (storage_needed > 0)
405 {
406 symbol_table = (asymbol **) xmalloc (storage_needed);
407 back_to = make_cleanup (xfree, (PTR) symbol_table);
408 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
409
410 for (i = 0; i < number_of_symbols; i++)
411 {
412 sym = *symbol_table++;
413 if (STREQ (sym->name, symname))
414 {
415 /* Bfd symbols are section relative. */
416 symaddr = sym->value + sym->section->vma;
417 break;
418 }
419 }
420 do_cleanups (back_to);
421 }
422
423 return symaddr;
424 }
425
426 #ifdef HANDLE_SVR4_EXEC_EMULATORS
427
428 /*
429 Solaris BCP (the part of Solaris which allows it to run SunOS4
430 a.out files) throws in another wrinkle. Solaris does not fill
431 in the usual a.out link map structures when running BCP programs,
432 the only way to get at them is via groping around in the dynamic
433 linker.
434 The dynamic linker and it's structures are located in the shared
435 C library, which gets run as the executable's "interpreter" by
436 the kernel.
437
438 Note that we can assume nothing about the process state at the time
439 we need to find these structures. We may be stopped on the first
440 instruction of the interpreter (C shared library), the first
441 instruction of the executable itself, or somewhere else entirely
442 (if we attached to the process for example).
443 */
444
445 static char *debug_base_symbols[] =
446 {
447 "r_debug", /* Solaris 2.3 */
448 "_r_debug", /* Solaris 2.1, 2.2 */
449 NULL
450 };
451
452 static int look_for_base (int, CORE_ADDR);
453
454 /*
455
456 LOCAL FUNCTION
457
458 look_for_base -- examine file for each mapped address segment
459
460 SYNOPSYS
461
462 static int look_for_base (int fd, CORE_ADDR baseaddr)
463
464 DESCRIPTION
465
466 This function is passed to proc_iterate_over_mappings, which
467 causes it to get called once for each mapped address space, with
468 an open file descriptor for the file mapped to that space, and the
469 base address of that mapped space.
470
471 Our job is to find the debug base symbol in the file that this
472 fd is open on, if it exists, and if so, initialize the dynamic
473 linker structure base address debug_base.
474
475 Note that this is a computationally expensive proposition, since
476 we basically have to open a bfd on every call, so we specifically
477 avoid opening the exec file.
478 */
479
480 static int
481 look_for_base (int fd, CORE_ADDR baseaddr)
482 {
483 bfd *interp_bfd;
484 CORE_ADDR address = 0;
485 char **symbolp;
486
487 /* If the fd is -1, then there is no file that corresponds to this
488 mapped memory segment, so skip it. Also, if the fd corresponds
489 to the exec file, skip it as well. */
490
491 if (fd == -1
492 || (exec_bfd != NULL
493 && fdmatch (fileno ((FILE *) (exec_bfd->iostream)), fd)))
494 {
495 return (0);
496 }
497
498 /* Try to open whatever random file this fd corresponds to. Note that
499 we have no way currently to find the filename. Don't gripe about
500 any problems we might have, just fail. */
501
502 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
503 {
504 return (0);
505 }
506 if (!bfd_check_format (interp_bfd, bfd_object))
507 {
508 /* FIXME-leak: on failure, might not free all memory associated with
509 interp_bfd. */
510 bfd_close (interp_bfd);
511 return (0);
512 }
513
514 /* Now try to find our debug base symbol in this file, which we at
515 least know to be a valid ELF executable or shared library. */
516
517 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
518 {
519 address = bfd_lookup_symbol (interp_bfd, *symbolp);
520 if (address != 0)
521 {
522 break;
523 }
524 }
525 if (address == 0)
526 {
527 /* FIXME-leak: on failure, might not free all memory associated with
528 interp_bfd. */
529 bfd_close (interp_bfd);
530 return (0);
531 }
532
533 /* Eureka! We found the symbol. But now we may need to relocate it
534 by the base address. If the symbol's value is less than the base
535 address of the shared library, then it hasn't yet been relocated
536 by the dynamic linker, and we have to do it ourself. FIXME: Note
537 that we make the assumption that the first segment that corresponds
538 to the shared library has the base address to which the library
539 was relocated. */
540
541 if (address < baseaddr)
542 {
543 address += baseaddr;
544 }
545 debug_base = address;
546 /* FIXME-leak: on failure, might not free all memory associated with
547 interp_bfd. */
548 bfd_close (interp_bfd);
549 return (1);
550 }
551 #endif /* HANDLE_SVR4_EXEC_EMULATORS */
552
553 /*
554
555 LOCAL FUNCTION
556
557 elf_locate_base -- locate the base address of dynamic linker structs
558 for SVR4 elf targets.
559
560 SYNOPSIS
561
562 CORE_ADDR elf_locate_base (void)
563
564 DESCRIPTION
565
566 For SVR4 elf targets the address of the dynamic linker's runtime
567 structure is contained within the dynamic info section in the
568 executable file. The dynamic section is also mapped into the
569 inferior address space. Because the runtime loader fills in the
570 real address before starting the inferior, we have to read in the
571 dynamic info section from the inferior address space.
572 If there are any errors while trying to find the address, we
573 silently return 0, otherwise the found address is returned.
574
575 */
576
577 static CORE_ADDR
578 elf_locate_base (void)
579 {
580 sec_ptr dyninfo_sect;
581 int dyninfo_sect_size;
582 CORE_ADDR dyninfo_addr;
583 char *buf;
584 char *bufend;
585 int arch_size;
586
587 /* Find the start address of the .dynamic section. */
588 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
589 if (dyninfo_sect == NULL)
590 return 0;
591 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
592
593 /* Read in .dynamic section, silently ignore errors. */
594 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
595 buf = alloca (dyninfo_sect_size);
596 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
597 return 0;
598
599 /* Find the DT_DEBUG entry in the the .dynamic section.
600 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
601 no DT_DEBUG entries. */
602
603 arch_size = bfd_get_arch_size (exec_bfd);
604 if (arch_size == -1) /* failure */
605 return 0;
606
607 if (arch_size == 32)
608 { /* 32-bit elf */
609 for (bufend = buf + dyninfo_sect_size;
610 buf < bufend;
611 buf += sizeof (Elf32_External_Dyn))
612 {
613 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *) buf;
614 long dyn_tag;
615 CORE_ADDR dyn_ptr;
616
617 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
618 if (dyn_tag == DT_NULL)
619 break;
620 else if (dyn_tag == DT_DEBUG)
621 {
622 dyn_ptr = bfd_h_get_32 (exec_bfd,
623 (bfd_byte *) x_dynp->d_un.d_ptr);
624 return dyn_ptr;
625 }
626 #ifdef DT_MIPS_RLD_MAP
627 else if (dyn_tag == DT_MIPS_RLD_MAP)
628 {
629 char *pbuf;
630
631 pbuf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
632 /* DT_MIPS_RLD_MAP contains a pointer to the address
633 of the dynamic link structure. */
634 dyn_ptr = bfd_h_get_32 (exec_bfd,
635 (bfd_byte *) x_dynp->d_un.d_ptr);
636 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
637 return 0;
638 return extract_unsigned_integer (pbuf, sizeof (pbuf));
639 }
640 #endif
641 }
642 }
643 else /* 64-bit elf */
644 {
645 for (bufend = buf + dyninfo_sect_size;
646 buf < bufend;
647 buf += sizeof (Elf64_External_Dyn))
648 {
649 Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *) buf;
650 long dyn_tag;
651 CORE_ADDR dyn_ptr;
652
653 dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
654 if (dyn_tag == DT_NULL)
655 break;
656 else if (dyn_tag == DT_DEBUG)
657 {
658 dyn_ptr = bfd_h_get_64 (exec_bfd,
659 (bfd_byte *) x_dynp->d_un.d_ptr);
660 return dyn_ptr;
661 }
662 }
663 }
664
665 /* DT_DEBUG entry not found. */
666 return 0;
667 }
668
669 #endif /* SVR4_SHARED_LIBS */
670
671 /*
672
673 LOCAL FUNCTION
674
675 locate_base -- locate the base address of dynamic linker structs
676
677 SYNOPSIS
678
679 CORE_ADDR locate_base (void)
680
681 DESCRIPTION
682
683 For both the SunOS and SVR4 shared library implementations, if the
684 inferior executable has been linked dynamically, there is a single
685 address somewhere in the inferior's data space which is the key to
686 locating all of the dynamic linker's runtime structures. This
687 address is the value of the debug base symbol. The job of this
688 function is to find and return that address, or to return 0 if there
689 is no such address (the executable is statically linked for example).
690
691 For SunOS, the job is almost trivial, since the dynamic linker and
692 all of it's structures are statically linked to the executable at
693 link time. Thus the symbol for the address we are looking for has
694 already been added to the minimal symbol table for the executable's
695 objfile at the time the symbol file's symbols were read, and all we
696 have to do is look it up there. Note that we explicitly do NOT want
697 to find the copies in the shared library.
698
699 The SVR4 version is a bit more complicated because the address
700 is contained somewhere in the dynamic info section. We have to go
701 to a lot more work to discover the address of the debug base symbol.
702 Because of this complexity, we cache the value we find and return that
703 value on subsequent invocations. Note there is no copy in the
704 executable symbol tables.
705
706 */
707
708 static CORE_ADDR
709 locate_base (void)
710 {
711
712 #ifndef SVR4_SHARED_LIBS
713
714 struct minimal_symbol *msymbol;
715 CORE_ADDR address = 0;
716 char **symbolp;
717
718 /* For SunOS, we want to limit the search for the debug base symbol to the
719 executable being debugged, since there is a duplicate named symbol in the
720 shared library. We don't want the shared library versions. */
721
722 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
723 {
724 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
725 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
726 {
727 address = SYMBOL_VALUE_ADDRESS (msymbol);
728 return (address);
729 }
730 }
731 return (0);
732
733 #else /* SVR4_SHARED_LIBS */
734
735 /* Check to see if we have a currently valid address, and if so, avoid
736 doing all this work again and just return the cached address. If
737 we have no cached address, try to locate it in the dynamic info
738 section for ELF executables. */
739
740 if (debug_base == 0)
741 {
742 if (exec_bfd != NULL
743 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
744 debug_base = elf_locate_base ();
745 #ifdef HANDLE_SVR4_EXEC_EMULATORS
746 /* Try it the hard way for emulated executables. */
747 else if (inferior_pid != 0 && target_has_execution)
748 proc_iterate_over_mappings (look_for_base);
749 #endif
750 }
751 return (debug_base);
752
753 #endif /* !SVR4_SHARED_LIBS */
754
755 }
756
757 /*
758
759 LOCAL FUNCTION
760
761 first_link_map_member -- locate first member in dynamic linker's map
762
763 SYNOPSIS
764
765 static CORE_ADDR first_link_map_member (void)
766
767 DESCRIPTION
768
769 Find the first element in the inferior's dynamic link map, and
770 return its address in the inferior. This function doesn't copy the
771 link map entry itself into our address space; current_sos actually
772 does the reading. */
773
774 static CORE_ADDR
775 first_link_map_member (void)
776 {
777 CORE_ADDR lm = 0;
778
779 #ifndef SVR4_SHARED_LIBS
780
781 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
782 if (dynamic_copy.ld_version >= 2)
783 {
784 /* It is a version that we can deal with, so read in the secondary
785 structure and find the address of the link map list from it. */
786 read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2),
787 (char *) &ld_2_copy, sizeof (struct link_dynamic_2));
788 lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded);
789 }
790
791 #else /* SVR4_SHARED_LIBS */
792 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
793 char *r_map_buf = xmalloc (lmo->r_map_size);
794 struct cleanup *cleanups = make_cleanup (xfree, r_map_buf);
795
796 read_memory (debug_base + lmo->r_map_offset, r_map_buf, lmo->r_map_size);
797
798 lm = extract_address (r_map_buf, lmo->r_map_size);
799
800 /* FIXME: Perhaps we should validate the info somehow, perhaps by
801 checking r_version for a known version number, or r_state for
802 RT_CONSISTENT. */
803
804 do_cleanups (cleanups);
805
806 #endif /* !SVR4_SHARED_LIBS */
807
808 return (lm);
809 }
810
811 #ifdef SVR4_SHARED_LIBS
812 /*
813
814 LOCAL FUNCTION
815
816 open_symbol_file_object
817
818 SYNOPSIS
819
820 void open_symbol_file_object (void *from_tty)
821
822 DESCRIPTION
823
824 If no open symbol file, attempt to locate and open the main symbol
825 file. On SVR4 systems, this is the first link map entry. If its
826 name is here, we can open it. Useful when attaching to a process
827 without first loading its symbol file.
828
829 If FROM_TTYP dereferences to a non-zero integer, allow messages to
830 be printed. This parameter is a pointer rather than an int because
831 open_symbol_file_object() is called via catch_errors() and
832 catch_errors() requires a pointer argument. */
833
834 static int
835 open_symbol_file_object (void *from_ttyp)
836 {
837 CORE_ADDR lm, l_name;
838 char *filename;
839 int errcode;
840 int from_tty = *(int *)from_ttyp;
841 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
842 char *l_name_buf = xmalloc (lmo->l_name_size);
843 struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
844
845 if (symfile_objfile)
846 if (!query ("Attempt to reload symbols from process? "))
847 return 0;
848
849 if ((debug_base = locate_base ()) == 0)
850 return 0; /* failed somehow... */
851
852 /* First link map member should be the executable. */
853 if ((lm = first_link_map_member ()) == 0)
854 return 0; /* failed somehow... */
855
856 /* Read address of name from target memory to GDB. */
857 read_memory (lm + lmo->l_name_offset, l_name_buf, lmo->l_name_size);
858
859 /* Convert the address to host format. */
860 l_name = extract_address (l_name_buf, lmo->l_name_size);
861
862 /* Free l_name_buf. */
863 do_cleanups (cleanups);
864
865 if (l_name == 0)
866 return 0; /* No filename. */
867
868 /* Now fetch the filename from target memory. */
869 target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
870
871 if (errcode)
872 {
873 warning ("failed to read exec filename from attached file: %s",
874 safe_strerror (errcode));
875 return 0;
876 }
877
878 make_cleanup (xfree, filename);
879 /* Have a pathname: read the symbol file. */
880 symbol_file_add_main (filename, from_tty);
881
882 return 1;
883 }
884 #else
885
886 static int
887 open_symbol_file_object (void *from_ttyp)
888 {
889 return 1;
890 }
891
892 #endif /* SVR4_SHARED_LIBS */
893
894
895 /* LOCAL FUNCTION
896
897 current_sos -- build a list of currently loaded shared objects
898
899 SYNOPSIS
900
901 struct so_list *current_sos ()
902
903 DESCRIPTION
904
905 Build a list of `struct so_list' objects describing the shared
906 objects currently loaded in the inferior. This list does not
907 include an entry for the main executable file.
908
909 Note that we only gather information directly available from the
910 inferior --- we don't examine any of the shared library files
911 themselves. The declaration of `struct so_list' says which fields
912 we provide values for. */
913
914 static struct so_list *
915 svr4_current_sos (void)
916 {
917 CORE_ADDR lm;
918 struct so_list *head = 0;
919 struct so_list **link_ptr = &head;
920
921 /* Make sure we've looked up the inferior's dynamic linker's base
922 structure. */
923 if (! debug_base)
924 {
925 debug_base = locate_base ();
926
927 /* If we can't find the dynamic linker's base structure, this
928 must not be a dynamically linked executable. Hmm. */
929 if (! debug_base)
930 return 0;
931 }
932
933 /* Walk the inferior's link map list, and build our list of
934 `struct so_list' nodes. */
935 lm = first_link_map_member ();
936 while (lm)
937 {
938 struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
939 struct so_list *new
940 = (struct so_list *) xmalloc (sizeof (struct so_list));
941 struct cleanup *old_chain = make_cleanup (xfree, new);
942
943 memset (new, 0, sizeof (*new));
944
945 new->lm_info = xmalloc (sizeof (struct lm_info));
946 make_cleanup (xfree, new->lm_info);
947
948 new->lm_info->lm = xmalloc (lmo->link_map_size);
949 make_cleanup (xfree, new->lm_info->lm);
950 memset (new->lm_info->lm, 0, lmo->link_map_size);
951
952 read_memory (lm, new->lm_info->lm, lmo->link_map_size);
953
954 lm = LM_NEXT (new);
955
956 /* For SVR4 versions, the first entry in the link map is for the
957 inferior executable, so we must ignore it. For some versions of
958 SVR4, it has no name. For others (Solaris 2.3 for example), it
959 does have a name, so we can no longer use a missing name to
960 decide when to ignore it. */
961 if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
962 free_so (new);
963 else
964 {
965 int errcode;
966 char *buffer;
967
968 /* Extract this shared object's name. */
969 target_read_string (LM_NAME (new), &buffer,
970 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
971 if (errcode != 0)
972 {
973 warning ("current_sos: Can't read pathname for load map: %s\n",
974 safe_strerror (errcode));
975 }
976 else
977 {
978 strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
979 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
980 xfree (buffer);
981 strcpy (new->so_original_name, new->so_name);
982 }
983
984 /* If this entry has no name, or its name matches the name
985 for the main executable, don't include it in the list. */
986 if (! new->so_name[0]
987 || match_main (new->so_name))
988 free_so (new);
989 else
990 {
991 new->next = 0;
992 *link_ptr = new;
993 link_ptr = &new->next;
994 }
995 }
996
997 discard_cleanups (old_chain);
998 }
999
1000 return head;
1001 }
1002
1003
1004 /* On some systems, the only way to recognize the link map entry for
1005 the main executable file is by looking at its name. Return
1006 non-zero iff SONAME matches one of the known main executable names. */
1007
1008 static int
1009 match_main (char *soname)
1010 {
1011 char **mainp;
1012
1013 for (mainp = main_name_list; *mainp != NULL; mainp++)
1014 {
1015 if (strcmp (soname, *mainp) == 0)
1016 return (1);
1017 }
1018
1019 return (0);
1020 }
1021
1022
1023 /* Return 1 if PC lies in the dynamic symbol resolution code of the
1024 SVR4 run time loader. */
1025 #ifdef SVR4_SHARED_LIBS
1026 static CORE_ADDR interp_text_sect_low;
1027 static CORE_ADDR interp_text_sect_high;
1028 static CORE_ADDR interp_plt_sect_low;
1029 static CORE_ADDR interp_plt_sect_high;
1030
1031 static int
1032 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
1033 {
1034 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
1035 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
1036 || in_plt_section (pc, NULL));
1037 }
1038 #else /* !SVR4_SHARED_LIBS */
1039 static int
1040 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
1041 {
1042 return 0;
1043 }
1044 #endif /* SVR4_SHARED_LIBS */
1045
1046 /*
1047
1048 LOCAL FUNCTION
1049
1050 disable_break -- remove the "mapping changed" breakpoint
1051
1052 SYNOPSIS
1053
1054 static int disable_break ()
1055
1056 DESCRIPTION
1057
1058 Removes the breakpoint that gets hit when the dynamic linker
1059 completes a mapping change.
1060
1061 */
1062
1063 #ifndef SVR4_SHARED_LIBS
1064
1065 static int
1066 disable_break (void)
1067 {
1068 int status = 1;
1069
1070 int in_debugger = 0;
1071
1072 /* Read the debugger structure from the inferior to retrieve the
1073 address of the breakpoint and the original contents of the
1074 breakpoint address. Remove the breakpoint by writing the original
1075 contents back. */
1076
1077 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1078
1079 /* Set `in_debugger' to zero now. */
1080
1081 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1082
1083 breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr);
1084 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1085 sizeof (debug_copy.ldd_bp_inst));
1086
1087 /* For the SVR4 version, we always know the breakpoint address. For the
1088 SunOS version we don't know it until the above code is executed.
1089 Grumble if we are stopped anywhere besides the breakpoint address. */
1090
1091 if (stop_pc != breakpoint_addr)
1092 {
1093 warning ("stopped at unknown breakpoint while handling shared libraries");
1094 }
1095
1096 return (status);
1097 }
1098
1099 #endif /* #ifdef SVR4_SHARED_LIBS */
1100
1101 /*
1102
1103 LOCAL FUNCTION
1104
1105 enable_break -- arrange for dynamic linker to hit breakpoint
1106
1107 SYNOPSIS
1108
1109 int enable_break (void)
1110
1111 DESCRIPTION
1112
1113 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1114 debugger interface, support for arranging for the inferior to hit
1115 a breakpoint after mapping in the shared libraries. This function
1116 enables that breakpoint.
1117
1118 For SunOS, there is a special flag location (in_debugger) which we
1119 set to 1. When the dynamic linker sees this flag set, it will set
1120 a breakpoint at a location known only to itself, after saving the
1121 original contents of that place and the breakpoint address itself,
1122 in it's own internal structures. When we resume the inferior, it
1123 will eventually take a SIGTRAP when it runs into the breakpoint.
1124 We handle this (in a different place) by restoring the contents of
1125 the breakpointed location (which is only known after it stops),
1126 chasing around to locate the shared libraries that have been
1127 loaded, then resuming.
1128
1129 For SVR4, the debugger interface structure contains a member (r_brk)
1130 which is statically initialized at the time the shared library is
1131 built, to the offset of a function (_r_debug_state) which is guaran-
1132 teed to be called once before mapping in a library, and again when
1133 the mapping is complete. At the time we are examining this member,
1134 it contains only the unrelocated offset of the function, so we have
1135 to do our own relocation. Later, when the dynamic linker actually
1136 runs, it relocates r_brk to be the actual address of _r_debug_state().
1137
1138 The debugger interface structure also contains an enumeration which
1139 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1140 depending upon whether or not the library is being mapped or unmapped,
1141 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1142 */
1143
1144 static int
1145 enable_break (void)
1146 {
1147 int success = 0;
1148
1149 #ifndef SVR4_SHARED_LIBS
1150
1151 int j;
1152 int in_debugger;
1153
1154 /* Get link_dynamic structure */
1155
1156 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1157 sizeof (dynamic_copy));
1158 if (j)
1159 {
1160 /* unreadable */
1161 return (0);
1162 }
1163
1164 /* Calc address of debugger interface structure */
1165
1166 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1167
1168 /* Calc address of `in_debugger' member of debugger interface structure */
1169
1170 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1171 (char *) &debug_copy);
1172
1173 /* Write a value of 1 to this member. */
1174
1175 in_debugger = 1;
1176 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1177 success = 1;
1178
1179 #else /* SVR4_SHARED_LIBS */
1180
1181 #ifdef BKPT_AT_SYMBOL
1182
1183 struct minimal_symbol *msymbol;
1184 char **bkpt_namep;
1185 asection *interp_sect;
1186
1187 /* First, remove all the solib event breakpoints. Their addresses
1188 may have changed since the last time we ran the program. */
1189 remove_solib_event_breakpoints ();
1190
1191 #ifdef SVR4_SHARED_LIBS
1192 interp_text_sect_low = interp_text_sect_high = 0;
1193 interp_plt_sect_low = interp_plt_sect_high = 0;
1194
1195 /* Find the .interp section; if not found, warn the user and drop
1196 into the old breakpoint at symbol code. */
1197 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1198 if (interp_sect)
1199 {
1200 unsigned int interp_sect_size;
1201 char *buf;
1202 CORE_ADDR load_addr;
1203 bfd *tmp_bfd = NULL;
1204 int tmp_fd = -1;
1205 char *tmp_pathname = NULL;
1206 CORE_ADDR sym_addr = 0;
1207
1208 /* Read the contents of the .interp section into a local buffer;
1209 the contents specify the dynamic linker this program uses. */
1210 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1211 buf = alloca (interp_sect_size);
1212 bfd_get_section_contents (exec_bfd, interp_sect,
1213 buf, 0, interp_sect_size);
1214
1215 /* Now we need to figure out where the dynamic linker was
1216 loaded so that we can load its symbols and place a breakpoint
1217 in the dynamic linker itself.
1218
1219 This address is stored on the stack. However, I've been unable
1220 to find any magic formula to find it for Solaris (appears to
1221 be trivial on GNU/Linux). Therefore, we have to try an alternate
1222 mechanism to find the dynamic linker's base address. */
1223
1224 tmp_fd = solib_open (buf, &tmp_pathname);
1225 if (tmp_fd >= 0)
1226 tmp_bfd = bfd_fdopenr (tmp_pathname, gnutarget, tmp_fd);
1227
1228 if (tmp_bfd == NULL)
1229 goto bkpt_at_symbol;
1230
1231 /* Make sure the dynamic linker's really a useful object. */
1232 if (!bfd_check_format (tmp_bfd, bfd_object))
1233 {
1234 warning ("Unable to grok dynamic linker %s as an object file", buf);
1235 bfd_close (tmp_bfd);
1236 goto bkpt_at_symbol;
1237 }
1238
1239 /* We find the dynamic linker's base address by examining the
1240 current pc (which point at the entry point for the dynamic
1241 linker) and subtracting the offset of the entry point. */
1242 load_addr = read_pc () - tmp_bfd->start_address;
1243
1244 /* Record the relocated start and end address of the dynamic linker
1245 text and plt section for svr4_in_dynsym_resolve_code. */
1246 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
1247 if (interp_sect)
1248 {
1249 interp_text_sect_low =
1250 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1251 interp_text_sect_high =
1252 interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1253 }
1254 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1255 if (interp_sect)
1256 {
1257 interp_plt_sect_low =
1258 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1259 interp_plt_sect_high =
1260 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1261 }
1262
1263 /* Now try to set a breakpoint in the dynamic linker. */
1264 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1265 {
1266 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1267 if (sym_addr != 0)
1268 break;
1269 }
1270
1271 /* We're done with the temporary bfd. */
1272 bfd_close (tmp_bfd);
1273
1274 if (sym_addr != 0)
1275 {
1276 create_solib_event_breakpoint (load_addr + sym_addr);
1277 return 1;
1278 }
1279
1280 /* For whatever reason we couldn't set a breakpoint in the dynamic
1281 linker. Warn and drop into the old code. */
1282 bkpt_at_symbol:
1283 warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
1284 }
1285 #endif
1286
1287 /* Scan through the list of symbols, trying to look up the symbol and
1288 set a breakpoint there. Terminate loop when we/if we succeed. */
1289
1290 breakpoint_addr = 0;
1291 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1292 {
1293 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1294 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1295 {
1296 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1297 return 1;
1298 }
1299 }
1300
1301 /* Nothing good happened. */
1302 success = 0;
1303
1304 #endif /* BKPT_AT_SYMBOL */
1305
1306 #endif /* !SVR4_SHARED_LIBS */
1307
1308 return (success);
1309 }
1310
1311 /*
1312
1313 LOCAL FUNCTION
1314
1315 special_symbol_handling -- additional shared library symbol handling
1316
1317 SYNOPSIS
1318
1319 void special_symbol_handling ()
1320
1321 DESCRIPTION
1322
1323 Once the symbols from a shared object have been loaded in the usual
1324 way, we are called to do any system specific symbol handling that
1325 is needed.
1326
1327 For SunOS4, this consists of grunging around in the dynamic
1328 linkers structures to find symbol definitions for "common" symbols
1329 and adding them to the minimal symbol table for the runtime common
1330 objfile.
1331
1332 */
1333
1334 static void
1335 svr4_special_symbol_handling (void)
1336 {
1337 #ifndef SVR4_SHARED_LIBS
1338 int j;
1339
1340 if (debug_addr == 0)
1341 {
1342 /* Get link_dynamic structure */
1343
1344 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1345 sizeof (dynamic_copy));
1346 if (j)
1347 {
1348 /* unreadable */
1349 return;
1350 }
1351
1352 /* Calc address of debugger interface structure */
1353 /* FIXME, this needs work for cross-debugging of core files
1354 (byteorder, size, alignment, etc). */
1355
1356 debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd);
1357 }
1358
1359 /* Read the debugger structure from the inferior, just to make sure
1360 we have a current copy. */
1361
1362 j = target_read_memory (debug_addr, (char *) &debug_copy,
1363 sizeof (debug_copy));
1364 if (j)
1365 return; /* unreadable */
1366
1367 /* Get common symbol definitions for the loaded object. */
1368
1369 if (debug_copy.ldd_cp)
1370 {
1371 solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp));
1372 }
1373
1374 #endif /* !SVR4_SHARED_LIBS */
1375 }
1376
1377 /* Relocate the main executable. This function should be called upon
1378 stopping the inferior process at the entry point to the program.
1379 The entry point from BFD is compared to the PC and if they are
1380 different, the main executable is relocated by the proper amount.
1381
1382 As written it will only attempt to relocate executables which
1383 lack interpreter sections. It seems likely that only dynamic
1384 linker executables will get relocated, though it should work
1385 properly for a position-independent static executable as well. */
1386
1387 static void
1388 svr4_relocate_main_executable (void)
1389 {
1390 asection *interp_sect;
1391 CORE_ADDR pc = read_pc ();
1392
1393 /* Decide if the objfile needs to be relocated. As indicated above,
1394 we will only be here when execution is stopped at the beginning
1395 of the program. Relocation is necessary if the address at which
1396 we are presently stopped differs from the start address stored in
1397 the executable AND there's no interpreter section. The condition
1398 regarding the interpreter section is very important because if
1399 there *is* an interpreter section, execution will begin there
1400 instead. When there is an interpreter section, the start address
1401 is (presumably) used by the interpreter at some point to start
1402 execution of the program.
1403
1404 If there is an interpreter, it is normal for it to be set to an
1405 arbitrary address at the outset. The job of finding it is
1406 handled in enable_break().
1407
1408 So, to summarize, relocations are necessary when there is no
1409 interpreter section and the start address obtained from the
1410 executable is different from the address at which GDB is
1411 currently stopped.
1412
1413 [ The astute reader will note that we also test to make sure that
1414 the executable in question has the DYNAMIC flag set. It is my
1415 opinion that this test is unnecessary (undesirable even). It
1416 was added to avoid inadvertent relocation of an executable
1417 whose e_type member in the ELF header is not ET_DYN. There may
1418 be a time in the future when it is desirable to do relocations
1419 on other types of files as well in which case this condition
1420 should either be removed or modified to accomodate the new file
1421 type. (E.g, an ET_EXEC executable which has been built to be
1422 position-independent could safely be relocated by the OS if
1423 desired. It is true that this violates the ABI, but the ABI
1424 has been known to be bent from time to time.) - Kevin, Nov 2000. ]
1425 */
1426
1427 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1428 if (interp_sect == NULL
1429 && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0
1430 && bfd_get_start_address (exec_bfd) != pc)
1431 {
1432 struct cleanup *old_chain;
1433 struct section_offsets *new_offsets;
1434 int i, changed;
1435 CORE_ADDR displacement;
1436
1437 /* It is necessary to relocate the objfile. The amount to
1438 relocate by is simply the address at which we are stopped
1439 minus the starting address from the executable.
1440
1441 We relocate all of the sections by the same amount. This
1442 behavior is mandated by recent editions of the System V ABI.
1443 According to the System V Application Binary Interface,
1444 Edition 4.1, page 5-5:
1445
1446 ... Though the system chooses virtual addresses for
1447 individual processes, it maintains the segments' relative
1448 positions. Because position-independent code uses relative
1449 addressesing between segments, the difference between
1450 virtual addresses in memory must match the difference
1451 between virtual addresses in the file. The difference
1452 between the virtual address of any segment in memory and
1453 the corresponding virtual address in the file is thus a
1454 single constant value for any one executable or shared
1455 object in a given process. This difference is the base
1456 address. One use of the base address is to relocate the
1457 memory image of the program during dynamic linking.
1458
1459 The same language also appears in Edition 4.0 of the System V
1460 ABI and is left unspecified in some of the earlier editions. */
1461
1462 displacement = pc - bfd_get_start_address (exec_bfd);
1463 changed = 0;
1464
1465 new_offsets = xcalloc (sizeof (struct section_offsets),
1466 symfile_objfile->num_sections);
1467 old_chain = make_cleanup (xfree, new_offsets);
1468
1469 for (i = 0; i < symfile_objfile->num_sections; i++)
1470 {
1471 if (displacement != ANOFFSET (symfile_objfile->section_offsets, i))
1472 changed = 1;
1473 new_offsets->offsets[i] = displacement;
1474 }
1475
1476 if (changed)
1477 objfile_relocate (symfile_objfile, new_offsets);
1478
1479 do_cleanups (old_chain);
1480 }
1481 }
1482
1483 /*
1484
1485 GLOBAL FUNCTION
1486
1487 svr4_solib_create_inferior_hook -- shared library startup support
1488
1489 SYNOPSIS
1490
1491 void svr4_solib_create_inferior_hook()
1492
1493 DESCRIPTION
1494
1495 When gdb starts up the inferior, it nurses it along (through the
1496 shell) until it is ready to execute it's first instruction. At this
1497 point, this function gets called via expansion of the macro
1498 SOLIB_CREATE_INFERIOR_HOOK.
1499
1500 For SunOS executables, this first instruction is typically the
1501 one at "_start", or a similar text label, regardless of whether
1502 the executable is statically or dynamically linked. The runtime
1503 startup code takes care of dynamically linking in any shared
1504 libraries, once gdb allows the inferior to continue.
1505
1506 For SVR4 executables, this first instruction is either the first
1507 instruction in the dynamic linker (for dynamically linked
1508 executables) or the instruction at "start" for statically linked
1509 executables. For dynamically linked executables, the system
1510 first exec's /lib/libc.so.N, which contains the dynamic linker,
1511 and starts it running. The dynamic linker maps in any needed
1512 shared libraries, maps in the actual user executable, and then
1513 jumps to "start" in the user executable.
1514
1515 For both SunOS shared libraries, and SVR4 shared libraries, we
1516 can arrange to cooperate with the dynamic linker to discover the
1517 names of shared libraries that are dynamically linked, and the
1518 base addresses to which they are linked.
1519
1520 This function is responsible for discovering those names and
1521 addresses, and saving sufficient information about them to allow
1522 their symbols to be read at a later time.
1523
1524 FIXME
1525
1526 Between enable_break() and disable_break(), this code does not
1527 properly handle hitting breakpoints which the user might have
1528 set in the startup code or in the dynamic linker itself. Proper
1529 handling will probably have to wait until the implementation is
1530 changed to use the "breakpoint handler function" method.
1531
1532 Also, what if child has exit()ed? Must exit loop somehow.
1533 */
1534
1535 static void
1536 svr4_solib_create_inferior_hook (void)
1537 {
1538 /* Relocate the main executable if necessary. */
1539 svr4_relocate_main_executable ();
1540
1541 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1542 yet. In fact, in the case of a SunOS4 executable being run on
1543 Solaris, we can't get it yet. current_sos will get it when it needs
1544 it. */
1545 #if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1546 if ((debug_base = locate_base ()) == 0)
1547 {
1548 /* Can't find the symbol or the executable is statically linked. */
1549 return;
1550 }
1551 #endif
1552
1553 if (!enable_break ())
1554 {
1555 warning ("shared library handler failed to enable breakpoint");
1556 return;
1557 }
1558
1559 #if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS)
1560 /* SCO and SunOS need the loop below, other systems should be using the
1561 special shared library breakpoints and the shared library breakpoint
1562 service routine.
1563
1564 Now run the target. It will eventually hit the breakpoint, at
1565 which point all of the libraries will have been mapped in and we
1566 can go groveling around in the dynamic linker structures to find
1567 out what we need to know about them. */
1568
1569 clear_proceed_status ();
1570 stop_soon_quietly = 1;
1571 stop_signal = TARGET_SIGNAL_0;
1572 do
1573 {
1574 target_resume (-1, 0, stop_signal);
1575 wait_for_inferior ();
1576 }
1577 while (stop_signal != TARGET_SIGNAL_TRAP);
1578 stop_soon_quietly = 0;
1579
1580 #if !defined(_SCO_DS)
1581 /* We are now either at the "mapping complete" breakpoint (or somewhere
1582 else, a condition we aren't prepared to deal with anyway), so adjust
1583 the PC as necessary after a breakpoint, disable the breakpoint, and
1584 add any shared libraries that were mapped in. */
1585
1586 if (DECR_PC_AFTER_BREAK)
1587 {
1588 stop_pc -= DECR_PC_AFTER_BREAK;
1589 write_register (PC_REGNUM, stop_pc);
1590 }
1591
1592 if (!disable_break ())
1593 {
1594 warning ("shared library handler failed to disable breakpoint");
1595 }
1596
1597 if (auto_solib_add)
1598 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1599 #endif /* ! _SCO_DS */
1600 #endif
1601 }
1602
1603 static void
1604 svr4_clear_solib (void)
1605 {
1606 debug_base = 0;
1607 }
1608
1609 static void
1610 svr4_free_so (struct so_list *so)
1611 {
1612 xfree (so->lm_info->lm);
1613 xfree (so->lm_info);
1614 }
1615
1616 static void
1617 svr4_relocate_section_addresses (struct so_list *so,
1618 struct section_table *sec)
1619 {
1620 sec->addr += LM_ADDR (so);
1621 sec->endaddr += LM_ADDR (so);
1622 }
1623
1624 void
1625 set_solib_svr4_fetch_link_map_offsets (struct link_map_offsets *(*flmo) (void))
1626 {
1627 fetch_link_map_offsets = flmo;
1628 }
1629
1630 static void
1631 init_fetch_link_map_offsets (void)
1632 {
1633 set_solib_svr4_fetch_link_map_offsets (default_svr4_fetch_link_map_offsets);
1634 }
1635
1636 static struct target_so_ops svr4_so_ops;
1637
1638 void
1639 _initialize_svr4_solib (void)
1640 {
1641 register_gdbarch_swap (&fetch_link_map_offsets,
1642 sizeof (fetch_link_map_offsets),
1643 init_fetch_link_map_offsets);
1644
1645 svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
1646 svr4_so_ops.free_so = svr4_free_so;
1647 svr4_so_ops.clear_solib = svr4_clear_solib;
1648 svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
1649 svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
1650 svr4_so_ops.current_sos = svr4_current_sos;
1651 svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
1652 svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
1653
1654 /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */
1655 current_target_so_ops = &svr4_so_ops;
1656 }
1657
This page took 0.064621 seconds and 4 git commands to generate.