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