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