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