OSDN Git Service

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