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