import gdb-1999-08-23 snapshot
[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
230 /* We need the BFD so that we can look at its sections. We open up the
231 file temporarily, then close it when we are done. */
232 tmp_bfd = bfd_openr (name, gnutarget);
233 if (tmp_bfd == NULL)
234 {
235 perror_with_name (name);
236 return;
237 }
238
239 if (!bfd_check_format (tmp_bfd, bfd_object))
240 {
241 bfd_close (tmp_bfd);
242 error ("\"%s\" is not an object file: %s", name,
243 bfd_errmsg (bfd_get_error ()));
244 }
245
246
247 /* Undo some braindamage from symfile.c.
248
249 First, symfile.c will subtract the VMA of the first .text section
250 in the shared library that it finds. Undo that. */
251 sec = bfd_get_section_by_name (tmp_bfd, ".text");
252 text_addr += bfd_section_vma (tmp_bfd, sec);
253
254 /* Now find the true lowest section in the shared library. */
255 sec = NULL;
256 bfd_map_over_sections (tmp_bfd, find_lowest_section, (PTR) &sec);
257
258 if (sec)
259 {
260 /* Subtract out the VMA of the lowest section. */
261 text_addr -= bfd_section_vma (tmp_bfd, sec);
262
263 /* ??? Add back in the filepos of that lowest section. */
264 text_addr += sec->filepos;
265 }
266
267 /* We are done with the temporary bfd. Get rid of it and make sure
268 nobody else can us it. */
269 bfd_close (tmp_bfd);
270 tmp_bfd = NULL;
271
272 /* Now let the generic code load up symbols for this library. */
273 so->objfile = symbol_file_add (name, from_tty, text_addr, 0, 0, 0, 0, 1);
274 so->abfd = so->objfile->obfd;
275
276 /* Mark this as a shared library and save private data. */
277 so->objfile->flags |= OBJF_SHARED;
278
279 if (so->objfile->obj_private == NULL)
280 {
281 obj_private = (obj_private_data_t *)
282 obstack_alloc (&so->objfile->psymbol_obstack,
283 sizeof (obj_private_data_t));
284 obj_private->unwind_info = NULL;
285 obj_private->so_info = NULL;
286 so->objfile->obj_private = (PTR) obj_private;
287 }
288
289 obj_private = (obj_private_data_t *) so->objfile->obj_private;
290 obj_private->so_info = so;
291 obj_private->dp = so->pa64_solib_desc.linkage_ptr;
292 }
293
294 /* Load debugging information for a shared library. TARGET may be
295 NULL if we are not attaching to a process or reading a core file. */
296
297 static void
298 pa64_solib_load_symbols (so, name, from_tty, text_addr, target)
299 struct so_list *so;
300 char *name;
301 int from_tty;
302 CORE_ADDR text_addr;
303 struct target_ops *target;
304 {
305 struct section_table *p;
306 asection *sec;
307 int status;
308 char buf[4];
309 CORE_ADDR presumed_data_start;
310
311 if (text_addr == 0)
312 text_addr = so->pa64_solib_desc.text_base;
313
314 pa64_solib_add_solib_objfile (so, name, from_tty, text_addr);
315
316 /* Now we need to build a section table for this library since
317 we might be debugging a core file from a dynamically linked
318 executable in which the libraries were not privately mapped. */
319 if (build_section_table (so->abfd,
320 &so->sections,
321 &so->sections_end))
322 {
323 error ("Unable to build section table for shared library\n.");
324 return;
325 }
326
327 ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT)
328 = so->pa64_solib_desc.text_base;
329 ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA)
330 = so->pa64_solib_desc.data_base;
331
332 /* Relocate all the sections based on where they got loaded. */
333 for (p = so->sections; p < so->sections_end; p++)
334 {
335 if (p->the_bfd_section->flags & SEC_CODE)
336 {
337 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT);
338 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT);
339 }
340 else if (p->the_bfd_section->flags & SEC_DATA)
341 {
342 p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA);
343 p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA);
344 }
345 }
346
347 /* Now see if we need to map in the text and data for this shared
348 library (for example debugging a core file which does not use
349 private shared libraries.).
350
351 Carefully peek at the first text address in the library. If the
352 read succeeds, then the libraries were privately mapped and were
353 included in the core dump file.
354
355 If the peek failed, then the libraries were not privately mapped
356 and are not in the core file, we'll have to read them in ourselves. */
357 status = target_read_memory (text_addr, buf, 4);
358 if (status != 0)
359 {
360 int old, new;
361 int update_coreops;
362 int update_execops;
363
364 /* We must update the to_sections field in the core_ops structure
365 here, otherwise we dereference a potential dangling pointer
366 for each call to target_read/write_memory within this routine. */
367 update_coreops = core_ops.to_sections == target->to_sections;
368
369 /* Ditto exec_ops (this was a bug). */
370 update_execops = exec_ops.to_sections == target->to_sections;
371
372 new = so->sections_end - so->sections;
373 /* Add sections from the shared library to the core target. */
374 if (target->to_sections)
375 {
376 old = target->to_sections_end - target->to_sections;
377 target->to_sections = (struct section_table *)
378 xrealloc ((char *) target->to_sections,
379 ((sizeof (struct section_table)) * (old + new)));
380 }
381 else
382 {
383 old = 0;
384 target->to_sections = (struct section_table *)
385 xmalloc ((sizeof (struct section_table)) * new);
386 }
387 target->to_sections_end = (target->to_sections + old + new);
388
389 /* Update the to_sections field in the core_ops structure
390 if needed, ditto exec_ops. */
391 if (update_coreops)
392 {
393 core_ops.to_sections = target->to_sections;
394 core_ops.to_sections_end = target->to_sections_end;
395 }
396
397 if (update_execops)
398 {
399 exec_ops.to_sections = target->to_sections;
400 exec_ops.to_sections_end = target->to_sections_end;
401 }
402
403 /* Copy over the old data before it gets clobbered. */
404 memcpy ((char *) (target->to_sections + old),
405 so->sections,
406 ((sizeof (struct section_table)) * new));
407 }
408 }
409
410
411 /* Add symbols from shared libraries into the symtab list, unless the
412 size threshold (specified by auto_solib_add, in megabytes) would
413 be exceeded. */
414
415 void
416 pa64_solib_add (arg_string, from_tty, target)
417 char *arg_string;
418 int from_tty;
419 struct target_ops *target;
420 {
421 struct minimal_symbol *msymbol;
422 CORE_ADDR addr;
423 asection *shlib_info;
424 int status;
425 unsigned int dld_flags;
426 char buf[4], *re_err;
427 int threshold_warning_given = 0;
428 int dll_index;
429 struct load_module_desc dll_desc;
430 char *dll_path;
431
432 /* First validate our arguments. */
433 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
434 {
435 error ("Invalid regexp: %s", re_err);
436 }
437
438 /* If we're debugging a core file, or have attached to a running
439 process, then pa64_solib_create_inferior_hook will not have been
440 called.
441
442 We need to first determine if we're dealing with a dynamically
443 linked executable. If not, then return without an error or warning.
444
445 We also need to examine __dld_flags to determine if the shared library
446 list is valid and to determine if the libraries have been privately
447 mapped. */
448 if (symfile_objfile == NULL)
449 return;
450
451 /* First see if the objfile was dynamically linked. */
452 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
453 if (!shlib_info)
454 return;
455
456 /* It's got a .dynamic section, make sure it's not empty. */
457 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
458 return;
459
460 /* Read in the load map pointer if we have not done so already. */
461 if (! dld_cache.have_read_dld_descriptor)
462 if (! read_dld_descriptor (target))
463 return;
464
465 /* If the libraries were not mapped private, warn the user. */
466 if ((dld_cache.dld_flags & DT_HP_DEBUG_PRIVATE) == 0)
467 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
468
469 /* For each shaerd library, add it to the shared library list. */
470 for (dll_index = 1; ; dll_index++)
471 {
472 /* Read in the load module descriptor. */
473 if (dlgetmodinfo (dll_index, &dll_desc, sizeof (dll_desc),
474 pa64_target_read_memory, 0, dld_cache.load_map)
475 == 0)
476 return;
477
478 /* Get the name of the shared library. */
479 dll_path = (char *)dlgetname (&dll_desc, sizeof (dll_desc),
480 pa64_target_read_memory,
481 0, dld_cache.load_map);
482
483 if (!dll_path)
484 error ("pa64_solib_add, unable to read shared library path.");
485
486 add_to_solist (from_tty, dll_path, &dll_desc, 0, target);
487 }
488 }
489
490
491 /* This hook gets called just before the first instruction in the
492 inferior process is executed.
493
494 This is our opportunity to set magic flags in the inferior so
495 that GDB can be notified when a shared library is mapped in and
496 to tell the dynamic linker that a private copy of the library is
497 needed (so GDB can set breakpoints in the library).
498
499 We need to set two flag bits in this routine.
500
501 DT_HP_DEBUG_PRIVATE to indicate that shared libraries should be
502 mapped private.
503
504 DT_HP_DEBUG_CALLBACK to indicate that we want the dynamic linker to
505 call the breakpoint routine for significant events. */
506
507 void
508 pa64_solib_create_inferior_hook ()
509 {
510 struct minimal_symbol *msymbol;
511 unsigned int dld_flags, status;
512 asection *shlib_info, *interp_sect;
513 char buf[4];
514 struct objfile *objfile;
515 CORE_ADDR anaddr;
516
517 /* First, remove all the solib event breakpoints. Their addresses
518 may have changed since the last time we ran the program. */
519 remove_solib_event_breakpoints ();
520
521 if (symfile_objfile == NULL)
522 return;
523
524 /* First see if the objfile was dynamically linked. */
525 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
526 if (!shlib_info)
527 return;
528
529 /* It's got a .dynamic section, make sure it's not empty. */
530 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
531 return;
532
533 /* Slam the pid of the process into __d_pid; failing is only a warning! */
534 msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
535 if (msymbol == NULL)
536 {
537 warning ("Unable to find __d_pid symbol in object file.");
538 warning ("Suggest linking with /opt/langtools/lib/end.o.");
539 warning ("GDB will be unable to track explicit library load/unload calls");
540 goto keep_going;
541 }
542
543 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
544 store_unsigned_integer (buf, 4, inferior_pid);
545 status = target_write_memory (anaddr, buf, 4);
546 if (status != 0)
547 {
548 warning ("Unable to write __d_pid");
549 warning ("Suggest linking with /opt/langtools/lib/end.o.");
550 warning ("GDB will be unable to track explicit library load/unload calls");
551 goto keep_going;
552 }
553
554 keep_going:
555
556 /* Read in the .dynamic section. */
557 if (! read_dynamic_info (shlib_info, &dld_cache))
558 error ("Unable to read the .dynamic section.");
559
560 /* Turn on the flags we care about. */
561 dld_cache.dld_flags |= DT_HP_DEBUG_PRIVATE;
562 /* ?!? Right now GDB is not recognizing hitting the callback breakpoint
563 as a shared library event. Fix that and remove the #if0 code. */
564 #if 0
565 dld_cache.dld_flags |= DT_HP_DEBUG_CALLBACK;
566 #endif
567 status = target_write_memory (dld_cache.dld_flags_addr,
568 (char *) &dld_cache.dld_flags,
569 sizeof (dld_cache.dld_flags));
570 if (status != 0)
571 error ("Unable to modify dynamic linker flags.");
572
573 /* Now we have to create a shared library breakpoint in the dynamic
574 linker. This can be somewhat tricky since the symbol is inside
575 the dynamic linker (for which we do not have symbols or a base
576 load address! Luckily I wrote this code for solib.c years ago. */
577 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
578 if (interp_sect)
579 {
580 unsigned int interp_sect_size;
581 char *buf;
582 CORE_ADDR load_addr;
583 bfd *tmp_bfd;
584 CORE_ADDR sym_addr = 0;
585
586 /* Read the contents of the .interp section into a local buffer;
587 the contents specify the dynamic linker this program uses. */
588 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
589 buf = alloca (interp_sect_size);
590 bfd_get_section_contents (exec_bfd, interp_sect,
591 buf, 0, interp_sect_size);
592
593 /* Now we need to figure out where the dynamic linker was
594 loaded so that we can load its symbols and place a breakpoint
595 in the dynamic linker itself.
596
597 This address is stored on the stack. However, I've been unable
598 to find any magic formula to find it for Solaris (appears to
599 be trivial on GNU/Linux). Therefore, we have to try an alternate
600 mechanism to find the dynamic linker's base address. */
601 tmp_bfd = bfd_openr (buf, gnutarget);
602 if (tmp_bfd == NULL)
603 goto get_out;
604
605 /* Make sure the dynamic linker's really a useful object. */
606 if (!bfd_check_format (tmp_bfd, bfd_object))
607 {
608 warning ("Unable to grok dynamic linker %s as an object file", buf);
609 bfd_close (tmp_bfd);
610 goto get_out;
611 }
612
613 /* We find the dynamic linker's base address by examining the
614 current pc (which point at the entry point for the dynamic
615 linker) and subtracting the offset of the entry point.
616
617 Also note the breakpoint is the second instruction in the
618 routine. */
619 load_addr = read_pc () - tmp_bfd->start_address;
620 sym_addr = bfd_lookup_symbol (tmp_bfd, "__dld_break");
621 sym_addr = load_addr + sym_addr + 4;
622
623 /* Create the shared library breakpoint. */
624 create_solib_event_breakpoint (sym_addr);
625
626 /* We're done with the temporary bfd. */
627 bfd_close (tmp_bfd);
628 }
629
630 get_out:
631 /* Wipe out all knowledge of old shared libraries since their
632 mapping can change from one exec to another! */
633 while (so_list_head)
634 {
635 struct so_list *temp;
636
637 temp = so_list_head;
638 free (so_list_head);
639 so_list_head = temp->next;
640 }
641 clear_symtab_users ();
642 }
643
644 /* This operation removes the "hook" between GDB and the dynamic linker,
645 which causes the dld to notify GDB of shared library events.
646
647 After this operation completes, the dld will no longer notify GDB of
648 shared library events. To resume notifications, GDB must call
649 pa64_solib_create_inferior_hook.
650
651 This operation does not remove any knowledge of shared libraries which
652 GDB may already have been notified of. */
653
654 void
655 pa64_solib_remove_inferior_hook (pid)
656 int pid;
657 {
658 /* Turn off the DT_HP_DEBUG_CALLBACK bit in the dynamic linker flags. */
659 dld_cache.dld_flags &= ~DT_HP_DEBUG_CALLBACK;
660 target_write_memory (dld_cache.dld_flags_addr,
661 (char *)&dld_cache.dld_flags,
662 sizeof (dld_cache.dld_flags));
663 }
664
665 /* This function creates a breakpoint on the dynamic linker hook, which
666 is called when e.g., a shl_load or shl_unload call is made. This
667 breakpoint will only trigger when a shl_load call is made.
668
669 If filename is NULL, then loads of any dll will be caught. Else,
670 only loads of the file whose pathname is the string contained by
671 filename will be caught.
672
673 Undefined behaviour is guaranteed if this function is called before
674 pa64_solib_create_inferior_hook. */
675
676 void
677 pa64_solib_create_catch_load_hook (pid, tempflag, filename, cond_string)
678 int pid;
679 int tempflag;
680 char *filename;
681 char *cond_string;
682 {
683 create_solib_load_event_breakpoint ("", tempflag, filename, cond_string);
684 }
685
686 /* This function creates a breakpoint on the dynamic linker hook, which
687 is called when e.g., a shl_load or shl_unload call is made. This
688 breakpoint will only trigger when a shl_unload call is made.
689
690 If filename is NULL, then unloads of any dll will be caught. Else,
691 only unloads of the file whose pathname is the string contained by
692 filename will be caught.
693
694 Undefined behaviour is guaranteed if this function is called before
695 pa64_solib_create_inferior_hook. */
696
697 void
698 pa64_solib_create_catch_unload_hook (pid, tempflag, filename, cond_string)
699 int pid;
700 int tempflag;
701 char *filename;
702 char *cond_string;
703 {
704 create_solib_unload_event_breakpoint ("", tempflag, filename, cond_string);
705 }
706
707 /* Return nonzero if the dynamic linker has reproted that a library
708 has been loaded. */
709
710 int
711 pa64_solib_have_load_event (pid)
712 int pid;
713 {
714 CORE_ADDR event_kind;
715
716 event_kind = read_register (ARG0_REGNUM);
717 return (event_kind == DLD_CB_LOAD);
718 }
719
720 /* Return nonzero if the dynamic linker has reproted that a library
721 has been unloaded. */
722 int
723 pa64_solib_have_unload_event (pid)
724 int pid;
725 {
726 CORE_ADDR event_kind;
727
728 event_kind = read_register (ARG0_REGNUM);
729 return (event_kind == DLD_CB_UNLOAD);
730 }
731
732 /* Return a pointer to a string indicating the pathname of the most
733 recently loaded library.
734
735 The caller is reposible for copying the string before the inferior is
736 restarted. */
737
738 char *
739 pa64_solib_loaded_library_pathname (pid)
740 int pid;
741 {
742 static char dll_path[MAXPATHLEN];
743 CORE_ADDR dll_path_addr = read_register (ARG3_REGNUM);
744 read_memory_string (dll_path_addr, dll_path, MAXPATHLEN);
745 return dll_path;
746 }
747
748 /* Return a pointer to a string indicating the pathname of the most
749 recently unloaded library.
750
751 The caller is reposible for copying the string before the inferior is
752 restarted. */
753
754 char *
755 pa64_solib_unloaded_library_pathname (pid)
756 int pid;
757 {
758 static char dll_path[MAXPATHLEN];
759 CORE_ADDR dll_path_addr = read_register (ARG3_REGNUM);
760 read_memory_string (dll_path_addr, dll_path, MAXPATHLEN);
761 return dll_path;
762 }
763
764 /* Return nonzero if PC is an address inside the dynamic linker. */
765
766 int
767 pa64_solib_in_dynamic_linker (pid, pc)
768 int pid;
769 CORE_ADDR pc;
770 {
771 asection *shlib_info;
772
773 if (symfile_objfile == NULL)
774 return 0;
775
776 if (!dld_cache.have_read_dld_descriptor)
777 if (!read_dld_descriptor (&current_target))
778 return 0;
779
780 return (pc >= dld_cache.dld_desc.text_base
781 && pc < dld_cache.dld_desc.text_base + dld_cache.dld_desc.text_size);
782 }
783
784
785 /* Return the GOT value for the shared library in which ADDR belongs. If
786 ADDR isn't in any known shared library, return zero. */
787
788 CORE_ADDR
789 pa64_solib_get_got_by_pc (addr)
790 CORE_ADDR addr;
791 {
792 struct so_list *so_list = so_list_head;
793 CORE_ADDR got_value = 0;
794
795 while (so_list)
796 {
797 if (so_list->pa64_solib_desc.text_base <= addr
798 && ((so_list->pa64_solib_desc.text_base
799 + so_list->pa64_solib_desc.text_size)
800 > addr))
801 {
802 got_value = so_list->pa64_solib_desc.linkage_ptr;
803 break;
804 }
805 so_list = so_list->next;
806 }
807 return got_value;
808 }
809
810 /* Return the address of the handle of the shared library in which ADDR
811 belongs. If ADDR isn't in any known shared library, return zero.
812
813 This function is used in hppa_fix_call_dummy in hppa-tdep.c. */
814
815 CORE_ADDR
816 pa64_solib_get_solib_by_pc (addr)
817 CORE_ADDR addr;
818 {
819 struct so_list *so_list = so_list_head;
820 CORE_ADDR retval = 0;
821
822 while (so_list)
823 {
824 if (so_list->pa64_solib_desc.text_base <= addr
825 && ((so_list->pa64_solib_desc.text_base
826 + so_list->pa64_solib_desc.text_size)
827 > addr))
828 {
829 retval = so_list->pa64_solib_desc_addr;
830 break;
831 }
832 so_list = so_list->next;
833 }
834 return retval;
835 }
836
837 /* Dump information about all the currently loaded shared libraries. */
838
839 static void
840 pa64_sharedlibrary_info_command (ignore, from_tty)
841 char *ignore;
842 int from_tty;
843 {
844 struct so_list *so_list = so_list_head;
845
846 if (exec_bfd == NULL)
847 {
848 printf_unfiltered ("no exec file.\n");
849 return;
850 }
851
852 if (so_list == NULL)
853 {
854 printf_unfiltered ("No shared libraries loaded at this time.\n");
855 return;
856 }
857
858 printf_unfiltered ("Shared Object Libraries\n");
859 printf_unfiltered (" %-19s%-19s%-19s%-19s\n",
860 " tstart", " tend",
861 " dstart", " dend");
862 while (so_list)
863 {
864 unsigned int flags;
865
866 printf_unfiltered ("%s", so_list->name);
867 if (so_list->objfile == NULL)
868 printf_unfiltered (" (symbols not loaded)");
869 if (so_list->loaded == 0)
870 printf_unfiltered (" (shared library unloaded)");
871 printf_unfiltered (" %-18s",
872 local_hex_string_custom (so_list->pa64_solib_desc.linkage_ptr,
873 "016l"));
874 printf_unfiltered ("\n");
875 printf_unfiltered ("%-18s",
876 local_hex_string_custom (so_list->pa64_solib_desc.text_base,
877 "016l"));
878 printf_unfiltered (" %-18s",
879 local_hex_string_custom ((so_list->pa64_solib_desc.text_base,
880 + so_list->pa64_solib_desc.text_size),
881 "016l"));
882 printf_unfiltered (" %-18s",
883 local_hex_string_custom (so_list->pa64_solib_desc.data_base,
884 "016l"));
885 printf_unfiltered (" %-18s\n",
886 local_hex_string_custom ((so_list->pa64_solib_desc.data_base,
887 + so_list->pa64_solib_desc.data_size),
888 "016l"));
889 so_list = so_list->next;
890 }
891 }
892
893 /* Load up one or more shared libraries as directed by the user. */
894
895 static void
896 pa64_solib_sharedlibrary_command (args, from_tty)
897 char *args;
898 int from_tty;
899 {
900 dont_repeat ();
901 pa64_solib_add (args, from_tty, (struct target_ops *) 0);
902 }
903
904 /* Return the name of the shared library containing ADDR or NULL if ADDR
905 is not contained in any known shared library. */
906
907 char *
908 pa64_solib_address (addr)
909 CORE_ADDR addr;
910 {
911 struct so_list *so = so_list_head;
912
913 while (so)
914 {
915 /* Is this address within this shlib's text range? If so,
916 return the shlib's name. */
917 if (addr >= so->pa64_solib_desc.text_base
918 && addr < (so->pa64_solib_desc.text_base
919 | so->pa64_solib_desc.text_size))
920 return so->name;
921
922 /* Nope, keep looking... */
923 so = so->next;
924 }
925
926 /* No, we couldn't prove that the address is within a shlib. */
927 return NULL;
928 }
929
930 /* We are killing the inferior and restarting the program. */
931
932 void
933 pa64_solib_restart ()
934 {
935 struct so_list *sl = so_list_head;
936
937 /* Before the shlib info vanishes, use it to disable any breakpoints
938 that may still be active in those shlibs. */
939 disable_breakpoints_in_shlibs (0);
940
941 /* Discard all the shlib descriptors. */
942 while (sl)
943 {
944 struct so_list *next_sl = sl->next;
945 free (sl);
946 sl = next_sl;
947 }
948 so_list_head = NULL;
949
950 pa64_solib_total_st_size = (LONGEST) 0;
951 pa64_solib_st_size_threshold_exceeded = 0;
952
953 dld_cache.is_valid = 0;
954 dld_cache.have_read_dld_descriptor = 0;
955 dld_cache.dld_flags_addr = 0;
956 dld_cache.load_map = 0;
957 dld_cache.load_map_addr = 0;
958 dld_cache.dld_desc.data_base = 0;
959 dld_cache.dld_flags = 0;
960 dld_cache.dyninfo_sect = 0;
961 }
962
963 void
964 _initialize_pa64_solib ()
965 {
966 add_com ("sharedlibrary", class_files, pa64_solib_sharedlibrary_command,
967 "Load shared object library symbols for files matching REGEXP.");
968 add_info ("sharedlibrary", pa64_sharedlibrary_info_command,
969 "Status of loaded shared object libraries.");
970 add_show_from_set
971 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
972 (char *) &auto_solib_add,
973 "Set autoloading size threshold (in megabytes) of shared library symbols.\n\
974 If nonzero, symbols from all shared object libraries will be loaded\n\
975 automatically when the inferior begins execution or when the dynamic linker\n\
976 informs gdb that a new library has been loaded, until the symbol table\n\
977 of the program and libraries exceeds this threshold.\n\
978 Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
979 &setlist),
980 &showlist);
981
982 /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how much
983 data space a process can use. We ought to be reading MAXDSIZ and
984 setting auto_solib_add to some large fraction of that value. If
985 not that, we maybe ought to be setting it smaller than the default
986 for MAXDSIZ (that being 64Mb, I believe). However, [1] this threshold
987 is only crudely approximated rather than actually measured, and [2]
988 50 Mbytes is too small for debugging gdb itself. Thus, the arbitrary
989 100 figure.
990 */
991 auto_solib_add = 100; /* Megabytes */
992
993 pa64_solib_restart ();
994 }
995
996 /* Get some HPUX-specific data from a shared lib. */
997 CORE_ADDR
998 so_lib_thread_start_addr (so)
999 struct so_list *so;
1000 {
1001 return so->pa64_solib_desc.tls_start_addr;
1002 }
1003
1004 /* Read the dynamic linker's internal shared library descriptor.
1005
1006 This must happen after dld starts running, so we can't do it in
1007 read_dynamic_info. Record the fact that we have loaded the
1008 descriptor. If the library is archive bound, then return zero, else
1009 return nonzero. */
1010
1011 static boolean
1012 read_dld_descriptor (target)
1013 struct target_ops *target;
1014 {
1015 char *dll_path;
1016 asection *dyninfo_sect;
1017
1018 /* If necessary call read_dynamic_info to extract the contents of the
1019 .dynamic section from the shared library. */
1020 if (!dld_cache.is_valid)
1021 {
1022 if (symfile_objfile == NULL)
1023 error ("No object file symbols.");
1024
1025 dyninfo_sect = bfd_get_section_by_name (symfile_objfile->obfd,
1026 ".dynamic");
1027 if (!dyninfo_sect)
1028 {
1029 return 0;
1030 }
1031
1032 if (!read_dynamic_info (dyninfo_sect, &dld_cache))
1033 error ("Unable to read in .dynamic section information.");
1034 }
1035
1036 /* Read the load map pointer. */
1037 if (target_read_memory (dld_cache.load_map_addr,
1038 (char*) &dld_cache.load_map,
1039 sizeof(dld_cache.load_map))
1040 != 0)
1041 {
1042 error ("Error while reading in load map pointer.");
1043 }
1044
1045 /* Read in the dld load module descriptor */
1046 if (dlgetmodinfo (-1,
1047 &dld_cache.dld_desc,
1048 sizeof(dld_cache.dld_desc),
1049 pa64_target_read_memory,
1050 0,
1051 dld_cache.load_map)
1052 == 0)
1053 {
1054 error ("Error trying to get information about dynamic linker.");
1055 }
1056
1057 /* Indicate that we have loaded the dld descriptor. */
1058 dld_cache.have_read_dld_descriptor = 1;
1059
1060 /* Add dld.sl to the list of known shared libraries so that we can
1061 do unwind, etc.
1062
1063 ?!? This may not be correct. Consider of dld.sl contains symbols
1064 which are also referenced/defined by the user program or some user
1065 shared library. We need to make absolutely sure that we do not
1066 pollute the namespace from GDB's point of view. */
1067 dll_path = dlgetname (&dld_cache.dld_desc,
1068 sizeof(dld_cache.dld_desc),
1069 pa64_target_read_memory,
1070 0,
1071 dld_cache.load_map);
1072 add_to_solist(0, dll_path, &dld_cache.dld_desc, 0, target);
1073
1074 return 1;
1075 }
1076
1077 /* Read the .dynamic section and extract the information of interest,
1078 which is stored in dld_cache. The routine elf_locate_base in solib.c
1079 was used as a model for this. */
1080
1081 static boolean
1082 read_dynamic_info (dyninfo_sect, dld_cache_p)
1083 asection *dyninfo_sect;
1084 dld_cache_t *dld_cache_p;
1085 {
1086 char *buf;
1087 char *bufend;
1088 CORE_ADDR dyninfo_addr;
1089 int dyninfo_sect_size;
1090 CORE_ADDR entry_addr;
1091
1092 /* Read in .dynamic section, silently ignore errors. */
1093 dyninfo_addr = bfd_section_vma (symfile_objfile->obfd, dyninfo_sect);
1094 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
1095 buf = alloca (dyninfo_sect_size);
1096 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
1097 return 0;
1098
1099 /* Scan the .dynamic section and record the items of interest.
1100 In particular, DT_HP_DLD_FLAGS */
1101 for (bufend = buf + dyninfo_sect_size, entry_addr = dyninfo_addr;
1102 buf < bufend;
1103 buf += sizeof (Elf64_Dyn), entry_addr += sizeof (Elf64_Dyn))
1104 {
1105 Elf64_Dyn *x_dynp = (Elf64_Dyn*)buf;
1106 Elf64_Sxword dyn_tag;
1107 CORE_ADDR dyn_ptr;
1108 char pbuf[TARGET_PTR_BIT / HOST_CHAR_BIT];
1109
1110 dyn_tag = bfd_h_get_64 (symfile_objfile->obfd,
1111 (bfd_byte*) &x_dynp->d_tag);
1112
1113 /* We can't use a switch here because dyn_tag is 64 bits and HP's
1114 lame comiler does not handle 64bit items in switch statements. */
1115 if (dyn_tag == DT_NULL)
1116 break;
1117 else if (dyn_tag == DT_HP_DLD_FLAGS)
1118 {
1119 /* Set dld_flags_addr and dld_flags in *dld_cache_p */
1120 dld_cache_p->dld_flags_addr = entry_addr + offsetof(Elf64_Dyn, d_un);
1121 if (target_read_memory (dld_cache_p->dld_flags_addr,
1122 (char*) &dld_cache_p->dld_flags,
1123 sizeof(dld_cache_p->dld_flags))
1124 != 0)
1125 {
1126 error ("Error while reading in .dynamic section of the program.");
1127 }
1128 }
1129 else if (dyn_tag == DT_HP_LOAD_MAP)
1130 {
1131 /* Dld will place the address of the load map at load_map_addr
1132 after it starts running. */
1133 if (target_read_memory (entry_addr + offsetof(Elf64_Dyn,
1134 d_un.d_ptr),
1135 (char*) &dld_cache_p->load_map_addr,
1136 sizeof(dld_cache_p->load_map_addr))
1137 != 0)
1138 {
1139 error ("Error while reading in .dynamic section of the program.");
1140 }
1141 }
1142 else
1143 {
1144 /* tag is not of interest */
1145 }
1146 }
1147
1148 /* Record other information and set is_valid to 1. */
1149 dld_cache_p->dyninfo_sect = dyninfo_sect;
1150
1151 /* Verify that we read in required info. These fields are re-set to zero
1152 in pa64_solib_restart. */
1153
1154 if (dld_cache_p->dld_flags_addr != 0 && dld_cache_p->load_map_addr != 0)
1155 dld_cache_p->is_valid = 1;
1156 else
1157 return 0;
1158
1159 return 1;
1160 }
1161
1162 /* Wrapper for target_read_memory to make dlgetmodinfo happy. */
1163
1164 static void *
1165 pa64_target_read_memory (buffer, ptr, bufsiz, ident)
1166 void *buffer;
1167 CORE_ADDR ptr;
1168 size_t bufsiz;
1169 int ident;
1170 {
1171 if (target_read_memory (ptr, buffer, bufsiz) != 0)
1172 return 0;
1173 return buffer;
1174 }
1175
1176 /* Called from handle_dynlink_load_event and pa64_solib_add to add
1177 a shared library to so_list_head list and possibly to read in the
1178 debug information for the library.
1179
1180 If load_module_desc_p is NULL, then the load module descriptor must
1181 be read from the inferior process at the address load_module_desc_addr. */
1182
1183 static void
1184 add_to_solist (from_tty, dll_path, load_module_desc_p,
1185 load_module_desc_addr, target)
1186 boolean from_tty;
1187 char *dll_path;
1188 struct load_module_desc *load_module_desc_p;
1189 CORE_ADDR load_module_desc_addr;
1190 struct target_ops *target;
1191 {
1192 struct so_list *new_so, *so_list_tail;
1193 int pa64_solib_st_size_threshhold_exceeded;
1194 LONGEST st_size;
1195
1196 if (symfile_objfile == NULL)
1197 return;
1198
1199 so_list_tail = so_list_head;
1200 /* Find the end of the list of shared objects. */
1201 while (so_list_tail && so_list_tail->next)
1202 {
1203 if (strcmp (so_list_tail->name, dll_path) == 0)
1204 return;
1205 so_list_tail = so_list_tail->next;
1206 }
1207
1208 if (so_list_tail && strcmp (so_list_tail->name, dll_path) == 0)
1209 return;
1210
1211 /* Add the shared library to the so_list_head list */
1212 new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
1213 memset ((char *)new_so, 0, sizeof (struct so_list));
1214 if (so_list_head == NULL)
1215 {
1216 so_list_head = new_so;
1217 so_list_tail = new_so;
1218 }
1219 else
1220 {
1221 so_list_tail->next = new_so;
1222 so_list_tail = new_so;
1223 }
1224
1225 /* Initialize the new_so */
1226 if (load_module_desc_p)
1227 {
1228 new_so->pa64_solib_desc = *load_module_desc_p;
1229 }
1230 else
1231 {
1232 if (target_read_memory (load_module_desc_addr,
1233 (char*) &new_so->pa64_solib_desc,
1234 sizeof(struct load_module_desc))
1235 != 0)
1236 {
1237 error ("Error while reading in dynamic library %s", dll_path);
1238 }
1239 }
1240
1241 new_so->pa64_solib_desc_addr = load_module_desc_addr;
1242 new_so->loaded = 1;
1243 new_so->name = obsavestring (dll_path, strlen(dll_path),
1244 &symfile_objfile->symbol_obstack);
1245
1246 /* If we are not going to load the library, tell the user if we
1247 haven't already and return. */
1248
1249 st_size = pa64_solib_sizeof_symbol_table (dll_path);
1250 pa64_solib_st_size_threshhold_exceeded =
1251 !from_tty
1252 && ( (st_size + pa64_solib_total_st_size)
1253 > (auto_solib_add * (LONGEST)1000000));
1254 if (pa64_solib_st_size_threshhold_exceeded)
1255 {
1256 pa64_solib_add_solib_objfile (new_so, dll_path, from_tty, 1);
1257 return;
1258 }
1259
1260 /* Now read in debug info. */
1261 pa64_solib_total_st_size += st_size;
1262
1263 /* This fills in new_so->objfile, among others. */
1264 pa64_solib_load_symbols (new_so,
1265 dll_path,
1266 from_tty,
1267 0);
1268 return;
1269 }
1270
1271
1272 /*
1273 LOCAL FUNCTION
1274
1275 bfd_lookup_symbol -- lookup the value for a specific symbol
1276
1277 SYNOPSIS
1278
1279 CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname)
1280
1281 DESCRIPTION
1282
1283 An expensive way to lookup the value of a single symbol for
1284 bfd's that are only temporary anyway. This is used by the
1285 shared library support to find the address of the debugger
1286 interface structures in the shared library.
1287
1288 Note that 0 is specifically allowed as an error return (no
1289 such symbol).
1290 */
1291
1292 static CORE_ADDR
1293 bfd_lookup_symbol (abfd, symname)
1294 bfd *abfd;
1295 char *symname;
1296 {
1297 unsigned int storage_needed;
1298 asymbol *sym;
1299 asymbol **symbol_table;
1300 unsigned int number_of_symbols;
1301 unsigned int i;
1302 struct cleanup *back_to;
1303 CORE_ADDR symaddr = 0;
1304
1305 storage_needed = bfd_get_symtab_upper_bound (abfd);
1306
1307 if (storage_needed > 0)
1308 {
1309 symbol_table = (asymbol **) xmalloc (storage_needed);
1310 back_to = make_cleanup (free, (PTR) symbol_table);
1311 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
1312
1313 for (i = 0; i < number_of_symbols; i++)
1314 {
1315 sym = *symbol_table++;
1316 if (STREQ (sym->name, symname))
1317 {
1318 /* Bfd symbols are section relative. */
1319 symaddr = sym->value + sym->section->vma;
1320 break;
1321 }
1322 }
1323 do_cleanups (back_to);
1324 }
1325 return (symaddr);
1326 }
1327
This page took 0.060566 seconds and 4 git commands to generate.