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