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