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