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