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