Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / contrib / gdb / gdb / solib.c
CommitLineData
984263bc
MD
1/* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 98, 1999
3 Free Software Foundation, Inc.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* $FreeBSD: src/contrib/gdb/gdb/solib.c,v 1.7 1999/11/22 18:04:53 peter Exp $ */
1de703da 22/* $DragonFly: src/contrib/gdb/gdb/Attic/solib.c,v 1.2 2003/06/17 04:24:02 dillon Exp $ */
984263bc
MD
23
24#include "defs.h"
25
26/* This file is only compilable if link.h is available. */
27
28#ifdef HAVE_LINK_H
29
30#include <sys/types.h>
31#include <signal.h>
32#include "gdb_string.h"
33#include <sys/param.h>
34#include <fcntl.h>
35#include <unistd.h>
36
37#ifndef SVR4_SHARED_LIBS
38 /* SunOS shared libs need the nlist structure. */
39#include <a.out.h>
40#else
41#include "elf/external.h"
42#include "elf/common.h"
43#endif
44
45#include <link.h>
46
47#include "symtab.h"
48#include "bfd.h"
49#include "symfile.h"
50#include "objfiles.h"
51#include "gdbcore.h"
52#include "command.h"
53#include "target.h"
54#include "frame.h"
55#include "gnu-regex.h"
56#include "inferior.h"
57#include "environ.h"
58#include "language.h"
59#include "gdbcmd.h"
60
61#define MAX_PATH_SIZE 512 /* FIXME: Should be dynamic */
62
63/* On SVR4 systems, a list of symbols in the dynamic linker where
64 GDB can try to place a breakpoint to monitor shared library
65 events.
66
67 If none of these symbols are found, or other errors occur, then
68 SVR4 systems will fall back to using a symbol as the "startup
69 mapping complete" breakpoint address. */
70
71#ifdef SVR4_SHARED_LIBS
72static char *solib_break_names[] = {
73 "r_debug_state",
74 "_r_debug_state",
75 "_dl_debug_state",
76 "rtld_db_dlactivity",
77 NULL
78};
79#endif
80
81#define BKPT_AT_SYMBOL 1
82
83#if defined (BKPT_AT_SYMBOL) && defined (SVR4_SHARED_LIBS)
84static char *bkpt_names[] = {
85#ifdef SOLIB_BKPT_NAME
86 SOLIB_BKPT_NAME, /* Prefer configured name if it exists. */
87#endif
88 "_start",
89 "main",
90 NULL
91};
92#endif
93
94/* Symbols which are used to locate the base of the link map structures. */
95
96#ifndef SVR4_SHARED_LIBS
97static char *debug_base_symbols[] = {
98 "_DYNAMIC",
99 "_DYNAMIC__MGC",
100 NULL
101};
102#endif
103
104static char *main_name_list[] = {
105 "main_$main",
106 NULL
107};
108
109/* local data declarations */
110
111#ifndef SVR4_SHARED_LIBS
112
113#define LM_ADDR(so) ((so) -> lm.lm_addr)
114#define LM_NEXT(so) ((so) -> lm.lm_next)
115#define LM_NAME(so) ((so) -> lm.lm_name)
116/* Test for first link map entry; first entry is a shared library. */
117#define IGNORE_FIRST_LINK_MAP_ENTRY(x) (0)
118static struct link_dynamic dynamic_copy;
119static struct link_dynamic_2 ld_2_copy;
120static struct ld_debug debug_copy;
121static CORE_ADDR debug_addr;
122static CORE_ADDR flag_addr;
123
124#else /* SVR4_SHARED_LIBS */
125
126#define LM_ADDR(so) ((so) -> lm.l_addr)
127#define LM_NEXT(so) ((so) -> lm.l_next)
128#define LM_NAME(so) ((so) -> lm.l_name)
129/* Test for first link map entry; first entry is the exec-file. */
130#define IGNORE_FIRST_LINK_MAP_ENTRY(x) ((x).l_prev == NULL)
131static struct r_debug debug_copy;
132char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
133
134#endif /* !SVR4_SHARED_LIBS */
135
136struct so_list {
137 struct so_list *next; /* next structure in linked list */
138 struct link_map lm; /* copy of link map from inferior */
139 struct link_map *lmaddr; /* addr in inferior lm was read from */
140 CORE_ADDR lmend; /* upper addr bound of mapped object */
141 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
142 char symbols_loaded; /* flag: symbols read in yet? */
143 char from_tty; /* flag: print msgs? */
144 struct objfile *objfile; /* objfile for loaded lib */
145 struct section_table *sections;
146 struct section_table *sections_end;
147 struct section_table *textsection;
148 bfd *abfd;
149};
150
151static struct so_list *so_list_head; /* List of known shared objects */
152static CORE_ADDR debug_base; /* Base of dynamic linker structures */
153static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
154
155static int solib_cleanup_queued = 0; /* make_run_cleanup called */
156
157extern int
158fdmatch PARAMS ((int, int)); /* In libiberty */
159
160/* Local function prototypes */
161
162static void
163do_clear_solib PARAMS ((PTR));
164
165static int
166match_main PARAMS ((char *));
167
168static void
169special_symbol_handling PARAMS ((struct so_list *));
170
171static void
172sharedlibrary_command PARAMS ((char *, int));
173
174static int
175enable_break PARAMS ((void));
176
177static void
178info_sharedlibrary_command PARAMS ((char *, int));
179
180static int symbol_add_stub PARAMS ((PTR));
181
182static struct so_list *
183alloc_solib PARAMS ((struct link_map *));
184
185static void
186free_solib PARAMS ((struct so_list *));
187
188static struct so_list *
189find_solib PARAMS ((struct so_list *, int maybe_changed));
190
191static struct link_map *
192first_link_map_member PARAMS ((void));
193
194static CORE_ADDR
195locate_base PARAMS ((void));
196
197static int solib_map_sections PARAMS ((PTR));
198
199#ifdef SVR4_SHARED_LIBS
200
201static CORE_ADDR
202elf_locate_base PARAMS ((void));
203
204#else
205
206static int
207disable_break PARAMS ((void));
208
209static void
210allocate_rt_common_objfile PARAMS ((void));
211
212static void
213solib_add_common_symbols PARAMS ((struct rtc_symb *));
214
215#endif
216
217void _initialize_solib PARAMS ((void));
218
219/* If non-zero, this is a prefix that will be added to the front of the name
220 shared libraries with an absolute filename for loading. */
221static char *solib_absolute_prefix = NULL;
222
223/* If non-empty, this is a search path for loading non-absolute shared library
224 symbol files. This takes precedence over the environment variables PATH
225 and LD_LIBRARY_PATH. */
226static char *solib_search_path = NULL;
227
228/*
229
230LOCAL FUNCTION
231
232 solib_map_sections -- open bfd and build sections for shared lib
233
234SYNOPSIS
235
236 static int solib_map_sections (struct so_list *so)
237
238DESCRIPTION
239
240 Given a pointer to one of the shared objects in our list
241 of mapped objects, use the recorded name to open a bfd
242 descriptor for the object, build a section table, and then
243 relocate all the section addresses by the base address at
244 which the shared object was mapped.
245
246FIXMES
247
248 In most (all?) cases the shared object file name recorded in the
249 dynamic linkage tables will be a fully qualified pathname. For
250 cases where it isn't, do we really mimic the systems search
251 mechanism correctly in the below code (particularly the tilde
252 expansion stuff?).
253 */
254
255static int
256solib_map_sections (arg)
257 PTR arg;
258{
259 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
260 char *filename;
261 char *scratch_pathname;
262 int scratch_chan;
263 struct section_table *p;
264 struct cleanup *old_chain;
265 bfd *abfd;
266
267 filename = tilde_expand (so -> so_name);
268
269 if (solib_absolute_prefix && ROOTED_P (filename))
270 /* Prefix shared libraries with absolute filenames with
271 SOLIB_ABSOLUTE_PREFIX. */
272 {
273 char *pfxed_fn;
274 int pfx_len;
275
276 pfx_len = strlen (solib_absolute_prefix);
277
278 /* Remove trailing slashes. */
279 while (pfx_len > 0 && SLASH_P (solib_absolute_prefix[pfx_len - 1]))
280 pfx_len--;
281
282 pfxed_fn = xmalloc (pfx_len + strlen (filename) + 1);
283 strcpy (pfxed_fn, solib_absolute_prefix);
284 strcat (pfxed_fn, filename);
285 free (filename);
286
287 filename = pfxed_fn;
288 }
289
290 old_chain = make_cleanup (free, filename);
291
292 scratch_chan = -1;
293
294 if (solib_search_path)
295 scratch_chan = openp (solib_search_path,
296 1, filename, O_RDONLY, 0, &scratch_pathname);
297 if (scratch_chan < 0)
298 scratch_chan = openp (get_in_environ (inferior_environ, "PATH"),
299 1, filename, O_RDONLY, 0, &scratch_pathname);
300 if (scratch_chan < 0)
301 {
302 scratch_chan = openp (get_in_environ
303 (inferior_environ, "LD_LIBRARY_PATH"),
304 1, filename, O_RDONLY, 0, &scratch_pathname);
305 }
306 if (scratch_chan < 0)
307 {
308 perror_with_name (filename);
309 }
310 /* Leave scratch_pathname allocated. abfd->name will point to it. */
311
312 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
313 if (!abfd)
314 {
315 close (scratch_chan);
316 error ("Could not open `%s' as an executable file: %s",
317 scratch_pathname, bfd_errmsg (bfd_get_error ()));
318 }
319 /* Leave bfd open, core_xfer_memory and "info files" need it. */
320 so -> abfd = abfd;
321 abfd -> cacheable = true;
322
323 /* copy full path name into so_name, so that later symbol_file_add can find
324 it */
325 if (strlen (scratch_pathname) >= MAX_PATH_SIZE)
326 error ("Full path name length of shared library exceeds MAX_PATH_SIZE in so_list structure.");
327 strcpy (so->so_name, scratch_pathname);
328
329 if (!bfd_check_format (abfd, bfd_object))
330 {
331 error ("\"%s\": not in executable format: %s.",
332 scratch_pathname, bfd_errmsg (bfd_get_error ()));
333 }
334 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
335 {
336 error ("Can't find the file sections in `%s': %s",
337 bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
338 }
339
340 for (p = so -> sections; p < so -> sections_end; p++)
341 {
342 /* Relocate the section binding addresses as recorded in the shared
343 object's file by the base address to which the object was actually
344 mapped. */
345 p -> addr += (CORE_ADDR) LM_ADDR (so);
346 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
347 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
348 if (STREQ (p -> the_bfd_section -> name, ".text"))
349 {
350 so -> textsection = p;
351 }
352 }
353
354 /* Free the file names, close the file now. */
355 do_cleanups (old_chain);
356
357 return (1);
358}
359
360#ifndef SVR4_SHARED_LIBS
361
362/* Allocate the runtime common object file. */
363
364static void
365allocate_rt_common_objfile ()
366{
367 struct objfile *objfile;
368 struct objfile *last_one;
369
370 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
371 memset (objfile, 0, sizeof (struct objfile));
372 objfile -> md = NULL;
373 obstack_specify_allocation (&objfile -> psymbol_cache.cache, 0, 0,
374 xmalloc, free);
375 obstack_specify_allocation (&objfile -> psymbol_obstack, 0, 0, xmalloc,
376 free);
377 obstack_specify_allocation (&objfile -> symbol_obstack, 0, 0, xmalloc,
378 free);
379 obstack_specify_allocation (&objfile -> type_obstack, 0, 0, xmalloc,
380 free);
381 objfile -> name = mstrsave (objfile -> md, "rt_common");
382
383 /* Add this file onto the tail of the linked list of other such files. */
384
385 objfile -> next = NULL;
386 if (object_files == NULL)
387 object_files = objfile;
388 else
389 {
390 for (last_one = object_files;
391 last_one -> next;
392 last_one = last_one -> next);
393 last_one -> next = objfile;
394 }
395
396 rt_common_objfile = objfile;
397}
398
399/* Read all dynamically loaded common symbol definitions from the inferior
400 and put them into the minimal symbol table for the runtime common
401 objfile. */
402
403static void
404solib_add_common_symbols (rtc_symp)
405 struct rtc_symb *rtc_symp;
406{
407 struct rtc_symb inferior_rtc_symb;
408 struct nlist inferior_rtc_nlist;
409 int len;
410 char *name;
411
412 /* Remove any runtime common symbols from previous runs. */
413
414 if (rt_common_objfile != NULL && rt_common_objfile -> minimal_symbol_count)
415 {
416 obstack_free (&rt_common_objfile -> symbol_obstack, 0);
417 obstack_specify_allocation (&rt_common_objfile -> symbol_obstack, 0, 0,
418 xmalloc, free);
419 rt_common_objfile -> minimal_symbol_count = 0;
420 rt_common_objfile -> msymbols = NULL;
421 }
422
423 init_minimal_symbol_collection ();
424 make_cleanup ((make_cleanup_func) discard_minimal_symbols, 0);
425
426 while (rtc_symp)
427 {
428 read_memory ((CORE_ADDR) rtc_symp,
429 (char *) &inferior_rtc_symb,
430 sizeof (inferior_rtc_symb));
431 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
432 (char *) &inferior_rtc_nlist,
433 sizeof(inferior_rtc_nlist));
434 if (inferior_rtc_nlist.n_type == N_COMM)
435 {
436 /* FIXME: The length of the symbol name is not available, but in the
437 current implementation the common symbol is allocated immediately
438 behind the name of the symbol. */
439 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
440
441 name = xmalloc (len);
442 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
443
444 /* Allocate the runtime common objfile if necessary. */
445 if (rt_common_objfile == NULL)
446 allocate_rt_common_objfile ();
447
448 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
449 mst_bss, rt_common_objfile);
450 free (name);
451 }
452 rtc_symp = inferior_rtc_symb.rtc_next;
453 }
454
455 /* Install any minimal symbols that have been collected as the current
456 minimal symbols for the runtime common objfile. */
457
458 install_minimal_symbols (rt_common_objfile);
459}
460
461#endif /* SVR4_SHARED_LIBS */
462
463
464#ifdef SVR4_SHARED_LIBS
465
466static CORE_ADDR
467bfd_lookup_symbol PARAMS ((bfd *, char *));
468
469/*
470
471LOCAL FUNCTION
472
473 bfd_lookup_symbol -- lookup the value for a specific symbol
474
475SYNOPSIS
476
477 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
478
479DESCRIPTION
480
481 An expensive way to lookup the value of a single symbol for
482 bfd's that are only temporary anyway. This is used by the
483 shared library support to find the address of the debugger
484 interface structures in the shared library.
485
486 Note that 0 is specifically allowed as an error return (no
487 such symbol).
488*/
489
490static CORE_ADDR
491bfd_lookup_symbol (abfd, symname)
492 bfd *abfd;
493 char *symname;
494{
495 long storage_needed;
496 asymbol *sym;
497 asymbol **symbol_table;
498 unsigned int number_of_symbols;
499 unsigned int i;
500 struct cleanup *back_to;
501 CORE_ADDR symaddr = 0;
502
503 storage_needed = bfd_get_symtab_upper_bound (abfd);
504
505 if (storage_needed > 0)
506 {
507 symbol_table = (asymbol **) xmalloc (storage_needed);
508 back_to = make_cleanup (free, (PTR)symbol_table);
509 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
510
511 for (i = 0; i < number_of_symbols; i++)
512 {
513 sym = *symbol_table++;
514 if (STREQ (sym -> name, symname))
515 {
516 /* Bfd symbols are section relative. */
517 symaddr = sym -> value + sym -> section -> vma;
518 break;
519 }
520 }
521 do_cleanups (back_to);
522 }
523
524 if (symaddr) return (symaddr);
525
526 storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
527
528 if (storage_needed > 0)
529 {
530 symbol_table = (asymbol **) xmalloc (storage_needed);
531 back_to = make_cleanup (free, (PTR)symbol_table);
532 number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
533
534 for (i = 0; i < number_of_symbols; i++)
535 {
536 sym = *symbol_table++;
537 if (STREQ (sym -> name, symname))
538 {
539 /* Bfd symbols are section relative. */
540 symaddr = sym -> value + sym -> section -> vma;
541 break;
542 }
543 }
544 do_cleanups (back_to);
545 }
546 return (symaddr);
547}
548
549#ifdef HANDLE_SVR4_EXEC_EMULATORS
550
551/*
552 Solaris BCP (the part of Solaris which allows it to run SunOS4
553 a.out files) throws in another wrinkle. Solaris does not fill
554 in the usual a.out link map structures when running BCP programs,
555 the only way to get at them is via groping around in the dynamic
556 linker.
557 The dynamic linker and it's structures are located in the shared
558 C library, which gets run as the executable's "interpreter" by
559 the kernel.
560
561 Note that we can assume nothing about the process state at the time
562 we need to find these structures. We may be stopped on the first
563 instruction of the interpreter (C shared library), the first
564 instruction of the executable itself, or somewhere else entirely
565 (if we attached to the process for example).
566*/
567
568static char *debug_base_symbols[] = {
569 "r_debug", /* Solaris 2.3 */
570 "_r_debug", /* Solaris 2.1, 2.2 */
571 NULL
572};
573
574static int
575look_for_base PARAMS ((int, CORE_ADDR));
576
577/*
578
579LOCAL FUNCTION
580
581 look_for_base -- examine file for each mapped address segment
582
583SYNOPSYS
584
585 static int look_for_base (int fd, CORE_ADDR baseaddr)
586
587DESCRIPTION
588
589 This function is passed to proc_iterate_over_mappings, which
590 causes it to get called once for each mapped address space, with
591 an open file descriptor for the file mapped to that space, and the
592 base address of that mapped space.
593
594 Our job is to find the debug base symbol in the file that this
595 fd is open on, if it exists, and if so, initialize the dynamic
596 linker structure base address debug_base.
597
598 Note that this is a computationally expensive proposition, since
599 we basically have to open a bfd on every call, so we specifically
600 avoid opening the exec file.
601 */
602
603static int
604look_for_base (fd, baseaddr)
605 int fd;
606 CORE_ADDR baseaddr;
607{
608 bfd *interp_bfd;
609 CORE_ADDR address = 0;
610 char **symbolp;
611
612 /* If the fd is -1, then there is no file that corresponds to this
613 mapped memory segment, so skip it. Also, if the fd corresponds
614 to the exec file, skip it as well. */
615
616 if (fd == -1
617 || (exec_bfd != NULL
618 && fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd)))
619 {
620 return (0);
621 }
622
623 /* Try to open whatever random file this fd corresponds to. Note that
624 we have no way currently to find the filename. Don't gripe about
625 any problems we might have, just fail. */
626
627 if ((interp_bfd = bfd_fdopenr ("unnamed", gnutarget, fd)) == NULL)
628 {
629 return (0);
630 }
631 if (!bfd_check_format (interp_bfd, bfd_object))
632 {
633 /* FIXME-leak: on failure, might not free all memory associated with
634 interp_bfd. */
635 bfd_close (interp_bfd);
636 return (0);
637 }
638
639 /* Now try to find our debug base symbol in this file, which we at
640 least know to be a valid ELF executable or shared library. */
641
642 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
643 {
644 address = bfd_lookup_symbol (interp_bfd, *symbolp);
645 if (address != 0)
646 {
647 break;
648 }
649 }
650 if (address == 0)
651 {
652 /* FIXME-leak: on failure, might not free all memory associated with
653 interp_bfd. */
654 bfd_close (interp_bfd);
655 return (0);
656 }
657
658 /* Eureka! We found the symbol. But now we may need to relocate it
659 by the base address. If the symbol's value is less than the base
660 address of the shared library, then it hasn't yet been relocated
661 by the dynamic linker, and we have to do it ourself. FIXME: Note
662 that we make the assumption that the first segment that corresponds
663 to the shared library has the base address to which the library
664 was relocated. */
665
666 if (address < baseaddr)
667 {
668 address += baseaddr;
669 }
670 debug_base = address;
671 /* FIXME-leak: on failure, might not free all memory associated with
672 interp_bfd. */
673 bfd_close (interp_bfd);
674 return (1);
675}
676#endif /* HANDLE_SVR4_EXEC_EMULATORS */
677
678/*
679
680LOCAL FUNCTION
681
682 elf_locate_base -- locate the base address of dynamic linker structs
683 for SVR4 elf targets.
684
685SYNOPSIS
686
687 CORE_ADDR elf_locate_base (void)
688
689DESCRIPTION
690
691 For SVR4 elf targets the address of the dynamic linker's runtime
692 structure is contained within the dynamic info section in the
693 executable file. The dynamic section is also mapped into the
694 inferior address space. Because the runtime loader fills in the
695 real address before starting the inferior, we have to read in the
696 dynamic info section from the inferior address space.
697 If there are any errors while trying to find the address, we
698 silently return 0, otherwise the found address is returned.
699
700 */
701
702static CORE_ADDR
703elf_locate_base ()
704{
705 sec_ptr dyninfo_sect;
706 int dyninfo_sect_size;
707 CORE_ADDR dyninfo_addr;
708 char *buf;
709 char *bufend;
710
711 /* Find the start address of the .dynamic section. */
712 dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
713 if (dyninfo_sect == NULL)
714 return 0;
715 dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
716
717 /* Read in .dynamic section, silently ignore errors. */
718 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
719 buf = alloca (dyninfo_sect_size);
720 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
721 return 0;
722
723 /* Find the DT_DEBUG entry in the the .dynamic section.
724 For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
725 no DT_DEBUG entries. */
726#ifndef TARGET_ELF64
727 for (bufend = buf + dyninfo_sect_size;
728 buf < bufend;
729 buf += sizeof (Elf32_External_Dyn))
730 {
731 Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *)buf;
732 long dyn_tag;
733 CORE_ADDR dyn_ptr;
734
735 dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
736 if (dyn_tag == DT_NULL)
737 break;
738 else if (dyn_tag == DT_DEBUG)
739 {
740 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
741 return dyn_ptr;
742 }
743#ifdef DT_MIPS_RLD_MAP
744 else if (dyn_tag == DT_MIPS_RLD_MAP)
745 {
746 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
747
748 /* DT_MIPS_RLD_MAP contains a pointer to the address
749 of the dynamic link structure. */
750 dyn_ptr = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
751 if (target_read_memory (dyn_ptr, pbuf, sizeof (pbuf)))
752 return 0;
753 return extract_unsigned_integer (pbuf, sizeof (pbuf));
754 }
755#endif
756 }
757#else /* ELF64 */
758 for (bufend = buf + dyninfo_sect_size;
759 buf < bufend;
760 buf += sizeof (Elf64_External_Dyn))
761 {
762 Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *)buf;
763 long dyn_tag;
764 CORE_ADDR dyn_ptr;
765
766 dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
767 if (dyn_tag == DT_NULL)
768 break;
769 else if (dyn_tag == DT_DEBUG)
770 {
771 dyn_ptr = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_un.d_ptr);
772 return dyn_ptr;
773 }
774 }
775#endif
776
777 /* DT_DEBUG entry not found. */
778 return 0;
779}
780
781#endif /* SVR4_SHARED_LIBS */
782
783/*
784
785LOCAL FUNCTION
786
787 locate_base -- locate the base address of dynamic linker structs
788
789SYNOPSIS
790
791 CORE_ADDR locate_base (void)
792
793DESCRIPTION
794
795 For both the SunOS and SVR4 shared library implementations, if the
796 inferior executable has been linked dynamically, there is a single
797 address somewhere in the inferior's data space which is the key to
798 locating all of the dynamic linker's runtime structures. This
799 address is the value of the debug base symbol. The job of this
800 function is to find and return that address, or to return 0 if there
801 is no such address (the executable is statically linked for example).
802
803 For SunOS, the job is almost trivial, since the dynamic linker and
804 all of it's structures are statically linked to the executable at
805 link time. Thus the symbol for the address we are looking for has
806 already been added to the minimal symbol table for the executable's
807 objfile at the time the symbol file's symbols were read, and all we
808 have to do is look it up there. Note that we explicitly do NOT want
809 to find the copies in the shared library.
810
811 The SVR4 version is a bit more complicated because the address
812 is contained somewhere in the dynamic info section. We have to go
813 to a lot more work to discover the address of the debug base symbol.
814 Because of this complexity, we cache the value we find and return that
815 value on subsequent invocations. Note there is no copy in the
816 executable symbol tables.
817
818 */
819
820static CORE_ADDR
821locate_base ()
822{
823
824#ifndef SVR4_SHARED_LIBS
825
826 struct minimal_symbol *msymbol;
827 CORE_ADDR address = 0;
828 char **symbolp;
829
830 /* For SunOS, we want to limit the search for the debug base symbol to the
831 executable being debugged, since there is a duplicate named symbol in the
832 shared library. We don't want the shared library versions. */
833
834 for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++)
835 {
836 msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile);
837 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
838 {
839 address = SYMBOL_VALUE_ADDRESS (msymbol);
840 return (address);
841 }
842 }
843 return (0);
844
845#else /* SVR4_SHARED_LIBS */
846
847 /* Check to see if we have a currently valid address, and if so, avoid
848 doing all this work again and just return the cached address. If
849 we have no cached address, try to locate it in the dynamic info
850 section for ELF executables. */
851
852 if (debug_base == 0)
853 {
854 if (exec_bfd != NULL
855 && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
856 debug_base = elf_locate_base ();
857#ifdef HANDLE_SVR4_EXEC_EMULATORS
858 /* Try it the hard way for emulated executables. */
859 else if (inferior_pid != 0 && target_has_execution)
860 proc_iterate_over_mappings (look_for_base);
861#endif
862 }
863 return (debug_base);
864
865#endif /* !SVR4_SHARED_LIBS */
866
867}
868
869/*
870
871LOCAL FUNCTION
872
873 first_link_map_member -- locate first member in dynamic linker's map
874
875SYNOPSIS
876
877 static struct link_map *first_link_map_member (void)
878
879DESCRIPTION
880
881 Read in a copy of the first member in the inferior's dynamic
882 link map from the inferior's dynamic linker structures, and return
883 a pointer to the copy in our address space.
884*/
885
886static struct link_map *
887first_link_map_member ()
888{
889 struct link_map *lm = NULL;
890
891#ifndef SVR4_SHARED_LIBS
892
893 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
894 if (dynamic_copy.ld_version >= 2)
895 {
896 /* It is a version that we can deal with, so read in the secondary
897 structure and find the address of the link map list from it. */
898 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
899 sizeof (struct link_dynamic_2));
900 lm = ld_2_copy.ld_loaded;
901 }
902
903#else /* SVR4_SHARED_LIBS */
904
905 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
906 /* FIXME: Perhaps we should validate the info somehow, perhaps by
907 checking r_version for a known version number, or r_state for
908 RT_CONSISTENT. */
909 lm = debug_copy.r_map;
910
911#endif /* !SVR4_SHARED_LIBS */
912
913 return (lm);
914}
915
916/*
917
918LOCAL FUNCTION
919
920 free_solib -- free a so_list structure
921
922SYNOPSIS
923
924 void free_solib (struct so_list *so_list_ptr)
925
926DESCRIPTION
927
928 Free the memory used by a struct so_list.
929
930 */
931
932void
933free_solib (so)
934 struct so_list *so;
935{
936 char *bfd_filename;
937 if (so -> sections)
938 {
939 free ((PTR)so -> sections);
940 }
941 if (so -> abfd)
942 {
943 bfd_filename = bfd_get_filename (so -> abfd);
944 if (!bfd_close (so -> abfd))
945 warning ("cannot close \"%s\": %s",
946 bfd_filename, bfd_errmsg (bfd_get_error ()));
947 }
948 else
949 /* This happens for the executable on SVR4. */
950 bfd_filename = NULL;
951
952 if (bfd_filename)
953 free ((PTR)bfd_filename);
954 free ((PTR)so);
955}
956
957/*
958
959LOCAL FUNCTION
960
961 alloc_solib -- free a so_list structure
962
963SYNOPSIS
964
965 struct so_list *alloc_solib (struct link_map *lm)
966
967DESCRIPTION
968
969 Allocate the struct so_list to cache debugging information
970 for a struct link_map entry in the target.
971
972 */
973
974struct so_list *
975alloc_solib (lm)
976 struct link_map *lm;
977{
978 struct so_list *new;
979
980 /* Get next link map structure from inferior image and build a local
981 abbreviated load_map structure */
982 new = (struct so_list *) xmalloc (sizeof (struct so_list));
983 memset ((char *) new, 0, sizeof (struct so_list));
984 new -> lmaddr = lm;
985
986 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
987 sizeof (struct link_map));
988 /* For SVR4 versions, the first entry in the link map is for the
989 inferior executable, so we must ignore it. For some versions of
990 SVR4, it has no name. For others (Solaris 2.3 for example), it
991 does have a name, so we can no longer use a missing name to
992 decide when to ignore it. */
993 if (!IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
994 {
995 int errcode;
996 char *buffer;
997 target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
998 MAX_PATH_SIZE - 1, &errcode);
999 if (errcode != 0)
1000 error ("find_solib: Can't read pathname for load map: %s\n",
1001 safe_strerror (errcode));
1002 strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
1003 new -> so_name[MAX_PATH_SIZE - 1] = '\0';
1004 free (buffer);
1005 solib_map_sections (new);
1006 }
1007
1008 return new;
1009}
1010
1011/*
1012
1013LOCAL FUNCTION
1014
1015 find_solib -- step through list of shared objects
1016
1017SYNOPSIS
1018
1019 struct so_list *find_solib (struct so_list *so_list_ptr, int maybe_changed)
1020
1021DESCRIPTION
1022
1023 This module contains the routine which finds the names of any
1024 loaded "images" in the current process. The argument in must be
1025 NULL on the first call, and then the returned value must be passed
1026 in on subsequent calls. This provides the capability to "step" down
1027 the list of loaded objects. On the last object, a NULL value is
1028 returned.
1029
1030 The arg and return value are "struct link_map" pointers, as defined
1031 in <link.h>.
1032
1033 If it is expected that the contents of the shared library list has changed
1034 (e.g. when the special shared library breakpoint is hit) then pass non-zero
1035 for maybe_changed, otherwise zero.
1036 */
1037
1038static struct so_list *
1039find_solib (so_list_ptr, maybe_changed)
1040 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
1041 int maybe_changed; /* non-zero if shlib list might have changed */
1042{
1043 struct link_map *lm = NULL;
1044 struct so_list *new;
1045 struct so_list *so_list_next;
1046 struct so_list *p, **prev;
1047
1048 if (so_list_ptr == NULL)
1049 {
1050 struct so_list **map;
1051
1052 /* If we have not already read in the dynamic linking structures
1053 from the inferior, lookup the address of the base structure. */
1054 if (debug_base == 0)
1055 debug_base = locate_base ();
1056 if (debug_base != 0)
1057 {
1058 /* Read the base structure in and find the address of the first
1059 link map list member. */
1060 lm = first_link_map_member ();
1061 }
1062 else
1063 lm = NULL;
1064
1065 prev = &so_list_head;
1066 so_list_ptr = so_list_head;
1067 }
1068 else
1069 {
1070 /* We have been called before, and are in the process of walking
1071 the shared library list. Advance to the next shared object.
1072
1073 Always read from the target to check to see if any were
1074 added, but be quiet if we can't read from the target any more. */
1075 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
1076 (char *) &(so_list_ptr -> lm),
1077 sizeof (struct link_map));
1078
1079 if (status == 0)
1080 {
1081 lm = LM_NEXT (so_list_ptr);
1082 }
1083 else
1084 {
1085 lm = NULL;
1086 }
1087
1088 prev = &so_list_ptr -> next;
1089 so_list_ptr = so_list_ptr -> next;
1090 }
1091
1092 /* If we don't believe that the list has changed, just return the cached copy. */
1093 if (!maybe_changed)
1094 return (so_list_ptr);
1095
1096 /* At this point, lm is the address of the next list element in the target and
1097 so_list_ptr is our cached entry for it. */
1098
1099 if (lm != NULL)
1100 {
1101 if (so_list_ptr == NULL || so_list_ptr -> lmaddr != lm)
1102 {
1103 /* We have detected a change in the list. Check for a deletion by searching
1104 forward in the cached list */
1105 if (so_list_ptr)
1106 {
1107 for (p = so_list_ptr -> next; p; p = p -> next)
1108 if (p -> lmaddr == lm)
1109 break;
1110 }
1111 else
1112 p = NULL;
1113
1114 if (p)
1115 {
1116 /* This lib has been deleted */
1117 while (so_list_ptr != p)
1118 {
1119 *prev = so_list_ptr -> next;
1120 free_solib (so_list_ptr);
1121 so_list_ptr = *prev;
1122 }
1123 }
1124 else
1125 {
1126 /* A new lib has been inserted into the list */
1127 new = alloc_solib (lm);
1128 new -> next = so_list_ptr;
1129 *prev = new;
1130 so_list_ptr = new;
1131 }
1132 }
1133 }
1134 else
1135 {
1136 if (so_list_ptr != NULL)
1137 {
1138 so_list_head = new;
1139
1140 if (! solib_cleanup_queued)
1141 {
1142 make_run_cleanup (do_clear_solib, NULL);
1143 solib_cleanup_queued = 1;
1144 }
1145
1146 }
1147 so_list_next = new;
1148 if (lm)
1149 target_read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
1150 sizeof (struct link_map));
1151 /* For SVR4 versions, the first entry in the link map is for the
1152 inferior executable, so we must ignore it. For some versions of
1153 SVR4, it has no name. For others (Solaris 2.3 for example), it
1154 does have a name, so we can no longer use a missing name to
1155 decide when to ignore it. */
1156 if (lm && new && !IGNORE_FIRST_LINK_MAP_ENTRY (new -> lm))
1157 {
1158 int errcode;
1159 char *buffer;
1160 target_read_string ((CORE_ADDR) LM_NAME (new), &buffer,
1161 MAX_PATH_SIZE - 1, &errcode);
1162 if (errcode != 0)
1163 {
1164 warning ("find_solib: Can't read pathname for load map: %s\n",
1165 safe_strerror (errcode));
1166 return (so_list_next);
1167 }
1168 strncpy (new -> so_name, buffer, MAX_PATH_SIZE - 1);
1169 new -> so_name[MAX_PATH_SIZE - 1] = '\0';
1170 free (buffer);
1171 catch_errors (solib_map_sections, new,
1172 "Error while mapping shared library sections:\n",
1173 RETURN_MASK_ALL);
1174 }
1175 }
1176
1177 return (so_list_ptr);
1178}
1179
1180/* A small stub to get us past the arg-passing pinhole of catch_errors. */
1181
1182static int
1183symbol_add_stub (arg)
1184 PTR arg;
1185{
1186 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
1187 CORE_ADDR text_addr = 0;
1188
1189 if (so -> textsection)
1190 text_addr = so -> textsection -> addr;
1191 else if (so -> abfd != NULL)
1192 {
1193 asection *lowest_sect;
1194
1195 /* If we didn't find a mapped non zero sized .text section, set up
1196 text_addr so that the relocation in symbol_file_add does no harm. */
1197
1198 lowest_sect = bfd_get_section_by_name (so -> abfd, ".text");
1199 if (lowest_sect == NULL)
1200 bfd_map_over_sections (so -> abfd, find_lowest_section,
1201 (PTR) &lowest_sect);
1202 if (lowest_sect)
1203 text_addr = bfd_section_vma (so -> abfd, lowest_sect)
1204 + (CORE_ADDR) LM_ADDR (so);
1205 }
1206
1207 ALL_OBJFILES (so -> objfile)
1208 {
1209 if (strcmp (so -> objfile -> name, so -> so_name) == 0)
1210 return 1;
1211 }
1212 so -> objfile =
1213 symbol_file_add (so -> so_name, so -> from_tty,
1214 text_addr,
1215 0, 0, 0, 0, 1);
1216 return (1);
1217}
1218
1219/* This function will check the so name to see if matches the main list.
1220 In some system the main object is in the list, which we want to exclude */
1221
1222static int match_main (soname)
1223 char *soname;
1224{
1225 char **mainp;
1226
1227 for (mainp = main_name_list; *mainp != NULL; mainp++)
1228 {
1229 if (strcmp (soname, *mainp) == 0)
1230 return (1);
1231 }
1232
1233 return (0);
1234}
1235
1236/*
1237
1238GLOBAL FUNCTION
1239
1240 solib_add -- add a shared library file to the symtab and section list
1241
1242SYNOPSIS
1243
1244 void solib_add (char *arg_string, int from_tty,
1245 struct target_ops *target)
1246
1247DESCRIPTION
1248
1249*/
1250
1251void
1252solib_add (arg_string, from_tty, target)
1253 char *arg_string;
1254 int from_tty;
1255 struct target_ops *target;
1256{
1257 register struct so_list *so = NULL; /* link map state variable */
1258
1259 /* Last shared library that we read. */
1260 struct so_list *so_last = NULL;
1261
1262 char *re_err;
1263 int count;
1264 int old;
1265
1266 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
1267 {
1268 error ("Invalid regexp: %s", re_err);
1269 }
1270
1271 /* Add the shared library sections to the section table of the
1272 specified target, if any. */
1273 if (target)
1274 {
1275 /* Count how many new section_table entries there are. */
1276 so = NULL;
1277 count = 0;
1278 while ((so = find_solib (so, 1)) != NULL)
1279 {
1280 if (so -> so_name[0] && !match_main (so -> so_name))
1281 {
1282 count += so -> sections_end - so -> sections;
1283 }
1284 }
1285
1286 if (count)
1287 {
1288 int update_coreops;
1289
1290 /* We must update the to_sections field in the core_ops structure
1291 here, otherwise we dereference a potential dangling pointer
1292 for each call to target_read/write_memory within this routine. */
1293 update_coreops = core_ops.to_sections == target->to_sections;
1294
1295 /* Reallocate the target's section table including the new size. */
1296 if (target -> to_sections)
1297 {
1298 old = target -> to_sections_end - target -> to_sections;
1299 target -> to_sections = (struct section_table *)
1300 xrealloc ((char *)target -> to_sections,
1301 (sizeof (struct section_table)) * (count + old));
1302 }
1303 else
1304 {
1305 old = 0;
1306 target -> to_sections = (struct section_table *)
1307 xmalloc ((sizeof (struct section_table)) * count);
1308 }
1309 target -> to_sections_end = target -> to_sections + (count + old);
1310
1311 /* Update the to_sections field in the core_ops structure
1312 if needed. */
1313 if (update_coreops)
1314 {
1315 core_ops.to_sections = target->to_sections;
1316 core_ops.to_sections_end = target->to_sections_end;
1317 }
1318
1319 /* Add these section table entries to the target's table. */
1320 while ((so = find_solib (so, 1)) != NULL)
1321 {
1322 if (so -> so_name[0])
1323 {
1324 count = so -> sections_end - so -> sections;
1325 memcpy ((char *) (target -> to_sections + old),
1326 so -> sections,
1327 (sizeof (struct section_table)) * count);
1328 old += count;
1329 }
1330 }
1331 }
1332 }
1333
1334 /* Now add the symbol files. */
1335 while ((so = find_solib (so, 1)) != NULL)
1336 {
1337 if (so -> so_name[0] && re_exec (so -> so_name) &&
1338 !match_main (so -> so_name))
1339 {
1340 so -> from_tty = from_tty;
1341 if (so -> symbols_loaded)
1342 {
1343 if (from_tty)
1344 {
1345 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
1346 }
1347 }
1348 else if (catch_errors
1349 (symbol_add_stub, so,
1350 "Error while reading shared library symbols:\n",
1351 RETURN_MASK_ALL))
1352 {
1353 so_last = so;
1354 so -> symbols_loaded = 1;
1355 }
1356 }
1357 }
1358
1359 /* Getting new symbols may change our opinion about what is
1360 frameless. */
1361 if (so_last)
1362 reinit_frame_cache ();
1363
1364 if (so_last)
1365 special_symbol_handling (so_last);
1366}
1367
1368/*
1369
1370LOCAL FUNCTION
1371
1372 info_sharedlibrary_command -- code for "info sharedlibrary"
1373
1374SYNOPSIS
1375
1376 static void info_sharedlibrary_command ()
1377
1378DESCRIPTION
1379
1380 Walk through the shared library list and print information
1381 about each attached library.
1382*/
1383
1384static void
1385info_sharedlibrary_command (ignore, from_tty)
1386 char *ignore;
1387 int from_tty;
1388{
1389 register struct so_list *so = NULL; /* link map state variable */
1390 int header_done = 0;
1391 int addr_width;
1392 char *addr_fmt;
1393
1394 if (exec_bfd == NULL)
1395 {
1396 printf_unfiltered ("No exec file.\n");
1397 return;
1398 }
1399
1400#ifndef TARGET_ELF64
1401 addr_width = 8+4;
1402 addr_fmt = "08l";
1403#else
1404 addr_width = 16+4;
1405 addr_fmt = "016l";
1406#endif
1407
1408 while ((so = find_solib (so, 0)) != NULL)
1409 {
1410 if (so -> so_name[0])
1411 {
1412 if (!header_done)
1413 {
1414 printf_unfiltered("%-*s%-*s%-12s%s\n", addr_width, "From",
1415 addr_width, "To", "Syms Read",
1416 "Shared Object Library");
1417 header_done++;
1418 }
1419
1420 printf_unfiltered ("%-*s", addr_width,
1421 local_hex_string_custom ((unsigned long) LM_ADDR (so),
1422 addr_fmt));
1423 printf_unfiltered ("%-*s", addr_width,
1424 local_hex_string_custom ((unsigned long) so -> lmend,
1425 addr_fmt));
1426 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
1427 printf_unfiltered ("%s\n", so -> so_name);
1428 }
1429 }
1430 if (so_list_head == NULL)
1431 {
1432 printf_unfiltered ("No shared libraries loaded at this time.\n");
1433 }
1434}
1435
1436/*
1437
1438GLOBAL FUNCTION
1439
1440 solib_address -- check to see if an address is in a shared lib
1441
1442SYNOPSIS
1443
1444 char * solib_address (CORE_ADDR address)
1445
1446DESCRIPTION
1447
1448 Provides a hook for other gdb routines to discover whether or
1449 not a particular address is within the mapped address space of
1450 a shared library. Any address between the base mapping address
1451 and the first address beyond the end of the last mapping, is
1452 considered to be within the shared library address space, for
1453 our purposes.
1454
1455 For example, this routine is called at one point to disable
1456 breakpoints which are in shared libraries that are not currently
1457 mapped in.
1458 */
1459
1460char *
1461solib_address (address)
1462 CORE_ADDR address;
1463{
1464 register struct so_list *so = 0; /* link map state variable */
1465
1466 while ((so = find_solib (so, 0)) != NULL)
1467 {
1468 if (so -> so_name[0])
1469 {
1470 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
1471 (address < (CORE_ADDR) so -> lmend))
1472 return (so->so_name);
1473 }
1474 }
1475 return (0);
1476}
1477
1478/* Called by free_all_symtabs */
1479
1480void
1481clear_solib()
1482{
1483 struct so_list *next;
1484 char *bfd_filename;
1485
1486 while (so_list_head)
1487 {
1488 next = so_list_head -> next;
1489 free_solib (so_list_head);
1490 so_list_head = next;
1491 }
1492 debug_base = 0;
1493}
1494
1495static void
1496do_clear_solib (dummy)
1497 PTR dummy;
1498{
1499 solib_cleanup_queued = 0;
1500 clear_solib ();
1501}
1502
1503#ifdef SVR4_SHARED_LIBS
1504
1505/* Return 1 if PC lies in the dynamic symbol resolution code of the
1506 SVR4 run time loader. */
1507
1508static CORE_ADDR interp_text_sect_low;
1509static CORE_ADDR interp_text_sect_high;
1510static CORE_ADDR interp_plt_sect_low;
1511static CORE_ADDR interp_plt_sect_high;
1512
1513int
1514in_svr4_dynsym_resolve_code (pc)
1515 CORE_ADDR pc;
1516{
1517 return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
1518 || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
1519 || in_plt_section (pc, NULL));
1520}
1521#endif
1522
1523/*
1524
1525LOCAL FUNCTION
1526
1527 disable_break -- remove the "mapping changed" breakpoint
1528
1529SYNOPSIS
1530
1531 static int disable_break ()
1532
1533DESCRIPTION
1534
1535 Removes the breakpoint that gets hit when the dynamic linker
1536 completes a mapping change.
1537
1538*/
1539
1540#ifndef SVR4_SHARED_LIBS
1541
1542static int
1543disable_break ()
1544{
1545 int status = 1;
1546
1547#ifndef SVR4_SHARED_LIBS
1548
1549 int in_debugger = 0;
1550
1551 /* Read the debugger structure from the inferior to retrieve the
1552 address of the breakpoint and the original contents of the
1553 breakpoint address. Remove the breakpoint by writing the original
1554 contents back. */
1555
1556 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1557
1558 /* Set `in_debugger' to zero now. */
1559
1560 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1561
1562 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
1563 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
1564 sizeof (debug_copy.ldd_bp_inst));
1565
1566#else /* SVR4_SHARED_LIBS */
1567
1568 /* Note that breakpoint address and original contents are in our address
1569 space, so we just need to write the original contents back. */
1570
1571 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
1572 {
1573 status = 0;
1574 }
1575
1576#endif /* !SVR4_SHARED_LIBS */
1577
1578 /* For the SVR4 version, we always know the breakpoint address. For the
1579 SunOS version we don't know it until the above code is executed.
1580 Grumble if we are stopped anywhere besides the breakpoint address. */
1581
1582 if (stop_pc != breakpoint_addr)
1583 {
1584 warning ("stopped at unknown breakpoint while handling shared libraries");
1585 }
1586
1587 return (status);
1588}
1589
1590#endif /* #ifdef SVR4_SHARED_LIBS */
1591
1592/*
1593
1594LOCAL FUNCTION
1595
1596 enable_break -- arrange for dynamic linker to hit breakpoint
1597
1598SYNOPSIS
1599
1600 int enable_break (void)
1601
1602DESCRIPTION
1603
1604 Both the SunOS and the SVR4 dynamic linkers have, as part of their
1605 debugger interface, support for arranging for the inferior to hit
1606 a breakpoint after mapping in the shared libraries. This function
1607 enables that breakpoint.
1608
1609 For SunOS, there is a special flag location (in_debugger) which we
1610 set to 1. When the dynamic linker sees this flag set, it will set
1611 a breakpoint at a location known only to itself, after saving the
1612 original contents of that place and the breakpoint address itself,
1613 in it's own internal structures. When we resume the inferior, it
1614 will eventually take a SIGTRAP when it runs into the breakpoint.
1615 We handle this (in a different place) by restoring the contents of
1616 the breakpointed location (which is only known after it stops),
1617 chasing around to locate the shared libraries that have been
1618 loaded, then resuming.
1619
1620 For SVR4, the debugger interface structure contains a member (r_brk)
1621 which is statically initialized at the time the shared library is
1622 built, to the offset of a function (_r_debug_state) which is guaran-
1623 teed to be called once before mapping in a library, and again when
1624 the mapping is complete. At the time we are examining this member,
1625 it contains only the unrelocated offset of the function, so we have
1626 to do our own relocation. Later, when the dynamic linker actually
1627 runs, it relocates r_brk to be the actual address of _r_debug_state().
1628
1629 The debugger interface structure also contains an enumeration which
1630 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
1631 depending upon whether or not the library is being mapped or unmapped,
1632 and then set to RT_CONSISTENT after the library is mapped/unmapped.
1633*/
1634
1635static int
1636enable_break ()
1637{
1638 int success = 0;
1639
1640#ifndef SVR4_SHARED_LIBS
1641
1642 int j;
1643 int in_debugger;
1644
1645 /* Get link_dynamic structure */
1646
1647 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1648 sizeof (dynamic_copy));
1649 if (j)
1650 {
1651 /* unreadable */
1652 return (0);
1653 }
1654
1655 /* Calc address of debugger interface structure */
1656
1657 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1658
1659 /* Calc address of `in_debugger' member of debugger interface structure */
1660
1661 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1662 (char *) &debug_copy);
1663
1664 /* Write a value of 1 to this member. */
1665
1666 in_debugger = 1;
1667 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1668 success = 1;
1669
1670#else /* SVR4_SHARED_LIBS */
1671
1672#ifdef BKPT_AT_SYMBOL
1673
1674 struct minimal_symbol *msymbol;
1675 char **bkpt_namep;
1676 asection *interp_sect;
1677
1678 /* First, remove all the solib event breakpoints. Their addresses
1679 may have changed since the last time we ran the program. */
1680 remove_solib_event_breakpoints ();
1681
1682#ifdef SVR4_SHARED_LIBS
1683 interp_text_sect_low = interp_text_sect_high = 0;
1684 interp_plt_sect_low = interp_plt_sect_high = 0;
1685
1686 /* Find the .interp section; if not found, warn the user and drop
1687 into the old breakpoint at symbol code. */
1688 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1689 if (interp_sect)
1690 {
1691 unsigned int interp_sect_size;
1692 char *buf;
1693 CORE_ADDR load_addr;
1694 bfd *tmp_bfd;
1695 CORE_ADDR sym_addr = 0;
1696
1697 /* Read the contents of the .interp section into a local buffer;
1698 the contents specify the dynamic linker this program uses. */
1699 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
1700 buf = alloca (interp_sect_size);
1701 bfd_get_section_contents (exec_bfd, interp_sect,
1702 buf, 0, interp_sect_size);
1703
1704 /* Now we need to figure out where the dynamic linker was
1705 loaded so that we can load its symbols and place a breakpoint
1706 in the dynamic linker itself.
1707
1708 This address is stored on the stack. However, I've been unable
1709 to find any magic formula to find it for Solaris (appears to
1710 be trivial on GNU/Linux). Therefore, we have to try an alternate
1711 mechanism to find the dynamic linker's base address. */
1712 tmp_bfd = bfd_openr (buf, gnutarget);
1713 if (tmp_bfd == NULL)
1714 goto bkpt_at_symbol;
1715
1716 /* Make sure the dynamic linker's really a useful object. */
1717 if (!bfd_check_format (tmp_bfd, bfd_object))
1718 {
1719 warning ("Unable to grok dynamic linker %s as an object file", buf);
1720 bfd_close (tmp_bfd);
1721 goto bkpt_at_symbol;
1722 }
1723
1724 /* We find the dynamic linker's base address by examining the
1725 current pc (which point at the entry point for the dynamic
1726 linker) and subtracting the offset of the entry point. */
1727 load_addr = read_pc () - tmp_bfd->start_address;
1728
1729 /* Record the relocated start and end address of the dynamic linker
1730 text and plt section for in_svr4_dynsym_resolve_code. */
1731 interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
1732 if (interp_sect)
1733 {
1734 interp_text_sect_low =
1735 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1736 interp_text_sect_high =
1737 interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1738 }
1739 interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1740 if (interp_sect)
1741 {
1742 interp_plt_sect_low =
1743 bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1744 interp_plt_sect_high =
1745 interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1746 }
1747
1748 /* Now try to set a breakpoint in the dynamic linker. */
1749 for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1750 {
1751 sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep);
1752 if (sym_addr != 0)
1753 break;
1754 }
1755
1756 /* We're done with the temporary bfd. */
1757 bfd_close (tmp_bfd);
1758
1759 if (sym_addr != 0)
1760 {
1761 create_solib_event_breakpoint (load_addr + sym_addr);
1762 return 1;
1763 }
1764
1765 /* For whatever reason we couldn't set a breakpoint in the dynamic
1766 linker. Warn and drop into the old code. */
1767bkpt_at_symbol:
1768 warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
1769 }
1770#endif
1771
1772 /* Scan through the list of symbols, trying to look up the symbol and
1773 set a breakpoint there. Terminate loop when we/if we succeed. */
1774
1775 breakpoint_addr = 0;
1776 for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1777 {
1778 msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1779 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1780 {
1781 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1782 return 1;
1783 }
1784 }
1785
1786 /* Nothing good happened. */
1787 success = 0;
1788
1789#endif /* BKPT_AT_SYMBOL */
1790
1791#endif /* !SVR4_SHARED_LIBS */
1792
1793 return (success);
1794}
1795
1796/*
1797
1798GLOBAL FUNCTION
1799
1800 solib_create_inferior_hook -- shared library startup support
1801
1802SYNOPSIS
1803
1804 void solib_create_inferior_hook()
1805
1806DESCRIPTION
1807
1808 When gdb starts up the inferior, it nurses it along (through the
1809 shell) until it is ready to execute it's first instruction. At this
1810 point, this function gets called via expansion of the macro
1811 SOLIB_CREATE_INFERIOR_HOOK.
1812
1813 For SunOS executables, this first instruction is typically the
1814 one at "_start", or a similar text label, regardless of whether
1815 the executable is statically or dynamically linked. The runtime
1816 startup code takes care of dynamically linking in any shared
1817 libraries, once gdb allows the inferior to continue.
1818
1819 For SVR4 executables, this first instruction is either the first
1820 instruction in the dynamic linker (for dynamically linked
1821 executables) or the instruction at "start" for statically linked
1822 executables. For dynamically linked executables, the system
1823 first exec's /lib/libc.so.N, which contains the dynamic linker,
1824 and starts it running. The dynamic linker maps in any needed
1825 shared libraries, maps in the actual user executable, and then
1826 jumps to "start" in the user executable.
1827
1828 For both SunOS shared libraries, and SVR4 shared libraries, we
1829 can arrange to cooperate with the dynamic linker to discover the
1830 names of shared libraries that are dynamically linked, and the
1831 base addresses to which they are linked.
1832
1833 This function is responsible for discovering those names and
1834 addresses, and saving sufficient information about them to allow
1835 their symbols to be read at a later time.
1836
1837FIXME
1838
1839 Between enable_break() and disable_break(), this code does not
1840 properly handle hitting breakpoints which the user might have
1841 set in the startup code or in the dynamic linker itself. Proper
1842 handling will probably have to wait until the implementation is
1843 changed to use the "breakpoint handler function" method.
1844
1845 Also, what if child has exit()ed? Must exit loop somehow.
1846 */
1847
1848void
1849solib_create_inferior_hook()
1850{
1851 /* If we are using the BKPT_AT_SYMBOL code, then we don't need the base
1852 yet. In fact, in the case of a SunOS4 executable being run on
1853 Solaris, we can't get it yet. find_solib will get it when it needs
1854 it. */
1855#if !(defined (SVR4_SHARED_LIBS) && defined (BKPT_AT_SYMBOL))
1856 if ((debug_base = locate_base ()) == 0)
1857 {
1858 /* Can't find the symbol or the executable is statically linked. */
1859 return;
1860 }
1861#endif
1862
1863 if (!enable_break ())
1864 {
1865 warning ("shared library handler failed to enable breakpoint");
1866 return;
1867 }
1868
1869#if !defined(SVR4_SHARED_LIBS) || defined(_SCO_DS)
1870 /* SCO and SunOS need the loop below, other systems should be using the
1871 special shared library breakpoints and the shared library breakpoint
1872 service routine.
1873
1874 Now run the target. It will eventually hit the breakpoint, at
1875 which point all of the libraries will have been mapped in and we
1876 can go groveling around in the dynamic linker structures to find
1877 out what we need to know about them. */
1878
1879 clear_proceed_status ();
1880 stop_soon_quietly = 1;
1881 stop_signal = TARGET_SIGNAL_0;
1882 do
1883 {
1884 target_resume (-1, 0, stop_signal);
1885 wait_for_inferior ();
1886 }
1887 while (stop_signal != TARGET_SIGNAL_TRAP);
1888 stop_soon_quietly = 0;
1889
1890#if !defined(_SCO_DS)
1891 /* We are now either at the "mapping complete" breakpoint (or somewhere
1892 else, a condition we aren't prepared to deal with anyway), so adjust
1893 the PC as necessary after a breakpoint, disable the breakpoint, and
1894 add any shared libraries that were mapped in. */
1895
1896 if (DECR_PC_AFTER_BREAK)
1897 {
1898 stop_pc -= DECR_PC_AFTER_BREAK;
1899 write_register (PC_REGNUM, stop_pc);
1900 }
1901
1902 if (!disable_break ())
1903 {
1904 warning ("shared library handler failed to disable breakpoint");
1905 }
1906
1907 if (auto_solib_add)
1908 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1909#endif /* ! _SCO_DS */
1910#endif
1911}
1912
1913/*
1914
1915LOCAL FUNCTION
1916
1917 special_symbol_handling -- additional shared library symbol handling
1918
1919SYNOPSIS
1920
1921 void special_symbol_handling (struct so_list *so)
1922
1923DESCRIPTION
1924
1925 Once the symbols from a shared object have been loaded in the usual
1926 way, we are called to do any system specific symbol handling that
1927 is needed.
1928
1929 For SunOS4, this consists of grunging around in the dynamic
1930 linkers structures to find symbol definitions for "common" symbols
1931 and adding them to the minimal symbol table for the runtime common
1932 objfile.
1933
1934*/
1935
1936static void
1937special_symbol_handling (so)
1938struct so_list *so;
1939{
1940#ifndef SVR4_SHARED_LIBS
1941 int j;
1942
1943 if (debug_addr == 0)
1944 {
1945 /* Get link_dynamic structure */
1946
1947 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1948 sizeof (dynamic_copy));
1949 if (j)
1950 {
1951 /* unreadable */
1952 return;
1953 }
1954
1955 /* Calc address of debugger interface structure */
1956 /* FIXME, this needs work for cross-debugging of core files
1957 (byteorder, size, alignment, etc). */
1958
1959 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1960 }
1961
1962 /* Read the debugger structure from the inferior, just to make sure
1963 we have a current copy. */
1964
1965 j = target_read_memory (debug_addr, (char *) &debug_copy,
1966 sizeof (debug_copy));
1967 if (j)
1968 return; /* unreadable */
1969
1970 /* Get common symbol definitions for the loaded object. */
1971
1972 if (debug_copy.ldd_cp)
1973 {
1974 solib_add_common_symbols (debug_copy.ldd_cp);
1975 }
1976
1977#endif /* !SVR4_SHARED_LIBS */
1978}
1979
1980
1981/*
1982
1983LOCAL FUNCTION
1984
1985 sharedlibrary_command -- handle command to explicitly add library
1986
1987SYNOPSIS
1988
1989 static void sharedlibrary_command (char *args, int from_tty)
1990
1991DESCRIPTION
1992
1993*/
1994
1995static void
1996sharedlibrary_command (args, from_tty)
1997char *args;
1998int from_tty;
1999{
2000 dont_repeat ();
2001 solib_add (args, from_tty, (struct target_ops *) 0);
2002}
2003
2004#endif /* HAVE_LINK_H */
2005
2006void
2007_initialize_solib()
2008{
2009#ifdef HAVE_LINK_H
2010
2011 add_com ("sharedlibrary", class_files, sharedlibrary_command,
2012 "Load shared object library symbols for files matching REGEXP.");
2013 add_info ("sharedlibrary", info_sharedlibrary_command,
2014 "Status of loaded shared object libraries.");
2015
2016 add_show_from_set
2017 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
2018 (char *) &auto_solib_add,
2019 "Set autoloading of shared library symbols.\n\
2020If nonzero, symbols from all shared object libraries will be loaded\n\
2021automatically when the inferior begins execution or when the dynamic linker\n\
2022informs gdb that a new library has been loaded. Otherwise, symbols\n\
2023must be loaded manually, using `sharedlibrary'.",
2024 &setlist),
2025 &showlist);
2026
2027 add_show_from_set
2028 (add_set_cmd ("solib-absolute-prefix", class_support, var_filename,
2029 (char *) &solib_absolute_prefix,
2030 "Set prefix for loading absolute shared library symbol files.\n\
2031For other (relative) files, you can add values using `set solib-search-path'.",
2032 &setlist),
2033 &showlist);
2034 add_show_from_set
2035 (add_set_cmd ("solib-search-path", class_support, var_string,
2036 (char *) &solib_search_path,
2037 "Set the search path for loading non-absolute shared library symbol files.\n\
2038This takes precedence over the environment variables PATH and LD_LIBRARY_PATH.",
2039 &setlist),
2040 &showlist);
2041
2042#endif /* HAVE_LINK_H */
2043}