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