* corelow.c (core_open): Don't update the to_sections and
[deliverable/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 int update_coreops;
409
410 /* We must update the to_sections field in the core_ops structure
411 here, otherwise we dereference a potential dangling pointer
412 for each call to target_read/write_memory within this routine. */
413 update_coreops = core_ops.to_sections == target->to_sections;
414
415 new = new_so->sections_end - new_so->sections;
416 /* Add sections from the shared library to the core target. */
417 if (target->to_sections)
418 {
419 old = target->to_sections_end - target->to_sections;
420 target->to_sections = (struct section_table *)
421 xrealloc ((char *)target->to_sections,
422 ((sizeof (struct section_table)) * (old + new)));
423 }
424 else
425 {
426 old = 0;
427 target->to_sections = (struct section_table *)
428 xmalloc ((sizeof (struct section_table)) * new);
429 }
430 target->to_sections_end = (target->to_sections + old + new);
431
432 /* Update the to_sections field in the core_ops structure
433 if needed. */
434 if (update_coreops)
435 {
436 core_ops.to_sections = target->to_sections;
437 core_ops.to_sections_end = target->to_sections_end;
438 }
439
440 /* Copy over the old data before it gets clobbered. */
441 memcpy ((char *)(target->to_sections + old),
442 new_so->sections,
443 ((sizeof (struct section_table)) * new));
444 }
445 }
446
447 /* Getting new symbols may change our opinion about what is
448 frameless. */
449 reinit_frame_cache ();
450 return;
451
452 old_dld:
453 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
454 return;
455
456 err:
457 error ("Error while reading dynamic library list.\n");
458 return;
459 }
460
461
462 /* This hook gets called just before the first instruction in the
463 inferior process is executed.
464
465 This is our opportunity to set magic flags in the inferior so
466 that GDB can be notified when a shared library is mapped in and
467 to tell the dynamic linker that a private copy of the library is
468 needed (so GDB can set breakpoints in the library).
469
470 __dld_flags is the location of the magic flags; as of this implementation
471 there are 3 flags of interest:
472
473 bit 0 when set indicates that private copies of the libraries are needed
474 bit 1 when set indicates that the callback hook routine is valid
475 bit 2 when set indicates that the dynamic linker should maintain the
476 __dld_list structure when loading/unloading libraries.
477
478 Note that shared libraries are not mapped in at this time, so we have
479 run the inferior until the libraries are mapped in. Typically this
480 means running until the "_start" is called. */
481
482 void
483 som_solib_create_inferior_hook()
484 {
485 struct minimal_symbol *msymbol;
486 unsigned int dld_flags, status;
487 asection *shlib_info;
488 char shadow_contents[BREAKPOINT_MAX], buf[4];
489 CORE_ADDR anaddr;
490
491 if (symfile_objfile == NULL)
492 return;
493
494 /* First see if the objfile was dynamically linked. */
495 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
496 if (!shlib_info)
497 return;
498
499 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
500 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
501 return;
502
503 /* Get the address of __dld_flags, if no such symbol exists, then we can
504 not debug the shared code. */
505 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
506 if (msymbol == NULL)
507 {
508 error ("Unable to find __dld_flags symbol in object file.\n");
509 return;
510 }
511
512 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
513 /* Read the current contents. */
514 status = target_read_memory (anaddr, buf, 4);
515 if (status != 0)
516 {
517 error ("Unable to read __dld_flags\n");
518 return;
519 }
520 dld_flags = extract_unsigned_integer (buf, 4);
521
522 /* Turn on the flags we care about. */
523 dld_flags |= 0x5;
524 store_unsigned_integer (buf, 4, dld_flags);
525 status = target_write_memory (anaddr, buf, 4);
526 if (status != 0)
527 {
528 error ("Unable to write __dld_flags\n");
529 return;
530 }
531
532 /* Now find the address of _start and set a breakpoint there. */
533 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
534 if (msymbol == NULL)
535 {
536 error ("Unable to find _start symbol in object file.\n");
537 return;
538 }
539
540 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
541 if (target_insert_breakpoint (anaddr, shadow_contents))
542 {
543 error ("Unable to set breakpoint at _start.\n");
544 return;
545 }
546
547 /* Wipe out all knowledge of old shared libraries since their
548 mapping can change from one exec to another! */
549 while (so_list_head)
550 {
551 struct so_list *temp;
552
553 free_objfile (so_list_head->objfile);
554 temp = so_list_head;
555 free (so_list_head);
556 so_list_head = temp->next;
557 }
558
559 /* Start the process again and wait for it to hit our breakpoint. */
560 clear_proceed_status ();
561 stop_soon_quietly = 1;
562 stop_signal = TARGET_SIGNAL_0;
563 do
564 {
565 target_resume (-1, 0, stop_signal);
566 wait_for_inferior ();
567 }
568 while (stop_signal != TARGET_SIGNAL_TRAP);
569 stop_soon_quietly = 0;
570
571 /* All the libraries should be mapped in now. Remove our breakpoint and
572 read in the symbol tables from the shared libraries. */
573 if (target_remove_breakpoint (anaddr, shadow_contents))
574 {
575 error ("Unable to remove breakpoint at _start.\n");
576 return;
577 }
578
579 if (auto_solib_add_at_startup)
580 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
581 }
582
583 /* Return the GOT value for the shared library in which ADDR belongs. If
584 ADDR isn't in any known shared library, return zero. */
585
586 CORE_ADDR
587 som_solib_get_got_by_pc (addr)
588 CORE_ADDR addr;
589 {
590 struct so_list *so_list = so_list_head;
591 CORE_ADDR got_value = 0;
592
593 while (so_list)
594 {
595 if (so_list->som_solib.text_addr <= addr
596 && so_list->som_solib.text_end > addr)
597 {
598 got_value = so_list->som_solib.got_value;
599 break;
600 }
601 so_list = so_list->next;
602 }
603 return got_value;
604 }
605
606 int
607 som_solib_section_offsets (objfile, offsets)
608 struct objfile *objfile;
609 struct section_offsets *offsets;
610 {
611 struct so_list *so_list = so_list_head;
612
613 while (so_list)
614 {
615 /* Oh what a pain! We need the offsets before so_list->objfile
616 is valid. The BFDs will never match. Make a best guess. */
617 if (strstr (objfile->name, so_list->som_solib.name))
618 {
619 asection *private_section;
620
621 /* The text offset is easy. */
622 ANOFFSET (offsets, SECT_OFF_TEXT)
623 = (so_list->som_solib.text_addr
624 - so_list->som_solib.text_link_addr);
625 ANOFFSET (offsets, SECT_OFF_RODATA)
626 = ANOFFSET (offsets, SECT_OFF_TEXT);
627
628 /* We should look at presumed_dp in the SOM header, but
629 that's not easily available. This should be OK though. */
630 private_section = bfd_get_section_by_name (objfile->obfd,
631 "$PRIVATE$");
632 if (!private_section)
633 {
634 warning ("Unable to find $PRIVATE$ in shared library!");
635 ANOFFSET (offsets, SECT_OFF_DATA) = 0;
636 ANOFFSET (offsets, SECT_OFF_BSS) = 0;
637 return 1;
638 }
639 ANOFFSET (offsets, SECT_OFF_DATA)
640 = (so_list->som_solib.data_start - private_section->vma);
641 ANOFFSET (offsets, SECT_OFF_BSS)
642 = ANOFFSET (offsets, SECT_OFF_DATA);
643 return 1;
644 }
645 so_list = so_list->next;
646 }
647 return 0;
648 }
649
650 /* Dump information about all the currently loaded shared libraries. */
651
652 static void
653 som_sharedlibrary_info_command (ignore, from_tty)
654 char *ignore;
655 int from_tty;
656 {
657 struct so_list *so_list = so_list_head;
658
659 if (exec_bfd == NULL)
660 {
661 printf_unfiltered ("no exec file.\n");
662 return;
663 }
664
665 if (so_list == NULL)
666 {
667 printf_unfiltered ("No shared libraries loaded at this time.\n");
668 return;
669 }
670
671 printf_unfiltered ("Shared Object Libraries\n");
672 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
673 " flags", " tstart", " tend", " dstart", " dend", " dlt");
674 while (so_list)
675 {
676 unsigned int flags;
677
678 flags = so_list->som_solib.struct_version << 24;
679 flags |= so_list->som_solib.bind_mode << 16;
680 flags |= so_list->som_solib.library_version;
681 printf_unfiltered ("%s\n", so_list->som_solib.name);
682 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
683 printf_unfiltered ("%-12s",
684 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
685 printf_unfiltered ("%-12s",
686 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
687 printf_unfiltered ("%-12s",
688 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
689 printf_unfiltered ("%-12s",
690 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
691 printf_unfiltered ("%-12s\n",
692 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
693 so_list = so_list->next;
694 }
695 }
696
697 static void
698 som_solib_sharedlibrary_command (args, from_tty)
699 char *args;
700 int from_tty;
701 {
702 dont_repeat ();
703 som_solib_add (args, from_tty, (struct target_ops *) 0);
704 }
705
706 void
707 _initialize_som_solib ()
708 {
709 add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command,
710 "Load shared object library symbols for files matching REGEXP.");
711 add_info ("sharedlibrary", som_sharedlibrary_info_command,
712 "Status of loaded shared object libraries.");
713 add_show_from_set
714 (add_set_cmd ("auto-solib-add", class_support, var_zinteger,
715 (char *) &auto_solib_add_at_startup,
716 "Set autoloading of shared library symbols at startup.\n\
717 If nonzero, symbols from all shared object libraries will be loaded\n\
718 automatically when the inferior begins execution. Otherwise, symbols\n\
719 must be loaded manually, using `sharedlibrary'.",
720 &setlist),
721 &showlist);
722
723 }
This page took 0.044292 seconds and 5 git commands to generate.