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