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