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