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