2000-05-04 Elena Zannoni <ezannoni@kwikemart.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / pa64solib.c
1 /* Handle HP ELF shared libraries for GDB, the GNU Debugger.
2 Copyright 1999 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,
19 Boston, MA 02111-1307, USA.
20
21 HP in their infinite stupidity choose not to use standard ELF dynamic
22 linker interfaces. They also choose not to make their ELF dymamic
23 linker interfaces compatible with the SOM dynamic linker. The
24 net result is we can not use either of the existing somsolib.c or
25 solib.c. What a crock.
26
27 Even more disgusting. This file depends on functions provided only
28 in certain PA64 libraries. Thus this file is supposed to only be
29 used native. When will HP ever learn that they need to provide the
30 same functionality in all their libraries! */
31
32 #include <dlfcn.h>
33 #include <elf.h>
34 #include <elf_hp.h>
35
36 #include "defs.h"
37
38 #include "frame.h"
39 #include "bfd.h"
40 #include "libhppa.h"
41 #include "gdbcore.h"
42 #include "symtab.h"
43 #include "breakpoint.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "inferior.h"
47 #include "gdb-stabs.h"
48 #include "gdb_stat.h"
49 #include "gdbcmd.h"
50 #include "assert.h"
51 #include "language.h"
52
53 #include <fcntl.h>
54
55 #ifndef O_BINARY
56 #define O_BINARY 0
57 #endif
58
59 /* Defined in exec.c; used to prevent dangling pointer bug. */
60 extern struct target_ops exec_ops;
61
62 static CORE_ADDR
63 bfd_lookup_symbol PARAMS ((bfd *, char *));
64 /* This lives in hppa-tdep.c. */
65 extern struct unwind_table_entry *find_unwind_entry PARAMS ((CORE_ADDR pc));
66
67 /* These ought to be defined in some public interface, but aren't. They
68 identify dynamic linker events. */
69 #define DLD_CB_LOAD 1
70 #define DLD_CB_UNLOAD 0
71
72 /* A structure to keep track of all the known shared objects. */
73 struct so_list
74 {
75 bfd *abfd;
76 char *name;
77 struct so_list *next;
78 struct objfile *objfile;
79 CORE_ADDR pa64_solib_desc_addr;
80 struct load_module_desc pa64_solib_desc;
81 struct section_table *sections;
82 struct section_table *sections_end;
83 boolean loaded;
84 };
85
86 static struct so_list *so_list_head;
87
88 /* This is the cumulative size in bytes of the symbol tables of all
89 shared objects on the so_list_head list. (When we say size, here
90 we mean of the information before it is brought into memory and
91 potentially expanded by GDB.) When adding a new shlib, this value
92 is compared against the threshold size, held by auto_solib_add
93 (in megabytes). If adding symbols for the new shlib would cause
94 the total size to exceed the threshold, then the new shlib's symbols
95 are not loaded. */
96 static LONGEST pa64_solib_total_st_size;
97
98 /* When the threshold is reached for any shlib, we refuse to add
99 symbols for subsequent shlibs, even if those shlibs' symbols would
100 be small enough to fit under the threshold. (Although this may
101 result in one, early large shlib preventing the loading of later,
102 smalller shlibs' symbols, it allows us to issue one informational
103 message. The alternative, to issue a message for each shlib whose
104 symbols aren't loaded, could be a big annoyance where the threshold
105 is exceeded due to a very large number of shlibs.) */
106 static int pa64_solib_st_size_threshold_exceeded;
107
108 /* When adding fields, be sure to clear them in _initialize_pa64_solib. */
109 typedef struct
110 {
111 CORE_ADDR dld_flags_addr;
112 long long dld_flags;
113 sec_ptr dyninfo_sect;
114 boolean have_read_dld_descriptor;
115 boolean is_valid;
116 CORE_ADDR load_map;
117 CORE_ADDR load_map_addr;
118 struct load_module_desc dld_desc;
119 }
120 dld_cache_t;
121
122 static dld_cache_t dld_cache;
123
124 static void pa64_sharedlibrary_info_command PARAMS ((char *, int));
125
126 static void pa64_solib_sharedlibrary_command PARAMS ((char *, int));
127
128 static void * pa64_target_read_memory PARAMS ((void *, CORE_ADDR, size_t, int));
129
130 static boolean read_dld_descriptor PARAMS ((struct target_ops *));
131
132 static boolean read_dynamic_info PARAMS ((asection *, dld_cache_t *));
133
134 static void add_to_solist PARAMS ((boolean, char *, struct load_module_desc *,
135 CORE_ADDR, struct target_ops *));
136
137 /* When examining the shared library for debugging information we have to
138 look for HP debug symbols, stabs and dwarf2 debug symbols. */
139 static char *pa64_debug_section_names[] = {
140 ".debug_header", ".debug_gntt", ".debug_lntt", ".debug_slt", ".debug_vt",
141 ".stabs", ".stabstr", ".debug_info", ".debug_abbrev", ".debug_aranges",
142 ".debug_macinfo", ".debug_line", ".debug_loc", ".debug_pubnames",
143 ".debug_str", NULL
144 };
145
146 /* Return a ballbark figure for the amount of memory GDB will need to
147 allocate to read in the debug symbols from FILENAME. */
148 static LONGEST
149 pa64_solib_sizeof_symbol_table (filename)
150 char *filename;
151 {
152 bfd *abfd;
153 int i;
154 int desc;
155 char *absolute_name;
156 LONGEST st_size = (LONGEST) 0;
157 asection *sect;
158
159 /* We believe that filename was handed to us by the dynamic linker, and
160 is therefore always an absolute path. */
161 desc = openp (getenv ("PATH"), 1, filename, O_RDONLY | O_BINARY,
162 0, &absolute_name);
163 if (desc < 0)
164 {
165 perror_with_name (filename);
166 }
167 filename = absolute_name;
168
169 abfd = bfd_fdopenr (filename, gnutarget, desc);
170 if (!abfd)
171 {
172 close (desc);
173 make_cleanup (free, filename);
174 error ("\"%s\": can't open to read symbols: %s.", filename,
175 bfd_errmsg (bfd_get_error ()));
176 }
177
178 if (!bfd_check_format (abfd, bfd_object))
179 {
180 bfd_close (abfd);
181 make_cleanup (free, filename);
182 error ("\"%s\": can't read symbols: %s.", filename,
183 bfd_errmsg (bfd_get_error ()));
184 }
185
186 /* Sum the sizes of the various sections that compose debug info. */
187 for (i = 0; pa64_debug_section_names[i] != NULL; i++)
188 {
189 asection *sect;
190
191 sect = bfd_get_section_by_name (abfd, pa64_debug_section_names[i]);
192 if (sect)
193 st_size += (LONGEST)bfd_section_size (abfd, sect);
194 }
195
196 bfd_close (abfd);
197 free (filename);
198
199 /* Unfortunately, just summing the sizes of various debug info
200 sections isn't a very accurate measurement of how much heap
201 space the debugger will need to hold them. It also doesn't
202 account for space needed by linker (aka "minimal") symbols.
203
204 Anecdotal evidence suggests that just summing the sizes of
205 debug-info-related sections understates the heap space needed
206 to represent it internally by about an order of magnitude.
207
208 Since it's not exactly brain surgery we're doing here, rather
209 than attempt to more accurately measure the size of a shlib's
210 symbol table in GDB's heap, we'll just apply a 10x fudge-
211 factor to the debug info sections' size-sum. No, this doesn't
212 account for minimal symbols in non-debuggable shlibs. But it
213 all roughly washes out in the end. */
214 return st_size * (LONGEST) 10;
215 }
216
217 /* Add a shared library to the objfile list and load its symbols into
218 GDB's symbol table. */
219 static void
220 pa64_solib_add_solib_objfile (so, name, from_tty, text_addr)
221 struct so_list *so;
222 char *name;
223 int from_tty;
224 CORE_ADDR text_addr;
225 {
226 bfd *tmp_bfd;
227 asection *sec;
228 obj_private_data_t *obj_private;
229 struct section_addr_info section_addrs;
230
231 memset (&section_addrs, 0, sizeof (section_addrs));
232 /* We need the BFD so that we can look at its sections. We open up the
233 file temporarily, then close it when we are done. */
234 tmp_bfd = bfd_openr (name, gnutarget);
235 if (tmp_bfd == NULL)
236 {
237 perror_with_name (name);
238 return;
239 }
240
241 if (!bfd_check_format (tmp_bfd, bfd_object))
242 {
243 bfd_close (tmp_bfd);
244 error ("\"%s\" is not an object file: %s", name,
245 bfd_errmsg (bfd_get_error ()));
246 }
247
248
249 /* Undo some braindamage from symfile.c.
250
251 First, symfile.c will subtract the VMA of the first .text section
252 in the shared library that it finds. Undo that. */
253 sec = bfd_get_section_by_name (tmp_bfd, ".text");
254 text_addr += bfd_section_vma (tmp_bfd, sec);
255
256 /* Now find the true lowest section in the shared library. */
257 sec = NULL;
258 bfd_map_over_sections (tmp_bfd, find_lowest_section, (PTR) &sec);
259
260 if (sec)
261 {
262 /* Subtract out the VMA of the lowest section. */
263 text_addr -= bfd_section_vma (tmp_bfd, sec);
264
265 /* ??? Add back in the filepos of that lowest section. */
266 text_addr += sec->filepos;
267 }
268
269 /* We are done with the temporary bfd. Get rid of it and make sure
270 nobody else can us it. */
271 bfd_close (tmp_bfd);
272 tmp_bfd = NULL;
273
274 /* Now let the generic code load up symbols for this library. */
275 section_addrs.other[0].addr = text_addr;
276 section_addrs.other[0].name = ".text";
277 so->objfile = symbol_file_add (name, from_tty, &section_addrs, 0, OBJF_SHARED);
278 so->abfd = so->objfile->obfd;
279
280 /* Mark this as a shared library and save private data. */
281 so->objfile->flags |= OBJF_SHARED;
282
283 if (so->objfile->obj_private == NULL)
284 {
285 obj_private = (obj_private_data_t *)
286 obstack_alloc (&so->objfile->psymbol_obstack,
287 sizeof (obj_private_data_t));
288 obj_private->unwind_info = NULL;
289 obj_private->so_info = NULL;
290 so->objfile->obj_private = (PTR) obj_private;
291 }
292
293 obj_private = (obj_private_data_t *) so->objfile->obj_private;
294 obj_private->so_info = so;
295 obj_private->dp = so->pa64_solib_desc.linkage_ptr;
296 }
297
298 /* Load debugging information for a shared library. TARGET may be
299 NULL if we are not attaching to a process or reading a core file. */
300
301 static void
302 pa64_solib_load_symbols (so, name, from_tty, text_addr, target)
303 struct so_list *so;
304 char *name;
305 int from_tty;
306 CORE_ADDR text_addr;
307 struct target_ops *target;
308 {
309 struct section_table *p;
310 asection *sec;
311 int status;
312 char buf[4];
313 CORE_ADDR presumed_data_start;
314
315 if (text_addr == 0)
316 text_addr = so->pa64_solib_desc.text_base;
317
318 pa64_solib_add_solib_objfile (so, name, from_tty, text_addr);
319
320 /* Now we need to build a section table for this library since
321 we might be debugging a core file from a dynamically linked
322 executable in which the libraries were not privately mapped. */
323 if (build_section_table (so->abfd,
324 &so->sections,
325 &so->sections_end))
326 {
327 error ("Unable to build section table for shared library\n.");
328 return;
329 }
330
331 ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile))
332 = so->pa64_solib_desc.text_base;
333 ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile))
334 = so->pa64_solib_desc.data_base;
335
336 /* Relocate all the sections based on where they got loaded. */
337 for (p = so->sections; p < so->sections_end; p++)
338 {
339 if (p->the_bfd_section->flags & SEC_CODE)
340 {
341 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
342 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT (so->objfile));
343 }
344 else if (p->the_bfd_section->flags & SEC_DATA)
345 {
346 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
347 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA (so->objfile));
348 }
349 }
350
351 /* Now see if we need to map in the text and data for this shared
352 library (for example debugging a core file which does not use
353 private shared libraries.).
354
355 Carefully peek at the first text address in the library. If the
356 read succeeds, then the libraries were privately mapped and were
357 included in the core dump file.
358
359 If the peek failed, then the libraries were not privately mapped
360 and are not in the core file, we'll have to read them in ourselves. */
361 status = target_read_memory (text_addr, buf, 4);
362 if (status != 0)
363 {
364 int new, old;
365
366 new = so->sections_end - so->sections;
367
368 old = target_resize_to_sections (target, new);
369
370 /* Copy over the old data before it gets clobbered. */
371 memcpy ((char *) (target->to_sections + old),
372 so->sections,
373 ((sizeof (struct section_table)) * new));
374 }
375 }
376
377
378 /* Add symbols from shared libraries into the symtab list, unless the
379 size threshold (specified by auto_solib_add, in megabytes) would
380 be exceeded. */
381
382 void
383 pa64_solib_add (arg_string, from_tty, target)
384 char *arg_string;
385 int from_tty;
386 struct target_ops *target;
387 {
388 struct minimal_symbol *msymbol;
389 CORE_ADDR addr;
390 asection *shlib_info;
391 int status;
392 unsigned int dld_flags;
393 char buf[4], *re_err;
394 int threshold_warning_given = 0;
395 int dll_index;
396 struct load_module_desc dll_desc;
397 char *dll_path;
398
399 /* First validate our arguments. */
400 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
401 {
402 error ("Invalid regexp: %s", re_err);
403 }
404
405 /* If we're debugging a core file, or have attached to a running
406 process, then pa64_solib_create_inferior_hook will not have been
407 called.
408
409 We need to first determine if we're dealing with a dynamically
410 linked executable. If not, then return without an error or warning.
411
412 We also need to examine __dld_flags to determine if the shared library
413 list is valid and to determine if the libraries have been privately
414 mapped. */
415 if (symfile_objfile == NULL)
416 return;
417
418 /* First see if the objfile was dynamically linked. */
419 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
420 if (!shlib_info)
421 return;
422
423 /* It's got a .dynamic section, make sure it's not empty. */
424 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
425 return;
426
427 /* Read in the load map pointer if we have not done so already. */
428 if (! dld_cache.have_read_dld_descriptor)
429 if (! read_dld_descriptor (target))
430 return;
431
432 /* If the libraries were not mapped private, warn the user. */
433 if ((dld_cache.dld_flags & DT_HP_DEBUG_PRIVATE) == 0)
434 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
435
436 /* For each shaerd library, add it to the shared library list. */
437 for (dll_index = 1; ; dll_index++)
438 {
439 /* Read in the load module descriptor. */
440 if (dlgetmodinfo (dll_index, &dll_desc, sizeof (dll_desc),
441 pa64_target_read_memory, 0, dld_cache.load_map)
442 == 0)
443 return;
444
445 /* Get the name of the shared library. */
446 dll_path = (char *)dlgetname (&dll_desc, sizeof (dll_desc),
447 pa64_target_read_memory,
448 0, dld_cache.load_map);
449
450 if (!dll_path)
451 error ("pa64_solib_add, unable to read shared library path.");
452
453 add_to_solist (from_tty, dll_path, &dll_desc, 0, target);
454 }
455 }
456
457
458 /* This hook gets called just before the first instruction in the
459 inferior process is executed.
460
461 This is our opportunity to set magic flags in the inferior so
462 that GDB can be notified when a shared library is mapped in and
463 to tell the dynamic linker that a private copy of the library is
464 needed (so GDB can set breakpoints in the library).
465
466 We need to set two flag bits in this routine.
467
468 DT_HP_DEBUG_PRIVATE to indicate that shared libraries should be
469 mapped private.
470
471 DT_HP_DEBUG_CALLBACK to indicate that we want the dynamic linker to
472 call the breakpoint routine for significant events. */
473
474 void
475 pa64_solib_create_inferior_hook ()
476 {
477 struct minimal_symbol *msymbol;
478 unsigned int dld_flags, status;
479 asection *shlib_info, *interp_sect;
480 char buf[4];
481 struct objfile *objfile;
482 CORE_ADDR anaddr;
483
484 /* First, remove all the solib event breakpoints. Their addresses
485 may have changed since the last time we ran the program. */
486 remove_solib_event_breakpoints ();
487
488 if (symfile_objfile == NULL)
489 return;
490
491 /* First see if the objfile was dynamically linked. */
492 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
493 if (!shlib_info)
494 return;
495
496 /* It's got a .dynamic section, make sure it's not empty. */
497 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
498 return;
499
500 /* Read in the .dynamic section. */
501 if (! read_dynamic_info (shlib_info, &dld_cache))
502 error ("Unable to read the .dynamic section.");
503
504 /* Turn on the flags we care about. */
505 dld_cache.dld_flags |= DT_HP_DEBUG_PRIVATE;
506 dld_cache.dld_flags |= DT_HP_DEBUG_CALLBACK;
507 status = target_write_memory (dld_cache.dld_flags_addr,
508 (char *) &dld_cache.dld_flags,
509 sizeof (dld_cache.dld_flags));
510 if (status != 0)
511 error ("Unable to modify dynamic linker flags.");
512
513 /* Now we have to create a shared library breakpoint in the dynamic
514 linker. This can be somewhat tricky since the symbol is inside
515 the dynamic linker (for which we do not have symbols or a base
516 load address! Luckily I wrote this code for solib.c years ago. */
517 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
518 if (interp_sect)
519 {
520 unsigned int interp_sect_size;
521 char *buf;
522 CORE_ADDR load_addr;
523 bfd *tmp_bfd;
524 CORE_ADDR sym_addr = 0;
525
526 /* Read the contents of the .interp section into a local buffer;
527 the contents specify the dynamic linker this program uses. */
528 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
529 buf = alloca (interp_sect_size);
530 bfd_get_section_contents (exec_bfd, interp_sect,
531 buf, 0, interp_sect_size);
532
533 /* Now we need to figure out where the dynamic linker was
534 loaded so that we can load its symbols and place a breakpoint
535 in the dynamic linker itself.
536
537 This address is stored on the stack. However, I've been unable
538 to find any magic formula to find it for Solaris (appears to
539 be trivial on GNU/Linux). Therefore, we have to try an alternate
540 mechanism to find the dynamic linker's base address. */
541 tmp_bfd = bfd_openr (buf, gnutarget);
542 if (tmp_bfd == NULL)
543 goto get_out;
544
545 /* Make sure the dynamic linker's really a useful object. */
546 if (!bfd_check_format (tmp_bfd, bfd_object))
547 {
548 warning ("Unable to grok dynamic linker %s as an object file", buf);
549 bfd_close (tmp_bfd);
550 goto get_out;
551 }
552
553 /* We find the dynamic linker's base address by examining the
554 current pc (which point at the entry point for the dynamic
555 linker) and subtracting the offset of the entry point.
556
557 Also note the breakpoint is the second instruction in the
558 routine. */
559 load_addr = read_pc () - tmp_bfd->start_address;
560 sym_addr = bfd_lookup_symbol (tmp_bfd, "__dld_break");
561 sym_addr = load_addr + sym_addr + 4;
562
563 /* Create the shared library breakpoint. */
564 {
565 struct breakpoint *b
566 = create_solib_event_breakpoint (sym_addr);
567
568 /* The breakpoint is actually hard-coded into the dynamic linker,
569 so we don't need to actually insert a breakpoint instruction
570 there. In fact, the dynamic linker's code is immutable, even to
571 ttrace, so we shouldn't even try to do that. For cases like
572 this, we have "permanent" breakpoints. */
573 make_breakpoint_permanent (b);
574 }
575
576 /* We're done with the temporary bfd. */
577 bfd_close (tmp_bfd);
578 }
579
580 get_out:
581 /* Wipe out all knowledge of old shared libraries since their
582 mapping can change from one exec to another! */
583 while (so_list_head)
584 {
585 struct so_list *temp;
586
587 temp = so_list_head;
588 free (so_list_head);
589 so_list_head = temp->next;
590 }
591 clear_symtab_users ();
592 }
593
594 /* This operation removes the "hook" between GDB and the dynamic linker,
595 which causes the dld to notify GDB of shared library events.
596
597 After this operation completes, the dld will no longer notify GDB of
598 shared library events. To resume notifications, GDB must call
599 pa64_solib_create_inferior_hook.
600
601 This operation does not remove any knowledge of shared libraries which
602 GDB may already have been notified of. */
603
604 void
605 pa64_solib_remove_inferior_hook (pid)
606 int pid;
607 {
608 /* Turn off the DT_HP_DEBUG_CALLBACK bit in the dynamic linker flags. */
609 dld_cache.dld_flags &= ~DT_HP_DEBUG_CALLBACK;
610 target_write_memory (dld_cache.dld_flags_addr,
611 (char *)&dld_cache.dld_flags,
612 sizeof (dld_cache.dld_flags));
613 }
614
615 /* This function creates a breakpoint on the dynamic linker hook, which
616 is called when e.g., a shl_load or shl_unload call is made. This
617 breakpoint will only trigger when a shl_load call is made.
618
619 If filename is NULL, then loads of any dll will be caught. Else,
620 only loads of the file whose pathname is the string contained by
621 filename will be caught.
622
623 Undefined behaviour is guaranteed if this function is called before
624 pa64_solib_create_inferior_hook. */
625
626 void
627 pa64_solib_create_catch_load_hook (pid, tempflag, filename, cond_string)
628 int pid;
629 int tempflag;
630 char *filename;
631 char *cond_string;
632 {
633 create_solib_load_event_breakpoint ("", tempflag, filename, cond_string);
634 }
635
636 /* This function creates a breakpoint on the dynamic linker hook, which
637 is called when e.g., a shl_load or shl_unload call is made. This
638 breakpoint will only trigger when a shl_unload call is made.
639
640 If filename is NULL, then unloads of any dll will be caught. Else,
641 only unloads of the file whose pathname is the string contained by
642 filename will be caught.
643
644 Undefined behaviour is guaranteed if this function is called before
645 pa64_solib_create_inferior_hook. */
646
647 void
648 pa64_solib_create_catch_unload_hook (pid, tempflag, filename, cond_string)
649 int pid;
650 int tempflag;
651 char *filename;
652 char *cond_string;
653 {
654 create_solib_unload_event_breakpoint ("", tempflag, filename, cond_string);
655 }
656
657 /* Return nonzero if the dynamic linker has reproted that a library
658 has been loaded. */
659
660 int
661 pa64_solib_have_load_event (pid)
662 int pid;
663 {
664 CORE_ADDR event_kind;
665
666 event_kind = read_register (ARG0_REGNUM);
667 return (event_kind == DLD_CB_LOAD);
668 }
669
670 /* Return nonzero if the dynamic linker has reproted that a library
671 has been unloaded. */
672 int
673 pa64_solib_have_unload_event (pid)
674 int pid;
675 {
676 CORE_ADDR event_kind;
677
678 event_kind = read_register (ARG0_REGNUM);
679 return (event_kind == DLD_CB_UNLOAD);
680 }
681
682 /* Return a pointer to a string indicating the pathname of the most
683 recently loaded library.
684
685 The caller is reposible for copying the string before the inferior is
686 restarted. */
687
688 char *
689 pa64_solib_loaded_library_pathname (pid)
690 int pid;
691 {
692 static char dll_path[MAXPATHLEN];
693 CORE_ADDR dll_path_addr = read_register (ARG3_REGNUM);
694 read_memory_string (dll_path_addr, dll_path, MAXPATHLEN);
695 return dll_path;
696 }
697
698 /* Return a pointer to a string indicating the pathname of the most
699 recently unloaded library.
700
701 The caller is reposible for copying the string before the inferior is
702 restarted. */
703
704 char *
705 pa64_solib_unloaded_library_pathname (pid)
706 int pid;
707 {
708 static char dll_path[MAXPATHLEN];
709 CORE_ADDR dll_path_addr = read_register (ARG3_REGNUM);
710 read_memory_string (dll_path_addr, dll_path, MAXPATHLEN);
711 return dll_path;
712 }
713
714 /* Return nonzero if PC is an address inside the dynamic linker. */
715
716 int
717 pa64_solib_in_dynamic_linker (pid, pc)
718 int pid;
719 CORE_ADDR pc;
720 {
721 asection *shlib_info;
722
723 if (symfile_objfile == NULL)
724 return 0;
725
726 if (!dld_cache.have_read_dld_descriptor)
727 if (!read_dld_descriptor (&current_target))
728 return 0;
729
730 return (pc >= dld_cache.dld_desc.text_base
731 && pc < dld_cache.dld_desc.text_base + dld_cache.dld_desc.text_size);
732 }
733
734
735 /* Return the GOT value for the shared library in which ADDR belongs. If
736 ADDR isn't in any known shared library, return zero. */
737
738 CORE_ADDR
739 pa64_solib_get_got_by_pc (addr)
740 CORE_ADDR addr;
741 {
742 struct so_list *so_list = so_list_head;
743 CORE_ADDR got_value = 0;
744
745 while (so_list)
746 {
747 if (so_list->pa64_solib_desc.text_base <= addr
748 && ((so_list->pa64_solib_desc.text_base
749 + so_list->pa64_solib_desc.text_size)
750 > addr))
751 {
752 got_value = so_list->pa64_solib_desc.linkage_ptr;
753 break;
754 }
755 so_list = so_list->next;
756 }
757 return got_value;
758 }
759
760 /* Return the address of the handle of the shared library in which ADDR
761 belongs. If ADDR isn't in any known shared library, return zero.
762
763 This function is used in hppa_fix_call_dummy in hppa-tdep.c. */
764
765 CORE_ADDR
766 pa64_solib_get_solib_by_pc (addr)
767 CORE_ADDR addr;
768 {
769 struct so_list *so_list = so_list_head;
770 CORE_ADDR retval = 0;
771
772 while (so_list)
773 {
774 if (so_list->pa64_solib_desc.text_base <= addr
775 && ((so_list->pa64_solib_desc.text_base
776 + so_list->pa64_solib_desc.text_size)
777 > addr))
778 {
779 retval = so_list->pa64_solib_desc_addr;
780 break;
781 }
782 so_list = so_list->next;
783 }
784 return retval;
785 }
786
787 /* Dump information about all the currently loaded shared libraries. */
788
789 static void
790 pa64_sharedlibrary_info_command (ignore, from_tty)
791 char *ignore;
792 int from_tty;
793 {
794 struct so_list *so_list = so_list_head;
795
796 if (exec_bfd == NULL)
797 {
798 printf_unfiltered ("No executable file.\n");
799 return;
800 }
801
802 if (so_list == NULL)
803 {
804 printf_unfiltered ("No shared libraries loaded at this time.\n");
805 return;
806 }
807
808 printf_unfiltered ("Shared Object Libraries\n");
809 printf_unfiltered (" %-19s%-19s%-19s%-19s\n",
810 " text start", " text end",
811 " data start", " data end");
812 while (so_list)
813 {
814 unsigned int flags;
815
816 printf_unfiltered ("%s", so_list->name);
817 if (so_list->objfile == NULL)
818 printf_unfiltered (" (symbols not loaded)");
819 if (so_list->loaded == 0)
820 printf_unfiltered (" (shared library unloaded)");
821 printf_unfiltered (" %-18s",
822 local_hex_string_custom (so_list->pa64_solib_desc.linkage_ptr,
823 "016l"));
824 printf_unfiltered ("\n");
825 printf_unfiltered ("%-18s",
826 local_hex_string_custom (so_list->pa64_solib_desc.text_base,
827 "016l"));
828 printf_unfiltered (" %-18s",
829 local_hex_string_custom ((so_list->pa64_solib_desc.text_base
830 + so_list->pa64_solib_desc.text_size),
831 "016l"));
832 printf_unfiltered (" %-18s",
833 local_hex_string_custom (so_list->pa64_solib_desc.data_base,
834 "016l"));
835 printf_unfiltered (" %-18s\n",
836 local_hex_string_custom ((so_list->pa64_solib_desc.data_base
837 + so_list->pa64_solib_desc.data_size),
838 "016l"));
839 so_list = so_list->next;
840 }
841 }
842
843 /* Load up one or more shared libraries as directed by the user. */
844
845 static void
846 pa64_solib_sharedlibrary_command (args, from_tty)
847 char *args;
848 int from_tty;
849 {
850 dont_repeat ();
851 pa64_solib_add (args, from_tty, (struct target_ops *) 0);
852 }
853
854 /* Return the name of the shared library containing ADDR or NULL if ADDR
855 is not contained in any known shared library. */
856
857 char *
858 pa64_solib_address (addr)
859 CORE_ADDR addr;
860 {
861 struct so_list *so = so_list_head;
862
863 while (so)
864 {
865 /* Is this address within this shlib's text range? If so,
866 return the shlib's name. */
867 if (addr >= so->pa64_solib_desc.text_base
868 && addr < (so->pa64_solib_desc.text_base
869 | so->pa64_solib_desc.text_size))
870 return so->name;
871
872 /* Nope, keep looking... */
873 so = so->next;
874 }
875
876 /* No, we couldn't prove that the address is within a shlib. */
877 return NULL;
878 }
879
880 /* We are killing the inferior and restarting the program. */
881
882 void
883 pa64_solib_restart ()
884 {
885 struct so_list *sl = so_list_head;
886
887 /* Before the shlib info vanishes, use it to disable any breakpoints
888 that may still be active in those shlibs. */
889 disable_breakpoints_in_shlibs (0);
890
891 /* Discard all the shlib descriptors. */
892 while (sl)
893 {
894 struct so_list *next_sl = sl->next;
895 free (sl);
896 sl = next_sl;
897 }
898 so_list_head = NULL;
899
900 pa64_solib_total_st_size = (LONGEST) 0;
901 pa64_solib_st_size_threshold_exceeded = 0;
902
903 dld_cache.is_valid = 0;
904 dld_cache.have_read_dld_descriptor = 0;
905 dld_cache.dld_flags_addr = 0;
906 dld_cache.load_map = 0;
907 dld_cache.load_map_addr = 0;
908 dld_cache.dld_desc.data_base = 0;
909 dld_cache.dld_flags = 0;
910 dld_cache.dyninfo_sect = 0;
911 }
912
913 void
914 _initialize_pa64_solib ()
915 {
916 add_com ("sharedlibrary", class_files, pa64_solib_sharedlibrary_command,
917 "Load shared object library symbols for files matching REGEXP.");
918 add_info ("sharedlibrary", pa64_sharedlibrary_info_command,
919 "Status of loaded shared object libraries.");
920 add_show_from_set
921 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
922 (char *) &auto_solib_add,
923 "Set autoloading size threshold (in megabytes) of shared library symbols.\n\
924 If nonzero, symbols from all shared object libraries will be loaded\n\
925 automatically when the inferior begins execution or when the dynamic linker\n\
926 informs gdb that a new library has been loaded, until the symbol table\n\
927 of the program and libraries exceeds this threshold.\n\
928 Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
929 &setlist),
930 &showlist);
931
932 /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how much
933 data space a process can use. We ought to be reading MAXDSIZ and
934 setting auto_solib_add to some large fraction of that value. If
935 not that, we maybe ought to be setting it smaller than the default
936 for MAXDSIZ (that being 64Mb, I believe). However, [1] this threshold
937 is only crudely approximated rather than actually measured, and [2]
938 50 Mbytes is too small for debugging gdb itself. Thus, the arbitrary
939 100 figure.
940 */
941 auto_solib_add = 100; /* Megabytes */
942
943 pa64_solib_restart ();
944 }
945
946 /* Get some HPUX-specific data from a shared lib. */
947 CORE_ADDR
948 so_lib_thread_start_addr (so)
949 struct so_list *so;
950 {
951 return so->pa64_solib_desc.tls_start_addr;
952 }
953
954 /* Read the dynamic linker's internal shared library descriptor.
955
956 This must happen after dld starts running, so we can't do it in
957 read_dynamic_info. Record the fact that we have loaded the
958 descriptor. If the library is archive bound, then return zero, else
959 return nonzero. */
960
961 static boolean
962 read_dld_descriptor (target)
963 struct target_ops *target;
964 {
965 char *dll_path;
966 asection *dyninfo_sect;
967
968 /* If necessary call read_dynamic_info to extract the contents of the
969 .dynamic section from the shared library. */
970 if (!dld_cache.is_valid)
971 {
972 if (symfile_objfile == NULL)
973 error ("No object file symbols.");
974
975 dyninfo_sect = bfd_get_section_by_name (symfile_objfile->obfd,
976 ".dynamic");
977 if (!dyninfo_sect)
978 {
979 return 0;
980 }
981
982 if (!read_dynamic_info (dyninfo_sect, &dld_cache))
983 error ("Unable to read in .dynamic section information.");
984 }
985
986 /* Read the load map pointer. */
987 if (target_read_memory (dld_cache.load_map_addr,
988 (char*) &dld_cache.load_map,
989 sizeof(dld_cache.load_map))
990 != 0)
991 {
992 error ("Error while reading in load map pointer.");
993 }
994
995 /* Read in the dld load module descriptor */
996 if (dlgetmodinfo (-1,
997 &dld_cache.dld_desc,
998 sizeof(dld_cache.dld_desc),
999 pa64_target_read_memory,
1000 0,
1001 dld_cache.load_map)
1002 == 0)
1003 {
1004 error ("Error trying to get information about dynamic linker.");
1005 }
1006
1007 /* Indicate that we have loaded the dld descriptor. */
1008 dld_cache.have_read_dld_descriptor = 1;
1009
1010 /* Add dld.sl to the list of known shared libraries so that we can
1011 do unwind, etc.
1012
1013 ?!? This may not be correct. Consider of dld.sl contains symbols
1014 which are also referenced/defined by the user program or some user
1015 shared library. We need to make absolutely sure that we do not
1016 pollute the namespace from GDB's point of view. */
1017 dll_path = dlgetname (&dld_cache.dld_desc,
1018 sizeof(dld_cache.dld_desc),
1019 pa64_target_read_memory,
1020 0,
1021 dld_cache.load_map);
1022 add_to_solist(0, dll_path, &dld_cache.dld_desc, 0, target);
1023
1024 return 1;
1025 }
1026
1027 /* Read the .dynamic section and extract the information of interest,
1028 which is stored in dld_cache. The routine elf_locate_base in solib.c
1029 was used as a model for this. */
1030
1031 static boolean
1032 read_dynamic_info (dyninfo_sect, dld_cache_p)
1033 asection *dyninfo_sect;
1034 dld_cache_t *dld_cache_p;
1035 {
1036 char *buf;
1037 char *bufend;
1038 CORE_ADDR dyninfo_addr;
1039 int dyninfo_sect_size;
1040 CORE_ADDR entry_addr;
1041
1042 /* Read in .dynamic section, silently ignore errors. */
1043 dyninfo_addr = bfd_section_vma (symfile_objfile->obfd, dyninfo_sect);
1044 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
1045 buf = alloca (dyninfo_sect_size);
1046 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
1047 return 0;
1048
1049 /* Scan the .dynamic section and record the items of interest.
1050 In particular, DT_HP_DLD_FLAGS */
1051 for (bufend = buf + dyninfo_sect_size, entry_addr = dyninfo_addr;
1052 buf < bufend;
1053 buf += sizeof (Elf64_Dyn), entry_addr += sizeof (Elf64_Dyn))
1054 {
1055 Elf64_Dyn *x_dynp = (Elf64_Dyn*)buf;
1056 Elf64_Sxword dyn_tag;
1057 CORE_ADDR dyn_ptr;
1058 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
1059
1060 dyn_tag = bfd_h_get_64 (symfile_objfile->obfd,
1061 (bfd_byte*) &x_dynp->d_tag);
1062
1063 /* We can't use a switch here because dyn_tag is 64 bits and HP's
1064 lame comiler does not handle 64bit items in switch statements. */
1065 if (dyn_tag == DT_NULL)
1066 break;
1067 else if (dyn_tag == DT_HP_DLD_FLAGS)
1068 {
1069 /* Set dld_flags_addr and dld_flags in *dld_cache_p */
1070 dld_cache_p->dld_flags_addr = entry_addr + offsetof(Elf64_Dyn, d_un);
1071 if (target_read_memory (dld_cache_p->dld_flags_addr,
1072 (char*) &dld_cache_p->dld_flags,
1073 sizeof(dld_cache_p->dld_flags))
1074 != 0)
1075 {
1076 error ("Error while reading in .dynamic section of the program.");
1077 }
1078 }
1079 else if (dyn_tag == DT_HP_LOAD_MAP)
1080 {
1081 /* Dld will place the address of the load map at load_map_addr
1082 after it starts running. */
1083 if (target_read_memory (entry_addr + offsetof(Elf64_Dyn,
1084 d_un.d_ptr),
1085 (char*) &dld_cache_p->load_map_addr,
1086 sizeof(dld_cache_p->load_map_addr))
1087 != 0)
1088 {
1089 error ("Error while reading in .dynamic section of the program.");
1090 }
1091 }
1092 else
1093 {
1094 /* tag is not of interest */
1095 }
1096 }
1097
1098 /* Record other information and set is_valid to 1. */
1099 dld_cache_p->dyninfo_sect = dyninfo_sect;
1100
1101 /* Verify that we read in required info. These fields are re-set to zero
1102 in pa64_solib_restart. */
1103
1104 if (dld_cache_p->dld_flags_addr != 0 && dld_cache_p->load_map_addr != 0)
1105 dld_cache_p->is_valid = 1;
1106 else
1107 return 0;
1108
1109 return 1;
1110 }
1111
1112 /* Wrapper for target_read_memory to make dlgetmodinfo happy. */
1113
1114 static void *
1115 pa64_target_read_memory (buffer, ptr, bufsiz, ident)
1116 void *buffer;
1117 CORE_ADDR ptr;
1118 size_t bufsiz;
1119 int ident;
1120 {
1121 if (target_read_memory (ptr, buffer, bufsiz) != 0)
1122 return 0;
1123 return buffer;
1124 }
1125
1126 /* Called from handle_dynlink_load_event and pa64_solib_add to add
1127 a shared library to so_list_head list and possibly to read in the
1128 debug information for the library.
1129
1130 If load_module_desc_p is NULL, then the load module descriptor must
1131 be read from the inferior process at the address load_module_desc_addr. */
1132
1133 static void
1134 add_to_solist (from_tty, dll_path, load_module_desc_p,
1135 load_module_desc_addr, target)
1136 boolean from_tty;
1137 char *dll_path;
1138 struct load_module_desc *load_module_desc_p;
1139 CORE_ADDR load_module_desc_addr;
1140 struct target_ops *target;
1141 {
1142 struct so_list *new_so, *so_list_tail;
1143 int pa64_solib_st_size_threshhold_exceeded;
1144 LONGEST st_size;
1145
1146 if (symfile_objfile == NULL)
1147 return;
1148
1149 so_list_tail = so_list_head;
1150 /* Find the end of the list of shared objects. */
1151 while (so_list_tail && so_list_tail->next)
1152 {
1153 if (strcmp (so_list_tail->name, dll_path) == 0)
1154 return;
1155 so_list_tail = so_list_tail->next;
1156 }
1157
1158 if (so_list_tail && strcmp (so_list_tail->name, dll_path) == 0)
1159 return;
1160
1161 /* Add the shared library to the so_list_head list */
1162 new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
1163 memset ((char *)new_so, 0, sizeof (struct so_list));
1164 if (so_list_head == NULL)
1165 {
1166 so_list_head = new_so;
1167 so_list_tail = new_so;
1168 }
1169 else
1170 {
1171 so_list_tail->next = new_so;
1172 so_list_tail = new_so;
1173 }
1174
1175 /* Initialize the new_so */
1176 if (load_module_desc_p)
1177 {
1178 new_so->pa64_solib_desc = *load_module_desc_p;
1179 }
1180 else
1181 {
1182 if (target_read_memory (load_module_desc_addr,
1183 (char*) &new_so->pa64_solib_desc,
1184 sizeof(struct load_module_desc))
1185 != 0)
1186 {
1187 error ("Error while reading in dynamic library %s", dll_path);
1188 }
1189 }
1190
1191 new_so->pa64_solib_desc_addr = load_module_desc_addr;
1192 new_so->loaded = 1;
1193 new_so->name = obsavestring (dll_path, strlen(dll_path),
1194 &symfile_objfile->symbol_obstack);
1195
1196 /* If we are not going to load the library, tell the user if we
1197 haven't already and return. */
1198
1199 st_size = pa64_solib_sizeof_symbol_table (dll_path);
1200 pa64_solib_st_size_threshhold_exceeded =
1201 !from_tty
1202 && ( (st_size + pa64_solib_total_st_size)
1203 > (auto_solib_add * (LONGEST)1000000));
1204 if (pa64_solib_st_size_threshhold_exceeded)
1205 {
1206 pa64_solib_add_solib_objfile (new_so, dll_path, from_tty, 1);
1207 return;
1208 }
1209
1210 /* Now read in debug info. */
1211 pa64_solib_total_st_size += st_size;
1212
1213 /* This fills in new_so->objfile, among others. */
1214 pa64_solib_load_symbols (new_so,
1215 dll_path,
1216 from_tty,
1217 0);
1218 return;
1219 }
1220
1221
1222 /*
1223 LOCAL FUNCTION
1224
1225 bfd_lookup_symbol -- lookup the value for a specific symbol
1226
1227 SYNOPSIS
1228
1229 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
1230
1231 DESCRIPTION
1232
1233 An expensive way to lookup the value of a single symbol for
1234 bfd's that are only temporary anyway. This is used by the
1235 shared library support to find the address of the debugger
1236 interface structures in the shared library.
1237
1238 Note that 0 is specifically allowed as an error return (no
1239 such symbol).
1240 */
1241
1242 static CORE_ADDR
1243 bfd_lookup_symbol (abfd, symname)
1244 bfd *abfd;
1245 char *symname;
1246 {
1247 unsigned int storage_needed;
1248 asymbol *sym;
1249 asymbol **symbol_table;
1250 unsigned int number_of_symbols;
1251 unsigned int i;
1252 struct cleanup *back_to;
1253 CORE_ADDR symaddr = 0;
1254
1255 storage_needed = bfd_get_symtab_upper_bound (abfd);
1256
1257 if (storage_needed > 0)
1258 {
1259 symbol_table = (asymbol **) xmalloc (storage_needed);
1260 back_to = make_cleanup (free, (PTR) symbol_table);
1261 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1262
1263 for (i = 0; i < number_of_symbols; i++)
1264 {
1265 sym = *symbol_table++;
1266 if (STREQ (sym->name, symname))
1267 {
1268 /* Bfd symbols are section relative. */
1269 symaddr = sym->value + sym->section->vma;
1270 break;
1271 }
1272 }
1273 do_cleanups (back_to);
1274 }
1275 return (symaddr);
1276 }
1277
This page took 0.057207 seconds and 4 git commands to generate.