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