* Makefile.in (diststuff): Make all-doc; diststuff target does not
[deliverable/binutils-gdb.git] / gdb / osfsolib.c
1 /* Handle OSF/1 shared libraries for GDB, the GNU Debugger.
2 Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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, Boston, MA 02111-1307, USA. */
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>
27 #include "gdb_string.h"
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"
38 #include "gnu-regex.h"
39 #include "inferior.h"
40 #include "language.h"
41 #include "gdbcmd.h"
42
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. */
64
65 #ifndef USE_LDR_ROUTINES
66
67 /* Definition of runtime loader structures, found by experimentation. */
68 #define RLD_CONTEXT_ADDRESS 0x3ffc0000000
69
70 typedef struct
71 {
72 CORE_ADDR next;
73 CORE_ADDR previous;
74 CORE_ADDR unknown1;
75 char *module_name;
76 CORE_ADDR modinfo_addr;
77 long module_id;
78 CORE_ADDR unknown2;
79 CORE_ADDR unknown3;
80 long region_count;
81 CORE_ADDR regioninfo_addr;
82 } ldr_module_info_t;
83
84 typedef 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
95 typedef struct
96 {
97 CORE_ADDR unknown1;
98 CORE_ADDR unknown2;
99 CORE_ADDR head;
100 CORE_ADDR tail;
101 } ldr_context_t;
102
103 static ldr_context_t ldr_context;
104
105 #else
106
107 #include <loader.h>
108 static ldr_process_t fake_ldr_process;
109
110 /* Called by ldr_* routines to read memory from the current target. */
111
112 static int ldr_read_memory PARAMS ((CORE_ADDR, char *, int, int));
113
114 static int
115 ldr_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
139 #endif
140
141 /* Define our own link_map structure.
142 This will help to share code with solib.c. */
143
144 struct link_map {
145 CORE_ADDR l_offset; /* prelink to load address offset */
146 char *l_name; /* full name of loaded object */
147 ldr_module_info_t module_info; /* corresponding module info */
148 };
149
150 #define LM_OFFSET(so) ((so) -> lm.l_offset)
151 #define LM_NAME(so) ((so) -> lm.l_name)
152
153 struct 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;
165 bfd *abfd;
166 };
167
168 static struct so_list *so_list_head; /* List of known shared objects */
169
170 extern int
171 fdmatch PARAMS ((int, int)); /* In libiberty */
172
173 /* Local function prototypes */
174
175 static void
176 sharedlibrary_command PARAMS ((char *, int));
177
178 static void
179 info_sharedlibrary_command PARAMS ((char *, int));
180
181 static int
182 symbol_add_stub PARAMS ((char *));
183
184 static struct so_list *
185 find_solib PARAMS ((struct so_list *));
186
187 static struct link_map *
188 first_link_map_member PARAMS ((void));
189
190 static struct link_map *
191 next_link_map_member PARAMS ((struct so_list *));
192
193 static void
194 xfer_link_map_member PARAMS ((struct so_list *, struct link_map *));
195
196 static void
197 solib_map_sections PARAMS ((struct so_list *));
198
199 /*
200
201 LOCAL FUNCTION
202
203 solib_map_sections -- open bfd and build sections for shared lib
204
205 SYNOPSIS
206
207 static void solib_map_sections (struct so_list *so)
208
209 DESCRIPTION
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
217 FIXMES
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
226 static void
227 solib_map_sections (so)
228 struct so_list *so;
229 {
230 char *filename;
231 char *scratch_pathname;
232 int scratch_chan;
233 struct section_table *p;
234 struct cleanup *old_chain;
235 bfd *abfd;
236
237 filename = tilde_expand (so -> so_name);
238 old_chain = make_cleanup (free, filename);
239
240 scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
241 &scratch_pathname);
242 if (scratch_chan < 0)
243 {
244 scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename,
245 O_RDONLY, 0, &scratch_pathname);
246 }
247 if (scratch_chan < 0)
248 {
249 perror_with_name (filename);
250 }
251 /* Leave scratch_pathname allocated. bfd->name will point to it. */
252
253 abfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
254 if (!abfd)
255 {
256 close (scratch_chan);
257 error ("Could not open `%s' as an executable file: %s",
258 scratch_pathname, bfd_errmsg (bfd_get_error ()));
259 }
260 /* Leave bfd open, core_xfer_memory and "info files" need it. */
261 so -> abfd = abfd;
262 abfd -> cacheable = true;
263
264 if (!bfd_check_format (abfd, bfd_object))
265 {
266 error ("\"%s\": not in executable format: %s.",
267 scratch_pathname, bfd_errmsg (bfd_get_error ()));
268 }
269 if (build_section_table (abfd, &so -> sections, &so -> sections_end))
270 {
271 error ("Can't find the file sections in `%s': %s",
272 bfd_get_filename (exec_bfd), bfd_errmsg (bfd_get_error ()));
273 }
274
275 for (p = so -> sections; p < so -> sections_end; p++)
276 {
277 /* Relocate the section binding addresses as recorded in the shared
278 object's file by the offset to get the address to which the
279 object was actually mapped. */
280 p -> addr += LM_OFFSET (so);
281 p -> endaddr += LM_OFFSET (so);
282 so -> lmend = (CORE_ADDR) max (p -> endaddr, so -> lmend);
283 if (STREQ (p -> the_bfd_section -> name, ".text"))
284 {
285 so -> textsection = p;
286 }
287 }
288
289 /* Free the file names, close the file now. */
290 do_cleanups (old_chain);
291 }
292
293 /*
294
295 LOCAL FUNCTION
296
297 first_link_map_member -- locate first member in dynamic linker's map
298
299 SYNOPSIS
300
301 static struct link_map *first_link_map_member (void)
302
303 DESCRIPTION
304
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 */
309
310 static struct link_map *
311 first_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
324 || ldr_next_module(fake_ldr_process, &mod_id) != 0
325 || mod_id == LDR_NULL_MODULE
326 || ldr_inq_module(fake_ldr_process, mod_id,
327 &first_lm.module_info, sizeof(ldr_module_info_t),
328 &retsize) != 0)
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
353 static struct link_map *
354 next_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
363 if (ldr_next_module(fake_ldr_process, &mod_id) != 0
364 || mod_id == LDR_NULL_MODULE
365 || ldr_inq_module(fake_ldr_process, mod_id,
366 &next_lm.module_info, sizeof(ldr_module_info_t),
367 &retsize) != 0)
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
395 static void
396 xfer_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
440 - (CORE_ADDR) region_info.lri_vaddr;
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
465 + i * sizeof (region_info),
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)",
482 so_list_ptr->so_name, region_name);
483 free (buffer);
484 }
485 }
486 #endif
487
488 solib_map_sections (so_list_ptr);
489 }
490 }
491
492 /*
493
494 LOCAL FUNCTION
495
496 find_solib -- step through list of shared objects
497
498 SYNOPSIS
499
500 struct so_list *find_solib (struct so_list *so_list_ptr)
501
502 DESCRIPTION
503
504 This module contains the routine which finds the names of any
505 loaded "images" in the current process. The argument in must be
506 NULL on the first call, and then the returned value must be passed
507 in on subsequent calls. This provides the capability to "step" down
508 the list of loaded objects. On the last object, a NULL value is
509 returned.
510
511 The arg and return value are "struct link_map" pointers, as defined
512 in <link.h>.
513 */
514
515 static struct so_list *
516 find_solib (so_list_ptr)
517 struct so_list *so_list_ptr; /* Last lm or NULL for first one */
518 {
519 struct so_list *so_list_next = NULL;
520 struct link_map *lm = NULL;
521 struct so_list *new;
522
523 if (so_list_ptr == NULL)
524 {
525 /* We are setting up for a new scan through the loaded images. */
526 if ((so_list_next = so_list_head) == NULL)
527 {
528 /* Find the first link map list member. */
529 lm = first_link_map_member ();
530 }
531 }
532 else
533 {
534 /* We have been called before, and are in the process of walking
535 the shared library list. Advance to the next shared object. */
536 lm = next_link_map_member (so_list_ptr);
537 so_list_next = so_list_ptr -> next;
538 }
539 if ((so_list_next == NULL) && (lm != NULL))
540 {
541 /* Get next link map structure from inferior image and build a local
542 abbreviated load_map structure */
543 new = (struct so_list *) xmalloc (sizeof (struct so_list));
544 memset ((char *) new, 0, sizeof (struct so_list));
545 new -> lmaddr = lm;
546 /* Add the new node as the next node in the list, or as the root
547 node if this is the first one. */
548 if (so_list_ptr != NULL)
549 {
550 so_list_ptr -> next = new;
551 }
552 else
553 {
554 so_list_head = new;
555 }
556 so_list_next = new;
557 xfer_link_map_member (new, lm);
558 }
559 return (so_list_next);
560 }
561
562 /* A small stub to get us past the arg-passing pinhole of catch_errors. */
563
564 static int
565 symbol_add_stub (arg)
566 char *arg;
567 {
568 register struct so_list *so = (struct so_list *) arg; /* catch_errs bogon */
569
570 so -> objfile = symbol_file_add (so -> so_name, so -> from_tty,
571 so -> textsection -> addr,
572 0, 0, 0);
573 return (1);
574 }
575
576 /*
577
578 GLOBAL FUNCTION
579
580 solib_add -- add a shared library file to the symtab and section list
581
582 SYNOPSIS
583
584 void solib_add (char *arg_string, int from_tty,
585 struct target_ops *target)
586
587 DESCRIPTION
588
589 */
590
591 void
592 solib_add (arg_string, from_tty, target)
593 char *arg_string;
594 int from_tty;
595 struct target_ops *target;
596 {
597 register struct so_list *so = NULL; /* link map state variable */
598
599 /* Last shared library that we read. */
600 struct so_list *so_last = NULL;
601
602 char *re_err;
603 int count;
604 int old;
605
606 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
607 {
608 error ("Invalid regexp: %s", re_err);
609 }
610
611
612 /* Add the shared library sections to the section table of the
613 specified target, if any. */
614 if (target)
615 {
616 /* Count how many new section_table entries there are. */
617 so = NULL;
618 count = 0;
619 while ((so = find_solib (so)) != NULL)
620 {
621 if (so -> so_name[0])
622 {
623 count += so -> sections_end - so -> sections;
624 }
625 }
626
627 if (count)
628 {
629 int update_coreops;
630
631 /* We must update the to_sections field in the core_ops structure
632 here, otherwise we dereference a potential dangling pointer
633 for each call to target_read/write_memory within this routine. */
634 update_coreops = core_ops.to_sections == target->to_sections;
635
636 /* Reallocate the target's section table including the new size. */
637 if (target -> to_sections)
638 {
639 old = target -> to_sections_end - target -> to_sections;
640 target -> to_sections = (struct section_table *)
641 xrealloc ((char *)target -> to_sections,
642 (sizeof (struct section_table)) * (count + old));
643 }
644 else
645 {
646 old = 0;
647 target -> to_sections = (struct section_table *)
648 xmalloc ((sizeof (struct section_table)) * count);
649 }
650 target -> to_sections_end = target -> to_sections + (count + old);
651
652 /* Update the to_sections field in the core_ops structure
653 if needed. */
654 if (update_coreops)
655 {
656 core_ops.to_sections = target->to_sections;
657 core_ops.to_sections_end = target->to_sections_end;
658 }
659
660 /* Add these section table entries to the target's table. */
661 while ((so = find_solib (so)) != NULL)
662 {
663 if (so -> so_name[0])
664 {
665 count = so -> sections_end - so -> sections;
666 memcpy ((char *) (target -> to_sections + old),
667 so -> sections,
668 (sizeof (struct section_table)) * count);
669 old += count;
670 }
671 }
672 }
673 }
674
675 /* Now add the symbol files. */
676 so = NULL;
677 while ((so = find_solib (so)) != NULL)
678 {
679 if (so -> so_name[0] && re_exec (so -> so_name))
680 {
681 so -> from_tty = from_tty;
682 if (so -> symbols_loaded)
683 {
684 if (from_tty)
685 {
686 printf_unfiltered ("Symbols already loaded for %s\n", so -> so_name);
687 }
688 }
689 else if (catch_errors
690 (symbol_add_stub, (char *) so,
691 "Error while reading shared library symbols:\n",
692 RETURN_MASK_ALL))
693 {
694 so_last = so;
695 so -> symbols_loaded = 1;
696 }
697 }
698 }
699
700 /* Getting new symbols may change our opinion about what is
701 frameless. */
702 if (so_last)
703 reinit_frame_cache ();
704 }
705
706 /*
707
708 LOCAL FUNCTION
709
710 info_sharedlibrary_command -- code for "info sharedlibrary"
711
712 SYNOPSIS
713
714 static void info_sharedlibrary_command ()
715
716 DESCRIPTION
717
718 Walk through the shared library list and print information
719 about each attached library.
720 */
721
722 static void
723 info_sharedlibrary_command (ignore, from_tty)
724 char *ignore;
725 int from_tty;
726 {
727 register struct so_list *so = NULL; /* link map state variable */
728 int header_done = 0;
729
730 if (exec_bfd == NULL)
731 {
732 printf_unfiltered ("No exec file.\n");
733 return;
734 }
735 while ((so = find_solib (so)) != NULL)
736 {
737 if (so -> so_name[0])
738 {
739 unsigned long txt_start = 0;
740 unsigned long txt_end = 0;
741
742 if (!header_done)
743 {
744 printf_unfiltered("%-20s%-20s%-12s%s\n", "From", "To", "Syms Read",
745 "Shared Object Library");
746 header_done++;
747 }
748 if (so -> textsection)
749 {
750 txt_start = (unsigned long) so -> textsection -> addr;
751 txt_end = (unsigned long) so -> textsection -> endaddr;
752 }
753 printf_unfiltered ("%-20s", local_hex_string_custom (txt_start, "08l"));
754 printf_unfiltered ("%-20s", local_hex_string_custom (txt_end, "08l"));
755 printf_unfiltered ("%-12s", so -> symbols_loaded ? "Yes" : "No");
756 printf_unfiltered ("%s\n", so -> so_name);
757 }
758 }
759 if (so_list_head == NULL)
760 {
761 printf_unfiltered ("No shared libraries loaded at this time.\n");
762 }
763 }
764
765 /*
766
767 GLOBAL FUNCTION
768
769 solib_address -- check to see if an address is in a shared lib
770
771 SYNOPSIS
772
773 char *solib_address (CORE_ADDR address)
774
775 DESCRIPTION
776
777 Provides a hook for other gdb routines to discover whether or
778 not a particular address is within the mapped address space of
779 a shared library. Any address between the base mapping address
780 and the first address beyond the end of the last mapping, is
781 considered to be within the shared library address space, for
782 our purposes.
783
784 For example, this routine is called at one point to disable
785 breakpoints which are in shared libraries that are not currently
786 mapped in.
787 */
788
789 char *
790 solib_address (address)
791 CORE_ADDR address;
792 {
793 register struct so_list *so = 0; /* link map state variable */
794
795 while ((so = find_solib (so)) != NULL)
796 {
797 if (so -> so_name[0] && so -> textsection)
798 {
799 if ((address >= (CORE_ADDR) so -> textsection -> addr) &&
800 (address < (CORE_ADDR) so -> textsection -> endaddr))
801 return (so->so_name);
802 }
803 }
804 return (0);
805 }
806
807 /* Called by free_all_symtabs */
808
809 void
810 clear_solib()
811 {
812 struct so_list *next;
813 char *bfd_filename;
814
815 while (so_list_head)
816 {
817 if (so_list_head -> sections)
818 {
819 free ((PTR)so_list_head -> sections);
820 }
821 if (so_list_head -> abfd)
822 {
823 bfd_filename = bfd_get_filename (so_list_head -> abfd);
824 if (!bfd_close (so_list_head -> abfd))
825 warning ("cannot close \"%s\": %s",
826 bfd_filename, bfd_errmsg (bfd_get_error ()));
827 }
828 else
829 /* This happens for the executable on SVR4. */
830 bfd_filename = NULL;
831
832 next = so_list_head -> next;
833 if (bfd_filename)
834 free ((PTR)bfd_filename);
835 free ((PTR)so_list_head);
836 so_list_head = next;
837 }
838 }
839
840 /*
841
842 GLOBAL FUNCTION
843
844 solib_create_inferior_hook -- shared library startup support
845
846 SYNOPSIS
847
848 void solib_create_inferior_hook()
849
850 DESCRIPTION
851
852 When gdb starts up the inferior, it nurses it along (through the
853 shell) until it is ready to execute it's first instruction. At this
854 point, this function gets called via expansion of the macro
855 SOLIB_CREATE_INFERIOR_HOOK.
856 For a statically bound executable, this first instruction is the
857 one at "_start", or a similar text label. No further processing is
858 needed in that case.
859 For a dynamically bound executable, this first instruction is somewhere
860 in the rld, and the actual user executable is not yet mapped in.
861 We continue the inferior again, rld then maps in the actual user
862 executable and any needed shared libraries and then sends
863 itself a SIGTRAP.
864 At that point we discover the names of all shared libraries and
865 read their symbols in.
866
867 FIXME
868
869 This code does not properly handle hitting breakpoints which the
870 user might have set in the rld itself. Proper handling would have
871 to check if the SIGTRAP happened due to a kill call.
872
873 Also, what if child has exit()ed? Must exit loop somehow.
874 */
875
876 void
877 solib_create_inferior_hook()
878 {
879
880 /* Nothing to do for statically bound executables. */
881
882 if (symfile_objfile == NULL
883 || symfile_objfile->obfd == NULL
884 || ((bfd_get_file_flags (symfile_objfile->obfd) & DYNAMIC) == 0))
885 return;
886
887 /* Now run the target. It will eventually get a SIGTRAP, at
888 which point all of the libraries will have been mapped in and we
889 can go groveling around in the rld structures to find
890 out what we need to know about them. */
891
892 clear_proceed_status ();
893 stop_soon_quietly = 1;
894 stop_signal = TARGET_SIGNAL_0;
895 do
896 {
897 target_resume (-1, 0, stop_signal);
898 wait_for_inferior ();
899 }
900 while (stop_signal != TARGET_SIGNAL_TRAP);
901
902 /* solib_add will call reinit_frame_cache.
903 But we are stopped in the runtime loader and we do not have symbols
904 for the runtime loader. So heuristic_proc_start will be called
905 and will put out an annoying warning.
906 Delaying the resetting of stop_soon_quietly until after symbol loading
907 suppresses the warning. */
908 if (auto_solib_add)
909 solib_add ((char *) 0, 0, (struct target_ops *) 0);
910 stop_soon_quietly = 0;
911 }
912
913
914 /*
915
916 LOCAL FUNCTION
917
918 sharedlibrary_command -- handle command to explicitly add library
919
920 SYNOPSIS
921
922 static void sharedlibrary_command (char *args, int from_tty)
923
924 DESCRIPTION
925
926 */
927
928 static void
929 sharedlibrary_command (args, from_tty)
930 char *args;
931 int from_tty;
932 {
933 dont_repeat ();
934 solib_add (args, from_tty, (struct target_ops *) 0);
935 }
936
937 void
938 _initialize_solib()
939 {
940 add_com ("sharedlibrary", class_files, sharedlibrary_command,
941 "Load shared object library symbols for files matching REGEXP.");
942 add_info ("sharedlibrary", info_sharedlibrary_command,
943 "Status of loaded shared object libraries.");
944
945 add_show_from_set
946 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
947 (char *) &auto_solib_add,
948 "Set autoloading of shared library symbols.\n\
949 If nonzero, symbols from all shared object libraries will be loaded\n\
950 automatically when the inferior begins execution or when the dynamic linker\n\
951 informs gdb that a new library has been loaded. Otherwise, symbols\n\
952 must be loaded manually, using `sharedlibrary'.",
953 &setlist),
954 &showlist);
955 }
This page took 0.10659 seconds and 4 git commands to generate.