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