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