* somsolib.c (auto_solib_add_at_startup): Delete definition. No
[binutils-gdb.git] / gdb / somsolib.c
1 /* Handle HP SOM shared libraries for GDB, the GNU Debugger.
2 Copyright 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 Written by the Center for Software Science at the Univerity of Utah
21 and by Cygnus Support. */
22
23
24 #include "defs.h"
25
26 #include "frame.h"
27 #include "bfd.h"
28 #include "som.h"
29 #include "libhppa.h"
30 #include "gdbcore.h"
31 #include "symtab.h"
32 #include "breakpoint.h"
33 #include "symfile.h"
34 #include "objfiles.h"
35 #include "inferior.h"
36 #include "gdb-stabs.h"
37 #include "gdbcmd.h"
38
39 /* TODO:
40
41 * Most of this code should work for hp300 shared libraries. Does
42 anyone care enough to weed out any SOM-isms.
43
44 * Do we need/want a command to load a shared library?
45
46 * Support for hpux8 dynamic linker.
47
48 * Support for tracking user calls to dld_load, dld_unload. */
49
50 /* The basic structure which describes a dynamically loaded object. This
51 data structure is private to the dynamic linker and isn't found in
52 any HPUX include file. */
53
54 struct som_solib_mapped_entry
55 {
56 /* The name of the library. */
57 char *name;
58
59 /* Version of this structure (it is expected to change again in hpux10). */
60 unsigned char struct_version;
61
62 /* Binding mode for this library. */
63 unsigned char bind_mode;
64
65 /* Version of this library. */
66 short library_version;
67
68 /* Start of text address, link-time text location, end of text address. */
69 CORE_ADDR text_addr;
70 CORE_ADDR text_link_addr;
71 CORE_ADDR text_end;
72
73 /* Start of data, start of bss and end of data. */
74 CORE_ADDR data_start;
75 CORE_ADDR bss_start;
76 CORE_ADDR data_end;
77
78 /* Value of linkage pointer (%r19). */
79 CORE_ADDR got_value;
80
81 /* Next entry. */
82 struct som_solib_mapped_entry *next;
83
84 /* There are other fields, but I don't have information as to what is
85 contained in them. */
86 };
87
88 /* A structure to keep track of all the known shared objects. */
89 struct so_list
90 {
91 struct som_solib_mapped_entry som_solib;
92 struct objfile *objfile;
93 bfd *abfd;
94 struct section_table *sections;
95 struct section_table *sections_end;
96 struct so_list *next;
97 };
98
99 static struct so_list *so_list_head;
100
101 static void som_sharedlibrary_info_command PARAMS ((char *, int));
102
103 /* Add symbols from shared libraries into the symtab list. */
104
105 void
106 som_solib_add (arg_string, from_tty, target)
107 char *arg_string;
108 int from_tty;
109 struct target_ops *target;
110 {
111 struct minimal_symbol *msymbol;
112 struct so_list *so_list_tail;
113 CORE_ADDR addr;
114 asection *shlib_info;
115 int status;
116 unsigned int dld_flags;
117 char buf[4], *re_err;
118
119 /* First validate our arguments. */
120 if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL)
121 {
122 error ("Invalid regexp: %s", re_err);
123 }
124
125 /* If we're debugging a core file, or have attached to a running
126 process, then som_solib_create_inferior_hook will not have been
127 called.
128
129 We need to first determine if we're dealing with a dynamically
130 linked executable. If not, then return without an error or warning.
131
132 We also need to examine __dld_flags to determine if the shared library
133 list is valid and to determine if the libraries have been privately
134 mapped. */
135 if (symfile_objfile == NULL)
136 return;
137
138 /* First see if the objfile was dynamically linked. */
139 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
140 if (!shlib_info)
141 return;
142
143 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
144 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
145 return;
146
147 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
148 if (msymbol == NULL)
149 {
150 error ("Unable to find __dld_flags symbol in object file.\n");
151 return;
152 }
153
154 addr = SYMBOL_VALUE_ADDRESS (msymbol);
155 /* Read the current contents. */
156 status = target_read_memory (addr, buf, 4);
157 if (status != 0)
158 {
159 error ("Unable to read __dld_flags\n");
160 return;
161 }
162 dld_flags = extract_unsigned_integer (buf, 4);
163
164 /* __dld_list may not be valid. If it's not valid tell the user. */
165 if ((dld_flags & 4) == 0)
166 {
167 error ("__dld_list is not valid according to __dld_flags.\n");
168 return;
169 }
170
171 /* If the libraries were not mapped private, warn the user. */
172 if ((dld_flags & 1) == 0)
173 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
174
175 msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL);
176 if (!msymbol)
177 {
178 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
179 but the data is still available if you know where to look. */
180 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
181 if (!msymbol)
182 {
183 error ("Unable to find dynamic library list.\n");
184 return;
185 }
186 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
187 }
188 else
189 addr = SYMBOL_VALUE_ADDRESS (msymbol);
190
191 status = target_read_memory (addr, buf, 4);
192 if (status != 0)
193 {
194 error ("Unable to find dynamic library list.\n");
195 return;
196 }
197
198 addr = extract_unsigned_integer (buf, 4);
199
200 /* If addr is zero, then we're using an old dynamic loader which
201 doesn't maintain __dld_list. We'll have to use a completely
202 different approach to get shared library information. */
203 if (addr == 0)
204 goto old_dld;
205
206 /* Using the information in __dld_list is the preferred method
207 to get at shared library information. It doesn't depend on
208 any functions in /usr/lib/end.o and has a chance of working
209 with hpux10 when it is released. */
210 status = target_read_memory (addr, buf, 4);
211 if (status != 0)
212 {
213 error ("Unable to find dynamic library list.\n");
214 return;
215 }
216
217 /* addr now holds the address of the first entry in the dynamic
218 library list. */
219 addr = extract_unsigned_integer (buf, 4);
220
221 /* Now that we have a pointer to the dynamic library list, walk
222 through it and add the symbols for each library. */
223
224 so_list_tail = so_list_head;
225 /* Find the end of the list of shared objects. */
226 while (so_list_tail && so_list_tail->next)
227 so_list_tail = so_list_tail->next;
228
229 while (1)
230 {
231 CORE_ADDR name_addr, text_addr;
232 unsigned int name_len;
233 char *name;
234 struct so_list *new_so;
235 struct so_list *so_list = so_list_head;
236 struct section_table *p;
237
238 if (addr == 0)
239 break;
240
241 /* Get a pointer to the name of this library. */
242 status = target_read_memory (addr, buf, 4);
243 if (status != 0)
244 goto err;
245
246 name_addr = extract_unsigned_integer (buf, 4);
247 name_len = 0;
248 while (1)
249 {
250 target_read_memory (name_addr + name_len, buf, 1);
251 if (status != 0)
252 goto err;
253
254 name_len++;
255 if (*buf == '\0')
256 break;
257 }
258 name = alloca (name_len);
259 status = target_read_memory (name_addr, name, name_len);
260 if (status != 0)
261 goto err;
262
263 /* See if we've already loaded something with this name. */
264 while (so_list)
265 {
266 if (!strcmp (so_list->som_solib.name, name))
267 break;
268 so_list = so_list->next;
269 }
270
271 /* We've already loaded this one or it's the main program, skip it. */
272 if (so_list || !strcmp (name, symfile_objfile->name))
273 {
274 status = target_read_memory (addr + 36, buf, 4);
275 if (status != 0)
276 goto err;
277
278 addr = (CORE_ADDR) extract_unsigned_integer (buf, 4);
279 continue;
280 }
281
282 name = obsavestring (name, name_len - 1,
283 &symfile_objfile->symbol_obstack);
284
285 status = target_read_memory (addr + 8, buf, 4);
286 if (status != 0)
287 goto err;
288
289 text_addr = extract_unsigned_integer (buf, 4);
290
291
292 new_so = (struct so_list *) malloc (sizeof (struct so_list));
293 memset ((char *)new_so, 0, sizeof (struct so_list));
294 if (so_list_head == NULL)
295 {
296 so_list_head = new_so;
297 so_list_tail = new_so;
298 }
299 else
300 {
301 so_list_tail->next = new_so;
302 so_list_tail = new_so;
303 }
304
305 /* Fill in all the entries in GDB's shared library list. */
306 new_so->som_solib.name = name;
307 status = target_read_memory (addr + 4, buf, 4);
308 if (status != 0)
309 goto err;
310
311 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
312 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
313 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
314 new_so->som_solib.text_addr = text_addr;
315
316 status = target_read_memory (addr + 12, buf, 4);
317 if (status != 0)
318 goto err;
319
320 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
321
322 status = target_read_memory (addr + 16, buf, 4);
323 if (status != 0)
324 goto err;
325
326 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
327
328 status = target_read_memory (addr + 20, buf, 4);
329 if (status != 0)
330 goto err;
331
332 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
333
334 status = target_read_memory (addr + 24, buf, 4);
335 if (status != 0)
336 goto err;
337
338 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
339
340 status = target_read_memory (addr + 28, buf, 4);
341 if (status != 0)
342 goto err;
343
344 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
345
346 status = target_read_memory (addr + 32, buf, 4);
347 if (status != 0)
348 goto err;
349
350 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
351
352 status = target_read_memory (addr + 36, buf, 4);
353 if (status != 0)
354 goto err;
355
356 new_so->som_solib.next = (void *)extract_unsigned_integer (buf, 4);
357 addr = (CORE_ADDR)new_so->som_solib.next;
358
359 new_so->objfile = symbol_file_add (name, from_tty, text_addr, 0, 0, 0);
360 new_so->abfd = new_so->objfile->obfd;
361
362 if (!bfd_check_format (new_so->abfd, bfd_object))
363 {
364 error ("\"%s\": not in executable format: %s.",
365 name, bfd_errmsg (bfd_get_error ()));
366 }
367
368 /* Now we need to build a section table for this library since
369 we might be debugging a core file from a dynamically linked
370 executable in which the libraries were not privately mapped. */
371 if (build_section_table (new_so->abfd,
372 &new_so->sections,
373 &new_so->sections_end))
374 {
375 error ("Unable to build section table for shared library\n.");
376 return;
377 }
378
379 /* Relocate all the sections based on where they got loaded. */
380 for (p = new_so->sections; p < new_so->sections_end; p++)
381 {
382 if (p->the_bfd_section->flags & SEC_CODE)
383 {
384 p->addr += text_addr - new_so->som_solib.text_link_addr;
385 p->endaddr += text_addr - new_so->som_solib.text_link_addr;
386 }
387 else if (p->the_bfd_section->flags & SEC_DATA)
388 {
389 p->addr += new_so->som_solib.data_start;
390 p->endaddr += new_so->som_solib.data_start;
391 }
392 }
393
394 /* Now see if we need to map in the text and data for this shared
395 library (for example debugging a core file which does not use
396 private shared libraries.).
397
398 Carefully peek at the first text address in the library. If the
399 read succeeds, then the libraries were privately mapped and were
400 included in the core dump file.
401
402 If the peek failed, then the libraries were not privately mapped
403 and are not in the core file, we'll have to read them in ourselves. */
404 status = target_read_memory (text_addr, buf, 4);
405 if (status != 0)
406 {
407 int old, new;
408
409 new = new_so->sections_end - new_so->sections;
410 /* Add sections from the shared library to the core target. */
411 if (target->to_sections)
412 {
413 old = target->to_sections_end - target->to_sections;
414 target->to_sections = (struct section_table *)
415 xrealloc ((char *)target->to_sections,
416 ((sizeof (struct section_table)) * (old + new)));
417 }
418 else
419 {
420 old = 0;
421 target->to_sections = (struct section_table *)
422 xmalloc ((sizeof (struct section_table)) * new);
423 }
424 target->to_sections_end = (target->to_sections + old + new);
425 memcpy ((char *)(target->to_sections + old),
426 new_so->sections,
427 ((sizeof (struct section_table)) * new));
428 }
429 }
430
431 /* Getting new symbols may change our opinion about what is
432 frameless. */
433 reinit_frame_cache ();
434 return;
435
436 old_dld:
437 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
438 return;
439
440 err:
441 error ("Error while reading dynamic library list.\n");
442 return;
443 }
444
445
446 /* This hook gets called just before the first instruction in the
447 inferior process is executed.
448
449 This is our opportunity to set magic flags in the inferior so
450 that GDB can be notified when a shared library is mapped in and
451 to tell the dynamic linker that a private copy of the library is
452 needed (so GDB can set breakpoints in the library).
453
454 __dld_flags is the location of the magic flags; as of this implementation
455 there are 3 flags of interest:
456
457 bit 0 when set indicates that private copies of the libraries are needed
458 bit 1 when set indicates that the callback hook routine is valid
459 bit 2 when set indicates that the dynamic linker should maintain the
460 __dld_list structure when loading/unloading libraries.
461
462 Note that shared libraries are not mapped in at this time, so we have
463 run the inferior until the libraries are mapped in. Typically this
464 means running until the "_start" is called. */
465
466 void
467 som_solib_create_inferior_hook()
468 {
469 struct minimal_symbol *msymbol;
470 unsigned int dld_flags, status;
471 asection *shlib_info;
472 char shadow_contents[BREAKPOINT_MAX], buf[4];
473 CORE_ADDR anaddr;
474
475 if (symfile_objfile == NULL)
476 return;
477
478 /* First see if the objfile was dynamically linked. */
479 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
480 if (!shlib_info)
481 return;
482
483 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
484 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
485 return;
486
487 /* Get the address of __dld_flags, if no such symbol exists, then we can
488 not debug the shared code. */
489 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
490 if (msymbol == NULL)
491 {
492 error ("Unable to find __dld_flags symbol in object file.\n");
493 return;
494 }
495
496 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
497 /* Read the current contents. */
498 status = target_read_memory (anaddr, buf, 4);
499 if (status != 0)
500 {
501 error ("Unable to read __dld_flags\n");
502 return;
503 }
504 dld_flags = extract_unsigned_integer (buf, 4);
505
506 /* Turn on the flags we care about. */
507 dld_flags |= 0x5;
508 store_unsigned_integer (buf, 4, dld_flags);
509 status = target_write_memory (anaddr, buf, 4);
510 if (status != 0)
511 {
512 error ("Unable to write __dld_flags\n");
513 return;
514 }
515
516 /* Now find the address of _start and set a breakpoint there. */
517 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
518 if (msymbol == NULL)
519 {
520 error ("Unable to find _start symbol in object file.\n");
521 return;
522 }
523
524 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
525 if (target_insert_breakpoint (anaddr, shadow_contents))
526 {
527 error ("Unable to set breakpoint at _start.\n");
528 return;
529 }
530
531 /* Wipe out all knowledge of old shared libraries since their
532 mapping can change from one exec to another! */
533 while (so_list_head)
534 {
535 struct so_list *temp;
536
537 free_objfile (so_list_head->objfile);
538 temp = so_list_head;
539 free (so_list_head);
540 so_list_head = temp->next;
541 }
542
543 /* Start the process again and wait for it to hit our breakpoint. */
544 clear_proceed_status ();
545 stop_soon_quietly = 1;
546 stop_signal = TARGET_SIGNAL_0;
547 do
548 {
549 target_resume (-1, 0, stop_signal);
550 wait_for_inferior ();
551 }
552 while (stop_signal != TARGET_SIGNAL_TRAP);
553 stop_soon_quietly = 0;
554
555 /* All the libraries should be mapped in now. Remove our breakpoint and
556 read in the symbol tables from the shared libraries. */
557 if (target_remove_breakpoint (anaddr, shadow_contents))
558 {
559 error ("Unable to remove breakpoint at _start.\n");
560 return;
561 }
562
563 if (auto_solib_add_at_startup)
564 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
565 }
566
567 /* Return the GOT value for the shared library in which ADDR belongs. If
568 ADDR isn't in any known shared library, return zero. */
569
570 CORE_ADDR
571 som_solib_get_got_by_pc (addr)
572 CORE_ADDR addr;
573 {
574 struct so_list *so_list = so_list_head;
575 CORE_ADDR got_value = 0;
576
577 while (so_list)
578 {
579 if (so_list->som_solib.text_addr <= addr
580 && so_list->som_solib.text_end > addr)
581 {
582 got_value = so_list->som_solib.got_value;
583 break;
584 }
585 so_list = so_list->next;
586 }
587 return got_value;
588 }
589
590 int
591 som_solib_section_offsets (objfile, offsets)
592 struct objfile *objfile;
593 struct section_offsets *offsets;
594 {
595 struct so_list *so_list = so_list_head;
596
597 while (so_list)
598 {
599 /* Oh what a pain! We need the offsets before so_list->objfile
600 is valid. The BFDs will never match. Make a best guess. */
601 if (strstr (objfile->name, so_list->som_solib.name))
602 {
603 asection *private_section;
604
605 /* The text offset is easy. */
606 ANOFFSET (offsets, SECT_OFF_TEXT)
607 = (so_list->som_solib.text_addr
608 - so_list->som_solib.text_link_addr);
609 ANOFFSET (offsets, SECT_OFF_RODATA)
610 = ANOFFSET (offsets, SECT_OFF_TEXT);
611
612 /* We should look at presumed_dp in the SOM header, but
613 that's not easily available. This should be OK though. */
614 private_section = bfd_get_section_by_name (objfile->obfd,
615 "$PRIVATE$");
616 if (!private_section)
617 {
618 warning ("Unable to find $PRIVATE$ in shared library!");
619 ANOFFSET (offsets, SECT_OFF_DATA) = 0;
620 ANOFFSET (offsets, SECT_OFF_BSS) = 0;
621 return 1;
622 }
623 ANOFFSET (offsets, SECT_OFF_DATA)
624 = (so_list->som_solib.data_start - private_section->vma);
625 ANOFFSET (offsets, SECT_OFF_BSS)
626 = ANOFFSET (offsets, SECT_OFF_DATA);
627 return 1;
628 }
629 so_list = so_list->next;
630 }
631 return 0;
632 }
633
634 /* Dump information about all the currently loaded shared libraries. */
635
636 static void
637 som_sharedlibrary_info_command (ignore, from_tty)
638 char *ignore;
639 int from_tty;
640 {
641 struct so_list *so_list = so_list_head;
642
643 if (exec_bfd == NULL)
644 {
645 printf_unfiltered ("no exec file.\n");
646 return;
647 }
648
649 if (so_list == NULL)
650 {
651 printf_unfiltered ("No shared libraries loaded at this time.\n");
652 return;
653 }
654
655 printf_unfiltered ("Shared Object Libraries\n");
656 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
657 " flags", " tstart", " tend", " dstart", " dend", " dlt");
658 while (so_list)
659 {
660 unsigned int flags;
661
662 flags = so_list->som_solib.struct_version << 24;
663 flags |= so_list->som_solib.bind_mode << 16;
664 flags |= so_list->som_solib.library_version;
665 printf_unfiltered ("%s\n", so_list->som_solib.name);
666 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
667 printf_unfiltered ("%-12s",
668 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
669 printf_unfiltered ("%-12s",
670 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
671 printf_unfiltered ("%-12s",
672 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
673 printf_unfiltered ("%-12s",
674 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
675 printf_unfiltered ("%-12s\n",
676 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
677 so_list = so_list->next;
678 }
679 }
680
681 static void
682 som_solib_sharedlibrary_command (args, from_tty)
683 char *args;
684 int from_tty;
685 {
686 dont_repeat ();
687 som_solib_add (args, from_tty, (struct target_ops *) 0);
688 }
689
690 void
691 _initialize_som_solib ()
692 {
693 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
694 "Load shared object library symbols for files matching REGEXP.");
695 add_info ("sharedlibrary", som_sharedlibrary_info_command,
696 "Status of loaded shared object libraries.");
697 add_show_from_set
698 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
699 (char *) &auto_solib_add_at_startup,
700 "Set autoloading of shared library symbols at startup.\n\
701 If nonzero, symbols from all shared object libraries will be loaded\n\
702 automatically when the inferior begins execution. Otherwise, symbols\n\
703 must be loaded manually, using `sharedlibrary'.",
704 &setlist),
705 &showlist);
706
707 }