* somsolib.c: Add TODO list.
[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
37 /* TODO:
38
39 * Relocate data addresses in the shared library.
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 * Test attaching to running dynamically linked code.
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 /* First see if the objfile was dynamically linked. */
132 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
133 if (!shlib_info)
134 return;
135
136 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
137 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
138 return;
139
140 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
141 if (msymbol == NULL)
142 {
143 error ("Unable to find __dld_flags symbol in object file.\n");
144 return;
145 }
146
147 addr = SYMBOL_VALUE_ADDRESS (msymbol);
148 /* Read the current contents. */
149 status = target_read_memory (addr, buf, 4);
150 if (status != 0)
151 {
152 error ("Unable to read __dld_flags\n");
153 return;
154 }
155 dld_flags = extract_unsigned_integer (buf, 4);
156
157 /* __dld_list may not be valid. If it's not valid tell the user. */
158 if ((dld_flags & 4) == 0)
159 {
160 error ("__dld_list is not valid according to __dld_flags.\n");
161 return;
162 }
163
164 /* If the libraries were not mapped private, warn the user. */
165 if ((dld_flags & 1) == 0)
166 warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n");
167
168 msymbol = lookup_minimal_symbol ("__dld_list", (struct objfile *) NULL);
169 if (!msymbol)
170 {
171 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
172 but the data is still available if you know where to look. */
173 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *)NULL);
174 if (!msymbol)
175 {
176 error ("Unable to find dynamic library list.\n");
177 return;
178 }
179 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
180 }
181 else
182 addr = SYMBOL_VALUE_ADDRESS (msymbol);
183
184 status = target_read_memory (addr, buf, 4);
185 if (status != 0)
186 {
187 error ("Unable to find dynamic library list.\n");
188 return;
189 }
190
191 addr = extract_unsigned_integer (buf, 4);
192
193 /* If addr is zero, then we're using an old dynamic loader which
194 doesn't maintain __dld_list. We'll have to use a completely
195 different approach to get shared library information. */
196 if (addr == 0)
197 goto old_dld;
198
199 /* Using the information in __dld_list is the preferred method
200 to get at shared library information. It doesn't depend on
201 any functions in /usr/lib/end.o and has a chance of working
202 with hpux10 when it is released. */
203 status = target_read_memory (addr, buf, 4);
204 if (status != 0)
205 {
206 error ("Unable to find dynamic library list.\n");
207 return;
208 }
209
210 /* addr now holds the address of the first entry in the dynamic
211 library list. */
212 addr = extract_unsigned_integer (buf, 4);
213
214 /* Now that we have a pointer to the dynamic library list, walk
215 through it and add the symbols for each library.
216
217 Skip the first entry since it's our executable. */
218 status = target_read_memory (addr + 36, buf, 4);
219 if (status != 0)
220 goto err;
221
222 addr = extract_unsigned_integer (buf, 4);
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 section_table *p;
236
237 if (addr == 0)
238 break;
239
240 /* Get a pointer to the name of this library. */
241 status = target_read_memory (addr, buf, 4);
242 if (status != 0)
243 goto err;
244
245 name_addr = extract_unsigned_integer (buf, 4);
246 name_len = 0;
247 while (1)
248 {
249 target_read_memory (name_addr + name_len, buf, 1);
250 if (status != 0)
251 goto err;
252
253 name_len++;
254 if (*buf == '\0')
255 break;
256 }
257 name = alloca (name_len);
258 status = target_read_memory (name_addr, name, name_len);
259 if (status != 0)
260 goto err;
261
262 name = obsavestring (name, name_len - 1,
263 &symfile_objfile->symbol_obstack);
264
265 status = target_read_memory (addr + 8, buf, 4);
266 if (status != 0)
267 goto err;
268
269 text_addr = extract_unsigned_integer (buf, 4);
270
271
272 new_so = (struct so_list *) malloc (sizeof (struct so_list));
273 memset ((char *)new_so, 0, sizeof (struct so_list));
274 if (so_list_head == NULL)
275 {
276 so_list_head = new_so;
277 so_list_tail = new_so;
278 }
279 else
280 {
281 so_list_tail->next = new_so;
282 so_list_tail = new_so;
283 }
284
285 /* Fill in all the entries in GDB's shared library list. */
286 new_so->som_solib.name = name;
287 status = target_read_memory (addr + 4, buf, 4);
288 if (status != 0)
289 goto err;
290
291 new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1);
292 new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1);
293 new_so->som_solib.library_version = extract_unsigned_integer (buf, 2);
294 new_so->som_solib.text_addr = text_addr;
295
296 status = target_read_memory (addr + 12, buf, 4);
297 if (status != 0)
298 goto err;
299
300 new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4);
301
302 status = target_read_memory (addr + 16, buf, 4);
303 if (status != 0)
304 goto err;
305
306 new_so->som_solib.text_end = extract_unsigned_integer (buf, 4);
307
308 status = target_read_memory (addr + 20, buf, 4);
309 if (status != 0)
310 goto err;
311
312 new_so->som_solib.data_start = extract_unsigned_integer (buf, 4);
313
314 status = target_read_memory (addr + 24, buf, 4);
315 if (status != 0)
316 goto err;
317
318 new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4);
319
320 status = target_read_memory (addr + 28, buf, 4);
321 if (status != 0)
322 goto err;
323
324 new_so->som_solib.data_end = extract_unsigned_integer (buf, 4);
325
326 status = target_read_memory (addr + 32, buf, 4);
327 if (status != 0)
328 goto err;
329
330 new_so->som_solib.got_value = extract_unsigned_integer (buf, 4);
331
332 status = target_read_memory (addr + 36, buf, 4);
333 if (status != 0)
334 goto err;
335
336 new_so->som_solib.next = (void *)extract_unsigned_integer (buf, 4);
337 addr = (CORE_ADDR)new_so->som_solib.next;
338
339 new_so->objfile = symbol_file_add (name, from_tty, text_addr, 0, 0, 0);
340 new_so->abfd = bfd_openr (name, gnutarget);
341 new_so->abfd->cacheable = true;
342
343 if (!bfd_check_format (new_so->abfd, bfd_object))
344 {
345 error ("\"%s\": not in executable format: %s.",
346 name, bfd_errmsg (bfd_get_error ()));
347 }
348
349 /* Now we need to build a section table for this library since
350 we might be debugging a core file from a dynamically linked
351 executable in which the libraries were not privately mapped. */
352 if (build_section_table (new_so->abfd,
353 &new_so->sections,
354 &new_so->sections_end))
355 {
356 error ("Unable to build section table for shared library\n.");
357 return;
358 }
359
360 /* Relocate all the sections based on where they got loaded. */
361 for (p = new_so->sections; p < new_so->sections_end; p++)
362 {
363 if (p->the_bfd_section->flags & SEC_CODE)
364 {
365 p->addr += text_addr - new_so->som_solib.text_link_addr;
366 p->endaddr += text_addr - new_so->som_solib.text_link_addr;
367 }
368 else if (p->the_bfd_section->flags & SEC_DATA)
369 {
370 p->addr += new_so->som_solib.data_start;
371 p->endaddr += new_so->som_solib.data_start;
372 }
373 }
374
375 /* Now see if we need to map in the text and data for this shared
376 library (for example debugging a core file which does not use
377 private shared libraries.).
378
379 Carefully peek at the first text address in the library. If the
380 read succeeds, then the libraries were privately mapped and were
381 included in the core dump file.
382
383 If the peek failed, then the libraries were not privately mapped
384 and are not in the core file, we'll have to read them in ourselves. */
385 status = target_read_memory (text_addr, buf, 4);
386 if (status != 0)
387 {
388 int old;
389
390 /* Add sections from the shared library to the core target. */
391 if (target->to_sections)
392 {
393 old = target->to_sections_end - target->to_sections;
394 target->to_sections = (struct section_table *)
395 xrealloc ((char *)target->to_sections,
396 ((sizeof (struct section_table))
397 * (old + bfd_count_sections (new_so->abfd))));
398 }
399 else
400 {
401 old = 0;
402 target->to_sections = (struct section_table *)
403 xmalloc ((sizeof (struct section_table))
404 * bfd_count_sections (new_so->abfd));
405 }
406 target->to_sections_end = (target->to_sections
407 + old + bfd_count_sections (new_so->abfd));
408 memcpy ((char *)(target->to_sections + old),
409 new_so->sections,
410 ((sizeof (struct section_table))
411 * bfd_count_sections (new_so->abfd)));
412 }
413 }
414
415 /* Getting new symbols may change our opinion about what is
416 frameless. */
417 reinit_frame_cache ();
418 return;
419
420 old_dld:
421 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
422 return;
423
424 err:
425 error ("Error while reading dynamic library list.\n");
426 return;
427 }
428
429
430 /* This hook gets called just before the first instruction in the
431 inferior process is executed.
432
433 This is our opportunity to set magic flags in the inferior so
434 that GDB can be notified when a shared library is mapped in and
435 to tell the dynamic linker that a private copy of the library is
436 needed (so GDB can set breakpoints in the library).
437
438 __dld_flags is the location of the magic flags; as of this implementation
439 there are 3 flags of interest:
440
441 bit 0 when set indicates that private copies of the libraries are needed
442 bit 1 when set indicates that the callback hook routine is valid
443 bit 2 when set indicates that the dynamic linker should maintain the
444 __dld_list structure when loading/unloading libraries.
445
446 Note that shared libraries are not mapped in at this time, so we have
447 run the inferior until the libraries are mapped in. Typically this
448 means running until the "_start" is called. */
449
450 void
451 som_solib_create_inferior_hook()
452 {
453 struct minimal_symbol *msymbol;
454 unsigned int dld_flags, status;
455 asection *shlib_info;
456 char shadow_contents[BREAKPOINT_MAX], buf[4];
457 CORE_ADDR anaddr;
458
459 /* First see if the objfile was dynamically linked. */
460 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
461 if (!shlib_info)
462 return;
463
464 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
465 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
466 return;
467
468 /* Get the address of __dld_flags, if no such symbol exists, then we can
469 not debug the shared code. */
470 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
471 if (msymbol == NULL)
472 {
473 error ("Unable to find __dld_flags symbol in object file.\n");
474 return;
475 }
476
477 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
478 /* Read the current contents. */
479 status = target_read_memory (anaddr, buf, 4);
480 if (status != 0)
481 {
482 error ("Unable to read __dld_flags\n");
483 return;
484 }
485 dld_flags = extract_unsigned_integer (buf, 4);
486
487 /* Turn on the flags we care about. */
488 dld_flags |= 0x5;
489 store_unsigned_integer (buf, 4, dld_flags);
490 status = target_write_memory (anaddr, buf, 4);
491 if (status != 0)
492 {
493 error ("Unable to write __dld_flags\n");
494 return;
495 }
496
497 /* Now find the address of _start and set a breakpoint there. */
498 msymbol = lookup_minimal_symbol ("_start", symfile_objfile);
499 if (msymbol == NULL)
500 {
501 error ("Unable to find _start symbol in object file.\n");
502 return;
503 }
504
505 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
506 if (target_insert_breakpoint (anaddr, shadow_contents))
507 {
508 error ("Unable to set breakpoint at _start.\n");
509 return;
510 }
511
512 /* Wipe out all knowledge of old shared libraries since their
513 mapping can change from one exec to another! */
514 while (so_list_head)
515 {
516 struct so_list *temp;
517
518 free_objfile (so_list_head->objfile);
519 temp = so_list_head;
520 free (so_list_head);
521 so_list_head = temp->next;
522 }
523
524 /* Start the process again and wait for it to hit our breakpoint. */
525 clear_proceed_status ();
526 stop_soon_quietly = 1;
527 stop_signal = TARGET_SIGNAL_0;
528 do
529 {
530 target_resume (-1, 0, stop_signal);
531 wait_for_inferior ();
532 }
533 while (stop_signal != TARGET_SIGNAL_TRAP);
534 stop_soon_quietly = 0;
535
536 /* All the libraries should be mapped in now. Remove our breakpoint and
537 read in the symbol tables from the shared libraries. */
538 if (target_remove_breakpoint (anaddr, shadow_contents))
539 {
540 error ("Unable to remove breakpoint at _start.\n");
541 return;
542 }
543
544 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
545 }
546
547 /* Dump information about all the currently loaded shared libraries. */
548
549 static void
550 som_sharedlibrary_info_command (ignore, from_tty)
551 char *ignore;
552 int from_tty;
553 {
554 struct so_list *so_list = so_list_head;
555
556 if (exec_bfd == NULL)
557 {
558 printf_unfiltered ("no exec file.\n");
559 return;
560 }
561
562 if (so_list == NULL)
563 {
564 printf_unfiltered ("No shared libraries loaded at this time.\n");
565 return;
566 }
567
568 printf_unfiltered ("Shared Object Libraries\n");
569 printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n",
570 " flags", " tstart", " tend", " dstart", " dend", " dlt");
571 while (so_list)
572 {
573 unsigned int flags;
574
575 flags = so_list->som_solib.struct_version << 24;
576 flags |= so_list->som_solib.bind_mode << 16;
577 flags |= so_list->som_solib.library_version;
578 printf_unfiltered ("%s\n", so_list->som_solib.name);
579 printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l"));
580 printf_unfiltered ("%-12s",
581 local_hex_string_custom (so_list->som_solib.text_addr, "08l"));
582 printf_unfiltered ("%-12s",
583 local_hex_string_custom (so_list->som_solib.text_end, "08l"));
584 printf_unfiltered ("%-12s",
585 local_hex_string_custom (so_list->som_solib.data_start, "08l"));
586 printf_unfiltered ("%-12s",
587 local_hex_string_custom (so_list->som_solib.data_end, "08l"));
588 printf_unfiltered ("%-12s\n",
589 local_hex_string_custom (so_list->som_solib.got_value, "08l"));
590 so_list = so_list->next;
591 }
592 }
593
594 void
595 _initialize_som_solib ()
596 {
597 add_info ("sharedlibrary", som_sharedlibrary_info_command,
598 "Status of loaded shared object libraries.");
599 }
This page took 0.047063 seconds and 5 git commands to generate.