1 /* Handle PA64 shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 HP in their infinite stupidity choose not to use standard ELF dynamic
22 linker interfaces. They also choose not to make their ELF dymamic
23 linker interfaces compatible with the SOM dynamic linker. The
24 net result is we can not use either of the existing somsolib.c or
25 solib.c. What a crock.
27 Even more disgusting. This file depends on functions provided only
28 in certain PA64 libraries. Thus this file is supposed to only be
29 used native. When will HP ever learn that they need to provide the
30 same functionality in all their libraries! */
42 #include "hppa-tdep.h"
45 #include "solib-pa64.h"
49 /* We can build this file only when running natively on 64-bit HP/UX.
50 We check for that by checking for the elf_hp.h header file. */
51 #if defined(HAVE_ELF_HP_H) && defined(__LP64__)
53 /* FIXME: kettenis/20041213: These includes should be eliminated. */
59 struct load_module_desc desc
;
63 /* When adding fields, be sure to clear them in _initialize_pa64_solib. */
66 CORE_ADDR dld_flags_addr
;
68 struct bfd_section
*dyninfo_sect
;
69 int have_read_dld_descriptor
;
72 CORE_ADDR load_map_addr
;
73 struct load_module_desc dld_desc
;
77 static dld_cache_t dld_cache
;
80 read_dynamic_info (asection
*dyninfo_sect
, dld_cache_t
*dld_cache_p
);
83 pa64_relocate_section_addresses (struct so_list
*so
,
84 struct target_section
*sec
)
86 asection
*asec
= sec
->the_bfd_section
;
87 CORE_ADDR load_offset
;
89 /* Relocate all the sections based on where they got loaded. */
91 load_offset
= bfd_section_vma (so
->abfd
, asec
) - asec
->filepos
;
93 if (asec
->flags
& SEC_CODE
)
95 sec
->addr
+= so
->lm_info
->desc
.text_base
- load_offset
;
96 sec
->endaddr
+= so
->lm_info
->desc
.text_base
- load_offset
;
98 else if (asec
->flags
& SEC_DATA
)
100 sec
->addr
+= so
->lm_info
->desc
.data_base
- load_offset
;
101 sec
->endaddr
+= so
->lm_info
->desc
.data_base
- load_offset
;
106 pa64_free_so (struct so_list
*so
)
112 pa64_clear_solib (void)
116 /* Wrapper for target_read_memory for dlgetmodinfo. */
119 pa64_target_read_memory (void *buffer
, CORE_ADDR ptr
, size_t bufsiz
, int ident
)
121 if (target_read_memory (ptr
, buffer
, bufsiz
) != 0)
126 /* Read the dynamic linker's internal shared library descriptor.
128 This must happen after dld starts running, so we can't do it in
129 read_dynamic_info. Record the fact that we have loaded the
130 descriptor. If the library is archive bound or the load map
131 hasn't been setup, then return zero; else return nonzero. */
134 read_dld_descriptor (void)
137 asection
*dyninfo_sect
;
139 /* If necessary call read_dynamic_info to extract the contents of the
140 .dynamic section from the shared library. */
141 if (!dld_cache
.is_valid
)
143 if (symfile_objfile
== NULL
)
144 error (_("No object file symbols."));
146 dyninfo_sect
= bfd_get_section_by_name (symfile_objfile
->obfd
,
153 if (!read_dynamic_info (dyninfo_sect
, &dld_cache
))
154 error (_("Unable to read in .dynamic section information."));
157 /* Read the load map pointer. */
158 if (target_read_memory (dld_cache
.load_map_addr
,
159 (char *) &dld_cache
.load_map
,
160 sizeof (dld_cache
.load_map
))
163 error (_("Error while reading in load map pointer."));
166 if (!dld_cache
.load_map
)
169 /* Read in the dld load module descriptor */
170 if (dlgetmodinfo (-1,
172 sizeof (dld_cache
.dld_desc
),
173 pa64_target_read_memory
,
178 error (_("Error trying to get information about dynamic linker."));
181 /* Indicate that we have loaded the dld descriptor. */
182 dld_cache
.have_read_dld_descriptor
= 1;
188 /* Read the .dynamic section and extract the information of interest,
189 which is stored in dld_cache. The routine elf_locate_base in solib.c
190 was used as a model for this. */
193 read_dynamic_info (asection
*dyninfo_sect
, dld_cache_t
*dld_cache_p
)
197 CORE_ADDR dyninfo_addr
;
198 int dyninfo_sect_size
;
199 CORE_ADDR entry_addr
;
201 /* Read in .dynamic section, silently ignore errors. */
202 dyninfo_addr
= bfd_section_vma (symfile_objfile
->obfd
, dyninfo_sect
);
203 dyninfo_sect_size
= bfd_section_size (exec_bfd
, dyninfo_sect
);
204 buf
= alloca (dyninfo_sect_size
);
205 if (target_read_memory (dyninfo_addr
, buf
, dyninfo_sect_size
))
208 /* Scan the .dynamic section and record the items of interest.
209 In particular, DT_HP_DLD_FLAGS */
210 for (bufend
= buf
+ dyninfo_sect_size
, entry_addr
= dyninfo_addr
;
212 buf
+= sizeof (Elf64_Dyn
), entry_addr
+= sizeof (Elf64_Dyn
))
214 Elf64_Dyn
*x_dynp
= (Elf64_Dyn
*)buf
;
215 Elf64_Sxword dyn_tag
;
218 dyn_tag
= bfd_h_get_64 (symfile_objfile
->obfd
,
219 (bfd_byte
*) &x_dynp
->d_tag
);
221 /* We can't use a switch here because dyn_tag is 64 bits and HP's
222 lame comiler does not handle 64bit items in switch statements. */
223 if (dyn_tag
== DT_NULL
)
225 else if (dyn_tag
== DT_HP_DLD_FLAGS
)
227 /* Set dld_flags_addr and dld_flags in *dld_cache_p */
228 dld_cache_p
->dld_flags_addr
= entry_addr
+ offsetof(Elf64_Dyn
, d_un
);
229 if (target_read_memory (dld_cache_p
->dld_flags_addr
,
230 (char*) &dld_cache_p
->dld_flags
,
231 sizeof (dld_cache_p
->dld_flags
))
234 error (_("Error while reading in "
235 ".dynamic section of the program."));
238 else if (dyn_tag
== DT_HP_LOAD_MAP
)
240 /* Dld will place the address of the load map at load_map_addr
241 after it starts running. */
242 if (target_read_memory (entry_addr
+ offsetof(Elf64_Dyn
,
244 (char*) &dld_cache_p
->load_map_addr
,
245 sizeof (dld_cache_p
->load_map_addr
))
248 error (_("Error while reading in "
249 ".dynamic section of the program."));
254 /* tag is not of interest */
258 /* Record other information and set is_valid to 1. */
259 dld_cache_p
->dyninfo_sect
= dyninfo_sect
;
261 /* Verify that we read in required info. These fields are re-set to zero
262 in pa64_solib_restart. */
264 if (dld_cache_p
->dld_flags_addr
!= 0 && dld_cache_p
->load_map_addr
!= 0)
265 dld_cache_p
->is_valid
= 1;
273 bfd_lookup_symbol -- lookup the value for a specific symbol
275 An expensive way to lookup the value of a single symbol for
276 bfd's that are only temporary anyway. This is used by the
277 shared library support to find the address of the debugger
278 interface structures in the shared library.
280 Note that 0 is specifically allowed as an error return (no
285 bfd_lookup_symbol (bfd
*abfd
, char *symname
)
287 unsigned int storage_needed
;
289 asymbol
**symbol_table
;
290 unsigned int number_of_symbols
;
292 struct cleanup
*back_to
;
293 CORE_ADDR symaddr
= 0;
295 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
297 if (storage_needed
> 0)
299 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
300 back_to
= make_cleanup (xfree
, symbol_table
);
301 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
303 for (i
= 0; i
< number_of_symbols
; i
++)
305 sym
= *symbol_table
++;
306 if (strcmp (sym
->name
, symname
) == 0)
308 /* Bfd symbols are section relative. */
309 symaddr
= sym
->value
+ sym
->section
->vma
;
313 do_cleanups (back_to
);
319 /* This hook gets called just before the first instruction in the
320 inferior process is executed.
322 This is our opportunity to set magic flags in the inferior so
323 that GDB can be notified when a shared library is mapped in and
324 to tell the dynamic linker that a private copy of the library is
325 needed (so GDB can set breakpoints in the library).
327 We need to set DT_HP_DEBUG_CALLBACK to indicate that we want the
328 dynamic linker to call the breakpoint routine for significant events.
329 We used to set DT_HP_DEBUG_PRIVATE to indicate that shared libraries
330 should be mapped private. However, this flag can be set using
331 "chatr +dbg enable". Not setting DT_HP_DEBUG_PRIVATE allows debugging
332 with shared libraries mapped shareable. */
335 pa64_solib_create_inferior_hook (int from_tty
)
337 struct minimal_symbol
*msymbol
;
338 unsigned int dld_flags
, status
;
339 asection
*shlib_info
, *interp_sect
;
341 struct objfile
*objfile
;
344 if (symfile_objfile
== NULL
)
347 /* First see if the objfile was dynamically linked. */
348 shlib_info
= bfd_get_section_by_name (symfile_objfile
->obfd
, ".dynamic");
352 /* It's got a .dynamic section, make sure it's not empty. */
353 if (bfd_section_size (symfile_objfile
->obfd
, shlib_info
) == 0)
356 /* Read in the .dynamic section. */
357 if (! read_dynamic_info (shlib_info
, &dld_cache
))
358 error (_("Unable to read the .dynamic section."));
360 /* If the libraries were not mapped private, warn the user. */
361 if ((dld_cache
.dld_flags
& DT_HP_DEBUG_PRIVATE
) == 0)
364 Private mapping of shared library text was not specified\n\
365 by the executable; setting a breakpoint in a shared library which\n\
366 is not privately mapped will not work. See the HP-UX 11i v3 chatr\n\
367 manpage for methods to privately map shared library text."));
369 /* Turn on the flags we care about. */
370 dld_cache
.dld_flags
|= DT_HP_DEBUG_CALLBACK
;
371 status
= target_write_memory (dld_cache
.dld_flags_addr
,
372 (char *) &dld_cache
.dld_flags
,
373 sizeof (dld_cache
.dld_flags
));
375 error (_("Unable to modify dynamic linker flags."));
377 /* Now we have to create a shared library breakpoint in the dynamic
378 linker. This can be somewhat tricky since the symbol is inside
379 the dynamic linker (for which we do not have symbols or a base
380 load address! Luckily I wrote this code for solib.c years ago. */
381 interp_sect
= bfd_get_section_by_name (exec_bfd
, ".interp");
384 unsigned int interp_sect_size
;
388 CORE_ADDR sym_addr
= 0;
390 /* Read the contents of the .interp section into a local buffer;
391 the contents specify the dynamic linker this program uses. */
392 interp_sect_size
= bfd_section_size (exec_bfd
, interp_sect
);
393 buf
= alloca (interp_sect_size
);
394 bfd_get_section_contents (exec_bfd
, interp_sect
,
395 buf
, 0, interp_sect_size
);
397 /* Now we need to figure out where the dynamic linker was
398 loaded so that we can load its symbols and place a breakpoint
399 in the dynamic linker itself.
401 This address is stored on the stack. However, I've been unable
402 to find any magic formula to find it for Solaris (appears to
403 be trivial on GNU/Linux). Therefore, we have to try an alternate
404 mechanism to find the dynamic linker's base address. */
405 tmp_bfd
= bfd_openr (buf
, gnutarget
);
409 /* Make sure the dynamic linker's really a useful object. */
410 if (!bfd_check_format (tmp_bfd
, bfd_object
))
412 warning (_("Unable to grok dynamic linker %s as an object file"),
418 /* We find the dynamic linker's base address by examining the
419 current pc (which point at the entry point for the dynamic
420 linker) and subtracting the offset of the entry point.
422 Also note the breakpoint is the second instruction in the
424 load_addr
= regcache_read_pc (get_current_regcache ())
425 - tmp_bfd
->start_address
;
426 sym_addr
= bfd_lookup_symbol (tmp_bfd
, "__dld_break");
427 sym_addr
= load_addr
+ sym_addr
+ 4;
429 /* Create the shared library breakpoint. */
432 = create_solib_event_breakpoint (target_gdbarch
, sym_addr
);
434 /* The breakpoint is actually hard-coded into the dynamic linker,
435 so we don't need to actually insert a breakpoint instruction
436 there. In fact, the dynamic linker's code is immutable, even to
437 ttrace, so we shouldn't even try to do that. For cases like
438 this, we have "permanent" breakpoints. */
439 make_breakpoint_permanent (b
);
442 /* We're done with the temporary bfd. */
448 pa64_special_symbol_handling (void)
452 static struct so_list
*
453 pa64_current_sos (void)
455 struct so_list
*head
= 0;
456 struct so_list
**link_ptr
= &head
;
459 /* Read in the load map pointer if we have not done so already. */
460 if (! dld_cache
.have_read_dld_descriptor
)
461 if (! read_dld_descriptor ())
464 for (dll_index
= -1; ; dll_index
++)
466 struct load_module_desc dll_desc
;
469 struct cleanup
*old_chain
;
474 /* Read in the load module descriptor. */
475 if (dlgetmodinfo (dll_index
, &dll_desc
, sizeof (dll_desc
),
476 pa64_target_read_memory
, 0, dld_cache
.load_map
)
480 /* Get the name of the shared library. */
481 dll_path
= (char *)dlgetname (&dll_desc
, sizeof (dll_desc
),
482 pa64_target_read_memory
,
483 0, dld_cache
.load_map
);
485 new = (struct so_list
*) xmalloc (sizeof (struct so_list
));
486 memset (new, 0, sizeof (struct so_list
));
487 new->lm_info
= (struct lm_info
*) xmalloc (sizeof (struct lm_info
));
488 memset (new->lm_info
, 0, sizeof (struct lm_info
));
490 strncpy (new->so_name
, dll_path
, SO_NAME_MAX_PATH_SIZE
- 1);
491 new->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
492 strcpy (new->so_original_name
, new->so_name
);
494 memcpy (&new->lm_info
->desc
, &dll_desc
, sizeof (dll_desc
));
496 #ifdef SOLIB_PA64_DBG
498 struct load_module_desc
*d
= &new->lm_info
->desc
;
500 printf ("\n+ library \"%s\" is described at index %d\n", new->so_name
,
502 printf (" text_base = %s\n", hex_string (d
->text_base
));
503 printf (" text_size = %s\n", hex_string (d
->text_size
));
504 printf (" data_base = %s\n", hex_string (d
->data_base
));
505 printf (" data_size = %s\n", hex_string (d
->data_size
));
506 printf (" unwind_base = %s\n", hex_string (d
->unwind_base
));
507 printf (" linkage_ptr = %s\n", hex_string (d
->linkage_ptr
));
508 printf (" phdr_base = %s\n", hex_string (d
->phdr_base
));
509 printf (" tls_size = %s\n", hex_string (d
->tls_size
));
510 printf (" tls_start_addr = %s\n", hex_string (d
->tls_start_addr
));
511 printf (" unwind_size = %s\n", hex_string (d
->unwind_size
));
512 printf (" tls_index = %s\n", hex_string (d
->tls_index
));
516 /* Link the new object onto the list. */
519 link_ptr
= &new->next
;
526 pa64_open_symbol_file_object (void *from_ttyp
)
528 int from_tty
= *(int *)from_ttyp
;
530 struct load_module_desc dll_desc
;
534 if (!query (_("Attempt to reload symbols from process? ")))
537 /* Read in the load map pointer if we have not done so already. */
538 if (! dld_cache
.have_read_dld_descriptor
)
539 if (! read_dld_descriptor ())
542 /* Read in the load module descriptor. */
543 if (dlgetmodinfo (0, &dll_desc
, sizeof (dll_desc
),
544 pa64_target_read_memory
, 0, dld_cache
.load_map
) == 0)
547 /* Get the name of the shared library. */
548 dll_path
= (char *)dlgetname (&dll_desc
, sizeof (dll_desc
),
549 pa64_target_read_memory
,
550 0, dld_cache
.load_map
);
552 /* Have a pathname: read the symbol file. */
553 symbol_file_add_main (dll_path
, from_tty
);
558 /* Return nonzero if PC is an address inside the dynamic linker. */
560 pa64_in_dynsym_resolve_code (CORE_ADDR pc
)
562 asection
*shlib_info
;
564 if (symfile_objfile
== NULL
)
567 if (!dld_cache
.have_read_dld_descriptor
)
568 if (!read_dld_descriptor ())
571 return (pc
>= dld_cache
.dld_desc
.text_base
572 && pc
< dld_cache
.dld_desc
.text_base
+ dld_cache
.dld_desc
.text_size
);
576 /* Return the GOT value for the shared library in which ADDR belongs. If
577 ADDR isn't in any known shared library, return zero. */
580 pa64_solib_get_got_by_pc (CORE_ADDR addr
)
582 struct so_list
*so_list
= master_so_list ();
583 CORE_ADDR got_value
= 0;
587 if (so_list
->lm_info
->desc
.text_base
<= addr
588 && ((so_list
->lm_info
->desc
.text_base
589 + so_list
->lm_info
->desc
.text_size
)
592 got_value
= so_list
->lm_info
->desc
.linkage_ptr
;
595 so_list
= so_list
->next
;
600 /* Get some HPUX-specific data from a shared lib. */
602 pa64_solib_thread_start_addr (struct so_list
*so
)
604 return so
->lm_info
->desc
.tls_start_addr
;
608 /* Return the address of the handle of the shared library in which ADDR
609 belongs. If ADDR isn't in any known shared library, return zero. */
612 pa64_solib_get_solib_by_pc (CORE_ADDR addr
)
614 struct so_list
*so_list
= master_so_list ();
615 CORE_ADDR retval
= 0;
619 if (so_list
->lm_info
->desc
.text_base
<= addr
620 && ((so_list
->lm_info
->desc
.text_base
621 + so_list
->lm_info
->desc
.text_size
)
624 retval
= so_list
->lm_info
->desc_addr
;
627 so_list
= so_list
->next
;
632 /* pa64 libraries do not seem to set the section offsets in a standard (i.e.
633 SVr4) way; the text section offset stored in the file doesn't correspond
634 to the place where the library is actually loaded into memory. Instead,
635 we rely on the dll descriptor to tell us where things were loaded. */
637 pa64_solib_get_text_base (struct objfile
*objfile
)
641 for (so
= master_so_list (); so
; so
= so
->next
)
642 if (so
->objfile
== objfile
)
643 return so
->lm_info
->desc
.text_base
;
648 static struct target_so_ops pa64_so_ops
;
650 extern initialize_file_ftype _initialize_pa64_solib
; /* -Wmissing-prototypes */
653 _initialize_pa64_solib (void)
655 pa64_so_ops
.relocate_section_addresses
= pa64_relocate_section_addresses
;
656 pa64_so_ops
.free_so
= pa64_free_so
;
657 pa64_so_ops
.clear_solib
= pa64_clear_solib
;
658 pa64_so_ops
.solib_create_inferior_hook
= pa64_solib_create_inferior_hook
;
659 pa64_so_ops
.special_symbol_handling
= pa64_special_symbol_handling
;
660 pa64_so_ops
.current_sos
= pa64_current_sos
;
661 pa64_so_ops
.open_symbol_file_object
= pa64_open_symbol_file_object
;
662 pa64_so_ops
.in_dynsym_resolve_code
= pa64_in_dynsym_resolve_code
;
663 pa64_so_ops
.bfd_open
= solib_bfd_open
;
665 memset (&dld_cache
, 0, sizeof (dld_cache
));
668 void pa64_solib_select (struct gdbarch
*gdbarch
)
670 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
672 set_solib_ops (gdbarch
, &pa64_so_ops
);
673 tdep
->solib_thread_start_addr
= pa64_solib_thread_start_addr
;
674 tdep
->solib_get_got_by_pc
= pa64_solib_get_got_by_pc
;
675 tdep
->solib_get_solib_by_pc
= pa64_solib_get_solib_by_pc
;
676 tdep
->solib_get_text_base
= pa64_solib_get_text_base
;
679 #else /* HAVE_ELF_HP_H */
681 extern initialize_file_ftype _initialize_pa64_solib
; /* -Wmissing-prototypes */
684 _initialize_pa64_solib (void)
688 void pa64_solib_select (struct gdbarch
*gdbarch
)
690 /* For a SOM-only target, there is no pa64 solib support. This is needed
691 for hppa-hpux-tdep.c to build. */
692 error (_("Cannot select pa64 solib support for this configuration."));