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