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