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