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