Reference: gdb-pend/657
[deliverable/binutils-gdb.git] / gdb / solib.c
CommitLineData
f8b76e70
FF
1/* Handle SunOS and SVR4 shared libraries for GDB, the GNU Debugger.
2 Copyright 1990, 1991 Free Software Foundation, Inc.
3
bd5635a1
RP
4This file is part of GDB.
5
bdbd5f50 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
bdbd5f50
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
bdbd5f50 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
bdbd5f50
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
f8b76e70 20
bd5635a1 21#include <sys/types.h>
f8b76e70 22#include <signal.h>
bd5635a1
RP
23#include <string.h>
24#include <link.h>
d0237a54
JK
25#include <sys/param.h>
26#include <fcntl.h>
27#include <stdio.h>
d261ece7 28#include <a.out.h>
f8b76e70 29
bd5635a1 30#include "defs.h"
bd5635a1
RP
31#include "symtab.h"
32#include "gdbcore.h"
33#include "command.h"
b3fdaf3d 34#include "target.h"
2403f49b 35#include "frame.h"
bdbd5f50
JG
36#include "regex.h"
37#include "inferior.h"
38
f8b76e70
FF
39extern char *getenv ();
40extern char *elf_interpreter (); /* Interpreter name from exec file */
41extern 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
d261ece7 59#ifndef SVR4_SHARED_LIBS
f8b76e70
FF
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)
65static struct link_dynamic dynamic_copy;
66static struct link_dynamic_2 ld_2_copy;
67static struct ld_debug debug_copy;
68static CORE_ADDR debug_addr;
69static CORE_ADDR flag_addr;
70
d261ece7 71#else /* SVR4_SHARED_LIBS */
f8b76e70
FF
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)
77static struct r_debug debug_copy;
f8b76e70
FF
78char shadow_contents[BREAKPOINT_MAX]; /* Stash old bkpt addr contents */
79extern CORE_ADDR proc_base_address ();
80extern int proc_address_to_fd ();
81
d261ece7 82#endif /* !SVR4_SHARED_LIBS */
bd5635a1 83
bd5635a1 84struct so_list {
f8b76e70
FF
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;
bd5635a1
RP
95};
96
f8b76e70
FF
97static struct so_list *so_list_head; /* List of known shared objects */
98static CORE_ADDR debug_base; /* Base of dynamic linker structures */
99static CORE_ADDR breakpoint_addr; /* Address where end bkpt is set */
100
bd5635a1 101
d0237a54 102/*
f8b76e70
FF
103
104LOCAL FUNCTION
105
106 solib_map_sections -- open bfd and build sections for shared lib
107
108SYNOPSIS
109
110 static void solib_map_sections (struct so_list *so)
111
112DESCRIPTION
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
120FIXMES
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
d0237a54 129static void
f8b76e70
FF
130solib_map_sections (so)
131 struct so_list *so;
d0237a54
JK
132{
133 char *filename;
134 char *scratch_pathname;
135 int scratch_chan;
136 struct section_table *p;
137
f8b76e70 138 filename = tilde_expand (so -> so_name);
d0237a54
JK
139 make_cleanup (free, filename);
140
141 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
f8b76e70 142 &scratch_pathname);
d0237a54 143 if (scratch_chan < 0)
f8b76e70
FF
144 {
145 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
146 O_RDONLY, 0, &scratch_pathname);
147 }
d0237a54 148 if (scratch_chan < 0)
f8b76e70
FF
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
d261ece7
SG
181/* Read all dynamically loaded common symbol definitions from the inferior
182 and add them to the misc_function_vector. */
183
184static void
185solib_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
f8b76e70
FF
232/*
233
234LOCAL FUNCTION
235
236 bfd_lookup_symbol -- lookup the value for a specific symbol
237
238SYNOPSIS
239
240 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
241
242DESCRIPTION
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
257static CORE_ADDR
258DEFUN (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++)
d0237a54 280 {
f8b76e70
FF
281 sym = *symbol_table++;
282 if (strcmp (sym -> name, symname) == 0)
283 {
284 symaddr = sym -> value;
285 break;
286 }
d0237a54 287 }
f8b76e70 288 do_cleanups (back_to);
d0237a54 289 }
f8b76e70 290 return (symaddr);
d0237a54
JK
291}
292
f8b76e70
FF
293/*
294
d261ece7
SG
295LOCAL FUNCTION
296
297 look_for_base -- examine file for each mapped address segment
298
299SYNOPSYS
300
301 static int look_for_base (int fd, CORE_ADDR baseaddr)
302
303DESCRIPTION
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
319static int
320DEFUN (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
f8b76e70
FF
378LOCAL FUNCTION
379
380 locate_base -- locate the base address of dynamic linker structs
381
382SYNOPSIS
383
384 CORE_ADDR locate_base (void)
385
386DESCRIPTION
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
d261ece7 391 locating all of the dynamic linker's runtime structures. This
f8b76e70
FF
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
d261ece7 401 file's symbols were read, and all we have to do is look it up there.
f8b76e70
FF
402
403 The SVR4 version is much more complicated because the dynamic linker
d261ece7
SG
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
f8b76e70 407 of this complexity, we cache the value we find and return that value
d261ece7 408 on subsequent invocations.
f8b76e70 409
d261ece7
SG
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).
f8b76e70
FF
415
416 */
417
418static CORE_ADDR
419locate_base ()
420{
f8b76e70 421
d261ece7 422#ifndef SVR4_SHARED_LIBS
f8b76e70
FF
423
424 int i;
d261ece7 425 CORE_ADDR address = 0;
f8b76e70
FF
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 }
d261ece7 432 return (address);
f8b76e70 433
d261ece7 434#else /* SVR4_SHARED_LIBS */
f8b76e70 435
d261ece7
SG
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. */
f8b76e70 442
d261ece7 443 if (debug_base == 0)
f8b76e70 444 {
d261ece7 445 proc_iterate_over_mappings (look_for_base);
f8b76e70 446 }
d261ece7 447 return (debug_base);
f8b76e70 448
d261ece7 449#endif /* !SVR4_SHARED_LIBS */
f8b76e70
FF
450
451}
bd5635a1 452
f8b76e70
FF
453static struct link_map *
454first_link_map_member ()
bd5635a1 455{
f8b76e70
FF
456 struct link_map *lm = NULL;
457
d261ece7 458#ifndef SVR4_SHARED_LIBS
f8b76e70
FF
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
d261ece7 470#else /* SVR4_SHARED_LIBS */
f8b76e70
FF
471
472 read_memory (debug_base, &debug_copy, sizeof (struct r_debug));
473 lm = debug_copy.r_map;
474
d261ece7 475#endif /* !SVR4_SHARED_LIBS */
d0237a54 476
f8b76e70
FF
477 return (lm);
478}
479
480/*
481
482GLOBAL FUNCTION
483
484 find_solib -- step through list of shared objects
485
486SYNOPSIS
487
488 struct so_list *find_solib (struct so_list *so_list_ptr)
489
490DESCRIPTION
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.
d0237a54 498
f8b76e70
FF
499 The arg and return value are "struct link_map" pointers, as defined
500 in <link.h>.
501 */
d0237a54 502
f8b76e70
FF
503struct so_list *
504find_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 {
d261ece7 572 (void) target_read_string((CORE_ADDR) LM_NAME (new), new -> so_name,
f8b76e70
FF
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);
bd5635a1 579}
d0237a54 580
bdbd5f50
JG
581/* A small stub to get us past the arg-passing pinhole of catch_errors. */
582
583static int
584symbol_add_stub (arg)
585 char *arg;
d0237a54 586{
f8b76e70
FF
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);
d0237a54 592}
bd5635a1 593
f8b76e70
FF
594/*
595
596GLOBAL FUNCTION
597
598 solib_add -- add a shared library file to the symtab and section list
599
600SYNOPSIS
601
602 void solib_add (char *arg_string, int from_tty,
603 struct target_ops *target)
604
605DESCRIPTION
606
607*/
bdbd5f50
JG
608
609void
610solib_add (arg_string, from_tty, target)
611 char *arg_string;
612 int from_tty;
613 struct target_ops *target;
bd5635a1 614{
f8b76e70
FF
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
bdbd5f50
JG
625 /* Getting new symbols may change our opinion about what is
626 frameless. */
627 reinit_frame_cache ();
bdbd5f50 628
f8b76e70
FF
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 {
bdbd5f50 635 if (from_tty)
f8b76e70
FF
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
bdbd5f50
JG
650 /* Now add the shared library sections to the section table of the
651 specified target, if any. */
f8b76e70
FF
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 }
bd5635a1 696}
bdbd5f50 697
f8b76e70 698/*
bd5635a1 699
f8b76e70
FF
700LOCAL FUNCTION
701
702 info_sharedlibrary_command -- code for "info sharedlibrary"
703
704SYNOPSIS
705
706 static void info_sharedlibrary_command ()
707
708DESCRIPTION
bd5635a1 709
f8b76e70
FF
710 Walk through the shared library list and print information
711 about each attached library.
712*/
713
714static void
715info_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);
bd5635a1 739 }
bd5635a1 740 }
f8b76e70
FF
741 if (so_list_head == NULL)
742 {
743 printf ("No shared libraries loaded at this time.\n");
bd5635a1
RP
744 }
745}
746
747/*
f8b76e70
FF
748
749GLOBAL FUNCTION
750
751 solib_address -- check to see if an address is in a shared lib
752
753SYNOPSIS
754
755 int solib_address (CORE_ADDR address)
756
757DESCRIPTION
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
bd5635a1 771int
f8b76e70 772solib_address (address)
bd5635a1
RP
773 CORE_ADDR address;
774{
f8b76e70
FF
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 */
bd5635a1 792
f8b76e70
FF
793void
794clear_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;
bd5635a1 811 }
f8b76e70 812 debug_base = 0;
bd5635a1
RP
813}
814
815/*
f8b76e70
FF
816
817LOCAL FUNCTION
818
819 disable_break -- remove the "mapping changed" breakpoint
820
821SYNOPSIS
822
823 static int disable_break ()
824
825DESCRIPTION
826
827 Removes the breakpoint that gets hit when the dynamic linker
828 completes a mapping change.
829
bd5635a1 830*/
f8b76e70
FF
831
832static int
833disable_break ()
bd5635a1 834{
f8b76e70
FF
835 int status = 1;
836
d261ece7 837#ifndef SVR4_SHARED_LIBS
f8b76e70
FF
838
839 int in_debugger = 0;
840
f8b76e70
FF
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));
d261ece7
SG
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
f8b76e70
FF
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
d261ece7 860#else /* SVR4_SHARED_LIBS */
f8b76e70
FF
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
d261ece7 870#endif /* !SVR4_SHARED_LIBS */
f8b76e70
FF
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);
bdbd5f50
JG
882}
883
f8b76e70 884/*
bdbd5f50 885
f8b76e70
FF
886LOCAL FUNCTION
887
888 enable_break -- arrange for dynamic linker to hit breakpoint
889
890SYNOPSIS
891
892 int enable_break (void)
893
894DESCRIPTION
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
927static int
928enable_break ()
bdbd5f50 929{
bdbd5f50 930
f8b76e70
FF
931 int j;
932
d261ece7 933#ifndef SVR4_SHARED_LIBS
bdbd5f50 934
f8b76e70
FF
935 int in_debugger;
936
bdbd5f50 937 /* Get link_dynamic structure */
f8b76e70
FF
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 }
06b6c733 946
bdbd5f50 947 /* Calc address of debugger interface structure */
f8b76e70
FF
948
949 debug_addr = (CORE_ADDR) dynamic_copy.ldd;
950
bdbd5f50 951 /* Calc address of `in_debugger' member of debugger interface structure */
f8b76e70
FF
952
953 flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger -
954 (char *) &debug_copy);
955
bdbd5f50 956 /* Write a value of 1 to this member. */
f8b76e70 957
bdbd5f50 958 in_debugger = 1;
bdbd5f50 959
f8b76e70
FF
960 write_memory (flag_addr, &in_debugger, sizeof (in_debugger));
961
d261ece7 962#else /* SVR4_SHARED_LIBS */
f8b76e70
FF
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
d261ece7 1000#endif /* !SVR4_SHARED_LIBS */
f8b76e70
FF
1001
1002 return (1);
1003}
1004
1005/*
1006
1007GLOBAL FUNCTION
1008
1009 solib_create_inferior_hook -- shared library startup support
1010
1011SYNOPSIS
1012
1013 void solib_create_inferior_hook()
1014
1015DESCRIPTION
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
1031FIXME
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
1042void
1043solib_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. */
bdbd5f50
JG
1067
1068 clear_proceed_status ();
1069 stop_soon_quietly = 1;
f8b76e70
FF
1070 stop_signal = 0;
1071 do
bdbd5f50 1072 {
bdbd5f50
JG
1073 target_resume (0, stop_signal);
1074 wait_for_inferior ();
1075 }
f8b76e70 1076 while (stop_signal != SIGTRAP);
bdbd5f50 1077 stop_soon_quietly = 0;
f8b76e70
FF
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. */
bdbd5f50 1083
f8b76e70
FF
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);
bdbd5f50
JG
1096}
1097
f8b76e70
FF
1098/*
1099
1100GLOBAL FUNCTION
1101
1102 sharedlibrary_command -- handle command to explicitly add library
1103
1104SYNOPSIS
1105
1106 void sharedlibrary_command (char *args, int from_tty)
1107
1108DESCRIPTION
1109
1110*/
1111
bdbd5f50
JG
1112void
1113sharedlibrary_command (args, from_tty)
f8b76e70
FF
1114char *args;
1115int from_tty;
bdbd5f50 1116{
f8b76e70
FF
1117 dont_repeat ();
1118 solib_add (args, from_tty, (struct target_ops *) 0);
bd5635a1
RP
1119}
1120
1121void
1122_initialize_solib()
1123{
f8b76e70
FF
1124
1125 add_com ("sharedlibrary", class_files, sharedlibrary_command,
bd5635a1 1126 "Load shared object library symbols for files matching REGEXP.");
f8b76e70
FF
1127 add_info ("sharedlibrary", info_sharedlibrary_command,
1128 "Status of loaded shared object libraries.");
bd5635a1 1129}
This page took 0.09264 seconds and 4 git commands to generate.