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