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