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