Changes to implement the -mapped and -readnow options for commands that
[deliverable/binutils-gdb.git] / gdb / solib.c
1 /* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include "defs.h"
22
23 #include <sys/types.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <link.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29 #include <a.out.h>
30
31 #include "symtab.h"
32 #include "bfd.h"
33 #include "symfile.h"
34 #include "gdbcore.h"
35 #include "command.h"
36 #include "target.h"
37 #include "frame.h"
38 #include "regex.h"
39 #include "inferior.h"
40
41 #define MAX_PATH_SIZE 256 /* FIXME: Should be dynamic */
42
43 /* On SVR4 systems, for the initial implementation, use main() as the
44 "startup mapping complete" breakpoint address. The models for SunOS
45 and SVR4 dynamic linking debugger support are different in that SunOS
46 hits one breakpoint when all mapping is complete while using the SVR4
47 debugger support takes two breakpoint hits for each file mapped, and
48 there is no way to know when the "last" one is hit. Both these
49 mechanisms should be tied to a "breakpoint service routine" that
50 gets automatically executed whenever one of the breakpoints indicating
51 a change in mapping is hit. This is a future enhancement. (FIXME) */
52
53 #define BKPT_AT_MAIN 1
54
55 /* local data declarations */
56
57 #ifndef SVR4_SHARED_LIBS
58
59 #define DEBUG_BASE "_DYNAMIC"
60 #define LM_ADDR(so) ((so) -> lm.lm_addr)
61 #define LM_NEXT(so) ((so) -> lm.lm_next)
62 #define LM_NAME(so) ((so) -> lm.lm_name)
63 static struct link_dynamic dynamic_copy;
64 static struct link_dynamic_2 ld_2_copy;
65 static struct ld_debug debug_copy;
66 static CORE_ADDR debug_addr;
67 static CORE_ADDR flag_addr;
68
69 #else /* SVR4_SHARED_LIBS */
70
71 #define DEBUG_BASE "_r_debug"
72 #define LM_ADDR(so) ((so) -> lm.l_addr)
73 #define LM_NEXT(so) ((so) -> lm.l_next)
74 #define LM_NAME(so) ((so) -> lm.l_name)
75 static struct r_debug debug_copy;
76 char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
77
78 #endif /* !SVR4_SHARED_LIBS */
79
80 struct so_list {
81 struct so_list *next; /* next structure in linked list */
82 struct link_map lm; /* copy of link map from inferior */
83 struct link_map *lmaddr; /* addr in inferior lm was read from */
84 CORE_ADDR lmend; /* upper addr bound of mapped object */
85 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
86 char symbols_loaded; /* flag: symbols read in yet? */
87 char from_tty; /* flag: print msgs? */
88 bfd *so_bfd; /* bfd for so_name */
89 struct objfile *objfile; /* objfile for loaded lib */
90 struct section_table *sections;
91 struct section_table *sections_end;
92 };
93
94 static struct so_list *so_list_head; /* List of known shared objects */
95 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
96 static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
97
98 /* Local function prototypes */
99
100 static void
101 special_symbol_handling PARAMS ((struct so_list *));
102
103 static void
104 sharedlibrary_command PARAMS ((char *, int));
105
106 static int
107 enable_break PARAMS ((void));
108
109 static int
110 disable_break PARAMS ((void));
111
112 static void
113 info_sharedlibrary_command PARAMS ((void));
114
115 static int
116 symbol_add_stub PARAMS ((char *));
117
118 static struct so_list *
119 find_solib PARAMS ((struct so_list *));
120
121 static struct link_map *
122 first_link_map_member PARAMS ((void));
123
124 static CORE_ADDR
125 locate_base PARAMS ((void));
126
127 static int
128 look_for_base PARAMS ((int, CORE_ADDR));
129
130 static CORE_ADDR
131 bfd_lookup_symbol PARAMS ((bfd *, char *));
132
133 static void
134 solib_map_sections PARAMS ((struct so_list *));
135
136 #ifndef SVR4_SHARED_LIBS
137
138 static void
139 solib_add_common_symbols PARAMS ((struct rtc_symb *, struct objfile *));
140
141 #endif
142
143 /*
144
145 LOCAL FUNCTION
146
147 solib_map_sections -- open bfd and build sections for shared lib
148
149 SYNOPSIS
150
151 static void solib_map_sections (struct so_list *so)
152
153 DESCRIPTION
154
155 Given a pointer to one of the shared objects in our list
156 of mapped objects, use the recorded name to open a bfd
157 descriptor for the object, build a section table, and then
158 relocate all the section addresses by the base address at
159 which the shared object was mapped.
160
161 FIXMES
162
163 In most (all?) cases the shared object file name recorded in the
164 dynamic linkage tables will be a fully qualified pathname. For
165 cases where it isn't, do we really mimic the systems search
166 mechanism correctly in the below code (particularly the tilde
167 expansion stuff?).
168 */
169
170 static void
171 solib_map_sections (so)
172 struct so_list *so;
173 {
174 char *filename;
175 char *scratch_pathname;
176 int scratch_chan;
177 struct section_table *p;
178
179 filename = tilde_expand (so -> so_name);
180 make_cleanup (free, filename);
181
182 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
183 &scratch_pathname);
184 if (scratch_chan < 0)
185 {
186 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
187 O_RDONLY, 0, &scratch_pathname);
188 }
189 if (scratch_chan < 0)
190 {
191 perror_with_name (filename);
192 }
193
194 so -> so_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
195 if (!so -> so_bfd)
196 {
197 error ("Could not open `%s' as an executable file: %s",
198 scratch_pathname, bfd_errmsg (bfd_error));
199 }
200 if (!bfd_check_format (so -> so_bfd, bfd_object))
201 {
202 error ("\"%s\": not in executable format: %s.",
203 scratch_pathname, bfd_errmsg (bfd_error));
204 }
205 if (build_section_table (so -> so_bfd, &so -> sections, &so -> sections_end))
206 {
207 error ("Can't find the file sections in `%s': %s",
208 exec_bfd -> filename, bfd_errmsg (bfd_error));
209 }
210
211 for (p = so -> sections; p < so -> sections_end; p++)
212 {
213 /* Relocate the section binding addresses as recorded in the shared
214 object's file by the base address to which the object was actually
215 mapped. */
216 p -> addr += (CORE_ADDR) LM_ADDR (so);
217 p -> endaddr += (CORE_ADDR) LM_ADDR (so);
218 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
219 }
220 }
221
222 /* Read all dynamically loaded common symbol definitions from the inferior
223 and add them to the minimal symbol table for the shared library objfile. */
224
225 #ifndef SVR4_SHARED_LIBS
226
227 static void
228 solib_add_common_symbols (rtc_symp, objfile)
229 struct rtc_symb *rtc_symp;
230 struct objfile *objfile;
231 {
232 struct rtc_symb inferior_rtc_symb;
233 struct nlist inferior_rtc_nlist;
234 int len;
235 char *name;
236 char *origname;
237
238 init_minimal_symbol_collection ();
239 make_cleanup (discard_minimal_symbols, 0);
240
241 while (rtc_symp)
242 {
243 read_memory ((CORE_ADDR) rtc_symp,
244 (char *) &inferior_rtc_symb,
245 sizeof (inferior_rtc_symb));
246 read_memory ((CORE_ADDR) inferior_rtc_symb.rtc_sp,
247 (char *) &inferior_rtc_nlist,
248 sizeof(inferior_rtc_nlist));
249 if (inferior_rtc_nlist.n_type == N_COMM)
250 {
251 /* FIXME: The length of the symbol name is not available, but in the
252 current implementation the common symbol is allocated immediately
253 behind the name of the symbol. */
254 len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx;
255
256 origname = name = xmalloc (len);
257 read_memory ((CORE_ADDR) inferior_rtc_nlist.n_un.n_name, name, len);
258
259 /* Don't enter the symbol twice if the target is re-run. */
260
261 #ifdef NAMES_HAVE_UNDERSCORE
262 if (*name == '_')
263 {
264 name++;
265 }
266 #endif
267 /* FIXME: Do we really want to exclude symbols which happen
268 to match symbols for other locations in the inferior's
269 address space, even when they are in different linkage units? */
270 if (lookup_minimal_symbol (name, (struct objfile *) NULL) == NULL)
271 {
272 name = obsavestring (name, strlen (name),
273 &objfile -> symbol_obstack);
274 prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value,
275 mst_bss);
276 }
277 free (origname);
278 }
279 rtc_symp = inferior_rtc_symb.rtc_next;
280 }
281
282 /* Install any minimal symbols that have been collected as the current
283 minimal symbols for this objfile. */
284
285 install_minimal_symbols (objfile);
286 }
287
288 #endif /* SVR4_SHARED_LIBS */
289
290 /*
291
292 LOCAL FUNCTION
293
294 bfd_lookup_symbol -- lookup the value for a specific symbol
295
296 SYNOPSIS
297
298 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
299
300 DESCRIPTION
301
302 An expensive way to lookup the value of a single symbol for
303 bfd's that are only temporary anyway. This is used by the
304 shared library support to find the address of the debugger
305 interface structures in the shared library.
306
307 Note that 0 is specifically allowed as an error return (no
308 such symbol).
309
310 FIXME: See if there is a less "expensive" way of doing this.
311 Also see if there is already another bfd or gdb function
312 that specifically does this, and if so, use it.
313 */
314
315 static CORE_ADDR
316 bfd_lookup_symbol (abfd, symname)
317 bfd *abfd;
318 char *symname;
319 {
320 unsigned int storage_needed;
321 asymbol *sym;
322 asymbol **symbol_table;
323 unsigned int number_of_symbols;
324 unsigned int i;
325 struct cleanup *back_to;
326 CORE_ADDR symaddr = 0;
327
328 storage_needed = get_symtab_upper_bound (abfd);
329
330 if (storage_needed > 0)
331 {
332 symbol_table = (asymbol **) bfd_xmalloc (storage_needed);
333 back_to = make_cleanup (free, symbol_table);
334 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
335
336 for (i = 0; i < number_of_symbols; i++)
337 {
338 sym = *symbol_table++;
339 if (strcmp (sym -> name, symname) == 0)
340 {
341 symaddr = sym -> value;
342 break;
343 }
344 }
345 do_cleanups (back_to);
346 }
347 return (symaddr);
348 }
349
350 /*
351
352 LOCAL FUNCTION
353
354 look_for_base -- examine file for each mapped address segment
355
356 SYNOPSYS
357
358 static int look_for_base (int fd, CORE_ADDR baseaddr)
359
360 DESCRIPTION
361
362 This function is passed to proc_iterate_over_mappings, which
363 causes it to get called once for each mapped address space, with
364 an open file descriptor for the file mapped to that space, and the
365 base address of that mapped space.
366
367 Our job is to find the symbol DEBUG_BASE in the file that this
368 fd is open on, if it exists, and if so, initialize the dynamic
369 linker structure base address debug_base.
370
371 Note that this is a computationally expensive proposition, since
372 we basically have to open a bfd on every call, so we specifically
373 avoid opening the exec file.
374 */
375
376 static int
377 look_for_base (fd, baseaddr)
378 int fd;
379 CORE_ADDR baseaddr;
380 {
381 bfd *interp_bfd;
382 CORE_ADDR address;
383
384 /* If the fd is -1, then there is no file that corresponds to this
385 mapped memory segment, so skip it. Also, if the fd corresponds
386 to the exec file, skip it as well. */
387
388 if ((fd == -1) || fdmatch (fileno ((FILE *)(exec_bfd -> iostream)), fd))
389 {
390 return (0);
391 }
392
393 /* Try to open whatever random file this fd corresponds to. Note that
394 we have no way currently to find the filename. Don't gripe about
395 any problems we might have, just fail. */
396
397 if ((interp_bfd = bfd_fdopenr ("unnamed", NULL, fd)) == NULL)
398 {
399 return (0);
400 }
401 if (!bfd_check_format (interp_bfd, bfd_object))
402 {
403 bfd_close (interp_bfd);
404 return (0);
405 }
406
407 /* Now try to find our DEBUG_BASE symbol in this file, which we at
408 least know to be a valid ELF executable or shared library. */
409
410 if ((address = bfd_lookup_symbol (interp_bfd, DEBUG_BASE)) == 0)
411 {
412 bfd_close (interp_bfd);
413 return (0);
414 }
415
416 /* Eureka! We found the symbol. But now we may need to relocate it
417 by the base address. If the symbol's value is less than the base
418 address of the shared library, then it hasn't yet been relocated
419 by the dynamic linker, and we have to do it ourself. FIXME: Note
420 that we make the assumption that the first segment that corresponds
421 to the shared library has the base address to which the library
422 was relocated. */
423
424 if (address < baseaddr)
425 {
426 address += baseaddr;
427 }
428 debug_base = address;
429 bfd_close (interp_bfd);
430 return (1);
431 }
432
433 /*
434
435 LOCAL FUNCTION
436
437 locate_base -- locate the base address of dynamic linker structs
438
439 SYNOPSIS
440
441 CORE_ADDR locate_base (void)
442
443 DESCRIPTION
444
445 For both the SunOS and SVR4 shared library implementations, if the
446 inferior executable has been linked dynamically, there is a single
447 address somewhere in the inferior's data space which is the key to
448 locating all of the dynamic linker's runtime structures. This
449 address is the value of the symbol defined by the macro DEBUG_BASE.
450 The job of this function is to find and return that address, or to
451 return 0 if there is no such address (the executable is statically
452 linked for example).
453
454 For SunOS, the job is almost trivial, since the dynamic linker and
455 all of it's structures are statically linked to the executable at
456 link time. Thus the symbol for the address we are looking for has
457 already been added to the minimal symbol table for the executable's
458 objfile at the time the symbol file's symbols were read, and all we
459 have to do is look it up there. Note that we explicitly do NOT want
460 to find the copies in the shared library.
461
462 The SVR4 version is much more complicated because the dynamic linker
463 and it's structures are located in the shared C library, which gets
464 run as the executable's "interpreter" by the kernel. We have to go
465 to a lot more work to discover the address of DEBUG_BASE. Because
466 of this complexity, we cache the value we find and return that value
467 on subsequent invocations. Note there is no copy in the executable
468 symbol tables.
469
470 Note that we can assume nothing about the process state at the time
471 we need to find this address. We may be stopped on the first instruc-
472 tion of the interpreter (C shared library), the first instruction of
473 the executable itself, or somewhere else entirely (if we attached
474 to the process for example).
475
476 */
477
478 static CORE_ADDR
479 locate_base ()
480 {
481
482 #ifndef SVR4_SHARED_LIBS
483
484 struct minimal_symbol *msymbol;
485 CORE_ADDR address = 0;
486
487 /* For SunOS, we want to limit the search for DEBUG_BASE to the executable
488 being debugged, since there is a duplicate named symbol in the shared
489 library. We don't want the shared library versions. */
490
491 msymbol = lookup_minimal_symbol (DEBUG_BASE, symfile_objfile);
492 if ((msymbol != NULL) && (msymbol -> address != 0))
493 {
494 address = msymbol -> address;
495 }
496 return (address);
497
498 #else /* SVR4_SHARED_LIBS */
499
500 /* Check to see if we have a currently valid address, and if so, avoid
501 doing all this work again and just return the cached address. If
502 we have no cached address, ask the /proc support interface to iterate
503 over the list of mapped address segments, calling look_for_base() for
504 each segment. When we are done, we will have either found the base
505 address or not. */
506
507 if (debug_base == 0)
508 {
509 proc_iterate_over_mappings (look_for_base);
510 }
511 return (debug_base);
512
513 #endif /* !SVR4_SHARED_LIBS */
514
515 }
516
517 static struct link_map *
518 first_link_map_member ()
519 {
520 struct link_map *lm = NULL;
521
522 #ifndef SVR4_SHARED_LIBS
523
524 read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy));
525 if (dynamic_copy.ld_version >= 2)
526 {
527 /* It is a version that we can deal with, so read in the secondary
528 structure and find the address of the link map list from it. */
529 read_memory ((CORE_ADDR) dynamic_copy.ld_un.ld_2, (char *) &ld_2_copy,
530 sizeof (struct link_dynamic_2));
531 lm = ld_2_copy.ld_loaded;
532 }
533
534 #else /* SVR4_SHARED_LIBS */
535
536 read_memory (debug_base, (char *) &debug_copy, sizeof (struct r_debug));
537 lm = debug_copy.r_map;
538
539 #endif /* !SVR4_SHARED_LIBS */
540
541 return (lm);
542 }
543
544 /*
545
546 LOCAL FUNCTION
547
548 find_solib -- step through list of shared objects
549
550 SYNOPSIS
551
552 struct so_list *find_solib (struct so_list *so_list_ptr)
553
554 DESCRIPTION
555
556 This module contains the routine which finds the names of any
557 loaded "images" in the current process. The argument in must be
558 NULL on the first call, and then the returned value must be passed
559 in on subsequent calls. This provides the capability to "step" down
560 the list of loaded objects. On the last object, a NULL value is
561 returned.
562
563 The arg and return value are "struct link_map" pointers, as defined
564 in <link.h>.
565 */
566
567 static struct so_list *
568 find_solib (so_list_ptr)
569 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
570 {
571 struct so_list *so_list_next = NULL;
572 struct link_map *lm = NULL;
573 struct so_list *new;
574
575 if (so_list_ptr == NULL)
576 {
577 /* We are setting up for a new scan through the loaded images. */
578 if ((so_list_next = so_list_head) == NULL)
579 {
580 /* We have not already read in the dynamic linking structures
581 from the inferior, lookup the address of the base structure. */
582 debug_base = locate_base ();
583 if (debug_base > 0)
584 {
585 /* Read the base structure in and find the address of the first
586 link map list member. */
587 lm = first_link_map_member ();
588 }
589 }
590 }
591 else
592 {
593 /* We have been called before, and are in the process of walking
594 the shared library list. Advance to the next shared object. */
595 if ((lm = LM_NEXT (so_list_ptr)) == NULL)
596 {
597 /* We have hit the end of the list, so check to see if any were
598 added, but be quiet if we can't read from the target any more. */
599 int status = target_read_memory ((CORE_ADDR) so_list_ptr -> lmaddr,
600 (char *) &(so_list_ptr -> lm),
601 sizeof (struct link_map));
602 if (status == 0)
603 {
604 lm = LM_NEXT (so_list_ptr);
605 }
606 else
607 {
608 lm = NULL;
609 }
610 }
611 so_list_next = so_list_ptr -> next;
612 }
613 if ((so_list_next == NULL) && (lm != NULL))
614 {
615 /* Get next link map structure from inferior image and build a local
616 abbreviated load_map structure */
617 new = (struct so_list *) xmalloc (sizeof (struct so_list));
618 (void) memset ((char *) new, 0, sizeof (struct so_list));
619 new -> lmaddr = lm;
620 /* Add the new node as the next node in the list, or as the root
621 node if this is the first one. */
622 if (so_list_ptr != NULL)
623 {
624 so_list_ptr -> next = new;
625 }
626 else
627 {
628 so_list_head = new;
629 }
630 so_list_next = new;
631 read_memory ((CORE_ADDR) lm, (char *) &(new -> lm),
632 sizeof (struct link_map));
633 /* For the SVR4 version, there is one entry that has no name
634 (for the inferior executable) since it is not a shared object. */
635 if (LM_NAME (new) != 0)
636 {
637 if (!target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
638 MAX_PATH_SIZE - 1))
639 error ("find_solib: Can't read pathname for load map\n");
640 new -> so_name[MAX_PATH_SIZE - 1] = 0;
641 solib_map_sections (new);
642 }
643 }
644 return (so_list_next);
645 }
646
647 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
648
649 static int
650 symbol_add_stub (arg)
651 char *arg;
652 {
653 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
654
655 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
656 (unsigned int) LM_ADDR (so), 0, 0, 0);
657 return (1);
658 }
659
660 /*
661
662 GLOBAL FUNCTION
663
664 solib_add -- add a shared library file to the symtab and section list
665
666 SYNOPSIS
667
668 void solib_add (char *arg_string, int from_tty,
669 struct target_ops *target)
670
671 DESCRIPTION
672
673 */
674
675 void
676 solib_add (arg_string, from_tty, target)
677 char *arg_string;
678 int from_tty;
679 struct target_ops *target;
680 {
681 register struct so_list *so = NULL; /* link map state variable */
682 char *re_err;
683 int count;
684 int old;
685
686 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
687 {
688 error ("Invalid regexp: %s", re_err);
689 }
690
691 /* Getting new symbols may change our opinion about what is
692 frameless. */
693 reinit_frame_cache ();
694
695 while ((so = find_solib (so)) != NULL)
696 {
697 if (so -> so_name[0] && re_exec (so -> so_name))
698 {
699 if (so -> symbols_loaded)
700 {
701 if (from_tty)
702 {
703 printf ("Symbols already loaded for %s\n", so -> so_name);
704 }
705 }
706 else
707 {
708 catch_errors (symbol_add_stub, (char *) so,
709 "Error while reading shared library symbols:\n");
710
711 special_symbol_handling (so);
712 so -> symbols_loaded = 1;
713 so -> from_tty = from_tty;
714 }
715 }
716 }
717
718 /* Now add the shared library sections to the section table of the
719 specified target, if any. */
720 if (target)
721 {
722 /* Count how many new section_table entries there are. */
723 so = NULL;
724 count = 0;
725 while ((so = find_solib (so)) != NULL)
726 {
727 if (so -> so_name[0])
728 {
729 count += so -> sections_end - so -> sections;
730 }
731 }
732
733 if (count)
734 {
735 /* Reallocate the target's section table including the new size. */
736 if (target -> to_sections)
737 {
738 old = target -> to_sections_end - target -> to_sections;
739 target -> to_sections = (struct section_table *)
740 realloc ((char *)target -> to_sections,
741 (sizeof (struct section_table)) * (count + old));
742 }
743 else
744 {
745 old = 0;
746 target -> to_sections = (struct section_table *)
747 malloc ((sizeof (struct section_table)) * count);
748 }
749 target -> to_sections_end = target -> to_sections + (count + old);
750
751 /* Add these section table entries to the target's table. */
752 while ((so = find_solib (so)) != NULL)
753 {
754 if (so -> so_name[0])
755 {
756 count = so -> sections_end - so -> sections;
757 bcopy (so -> sections, (char *)(target -> to_sections + old),
758 (sizeof (struct section_table)) * count);
759 old += count;
760 }
761 }
762 }
763 }
764 }
765
766 /*
767
768 LOCAL FUNCTION
769
770 info_sharedlibrary_command -- code for "info sharedlibrary"
771
772 SYNOPSIS
773
774 static void info_sharedlibrary_command ()
775
776 DESCRIPTION
777
778 Walk through the shared library list and print information
779 about each attached library.
780 */
781
782 static void
783 info_sharedlibrary_command ()
784 {
785 register struct so_list *so = NULL; /* link map state variable */
786 int header_done = 0;
787
788 if (exec_bfd == NULL)
789 {
790 printf ("No exec file.\n");
791 return;
792 }
793 while ((so = find_solib (so)) != NULL)
794 {
795 if (so -> so_name[0])
796 {
797 if (!header_done)
798 {
799 printf("%-12s%-12s%-12s%s\n", "From", "To", "Syms Read",
800 "Shared Object Library");
801 header_done++;
802 }
803 printf ("%-12s", local_hex_string_custom ((int) LM_ADDR (so), "08"));
804 printf ("%-12s", local_hex_string_custom (so -> lmend, "08"));
805 printf ("%-12s", so -> symbols_loaded ? "Yes" : "No");
806 printf ("%s\n", so -> so_name);
807 }
808 }
809 if (so_list_head == NULL)
810 {
811 printf ("No shared libraries loaded at this time.\n");
812 }
813 }
814
815 /*
816
817 GLOBAL FUNCTION
818
819 solib_address -- check to see if an address is in a shared lib
820
821 SYNOPSIS
822
823 int solib_address (CORE_ADDR address)
824
825 DESCRIPTION
826
827 Provides a hook for other gdb routines to discover whether or
828 not a particular address is within the mapped address space of
829 a shared library. Any address between the base mapping address
830 and the first address beyond the end of the last mapping, is
831 considered to be within the shared library address space, for
832 our purposes.
833
834 For example, this routine is called at one point to disable
835 breakpoints which are in shared libraries that are not currently
836 mapped in.
837 */
838
839 int
840 solib_address (address)
841 CORE_ADDR address;
842 {
843 register struct so_list *so = 0; /* link map state variable */
844
845 while ((so = find_solib (so)) != NULL)
846 {
847 if (so -> so_name[0])
848 {
849 if ((address >= (CORE_ADDR) LM_ADDR (so)) &&
850 (address < (CORE_ADDR) so -> lmend))
851 {
852 return (1);
853 }
854 }
855 }
856 return (0);
857 }
858
859 /* Called by free_all_symtabs */
860
861 void
862 clear_solib()
863 {
864 struct so_list *next;
865
866 while (so_list_head)
867 {
868 if (so_list_head -> sections)
869 {
870 free (so_list_head -> sections);
871 }
872 if (so_list_head -> so_bfd)
873 {
874 bfd_close (so_list_head -> so_bfd);
875 }
876 next = so_list_head -> next;
877 free(so_list_head);
878 so_list_head = next;
879 }
880 debug_base = 0;
881 }
882
883 /*
884
885 LOCAL FUNCTION
886
887 disable_break -- remove the "mapping changed" breakpoint
888
889 SYNOPSIS
890
891 static int disable_break ()
892
893 DESCRIPTION
894
895 Removes the breakpoint that gets hit when the dynamic linker
896 completes a mapping change.
897
898 */
899
900 static int
901 disable_break ()
902 {
903 int status = 1;
904
905 #ifndef SVR4_SHARED_LIBS
906
907 int in_debugger = 0;
908
909 /* Read the debugger structure from the inferior to retrieve the
910 address of the breakpoint and the original contents of the
911 breakpoint address. Remove the breakpoint by writing the original
912 contents back. */
913
914 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
915
916 /* Set `in_debugger' to zero now. */
917
918 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
919
920 breakpoint_addr = (CORE_ADDR) debug_copy.ldd_bp_addr;
921 write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst,
922 sizeof (debug_copy.ldd_bp_inst));
923
924 #else /* SVR4_SHARED_LIBS */
925
926 /* Note that breakpoint address and original contents are in our address
927 space, so we just need to write the original contents back. */
928
929 if (memory_remove_breakpoint (breakpoint_addr, shadow_contents) != 0)
930 {
931 status = 0;
932 }
933
934 #endif /* !SVR4_SHARED_LIBS */
935
936 /* For the SVR4 version, we always know the breakpoint address. For the
937 SunOS version we don't know it until the above code is executed.
938 Grumble if we are stopped anywhere besides the breakpoint address. */
939
940 if (stop_pc != breakpoint_addr)
941 {
942 warning ("stopped at unknown breakpoint while handling shared libraries");
943 }
944
945 return (status);
946 }
947
948 /*
949
950 LOCAL FUNCTION
951
952 enable_break -- arrange for dynamic linker to hit breakpoint
953
954 SYNOPSIS
955
956 int enable_break (void)
957
958 DESCRIPTION
959
960 Both the SunOS and the SVR4 dynamic linkers have, as part of their
961 debugger interface, support for arranging for the inferior to hit
962 a breakpoint after mapping in the shared libraries. This function
963 enables that breakpoint.
964
965 For SunOS, there is a special flag location (in_debugger) which we
966 set to 1. When the dynamic linker sees this flag set, it will set
967 a breakpoint at a location known only to itself, after saving the
968 original contents of that place and the breakpoint address itself,
969 in it's own internal structures. When we resume the inferior, it
970 will eventually take a SIGTRAP when it runs into the breakpoint.
971 We handle this (in a different place) by restoring the contents of
972 the breakpointed location (which is only known after it stops),
973 chasing around to locate the shared libraries that have been
974 loaded, then resuming.
975
976 For SVR4, the debugger interface structure contains a member (r_brk)
977 which is statically initialized at the time the shared library is
978 built, to the offset of a function (_r_debug_state) which is guaran-
979 teed to be called once before mapping in a library, and again when
980 the mapping is complete. At the time we are examining this member,
981 it contains only the unrelocated offset of the function, so we have
982 to do our own relocation. Later, when the dynamic linker actually
983 runs, it relocates r_brk to be the actual address of _r_debug_state().
984
985 The debugger interface structure also contains an enumeration which
986 is set to either RT_ADD or RT_DELETE prior to changing the mapping,
987 depending upon whether or not the library is being mapped or unmapped,
988 and then set to RT_CONSISTENT after the library is mapped/unmapped.
989 */
990
991 static int
992 enable_break ()
993 {
994
995 int j;
996
997 #ifndef SVR4_SHARED_LIBS
998
999 int in_debugger;
1000
1001 /* Get link_dynamic structure */
1002
1003 j = target_read_memory (debug_base, (char *) &dynamic_copy,
1004 sizeof (dynamic_copy));
1005 if (j)
1006 {
1007 /* unreadable */
1008 return (0);
1009 }
1010
1011 /* Calc address of debugger interface structure */
1012
1013 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
1014
1015 /* Calc address of `in_debugger' member of debugger interface structure */
1016
1017 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
1018 (char *) &debug_copy);
1019
1020 /* Write a value of 1 to this member. */
1021
1022 in_debugger = 1;
1023
1024 write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger));
1025
1026 #else /* SVR4_SHARED_LIBS */
1027
1028 #ifdef BKPT_AT_MAIN
1029
1030 struct minimal_symbol *msymbol;
1031
1032 msymbol = lookup_minimal_symbol ("main", symfile_objfile);
1033 if ((msymbol != NULL) && (msymbol -> address != 0))
1034 {
1035 breakpoint_addr = msymbol -> address;
1036 }
1037 else
1038 {
1039 return (0);
1040 }
1041
1042 if (target_insert_breakpoint (breakpoint_addr, shadow_contents) != 0)
1043 {
1044 return (0);
1045 }
1046
1047 #else /* !BKPT_AT_MAIN */
1048
1049 struct symtab_and_line sal;
1050
1051 /* Read the debugger interface structure directly. */
1052
1053 read_memory (debug_base, (char *) &debug_copy, sizeof (debug_copy));
1054
1055 /* Set breakpoint at the debugger interface stub routine that will
1056 be called just prior to each mapping change and again after the
1057 mapping change is complete. Set up the (nonexistent) handler to
1058 deal with hitting these breakpoints. (FIXME). */
1059
1060 warning ("'%s': line %d: missing SVR4 support code", __FILE__, __LINE__);
1061
1062 #endif /* BKPT_AT_MAIN */
1063
1064 #endif /* !SVR4_SHARED_LIBS */
1065
1066 return (1);
1067 }
1068
1069 /*
1070
1071 GLOBAL FUNCTION
1072
1073 solib_create_inferior_hook -- shared library startup support
1074
1075 SYNOPSIS
1076
1077 void solib_create_inferior_hook()
1078
1079 DESCRIPTION
1080
1081 When gdb starts up the inferior, it nurses it along (through the
1082 shell) until it is ready to execute it's first instruction. At this
1083 point, this function gets called via expansion of the macro
1084 SOLIB_CREATE_INFERIOR_HOOK.
1085
1086 For both SunOS shared libraries, and SVR4 shared libraries, we
1087 can arrange to cooperate with the dynamic linker to discover the
1088 names of shared libraries that are dynamically linked, and the
1089 base addresses to which they are linked.
1090
1091 This function is responsible for discovering those names and
1092 addresses, and saving sufficient information about them to allow
1093 their symbols to be read at a later time.
1094
1095 FIXME
1096
1097 Between enable_break() and disable_break(), this code does not
1098 properly handle hitting breakpoints which the user might have
1099 set in the startup code or in the dynamic linker itself. Proper
1100 handling will probably have to wait until the implementation is
1101 changed to use the "breakpoint handler function" method.
1102
1103 Also, what if child has exit()ed? Must exit loop somehow.
1104 */
1105
1106 void
1107 solib_create_inferior_hook()
1108 {
1109 CORE_ADDR debug_addr;
1110 int in_debugger;
1111 CORE_ADDR in_debugger_addr;
1112 CORE_ADDR breakpoint_addr;
1113 int i, j;
1114
1115 if ((debug_base = locate_base ()) == 0)
1116 {
1117 /* Can't find the symbol or the executable is statically linked. */
1118 return;
1119 }
1120
1121 if (!enable_break ())
1122 {
1123 warning ("shared library handler failed to enable breakpoint");
1124 return;
1125 }
1126
1127 /* Now run the target. It will eventually hit the breakpoint, at
1128 which point all of the libraries will have been mapped in and we
1129 can go groveling around in the dynamic linker structures to find
1130 out what we need to know about them. */
1131
1132 clear_proceed_status ();
1133 stop_soon_quietly = 1;
1134 stop_signal = 0;
1135 do
1136 {
1137 target_resume (0, stop_signal);
1138 wait_for_inferior ();
1139 }
1140 while (stop_signal != SIGTRAP);
1141 stop_soon_quietly = 0;
1142
1143 /* We are now either at the "mapping complete" breakpoint (or somewhere
1144 else, a condition we aren't prepared to deal with anyway), so adjust
1145 the PC as necessary after a breakpoint, disable the breakpoint, and
1146 add any shared libraries that were mapped in. */
1147
1148 if (DECR_PC_AFTER_BREAK)
1149 {
1150 stop_pc -= DECR_PC_AFTER_BREAK;
1151 write_register (PC_REGNUM, stop_pc);
1152 }
1153
1154 if (!disable_break ())
1155 {
1156 warning ("shared library handler failed to disable breakpoint");
1157 }
1158
1159 solib_add ((char *) 0, 0, (struct target_ops *) 0);
1160 }
1161
1162 /*
1163
1164 LOCAL FUNCTION
1165
1166 special_symbol_handling -- additional shared library symbol handling
1167
1168 SYNOPSIS
1169
1170 void special_symbol_handling (struct so_list *so)
1171
1172 DESCRIPTION
1173
1174 Once the symbols from a shared object have been loaded in the usual
1175 way, we are called to do any system specific symbol handling that
1176 is needed.
1177
1178 For Suns, this consists of grunging around in the dynamic linkers
1179 structures to find symbol definitions for "common" symbols and
1180 adding them to the minimal symbol table for the corresponding
1181 objfile.
1182
1183 */
1184
1185 static void
1186 special_symbol_handling (so)
1187 struct so_list *so;
1188 {
1189 #ifndef SVR4_SHARED_LIBS
1190
1191 /* Read the debugger structure from the inferior, just to make sure
1192 we have a current copy. */
1193
1194 read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy));
1195
1196 /* Get common symbol definitions for the loaded object. */
1197
1198 if (debug_copy.ldd_cp)
1199 {
1200 solib_add_common_symbols (debug_copy.ldd_cp, so -> objfile);
1201 }
1202
1203 #endif /* !SVR4_SHARED_LIBS */
1204 }
1205
1206
1207 /*
1208
1209 LOCAL FUNCTION
1210
1211 sharedlibrary_command -- handle command to explicitly add library
1212
1213 SYNOPSIS
1214
1215 static void sharedlibrary_command (char *args, int from_tty)
1216
1217 DESCRIPTION
1218
1219 */
1220
1221 static void
1222 sharedlibrary_command (args, from_tty)
1223 char *args;
1224 int from_tty;
1225 {
1226 dont_repeat ();
1227 solib_add (args, from_tty, (struct target_ops *) 0);
1228 }
1229
1230 void
1231 _initialize_solib()
1232 {
1233
1234 add_com ("sharedlibrary", class_files, sharedlibrary_command,
1235 "Load shared object library symbols for files matching REGEXP.");
1236 add_info ("sharedlibrary", info_sharedlibrary_command,
1237 "Status of loaded shared object libraries.");
1238 }
This page took 0.057505 seconds and 5 git commands to generate.