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