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