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