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