2000-04-21 Elena Zannoni <ezannoni@kwikemart.cygnus.com>
[external/binutils.git] / gdb / somsolib.c
1 /* Handle HP SOM shared libraries for GDB, the GNU Debugger.
2    Copyright 1993, 1996, 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    Written by the Center for Software Science at the Univerity of Utah
22    and by Cygnus Support.  */
23
24
25 #include "defs.h"
26
27 #include "frame.h"
28 #include "bfd.h"
29 #include "som.h"
30 #include "libhppa.h"
31 #include "gdbcore.h"
32 #include "symtab.h"
33 #include "breakpoint.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "inferior.h"
37 #include "gdb-stabs.h"
38 #include "gdb_stat.h"
39 #include "gdbcmd.h"
40 #include "assert.h"
41 #include "language.h"
42
43 #include <fcntl.h>
44
45 #ifndef O_BINARY
46 #define O_BINARY 0
47 #endif
48
49 /* Uncomment this to turn on some debugging output.
50  */
51
52 /* #define SOLIB_DEBUG
53  */
54
55 /* Defined in exec.c; used to prevent dangling pointer bug.
56  */
57 extern struct target_ops exec_ops;
58
59 /* This lives in hppa-tdep.c. */
60 extern struct unwind_table_entry *find_unwind_entry PARAMS ((CORE_ADDR pc));
61
62 /* These ought to be defined in some public interface, but aren't.  They
63    define the meaning of the various bits in the distinguished __dld_flags
64    variable that is declared in every debuggable a.out on HP-UX, and that
65    is shared between the debugger and the dynamic linker.
66  */
67 #define DLD_FLAGS_MAPPRIVATE    0x1
68 #define DLD_FLAGS_HOOKVALID     0x2
69 #define DLD_FLAGS_LISTVALID     0x4
70 #define DLD_FLAGS_BOR_ENABLE    0x8
71
72 /* TODO:
73
74    * Most of this code should work for hp300 shared libraries.  Does
75    anyone care enough to weed out any SOM-isms.
76
77    * Support for hpux8 dynamic linker.  */
78
79 /* The basic structure which describes a dynamically loaded object.  This
80    data structure is private to the dynamic linker and isn't found in
81    any HPUX include file.  */
82
83 struct som_solib_mapped_entry
84   {
85     /* The name of the library.  */
86     char *name;
87
88     /* Version of this structure (it is expected to change again in hpux10).  */
89     unsigned char struct_version;
90
91     /* Binding mode for this library.  */
92     unsigned char bind_mode;
93
94     /* Version of this library.  */
95     short library_version;
96
97     /* Start of text address,
98      * link-time text location (length of text area),
99      * end of text address.  */
100     CORE_ADDR text_addr;
101     CORE_ADDR text_link_addr;
102     CORE_ADDR text_end;
103
104     /* Start of data, start of bss and end of data.  */
105     CORE_ADDR data_start;
106     CORE_ADDR bss_start;
107     CORE_ADDR data_end;
108
109     /* Value of linkage pointer (%r19).  */
110     CORE_ADDR got_value;
111
112     /* Next entry.  */
113     struct som_solib_mapped_entry *next;
114
115     /* There are other fields, but I don't have information as to what is
116        contained in them.  */
117
118     /* For versions from HPUX-10.30 and up */
119
120     /* Address in target of offset from thread-local register of
121      * start of this thread's data.  I.e., the first thread-local
122      * variable in this shared library starts at *(tsd_start_addr)
123      * from that area pointed to by cr27 (mpsfu_hi).
124      *
125      * We do the indirection as soon as we read it, so from then
126      * on it's the offset itself.
127      */
128     CORE_ADDR tsd_start_addr;
129
130     /* Following this are longwords holding:
131
132      * ?, ?, ?, ptr to -1, ptr to-1, ptr to lib name (leaf name),
133      * ptr to __data_start, ptr to __data_end
134      */
135
136
137   };
138
139 /* A structure to keep track of all the known shared objects.  */
140 struct so_list
141   {
142     struct som_solib_mapped_entry som_solib;
143     struct objfile *objfile;
144     bfd *abfd;
145     struct section_table *sections;
146     struct section_table *sections_end;
147 /* elz: added this field to store the address in target space (in the
148    library) of the library descriptor (handle) which we read into
149    som_solib_mapped_entry structure */
150     CORE_ADDR solib_addr;
151     struct so_list *next;
152
153   };
154
155 static struct so_list *so_list_head;
156
157
158 /* This is the cumulative size in bytes of the symbol tables of all
159    shared objects on the so_list_head list.  (When we say size, here
160    we mean of the information before it is brought into memory and
161    potentially expanded by GDB.)  When adding a new shlib, this value
162    is compared against the threshold size, held by auto_solib_add
163    (in megabytes).  If adding symbols for the new shlib would cause
164    the total size to exceed the threshold, then the new shlib's symbols
165    are not loaded.
166  */
167 static LONGEST som_solib_total_st_size;
168
169 /* When the threshold is reached for any shlib, we refuse to add
170    symbols for subsequent shlibs, even if those shlibs' symbols would
171    be small enough to fit under the threshold.  (Although this may
172    result in one, early large shlib preventing the loading of later,
173    smalller shlibs' symbols, it allows us to issue one informational
174    message.  The alternative, to issue a message for each shlib whose
175    symbols aren't loaded, could be a big annoyance where the threshold
176    is exceeded due to a very large number of shlibs.)
177  */
178 static int som_solib_st_size_threshold_exceeded;
179
180 /* These addresses should be filled in by som_solib_create_inferior_hook.
181    They are also used elsewhere in this module.
182  */
183 typedef struct
184   {
185     CORE_ADDR address;
186     struct unwind_table_entry *unwind;
187   }
188 addr_and_unwind_t;
189
190 /* When adding fields, be sure to clear them in _initialize_som_solib. */
191 static struct
192   {
193     boolean is_valid;
194     addr_and_unwind_t hook;
195     addr_and_unwind_t hook_stub;
196     addr_and_unwind_t load;
197     addr_and_unwind_t load_stub;
198     addr_and_unwind_t unload;
199     addr_and_unwind_t unload2;
200     addr_and_unwind_t unload_stub;
201   }
202 dld_cache;
203
204
205
206 static void som_sharedlibrary_info_command PARAMS ((char *, int));
207
208 static void som_solib_sharedlibrary_command PARAMS ((char *, int));
209
210 static LONGEST
211 som_solib_sizeof_symbol_table (filename)
212      char *filename;
213 {
214   bfd *abfd;
215   int desc;
216   char *absolute_name;
217   LONGEST st_size = (LONGEST) 0;
218   asection *sect;
219
220   /* We believe that filename was handed to us by the dynamic linker, and
221      is therefore always an absolute path.
222    */
223   desc = openp (getenv ("PATH"), 1, filename, O_RDONLY | O_BINARY, 0, &absolute_name);
224   if (desc < 0)
225     {
226       perror_with_name (filename);
227     }
228   filename = absolute_name;
229
230   abfd = bfd_fdopenr (filename, gnutarget, desc);
231   if (!abfd)
232     {
233       close (desc);
234       make_cleanup (free, filename);
235       error ("\"%s\": can't open to read symbols: %s.", filename,
236              bfd_errmsg (bfd_get_error ()));
237     }
238
239   if (!bfd_check_format (abfd, bfd_object))     /* Reads in section info */
240     {
241       bfd_close (abfd);         /* This also closes desc */
242       make_cleanup (free, filename);
243       error ("\"%s\": can't read symbols: %s.", filename,
244              bfd_errmsg (bfd_get_error ()));
245     }
246
247   /* Sum the sizes of the various sections that compose debug info. */
248
249   /* This contains non-DOC information. */
250   sect = bfd_get_section_by_name (abfd, "$DEBUG$");
251   if (sect)
252     st_size += (LONGEST) bfd_section_size (abfd, sect);
253
254   /* This contains DOC information. */
255   sect = bfd_get_section_by_name (abfd, "$PINFO$");
256   if (sect)
257     st_size += (LONGEST) bfd_section_size (abfd, sect);
258
259   bfd_close (abfd);             /* This also closes desc */
260   free (filename);
261
262   /* Unfortunately, just summing the sizes of various debug info
263      sections isn't a very accurate measurement of how much heap
264      space the debugger will need to hold them.  It also doesn't
265      account for space needed by linker (aka "minimal") symbols.
266
267      Anecdotal evidence suggests that just summing the sizes of
268      debug-info-related sections understates the heap space needed
269      to represent it internally by about an order of magnitude.
270
271      Since it's not exactly brain surgery we're doing here, rather
272      than attempt to more accurately measure the size of a shlib's
273      symbol table in GDB's heap, we'll just apply a 10x fudge-
274      factor to the debug info sections' size-sum.  No, this doesn't
275      account for minimal symbols in non-debuggable shlibs.  But it
276      all roughly washes out in the end.
277    */
278   return st_size * (LONGEST) 10;
279 }
280
281
282 static void
283 som_solib_add_solib_objfile (so, name, from_tty, text_addr)
284      struct so_list *so;
285      char *name;
286      int from_tty;
287      CORE_ADDR text_addr;
288 {
289   obj_private_data_t *obj_private;
290   struct section_addr_info section_addrs;
291
292   memset (&section_addrs, 0, sizeof (section_addrs));
293   section_addrs.other[0].name = ".text";
294   section_addrs.other[0].addr = text_addr;
295   so->objfile = symbol_file_add (name, from_tty, &section_addrs, 0, OBJF_SHARED);
296   so->abfd = so->objfile->obfd;
297
298   /* Mark this as a shared library and save private data.
299    */
300   so->objfile->flags |= OBJF_SHARED;
301
302   if (so->objfile->obj_private == NULL)
303     {
304       obj_private = (obj_private_data_t *)
305         obstack_alloc (&so->objfile->psymbol_obstack,
306                        sizeof (obj_private_data_t));
307       obj_private->unwind_info = NULL;
308       obj_private->so_info = NULL;
309       so->objfile->obj_private = (PTR) obj_private;
310     }
311
312   obj_private = (obj_private_data_t *) so->objfile->obj_private;
313   obj_private->so_info = so;
314
315   if (!bfd_check_format (so->abfd, bfd_object))
316     {
317       error ("\"%s\": not in executable format: %s.",
318              name, bfd_errmsg (bfd_get_error ()));
319     }
320 }
321
322
323 static void
324 som_solib_load_symbols (so, name, from_tty, text_addr, target)
325      struct so_list *so;
326      char *name;
327      int from_tty;
328      CORE_ADDR text_addr;
329      struct target_ops *target;
330 {
331   struct section_table *p;
332   int status;
333   char buf[4];
334   CORE_ADDR presumed_data_start;
335
336 #ifdef SOLIB_DEBUG
337   printf ("--Adding symbols for shared library \"%s\"\n", name);
338 #endif
339
340   som_solib_add_solib_objfile (so, name, from_tty, text_addr);
341
342   /* Now we need to build a section table for this library since
343      we might be debugging a core file from a dynamically linked
344      executable in which the libraries were not privately mapped.  */
345   if (build_section_table (so->abfd,
346                            &so->sections,
347                            &so->sections_end))
348     {
349       error ("Unable to build section table for shared library\n.");
350       return;
351     }
352
353   /* Relocate all the sections based on where they got loaded.  */
354   for (p = so->sections; p < so->sections_end; p++)
355     {
356       if (p->the_bfd_section->flags & SEC_CODE)
357         {
358           p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT);
359           p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_TEXT);
360         }
361       else if (p->the_bfd_section->flags & SEC_DATA)
362         {
363           p->addr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA);
364           p->endaddr += ANOFFSET (so->objfile->section_offsets, SECT_OFF_DATA);
365         }
366     }
367
368   /* Now see if we need to map in the text and data for this shared
369      library (for example debugging a core file which does not use
370      private shared libraries.). 
371
372      Carefully peek at the first text address in the library.  If the
373      read succeeds, then the libraries were privately mapped and were
374      included in the core dump file.
375
376      If the peek failed, then the libraries were not privately mapped
377      and are not in the core file, we'll have to read them in ourselves.  */
378   status = target_read_memory (text_addr, buf, 4);
379   if (status != 0)
380     {
381       int old, new;
382
383       new = so->sections_end - so->sections;
384       
385       old = target_resize_to_sections (target, new);
386       
387       /* Copy over the old data before it gets clobbered.  */
388       memcpy ((char *) (target->to_sections + old),
389               so->sections,
390               ((sizeof (struct section_table)) * new));
391     }
392 }
393
394
395 /* Add symbols from shared libraries into the symtab list, unless the
396    size threshold (specified by auto_solib_add, in megabytes) would
397    be exceeded.  */
398
399 void
400 som_solib_add (arg_string, from_tty, target)
401      char *arg_string;
402      int from_tty;
403      struct target_ops *target;
404 {
405   struct minimal_symbol *msymbol;
406   struct so_list *so_list_tail;
407   CORE_ADDR addr;
408   asection *shlib_info;
409   int status;
410   unsigned int dld_flags;
411   char buf[4], *re_err;
412   int threshold_warning_given = 0;
413
414   /* First validate our arguments.  */
415   if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
416     {
417       error ("Invalid regexp: %s", re_err);
418     }
419
420   /* If we're debugging a core file, or have attached to a running
421      process, then som_solib_create_inferior_hook will not have been
422      called.
423
424      We need to first determine if we're dealing with a dynamically
425      linked executable.  If not, then return without an error or warning.
426
427      We also need to examine __dld_flags to determine if the shared library
428      list is valid and to determine if the libraries have been privately
429      mapped.  */
430   if (symfile_objfile == NULL)
431     return;
432
433   /* First see if the objfile was dynamically linked.  */
434   shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
435   if (!shlib_info)
436     return;
437
438   /* It's got a $SHLIB_INFO$ section, make sure it's not empty.  */
439   if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
440     return;
441
442   msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
443   if (msymbol == NULL)
444     {
445       error ("Unable to find __dld_flags symbol in object file.\n");
446       return;
447     }
448
449   addr = SYMBOL_VALUE_ADDRESS (msymbol);
450   /* Read the current contents.  */
451   status = target_read_memory (addr, buf, 4);
452   if (status != 0)
453     {
454       error ("Unable to read __dld_flags\n");
455       return;
456     }
457   dld_flags = extract_unsigned_integer (buf, 4);
458
459   /* __dld_list may not be valid.  If not, then we punt, warning the user if
460      we were called as a result of the add-symfile command.
461    */
462   if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
463     {
464       if (from_tty)
465         error ("__dld_list is not valid according to __dld_flags.\n");
466       return;
467     }
468
469   /* If the libraries were not mapped private, warn the user.  */
470   if ((dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
471     warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
472
473   msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
474   if (!msymbol)
475     {
476       /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
477          but the data is still available if you know where to look.  */
478       msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
479       if (!msymbol)
480         {
481           error ("Unable to find dynamic library list.\n");
482           return;
483         }
484       addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
485     }
486   else
487     addr = SYMBOL_VALUE_ADDRESS (msymbol);
488
489   status = target_read_memory (addr, buf, 4);
490   if (status != 0)
491     {
492       error ("Unable to find dynamic library list.\n");
493       return;
494     }
495
496   addr = extract_unsigned_integer (buf, 4);
497
498   /* If addr is zero, then we're using an old dynamic loader which
499      doesn't maintain __dld_list.  We'll have to use a completely
500      different approach to get shared library information.  */
501   if (addr == 0)
502     goto old_dld;
503
504   /* Using the information in __dld_list is the preferred method
505      to get at shared library information.  It doesn't depend on
506      any functions in /opt/langtools/lib/end.o and has a chance of working
507      with hpux10 when it is released.  */
508   status = target_read_memory (addr, buf, 4);
509   if (status != 0)
510     {
511       error ("Unable to find dynamic library list.\n");
512       return;
513     }
514
515   /* addr now holds the address of the first entry in the dynamic
516      library list.  */
517   addr = extract_unsigned_integer (buf, 4);
518
519   /* Now that we have a pointer to the dynamic library list, walk
520      through it and add the symbols for each library.  */
521
522   so_list_tail = so_list_head;
523   /* Find the end of the list of shared objects.  */
524   while (so_list_tail && so_list_tail->next)
525     so_list_tail = so_list_tail->next;
526
527 #ifdef SOLIB_DEBUG
528   printf ("--About to read shared library list data\n");
529 #endif
530
531   /* "addr" will always point to the base of the
532    * current data entry describing the current
533    * shared library.
534    */
535   while (1)
536     {
537       CORE_ADDR name_addr, text_addr;
538       unsigned int name_len;
539       char *name;
540       struct so_list *new_so;
541       struct so_list *so_list = so_list_head;
542       struct stat statbuf;
543       LONGEST st_size;
544       int is_main_program;
545
546       if (addr == 0)
547         break;
548
549       /* Get a pointer to the name of this library.  */
550       status = target_read_memory (addr, buf, 4);
551       if (status != 0)
552         goto err;
553
554       name_addr = extract_unsigned_integer (buf, 4);
555       name_len = 0;
556       while (1)
557         {
558           target_read_memory (name_addr + name_len, buf, 1);
559           if (status != 0)
560             goto err;
561
562           name_len++;
563           if (*buf == '\0')
564             break;
565         }
566       name = alloca (name_len);
567       status = target_read_memory (name_addr, name, name_len);
568       if (status != 0)
569         goto err;
570
571       /* See if we've already loaded something with this name.  */
572       while (so_list)
573         {
574           if (!strcmp (so_list->som_solib.name, name))
575             break;
576           so_list = so_list->next;
577         }
578
579       /* See if the file exists.  If not, give a warning, but don't
580          die.  */
581       status = stat (name, &statbuf);
582       if (status == -1)
583         {
584           warning ("Can't find file %s referenced in dld_list.", name);
585
586           status = target_read_memory (addr + 36, buf, 4);
587           if (status != 0)
588             goto err;
589
590           addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
591           continue;
592         }
593
594       /* If we've already loaded this one or it's the main program, skip it.  */
595       is_main_program = (strcmp (name, symfile_objfile->name) == 0);
596       if (so_list || is_main_program)
597         {
598           /* This is the "next" pointer in the strcuture.
599            */
600           status = target_read_memory (addr + 36, buf, 4);
601           if (status != 0)
602             goto err;
603
604           addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
605
606           /* Record the main program's symbol table size. */
607           if (is_main_program && !so_list)
608             {
609               st_size = som_solib_sizeof_symbol_table (name);
610               som_solib_total_st_size += st_size;
611             }
612
613           /* Was this a shlib that we noted but didn't load the symbols for?
614              If so, were we invoked this time from the command-line, via
615              a 'sharedlibrary' or 'add-symbol-file' command?  If yes to
616              both, we'd better load the symbols this time.
617            */
618           if (from_tty && so_list && !is_main_program && (so_list->objfile == NULL))
619             som_solib_load_symbols (so_list,
620                                     name,
621                                     from_tty,
622                                     so_list->som_solib.text_addr,
623                                     target);
624
625           continue;
626         }
627
628       name = obsavestring (name, name_len - 1,
629                            &symfile_objfile->symbol_obstack);
630
631       status = target_read_memory (addr + 8, buf, 4);
632       if (status != 0)
633         goto err;
634
635       text_addr = extract_unsigned_integer (buf, 4);
636
637       new_so = (struct so_list *) xmalloc (sizeof (struct so_list));
638       memset ((char *) new_so, 0, sizeof (struct so_list));
639       if (so_list_head == NULL)
640         {
641           so_list_head = new_so;
642           so_list_tail = new_so;
643         }
644       else
645         {
646           so_list_tail->next = new_so;
647           so_list_tail = new_so;
648         }
649
650       /* Fill in all the entries in GDB's shared library list.
651        */
652
653       new_so->solib_addr = addr;
654       new_so->som_solib.name = name;
655       status = target_read_memory (addr + 4, buf, 4);
656       if (status != 0)
657         goto err;
658
659       new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
660       new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
661       /* Following is "high water mark", highest version number
662        * seen, rather than plain version number.
663        */
664       new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
665       new_so->som_solib.text_addr = text_addr;
666
667       /* Q: What about longword at "addr + 8"?
668        * A: It's read above, out of order, into "text_addr".
669        */
670
671       status = target_read_memory (addr + 12, buf, 4);
672       if (status != 0)
673         goto err;
674
675       new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
676
677       status = target_read_memory (addr + 16, buf, 4);
678       if (status != 0)
679         goto err;
680
681       new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
682
683       status = target_read_memory (addr + 20, buf, 4);
684       if (status != 0)
685         goto err;
686
687       new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
688
689       status = target_read_memory (addr + 24, buf, 4);
690       if (status != 0)
691         goto err;
692
693       new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
694
695       status = target_read_memory (addr + 28, buf, 4);
696       if (status != 0)
697         goto err;
698
699       new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
700
701       status = target_read_memory (addr + 32, buf, 4);
702       if (status != 0)
703         goto err;
704
705       new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
706
707       status = target_read_memory (addr + 36, buf, 4);
708       if (status != 0)
709         goto err;
710
711       new_so->som_solib.next = (void *) extract_unsigned_integer (buf, 4);
712
713       /* Note that we don't re-set "addr" to the next pointer
714        * until after we've read the trailing data.
715        */
716
717       status = target_read_memory (addr + 40, buf, 4);
718       new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
719       if (status != 0)
720         goto err;
721
722       /* Now indirect via that value!
723        */
724       status = target_read_memory (new_so->som_solib.tsd_start_addr, buf, 4);
725       new_so->som_solib.tsd_start_addr = extract_unsigned_integer (buf, 4);
726       if (status != 0)
727         goto err;
728 #ifdef SOLIB_DEBUG
729       printf ("\n+ library \"%s\" is described at 0x%x\n", name, addr);
730       printf ("  'version' is %d\n", new_so->som_solib.struct_version);
731       printf ("  'bind_mode' is %d\n", new_so->som_solib.bind_mode);
732       printf ("  'library_version' is %d\n", new_so->som_solib.library_version);
733       printf ("  'text_addr' is 0x%x\n", new_so->som_solib.text_addr);
734       printf ("  'text_link_addr' is 0x%x\n", new_so->som_solib.text_link_addr);
735       printf ("  'text_end' is 0x%x\n", new_so->som_solib.text_end);
736       printf ("  'data_start' is 0x%x\n", new_so->som_solib.data_start);
737       printf ("  'bss_start' is 0x%x\n", new_so->som_solib.bss_start);
738       printf ("  'data_end' is 0x%x\n", new_so->som_solib.data_end);
739       printf ("  'got_value' is %x\n", new_so->som_solib.got_value);
740       printf ("  'next' is 0x%x\n", new_so->som_solib.next);
741       printf ("  'tsd_start_addr' is 0x%x\n", new_so->som_solib.tsd_start_addr);
742 #endif
743
744       /* Go on to the next shared library descriptor.
745        */
746       addr = (CORE_ADDR) new_so->som_solib.next;
747
748
749
750       /* At this point, we have essentially hooked the shlib into the
751          "info share" command.  However, we haven't yet loaded its
752          symbol table.  We must now decide whether we ought to, i.e.,
753          whether doing so would exceed the symbol table size threshold.
754
755          If the threshold has just now been exceeded, then we'll issue
756          a warning message (which explains how to load symbols manually,
757          if the user so desires).
758
759          If the threshold has just now or previously been exceeded,
760          we'll just add the shlib to the list of object files, but won't
761          actually load its symbols.  (This is more useful than it might
762          sound, for it allows us to e.g., still load and use the shlibs'
763          unwind information for stack tracebacks.)
764        */
765
766       /* Note that we DON'T want to preclude the user from using the
767          add-symbol-file command!  Thus, we only worry about the threshold
768          when we're invoked for other reasons.
769        */
770       st_size = som_solib_sizeof_symbol_table (name);
771       som_solib_st_size_threshold_exceeded =
772         !from_tty &&
773         ((st_size + som_solib_total_st_size) > (auto_solib_add * (LONGEST) 1000000));
774
775       if (som_solib_st_size_threshold_exceeded)
776         {
777           if (!threshold_warning_given)
778             warning ("Symbols for some libraries have not been loaded, because\ndoing so would exceed the size threshold specified by auto-solib-add.\nTo manually load symbols, use the 'sharedlibrary' command.\nTo raise the threshold, set auto-solib-add to a larger value and rerun\nthe program.\n");
779           threshold_warning_given = 1;
780
781           /* We'll still make note of this shlib, even if we don't
782              read its symbols.  This allows us to use its unwind
783              information well enough to know how to e.g., correctly
784              do a traceback from a PC within the shlib, even if we
785              can't symbolize those PCs...
786            */
787           som_solib_add_solib_objfile (new_so, name, from_tty, text_addr);
788           continue;
789         }
790
791       som_solib_total_st_size += st_size;
792
793       /* This fills in new_so->objfile, among others. */
794       som_solib_load_symbols (new_so, name, from_tty, text_addr, target);
795     }
796
797 #ifdef SOLIB_DEBUG
798   printf ("--Done reading shared library data\n");
799 #endif
800
801   /* Getting new symbols may change our opinion about what is
802      frameless.  */
803   reinit_frame_cache ();
804   return;
805
806 old_dld:
807   error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
808   return;
809
810 err:
811   error ("Error while reading dynamic library list.\n");
812   return;
813 }
814
815
816 /* This hook gets called just before the first instruction in the
817    inferior process is executed.
818
819    This is our opportunity to set magic flags in the inferior so
820    that GDB can be notified when a shared library is mapped in and
821    to tell the dynamic linker that a private copy of the library is
822    needed (so GDB can set breakpoints in the library).
823
824    __dld_flags is the location of the magic flags; as of this implementation
825    there are 3 flags of interest:
826
827    bit 0 when set indicates that private copies of the libraries are needed
828    bit 1 when set indicates that the callback hook routine is valid
829    bit 2 when set indicates that the dynamic linker should maintain the
830    __dld_list structure when loading/unloading libraries.
831
832    Note that shared libraries are not mapped in at this time, so we have
833    run the inferior until the libraries are mapped in.  Typically this
834    means running until the "_start" is called.  */
835
836 void
837 som_solib_create_inferior_hook ()
838 {
839   struct minimal_symbol *msymbol;
840   unsigned int dld_flags, status, have_endo;
841   asection *shlib_info;
842   char buf[4];
843   struct objfile *objfile;
844   CORE_ADDR anaddr;
845
846   /* First, remove all the solib event breakpoints.  Their addresses
847      may have changed since the last time we ran the program.  */
848   remove_solib_event_breakpoints ();
849
850   if (symfile_objfile == NULL)
851     return;
852
853   /* First see if the objfile was dynamically linked.  */
854   shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
855   if (!shlib_info)
856     return;
857
858   /* It's got a $SHLIB_INFO$ section, make sure it's not empty.  */
859   if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
860     return;
861
862   have_endo = 0;
863   /* Slam the pid of the process into __d_pid; failing is only a warning!  */
864   msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
865   if (msymbol == NULL)
866     {
867       warning ("Unable to find __d_pid symbol in object file.");
868       warning ("Suggest linking with /opt/langtools/lib/end.o.");
869       warning ("GDB will be unable to track shl_load/shl_unload calls");
870       goto keep_going;
871     }
872
873   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
874   store_unsigned_integer (buf, 4, inferior_pid);
875   status = target_write_memory (anaddr, buf, 4);
876   if (status != 0)
877     {
878       warning ("Unable to write __d_pid");
879       warning ("Suggest linking with /opt/langtools/lib/end.o.");
880       warning ("GDB will be unable to track shl_load/shl_unload calls");
881       goto keep_going;
882     }
883
884   /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
885      This will force the dynamic linker to call __d_trap when significant
886      events occur.
887
888      Note that the above is the pre-HP-UX 9.0 behaviour.  At 9.0 and above,
889      the dld provides an export stub named "__d_trap" as well as the
890      function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
891      We'll look first for the old flavor and then the new.
892    */
893   msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
894   if (msymbol == NULL)
895     msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
896   if (msymbol == NULL)
897     {
898       warning ("Unable to find _DLD_HOOK symbol in object file.");
899       warning ("Suggest linking with /opt/langtools/lib/end.o.");
900       warning ("GDB will be unable to track shl_load/shl_unload calls");
901       goto keep_going;
902     }
903   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
904   dld_cache.hook.address = anaddr;
905
906   /* Grrr, this might not be an export symbol!  We have to find the
907      export stub.  */
908   ALL_OBJFILES (objfile)
909   {
910     struct unwind_table_entry *u;
911     struct minimal_symbol *msymbol2;
912
913     /* What a crock.  */
914     msymbol2 = lookup_minimal_symbol_solib_trampoline (SYMBOL_NAME (msymbol),
915                                                        NULL, objfile);
916     /* Found a symbol with the right name.  */
917     if (msymbol2)
918       {
919         struct unwind_table_entry *u;
920         /* It must be a shared library trampoline.  */
921         if (SYMBOL_TYPE (msymbol2) != mst_solib_trampoline)
922           continue;
923
924         /* It must also be an export stub.  */
925         u = find_unwind_entry (SYMBOL_VALUE (msymbol2));
926         if (!u || u->stub_unwind.stub_type != EXPORT)
927           continue;
928
929         /* OK.  Looks like the correct import stub.  */
930         anaddr = SYMBOL_VALUE (msymbol2);
931         dld_cache.hook_stub.address = anaddr;
932       }
933   }
934   store_unsigned_integer (buf, 4, anaddr);
935
936   msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
937   if (msymbol == NULL)
938     {
939       warning ("Unable to find __dld_hook symbol in object file.");
940       warning ("Suggest linking with /opt/langtools/lib/end.o.");
941       warning ("GDB will be unable to track shl_load/shl_unload calls");
942       goto keep_going;
943     }
944   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
945   status = target_write_memory (anaddr, buf, 4);
946
947   /* Now set a shlib_event breakpoint at __d_trap so we can track
948      significant shared library events.  */
949   msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
950   if (msymbol == NULL)
951     {
952       warning ("Unable to find __dld_d_trap symbol in object file.");
953       warning ("Suggest linking with /opt/langtools/lib/end.o.");
954       warning ("GDB will be unable to track shl_load/shl_unload calls");
955       goto keep_going;
956     }
957   create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
958
959   /* We have all the support usually found in end.o, so we can track
960      shl_load and shl_unload calls.  */
961   have_endo = 1;
962
963 keep_going:
964
965   /* Get the address of __dld_flags, if no such symbol exists, then we can
966      not debug the shared code.  */
967   msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
968   if (msymbol == NULL)
969     {
970       error ("Unable to find __dld_flags symbol in object file.\n");
971     }
972
973   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
974
975   /* Read the current contents.  */
976   status = target_read_memory (anaddr, buf, 4);
977   if (status != 0)
978     {
979       error ("Unable to read __dld_flags\n");
980     }
981   dld_flags = extract_unsigned_integer (buf, 4);
982
983   /* Turn on the flags we care about.  */
984   dld_flags |= DLD_FLAGS_MAPPRIVATE;
985   if (have_endo)
986     dld_flags |= DLD_FLAGS_HOOKVALID;
987   store_unsigned_integer (buf, 4, dld_flags);
988   status = target_write_memory (anaddr, buf, 4);
989   if (status != 0)
990     {
991       error ("Unable to write __dld_flags\n");
992     }
993
994   /* Now find the address of _start and set a breakpoint there. 
995      We still need this code for two reasons:
996
997      * Not all sites have /opt/langtools/lib/end.o, so it's not always
998      possible to track the dynamic linker's events.
999
1000      * At this time no events are triggered for shared libraries
1001      loaded at startup time (what a crock).  */
1002
1003   msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1004   if (msymbol == NULL)
1005     {
1006       error ("Unable to find _start symbol in object file.\n");
1007     }
1008
1009   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
1010
1011   /* Make the breakpoint at "_start" a shared library event breakpoint.  */
1012   create_solib_event_breakpoint (anaddr);
1013
1014   /* Wipe out all knowledge of old shared libraries since their
1015      mapping can change from one exec to another!  */
1016   while (so_list_head)
1017     {
1018       struct so_list *temp;
1019
1020       temp = so_list_head;
1021       free (so_list_head);
1022       so_list_head = temp->next;
1023     }
1024   clear_symtab_users ();
1025 }
1026
1027
1028 static void
1029 reset_inferior_pid (saved_inferior_pid)
1030      int saved_inferior_pid;
1031 {
1032   inferior_pid = saved_inferior_pid;
1033 }
1034
1035
1036 /* This operation removes the "hook" between GDB and the dynamic linker,
1037    which causes the dld to notify GDB of shared library events.
1038
1039    After this operation completes, the dld will no longer notify GDB of
1040    shared library events.  To resume notifications, GDB must call
1041    som_solib_create_inferior_hook.
1042
1043    This operation does not remove any knowledge of shared libraries which
1044    GDB may already have been notified of.
1045  */
1046 void
1047 som_solib_remove_inferior_hook (pid)
1048      int pid;
1049 {
1050   CORE_ADDR addr;
1051   struct minimal_symbol *msymbol;
1052   int status;
1053   char dld_flags_buffer[TARGET_INT_BIT / TARGET_CHAR_BIT];
1054   unsigned int dld_flags_value;
1055   int saved_inferior_pid = inferior_pid;
1056   struct cleanup *old_cleanups = make_cleanup (reset_inferior_pid, saved_inferior_pid);
1057
1058   /* Ensure that we're really operating on the specified process. */
1059   inferior_pid = pid;
1060
1061   /* We won't bother to remove the solib breakpoints from this process.
1062
1063      In fact, on PA64 the breakpoint is hard-coded into the dld callback,
1064      and thus we're not supposed to remove it.
1065
1066      Rather, we'll merely clear the dld_flags bit that enables callbacks.
1067    */
1068   msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
1069
1070   addr = SYMBOL_VALUE_ADDRESS (msymbol);
1071   status = target_read_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT);
1072
1073   dld_flags_value = extract_unsigned_integer (dld_flags_buffer,
1074                                               sizeof (dld_flags_value));
1075
1076   dld_flags_value &= ~DLD_FLAGS_HOOKVALID;
1077   store_unsigned_integer (dld_flags_buffer,
1078                           sizeof (dld_flags_value),
1079                           dld_flags_value);
1080   status = target_write_memory (addr, dld_flags_buffer, TARGET_INT_BIT / TARGET_CHAR_BIT);
1081
1082   do_cleanups (old_cleanups);
1083 }
1084
1085
1086 /* This function creates a breakpoint on the dynamic linker hook, which
1087    is called when e.g., a shl_load or shl_unload call is made.  This
1088    breakpoint will only trigger when a shl_load call is made.
1089
1090    If filename is NULL, then loads of any dll will be caught.  Else,
1091    only loads of the file whose pathname is the string contained by
1092    filename will be caught.
1093
1094    Undefined behaviour is guaranteed if this function is called before
1095    som_solib_create_inferior_hook.
1096  */
1097 void
1098 som_solib_create_catch_load_hook (pid, tempflag, filename, cond_string)
1099      int pid;
1100      int tempflag;
1101      char *filename;
1102      char *cond_string;
1103 {
1104   create_solib_load_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1105 }
1106
1107 /* This function creates a breakpoint on the dynamic linker hook, which
1108    is called when e.g., a shl_load or shl_unload call is made.  This
1109    breakpoint will only trigger when a shl_unload call is made.
1110
1111    If filename is NULL, then unloads of any dll will be caught.  Else,
1112    only unloads of the file whose pathname is the string contained by
1113    filename will be caught.
1114
1115    Undefined behaviour is guaranteed if this function is called before
1116    som_solib_create_inferior_hook.
1117  */
1118 void
1119 som_solib_create_catch_unload_hook (pid, tempflag, filename, cond_string)
1120      int pid;
1121      int tempflag;
1122      char *filename;
1123      char *cond_string;
1124 {
1125   create_solib_unload_event_breakpoint ("__d_trap", tempflag, filename, cond_string);
1126 }
1127
1128 int
1129 som_solib_have_load_event (pid)
1130      int pid;
1131 {
1132   CORE_ADDR event_kind;
1133
1134   event_kind = read_register (ARG0_REGNUM);
1135   return (event_kind == SHL_LOAD);
1136 }
1137
1138 int
1139 som_solib_have_unload_event (pid)
1140      int pid;
1141 {
1142   CORE_ADDR event_kind;
1143
1144   event_kind = read_register (ARG0_REGNUM);
1145   return (event_kind == SHL_UNLOAD);
1146 }
1147
1148 static char *
1149 som_solib_library_pathname (pid)
1150      int pid;
1151 {
1152   CORE_ADDR dll_handle_address;
1153   CORE_ADDR dll_pathname_address;
1154   struct som_solib_mapped_entry dll_descriptor;
1155   char *p;
1156   static char dll_pathname[1024];
1157
1158   /* Read the descriptor of this newly-loaded library. */
1159   dll_handle_address = read_register (ARG1_REGNUM);
1160   read_memory (dll_handle_address, (char *) &dll_descriptor, sizeof (dll_descriptor));
1161
1162   /* We can find a pointer to the dll's pathname within the descriptor. */
1163   dll_pathname_address = (CORE_ADDR) dll_descriptor.name;
1164
1165   /* Read the pathname, one byte at a time. */
1166   p = dll_pathname;
1167   for (;;)
1168     {
1169       char b;
1170       read_memory (dll_pathname_address++, (char *) &b, 1);
1171       *p++ = b;
1172       if (b == '\0')
1173         break;
1174     }
1175
1176   return dll_pathname;
1177 }
1178
1179 char *
1180 som_solib_loaded_library_pathname (pid)
1181      int pid;
1182 {
1183   if (!som_solib_have_load_event (pid))
1184     error ("Must have a load event to use this query");
1185
1186   return som_solib_library_pathname (pid);
1187 }
1188
1189 char *
1190 som_solib_unloaded_library_pathname (pid)
1191      int pid;
1192 {
1193   if (!som_solib_have_unload_event (pid))
1194     error ("Must have an unload event to use this query");
1195
1196   return som_solib_library_pathname (pid);
1197 }
1198
1199 static void
1200 som_solib_desire_dynamic_linker_symbols ()
1201 {
1202   struct objfile *objfile;
1203   struct unwind_table_entry *u;
1204   struct minimal_symbol *dld_msymbol;
1205
1206   /* Do we already know the value of these symbols?  If so, then
1207      we've no work to do.
1208
1209      (If you add clauses to this test, be sure to likewise update the
1210      test within the loop.)
1211    */
1212   if (dld_cache.is_valid)
1213     return;
1214
1215   ALL_OBJFILES (objfile)
1216   {
1217     dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
1218     if (dld_msymbol != NULL)
1219       {
1220         dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
1221         dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
1222       }
1223
1224     dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
1225                                                           NULL,
1226                                                           objfile);
1227     if (dld_msymbol != NULL)
1228       {
1229         if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1230           {
1231             u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1232             if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1233               {
1234                 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
1235                 dld_cache.load_stub.unwind = u;
1236               }
1237           }
1238       }
1239
1240     dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
1241     if (dld_msymbol != NULL)
1242       {
1243         dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
1244         dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
1245
1246         /* ??rehrauer: I'm not sure exactly what this is, but it appears
1247            that on some HPUX 10.x versions, there's two unwind regions to
1248            cover the body of "shl_unload", the second being 4 bytes past
1249            the end of the first.  This is a large hack to handle that
1250            case, but since I don't seem to have any legitimate way to
1251            look for this thing via the symbol table...
1252          */
1253         if (dld_cache.unload.unwind != NULL)
1254           {
1255             u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
1256             if (u != NULL)
1257               {
1258                 dld_cache.unload2.address = u->region_start;
1259                 dld_cache.unload2.unwind = u;
1260               }
1261           }
1262       }
1263
1264     dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
1265                                                           NULL,
1266                                                           objfile);
1267     if (dld_msymbol != NULL)
1268       {
1269         if (SYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
1270           {
1271             u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
1272             if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
1273               {
1274                 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
1275                 dld_cache.unload_stub.unwind = u;
1276               }
1277           }
1278       }
1279
1280     /* Did we find everything we were looking for?  If so, stop. */
1281     if ((dld_cache.load.address != NULL) && (dld_cache.load_stub.address != NULL)
1282         && (dld_cache.unload.address != NULL) && (dld_cache.unload_stub.address != NULL))
1283       {
1284         dld_cache.is_valid = 1;
1285         break;
1286       }
1287   }
1288
1289   dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
1290   dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
1291
1292   /* We're prepared not to find some of these symbols, which is why
1293      this function is a "desire" operation, and not a "require".
1294    */
1295 }
1296
1297 int
1298 som_solib_in_dynamic_linker (pid, pc)
1299      int pid;
1300      CORE_ADDR pc;
1301 {
1302   struct unwind_table_entry *u_pc;
1303
1304   /* Are we in the dld itself?
1305
1306      ??rehrauer: Large hack -- We'll assume that any address in a
1307      shared text region is the dld's text.  This would obviously
1308      fall down if the user attached to a process, whose shlibs
1309      weren't mapped to a (writeable) private region.  However, in
1310      that case the debugger probably isn't able to set the fundamental
1311      breakpoint in the dld callback anyways, so this hack should be
1312      safe.
1313    */
1314   if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
1315     return 1;
1316
1317   /* Cache the address of some symbols that are part of the dynamic
1318      linker, if not already known.
1319    */
1320   som_solib_desire_dynamic_linker_symbols ();
1321
1322   /* Are we in the dld callback?  Or its export stub? */
1323   u_pc = find_unwind_entry (pc);
1324   if (u_pc == NULL)
1325     return 0;
1326
1327   if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
1328     return 1;
1329
1330   /* Or the interface of the dld (i.e., "shl_load" or friends)? */
1331   if ((u_pc == dld_cache.load.unwind)
1332       || (u_pc == dld_cache.unload.unwind)
1333       || (u_pc == dld_cache.unload2.unwind)
1334       || (u_pc == dld_cache.load_stub.unwind)
1335       || (u_pc == dld_cache.unload_stub.unwind))
1336     return 1;
1337
1338   /* Apparently this address isn't part of the dld's text. */
1339   return 0;
1340 }
1341
1342
1343 /* Return the GOT value for the shared library in which ADDR belongs.  If
1344    ADDR isn't in any known shared library, return zero.  */
1345
1346 CORE_ADDR
1347 som_solib_get_got_by_pc (addr)
1348      CORE_ADDR addr;
1349 {
1350   struct so_list *so_list = so_list_head;
1351   CORE_ADDR got_value = 0;
1352
1353   while (so_list)
1354     {
1355       if (so_list->som_solib.text_addr <= addr
1356           && so_list->som_solib.text_end > addr)
1357         {
1358           got_value = so_list->som_solib.got_value;
1359           break;
1360         }
1361       so_list = so_list->next;
1362     }
1363   return got_value;
1364 }
1365
1366 /*  elz:
1367    Return the address of the handle of the shared library
1368    in which ADDR belongs.  If
1369    ADDR isn't in any known shared library, return zero.  */
1370 /* this function is used in hppa_fix_call_dummy in hppa-tdep.c */
1371
1372 CORE_ADDR
1373 som_solib_get_solib_by_pc (addr)
1374      CORE_ADDR addr;
1375 {
1376   struct so_list *so_list = so_list_head;
1377
1378   while (so_list)
1379     {
1380       if (so_list->som_solib.text_addr <= addr
1381           && so_list->som_solib.text_end > addr)
1382         {
1383           break;
1384         }
1385       so_list = so_list->next;
1386     }
1387   if (so_list)
1388     return so_list->solib_addr;
1389   else
1390     return 0;
1391 }
1392
1393
1394 int
1395 som_solib_section_offsets (objfile, offsets)
1396      struct objfile *objfile;
1397      struct section_offsets *offsets;
1398 {
1399   struct so_list *so_list = so_list_head;
1400
1401   while (so_list)
1402     {
1403       /* Oh what a pain!  We need the offsets before so_list->objfile
1404          is valid.  The BFDs will never match.  Make a best guess.  */
1405       if (strstr (objfile->name, so_list->som_solib.name))
1406         {
1407           asection *private_section;
1408
1409           /* The text offset is easy.  */
1410           ANOFFSET (offsets, SECT_OFF_TEXT)
1411             = (so_list->som_solib.text_addr
1412                - so_list->som_solib.text_link_addr);
1413           ANOFFSET (offsets, SECT_OFF_RODATA)
1414             = ANOFFSET (offsets, SECT_OFF_TEXT);
1415
1416           /* We should look at presumed_dp in the SOM header, but
1417              that's not easily available.  This should be OK though.  */
1418           private_section = bfd_get_section_by_name (objfile->obfd,
1419                                                      "$PRIVATE$");
1420           if (!private_section)
1421             {
1422               warning ("Unable to find $PRIVATE$ in shared library!");
1423               ANOFFSET (offsets, SECT_OFF_DATA) = 0;
1424               ANOFFSET (offsets, SECT_OFF_BSS) = 0;
1425               return 1;
1426             }
1427           ANOFFSET (offsets, SECT_OFF_DATA)
1428             = (so_list->som_solib.data_start - private_section->vma);
1429           ANOFFSET (offsets, SECT_OFF_BSS)
1430             = ANOFFSET (offsets, SECT_OFF_DATA);
1431           return 1;
1432         }
1433       so_list = so_list->next;
1434     }
1435   return 0;
1436 }
1437
1438 /* Dump information about all the currently loaded shared libraries.  */
1439
1440 static void
1441 som_sharedlibrary_info_command (ignore, from_tty)
1442      char *ignore;
1443      int from_tty;
1444 {
1445   struct so_list *so_list = so_list_head;
1446
1447   if (exec_bfd == NULL)
1448     {
1449       printf_unfiltered ("No executable file.\n");
1450       return;
1451     }
1452
1453   if (so_list == NULL)
1454     {
1455       printf_unfiltered ("No shared libraries loaded at this time.\n");
1456       return;
1457     }
1458
1459   printf_unfiltered ("Shared Object Libraries\n");
1460   printf_unfiltered ("    %-12s%-12s%-12s%-12s%-12s%-12s\n",
1461          "  flags", "  tstart", "   tend", "  dstart", "   dend", "   dlt");
1462   while (so_list)
1463     {
1464       unsigned int flags;
1465
1466       flags = so_list->som_solib.struct_version << 24;
1467       flags |= so_list->som_solib.bind_mode << 16;
1468       flags |= so_list->som_solib.library_version;
1469       printf_unfiltered ("%s", so_list->som_solib.name);
1470       if (so_list->objfile == NULL)
1471         printf_unfiltered ("  (symbols not loaded)");
1472       printf_unfiltered ("\n");
1473       printf_unfiltered ("    %-12s", local_hex_string_custom (flags, "08l"));
1474       printf_unfiltered ("%-12s",
1475              local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
1476       printf_unfiltered ("%-12s",
1477               local_hex_string_custom (so_list->som_solib.text_end, "08l"));
1478       printf_unfiltered ("%-12s",
1479             local_hex_string_custom (so_list->som_solib.data_start, "08l"));
1480       printf_unfiltered ("%-12s",
1481               local_hex_string_custom (so_list->som_solib.data_end, "08l"));
1482       printf_unfiltered ("%-12s\n",
1483              local_hex_string_custom (so_list->som_solib.got_value, "08l"));
1484       so_list = so_list->next;
1485     }
1486 }
1487
1488 static void
1489 som_solib_sharedlibrary_command (args, from_tty)
1490      char *args;
1491      int from_tty;
1492 {
1493   dont_repeat ();
1494   som_solib_add (args, from_tty, (struct target_ops *) 0);
1495 }
1496
1497
1498
1499 char *
1500 som_solib_address (addr)
1501      CORE_ADDR addr;
1502 {
1503   struct so_list *so = so_list_head;
1504
1505   while (so)
1506     {
1507       /* Is this address within this shlib's text range?  If so,
1508          return the shlib's name.
1509        */
1510       if ((addr >= so->som_solib.text_addr) && (addr <= so->som_solib.text_end))
1511         return so->som_solib.name;
1512
1513       /* Nope, keep looking... */
1514       so = so->next;
1515     }
1516
1517   /* No, we couldn't prove that the address is within a shlib. */
1518   return NULL;
1519 }
1520
1521
1522 void
1523 som_solib_restart ()
1524 {
1525   struct so_list *sl = so_list_head;
1526
1527   /* Before the shlib info vanishes, use it to disable any breakpoints
1528      that may still be active in those shlibs.
1529    */
1530   disable_breakpoints_in_shlibs (0);
1531
1532   /* Discard all the shlib descriptors.
1533    */
1534   while (sl)
1535     {
1536       struct so_list *next_sl = sl->next;
1537       free (sl);
1538       sl = next_sl;
1539     }
1540   so_list_head = NULL;
1541
1542   som_solib_total_st_size = (LONGEST) 0;
1543   som_solib_st_size_threshold_exceeded = 0;
1544
1545   dld_cache.is_valid = 0;
1546
1547   dld_cache.hook.address = 0;
1548   dld_cache.hook.unwind = NULL;
1549
1550   dld_cache.hook_stub.address = 0;
1551   dld_cache.hook_stub.unwind = NULL;
1552
1553   dld_cache.load.address = 0;
1554   dld_cache.load.unwind = NULL;
1555
1556   dld_cache.load_stub.address = 0;
1557   dld_cache.load_stub.unwind = NULL;
1558
1559   dld_cache.unload.address = 0;
1560   dld_cache.unload.unwind = NULL;
1561
1562   dld_cache.unload2.address = 0;
1563   dld_cache.unload2.unwind = NULL;
1564
1565   dld_cache.unload_stub.address = 0;
1566   dld_cache.unload_stub.unwind = NULL;
1567 }
1568
1569
1570
1571 void
1572 _initialize_som_solib ()
1573 {
1574   add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
1575            "Load shared object library symbols for files matching REGEXP.");
1576   add_info ("sharedlibrary", som_sharedlibrary_info_command,
1577             "Status of loaded shared object libraries.");
1578   add_show_from_set
1579     (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
1580                   (char *) &auto_solib_add,
1581                   "Set autoloading size threshold (in megabytes) of shared library symbols.\n\
1582 If nonzero, symbols from all shared object libraries will be loaded\n\
1583 automatically when the inferior begins execution or when the dynamic linker\n\
1584 informs gdb that a new library has been loaded, until the symbol table\n\
1585 of the program and libraries exceeds this threshold.\n\
1586 Otherwise, symbols must be loaded manually, using `sharedlibrary'.",
1587                   &setlist),
1588      &showlist);
1589
1590   /* ??rehrauer: On HP-UX, the kernel parameter MAXDSIZ limits how much
1591      data space a process can use.  We ought to be reading MAXDSIZ and
1592      setting auto_solib_add to some large fraction of that value.  If
1593      not that, we maybe ought to be setting it smaller than the default
1594      for MAXDSIZ (that being 64Mb, I believe).  However, [1] this threshold
1595      is only crudely approximated rather than actually measured, and [2]
1596      50 Mbytes is too small for debugging gdb itself.  Thus, the arbitrary
1597      100 figure.
1598    */
1599   auto_solib_add = 100;         /* Megabytes */
1600
1601   som_solib_restart ();
1602 }
1603
1604 /* Get some HPUX-specific data from a shared lib.
1605  */
1606 CORE_ADDR
1607 so_lib_thread_start_addr (so)
1608      struct so_list *so;
1609 {
1610   return so->som_solib.tsd_start_addr;
1611 }