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