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