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