* configure.tgt (*-*-freebsd*): Set gdb_osabi to
[deliverable/binutils-gdb.git] / gdb / solib-pa64.c
1 /* Handle PA64 shared libraries for GDB, the GNU Debugger.
2
3 Copyright 2004 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 HP in their infinite stupidity choose not to use standard ELF dynamic
23 linker interfaces. They also choose not to make their ELF dymamic
24 linker interfaces compatible with the SOM dynamic linker. The
25 net result is we can not use either of the existing somsolib.c or
26 solib.c. What a crock.
27
28 Even more disgusting. This file depends on functions provided only
29 in certain PA64 libraries. Thus this file is supposed to only be
30 used native. When will HP ever learn that they need to provide the
31 same functionality in all their libraries! */
32
33 #include "defs.h"
34 #include "symtab.h"
35 #include "bfd.h"
36 #include "symfile.h"
37 #include "objfiles.h"
38 #include "gdbcore.h"
39 #include "target.h"
40 #include "inferior.h"
41
42 #include "hppa-tdep.h"
43 #include "solist.h"
44 #include "solib-pa64.h"
45
46 #undef SOLIB_PA64_DBG
47
48 /* If we are building for a SOM-only target, then we don't need this. */
49 #ifndef PA_SOM_ONLY
50
51 /* FIXME: kettenis/20041213: These includes should be eliminated. */
52 #include <dlfcn.h>
53 #include <elf.h>
54 #include <elf_hp.h>
55
56 struct lm_info {
57 struct load_module_desc desc;
58 CORE_ADDR desc_addr;
59 };
60
61 /* When adding fields, be sure to clear them in _initialize_pa64_solib. */
62 typedef struct
63 {
64 CORE_ADDR dld_flags_addr;
65 LONGEST dld_flags;
66 struct bfd_section *dyninfo_sect;
67 int have_read_dld_descriptor;
68 int is_valid;
69 CORE_ADDR load_map;
70 CORE_ADDR load_map_addr;
71 struct load_module_desc dld_desc;
72 }
73 dld_cache_t;
74
75 static dld_cache_t dld_cache;
76
77 static int
78 read_dynamic_info (asection *dyninfo_sect, dld_cache_t *dld_cache_p);
79
80 static void
81 pa64_relocate_section_addresses (struct so_list *so,
82 struct section_table *sec)
83 {
84 asection *asec = sec->the_bfd_section;
85 CORE_ADDR load_offset;
86
87 /* Relocate all the sections based on where they got loaded. */
88
89 load_offset = bfd_section_vma (so->abfd, asec) - asec->filepos;
90
91 if (asec->flags & SEC_CODE)
92 {
93 sec->addr += so->lm_info->desc.text_base - load_offset;
94 sec->endaddr += so->lm_info->desc.text_base - load_offset;
95 }
96 else if (asec->flags & SEC_DATA)
97 {
98 sec->addr += so->lm_info->desc.data_base - load_offset;
99 sec->endaddr += so->lm_info->desc.data_base - load_offset;
100 }
101 }
102
103 static void
104 pa64_free_so (struct so_list *so)
105 {
106 xfree (so->lm_info);
107 }
108
109 static void
110 pa64_clear_solib (void)
111 {
112 }
113
114 /* Wrapper for target_read_memory for dlgetmodinfo. */
115
116 static void *
117 pa64_target_read_memory (void *buffer, CORE_ADDR ptr, size_t bufsiz, int ident)
118 {
119 if (target_read_memory (ptr, buffer, bufsiz) != 0)
120 return 0;
121 return buffer;
122 }
123
124 /* Read the dynamic linker's internal shared library descriptor.
125
126 This must happen after dld starts running, so we can't do it in
127 read_dynamic_info. Record the fact that we have loaded the
128 descriptor. If the library is archive bound, then return zero, else
129 return nonzero. */
130
131 static int
132 read_dld_descriptor (void)
133 {
134 char *dll_path;
135 asection *dyninfo_sect;
136
137 /* If necessary call read_dynamic_info to extract the contents of the
138 .dynamic section from the shared library. */
139 if (!dld_cache.is_valid)
140 {
141 if (symfile_objfile == NULL)
142 error (_("No object file symbols."));
143
144 dyninfo_sect = bfd_get_section_by_name (symfile_objfile->obfd,
145 ".dynamic");
146 if (!dyninfo_sect)
147 {
148 return 0;
149 }
150
151 if (!read_dynamic_info (dyninfo_sect, &dld_cache))
152 error (_("Unable to read in .dynamic section information."));
153 }
154
155 /* Read the load map pointer. */
156 if (target_read_memory (dld_cache.load_map_addr,
157 (char *) &dld_cache.load_map,
158 sizeof (dld_cache.load_map))
159 != 0)
160 {
161 error (_("Error while reading in load map pointer."));
162 }
163
164 /* Read in the dld load module descriptor */
165 if (dlgetmodinfo (-1,
166 &dld_cache.dld_desc,
167 sizeof (dld_cache.dld_desc),
168 pa64_target_read_memory,
169 0,
170 dld_cache.load_map)
171 == 0)
172 {
173 error (_("Error trying to get information about dynamic linker."));
174 }
175
176 /* Indicate that we have loaded the dld descriptor. */
177 dld_cache.have_read_dld_descriptor = 1;
178
179 return 1;
180 }
181
182
183 /* Read the .dynamic section and extract the information of interest,
184 which is stored in dld_cache. The routine elf_locate_base in solib.c
185 was used as a model for this. */
186
187 static int
188 read_dynamic_info (asection *dyninfo_sect, dld_cache_t *dld_cache_p)
189 {
190 char *buf;
191 char *bufend;
192 CORE_ADDR dyninfo_addr;
193 int dyninfo_sect_size;
194 CORE_ADDR entry_addr;
195
196 /* Read in .dynamic section, silently ignore errors. */
197 dyninfo_addr = bfd_section_vma (symfile_objfile->obfd, dyninfo_sect);
198 dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
199 buf = alloca (dyninfo_sect_size);
200 if (target_read_memory (dyninfo_addr, buf, dyninfo_sect_size))
201 return 0;
202
203 /* Scan the .dynamic section and record the items of interest.
204 In particular, DT_HP_DLD_FLAGS */
205 for (bufend = buf + dyninfo_sect_size, entry_addr = dyninfo_addr;
206 buf < bufend;
207 buf += sizeof (Elf64_Dyn), entry_addr += sizeof (Elf64_Dyn))
208 {
209 Elf64_Dyn *x_dynp = (Elf64_Dyn*)buf;
210 Elf64_Sxword dyn_tag;
211 CORE_ADDR dyn_ptr;
212 char *pbuf;
213
214 pbuf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT);
215 dyn_tag = bfd_h_get_64 (symfile_objfile->obfd,
216 (bfd_byte*) &x_dynp->d_tag);
217
218 /* We can't use a switch here because dyn_tag is 64 bits and HP's
219 lame comiler does not handle 64bit items in switch statements. */
220 if (dyn_tag == DT_NULL)
221 break;
222 else if (dyn_tag == DT_HP_DLD_FLAGS)
223 {
224 /* Set dld_flags_addr and dld_flags in *dld_cache_p */
225 dld_cache_p->dld_flags_addr = entry_addr + offsetof(Elf64_Dyn, d_un);
226 if (target_read_memory (dld_cache_p->dld_flags_addr,
227 (char*) &dld_cache_p->dld_flags,
228 sizeof (dld_cache_p->dld_flags))
229 != 0)
230 {
231 error (_("Error while reading in .dynamic section of the program."));
232 }
233 }
234 else if (dyn_tag == DT_HP_LOAD_MAP)
235 {
236 /* Dld will place the address of the load map at load_map_addr
237 after it starts running. */
238 if (target_read_memory (entry_addr + offsetof(Elf64_Dyn,
239 d_un.d_ptr),
240 (char*) &dld_cache_p->load_map_addr,
241 sizeof (dld_cache_p->load_map_addr))
242 != 0)
243 {
244 error (_("Error while reading in .dynamic section of the program."));
245 }
246 }
247 else
248 {
249 /* tag is not of interest */
250 }
251 }
252
253 /* Record other information and set is_valid to 1. */
254 dld_cache_p->dyninfo_sect = dyninfo_sect;
255
256 /* Verify that we read in required info. These fields are re-set to zero
257 in pa64_solib_restart. */
258
259 if (dld_cache_p->dld_flags_addr != 0 && dld_cache_p->load_map_addr != 0)
260 dld_cache_p->is_valid = 1;
261 else
262 return 0;
263
264 return 1;
265 }
266
267 /*
268 bfd_lookup_symbol -- lookup the value for a specific symbol
269
270 An expensive way to lookup the value of a single symbol for
271 bfd's that are only temporary anyway. This is used by the
272 shared library support to find the address of the debugger
273 interface structures in the shared library.
274
275 Note that 0 is specifically allowed as an error return (no
276 such symbol).
277 */
278
279 static CORE_ADDR
280 bfd_lookup_symbol (bfd *abfd, char *symname)
281 {
282 unsigned int storage_needed;
283 asymbol *sym;
284 asymbol **symbol_table;
285 unsigned int number_of_symbols;
286 unsigned int i;
287 struct cleanup *back_to;
288 CORE_ADDR symaddr = 0;
289
290 storage_needed = bfd_get_symtab_upper_bound (abfd);
291
292 if (storage_needed > 0)
293 {
294 symbol_table = (asymbol **) xmalloc (storage_needed);
295 back_to = make_cleanup (xfree, symbol_table);
296 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
297
298 for (i = 0; i < number_of_symbols; i++)
299 {
300 sym = *symbol_table++;
301 if (strcmp (sym->name, symname) == 0)
302 {
303 /* Bfd symbols are section relative. */
304 symaddr = sym->value + sym->section->vma;
305 break;
306 }
307 }
308 do_cleanups (back_to);
309 }
310 return (symaddr);
311 }
312
313
314 /* This hook gets called just before the first instruction in the
315 inferior process is executed.
316
317 This is our opportunity to set magic flags in the inferior so
318 that GDB can be notified when a shared library is mapped in and
319 to tell the dynamic linker that a private copy of the library is
320 needed (so GDB can set breakpoints in the library).
321
322 We need to set two flag bits in this routine.
323
324 DT_HP_DEBUG_PRIVATE to indicate that shared libraries should be
325 mapped private.
326
327 DT_HP_DEBUG_CALLBACK to indicate that we want the dynamic linker to
328 call the breakpoint routine for significant events. */
329
330 static void
331 pa64_solib_create_inferior_hook (void)
332 {
333 struct minimal_symbol *msymbol;
334 unsigned int dld_flags, status;
335 asection *shlib_info, *interp_sect;
336 char buf[4];
337 struct objfile *objfile;
338 CORE_ADDR anaddr;
339
340 /* First, remove all the solib event breakpoints. Their addresses
341 may have changed since the last time we ran the program. */
342 remove_solib_event_breakpoints ();
343
344 if (symfile_objfile == NULL)
345 return;
346
347 /* First see if the objfile was dynamically linked. */
348 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, ".dynamic");
349 if (!shlib_info)
350 return;
351
352 /* It's got a .dynamic section, make sure it's not empty. */
353 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
354 return;
355
356 /* Read in the .dynamic section. */
357 if (! read_dynamic_info (shlib_info, &dld_cache))
358 error (_("Unable to read the .dynamic section."));
359
360 /* Turn on the flags we care about. */
361 dld_cache.dld_flags |= DT_HP_DEBUG_PRIVATE;
362 dld_cache.dld_flags |= DT_HP_DEBUG_CALLBACK;
363 status = target_write_memory (dld_cache.dld_flags_addr,
364 (char *) &dld_cache.dld_flags,
365 sizeof (dld_cache.dld_flags));
366 if (status != 0)
367 error (_("Unable to modify dynamic linker flags."));
368
369 /* Now we have to create a shared library breakpoint in the dynamic
370 linker. This can be somewhat tricky since the symbol is inside
371 the dynamic linker (for which we do not have symbols or a base
372 load address! Luckily I wrote this code for solib.c years ago. */
373 interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
374 if (interp_sect)
375 {
376 unsigned int interp_sect_size;
377 char *buf;
378 CORE_ADDR load_addr;
379 bfd *tmp_bfd;
380 CORE_ADDR sym_addr = 0;
381
382 /* Read the contents of the .interp section into a local buffer;
383 the contents specify the dynamic linker this program uses. */
384 interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
385 buf = alloca (interp_sect_size);
386 bfd_get_section_contents (exec_bfd, interp_sect,
387 buf, 0, interp_sect_size);
388
389 /* Now we need to figure out where the dynamic linker was
390 loaded so that we can load its symbols and place a breakpoint
391 in the dynamic linker itself.
392
393 This address is stored on the stack. However, I've been unable
394 to find any magic formula to find it for Solaris (appears to
395 be trivial on GNU/Linux). Therefore, we have to try an alternate
396 mechanism to find the dynamic linker's base address. */
397 tmp_bfd = bfd_openr (buf, gnutarget);
398 if (tmp_bfd == NULL)
399 return;
400
401 /* Make sure the dynamic linker's really a useful object. */
402 if (!bfd_check_format (tmp_bfd, bfd_object))
403 {
404 warning (_("Unable to grok dynamic linker %s as an object file"), buf);
405 bfd_close (tmp_bfd);
406 return;
407 }
408
409 /* We find the dynamic linker's base address by examining the
410 current pc (which point at the entry point for the dynamic
411 linker) and subtracting the offset of the entry point.
412
413 Also note the breakpoint is the second instruction in the
414 routine. */
415 load_addr = read_pc () - tmp_bfd->start_address;
416 sym_addr = bfd_lookup_symbol (tmp_bfd, "__dld_break");
417 sym_addr = load_addr + sym_addr + 4;
418
419 /* Create the shared library breakpoint. */
420 {
421 struct breakpoint *b
422 = create_solib_event_breakpoint (sym_addr);
423
424 /* The breakpoint is actually hard-coded into the dynamic linker,
425 so we don't need to actually insert a breakpoint instruction
426 there. In fact, the dynamic linker's code is immutable, even to
427 ttrace, so we shouldn't even try to do that. For cases like
428 this, we have "permanent" breakpoints. */
429 make_breakpoint_permanent (b);
430 }
431
432 /* We're done with the temporary bfd. */
433 bfd_close (tmp_bfd);
434 }
435 }
436
437 static void
438 pa64_special_symbol_handling (void)
439 {
440 }
441
442 static struct so_list *
443 pa64_current_sos (void)
444 {
445 struct so_list *head = 0;
446 struct so_list **link_ptr = &head;
447 int dll_index;
448
449 /* Read in the load map pointer if we have not done so already. */
450 if (! dld_cache.have_read_dld_descriptor)
451 if (! read_dld_descriptor ())
452 return NULL;
453
454 /* If the libraries were not mapped private, warn the user. */
455 if ((dld_cache.dld_flags & DT_HP_DEBUG_PRIVATE) == 0)
456 warning (_("The shared libraries were not privately mapped; setting a\n"
457 "breakpoint in a shared library will not work until you rerun "
458 "the program.\n"));
459
460 for (dll_index = -1; ; dll_index++)
461 {
462 struct load_module_desc dll_desc;
463 char *dll_path;
464 struct so_list *new;
465 struct cleanup *old_chain;
466
467 if (dll_index == 0)
468 continue;
469
470 /* Read in the load module descriptor. */
471 if (dlgetmodinfo (dll_index, &dll_desc, sizeof (dll_desc),
472 pa64_target_read_memory, 0, dld_cache.load_map)
473 == 0)
474 break;
475
476 /* Get the name of the shared library. */
477 dll_path = (char *)dlgetname (&dll_desc, sizeof (dll_desc),
478 pa64_target_read_memory,
479 0, dld_cache.load_map);
480
481 new = (struct so_list *) xmalloc (sizeof (struct so_list));
482 memset (new, 0, sizeof (struct so_list));
483 new->lm_info = (struct lm_info *) xmalloc (sizeof (struct lm_info));
484 memset (new->lm_info, 0, sizeof (struct lm_info));
485
486 strncpy (new->so_name, dll_path, SO_NAME_MAX_PATH_SIZE - 1);
487 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
488 strcpy (new->so_original_name, new->so_name);
489
490 memcpy (&new->lm_info->desc, &dll_desc, sizeof (dll_desc));
491
492 #ifdef SOLIB_PA64_DBG
493 {
494 struct load_module_desc *d = &new->lm_info->desc;
495 printf ("\n+ library \"%s\" is described at index %d\n", new->so_name,
496 dll_index);
497 printf (" text_base = 0x%llx\n", d->text_base);
498 printf (" text_size = 0x%llx\n", d->text_size);
499 printf (" data_base = 0x%llx\n", d->data_base);
500 printf (" data_size = 0x%llx\n", d->data_size);
501 printf (" unwind_base = 0x%llx\n", d->unwind_base);
502 printf (" linkage_ptr = 0x%llx\n", d->linkage_ptr);
503 printf (" phdr_base = 0x%llx\n", d->phdr_base);
504 printf (" tls_size = 0x%llx\n", d->tls_size);
505 printf (" tls_start_addr = 0x%llx\n", d->tls_start_addr);
506 printf (" unwind_size = 0x%llx\n", d->unwind_size);
507 printf (" tls_index = 0x%llx\n", d->tls_index);
508 }
509 #endif
510
511 /* Link the new object onto the list. */
512 new->next = NULL;
513 *link_ptr = new;
514 link_ptr = &new->next;
515 }
516
517 return head;
518 }
519
520 static int
521 pa64_open_symbol_file_object (void *from_ttyp)
522 {
523 int from_tty = *(int *)from_ttyp;
524 char buf[4];
525 struct load_module_desc dll_desc;
526 char *dll_path;
527
528 if (symfile_objfile)
529 if (!query ("Attempt to reload symbols from process? "))
530 return 0;
531
532 /* Read in the load map pointer if we have not done so already. */
533 if (! dld_cache.have_read_dld_descriptor)
534 if (! read_dld_descriptor ())
535 return 0;
536
537 /* Read in the load module descriptor. */
538 if (dlgetmodinfo (0, &dll_desc, sizeof (dll_desc),
539 pa64_target_read_memory, 0, dld_cache.load_map) == 0)
540 return 0;
541
542 /* Get the name of the shared library. */
543 dll_path = (char *)dlgetname (&dll_desc, sizeof (dll_desc),
544 pa64_target_read_memory,
545 0, dld_cache.load_map);
546
547 /* Have a pathname: read the symbol file. */
548 symbol_file_add_main (dll_path, from_tty);
549
550 return 1;
551 }
552
553 /* Return nonzero if PC is an address inside the dynamic linker. */
554 static int
555 pa64_in_dynsym_resolve_code (CORE_ADDR pc)
556 {
557 asection *shlib_info;
558
559 if (symfile_objfile == NULL)
560 return 0;
561
562 if (!dld_cache.have_read_dld_descriptor)
563 if (!read_dld_descriptor ())
564 return 0;
565
566 return (pc >= dld_cache.dld_desc.text_base
567 && pc < dld_cache.dld_desc.text_base + dld_cache.dld_desc.text_size);
568 }
569
570
571 /* Return the GOT value for the shared library in which ADDR belongs. If
572 ADDR isn't in any known shared library, return zero. */
573
574 static CORE_ADDR
575 pa64_solib_get_got_by_pc (CORE_ADDR addr)
576 {
577 struct so_list *so_list = master_so_list ();
578 CORE_ADDR got_value = 0;
579
580 while (so_list)
581 {
582 if (so_list->lm_info->desc.text_base <= addr
583 && ((so_list->lm_info->desc.text_base
584 + so_list->lm_info->desc.text_size)
585 > addr))
586 {
587 got_value = so_list->lm_info->desc.linkage_ptr;
588 break;
589 }
590 so_list = so_list->next;
591 }
592 return got_value;
593 }
594
595 /* Get some HPUX-specific data from a shared lib. */
596 static CORE_ADDR
597 pa64_solib_thread_start_addr (struct so_list *so)
598 {
599 return so->lm_info->desc.tls_start_addr;
600 }
601
602
603 /* Return the address of the handle of the shared library in which ADDR
604 belongs. If ADDR isn't in any known shared library, return zero. */
605
606 static CORE_ADDR
607 pa64_solib_get_solib_by_pc (CORE_ADDR addr)
608 {
609 struct so_list *so_list = master_so_list ();
610 CORE_ADDR retval = 0;
611
612 while (so_list)
613 {
614 if (so_list->lm_info->desc.text_base <= addr
615 && ((so_list->lm_info->desc.text_base
616 + so_list->lm_info->desc.text_size)
617 > addr))
618 {
619 retval = so_list->lm_info->desc_addr;
620 break;
621 }
622 so_list = so_list->next;
623 }
624 return retval;
625 }
626
627 /* pa64 libraries do not seem to set the section offsets in a standard (i.e.
628 SVr4) way; the text section offset stored in the file doesn't correspond
629 to the place where the library is actually loaded into memory. Instead,
630 we rely on the dll descriptor to tell us where things were loaded. */
631 static CORE_ADDR
632 pa64_solib_get_text_base (struct objfile *objfile)
633 {
634 struct so_list *so;
635
636 for (so = master_so_list (); so; so = so->next)
637 if (so->objfile == objfile)
638 return so->lm_info->desc.text_base;
639
640 return 0;
641 }
642
643 static struct target_so_ops pa64_so_ops;
644
645 extern initialize_file_ftype _initialize_pa64_solib; /* -Wmissing-prototypes */
646
647 void
648 _initialize_pa64_solib (void)
649 {
650 pa64_so_ops.relocate_section_addresses = pa64_relocate_section_addresses;
651 pa64_so_ops.free_so = pa64_free_so;
652 pa64_so_ops.clear_solib = pa64_clear_solib;
653 pa64_so_ops.solib_create_inferior_hook = pa64_solib_create_inferior_hook;
654 pa64_so_ops.special_symbol_handling = pa64_special_symbol_handling;
655 pa64_so_ops.current_sos = pa64_current_sos;
656 pa64_so_ops.open_symbol_file_object = pa64_open_symbol_file_object;
657 pa64_so_ops.in_dynsym_resolve_code = pa64_in_dynsym_resolve_code;
658
659 memset (&dld_cache, 0, sizeof (dld_cache));
660 }
661
662 void pa64_solib_select (struct gdbarch_tdep *tdep)
663 {
664 current_target_so_ops = &pa64_so_ops;
665
666 tdep->solib_thread_start_addr = pa64_solib_thread_start_addr;
667 tdep->solib_get_got_by_pc = pa64_solib_get_got_by_pc;
668 tdep->solib_get_solib_by_pc = pa64_solib_get_solib_by_pc;
669 tdep->solib_get_text_base = pa64_solib_get_text_base;
670 }
671
672 #else /* PA_SOM_ONLY */
673
674 extern initialize_file_ftype _initialize_pa64_solib; /* -Wmissing-prototypes */
675
676 void
677 _initialize_pa64_solib (void)
678 {
679 }
680
681 void pa64_solib_select (struct gdbarch_tdep *tdep)
682 {
683 /* For a SOM-only target, there is no pa64 solib support. This is needed
684 for hppa-hpux-tdep.c to build. */
685 error (_("Cannot select pa64 solib support for this configuration."));
686 }
687 #endif
This page took 0.044744 seconds and 4 git commands to generate.