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