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