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