* hppa-hpux-tdep.c (hppa_hpux_sigtramp_frame_unwind_cache): Fix
[external/binutils.git] / gdb / hppa-hpux-tdep.c
1 /* Target-dependent code for HP-UX on PA-RISC.
2
3    Copyright 2002, 2003, 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 #include "defs.h"
23 #include "arch-utils.h"
24 #include "gdbcore.h"
25 #include "osabi.h"
26 #include "frame.h"
27 #include "frame-unwind.h"
28 #include "trad-frame.h"
29 #include "symtab.h"
30 #include "objfiles.h"
31 #include "inferior.h"
32 #include "infcall.h"
33 #include "observer.h"
34 #include "regset.h"
35
36 #include "gdb_string.h"
37
38 #include <dl.h>
39 #include <machine/save_state.h>
40
41 #include "hppa-tdep.h"
42
43 #ifndef offsetof
44 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
45 #endif
46
47 /* Forward declarations.  */
48 extern void _initialize_hppa_hpux_tdep (void);
49 extern initialize_file_ftype _initialize_hppa_hpux_tdep;
50
51 typedef struct
52   {
53     struct minimal_symbol *msym;
54     CORE_ADDR solib_handle;
55     CORE_ADDR return_val;
56   }
57 args_for_find_stub;
58
59 /* Return one if PC is in the call path of a trampoline, else return zero.
60
61    Note we return one for *any* call trampoline (long-call, arg-reloc), not
62    just shared library trampolines (import, export).  */
63
64 static int
65 hppa32_hpux_in_solib_call_trampoline (CORE_ADDR pc, char *name)
66 {
67   struct minimal_symbol *minsym;
68   struct unwind_table_entry *u;
69
70   /* First see if PC is in one of the two C-library trampolines.  */
71   if (pc == hppa_symbol_address("$$dyncall") 
72       || pc == hppa_symbol_address("_sr4export"))
73     return 1;
74
75   minsym = lookup_minimal_symbol_by_pc (pc);
76   if (minsym && strcmp (DEPRECATED_SYMBOL_NAME (minsym), ".stub") == 0)
77     return 1;
78
79   /* Get the unwind descriptor corresponding to PC, return zero
80      if no unwind was found.  */
81   u = find_unwind_entry (pc);
82   if (!u)
83     return 0;
84
85   /* If this isn't a linker stub, then return now.  */
86   if (u->stub_unwind.stub_type == 0)
87     return 0;
88
89   /* By definition a long-branch stub is a call stub.  */
90   if (u->stub_unwind.stub_type == LONG_BRANCH)
91     return 1;
92
93   /* The call and return path execute the same instructions within
94      an IMPORT stub!  So an IMPORT stub is both a call and return
95      trampoline.  */
96   if (u->stub_unwind.stub_type == IMPORT)
97     return 1;
98
99   /* Parameter relocation stubs always have a call path and may have a
100      return path.  */
101   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
102       || u->stub_unwind.stub_type == EXPORT)
103     {
104       CORE_ADDR addr;
105
106       /* Search forward from the current PC until we hit a branch
107          or the end of the stub.  */
108       for (addr = pc; addr <= u->region_end; addr += 4)
109         {
110           unsigned long insn;
111
112           insn = read_memory_integer (addr, 4);
113
114           /* Does it look like a bl?  If so then it's the call path, if
115              we find a bv or be first, then we're on the return path.  */
116           if ((insn & 0xfc00e000) == 0xe8000000)
117             return 1;
118           else if ((insn & 0xfc00e001) == 0xe800c000
119                    || (insn & 0xfc000000) == 0xe0000000)
120             return 0;
121         }
122
123       /* Should never happen.  */
124       warning ("Unable to find branch in parameter relocation stub.\n");
125       return 0;
126     }
127
128   /* Unknown stub type.  For now, just return zero.  */
129   return 0;
130 }
131
132 static int
133 hppa64_hpux_in_solib_call_trampoline (CORE_ADDR pc, char *name)
134 {
135   /* PA64 has a completely different stub/trampoline scheme.  Is it
136      better?  Maybe.  It's certainly harder to determine with any
137      certainty that we are in a stub because we can not refer to the
138      unwinders to help. 
139
140      The heuristic is simple.  Try to lookup the current PC value in th
141      minimal symbol table.  If that fails, then assume we are not in a
142      stub and return.
143
144      Then see if the PC value falls within the section bounds for the
145      section containing the minimal symbol we found in the first
146      step.  If it does, then assume we are not in a stub and return.
147
148      Finally peek at the instructions to see if they look like a stub.  */
149   struct minimal_symbol *minsym;
150   asection *sec;
151   CORE_ADDR addr;
152   int insn, i;
153
154   minsym = lookup_minimal_symbol_by_pc (pc);
155   if (! minsym)
156     return 0;
157
158   sec = SYMBOL_BFD_SECTION (minsym);
159
160   if (bfd_get_section_vma (sec->owner, sec) <= pc
161       && pc < (bfd_get_section_vma (sec->owner, sec)
162                  + bfd_section_size (sec->owner, sec)))
163       return 0;
164
165   /* We might be in a stub.  Peek at the instructions.  Stubs are 3
166      instructions long. */
167   insn = read_memory_integer (pc, 4);
168
169   /* Find out where we think we are within the stub.  */
170   if ((insn & 0xffffc00e) == 0x53610000)
171     addr = pc;
172   else if ((insn & 0xffffffff) == 0xe820d000)
173     addr = pc - 4;
174   else if ((insn & 0xffffc00e) == 0x537b0000)
175     addr = pc - 8;
176   else
177     return 0;
178
179   /* Now verify each insn in the range looks like a stub instruction.  */
180   insn = read_memory_integer (addr, 4);
181   if ((insn & 0xffffc00e) != 0x53610000)
182     return 0;
183         
184   /* Now verify each insn in the range looks like a stub instruction.  */
185   insn = read_memory_integer (addr + 4, 4);
186   if ((insn & 0xffffffff) != 0xe820d000)
187     return 0;
188     
189   /* Now verify each insn in the range looks like a stub instruction.  */
190   insn = read_memory_integer (addr + 8, 4);
191   if ((insn & 0xffffc00e) != 0x537b0000)
192     return 0;
193
194   /* Looks like a stub.  */
195   return 1;
196 }
197
198 /* Return one if PC is in the return path of a trampoline, else return zero.
199
200    Note we return one for *any* call trampoline (long-call, arg-reloc), not
201    just shared library trampolines (import, export).  */
202
203 static int
204 hppa_hpux_in_solib_return_trampoline (CORE_ADDR pc, char *name)
205 {
206   struct unwind_table_entry *u;
207
208   /* Get the unwind descriptor corresponding to PC, return zero
209      if no unwind was found.  */
210   u = find_unwind_entry (pc);
211   if (!u)
212     return 0;
213
214   /* If this isn't a linker stub or it's just a long branch stub, then
215      return zero.  */
216   if (u->stub_unwind.stub_type == 0 || u->stub_unwind.stub_type == LONG_BRANCH)
217     return 0;
218
219   /* The call and return path execute the same instructions within
220      an IMPORT stub!  So an IMPORT stub is both a call and return
221      trampoline.  */
222   if (u->stub_unwind.stub_type == IMPORT)
223     return 1;
224
225   /* Parameter relocation stubs always have a call path and may have a
226      return path.  */
227   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
228       || u->stub_unwind.stub_type == EXPORT)
229     {
230       CORE_ADDR addr;
231
232       /* Search forward from the current PC until we hit a branch
233          or the end of the stub.  */
234       for (addr = pc; addr <= u->region_end; addr += 4)
235         {
236           unsigned long insn;
237
238           insn = read_memory_integer (addr, 4);
239
240           /* Does it look like a bl?  If so then it's the call path, if
241              we find a bv or be first, then we're on the return path.  */
242           if ((insn & 0xfc00e000) == 0xe8000000)
243             return 0;
244           else if ((insn & 0xfc00e001) == 0xe800c000
245                    || (insn & 0xfc000000) == 0xe0000000)
246             return 1;
247         }
248
249       /* Should never happen.  */
250       warning ("Unable to find branch in parameter relocation stub.\n");
251       return 0;
252     }
253
254   /* Unknown stub type.  For now, just return zero.  */
255   return 0;
256
257 }
258
259 /* Figure out if PC is in a trampoline, and if so find out where
260    the trampoline will jump to.  If not in a trampoline, return zero.
261
262    Simple code examination probably is not a good idea since the code
263    sequences in trampolines can also appear in user code.
264
265    We use unwinds and information from the minimal symbol table to
266    determine when we're in a trampoline.  This won't work for ELF
267    (yet) since it doesn't create stub unwind entries.  Whether or
268    not ELF will create stub unwinds or normal unwinds for linker
269    stubs is still being debated.
270
271    This should handle simple calls through dyncall or sr4export,
272    long calls, argument relocation stubs, and dyncall/sr4export
273    calling an argument relocation stub.  It even handles some stubs
274    used in dynamic executables.  */
275
276 static CORE_ADDR
277 hppa_hpux_skip_trampoline_code (CORE_ADDR pc)
278 {
279   long orig_pc = pc;
280   long prev_inst, curr_inst, loc;
281   struct minimal_symbol *msym;
282   struct unwind_table_entry *u;
283
284   /* Addresses passed to dyncall may *NOT* be the actual address
285      of the function.  So we may have to do something special.  */
286   if (pc == hppa_symbol_address("$$dyncall"))
287     {
288       pc = (CORE_ADDR) read_register (22);
289
290       /* If bit 30 (counting from the left) is on, then pc is the address of
291          the PLT entry for this function, not the address of the function
292          itself.  Bit 31 has meaning too, but only for MPE.  */
293       if (pc & 0x2)
294         pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, TARGET_PTR_BIT / 8);
295     }
296   if (pc == hppa_symbol_address("$$dyncall_external"))
297     {
298       pc = (CORE_ADDR) read_register (22);
299       pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, TARGET_PTR_BIT / 8);
300     }
301   else if (pc == hppa_symbol_address("_sr4export"))
302     pc = (CORE_ADDR) (read_register (22));
303
304   /* Get the unwind descriptor corresponding to PC, return zero
305      if no unwind was found.  */
306   u = find_unwind_entry (pc);
307   if (!u)
308     return 0;
309
310   /* If this isn't a linker stub, then return now.  */
311   /* elz: attention here! (FIXME) because of a compiler/linker 
312      error, some stubs which should have a non zero stub_unwind.stub_type 
313      have unfortunately a value of zero. So this function would return here
314      as if we were not in a trampoline. To fix this, we go look at the partial
315      symbol information, which reports this guy as a stub.
316      (FIXME): Unfortunately, we are not that lucky: it turns out that the 
317      partial symbol information is also wrong sometimes. This is because 
318      when it is entered (somread.c::som_symtab_read()) it can happen that
319      if the type of the symbol (from the som) is Entry, and the symbol is
320      in a shared library, then it can also be a trampoline.  This would
321      be OK, except that I believe the way they decide if we are ina shared library
322      does not work. SOOOO..., even if we have a regular function w/o trampolines
323      its minimal symbol can be assigned type mst_solib_trampoline.
324      Also, if we find that the symbol is a real stub, then we fix the unwind
325      descriptor, and define the stub type to be EXPORT.
326      Hopefully this is correct most of the times. */
327   if (u->stub_unwind.stub_type == 0)
328     {
329
330 /* elz: NOTE (FIXME!) once the problem with the unwind information is fixed
331    we can delete all the code which appears between the lines */
332 /*--------------------------------------------------------------------------*/
333       msym = lookup_minimal_symbol_by_pc (pc);
334
335       if (msym == NULL || MSYMBOL_TYPE (msym) != mst_solib_trampoline)
336         return orig_pc == pc ? 0 : pc & ~0x3;
337
338       else if (msym != NULL && MSYMBOL_TYPE (msym) == mst_solib_trampoline)
339         {
340           struct objfile *objfile;
341           struct minimal_symbol *msymbol;
342           int function_found = 0;
343
344           /* go look if there is another minimal symbol with the same name as 
345              this one, but with type mst_text. This would happen if the msym
346              is an actual trampoline, in which case there would be another
347              symbol with the same name corresponding to the real function */
348
349           ALL_MSYMBOLS (objfile, msymbol)
350           {
351             if (MSYMBOL_TYPE (msymbol) == mst_text
352                 && DEPRECATED_STREQ (DEPRECATED_SYMBOL_NAME (msymbol), DEPRECATED_SYMBOL_NAME (msym)))
353               {
354                 function_found = 1;
355                 break;
356               }
357           }
358
359           if (function_found)
360             /* the type of msym is correct (mst_solib_trampoline), but
361                the unwind info is wrong, so set it to the correct value */
362             u->stub_unwind.stub_type = EXPORT;
363           else
364             /* the stub type info in the unwind is correct (this is not a
365                trampoline), but the msym type information is wrong, it
366                should be mst_text. So we need to fix the msym, and also
367                get out of this function */
368             {
369               MSYMBOL_TYPE (msym) = mst_text;
370               return orig_pc == pc ? 0 : pc & ~0x3;
371             }
372         }
373
374 /*--------------------------------------------------------------------------*/
375     }
376
377   /* It's a stub.  Search for a branch and figure out where it goes.
378      Note we have to handle multi insn branch sequences like ldil;ble.
379      Most (all?) other branches can be determined by examining the contents
380      of certain registers and the stack.  */
381
382   loc = pc;
383   curr_inst = 0;
384   prev_inst = 0;
385   while (1)
386     {
387       /* Make sure we haven't walked outside the range of this stub.  */
388       if (u != find_unwind_entry (loc))
389         {
390           warning ("Unable to find branch in linker stub");
391           return orig_pc == pc ? 0 : pc & ~0x3;
392         }
393
394       prev_inst = curr_inst;
395       curr_inst = read_memory_integer (loc, 4);
396
397       /* Does it look like a branch external using %r1?  Then it's the
398          branch from the stub to the actual function.  */
399       if ((curr_inst & 0xffe0e000) == 0xe0202000)
400         {
401           /* Yup.  See if the previous instruction loaded
402              a value into %r1.  If so compute and return the jump address.  */
403           if ((prev_inst & 0xffe00000) == 0x20200000)
404             return (hppa_extract_21 (prev_inst) + hppa_extract_17 (curr_inst)) & ~0x3;
405           else
406             {
407               warning ("Unable to find ldil X,%%r1 before ble Y(%%sr4,%%r1).");
408               return orig_pc == pc ? 0 : pc & ~0x3;
409             }
410         }
411
412       /* Does it look like a be 0(sr0,%r21)? OR 
413          Does it look like a be, n 0(sr0,%r21)? OR 
414          Does it look like a bve (r21)? (this is on PA2.0)
415          Does it look like a bve, n(r21)? (this is also on PA2.0)
416          That's the branch from an
417          import stub to an export stub.
418
419          It is impossible to determine the target of the branch via
420          simple examination of instructions and/or data (consider
421          that the address in the plabel may be the address of the
422          bind-on-reference routine in the dynamic loader).
423
424          So we have try an alternative approach.
425
426          Get the name of the symbol at our current location; it should
427          be a stub symbol with the same name as the symbol in the
428          shared library.
429
430          Then lookup a minimal symbol with the same name; we should
431          get the minimal symbol for the target routine in the shared
432          library as those take precedence of import/export stubs.  */
433       if ((curr_inst == 0xe2a00000) ||
434           (curr_inst == 0xe2a00002) ||
435           (curr_inst == 0xeaa0d000) ||
436           (curr_inst == 0xeaa0d002))
437         {
438           struct minimal_symbol *stubsym, *libsym;
439
440           stubsym = lookup_minimal_symbol_by_pc (loc);
441           if (stubsym == NULL)
442             {
443               warning ("Unable to find symbol for 0x%lx", loc);
444               return orig_pc == pc ? 0 : pc & ~0x3;
445             }
446
447           libsym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (stubsym), NULL, NULL);
448           if (libsym == NULL)
449             {
450               warning ("Unable to find library symbol for %s\n",
451                        DEPRECATED_SYMBOL_NAME (stubsym));
452               return orig_pc == pc ? 0 : pc & ~0x3;
453             }
454
455           return SYMBOL_VALUE (libsym);
456         }
457
458       /* Does it look like bl X,%rp or bl X,%r0?  Another way to do a
459          branch from the stub to the actual function.  */
460       /*elz */
461       else if ((curr_inst & 0xffe0e000) == 0xe8400000
462                || (curr_inst & 0xffe0e000) == 0xe8000000
463                || (curr_inst & 0xffe0e000) == 0xe800A000)
464         return (loc + hppa_extract_17 (curr_inst) + 8) & ~0x3;
465
466       /* Does it look like bv (rp)?   Note this depends on the
467          current stack pointer being the same as the stack
468          pointer in the stub itself!  This is a branch on from the
469          stub back to the original caller.  */
470       /*else if ((curr_inst & 0xffe0e000) == 0xe840c000) */
471       else if ((curr_inst & 0xffe0f000) == 0xe840c000)
472         {
473           /* Yup.  See if the previous instruction loaded
474              rp from sp - 8.  */
475           if (prev_inst == 0x4bc23ff1)
476             return (read_memory_integer
477                     (read_register (HPPA_SP_REGNUM) - 8, 4)) & ~0x3;
478           else
479             {
480               warning ("Unable to find restore of %%rp before bv (%%rp).");
481               return orig_pc == pc ? 0 : pc & ~0x3;
482             }
483         }
484
485       /* elz: added this case to capture the new instruction
486          at the end of the return part of an export stub used by
487          the PA2.0: BVE, n (rp) */
488       else if ((curr_inst & 0xffe0f000) == 0xe840d000)
489         {
490           return (read_memory_integer
491                   (read_register (HPPA_SP_REGNUM) - 24, TARGET_PTR_BIT / 8)) & ~0x3;
492         }
493
494       /* What about be,n 0(sr0,%rp)?  It's just another way we return to
495          the original caller from the stub.  Used in dynamic executables.  */
496       else if (curr_inst == 0xe0400002)
497         {
498           /* The value we jump to is sitting in sp - 24.  But that's
499              loaded several instructions before the be instruction.
500              I guess we could check for the previous instruction being
501              mtsp %r1,%sr0 if we want to do sanity checking.  */
502           return (read_memory_integer
503                   (read_register (HPPA_SP_REGNUM) - 24, TARGET_PTR_BIT / 8)) & ~0x3;
504         }
505
506       /* Haven't found the branch yet, but we're still in the stub.
507          Keep looking.  */
508       loc += 4;
509     }
510 }
511
512 void
513 hppa_skip_permanent_breakpoint (void)
514 {
515   /* To step over a breakpoint instruction on the PA takes some
516      fiddling with the instruction address queue.
517
518      When we stop at a breakpoint, the IA queue front (the instruction
519      we're executing now) points at the breakpoint instruction, and
520      the IA queue back (the next instruction to execute) points to
521      whatever instruction we would execute after the breakpoint, if it
522      were an ordinary instruction.  This is the case even if the
523      breakpoint is in the delay slot of a branch instruction.
524
525      Clearly, to step past the breakpoint, we need to set the queue
526      front to the back.  But what do we put in the back?  What
527      instruction comes after that one?  Because of the branch delay
528      slot, the next insn is always at the back + 4.  */
529   write_register (HPPA_PCOQ_HEAD_REGNUM, read_register (HPPA_PCOQ_TAIL_REGNUM));
530   write_register (HPPA_PCSQ_HEAD_REGNUM, read_register (HPPA_PCSQ_TAIL_REGNUM));
531
532   write_register (HPPA_PCOQ_TAIL_REGNUM, read_register (HPPA_PCOQ_TAIL_REGNUM) + 4);
533   /* We can leave the tail's space the same, since there's no jump.  */
534 }
535
536 /* Exception handling support for the HP-UX ANSI C++ compiler.
537    The compiler (aCC) provides a callback for exception events;
538    GDB can set a breakpoint on this callback and find out what
539    exception event has occurred. */
540
541 /* The name of the hook to be set to point to the callback function */
542 static char HP_ACC_EH_notify_hook[] = "__eh_notify_hook";
543 /* The name of the function to be used to set the hook value */
544 static char HP_ACC_EH_set_hook_value[] = "__eh_set_hook_value";
545 /* The name of the callback function in end.o */
546 static char HP_ACC_EH_notify_callback[] = "__d_eh_notify_callback";
547 /* Name of function in end.o on which a break is set (called by above) */
548 static char HP_ACC_EH_break[] = "__d_eh_break";
549 /* Name of flag (in end.o) that enables catching throws */
550 static char HP_ACC_EH_catch_throw[] = "__d_eh_catch_throw";
551 /* Name of flag (in end.o) that enables catching catching */
552 static char HP_ACC_EH_catch_catch[] = "__d_eh_catch_catch";
553 /* The enum used by aCC */
554 typedef enum
555   {
556     __EH_NOTIFY_THROW,
557     __EH_NOTIFY_CATCH
558   }
559 __eh_notification;
560
561 /* Is exception-handling support available with this executable? */
562 static int hp_cxx_exception_support = 0;
563 /* Has the initialize function been run? */
564 static int hp_cxx_exception_support_initialized = 0;
565 /* Address of __eh_notify_hook */
566 static CORE_ADDR eh_notify_hook_addr = 0;
567 /* Address of __d_eh_notify_callback */
568 static CORE_ADDR eh_notify_callback_addr = 0;
569 /* Address of __d_eh_break */
570 static CORE_ADDR eh_break_addr = 0;
571 /* Address of __d_eh_catch_catch */
572 static CORE_ADDR eh_catch_catch_addr = 0;
573 /* Address of __d_eh_catch_throw */
574 static CORE_ADDR eh_catch_throw_addr = 0;
575 /* Sal for __d_eh_break */
576 static struct symtab_and_line *break_callback_sal = 0;
577
578 /* Code in end.c expects __d_pid to be set in the inferior,
579    otherwise __d_eh_notify_callback doesn't bother to call
580    __d_eh_break!  So we poke the pid into this symbol
581    ourselves.
582    0 => success
583    1 => failure  */
584 int
585 setup_d_pid_in_inferior (void)
586 {
587   CORE_ADDR anaddr;
588   struct minimal_symbol *msymbol;
589   char buf[4];                  /* FIXME 32x64? */
590
591   /* Slam the pid of the process into __d_pid; failing is only a warning!  */
592   msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
593   if (msymbol == NULL)
594     {
595       warning ("Unable to find __d_pid symbol in object file.");
596       warning ("Suggest linking executable with -g (links in /opt/langtools/lib/end.o).");
597       return 1;
598     }
599
600   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
601   store_unsigned_integer (buf, 4, PIDGET (inferior_ptid)); /* FIXME 32x64? */
602   if (target_write_memory (anaddr, buf, 4))     /* FIXME 32x64? */
603     {
604       warning ("Unable to write __d_pid");
605       warning ("Suggest linking executable with -g (links in /opt/langtools/lib/end.o).");
606       return 1;
607     }
608   return 0;
609 }
610
611 /* elz: Used to lookup a symbol in the shared libraries.
612    This function calls shl_findsym, indirectly through a
613    call to __d_shl_get. __d_shl_get is in end.c, which is always
614    linked in by the hp compilers/linkers. 
615    The call to shl_findsym cannot be made directly because it needs
616    to be active in target address space. 
617    inputs: - minimal symbol pointer for the function we want to look up
618    - address in target space of the descriptor for the library
619    where we want to look the symbol up.
620    This address is retrieved using the 
621    som_solib_get_solib_by_pc function (somsolib.c). 
622    output: - real address in the library of the function.          
623    note: the handle can be null, in which case shl_findsym will look for
624    the symbol in all the loaded shared libraries.
625    files to look at if you need reference on this stuff:
626    dld.c, dld_shl_findsym.c
627    end.c
628    man entry for shl_findsym */
629
630 CORE_ADDR
631 find_stub_with_shl_get (struct minimal_symbol *function, CORE_ADDR handle)
632 {
633   struct symbol *get_sym, *symbol2;
634   struct minimal_symbol *buff_minsym, *msymbol;
635   struct type *ftype;
636   struct value **args;
637   struct value *funcval;
638   struct value *val;
639
640   int x, namelen, err_value, tmp = -1;
641   CORE_ADDR endo_buff_addr, value_return_addr, errno_return_addr;
642   CORE_ADDR stub_addr;
643
644
645   args = alloca (sizeof (struct value *) * 8);          /* 6 for the arguments and one null one??? */
646   funcval = find_function_in_inferior ("__d_shl_get");
647   get_sym = lookup_symbol ("__d_shl_get", NULL, VAR_DOMAIN, NULL, NULL);
648   buff_minsym = lookup_minimal_symbol ("__buffer", NULL, NULL);
649   msymbol = lookup_minimal_symbol ("__shldp", NULL, NULL);
650   symbol2 = lookup_symbol ("__shldp", NULL, VAR_DOMAIN, NULL, NULL);
651   endo_buff_addr = SYMBOL_VALUE_ADDRESS (buff_minsym);
652   namelen = strlen (DEPRECATED_SYMBOL_NAME (function));
653   value_return_addr = endo_buff_addr + namelen;
654   ftype = check_typedef (SYMBOL_TYPE (get_sym));
655
656   /* do alignment */
657   if ((x = value_return_addr % 64) != 0)
658     value_return_addr = value_return_addr + 64 - x;
659
660   errno_return_addr = value_return_addr + 64;
661
662
663   /* set up stuff needed by __d_shl_get in buffer in end.o */
664
665   target_write_memory (endo_buff_addr, DEPRECATED_SYMBOL_NAME (function), namelen);
666
667   target_write_memory (value_return_addr, (char *) &tmp, 4);
668
669   target_write_memory (errno_return_addr, (char *) &tmp, 4);
670
671   target_write_memory (SYMBOL_VALUE_ADDRESS (msymbol),
672                        (char *) &handle, 4);
673
674   /* now prepare the arguments for the call */
675
676   args[0] = value_from_longest (TYPE_FIELD_TYPE (ftype, 0), 12);
677   args[1] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 1), SYMBOL_VALUE_ADDRESS (msymbol));
678   args[2] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 2), endo_buff_addr);
679   args[3] = value_from_longest (TYPE_FIELD_TYPE (ftype, 3), TYPE_PROCEDURE);
680   args[4] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 4), value_return_addr);
681   args[5] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 5), errno_return_addr);
682
683   /* now call the function */
684
685   val = call_function_by_hand (funcval, 6, args);
686
687   /* now get the results */
688
689   target_read_memory (errno_return_addr, (char *) &err_value, sizeof (err_value));
690
691   target_read_memory (value_return_addr, (char *) &stub_addr, sizeof (stub_addr));
692   if (stub_addr <= 0)
693     error ("call to __d_shl_get failed, error code is %d", err_value);
694
695   return (stub_addr);
696 }
697
698 /* Cover routine for find_stub_with_shl_get to pass to catch_errors */
699 static int
700 cover_find_stub_with_shl_get (void *args_untyped)
701 {
702   args_for_find_stub *args = args_untyped;
703   args->return_val = find_stub_with_shl_get (args->msym, args->solib_handle);
704   return 0;
705 }
706
707 /* Initialize exception catchpoint support by looking for the
708    necessary hooks/callbacks in end.o, etc., and set the hook value to
709    point to the required debug function
710
711    Return 0 => failure
712    1 => success          */
713
714 static int
715 initialize_hp_cxx_exception_support (void)
716 {
717   struct symtabs_and_lines sals;
718   struct cleanup *old_chain;
719   struct cleanup *canonical_strings_chain = NULL;
720   int i;
721   char *addr_start;
722   char *addr_end = NULL;
723   char **canonical = (char **) NULL;
724   int thread = -1;
725   struct symbol *sym = NULL;
726   struct minimal_symbol *msym = NULL;
727   struct objfile *objfile;
728   asection *shlib_info;
729
730   /* Detect and disallow recursion.  On HP-UX with aCC, infinite
731      recursion is a possibility because finding the hook for exception
732      callbacks involves making a call in the inferior, which means
733      re-inserting breakpoints which can re-invoke this code */
734
735   static int recurse = 0;
736   if (recurse > 0)
737     {
738       hp_cxx_exception_support_initialized = 0;
739       deprecated_exception_support_initialized = 0;
740       return 0;
741     }
742
743   hp_cxx_exception_support = 0;
744
745   /* First check if we have seen any HP compiled objects; if not,
746      it is very unlikely that HP's idiosyncratic callback mechanism
747      for exception handling debug support will be available!
748      This will percolate back up to breakpoint.c, where our callers
749      will decide to try the g++ exception-handling support instead. */
750   if (!deprecated_hp_som_som_object_present)
751     return 0;
752
753   /* We have a SOM executable with SOM debug info; find the hooks */
754
755   /* First look for the notify hook provided by aCC runtime libs */
756   /* If we find this symbol, we conclude that the executable must
757      have HP aCC exception support built in.  If this symbol is not
758      found, even though we're a HP SOM-SOM file, we may have been
759      built with some other compiler (not aCC).  This results percolates
760      back up to our callers in breakpoint.c which can decide to
761      try the g++ style of exception support instead.
762      If this symbol is found but the other symbols we require are
763      not found, there is something weird going on, and g++ support
764      should *not* be tried as an alternative.
765
766      ASSUMPTION: Only HP aCC code will have __eh_notify_hook defined.  
767      ASSUMPTION: HP aCC and g++ modules cannot be linked together. */
768
769   /* libCsup has this hook; it'll usually be non-debuggable */
770   msym = lookup_minimal_symbol (HP_ACC_EH_notify_hook, NULL, NULL);
771   if (msym)
772     {
773       eh_notify_hook_addr = SYMBOL_VALUE_ADDRESS (msym);
774       hp_cxx_exception_support = 1;
775     }
776   else
777     {
778       warning ("Unable to find exception callback hook (%s).", HP_ACC_EH_notify_hook);
779       warning ("Executable may not have been compiled debuggable with HP aCC.");
780       warning ("GDB will be unable to intercept exception events.");
781       eh_notify_hook_addr = 0;
782       hp_cxx_exception_support = 0;
783       return 0;
784     }
785
786   /* Next look for the notify callback routine in end.o */
787   /* This is always available in the SOM symbol dictionary if end.o is linked in */
788   msym = lookup_minimal_symbol (HP_ACC_EH_notify_callback, NULL, NULL);
789   if (msym)
790     {
791       eh_notify_callback_addr = SYMBOL_VALUE_ADDRESS (msym);
792       hp_cxx_exception_support = 1;
793     }
794   else
795     {
796       warning ("Unable to find exception callback routine (%s).", HP_ACC_EH_notify_callback);
797       warning ("Suggest linking executable with -g (links in /opt/langtools/lib/end.o).");
798       warning ("GDB will be unable to intercept exception events.");
799       eh_notify_callback_addr = 0;
800       return 0;
801     }
802
803 #ifndef GDB_TARGET_IS_HPPA_20W
804   /* Check whether the executable is dynamically linked or archive bound */
805   /* With an archive-bound executable we can use the raw addresses we find
806      for the callback function, etc. without modification. For an executable
807      with shared libraries, we have to do more work to find the plabel, which
808      can be the target of a call through $$dyncall from the aCC runtime support
809      library (libCsup) which is linked shared by default by aCC. */
810   /* This test below was copied from somsolib.c/somread.c.  It may not be a very
811      reliable one to test that an executable is linked shared. pai/1997-07-18 */
812   shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
813   if (shlib_info && (bfd_section_size (symfile_objfile->obfd, shlib_info) != 0))
814     {
815       /* The minsym we have has the local code address, but that's not the
816          plabel that can be used by an inter-load-module call. */
817       /* Find solib handle for main image (which has end.o), and use that
818          and the min sym as arguments to __d_shl_get() (which does the equivalent
819          of shl_findsym()) to find the plabel. */
820
821       args_for_find_stub args;
822       static char message[] = "Error while finding exception callback hook:\n";
823
824       args.solib_handle = som_solib_get_solib_by_pc (eh_notify_callback_addr);
825       args.msym = msym;
826       args.return_val = 0;
827
828       recurse++;
829       catch_errors (cover_find_stub_with_shl_get, &args, message,
830                     RETURN_MASK_ALL);
831       eh_notify_callback_addr = args.return_val;
832       recurse--;
833
834       deprecated_exception_catchpoints_are_fragile = 1;
835
836       if (!eh_notify_callback_addr)
837         {
838           /* We can get here either if there is no plabel in the export list
839              for the main image, or if something strange happened (?) */
840           warning ("Couldn't find a plabel (indirect function label) for the exception callback.");
841           warning ("GDB will not be able to intercept exception events.");
842           return 0;
843         }
844     }
845   else
846     deprecated_exception_catchpoints_are_fragile = 0;
847 #endif
848
849   /* Now, look for the breakpointable routine in end.o */
850   /* This should also be available in the SOM symbol dict. if end.o linked in */
851   msym = lookup_minimal_symbol (HP_ACC_EH_break, NULL, NULL);
852   if (msym)
853     {
854       eh_break_addr = SYMBOL_VALUE_ADDRESS (msym);
855       hp_cxx_exception_support = 1;
856     }
857   else
858     {
859       warning ("Unable to find exception callback routine to set breakpoint (%s).", HP_ACC_EH_break);
860       warning ("Suggest linking executable with -g (link in /opt/langtools/lib/end.o).");
861       warning ("GDB will be unable to intercept exception events.");
862       eh_break_addr = 0;
863       return 0;
864     }
865
866   /* Next look for the catch enable flag provided in end.o */
867   sym = lookup_symbol (HP_ACC_EH_catch_catch, (struct block *) NULL,
868                        VAR_DOMAIN, 0, (struct symtab **) NULL);
869   if (sym)                      /* sometimes present in debug info */
870     {
871       eh_catch_catch_addr = SYMBOL_VALUE_ADDRESS (sym);
872       hp_cxx_exception_support = 1;
873     }
874   else
875     /* otherwise look in SOM symbol dict. */
876     {
877       msym = lookup_minimal_symbol (HP_ACC_EH_catch_catch, NULL, NULL);
878       if (msym)
879         {
880           eh_catch_catch_addr = SYMBOL_VALUE_ADDRESS (msym);
881           hp_cxx_exception_support = 1;
882         }
883       else
884         {
885           warning ("Unable to enable interception of exception catches.");
886           warning ("Executable may not have been compiled debuggable with HP aCC.");
887           warning ("Suggest linking executable with -g (link in /opt/langtools/lib/end.o).");
888           return 0;
889         }
890     }
891
892   /* Next look for the catch enable flag provided end.o */
893   sym = lookup_symbol (HP_ACC_EH_catch_catch, (struct block *) NULL,
894                        VAR_DOMAIN, 0, (struct symtab **) NULL);
895   if (sym)                      /* sometimes present in debug info */
896     {
897       eh_catch_throw_addr = SYMBOL_VALUE_ADDRESS (sym);
898       hp_cxx_exception_support = 1;
899     }
900   else
901     /* otherwise look in SOM symbol dict. */
902     {
903       msym = lookup_minimal_symbol (HP_ACC_EH_catch_throw, NULL, NULL);
904       if (msym)
905         {
906           eh_catch_throw_addr = SYMBOL_VALUE_ADDRESS (msym);
907           hp_cxx_exception_support = 1;
908         }
909       else
910         {
911           warning ("Unable to enable interception of exception throws.");
912           warning ("Executable may not have been compiled debuggable with HP aCC.");
913           warning ("Suggest linking executable with -g (link in /opt/langtools/lib/end.o).");
914           return 0;
915         }
916     }
917
918   /* Set the flags */
919   hp_cxx_exception_support = 2; /* everything worked so far */
920   hp_cxx_exception_support_initialized = 1;
921   deprecated_exception_support_initialized = 1;
922
923   return 1;
924 }
925
926 /* Target operation for enabling or disabling interception of
927    exception events.
928    KIND is either EX_EVENT_THROW or EX_EVENT_CATCH
929    ENABLE is either 0 (disable) or 1 (enable).
930    Return value is NULL if no support found;
931    -1 if something went wrong,
932    or a pointer to a symtab/line struct if the breakpointable
933    address was found. */
934
935 struct symtab_and_line *
936 child_enable_exception_callback (enum exception_event_kind kind, int enable)
937 {
938   char buf[4];
939
940   if (!deprecated_exception_support_initialized
941       || !hp_cxx_exception_support_initialized)
942     if (!initialize_hp_cxx_exception_support ())
943       return NULL;
944
945   switch (hp_cxx_exception_support)
946     {
947     case 0:
948       /* Assuming no HP support at all */
949       return NULL;
950     case 1:
951       /* HP support should be present, but something went wrong */
952       return (struct symtab_and_line *) -1;     /* yuck! */
953       /* there may be other cases in the future */
954     }
955
956   /* Set the EH hook to point to the callback routine */
957   store_unsigned_integer (buf, 4, enable ? eh_notify_callback_addr : 0);        /* FIXME 32x64 problem */
958   /* pai: (temp) FIXME should there be a pack operation first? */
959   if (target_write_memory (eh_notify_hook_addr, buf, 4))        /* FIXME 32x64 problem */
960     {
961       warning ("Could not write to target memory for exception event callback.");
962       warning ("Interception of exception events may not work.");
963       return (struct symtab_and_line *) -1;
964     }
965   if (enable)
966     {
967       /* Ensure that __d_pid is set up correctly -- end.c code checks this. :-( */
968       if (PIDGET (inferior_ptid) > 0)
969         {
970           if (setup_d_pid_in_inferior ())
971             return (struct symtab_and_line *) -1;
972         }
973       else
974         {
975           warning ("Internal error: Invalid inferior pid?  Cannot intercept exception events.");
976           return (struct symtab_and_line *) -1;
977         }
978     }
979
980   switch (kind)
981     {
982     case EX_EVENT_THROW:
983       store_unsigned_integer (buf, 4, enable ? 1 : 0);
984       if (target_write_memory (eh_catch_throw_addr, buf, 4))    /* FIXME 32x64? */
985         {
986           warning ("Couldn't enable exception throw interception.");
987           return (struct symtab_and_line *) -1;
988         }
989       break;
990     case EX_EVENT_CATCH:
991       store_unsigned_integer (buf, 4, enable ? 1 : 0);
992       if (target_write_memory (eh_catch_catch_addr, buf, 4))    /* FIXME 32x64? */
993         {
994           warning ("Couldn't enable exception catch interception.");
995           return (struct symtab_and_line *) -1;
996         }
997       break;
998     default:
999       error ("Request to enable unknown or unsupported exception event.");
1000     }
1001
1002   /* Copy break address into new sal struct, malloc'ing if needed.  */
1003   if (!break_callback_sal)
1004     break_callback_sal = XMALLOC (struct symtab_and_line);
1005   init_sal (break_callback_sal);
1006   break_callback_sal->symtab = NULL;
1007   break_callback_sal->pc = eh_break_addr;
1008   break_callback_sal->line = 0;
1009   break_callback_sal->end = eh_break_addr;
1010
1011   return break_callback_sal;
1012 }
1013
1014 /* Record some information about the current exception event */
1015 static struct exception_event_record current_ex_event;
1016 /* Convenience struct */
1017 static struct symtab_and_line null_symtab_and_line =
1018 {NULL, 0, 0, 0};
1019
1020 /* Report current exception event.  Returns a pointer to a record
1021    that describes the kind of the event, where it was thrown from,
1022    and where it will be caught.  More information may be reported
1023    in the future */
1024 struct exception_event_record *
1025 child_get_current_exception_event (void)
1026 {
1027   CORE_ADDR event_kind;
1028   CORE_ADDR throw_addr;
1029   CORE_ADDR catch_addr;
1030   struct frame_info *fi, *curr_frame;
1031   int level = 1;
1032
1033   curr_frame = get_current_frame ();
1034   if (!curr_frame)
1035     return (struct exception_event_record *) NULL;
1036
1037   /* Go up one frame to __d_eh_notify_callback, because at the
1038      point when this code is executed, there's garbage in the
1039      arguments of __d_eh_break. */
1040   fi = find_relative_frame (curr_frame, &level);
1041   if (level != 0)
1042     return (struct exception_event_record *) NULL;
1043
1044   select_frame (fi);
1045
1046   /* Read in the arguments */
1047   /* __d_eh_notify_callback() is called with 3 arguments:
1048      1. event kind catch or throw
1049      2. the target address if known
1050      3. a flag -- not sure what this is. pai/1997-07-17 */
1051   event_kind = read_register (HPPA_ARG0_REGNUM);
1052   catch_addr = read_register (HPPA_ARG1_REGNUM);
1053
1054   /* Now go down to a user frame */
1055   /* For a throw, __d_eh_break is called by
1056      __d_eh_notify_callback which is called by
1057      __notify_throw which is called
1058      from user code.
1059      For a catch, __d_eh_break is called by
1060      __d_eh_notify_callback which is called by
1061      <stackwalking stuff> which is called by
1062      __throw__<stuff> or __rethrow_<stuff> which is called
1063      from user code. */
1064   /* FIXME: Don't use such magic numbers; search for the frames */
1065   level = (event_kind == EX_EVENT_THROW) ? 3 : 4;
1066   fi = find_relative_frame (curr_frame, &level);
1067   if (level != 0)
1068     return (struct exception_event_record *) NULL;
1069
1070   select_frame (fi);
1071   throw_addr = get_frame_pc (fi);
1072
1073   /* Go back to original (top) frame */
1074   select_frame (curr_frame);
1075
1076   current_ex_event.kind = (enum exception_event_kind) event_kind;
1077   current_ex_event.throw_sal = find_pc_line (throw_addr, 1);
1078   current_ex_event.catch_sal = find_pc_line (catch_addr, 1);
1079
1080   return &current_ex_event;
1081 }
1082
1083 /* Signal frames.  */
1084 struct hppa_hpux_sigtramp_unwind_cache
1085 {
1086   CORE_ADDR base;
1087   struct trad_frame_saved_reg *saved_regs;
1088 };
1089
1090 static int hppa_hpux_tramp_reg[] = {
1091   HPPA_SAR_REGNUM,
1092   HPPA_PCOQ_HEAD_REGNUM,
1093   HPPA_PCSQ_HEAD_REGNUM,
1094   HPPA_PCOQ_TAIL_REGNUM,
1095   HPPA_PCSQ_TAIL_REGNUM,
1096   HPPA_EIEM_REGNUM,
1097   HPPA_IIR_REGNUM,
1098   HPPA_ISR_REGNUM,
1099   HPPA_IOR_REGNUM,
1100   HPPA_IPSW_REGNUM,
1101   -1,
1102   HPPA_SR4_REGNUM,
1103   HPPA_SR4_REGNUM + 1,
1104   HPPA_SR4_REGNUM + 2,
1105   HPPA_SR4_REGNUM + 3,
1106   HPPA_SR4_REGNUM + 4,
1107   HPPA_SR4_REGNUM + 5,
1108   HPPA_SR4_REGNUM + 6,
1109   HPPA_SR4_REGNUM + 7,
1110   HPPA_RCR_REGNUM,
1111   HPPA_PID0_REGNUM,
1112   HPPA_PID1_REGNUM,
1113   HPPA_CCR_REGNUM,
1114   HPPA_PID2_REGNUM,
1115   HPPA_PID3_REGNUM,
1116   HPPA_TR0_REGNUM,
1117   HPPA_TR0_REGNUM + 1,
1118   HPPA_TR0_REGNUM + 2,
1119   HPPA_CR27_REGNUM
1120 };
1121
1122 static struct hppa_hpux_sigtramp_unwind_cache *
1123 hppa_hpux_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
1124                                        void **this_cache)
1125
1126 {
1127   struct gdbarch *gdbarch = get_frame_arch (next_frame);
1128   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1129   struct hppa_hpux_sigtramp_unwind_cache *info;
1130   unsigned int flag;
1131   CORE_ADDR sp, scptr;
1132   int i, incr, off, szoff;
1133
1134   if (*this_cache)
1135     return *this_cache;
1136
1137   info = FRAME_OBSTACK_ZALLOC (struct hppa_hpux_sigtramp_unwind_cache);
1138   *this_cache = info;
1139   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1140
1141   sp = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
1142
1143   scptr = sp - 1352;
1144   off = scptr;
1145
1146   /* See /usr/include/machine/save_state.h for the structure of the save_state_t
1147      structure. */
1148   
1149   flag = read_memory_unsigned_integer(scptr, 4);
1150     
1151   if (!(flag & 0x40))
1152     {
1153       /* Narrow registers. */
1154       off = scptr + offsetof (save_state_t, ss_narrow);
1155       incr = 4;
1156       szoff = 0;
1157     }
1158   else
1159     {
1160       /* Wide registers. */
1161       off = scptr + offsetof (save_state_t, ss_wide) + 8;
1162       incr = 8;
1163       szoff = (tdep->bytes_per_address == 4 ? 4 : 0);
1164     }
1165
1166   for (i = 1; i < 32; i++)
1167     {
1168       info->saved_regs[HPPA_R0_REGNUM + i].addr = off + szoff;
1169       off += incr;
1170     }
1171
1172   for (i = 0; i < ARRAY_SIZE (hppa_hpux_tramp_reg); i++)
1173     {
1174       if (hppa_hpux_tramp_reg[i] > 0)
1175         info->saved_regs[hppa_hpux_tramp_reg[i]].addr = off + szoff;
1176       off += incr;
1177     }
1178
1179   /* TODO: fp regs */
1180
1181   info->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
1182
1183   return info;
1184 }
1185
1186 static void
1187 hppa_hpux_sigtramp_frame_this_id (struct frame_info *next_frame,
1188                                    void **this_prologue_cache,
1189                                    struct frame_id *this_id)
1190 {
1191   struct hppa_hpux_sigtramp_unwind_cache *info
1192     = hppa_hpux_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
1193   *this_id = frame_id_build (info->base, frame_pc_unwind (next_frame));
1194 }
1195
1196 static void
1197 hppa_hpux_sigtramp_frame_prev_register (struct frame_info *next_frame,
1198                                          void **this_prologue_cache,
1199                                          int regnum, int *optimizedp,
1200                                          enum lval_type *lvalp, 
1201                                          CORE_ADDR *addrp,
1202                                          int *realnump, void *valuep)
1203 {
1204   struct hppa_hpux_sigtramp_unwind_cache *info
1205     = hppa_hpux_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
1206   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
1207                                    optimizedp, lvalp, addrp, realnump, valuep);
1208 }
1209
1210 static const struct frame_unwind hppa_hpux_sigtramp_frame_unwind = {
1211   SIGTRAMP_FRAME,
1212   hppa_hpux_sigtramp_frame_this_id,
1213   hppa_hpux_sigtramp_frame_prev_register
1214 };
1215
1216 static const struct frame_unwind *
1217 hppa_hpux_sigtramp_unwind_sniffer (struct frame_info *next_frame)
1218 {
1219   CORE_ADDR pc = frame_pc_unwind (next_frame);
1220   char *name;
1221
1222   find_pc_partial_function (pc, &name, NULL, NULL);
1223
1224   if (name && strcmp(name, "_sigreturn") == 0)
1225     return &hppa_hpux_sigtramp_frame_unwind;
1226
1227   return NULL;
1228 }
1229
1230 static CORE_ADDR
1231 hppa_hpux_som_find_global_pointer (struct value *function)
1232 {
1233   CORE_ADDR faddr;
1234   
1235   faddr = value_as_address (function);
1236
1237   /* Is this a plabel? If so, dereference it to get the gp value.  */
1238   if (faddr & 2)
1239     {
1240       int status;
1241       char buf[4];
1242
1243       faddr &= ~3;
1244
1245       status = target_read_memory (faddr + 4, buf, sizeof (buf));
1246       if (status == 0)
1247         return extract_unsigned_integer (buf, sizeof (buf));
1248     }
1249
1250   return som_solib_get_got_by_pc (faddr);
1251 }
1252
1253 static CORE_ADDR
1254 hppa_hpux_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
1255                            CORE_ADDR funcaddr, int using_gcc,
1256                            struct value **args, int nargs,
1257                            struct type *value_type,
1258                            CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
1259 {
1260   /* FIXME: tausq/2004-06-09: This needs much more testing.  It is broken
1261      for pa64, but we should be able to get it to work with a little bit
1262      of work. gdb-6.1 has a lot of code to handle various cases; I've tried to
1263      simplify it and avoid compile-time conditionals.  */
1264
1265   /* On HPUX, functions in the main executable and in libraries can be located
1266      in different spaces.  In order for us to be able to select the right 
1267      space for the function call, we need to go through an instruction seqeunce
1268      to select the right space for the target function, call it, and then
1269      restore the space on return.
1270
1271      There are two helper routines that can be used for this task -- if
1272      an application is linked with gcc, it will contain a __gcc_plt_call
1273      helper function.  __gcc_plt_call, when passed the entry point of an
1274      import stub, will do the necessary space setting/restoration for the
1275      target function.
1276
1277      For programs that are compiled/linked with the HP compiler, a similar
1278      function called __d_plt_call exists; __d_plt_call expects a PLABEL instead
1279      of an import stub as an argument.
1280
1281      // *INDENT-OFF*
1282      To summarize, the call flow is:
1283        current function -> dummy frame -> __gcc_plt_call (import stub) 
1284                         -> target function
1285      or
1286        current function -> dummy frame -> __d_plt_call (plabel)
1287                         -> target function
1288      // *INDENT-ON*
1289
1290      In general the "funcaddr" argument passed to push_dummy_code is the actual
1291      entry point of the target function.  For __gcc_plt_call, we need to 
1292      locate the import stub for the corresponding function.  Failing that,
1293      we construct a dummy "import stub" on the stack to pass as an argument.
1294      For __d_plt_call, we similarly synthesize a PLABEL on the stack to
1295      pass to the helper function.
1296
1297      An additional twist is that, in order for us to restore the space register
1298      to its starting state, we need __gcc_plt_call/__d_plt_call to return
1299      to the instruction where we started the call.  However, if we put
1300      the breakpoint there, gdb will complain because it will find two 
1301      frames on the stack with the same (sp, pc) (with the dummy frame in 
1302      between).  Currently, we set the return pointer to (pc - 4) of the 
1303      current function.  FIXME: This is not an ideal solution; possibly if the 
1304      current pc is at the beginning of a page, this will cause a page fault. 
1305      Need to understand this better and figure out a better way to fix it.  */
1306
1307   struct minimal_symbol *sym;
1308
1309   /* Nonzero if we will use GCC's PLT call routine.  This routine must be
1310      passed an import stub, not a PLABEL.  It is also necessary to get %r19
1311      before performing the call.  (This is done by push_dummy_call.)  */
1312   int use_gcc_plt_call = 1;
1313
1314   /* See if __gcc_plt_call is available; if not we will use the HP version
1315      instead.  */
1316   sym = lookup_minimal_symbol ("__gcc_plt_call", NULL, NULL);
1317   if (sym == NULL)
1318     use_gcc_plt_call = 0;
1319
1320   /* If using __gcc_plt_call, we need to make sure we pass in an import
1321      stub.  funcaddr can be pointing to an export stub or a real function,
1322      so we try to resolve it to the import stub.  */
1323   if (use_gcc_plt_call)
1324     {
1325       struct objfile *objfile;
1326       struct minimal_symbol *funsym, *stubsym;
1327       CORE_ADDR stubaddr = 0;
1328
1329       funsym = lookup_minimal_symbol_by_pc (funcaddr);
1330       if (!funsym)
1331         error ("Unable to find symbol for target function.\n");
1332
1333       ALL_OBJFILES (objfile)
1334         {
1335           stubsym = lookup_minimal_symbol_solib_trampoline
1336             (SYMBOL_LINKAGE_NAME (funsym), objfile);
1337
1338           if (stubsym)
1339             {
1340               struct unwind_table_entry *u;
1341
1342               u = find_unwind_entry (SYMBOL_VALUE (stubsym));
1343               if (u == NULL 
1344                   || (u->stub_unwind.stub_type != IMPORT
1345                       && u->stub_unwind.stub_type != IMPORT_SHLIB))
1346                 continue;
1347
1348               stubaddr = SYMBOL_VALUE (stubsym);
1349
1350               /* If we found an IMPORT stub, then we can stop searching;
1351                  if we found an IMPORT_SHLIB, we want to continue the search
1352                  in the hopes that we will find an IMPORT stub.  */
1353               if (u->stub_unwind.stub_type == IMPORT)
1354                 break;
1355             }
1356         }
1357
1358       if (stubaddr != 0)
1359         {
1360           /* Argument to __gcc_plt_call is passed in r22.  */
1361           regcache_cooked_write_unsigned (current_regcache, 22, stubaddr);
1362         }
1363       else
1364         {
1365           /* No import stub found; let's synthesize one.  */
1366
1367           /* ldsid %r21, %r1 */
1368           write_memory_unsigned_integer (sp, 4, 0x02a010a1);
1369           /* mtsp %r1,%sr0 */
1370           write_memory_unsigned_integer (sp + 4, 4, 0x00011820);
1371           /* be 0(%sr0, %r21) */
1372           write_memory_unsigned_integer (sp + 8, 4, 0xe2a00000);
1373           /* nop */
1374           write_memory_unsigned_integer (sp + 12, 4, 0x08000240);
1375
1376           regcache_cooked_write_unsigned (current_regcache, 21, funcaddr);
1377           regcache_cooked_write_unsigned (current_regcache, 22, sp);
1378         }
1379
1380       /* We set the breakpoint address and r31 to (close to) where the current
1381          pc is; when __gcc_plt_call returns, it will restore pcsqh to the
1382          current value based on this.  The -4 is needed for frame unwinding
1383          to work properly -- we need to land in a different function than
1384          the current function.  */
1385       *bp_addr = (read_register (HPPA_PCOQ_HEAD_REGNUM) & ~3) - 4;
1386       regcache_cooked_write_unsigned (current_regcache, 31, *bp_addr);
1387
1388       /* Continue from __gcc_plt_call.  */
1389       *real_pc = SYMBOL_VALUE (sym);
1390     }
1391   else
1392     {
1393       ULONGEST gp;
1394
1395       /* Use __d_plt_call as a fallback; __d_plt_call expects to be called 
1396          with a plabel, so we need to build one.  */
1397
1398       sym = lookup_minimal_symbol ("__d_plt_call", NULL, NULL);
1399       if (sym == NULL)
1400         error("Can't find an address for __d_plt_call or __gcc_plt_call "
1401               "trampoline\nSuggest linking executable with -g or compiling "
1402               "with gcc.");
1403
1404       gp = gdbarch_tdep (gdbarch)->find_global_pointer (funcaddr);
1405       write_memory_unsigned_integer (sp, 4, funcaddr);
1406       write_memory_unsigned_integer (sp + 4, 4, gp);
1407
1408       /* plabel is passed in r22 */
1409       regcache_cooked_write_unsigned (current_regcache, 22, sp);
1410     }
1411
1412   /* Pushed one stack frame, which has to be 64-byte aligned.  */
1413   sp += 64;
1414
1415   return sp;
1416 }
1417 \f
1418
1419 /* Bit in the `ss_flag' member of `struct save_state' that indicates
1420    that the 64-bit register values are live.  From
1421    <machine/save_state.h>.  */
1422 #define HPPA_HPUX_SS_WIDEREGS           0x40
1423
1424 /* Offsets of various parts of `struct save_state'.  From
1425    <machine/save_state.h>.  */
1426 #define HPPA_HPUX_SS_FLAGS_OFFSET       0
1427 #define HPPA_HPUX_SS_NARROW_OFFSET      4
1428 #define HPPA_HPUX_SS_FPBLOCK_OFFSET     256
1429 #define HPPA_HPUX_SS_WIDE_OFFSET        640
1430
1431 /* The size of `struct save_state.  */
1432 #define HPPA_HPUX_SAVE_STATE_SIZE       1152
1433
1434 /* The size of `struct pa89_save_state', which corresponds to PA-RISC
1435    1.1, the lowest common denominator that we support.  */
1436 #define HPPA_HPUX_PA89_SAVE_STATE_SIZE  512
1437
1438 static void
1439 hppa_hpux_supply_ss_narrow (struct regcache *regcache,
1440                             int regnum, const char *save_state)
1441 {
1442   const char *ss_narrow = save_state + HPPA_HPUX_SS_NARROW_OFFSET;
1443   int i, offset = 0;
1444
1445   for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
1446     {
1447       if (regnum == i || regnum == -1)
1448         regcache_raw_supply (regcache, i, ss_narrow + offset);
1449
1450       offset += 4;
1451     }
1452 }
1453
1454 static void
1455 hppa_hpux_supply_ss_fpblock (struct regcache *regcache,
1456                              int regnum, const char *save_state)
1457 {
1458   const char *ss_fpblock = save_state + HPPA_HPUX_SS_FPBLOCK_OFFSET;
1459   int i, offset = 0;
1460
1461   /* FIXME: We view the floating-point state as 64 single-precision
1462      registers for 32-bit code, and 32 double-precision register for
1463      64-bit code.  This distinction is artificial and should be
1464      eliminated.  If that ever happens, we should remove the if-clause
1465      below.  */
1466
1467   if (register_size (get_regcache_arch (regcache), HPPA_FP0_REGNUM) == 4)
1468     {
1469       for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 64; i++)
1470         {
1471           if (regnum == i || regnum == -1)
1472             regcache_raw_supply (regcache, i, ss_fpblock + offset);
1473
1474           offset += 4;
1475         }
1476     }
1477   else
1478     {
1479       for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32; i++)
1480         {
1481           if (regnum == i || regnum == -1)
1482             regcache_raw_supply (regcache, i, ss_fpblock + offset);
1483
1484           offset += 8;
1485         }
1486     }
1487 }
1488
1489 static void
1490 hppa_hpux_supply_ss_wide (struct regcache *regcache,
1491                           int regnum, const char *save_state)
1492 {
1493   const char *ss_wide = save_state + HPPA_HPUX_SS_WIDE_OFFSET;
1494   int i, offset = 8;
1495
1496   if (register_size (get_regcache_arch (regcache), HPPA_R1_REGNUM) == 4)
1497     offset += 4;
1498
1499   for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
1500     {
1501       if (regnum == i || regnum == -1)
1502         regcache_raw_supply (regcache, i, ss_wide + offset);
1503
1504       offset += 8;
1505     }
1506 }
1507
1508 static void
1509 hppa_hpux_supply_save_state (const struct regset *regset,
1510                              struct regcache *regcache,
1511                              int regnum, const void *regs, size_t len)
1512 {
1513   const char *proc_info = regs;
1514   const char *save_state = proc_info + 8;
1515   ULONGEST flags;
1516
1517   flags = extract_unsigned_integer (save_state + HPPA_HPUX_SS_FLAGS_OFFSET, 4);
1518   if (regnum == -1 || regnum == HPPA_FLAGS_REGNUM)
1519     {
1520       struct gdbarch *arch = get_regcache_arch (regcache);
1521       size_t size = register_size (arch, HPPA_FLAGS_REGNUM);
1522       char buf[8];
1523
1524       store_unsigned_integer (buf, size, flags);
1525       regcache_raw_supply (regcache, HPPA_FLAGS_REGNUM, buf);
1526     }
1527
1528   /* If the SS_WIDEREGS flag is set, we really do need the full
1529      `struct save_state'.  */
1530   if (flags & HPPA_HPUX_SS_WIDEREGS && len < HPPA_HPUX_SAVE_STATE_SIZE)
1531     error ("Register set contents too small");
1532
1533   if (flags & HPPA_HPUX_SS_WIDEREGS)
1534     hppa_hpux_supply_ss_wide (regcache, regnum, save_state);
1535   else
1536     hppa_hpux_supply_ss_narrow (regcache, regnum, save_state);
1537
1538   hppa_hpux_supply_ss_fpblock (regcache, regnum, save_state);
1539 }
1540
1541 /* HP-UX register set.  */
1542
1543 static struct regset hppa_hpux_regset =
1544 {
1545   NULL,
1546   hppa_hpux_supply_save_state
1547 };
1548
1549 static const struct regset *
1550 hppa_hpux_regset_from_core_section (struct gdbarch *gdbarch,
1551                                     const char *sect_name, size_t sect_size)
1552 {
1553   if (strcmp (sect_name, ".reg") == 0
1554       && sect_size >= HPPA_HPUX_PA89_SAVE_STATE_SIZE + 8)
1555     return &hppa_hpux_regset;
1556
1557   return NULL;
1558 }
1559 \f
1560
1561 /* Bit in the `ss_flag' member of `struct save_state' that indicates
1562    the state was saved from a system call.  From
1563    <machine/save_state.h>.  */
1564 #define HPPA_HPUX_SS_INSYSCALL  0x02
1565
1566 static CORE_ADDR
1567 hppa_hpux_read_pc (ptid_t ptid)
1568 {
1569   ULONGEST flags;
1570
1571   /* If we're currently in a system call return the contents of %r31.  */
1572   flags = read_register_pid (HPPA_FLAGS_REGNUM, ptid);
1573   if (flags & HPPA_HPUX_SS_INSYSCALL)
1574     return read_register_pid (HPPA_R31_REGNUM, ptid) & ~0x3;
1575
1576   return hppa_read_pc (ptid);
1577 }
1578
1579 static void
1580 hppa_hpux_write_pc (CORE_ADDR pc, ptid_t ptid)
1581 {
1582   ULONGEST flags;
1583
1584   /* If we're currently in a system call also write PC into %r31.  */
1585   flags = read_register_pid (HPPA_FLAGS_REGNUM, ptid);
1586   if (flags & HPPA_HPUX_SS_INSYSCALL)
1587     write_register_pid (HPPA_R31_REGNUM, pc | 0x3, ptid);
1588
1589   return hppa_write_pc (pc, ptid);
1590 }
1591
1592 static CORE_ADDR
1593 hppa_hpux_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1594 {
1595   ULONGEST flags;
1596
1597   /* If we're currently in a system call return the contents of %r31.  */
1598   flags = frame_unwind_register_unsigned (next_frame, HPPA_FLAGS_REGNUM);
1599   if (flags & HPPA_HPUX_SS_INSYSCALL)
1600     return frame_unwind_register_unsigned (next_frame, HPPA_R31_REGNUM) & ~0x3;
1601
1602   return hppa_unwind_pc (gdbarch, next_frame);
1603 }
1604 \f
1605
1606 static void
1607 hppa_hpux_inferior_created (struct target_ops *objfile, int from_tty)
1608 {
1609   /* Some HP-UX related globals to clear when a new "main"
1610      symbol file is loaded.  HP-specific.  */
1611   deprecated_hp_som_som_object_present = 0;
1612   hp_cxx_exception_support_initialized = 0;
1613 }
1614
1615 /* Given the current value of the pc, check to see if it is inside a stub, and
1616    if so, change the value of the pc to point to the caller of the stub.
1617    NEXT_FRAME is the next frame in the current list of frames.
1618    BASE contains to stack frame base of the current frame. 
1619    SAVE_REGS is the register file stored in the frame cache. */
1620 static void
1621 hppa_hpux_unwind_adjust_stub (struct frame_info *next_frame, CORE_ADDR base,
1622                               struct trad_frame_saved_reg *saved_regs)
1623 {
1624   int optimized, realreg;
1625   enum lval_type lval;
1626   CORE_ADDR addr;
1627   char buffer[sizeof(ULONGEST)];
1628   ULONGEST val;
1629   CORE_ADDR stubpc;
1630   struct unwind_table_entry *u;
1631
1632   trad_frame_get_prev_register (next_frame, saved_regs, 
1633                                 HPPA_PCOQ_HEAD_REGNUM, 
1634                                 &optimized, &lval, &addr, &realreg, buffer);
1635   val = extract_unsigned_integer (buffer, 
1636                                   register_size (get_frame_arch (next_frame), 
1637                                                  HPPA_PCOQ_HEAD_REGNUM));
1638
1639   u = find_unwind_entry (val);
1640   if (u && u->stub_unwind.stub_type == EXPORT)
1641     {
1642       stubpc = read_memory_integer (base - 24, TARGET_PTR_BIT / 8);
1643       trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
1644     }
1645   else if (hppa_symbol_address ("__gcc_plt_call") 
1646            == get_pc_function_start (val))
1647     {
1648       stubpc = read_memory_integer (base - 8, TARGET_PTR_BIT / 8);
1649       trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
1650     }
1651 }
1652
1653 static void
1654 hppa_hpux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1655 {
1656   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1657
1658   if (tdep->bytes_per_address == 4)
1659     tdep->in_solib_call_trampoline = hppa32_hpux_in_solib_call_trampoline;
1660   else
1661     tdep->in_solib_call_trampoline = hppa64_hpux_in_solib_call_trampoline;
1662
1663   tdep->unwind_adjust_stub = hppa_hpux_unwind_adjust_stub;
1664
1665   set_gdbarch_in_solib_return_trampoline
1666     (gdbarch, hppa_hpux_in_solib_return_trampoline);
1667   set_gdbarch_skip_trampoline_code (gdbarch, hppa_hpux_skip_trampoline_code);
1668
1669   set_gdbarch_push_dummy_code (gdbarch, hppa_hpux_push_dummy_code);
1670   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1671
1672   set_gdbarch_read_pc (gdbarch, hppa_hpux_read_pc);
1673   set_gdbarch_write_pc (gdbarch, hppa_hpux_write_pc);
1674   set_gdbarch_unwind_pc (gdbarch, hppa_hpux_unwind_pc);
1675
1676   set_gdbarch_regset_from_core_section
1677     (gdbarch, hppa_hpux_regset_from_core_section);
1678
1679   frame_unwind_append_sniffer (gdbarch, hppa_hpux_sigtramp_unwind_sniffer);
1680
1681   observer_attach_inferior_created (hppa_hpux_inferior_created);
1682 }
1683
1684 static void
1685 hppa_hpux_som_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1686 {
1687   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1688
1689   tdep->is_elf = 0;
1690
1691   tdep->find_global_pointer = hppa_hpux_som_find_global_pointer;
1692   hppa_hpux_init_abi (info, gdbarch);
1693 }
1694
1695 static void
1696 hppa_hpux_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1697 {
1698   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1699
1700   tdep->is_elf = 1;
1701   hppa_hpux_init_abi (info, gdbarch);
1702 }
1703
1704 static enum gdb_osabi
1705 hppa_hpux_core_osabi_sniffer (bfd *abfd)
1706 {
1707   if (strcmp (bfd_get_target (abfd), "hpux-core") == 0)
1708     return GDB_OSABI_HPUX_SOM;
1709
1710   return GDB_OSABI_UNKNOWN;
1711 }
1712
1713 void
1714 _initialize_hppa_hpux_tdep (void)
1715 {
1716   /* BFD doesn't set a flavour for HP-UX style core files.  It doesn't
1717      set the architecture either.  */
1718   gdbarch_register_osabi_sniffer (bfd_arch_unknown,
1719                                   bfd_target_unknown_flavour,
1720                                   hppa_hpux_core_osabi_sniffer);
1721
1722   gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_HPUX_SOM,
1723                           hppa_hpux_som_init_abi);
1724   gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w, GDB_OSABI_HPUX_ELF,
1725                           hppa_hpux_elf_init_abi);
1726 }