Update texinfo.tex to version 2000-05-28.15.
[deliverable/binutils-gdb.git] / gdb / osfsolib.c
CommitLineData
c906108c
SS
1/* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 94, 95, 96, 98, 1999 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
c906108c 10
c5aa993b
JM
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21/* FIXME: Most of this code could be merged with solib.c by using
22 next_link_map_member and xfer_link_map_member in solib.c. */
23
24#include "defs.h"
25
26#include <sys/types.h>
27#include <signal.h>
28#include "gdb_string.h"
29#include <fcntl.h>
30
31#include "symtab.h"
32#include "bfd.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "gdbcore.h"
36#include "command.h"
37#include "target.h"
38#include "frame.h"
88987551 39#include "gdb_regex.h"
c906108c
SS
40#include "inferior.h"
41#include "language.h"
42#include "gdbcmd.h"
43
c5aa993b 44#define MAX_PATH_SIZE 1024 /* FIXME: Should be dynamic */
c906108c
SS
45
46/* When handling shared libraries, GDB has to find out the pathnames
47 of all shared libraries that are currently loaded (to read in their
48 symbols) and where the shared libraries are loaded in memory
49 (to relocate them properly from their prelinked addresses to the
50 current load address).
51
52 Under OSF/1 there are two possibilities to get at this information:
53 1) Peek around in the runtime loader structures.
c5aa993b
JM
54 These are not documented, and they are not defined in the system
55 header files. The definitions below were obtained by experimentation,
56 but they seem stable enough.
c906108c 57 2) Use the undocumented libxproc.a library, which contains the
c5aa993b
JM
58 equivalent ldr_* routines.
59 This approach is somewhat cleaner, but it requires that the GDB
60 executable is dynamically linked. In addition it requires a
61 NAT_CLIBS= -lxproc -Wl,-expect_unresolved,ldr_process_context
62 linker specification for GDB and all applications that are using
63 libgdb.
c906108c
SS
64 We will use the peeking approach until it becomes unwieldy. */
65
66#ifndef USE_LDR_ROUTINES
67
68/* Definition of runtime loader structures, found by experimentation. */
69#define RLD_CONTEXT_ADDRESS 0x3ffc0000000
70
71typedef struct
c5aa993b
JM
72 {
73 CORE_ADDR next;
74 CORE_ADDR previous;
75 CORE_ADDR unknown1;
76 char *module_name;
77 CORE_ADDR modinfo_addr;
78 long module_id;
79 CORE_ADDR unknown2;
80 CORE_ADDR unknown3;
81 long region_count;
82 CORE_ADDR regioninfo_addr;
83 }
84ldr_module_info_t;
c906108c
SS
85
86typedef struct
c5aa993b
JM
87 {
88 long unknown1;
89 CORE_ADDR regionname_addr;
90 long protection;
91 CORE_ADDR vaddr;
92 CORE_ADDR mapaddr;
93 long size;
94 long unknown2[5];
95 }
96ldr_region_info_t;
c906108c
SS
97
98typedef struct
c5aa993b
JM
99 {
100 CORE_ADDR unknown1;
101 CORE_ADDR unknown2;
102 CORE_ADDR head;
103 CORE_ADDR tail;
104 }
105ldr_context_t;
c906108c
SS
106
107static ldr_context_t ldr_context;
108
109#else
110
111#include <loader.h>
112static ldr_process_t fake_ldr_process;
113
114/* Called by ldr_* routines to read memory from the current target. */
115
a14ed312 116static int ldr_read_memory (CORE_ADDR, char *, int, int);
c906108c
SS
117
118static int
119ldr_read_memory (memaddr, myaddr, len, readstring)
120 CORE_ADDR memaddr;
121 char *myaddr;
122 int len;
123 int readstring;
124{
125 int result;
126 char *buffer;
127
128 if (readstring)
129 {
130 target_read_string (memaddr, &buffer, len, &result);
131 if (result == 0)
c5aa993b 132 strcpy (myaddr, buffer);
c906108c
SS
133 free (buffer);
134 }
135 else
136 result = target_read_memory (memaddr, myaddr, len);
137
138 if (result != 0)
139 result = -result;
140 return result;
141}
142
143#endif
144
145/* Define our own link_map structure.
146 This will help to share code with solib.c. */
147
c5aa993b
JM
148struct link_map
149{
150 CORE_ADDR l_offset; /* prelink to load address offset */
151 char *l_name; /* full name of loaded object */
c906108c
SS
152 ldr_module_info_t module_info; /* corresponding module info */
153};
154
155#define LM_OFFSET(so) ((so) -> lm.l_offset)
156#define LM_NAME(so) ((so) -> lm.l_name)
157
c5aa993b
JM
158struct so_list
159 {
160 struct so_list *next; /* next structure in linked list */
161 struct link_map lm; /* copy of link map from inferior */
162 struct link_map *lmaddr; /* addr in inferior lm was read from */
163 CORE_ADDR lmend; /* upper addr bound of mapped object */
164 char so_name[MAX_PATH_SIZE]; /* shared object lib name (FIXME) */
165 char symbols_loaded; /* flag: symbols read in yet? */
166 char from_tty; /* flag: print msgs? */
167 struct objfile *objfile; /* objfile for loaded lib */
168 struct section_table *sections;
169 struct section_table *sections_end;
170 struct section_table *textsection;
171 bfd *abfd;
172 };
c906108c
SS
173
174static struct so_list *so_list_head; /* List of known shared objects */
175
a14ed312 176extern int fdmatch (int, int); /* In libiberty */
c906108c
SS
177
178/* Local function prototypes */
179
a14ed312 180static void sharedlibrary_command (char *, int);
c906108c 181
a14ed312 182static void info_sharedlibrary_command (char *, int);
c906108c 183
a14ed312 184static int symbol_add_stub (char *);
c906108c 185
a14ed312 186static struct so_list *find_solib (struct so_list *);
c906108c 187
a14ed312 188static struct link_map *first_link_map_member (void);
c906108c 189
a14ed312 190static struct link_map *next_link_map_member (struct so_list *);
c906108c 191
a14ed312 192static void xfer_link_map_member (struct so_list *, struct link_map *);
c906108c 193
a14ed312 194static int solib_map_sections (char *);
c906108c
SS
195
196/*
197
c5aa993b 198 LOCAL FUNCTION
c906108c 199
c5aa993b 200 solib_map_sections -- open bfd and build sections for shared lib
c906108c 201
c5aa993b 202 SYNOPSIS
c906108c 203
c5aa993b 204 static int solib_map_sections (struct so_list *so)
c906108c 205
c5aa993b 206 DESCRIPTION
c906108c 207
c5aa993b
JM
208 Given a pointer to one of the shared objects in our list
209 of mapped objects, use the recorded name to open a bfd
210 descriptor for the object, build a section table, and then
211 relocate all the section addresses by the base address at
212 which the shared object was mapped.
c906108c 213
c5aa993b 214 FIXMES
c906108c 215
c5aa993b
JM
216 In most (all?) cases the shared object file name recorded in the
217 dynamic linkage tables will be a fully qualified pathname. For
218 cases where it isn't, do we really mimic the systems search
219 mechanism correctly in the below code (particularly the tilde
220 expansion stuff?).
c906108c
SS
221 */
222
223static int
224solib_map_sections (arg)
225 char *arg;
226{
227 struct so_list *so = (struct so_list *) arg; /* catch_errors bogon */
228 char *filename;
229 char *scratch_pathname;
230 int scratch_chan;
231 struct section_table *p;
232 struct cleanup *old_chain;
233 bfd *abfd;
c5aa993b
JM
234
235 filename = tilde_expand (so->so_name);
c906108c 236 old_chain = make_cleanup (free, filename);
c5aa993b 237
c906108c
SS
238 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
239 &scratch_pathname);
240 if (scratch_chan < 0)
241 {
242 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
243 O_RDONLY, 0, &scratch_pathname);
244 }
245 if (scratch_chan < 0)
246 {
247 perror_with_name (filename);
248 }
249 /* Leave scratch_pathname allocated. bfd->name will point to it. */
250
251 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
252 if (!abfd)
253 {
254 close (scratch_chan);
255 error ("Could not open `%s' as an executable file: %s",
256 scratch_pathname, bfd_errmsg (bfd_get_error ()));
257 }
258 /* Leave bfd open, core_xfer_memory and "info files" need it. */
c5aa993b
JM
259 so->abfd = abfd;
260 abfd->cacheable = true;
c906108c
SS
261
262 if (!bfd_check_format (abfd, bfd_object))
263 {
264 error ("\"%s\": not in executable format: %s.",
265 scratch_pathname, bfd_errmsg (bfd_get_error ()));
266 }
c5aa993b 267 if (build_section_table (abfd, &so->sections, &so->sections_end))
c906108c 268 {
c5aa993b 269 error ("Can't find the file sections in `%s': %s",
c906108c
SS
270 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
271 }
272
c5aa993b 273 for (p = so->sections; p < so->sections_end; p++)
c906108c
SS
274 {
275 /* Relocate the section binding addresses as recorded in the shared
c5aa993b
JM
276 object's file by the offset to get the address to which the
277 object was actually mapped. */
278 p->addr += LM_OFFSET (so);
279 p->endaddr += LM_OFFSET (so);
280 so->lmend = (CORE_ADDR) max (p->endaddr, so->lmend);
281 if (STREQ (p->the_bfd_section->name, ".text"))
c906108c 282 {
c5aa993b 283 so->textsection = p;
c906108c
SS
284 }
285 }
286
287 /* Free the file names, close the file now. */
288 do_cleanups (old_chain);
289
290 return (1);
291}
292
293/*
294
c5aa993b 295 LOCAL FUNCTION
c906108c 296
c5aa993b 297 first_link_map_member -- locate first member in dynamic linker's map
c906108c 298
c5aa993b 299 SYNOPSIS
c906108c 300
c5aa993b 301 static struct link_map *first_link_map_member (void)
c906108c 302
c5aa993b 303 DESCRIPTION
c906108c 304
c5aa993b
JM
305 Read in a copy of the first member in the inferior's dynamic
306 link map from the inferior's dynamic linker structures, and return
307 a pointer to the copy in our address space.
308 */
c906108c
SS
309
310static struct link_map *
311first_link_map_member ()
312{
313 struct link_map *lm = NULL;
314 static struct link_map first_lm;
315
316#ifdef USE_LDR_ROUTINES
317 ldr_module_t mod_id = LDR_NULL_MODULE;
318 size_t retsize;
319
320 fake_ldr_process = ldr_core_process ();
321 ldr_set_core_reader (ldr_read_memory);
322 ldr_xdetach (fake_ldr_process);
323 if (ldr_xattach (fake_ldr_process) != 0
c5aa993b 324 || ldr_next_module (fake_ldr_process, &mod_id) != 0
c906108c 325 || mod_id == LDR_NULL_MODULE
c5aa993b
JM
326 || ldr_inq_module (fake_ldr_process, mod_id,
327 &first_lm.module_info, sizeof (ldr_module_info_t),
328 &retsize) != 0)
c906108c
SS
329 return lm;
330#else
331 CORE_ADDR ldr_context_addr;
332
333 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
334 (char *) &ldr_context_addr,
335 sizeof (CORE_ADDR)) != 0
336 || target_read_memory (ldr_context_addr,
337 (char *) &ldr_context,
338 sizeof (ldr_context_t)) != 0
339 || target_read_memory ((CORE_ADDR) ldr_context.head,
340 (char *) &first_lm.module_info,
341 sizeof (ldr_module_info_t)) != 0)
342 return lm;
343#endif
344
345 lm = &first_lm;
346
347 /* The first entry is for the main program and should be skipped. */
348 lm->l_name = NULL;
349
350 return lm;
351}
352
353static struct link_map *
354next_link_map_member (so_list_ptr)
355 struct so_list *so_list_ptr;
356{
357 struct link_map *lm = NULL;
358 static struct link_map next_lm;
359#ifdef USE_LDR_ROUTINES
360 ldr_module_t mod_id = so_list_ptr->lm.module_info.lmi_modid;
361 size_t retsize;
362
c5aa993b 363 if (ldr_next_module (fake_ldr_process, &mod_id) != 0
c906108c 364 || mod_id == LDR_NULL_MODULE
c5aa993b
JM
365 || ldr_inq_module (fake_ldr_process, mod_id,
366 &next_lm.module_info, sizeof (ldr_module_info_t),
367 &retsize) != 0)
c906108c
SS
368 return lm;
369
370 lm = &next_lm;
371 lm->l_name = lm->module_info.lmi_name;
372#else
373 CORE_ADDR ldr_context_addr;
374
375 /* Reread context in case ldr_context.tail was updated. */
376
377 if (target_read_memory ((CORE_ADDR) RLD_CONTEXT_ADDRESS,
378 (char *) &ldr_context_addr,
379 sizeof (CORE_ADDR)) != 0
380 || target_read_memory (ldr_context_addr,
381 (char *) &ldr_context,
382 sizeof (ldr_context_t)) != 0
383 || so_list_ptr->lm.module_info.modinfo_addr == ldr_context.tail
384 || target_read_memory (so_list_ptr->lm.module_info.next,
385 (char *) &next_lm.module_info,
386 sizeof (ldr_module_info_t)) != 0)
387 return lm;
388
389 lm = &next_lm;
390 lm->l_name = lm->module_info.module_name;
391#endif
392 return lm;
393}
394
395static void
396xfer_link_map_member (so_list_ptr, lm)
397 struct so_list *so_list_ptr;
398 struct link_map *lm;
399{
400 int i;
401 so_list_ptr->lm = *lm;
402
403 /* OSF/1 shared libraries are pre-linked to particular addresses,
404 but the runtime loader may have to relocate them if the
405 address ranges of the libraries used by the target executable clash,
406 or if the target executable is linked with the -taso option.
407 The offset is the difference between the address where the shared
408 library is mapped and the pre-linked address of the shared library.
409
410 FIXME: GDB is currently unable to relocate the shared library
411 sections by different offsets. If sections are relocated by
412 different offsets, put out a warning and use the offset of the
413 first section for all remaining sections. */
414 LM_OFFSET (so_list_ptr) = 0;
415
416 /* There is one entry that has no name (for the inferior executable)
417 since it is not a shared object. */
418 if (LM_NAME (so_list_ptr) != 0)
419 {
420
421#ifdef USE_LDR_ROUTINES
422 int len = strlen (LM_NAME (so_list_ptr) + 1);
423
424 if (len > MAX_PATH_SIZE)
425 len = MAX_PATH_SIZE;
426 strncpy (so_list_ptr->so_name, LM_NAME (so_list_ptr), MAX_PATH_SIZE);
427 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
428
429 for (i = 0; i < lm->module_info.lmi_nregion; i++)
430 {
431 ldr_region_info_t region_info;
432 size_t retsize;
433 CORE_ADDR region_offset;
434
435 if (ldr_inq_region (fake_ldr_process, lm->module_info.lmi_modid,
436 i, &region_info, sizeof (region_info),
437 &retsize) != 0)
438 break;
439 region_offset = (CORE_ADDR) region_info.lri_mapaddr
c5aa993b 440 - (CORE_ADDR) region_info.lri_vaddr;
c906108c
SS
441 if (i == 0)
442 LM_OFFSET (so_list_ptr) = region_offset;
443 else if (LM_OFFSET (so_list_ptr) != region_offset)
444 warning ("cannot handle shared library relocation for %s (%s)",
445 so_list_ptr->so_name, region_info.lri_name);
446 }
447#else
448 int errcode;
449 char *buffer;
450 target_read_string ((CORE_ADDR) LM_NAME (so_list_ptr), &buffer,
451 MAX_PATH_SIZE - 1, &errcode);
452 if (errcode != 0)
453 error ("xfer_link_map_member: Can't read pathname for load map: %s\n",
454 safe_strerror (errcode));
455 strncpy (so_list_ptr->so_name, buffer, MAX_PATH_SIZE - 1);
456 free (buffer);
457 so_list_ptr->so_name[MAX_PATH_SIZE - 1] = '\0';
458
459 for (i = 0; i < lm->module_info.region_count; i++)
460 {
461 ldr_region_info_t region_info;
462 CORE_ADDR region_offset;
463
464 if (target_read_memory (lm->module_info.regioninfo_addr
c5aa993b 465 + i * sizeof (region_info),
c906108c
SS
466 (char *) &region_info,
467 sizeof (region_info)) != 0)
468 break;
469 region_offset = region_info.mapaddr - region_info.vaddr;
470 if (i == 0)
471 LM_OFFSET (so_list_ptr) = region_offset;
472 else if (LM_OFFSET (so_list_ptr) != region_offset)
473 {
474 char *region_name;
475 target_read_string (region_info.regionname_addr, &buffer,
476 MAX_PATH_SIZE - 1, &errcode);
477 if (errcode == 0)
478 region_name = buffer;
479 else
480 region_name = "??";
481 warning ("cannot handle shared library relocation for %s (%s)",
c5aa993b 482 so_list_ptr->so_name, region_name);
c906108c
SS
483 free (buffer);
484 }
485 }
486#endif
487
488 catch_errors (solib_map_sections, (char *) so_list_ptr,
489 "Error while mapping shared library sections:\n",
490 RETURN_MASK_ALL);
491 }
492}
493
494/*
495
c5aa993b 496 LOCAL FUNCTION
c906108c 497
c5aa993b 498 find_solib -- step through list of shared objects
c906108c 499
c5aa993b 500 SYNOPSIS
c906108c 501
c5aa993b 502 struct so_list *find_solib (struct so_list *so_list_ptr)
c906108c 503
c5aa993b 504 DESCRIPTION
c906108c 505
c5aa993b
JM
506 This module contains the routine which finds the names of any
507 loaded "images" in the current process. The argument in must be
508 NULL on the first call, and then the returned value must be passed
509 in on subsequent calls. This provides the capability to "step" down
510 the list of loaded objects. On the last object, a NULL value is
511 returned.
c906108c 512
c5aa993b
JM
513 The arg and return value are "struct link_map" pointers, as defined
514 in <link.h>.
c906108c
SS
515 */
516
517static struct so_list *
518find_solib (so_list_ptr)
519 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
520{
521 struct so_list *so_list_next = NULL;
522 struct link_map *lm = NULL;
523 struct so_list *new;
c5aa993b 524
c906108c
SS
525 if (so_list_ptr == NULL)
526 {
527 /* We are setting up for a new scan through the loaded images. */
528 if ((so_list_next = so_list_head) == NULL)
529 {
530 /* Find the first link map list member. */
531 lm = first_link_map_member ();
532 }
533 }
534 else
535 {
536 /* We have been called before, and are in the process of walking
c5aa993b 537 the shared library list. Advance to the next shared object. */
c906108c 538 lm = next_link_map_member (so_list_ptr);
c5aa993b 539 so_list_next = so_list_ptr->next;
c906108c
SS
540 }
541 if ((so_list_next == NULL) && (lm != NULL))
542 {
543 /* Get next link map structure from inferior image and build a local
c5aa993b 544 abbreviated load_map structure */
c906108c
SS
545 new = (struct so_list *) xmalloc (sizeof (struct so_list));
546 memset ((char *) new, 0, sizeof (struct so_list));
c5aa993b 547 new->lmaddr = lm;
c906108c 548 /* Add the new node as the next node in the list, or as the root
c5aa993b 549 node if this is the first one. */
c906108c
SS
550 if (so_list_ptr != NULL)
551 {
c5aa993b 552 so_list_ptr->next = new;
c906108c
SS
553 }
554 else
555 {
556 so_list_head = new;
c5aa993b 557 }
c906108c
SS
558 so_list_next = new;
559 xfer_link_map_member (new, lm);
560 }
561 return (so_list_next);
562}
563
564/* A small stub to get us past the arg-passing pinhole of catch_errors. */
565
566static int
567symbol_add_stub (arg)
568 char *arg;
569{
c5aa993b 570 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
c906108c 571 CORE_ADDR text_addr = 0;
2acceee2 572 struct section_addr_info section_addrs;
c906108c 573
2acceee2 574 memset (&section_addrs, 0, sizeof (section_addrs));
c5aa993b
JM
575 if (so->textsection)
576 text_addr = so->textsection->addr;
577 else if (so->abfd != NULL)
c906108c
SS
578 {
579 asection *lowest_sect;
580
581 /* If we didn't find a mapped non zero sized .text section, set up
c5aa993b 582 text_addr so that the relocation in symbol_file_add does no harm. */
c906108c 583
c5aa993b 584 lowest_sect = bfd_get_section_by_name (so->abfd, ".text");
c906108c 585 if (lowest_sect == NULL)
c5aa993b 586 bfd_map_over_sections (so->abfd, find_lowest_section,
96baa820 587 (PTR) &lowest_sect);
c906108c 588 if (lowest_sect)
c5aa993b 589 text_addr = bfd_section_vma (so->abfd, lowest_sect) + LM_OFFSET (so);
c906108c 590 }
c5aa993b 591
a034fba4
EZ
592 section_addrs.other[0].addr = text_addr;
593 section_addrs.other[0].name = ".text";
c5aa993b 594 so->objfile = symbol_file_add (so->so_name, so->from_tty,
2df3850c 595 &section_addrs, 0, OBJF_SHARED);
c906108c
SS
596 return (1);
597}
598
599/*
600
c5aa993b 601 GLOBAL FUNCTION
c906108c 602
c5aa993b 603 solib_add -- add a shared library file to the symtab and section list
c906108c 604
c5aa993b 605 SYNOPSIS
c906108c 606
c5aa993b
JM
607 void solib_add (char *arg_string, int from_tty,
608 struct target_ops *target)
c906108c 609
c5aa993b 610 DESCRIPTION
c906108c 611
c5aa993b 612 */
c906108c
SS
613
614void
615solib_add (arg_string, from_tty, target)
616 char *arg_string;
617 int from_tty;
618 struct target_ops *target;
c5aa993b
JM
619{
620 register struct so_list *so = NULL; /* link map state variable */
c906108c
SS
621
622 /* Last shared library that we read. */
623 struct so_list *so_last = NULL;
624
625 char *re_err;
626 int count;
627 int old;
c5aa993b 628
c906108c
SS
629 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
630 {
631 error ("Invalid regexp: %s", re_err);
632 }
c5aa993b
JM
633
634
c906108c
SS
635 /* Add the shared library sections to the section table of the
636 specified target, if any. */
637 if (target)
638 {
639 /* Count how many new section_table entries there are. */
640 so = NULL;
641 count = 0;
642 while ((so = find_solib (so)) != NULL)
643 {
c5aa993b 644 if (so->so_name[0])
c906108c 645 {
c5aa993b 646 count += so->sections_end - so->sections;
c906108c
SS
647 }
648 }
c5aa993b 649
c906108c
SS
650 if (count)
651 {
c906108c 652 /* Add these section table entries to the target's table. */
6426a772
JM
653
654 old = target_resize_to_sections (target, count);
655
c906108c
SS
656 while ((so = find_solib (so)) != NULL)
657 {
c5aa993b 658 if (so->so_name[0])
c906108c 659 {
c5aa993b
JM
660 count = so->sections_end - so->sections;
661 memcpy ((char *) (target->to_sections + old),
662 so->sections,
c906108c
SS
663 (sizeof (struct section_table)) * count);
664 old += count;
665 }
666 }
667 }
668 }
c5aa993b 669
c906108c
SS
670 /* Now add the symbol files. */
671 so = NULL;
672 while ((so = find_solib (so)) != NULL)
673 {
c5aa993b 674 if (so->so_name[0] && re_exec (so->so_name))
c906108c 675 {
c5aa993b
JM
676 so->from_tty = from_tty;
677 if (so->symbols_loaded)
c906108c
SS
678 {
679 if (from_tty)
680 {
c5aa993b 681 printf_unfiltered ("Symbols already loaded for %s\n", so->so_name);
c906108c
SS
682 }
683 }
684 else if (catch_errors
685 (symbol_add_stub, (char *) so,
686 "Error while reading shared library symbols:\n",
687 RETURN_MASK_ALL))
688 {
689 so_last = so;
c5aa993b 690 so->symbols_loaded = 1;
c906108c
SS
691 }
692 }
693 }
694
695 /* Getting new symbols may change our opinion about what is
696 frameless. */
697 if (so_last)
698 reinit_frame_cache ();
699}
700
701/*
702
c5aa993b 703 LOCAL FUNCTION
c906108c 704
c5aa993b 705 info_sharedlibrary_command -- code for "info sharedlibrary"
c906108c 706
c5aa993b 707 SYNOPSIS
c906108c 708
c5aa993b 709 static void info_sharedlibrary_command ()
c906108c 710
c5aa993b 711 DESCRIPTION
c906108c 712
c5aa993b
JM
713 Walk through the shared library list and print information
714 about each attached library.
715 */
c906108c
SS
716
717static void
718info_sharedlibrary_command (ignore, from_tty)
719 char *ignore;
720 int from_tty;
721{
c5aa993b 722 register struct so_list *so = NULL; /* link map state variable */
c906108c 723 int header_done = 0;
c5aa993b 724
c906108c
SS
725 if (exec_bfd == NULL)
726 {
4ce44c66 727 printf_unfiltered ("No executable file.\n");
c906108c
SS
728 return;
729 }
730 while ((so = find_solib (so)) != NULL)
731 {
c5aa993b 732 if (so->so_name[0])
c906108c
SS
733 {
734 unsigned long txt_start = 0;
735 unsigned long txt_end = 0;
736
737 if (!header_done)
738 {
c5aa993b
JM
739 printf_unfiltered ("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
740 "Shared Object Library");
c906108c
SS
741 header_done++;
742 }
c5aa993b 743 if (so->textsection)
c906108c 744 {
c5aa993b
JM
745 txt_start = (unsigned long) so->textsection->addr;
746 txt_end = (unsigned long) so->textsection->endaddr;
c906108c
SS
747 }
748 printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
749 printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
c5aa993b
JM
750 printf_unfiltered ("%-12s", so->symbols_loaded ? "Yes" : "No");
751 printf_unfiltered ("%s\n", so->so_name);
c906108c
SS
752 }
753 }
754 if (so_list_head == NULL)
755 {
c5aa993b 756 printf_unfiltered ("No shared libraries loaded at this time.\n");
c906108c
SS
757 }
758}
759
760/*
761
c5aa993b 762 GLOBAL FUNCTION
c906108c 763
c5aa993b 764 solib_address -- check to see if an address is in a shared lib
c906108c 765
c5aa993b 766 SYNOPSIS
c906108c 767
c5aa993b 768 char *solib_address (CORE_ADDR address)
c906108c 769
c5aa993b 770 DESCRIPTION
c906108c 771
c5aa993b
JM
772 Provides a hook for other gdb routines to discover whether or
773 not a particular address is within the mapped address space of
774 a shared library. Any address between the base mapping address
775 and the first address beyond the end of the last mapping, is
776 considered to be within the shared library address space, for
777 our purposes.
c906108c 778
c5aa993b
JM
779 For example, this routine is called at one point to disable
780 breakpoints which are in shared libraries that are not currently
781 mapped in.
c906108c
SS
782 */
783
784char *
785solib_address (address)
786 CORE_ADDR address;
787{
c5aa993b
JM
788 register struct so_list *so = 0; /* link map state variable */
789
c906108c
SS
790 while ((so = find_solib (so)) != NULL)
791 {
c5aa993b 792 if (so->so_name[0] && so->textsection)
c906108c 793 {
c5aa993b
JM
794 if ((address >= (CORE_ADDR) so->textsection->addr) &&
795 (address < (CORE_ADDR) so->textsection->endaddr))
c906108c
SS
796 return (so->so_name);
797 }
798 }
799 return (0);
800}
801
802/* Called by free_all_symtabs */
803
c5aa993b
JM
804void
805clear_solib ()
c906108c
SS
806{
807 struct so_list *next;
808 char *bfd_filename;
c5aa993b 809
c906108c
SS
810 disable_breakpoints_in_shlibs (1);
811
812 while (so_list_head)
813 {
c5aa993b 814 if (so_list_head->sections)
c906108c 815 {
c5aa993b 816 free ((PTR) so_list_head->sections);
c906108c 817 }
c5aa993b 818 if (so_list_head->abfd)
c906108c 819 {
c5aa993b
JM
820 bfd_filename = bfd_get_filename (so_list_head->abfd);
821 if (!bfd_close (so_list_head->abfd))
c906108c
SS
822 warning ("cannot close \"%s\": %s",
823 bfd_filename, bfd_errmsg (bfd_get_error ()));
824 }
825 else
826 /* This happens for the executable on SVR4. */
827 bfd_filename = NULL;
828
c5aa993b 829 next = so_list_head->next;
c906108c 830 if (bfd_filename)
c5aa993b
JM
831 free ((PTR) bfd_filename);
832 free ((PTR) so_list_head);
c906108c
SS
833 so_list_head = next;
834 }
835}
c5aa993b 836
c906108c 837/*
c5aa993b
JM
838
839 GLOBAL FUNCTION
840
841 solib_create_inferior_hook -- shared library startup support
842
843 SYNOPSIS
844
845 void solib_create_inferior_hook()
846
847 DESCRIPTION
848
849 When gdb starts up the inferior, it nurses it along (through the
850 shell) until it is ready to execute it's first instruction. At this
851 point, this function gets called via expansion of the macro
852 SOLIB_CREATE_INFERIOR_HOOK.
853 For a statically bound executable, this first instruction is the
854 one at "_start", or a similar text label. No further processing is
855 needed in that case.
856 For a dynamically bound executable, this first instruction is somewhere
857 in the rld, and the actual user executable is not yet mapped in.
858 We continue the inferior again, rld then maps in the actual user
859 executable and any needed shared libraries and then sends
860 itself a SIGTRAP.
861 At that point we discover the names of all shared libraries and
862 read their symbols in.
863
864 FIXME
865
866 This code does not properly handle hitting breakpoints which the
867 user might have set in the rld itself. Proper handling would have
868 to check if the SIGTRAP happened due to a kill call.
869
870 Also, what if child has exit()ed? Must exit loop somehow.
871 */
c906108c
SS
872
873void
c5aa993b 874solib_create_inferior_hook ()
c906108c
SS
875{
876
877 /* Nothing to do for statically bound executables. */
878
879 if (symfile_objfile == NULL
880 || symfile_objfile->obfd == NULL
881 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
882 return;
883
884 /* Now run the target. It will eventually get a SIGTRAP, at
885 which point all of the libraries will have been mapped in and we
886 can go groveling around in the rld structures to find
887 out what we need to know about them. */
c5aa993b 888
c906108c
SS
889 clear_proceed_status ();
890 stop_soon_quietly = 1;
891 stop_signal = TARGET_SIGNAL_0;
892 do
893 {
894 target_resume (-1, 0, stop_signal);
895 wait_for_inferior ();
896 }
897 while (stop_signal != TARGET_SIGNAL_TRAP);
898
899 /* solib_add will call reinit_frame_cache.
c5aa993b
JM
900 But we are stopped in the runtime loader and we do not have symbols
901 for the runtime loader. So heuristic_proc_start will be called
902 and will put out an annoying warning.
903 Delaying the resetting of stop_soon_quietly until after symbol loading
904 suppresses the warning. */
c906108c
SS
905 if (auto_solib_add)
906 solib_add ((char *) 0, 0, (struct target_ops *) 0);
907 stop_soon_quietly = 0;
908}
909
910
911/*
912
c5aa993b 913 LOCAL FUNCTION
c906108c 914
c5aa993b 915 sharedlibrary_command -- handle command to explicitly add library
c906108c 916
c5aa993b 917 SYNOPSIS
c906108c 918
c5aa993b 919 static void sharedlibrary_command (char *args, int from_tty)
c906108c 920
c5aa993b 921 DESCRIPTION
c906108c 922
c5aa993b 923 */
c906108c
SS
924
925static void
926sharedlibrary_command (args, from_tty)
c5aa993b
JM
927 char *args;
928 int from_tty;
c906108c
SS
929{
930 dont_repeat ();
931 solib_add (args, from_tty, (struct target_ops *) 0);
932}
933
934void
c5aa993b 935_initialize_solib ()
c906108c
SS
936{
937 add_com ("sharedlibrary", class_files, sharedlibrary_command,
938 "Load shared object library symbols for files matching REGEXP.");
c5aa993b 939 add_info ("sharedlibrary", info_sharedlibrary_command,
c906108c
SS
940 "Status of loaded shared object libraries.");
941
942 add_show_from_set
943 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
944 (char *) &auto_solib_add,
945 "Set autoloading of shared library symbols.\n\
946If nonzero, symbols from all shared object libraries will be loaded\n\
947automatically when the inferior begins execution or when the dynamic linker\n\
948informs gdb that a new library has been loaded. Otherwise, symbols\n\
949must be loaded manually, using `sharedlibrary'.",
950 &setlist),
951 &showlist);
952}
This page took 0.103519 seconds and 4 git commands to generate.