2004-01-17 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / hppa-tdep.c
1 /* Target-dependent code for the HP PA architecture, for GDB.
2
3    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5    Foundation, Inc.
6
7    Contributed by the Center for Software Science at the
8    University of Utah (pa-gdb-bugs@cs.utah.edu).
9
10    This file is part of GDB.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330,
25    Boston, MA 02111-1307, USA.  */
26
27 #include "defs.h"
28 #include "frame.h"
29 #include "bfd.h"
30 #include "inferior.h"
31 #include "value.h"
32 #include "regcache.h"
33 #include "completer.h"
34 #include "language.h"
35 #include "osabi.h"
36 #include "gdb_assert.h"
37 #include "infttrace.h"
38 /* For argument passing to the inferior */
39 #include "symtab.h"
40 #include "infcall.h"
41 #include "dis-asm.h"
42
43 #ifdef USG
44 #include <sys/types.h>
45 #endif
46
47 #include <dl.h>
48 #include <sys/param.h>
49 #include <signal.h>
50
51 #include <sys/ptrace.h>
52 #include <machine/save_state.h>
53
54 #ifdef COFF_ENCAPSULATE
55 #include "a.out.encap.h"
56 #else
57 #endif
58
59 /*#include <sys/user.h>         After a.out.h  */
60 #include <sys/file.h>
61 #include "gdb_stat.h"
62 #include "gdb_wait.h"
63
64 #include "gdbcore.h"
65 #include "gdbcmd.h"
66 #include "target.h"
67 #include "symfile.h"
68 #include "objfiles.h"
69 #include "hppa-tdep.h"
70
71 /* Some local constants.  */
72 static const int hppa32_num_regs = 128;
73 static const int hppa64_num_regs = 96;
74
75 static const int hppa64_call_dummy_breakpoint_offset = 22 * 4;
76
77 /* DEPRECATED_CALL_DUMMY_LENGTH is computed based on the size of a
78    word on the target machine, not the size of an instruction.  Since
79    a word on this target holds two instructions we have to divide the
80    instruction size by two to get the word size of the dummy.  */
81 static const int hppa32_call_dummy_length = INSTRUCTION_SIZE * 28;
82 static const int hppa64_call_dummy_length = INSTRUCTION_SIZE * 26 / 2;
83
84 /* Get at various relevent fields of an instruction word. */
85 #define MASK_5 0x1f
86 #define MASK_11 0x7ff
87 #define MASK_14 0x3fff
88 #define MASK_21 0x1fffff
89
90 /* Define offsets into the call dummy for the target function address.
91    See comments related to CALL_DUMMY for more info.  */
92 #define FUNC_LDIL_OFFSET (INSTRUCTION_SIZE * 9)
93 #define FUNC_LDO_OFFSET (INSTRUCTION_SIZE * 10)
94
95 /* Define offsets into the call dummy for the _sr4export address.
96    See comments related to CALL_DUMMY for more info.  */
97 #define SR4EXPORT_LDIL_OFFSET (INSTRUCTION_SIZE * 12)
98 #define SR4EXPORT_LDO_OFFSET (INSTRUCTION_SIZE * 13)
99
100 /* To support detection of the pseudo-initial frame
101    that threads have. */
102 #define THREAD_INITIAL_FRAME_SYMBOL  "__pthread_exit"
103 #define THREAD_INITIAL_FRAME_SYM_LEN  sizeof(THREAD_INITIAL_FRAME_SYMBOL)
104
105 /* Sizes (in bytes) of the native unwind entries.  */
106 #define UNWIND_ENTRY_SIZE 16
107 #define STUB_UNWIND_ENTRY_SIZE 8
108
109 static int get_field (unsigned word, int from, int to);
110
111 static int extract_5_load (unsigned int);
112
113 static unsigned extract_5R_store (unsigned int);
114
115 static unsigned extract_5r_store (unsigned int);
116
117 static void find_dummy_frame_regs (struct frame_info *, CORE_ADDR *);
118
119 static int find_proc_framesize (CORE_ADDR);
120
121 static int find_return_regnum (CORE_ADDR);
122
123 struct unwind_table_entry *find_unwind_entry (CORE_ADDR);
124
125 static int extract_17 (unsigned int);
126
127 static unsigned deposit_21 (unsigned int, unsigned int);
128
129 static int extract_21 (unsigned);
130
131 static unsigned deposit_14 (int, unsigned int);
132
133 static int extract_14 (unsigned);
134
135 static void unwind_command (char *, int);
136
137 static int low_sign_extend (unsigned int, unsigned int);
138
139 static int sign_extend (unsigned int, unsigned int);
140
141 static int restore_pc_queue (CORE_ADDR *);
142
143 static int hppa_alignof (struct type *);
144
145 static int prologue_inst_adjust_sp (unsigned long);
146
147 static int is_branch (unsigned long);
148
149 static int inst_saves_gr (unsigned long);
150
151 static int inst_saves_fr (unsigned long);
152
153 static int pc_in_interrupt_handler (CORE_ADDR);
154
155 static int pc_in_linker_stub (CORE_ADDR);
156
157 static int compare_unwind_entries (const void *, const void *);
158
159 static void read_unwind_info (struct objfile *);
160
161 static void internalize_unwinds (struct objfile *,
162                                  struct unwind_table_entry *,
163                                  asection *, unsigned int,
164                                  unsigned int, CORE_ADDR);
165 static void pa_print_registers (char *, int, int);
166 static void pa_strcat_registers (char *, int, int, struct ui_file *);
167 static void pa_register_look_aside (char *, int, long *);
168 static void pa_print_fp_reg (int);
169 static void pa_strcat_fp_reg (int, struct ui_file *, enum precision_type);
170 static void record_text_segment_lowaddr (bfd *, asection *, void *);
171 /* FIXME: brobecker 2002-11-07: We will likely be able to make the
172    following functions static, once we hppa is partially multiarched.  */
173 int hppa_reg_struct_has_addr (int gcc_p, struct type *type);
174 CORE_ADDR hppa_skip_prologue (CORE_ADDR pc);
175 CORE_ADDR hppa_skip_trampoline_code (CORE_ADDR pc);
176 int hppa_in_solib_call_trampoline (CORE_ADDR pc, char *name);
177 int hppa_in_solib_return_trampoline (CORE_ADDR pc, char *name);
178 CORE_ADDR hppa_saved_pc_after_call (struct frame_info *frame);
179 int hppa_inner_than (CORE_ADDR lhs, CORE_ADDR rhs);
180 CORE_ADDR hppa32_stack_align (CORE_ADDR sp);
181 CORE_ADDR hppa64_stack_align (CORE_ADDR sp);
182 int hppa_pc_requires_run_before_use (CORE_ADDR pc);
183 int hppa_instruction_nullified (void);
184 int hppa_register_raw_size (int reg_nr);
185 int hppa_register_byte (int reg_nr);
186 struct type * hppa32_register_virtual_type (int reg_nr);
187 struct type * hppa64_register_virtual_type (int reg_nr);
188 void hppa_store_struct_return (CORE_ADDR addr, CORE_ADDR sp);
189 void hppa32_extract_return_value (struct type *type, char *regbuf,
190                                   char *valbuf);
191 void hppa64_extract_return_value (struct type *type, char *regbuf,
192                                   char *valbuf);
193 int hppa32_use_struct_convention (int gcc_p, struct type *type);
194 int hppa64_use_struct_convention (int gcc_p, struct type *type);
195 void hppa32_store_return_value (struct type *type, char *valbuf);
196 void hppa64_store_return_value (struct type *type, char *valbuf);
197 CORE_ADDR hppa_extract_struct_value_address (char *regbuf);
198 int hppa_cannot_store_register (int regnum);
199 void hppa_init_extra_frame_info (int fromleaf, struct frame_info *frame);
200 CORE_ADDR hppa_frame_chain (struct frame_info *frame);
201 int hppa_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe);
202 int hppa_frameless_function_invocation (struct frame_info *frame);
203 CORE_ADDR hppa_frame_saved_pc (struct frame_info *frame);
204 CORE_ADDR hppa_frame_args_address (struct frame_info *fi);
205 int hppa_frame_num_args (struct frame_info *frame);
206 void hppa_push_dummy_frame (void);
207 void hppa_pop_frame (void);
208 CORE_ADDR hppa_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun,
209                                int nargs, struct value **args,
210                                struct type *type, int gcc_p);
211 CORE_ADDR hppa_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
212                                int struct_return, CORE_ADDR struct_addr);
213 CORE_ADDR hppa_smash_text_address (CORE_ADDR addr);
214 CORE_ADDR hppa_target_read_pc (ptid_t ptid);
215 void hppa_target_write_pc (CORE_ADDR v, ptid_t ptid);
216 CORE_ADDR hppa_target_read_fp (void);
217
218 typedef struct
219   {
220     struct minimal_symbol *msym;
221     CORE_ADDR solib_handle;
222     CORE_ADDR return_val;
223   }
224 args_for_find_stub;
225
226 static int cover_find_stub_with_shl_get (void *);
227
228 static int is_pa_2 = 0;         /* False */
229
230 /* This is declared in symtab.c; set to 1 in hp-symtab-read.c */
231 extern int hp_som_som_object_present;
232
233 /* In breakpoint.c */
234 extern int exception_catchpoints_are_fragile;
235
236 /* Should call_function allocate stack space for a struct return?  */
237
238 int
239 hppa32_use_struct_convention (int gcc_p, struct type *type)
240 {
241   return (TYPE_LENGTH (type) > 2 * DEPRECATED_REGISTER_SIZE);
242 }
243
244 /* Same as hppa32_use_struct_convention() for the PA64 ABI.  */
245
246 int
247 hppa64_use_struct_convention (int gcc_p, struct type *type)
248 {
249   /* RM: struct upto 128 bits are returned in registers */
250   return TYPE_LENGTH (type) > 16;
251 }
252
253 /* Routines to extract various sized constants out of hppa 
254    instructions. */
255
256 /* This assumes that no garbage lies outside of the lower bits of 
257    value. */
258
259 static int
260 sign_extend (unsigned val, unsigned bits)
261 {
262   return (int) (val >> (bits - 1) ? (-1 << bits) | val : val);
263 }
264
265 /* For many immediate values the sign bit is the low bit! */
266
267 static int
268 low_sign_extend (unsigned val, unsigned bits)
269 {
270   return (int) ((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
271 }
272
273 /* Extract the bits at positions between FROM and TO, using HP's numbering
274    (MSB = 0). */
275
276 static int
277 get_field (unsigned word, int from, int to)
278 {
279   return ((word) >> (31 - (to)) & ((1 << ((to) - (from) + 1)) - 1));
280 }
281
282 /* extract the immediate field from a ld{bhw}s instruction */
283
284 static int
285 extract_5_load (unsigned word)
286 {
287   return low_sign_extend (word >> 16 & MASK_5, 5);
288 }
289
290 /* extract the immediate field from a break instruction */
291
292 static unsigned
293 extract_5r_store (unsigned word)
294 {
295   return (word & MASK_5);
296 }
297
298 /* extract the immediate field from a {sr}sm instruction */
299
300 static unsigned
301 extract_5R_store (unsigned word)
302 {
303   return (word >> 16 & MASK_5);
304 }
305
306 /* extract a 14 bit immediate field */
307
308 static int
309 extract_14 (unsigned word)
310 {
311   return low_sign_extend (word & MASK_14, 14);
312 }
313
314 /* deposit a 14 bit constant in a word */
315
316 static unsigned
317 deposit_14 (int opnd, unsigned word)
318 {
319   unsigned sign = (opnd < 0 ? 1 : 0);
320
321   return word | ((unsigned) opnd << 1 & MASK_14) | sign;
322 }
323
324 /* extract a 21 bit constant */
325
326 static int
327 extract_21 (unsigned word)
328 {
329   int val;
330
331   word &= MASK_21;
332   word <<= 11;
333   val = get_field (word, 20, 20);
334   val <<= 11;
335   val |= get_field (word, 9, 19);
336   val <<= 2;
337   val |= get_field (word, 5, 6);
338   val <<= 5;
339   val |= get_field (word, 0, 4);
340   val <<= 2;
341   val |= get_field (word, 7, 8);
342   return sign_extend (val, 21) << 11;
343 }
344
345 /* deposit a 21 bit constant in a word. Although 21 bit constants are
346    usually the top 21 bits of a 32 bit constant, we assume that only
347    the low 21 bits of opnd are relevant */
348
349 static unsigned
350 deposit_21 (unsigned opnd, unsigned word)
351 {
352   unsigned val = 0;
353
354   val |= get_field (opnd, 11 + 14, 11 + 18);
355   val <<= 2;
356   val |= get_field (opnd, 11 + 12, 11 + 13);
357   val <<= 2;
358   val |= get_field (opnd, 11 + 19, 11 + 20);
359   val <<= 11;
360   val |= get_field (opnd, 11 + 1, 11 + 11);
361   val <<= 1;
362   val |= get_field (opnd, 11 + 0, 11 + 0);
363   return word | val;
364 }
365
366 /* extract a 17 bit constant from branch instructions, returning the
367    19 bit signed value. */
368
369 static int
370 extract_17 (unsigned word)
371 {
372   return sign_extend (get_field (word, 19, 28) |
373                       get_field (word, 29, 29) << 10 |
374                       get_field (word, 11, 15) << 11 |
375                       (word & 0x1) << 16, 17) << 2;
376 }
377 \f
378
379 /* Compare the start address for two unwind entries returning 1 if 
380    the first address is larger than the second, -1 if the second is
381    larger than the first, and zero if they are equal.  */
382
383 static int
384 compare_unwind_entries (const void *arg1, const void *arg2)
385 {
386   const struct unwind_table_entry *a = arg1;
387   const struct unwind_table_entry *b = arg2;
388
389   if (a->region_start > b->region_start)
390     return 1;
391   else if (a->region_start < b->region_start)
392     return -1;
393   else
394     return 0;
395 }
396
397 static CORE_ADDR low_text_segment_address;
398
399 static void
400 record_text_segment_lowaddr (bfd *abfd, asection *section, void *ignored)
401 {
402   if (((section->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
403        == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
404       && section->vma < low_text_segment_address)
405     low_text_segment_address = section->vma;
406 }
407
408 static void
409 internalize_unwinds (struct objfile *objfile, struct unwind_table_entry *table,
410                      asection *section, unsigned int entries, unsigned int size,
411                      CORE_ADDR text_offset)
412 {
413   /* We will read the unwind entries into temporary memory, then
414      fill in the actual unwind table.  */
415   if (size > 0)
416     {
417       unsigned long tmp;
418       unsigned i;
419       char *buf = alloca (size);
420
421       low_text_segment_address = -1;
422
423       /* If addresses are 64 bits wide, then unwinds are supposed to
424          be segment relative offsets instead of absolute addresses. 
425
426          Note that when loading a shared library (text_offset != 0) the
427          unwinds are already relative to the text_offset that will be
428          passed in.  */
429       if (TARGET_PTR_BIT == 64 && text_offset == 0)
430         {
431           bfd_map_over_sections (objfile->obfd,
432                                  record_text_segment_lowaddr, NULL);
433
434           /* ?!? Mask off some low bits.  Should this instead subtract
435              out the lowest section's filepos or something like that?
436              This looks very hokey to me.  */
437           low_text_segment_address &= ~0xfff;
438           text_offset += low_text_segment_address;
439         }
440
441       bfd_get_section_contents (objfile->obfd, section, buf, 0, size);
442
443       /* Now internalize the information being careful to handle host/target
444          endian issues.  */
445       for (i = 0; i < entries; i++)
446         {
447           table[i].region_start = bfd_get_32 (objfile->obfd,
448                                               (bfd_byte *) buf);
449           table[i].region_start += text_offset;
450           buf += 4;
451           table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
452           table[i].region_end += text_offset;
453           buf += 4;
454           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
455           buf += 4;
456           table[i].Cannot_unwind = (tmp >> 31) & 0x1;
457           table[i].Millicode = (tmp >> 30) & 0x1;
458           table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
459           table[i].Region_description = (tmp >> 27) & 0x3;
460           table[i].reserved1 = (tmp >> 26) & 0x1;
461           table[i].Entry_SR = (tmp >> 25) & 0x1;
462           table[i].Entry_FR = (tmp >> 21) & 0xf;
463           table[i].Entry_GR = (tmp >> 16) & 0x1f;
464           table[i].Args_stored = (tmp >> 15) & 0x1;
465           table[i].Variable_Frame = (tmp >> 14) & 0x1;
466           table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
467           table[i].Frame_Extension_Millicode = (tmp >> 12) & 0x1;
468           table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
469           table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
470           table[i].Ada_Region = (tmp >> 9) & 0x1;
471           table[i].cxx_info = (tmp >> 8) & 0x1;
472           table[i].cxx_try_catch = (tmp >> 7) & 0x1;
473           table[i].sched_entry_seq = (tmp >> 6) & 0x1;
474           table[i].reserved2 = (tmp >> 5) & 0x1;
475           table[i].Save_SP = (tmp >> 4) & 0x1;
476           table[i].Save_RP = (tmp >> 3) & 0x1;
477           table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
478           table[i].extn_ptr_defined = (tmp >> 1) & 0x1;
479           table[i].Cleanup_defined = tmp & 0x1;
480           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
481           buf += 4;
482           table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
483           table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
484           table[i].Large_frame = (tmp >> 29) & 0x1;
485           table[i].Pseudo_SP_Set = (tmp >> 28) & 0x1;
486           table[i].reserved4 = (tmp >> 27) & 0x1;
487           table[i].Total_frame_size = tmp & 0x7ffffff;
488
489           /* Stub unwinds are handled elsewhere. */
490           table[i].stub_unwind.stub_type = 0;
491           table[i].stub_unwind.padding = 0;
492         }
493     }
494 }
495
496 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
497    the object file.  This info is used mainly by find_unwind_entry() to find
498    out the stack frame size and frame pointer used by procedures.  We put
499    everything on the psymbol obstack in the objfile so that it automatically
500    gets freed when the objfile is destroyed.  */
501
502 static void
503 read_unwind_info (struct objfile *objfile)
504 {
505   asection *unwind_sec, *stub_unwind_sec;
506   unsigned unwind_size, stub_unwind_size, total_size;
507   unsigned index, unwind_entries;
508   unsigned stub_entries, total_entries;
509   CORE_ADDR text_offset;
510   struct obj_unwind_info *ui;
511   obj_private_data_t *obj_private;
512
513   text_offset = ANOFFSET (objfile->section_offsets, 0);
514   ui = (struct obj_unwind_info *) obstack_alloc (&objfile->psymbol_obstack,
515                                            sizeof (struct obj_unwind_info));
516
517   ui->table = NULL;
518   ui->cache = NULL;
519   ui->last = -1;
520
521   /* For reasons unknown the HP PA64 tools generate multiple unwinder
522      sections in a single executable.  So we just iterate over every
523      section in the BFD looking for unwinder sections intead of trying
524      to do a lookup with bfd_get_section_by_name. 
525
526      First determine the total size of the unwind tables so that we
527      can allocate memory in a nice big hunk.  */
528   total_entries = 0;
529   for (unwind_sec = objfile->obfd->sections;
530        unwind_sec;
531        unwind_sec = unwind_sec->next)
532     {
533       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
534           || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
535         {
536           unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
537           unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
538
539           total_entries += unwind_entries;
540         }
541     }
542
543   /* Now compute the size of the stub unwinds.  Note the ELF tools do not
544      use stub unwinds at the curren time.  */
545   stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
546
547   if (stub_unwind_sec)
548     {
549       stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
550       stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
551     }
552   else
553     {
554       stub_unwind_size = 0;
555       stub_entries = 0;
556     }
557
558   /* Compute total number of unwind entries and their total size.  */
559   total_entries += stub_entries;
560   total_size = total_entries * sizeof (struct unwind_table_entry);
561
562   /* Allocate memory for the unwind table.  */
563   ui->table = (struct unwind_table_entry *)
564     obstack_alloc (&objfile->psymbol_obstack, total_size);
565   ui->last = total_entries - 1;
566
567   /* Now read in each unwind section and internalize the standard unwind
568      entries.  */
569   index = 0;
570   for (unwind_sec = objfile->obfd->sections;
571        unwind_sec;
572        unwind_sec = unwind_sec->next)
573     {
574       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
575           || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
576         {
577           unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
578           unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
579
580           internalize_unwinds (objfile, &ui->table[index], unwind_sec,
581                                unwind_entries, unwind_size, text_offset);
582           index += unwind_entries;
583         }
584     }
585
586   /* Now read in and internalize the stub unwind entries.  */
587   if (stub_unwind_size > 0)
588     {
589       unsigned int i;
590       char *buf = alloca (stub_unwind_size);
591
592       /* Read in the stub unwind entries.  */
593       bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
594                                 0, stub_unwind_size);
595
596       /* Now convert them into regular unwind entries.  */
597       for (i = 0; i < stub_entries; i++, index++)
598         {
599           /* Clear out the next unwind entry.  */
600           memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));
601
602           /* Convert offset & size into region_start and region_end.  
603              Stuff away the stub type into "reserved" fields.  */
604           ui->table[index].region_start = bfd_get_32 (objfile->obfd,
605                                                       (bfd_byte *) buf);
606           ui->table[index].region_start += text_offset;
607           buf += 4;
608           ui->table[index].stub_unwind.stub_type = bfd_get_8 (objfile->obfd,
609                                                           (bfd_byte *) buf);
610           buf += 2;
611           ui->table[index].region_end
612             = ui->table[index].region_start + 4 *
613             (bfd_get_16 (objfile->obfd, (bfd_byte *) buf) - 1);
614           buf += 2;
615         }
616
617     }
618
619   /* Unwind table needs to be kept sorted.  */
620   qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
621          compare_unwind_entries);
622
623   /* Keep a pointer to the unwind information.  */
624   if (objfile->obj_private == NULL)
625     {
626       obj_private = (obj_private_data_t *)
627         obstack_alloc (&objfile->psymbol_obstack,
628                        sizeof (obj_private_data_t));
629       obj_private->unwind_info = NULL;
630       obj_private->so_info = NULL;
631       obj_private->dp = 0;
632
633       objfile->obj_private = obj_private;
634     }
635   obj_private = (obj_private_data_t *) objfile->obj_private;
636   obj_private->unwind_info = ui;
637 }
638
639 /* Lookup the unwind (stack backtrace) info for the given PC.  We search all
640    of the objfiles seeking the unwind table entry for this PC.  Each objfile
641    contains a sorted list of struct unwind_table_entry.  Since we do a binary
642    search of the unwind tables, we depend upon them to be sorted.  */
643
644 struct unwind_table_entry *
645 find_unwind_entry (CORE_ADDR pc)
646 {
647   int first, middle, last;
648   struct objfile *objfile;
649
650   /* A function at address 0?  Not in HP-UX! */
651   if (pc == (CORE_ADDR) 0)
652     return NULL;
653
654   ALL_OBJFILES (objfile)
655   {
656     struct obj_unwind_info *ui;
657     ui = NULL;
658     if (objfile->obj_private)
659       ui = ((obj_private_data_t *) (objfile->obj_private))->unwind_info;
660
661     if (!ui)
662       {
663         read_unwind_info (objfile);
664         if (objfile->obj_private == NULL)
665           error ("Internal error reading unwind information.");
666         ui = ((obj_private_data_t *) (objfile->obj_private))->unwind_info;
667       }
668
669     /* First, check the cache */
670
671     if (ui->cache
672         && pc >= ui->cache->region_start
673         && pc <= ui->cache->region_end)
674       return ui->cache;
675
676     /* Not in the cache, do a binary search */
677
678     first = 0;
679     last = ui->last;
680
681     while (first <= last)
682       {
683         middle = (first + last) / 2;
684         if (pc >= ui->table[middle].region_start
685             && pc <= ui->table[middle].region_end)
686           {
687             ui->cache = &ui->table[middle];
688             return &ui->table[middle];
689           }
690
691         if (pc < ui->table[middle].region_start)
692           last = middle - 1;
693         else
694           first = middle + 1;
695       }
696   }                             /* ALL_OBJFILES() */
697   return NULL;
698 }
699
700 const unsigned char *
701 hppa_breakpoint_from_pc (CORE_ADDR *pc, int *len)
702 {
703   static const char breakpoint[] = {0x00, 0x01, 0x00, 0x04};
704   (*len) = sizeof (breakpoint);
705   return breakpoint;
706 }
707
708 /* Return the name of a register.  */
709
710 const char *
711 hppa32_register_name (int i)
712 {
713   static char *names[] = {
714     "flags",  "r1",      "rp",     "r3",
715     "r4",     "r5",      "r6",     "r7",
716     "r8",     "r9",      "r10",    "r11",
717     "r12",    "r13",     "r14",    "r15",
718     "r16",    "r17",     "r18",    "r19",
719     "r20",    "r21",     "r22",    "r23",
720     "r24",    "r25",     "r26",    "dp",
721     "ret0",   "ret1",    "sp",     "r31",
722     "sar",    "pcoqh",   "pcsqh",  "pcoqt",
723     "pcsqt",  "eiem",    "iir",    "isr",
724     "ior",    "ipsw",    "goto",   "sr4",
725     "sr0",    "sr1",     "sr2",    "sr3",
726     "sr5",    "sr6",     "sr7",    "cr0",
727     "cr8",    "cr9",     "ccr",    "cr12",
728     "cr13",   "cr24",    "cr25",   "cr26",
729     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
730     "fpsr",    "fpe1",   "fpe2",   "fpe3",
731     "fpe4",   "fpe5",    "fpe6",   "fpe7",
732     "fr4",     "fr4R",   "fr5",    "fr5R",
733     "fr6",    "fr6R",    "fr7",    "fr7R",
734     "fr8",     "fr8R",   "fr9",    "fr9R",
735     "fr10",   "fr10R",   "fr11",   "fr11R",
736     "fr12",    "fr12R",  "fr13",   "fr13R",
737     "fr14",   "fr14R",   "fr15",   "fr15R",
738     "fr16",    "fr16R",  "fr17",   "fr17R",
739     "fr18",   "fr18R",   "fr19",   "fr19R",
740     "fr20",    "fr20R",  "fr21",   "fr21R",
741     "fr22",   "fr22R",   "fr23",   "fr23R",
742     "fr24",    "fr24R",  "fr25",   "fr25R",
743     "fr26",   "fr26R",   "fr27",   "fr27R",
744     "fr28",    "fr28R",  "fr29",   "fr29R",
745     "fr30",   "fr30R",   "fr31",   "fr31R"
746   };
747   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
748     return NULL;
749   else
750     return names[i];
751 }
752
753 const char *
754 hppa64_register_name (int i)
755 {
756   static char *names[] = {
757     "flags",  "r1",      "rp",     "r3",
758     "r4",     "r5",      "r6",     "r7",
759     "r8",     "r9",      "r10",    "r11",
760     "r12",    "r13",     "r14",    "r15",
761     "r16",    "r17",     "r18",    "r19",
762     "r20",    "r21",     "r22",    "r23",
763     "r24",    "r25",     "r26",    "dp",
764     "ret0",   "ret1",    "sp",     "r31",
765     "sar",    "pcoqh",   "pcsqh",  "pcoqt",
766     "pcsqt",  "eiem",    "iir",    "isr",
767     "ior",    "ipsw",    "goto",   "sr4",
768     "sr0",    "sr1",     "sr2",    "sr3",
769     "sr5",    "sr6",     "sr7",    "cr0",
770     "cr8",    "cr9",     "ccr",    "cr12",
771     "cr13",   "cr24",    "cr25",   "cr26",
772     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
773     "fpsr",    "fpe1",   "fpe2",   "fpe3",
774     "fr4",    "fr5",     "fr6",    "fr7",
775     "fr8",     "fr9",    "fr10",   "fr11",
776     "fr12",   "fr13",    "fr14",   "fr15",
777     "fr16",    "fr17",   "fr18",   "fr19",
778     "fr20",   "fr21",    "fr22",   "fr23",
779     "fr24",    "fr25",   "fr26",   "fr27",
780     "fr28",  "fr29",    "fr30",   "fr31"
781   };
782   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
783     return NULL;
784   else
785     return names[i];
786 }
787
788
789
790 /* Return the adjustment necessary to make for addresses on the stack
791    as presented by hpread.c.
792
793    This is necessary because of the stack direction on the PA and the
794    bizarre way in which someone (?) decided they wanted to handle
795    frame pointerless code in GDB.  */
796 int
797 hpread_adjust_stack_address (CORE_ADDR func_addr)
798 {
799   struct unwind_table_entry *u;
800
801   u = find_unwind_entry (func_addr);
802   if (!u)
803     return 0;
804   else
805     return u->Total_frame_size << 3;
806 }
807
808 /* Called to determine if PC is in an interrupt handler of some
809    kind.  */
810
811 static int
812 pc_in_interrupt_handler (CORE_ADDR pc)
813 {
814   struct unwind_table_entry *u;
815   struct minimal_symbol *msym_us;
816
817   u = find_unwind_entry (pc);
818   if (!u)
819     return 0;
820
821   /* Oh joys.  HPUX sets the interrupt bit for _sigreturn even though
822      its frame isn't a pure interrupt frame.  Deal with this.  */
823   msym_us = lookup_minimal_symbol_by_pc (pc);
824
825   return (u->HP_UX_interrupt_marker
826           && !PC_IN_SIGTRAMP (pc, DEPRECATED_SYMBOL_NAME (msym_us)));
827 }
828
829 /* Called when no unwind descriptor was found for PC.  Returns 1 if it
830    appears that PC is in a linker stub.
831
832    ?!? Need to handle stubs which appear in PA64 code.  */
833
834 static int
835 pc_in_linker_stub (CORE_ADDR pc)
836 {
837   int found_magic_instruction = 0;
838   int i;
839   char buf[4];
840
841   /* If unable to read memory, assume pc is not in a linker stub.  */
842   if (target_read_memory (pc, buf, 4) != 0)
843     return 0;
844
845   /* We are looking for something like
846
847      ; $$dyncall jams RP into this special spot in the frame (RP')
848      ; before calling the "call stub"
849      ldw     -18(sp),rp
850
851      ldsid   (rp),r1         ; Get space associated with RP into r1
852      mtsp    r1,sp           ; Move it into space register 0
853      be,n    0(sr0),rp)      ; back to your regularly scheduled program */
854
855   /* Maximum known linker stub size is 4 instructions.  Search forward
856      from the given PC, then backward.  */
857   for (i = 0; i < 4; i++)
858     {
859       /* If we hit something with an unwind, stop searching this direction.  */
860
861       if (find_unwind_entry (pc + i * 4) != 0)
862         break;
863
864       /* Check for ldsid (rp),r1 which is the magic instruction for a 
865          return from a cross-space function call.  */
866       if (read_memory_integer (pc + i * 4, 4) == 0x004010a1)
867         {
868           found_magic_instruction = 1;
869           break;
870         }
871       /* Add code to handle long call/branch and argument relocation stubs
872          here.  */
873     }
874
875   if (found_magic_instruction != 0)
876     return 1;
877
878   /* Now look backward.  */
879   for (i = 0; i < 4; i++)
880     {
881       /* If we hit something with an unwind, stop searching this direction.  */
882
883       if (find_unwind_entry (pc - i * 4) != 0)
884         break;
885
886       /* Check for ldsid (rp),r1 which is the magic instruction for a 
887          return from a cross-space function call.  */
888       if (read_memory_integer (pc - i * 4, 4) == 0x004010a1)
889         {
890           found_magic_instruction = 1;
891           break;
892         }
893       /* Add code to handle long call/branch and argument relocation stubs
894          here.  */
895     }
896   return found_magic_instruction;
897 }
898
899 static int
900 find_return_regnum (CORE_ADDR pc)
901 {
902   struct unwind_table_entry *u;
903
904   u = find_unwind_entry (pc);
905
906   if (!u)
907     return RP_REGNUM;
908
909   if (u->Millicode)
910     return 31;
911
912   return RP_REGNUM;
913 }
914
915 /* Return size of frame, or -1 if we should use a frame pointer.  */
916 static int
917 find_proc_framesize (CORE_ADDR pc)
918 {
919   struct unwind_table_entry *u;
920   struct minimal_symbol *msym_us;
921
922   /* This may indicate a bug in our callers... */
923   if (pc == (CORE_ADDR) 0)
924     return -1;
925
926   u = find_unwind_entry (pc);
927
928   if (!u)
929     {
930       if (pc_in_linker_stub (pc))
931         /* Linker stubs have a zero size frame.  */
932         return 0;
933       else
934         return -1;
935     }
936
937   msym_us = lookup_minimal_symbol_by_pc (pc);
938
939   /* If Save_SP is set, and we're not in an interrupt or signal caller,
940      then we have a frame pointer.  Use it.  */
941   if (u->Save_SP
942       && !pc_in_interrupt_handler (pc)
943       && msym_us
944       && !PC_IN_SIGTRAMP (pc, DEPRECATED_SYMBOL_NAME (msym_us)))
945     return -1;
946
947   return u->Total_frame_size << 3;
948 }
949
950 /* Return offset from sp at which rp is saved, or 0 if not saved.  */
951 static int rp_saved (CORE_ADDR);
952
953 static int
954 rp_saved (CORE_ADDR pc)
955 {
956   struct unwind_table_entry *u;
957
958   /* A function at, and thus a return PC from, address 0?  Not in HP-UX! */
959   if (pc == (CORE_ADDR) 0)
960     return 0;
961
962   u = find_unwind_entry (pc);
963
964   if (!u)
965     {
966       if (pc_in_linker_stub (pc))
967         /* This is the so-called RP'.  */
968         return -24;
969       else
970         return 0;
971     }
972
973   if (u->Save_RP)
974     return (TARGET_PTR_BIT == 64 ? -16 : -20);
975   else if (u->stub_unwind.stub_type != 0)
976     {
977       switch (u->stub_unwind.stub_type)
978         {
979         case EXPORT:
980         case IMPORT:
981           return -24;
982         case PARAMETER_RELOCATION:
983           return -8;
984         default:
985           return 0;
986         }
987     }
988   else
989     return 0;
990 }
991 \f
992 int
993 hppa_frameless_function_invocation (struct frame_info *frame)
994 {
995   struct unwind_table_entry *u;
996
997   u = find_unwind_entry (get_frame_pc (frame));
998
999   if (u == 0)
1000     return 0;
1001
1002   return (u->Total_frame_size == 0 && u->stub_unwind.stub_type == 0);
1003 }
1004
1005 /* Immediately after a function call, return the saved pc.
1006    Can't go through the frames for this because on some machines
1007    the new frame is not set up until the new function executes
1008    some instructions.  */
1009
1010 CORE_ADDR
1011 hppa_saved_pc_after_call (struct frame_info *frame)
1012 {
1013   int ret_regnum;
1014   CORE_ADDR pc;
1015   struct unwind_table_entry *u;
1016
1017   ret_regnum = find_return_regnum (get_frame_pc (frame));
1018   pc = read_register (ret_regnum) & ~0x3;
1019
1020   /* If PC is in a linker stub, then we need to dig the address
1021      the stub will return to out of the stack.  */
1022   u = find_unwind_entry (pc);
1023   if (u && u->stub_unwind.stub_type != 0)
1024     return DEPRECATED_FRAME_SAVED_PC (frame);
1025   else
1026     return pc;
1027 }
1028 \f
1029 CORE_ADDR
1030 hppa_frame_saved_pc (struct frame_info *frame)
1031 {
1032   CORE_ADDR pc = get_frame_pc (frame);
1033   struct unwind_table_entry *u;
1034   CORE_ADDR old_pc = 0;
1035   int spun_around_loop = 0;
1036   int rp_offset = 0;
1037
1038   /* BSD, HPUX & OSF1 all lay out the hardware state in the same manner
1039      at the base of the frame in an interrupt handler.  Registers within
1040      are saved in the exact same order as GDB numbers registers.  How
1041      convienent.  */
1042   if (pc_in_interrupt_handler (pc))
1043     return read_memory_integer (get_frame_base (frame) + PC_REGNUM * 4,
1044                                 TARGET_PTR_BIT / 8) & ~0x3;
1045
1046   if ((get_frame_pc (frame) >= get_frame_base (frame)
1047        && (get_frame_pc (frame)
1048            <= (get_frame_base (frame)
1049                /* A call dummy is sized in words, but it is actually a
1050                   series of instructions.  Account for that scaling
1051                   factor.  */
1052                + ((DEPRECATED_REGISTER_SIZE / INSTRUCTION_SIZE)
1053                   * DEPRECATED_CALL_DUMMY_LENGTH)
1054                /* Similarly we have to account for 64bit wide register
1055                   saves.  */
1056                + (32 * DEPRECATED_REGISTER_SIZE)
1057                /* We always consider FP regs 8 bytes long.  */
1058                + (NUM_REGS - FP0_REGNUM) * 8
1059                /* Similarly we have to account for 64bit wide register
1060                   saves.  */
1061                + (6 * DEPRECATED_REGISTER_SIZE)))))
1062     {
1063       return read_memory_integer ((get_frame_base (frame)
1064                                    + (TARGET_PTR_BIT == 64 ? -16 : -20)),
1065                                   TARGET_PTR_BIT / 8) & ~0x3;
1066     }
1067
1068 #ifdef FRAME_SAVED_PC_IN_SIGTRAMP
1069   /* Deal with signal handler caller frames too.  */
1070   if ((get_frame_type (frame) == SIGTRAMP_FRAME))
1071     {
1072       CORE_ADDR rp;
1073       FRAME_SAVED_PC_IN_SIGTRAMP (frame, &rp);
1074       return rp & ~0x3;
1075     }
1076 #endif
1077
1078   if (hppa_frameless_function_invocation (frame))
1079     {
1080       int ret_regnum;
1081
1082       ret_regnum = find_return_regnum (pc);
1083
1084       /* If the next frame is an interrupt frame or a signal
1085          handler caller, then we need to look in the saved
1086          register area to get the return pointer (the values
1087          in the registers may not correspond to anything useful).  */
1088       if (get_next_frame (frame)
1089           && ((get_frame_type (get_next_frame (frame)) == SIGTRAMP_FRAME)
1090               || pc_in_interrupt_handler (get_frame_pc (get_next_frame (frame)))))
1091         {
1092           CORE_ADDR *saved_regs;
1093           hppa_frame_init_saved_regs (get_next_frame (frame));
1094           saved_regs = deprecated_get_frame_saved_regs (get_next_frame (frame));
1095           if (read_memory_integer (saved_regs[FLAGS_REGNUM],
1096                                    TARGET_PTR_BIT / 8) & 0x2)
1097             {
1098               pc = read_memory_integer (saved_regs[31],
1099                                         TARGET_PTR_BIT / 8) & ~0x3;
1100
1101               /* Syscalls are really two frames.  The syscall stub itself
1102                  with a return pointer in %rp and the kernel call with
1103                  a return pointer in %r31.  We return the %rp variant
1104                  if %r31 is the same as frame->pc.  */
1105               if (pc == get_frame_pc (frame))
1106                 pc = read_memory_integer (saved_regs[RP_REGNUM],
1107                                           TARGET_PTR_BIT / 8) & ~0x3;
1108             }
1109           else
1110             pc = read_memory_integer (saved_regs[RP_REGNUM],
1111                                       TARGET_PTR_BIT / 8) & ~0x3;
1112         }
1113       else
1114         pc = read_register (ret_regnum) & ~0x3;
1115     }
1116   else
1117     {
1118       spun_around_loop = 0;
1119       old_pc = pc;
1120
1121     restart:
1122       rp_offset = rp_saved (pc);
1123
1124       /* Similar to code in frameless function case.  If the next
1125          frame is a signal or interrupt handler, then dig the right
1126          information out of the saved register info.  */
1127       if (rp_offset == 0
1128           && get_next_frame (frame)
1129           && ((get_frame_type (get_next_frame (frame)) == SIGTRAMP_FRAME)
1130               || pc_in_interrupt_handler (get_frame_pc (get_next_frame (frame)))))
1131         {
1132           CORE_ADDR *saved_regs;
1133           hppa_frame_init_saved_regs (get_next_frame (frame));
1134           saved_regs = deprecated_get_frame_saved_regs (get_next_frame (frame));
1135           if (read_memory_integer (saved_regs[FLAGS_REGNUM],
1136                                    TARGET_PTR_BIT / 8) & 0x2)
1137             {
1138               pc = read_memory_integer (saved_regs[31],
1139                                         TARGET_PTR_BIT / 8) & ~0x3;
1140
1141               /* Syscalls are really two frames.  The syscall stub itself
1142                  with a return pointer in %rp and the kernel call with
1143                  a return pointer in %r31.  We return the %rp variant
1144                  if %r31 is the same as frame->pc.  */
1145               if (pc == get_frame_pc (frame))
1146                 pc = read_memory_integer (saved_regs[RP_REGNUM],
1147                                           TARGET_PTR_BIT / 8) & ~0x3;
1148             }
1149           else
1150             pc = read_memory_integer (saved_regs[RP_REGNUM],
1151                                       TARGET_PTR_BIT / 8) & ~0x3;
1152         }
1153       else if (rp_offset == 0)
1154         {
1155           old_pc = pc;
1156           pc = read_register (RP_REGNUM) & ~0x3;
1157         }
1158       else
1159         {
1160           old_pc = pc;
1161           pc = read_memory_integer (get_frame_base (frame) + rp_offset,
1162                                     TARGET_PTR_BIT / 8) & ~0x3;
1163         }
1164     }
1165
1166   /* If PC is inside a linker stub, then dig out the address the stub
1167      will return to. 
1168
1169      Don't do this for long branch stubs.  Why?  For some unknown reason
1170      _start is marked as a long branch stub in hpux10.  */
1171   u = find_unwind_entry (pc);
1172   if (u && u->stub_unwind.stub_type != 0
1173       && u->stub_unwind.stub_type != LONG_BRANCH)
1174     {
1175       unsigned int insn;
1176
1177       /* If this is a dynamic executable, and we're in a signal handler,
1178          then the call chain will eventually point us into the stub for
1179          _sigreturn.  Unlike most cases, we'll be pointed to the branch
1180          to the real sigreturn rather than the code after the real branch!. 
1181
1182          Else, try to dig the address the stub will return to in the normal
1183          fashion.  */
1184       insn = read_memory_integer (pc, 4);
1185       if ((insn & 0xfc00e000) == 0xe8000000)
1186         return (pc + extract_17 (insn) + 8) & ~0x3;
1187       else
1188         {
1189           if (old_pc == pc)
1190             spun_around_loop++;
1191
1192           if (spun_around_loop > 1)
1193             {
1194               /* We're just about to go around the loop again with
1195                  no more hope of success.  Die. */
1196               error ("Unable to find return pc for this frame");
1197             }
1198           else
1199             goto restart;
1200         }
1201     }
1202
1203   return pc;
1204 }
1205 \f
1206 /* We need to correct the PC and the FP for the outermost frame when we are
1207    in a system call.  */
1208
1209 void
1210 hppa_init_extra_frame_info (int fromleaf, struct frame_info *frame)
1211 {
1212   int flags;
1213   int framesize;
1214
1215   if (get_next_frame (frame) && !fromleaf)
1216     return;
1217
1218   /* If the next frame represents a frameless function invocation then
1219      we have to do some adjustments that are normally done by
1220      DEPRECATED_FRAME_CHAIN.  (DEPRECATED_FRAME_CHAIN is not called in
1221      this case.)  */
1222   if (fromleaf)
1223     {
1224       /* Find the framesize of *this* frame without peeking at the PC
1225          in the current frame structure (it isn't set yet).  */
1226       framesize = find_proc_framesize (DEPRECATED_FRAME_SAVED_PC (get_next_frame (frame)));
1227
1228       /* Now adjust our base frame accordingly.  If we have a frame pointer
1229          use it, else subtract the size of this frame from the current
1230          frame.  (we always want frame->frame to point at the lowest address
1231          in the frame).  */
1232       if (framesize == -1)
1233         deprecated_update_frame_base_hack (frame, deprecated_read_fp ());
1234       else
1235         deprecated_update_frame_base_hack (frame, get_frame_base (frame) - framesize);
1236       return;
1237     }
1238
1239   flags = read_register (FLAGS_REGNUM);
1240   if (flags & 2)                /* In system call? */
1241     deprecated_update_frame_pc_hack (frame, read_register (31) & ~0x3);
1242
1243   /* The outermost frame is always derived from PC-framesize
1244
1245      One might think frameless innermost frames should have
1246      a frame->frame that is the same as the parent's frame->frame.
1247      That is wrong; frame->frame in that case should be the *high*
1248      address of the parent's frame.  It's complicated as hell to
1249      explain, but the parent *always* creates some stack space for
1250      the child.  So the child actually does have a frame of some
1251      sorts, and its base is the high address in its parent's frame.  */
1252   framesize = find_proc_framesize (get_frame_pc (frame));
1253   if (framesize == -1)
1254     deprecated_update_frame_base_hack (frame, deprecated_read_fp ());
1255   else
1256     deprecated_update_frame_base_hack (frame, read_register (SP_REGNUM) - framesize);
1257 }
1258 \f
1259 /* Given a GDB frame, determine the address of the calling function's
1260    frame.  This will be used to create a new GDB frame struct, and
1261    then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
1262    will be called for the new frame.
1263
1264    This may involve searching through prologues for several functions
1265    at boundaries where GCC calls HP C code, or where code which has
1266    a frame pointer calls code without a frame pointer.  */
1267
1268 CORE_ADDR
1269 hppa_frame_chain (struct frame_info *frame)
1270 {
1271   int my_framesize, caller_framesize;
1272   struct unwind_table_entry *u;
1273   CORE_ADDR frame_base;
1274   struct frame_info *tmp_frame;
1275
1276   /* A frame in the current frame list, or zero.  */
1277   struct frame_info *saved_regs_frame = 0;
1278   /* Where the registers were saved in saved_regs_frame.  If
1279      saved_regs_frame is zero, this is garbage.  */
1280   CORE_ADDR *saved_regs = NULL;
1281
1282   CORE_ADDR caller_pc;
1283
1284   struct minimal_symbol *min_frame_symbol;
1285   struct symbol *frame_symbol;
1286   char *frame_symbol_name;
1287
1288   /* If this is a threaded application, and we see the
1289      routine "__pthread_exit", treat it as the stack root
1290      for this thread. */
1291   min_frame_symbol = lookup_minimal_symbol_by_pc (get_frame_pc (frame));
1292   frame_symbol = find_pc_function (get_frame_pc (frame));
1293
1294   if ((min_frame_symbol != 0) /* && (frame_symbol == 0) */ )
1295     {
1296       /* The test above for "no user function name" would defend
1297          against the slim likelihood that a user might define a
1298          routine named "__pthread_exit" and then try to debug it.
1299
1300          If it weren't commented out, and you tried to debug the
1301          pthread library itself, you'd get errors.
1302
1303          So for today, we don't make that check. */
1304       frame_symbol_name = DEPRECATED_SYMBOL_NAME (min_frame_symbol);
1305       if (frame_symbol_name != 0)
1306         {
1307           if (0 == strncmp (frame_symbol_name,
1308                             THREAD_INITIAL_FRAME_SYMBOL,
1309                             THREAD_INITIAL_FRAME_SYM_LEN))
1310             {
1311               /* Pretend we've reached the bottom of the stack. */
1312               return (CORE_ADDR) 0;
1313             }
1314         }
1315     }                           /* End of hacky code for threads. */
1316
1317   /* Handle HPUX, BSD, and OSF1 style interrupt frames first.  These
1318      are easy; at *sp we have a full save state strucutre which we can
1319      pull the old stack pointer from.  Also see frame_saved_pc for
1320      code to dig a saved PC out of the save state structure.  */
1321   if (pc_in_interrupt_handler (get_frame_pc (frame)))
1322     frame_base = read_memory_integer (get_frame_base (frame) + SP_REGNUM * 4,
1323                                       TARGET_PTR_BIT / 8);
1324 #ifdef FRAME_BASE_BEFORE_SIGTRAMP
1325   else if ((get_frame_type (frame) == SIGTRAMP_FRAME))
1326     {
1327       FRAME_BASE_BEFORE_SIGTRAMP (frame, &frame_base);
1328     }
1329 #endif
1330   else
1331     frame_base = get_frame_base (frame);
1332
1333   /* Get frame sizes for the current frame and the frame of the 
1334      caller.  */
1335   my_framesize = find_proc_framesize (get_frame_pc (frame));
1336   caller_pc = DEPRECATED_FRAME_SAVED_PC (frame);
1337
1338   /* If we can't determine the caller's PC, then it's not likely we can
1339      really determine anything meaningful about its frame.  We'll consider
1340      this to be stack bottom. */
1341   if (caller_pc == (CORE_ADDR) 0)
1342     return (CORE_ADDR) 0;
1343
1344   caller_framesize = find_proc_framesize (DEPRECATED_FRAME_SAVED_PC (frame));
1345
1346   /* If caller does not have a frame pointer, then its frame
1347      can be found at current_frame - caller_framesize.  */
1348   if (caller_framesize != -1)
1349     {
1350       return frame_base - caller_framesize;
1351     }
1352   /* Both caller and callee have frame pointers and are GCC compiled
1353      (SAVE_SP bit in unwind descriptor is on for both functions.
1354      The previous frame pointer is found at the top of the current frame.  */
1355   if (caller_framesize == -1 && my_framesize == -1)
1356     {
1357       return read_memory_integer (frame_base, TARGET_PTR_BIT / 8);
1358     }
1359   /* Caller has a frame pointer, but callee does not.  This is a little
1360      more difficult as GCC and HP C lay out locals and callee register save
1361      areas very differently.
1362
1363      The previous frame pointer could be in a register, or in one of 
1364      several areas on the stack.
1365
1366      Walk from the current frame to the innermost frame examining 
1367      unwind descriptors to determine if %r3 ever gets saved into the
1368      stack.  If so return whatever value got saved into the stack.
1369      If it was never saved in the stack, then the value in %r3 is still
1370      valid, so use it. 
1371
1372      We use information from unwind descriptors to determine if %r3
1373      is saved into the stack (Entry_GR field has this information).  */
1374
1375   for (tmp_frame = frame; tmp_frame; tmp_frame = get_next_frame (tmp_frame))
1376     {
1377       u = find_unwind_entry (get_frame_pc (tmp_frame));
1378
1379       if (!u)
1380         {
1381           /* We could find this information by examining prologues.  I don't
1382              think anyone has actually written any tools (not even "strip")
1383              which leave them out of an executable, so maybe this is a moot
1384              point.  */
1385           /* ??rehrauer: Actually, it's quite possible to stepi your way into
1386              code that doesn't have unwind entries.  For example, stepping into
1387              the dynamic linker will give you a PC that has none.  Thus, I've
1388              disabled this warning. */
1389 #if 0
1390           warning ("Unable to find unwind for PC 0x%x -- Help!", get_frame_pc (tmp_frame));
1391 #endif
1392           return (CORE_ADDR) 0;
1393         }
1394
1395       if (u->Save_SP
1396           || (get_frame_type (tmp_frame) == SIGTRAMP_FRAME)
1397           || pc_in_interrupt_handler (get_frame_pc (tmp_frame)))
1398         break;
1399
1400       /* Entry_GR specifies the number of callee-saved general registers
1401          saved in the stack.  It starts at %r3, so %r3 would be 1.  */
1402       if (u->Entry_GR >= 1)
1403         {
1404           /* The unwind entry claims that r3 is saved here.  However,
1405              in optimized code, GCC often doesn't actually save r3.
1406              We'll discover this if we look at the prologue.  */
1407           hppa_frame_init_saved_regs (tmp_frame);
1408           saved_regs = deprecated_get_frame_saved_regs (tmp_frame);
1409           saved_regs_frame = tmp_frame;
1410
1411           /* If we have an address for r3, that's good.  */
1412           if (saved_regs[DEPRECATED_FP_REGNUM])
1413             break;
1414         }
1415     }
1416
1417   if (tmp_frame)
1418     {
1419       /* We may have walked down the chain into a function with a frame
1420          pointer.  */
1421       if (u->Save_SP
1422           && !(get_frame_type (tmp_frame) == SIGTRAMP_FRAME)
1423           && !pc_in_interrupt_handler (get_frame_pc (tmp_frame)))
1424         {
1425           return read_memory_integer (get_frame_base (tmp_frame), TARGET_PTR_BIT / 8);
1426         }
1427       /* %r3 was saved somewhere in the stack.  Dig it out.  */
1428       else
1429         {
1430           /* Sick.
1431
1432              For optimization purposes many kernels don't have the
1433              callee saved registers into the save_state structure upon
1434              entry into the kernel for a syscall; the optimization
1435              is usually turned off if the process is being traced so
1436              that the debugger can get full register state for the
1437              process.
1438
1439              This scheme works well except for two cases:
1440
1441              * Attaching to a process when the process is in the
1442              kernel performing a system call (debugger can't get
1443              full register state for the inferior process since
1444              the process wasn't being traced when it entered the
1445              system call).
1446
1447              * Register state is not complete if the system call
1448              causes the process to core dump.
1449
1450
1451              The following heinous code is an attempt to deal with
1452              the lack of register state in a core dump.  It will
1453              fail miserably if the function which performs the
1454              system call has a variable sized stack frame.  */
1455
1456           if (tmp_frame != saved_regs_frame)
1457             {
1458               hppa_frame_init_saved_regs (tmp_frame);
1459               saved_regs = deprecated_get_frame_saved_regs (tmp_frame);
1460             }
1461
1462           /* Abominable hack.  */
1463           if (current_target.to_has_execution == 0
1464               && ((saved_regs[FLAGS_REGNUM]
1465                    && (read_memory_integer (saved_regs[FLAGS_REGNUM],
1466                                             TARGET_PTR_BIT / 8)
1467                        & 0x2))
1468                   || (saved_regs[FLAGS_REGNUM] == 0
1469                       && read_register (FLAGS_REGNUM) & 0x2)))
1470             {
1471               u = find_unwind_entry (DEPRECATED_FRAME_SAVED_PC (frame));
1472               if (!u)
1473                 {
1474                   return read_memory_integer (saved_regs[DEPRECATED_FP_REGNUM],
1475                                               TARGET_PTR_BIT / 8);
1476                 }
1477               else
1478                 {
1479                   return frame_base - (u->Total_frame_size << 3);
1480                 }
1481             }
1482
1483           return read_memory_integer (saved_regs[DEPRECATED_FP_REGNUM],
1484                                       TARGET_PTR_BIT / 8);
1485         }
1486     }
1487   else
1488     {
1489       /* Get the innermost frame.  */
1490       tmp_frame = frame;
1491       while (get_next_frame (tmp_frame) != NULL)
1492         tmp_frame = get_next_frame (tmp_frame);
1493
1494       if (tmp_frame != saved_regs_frame)
1495         {
1496           hppa_frame_init_saved_regs (tmp_frame);
1497           saved_regs = deprecated_get_frame_saved_regs (tmp_frame);
1498         }
1499
1500       /* Abominable hack.  See above.  */
1501       if (current_target.to_has_execution == 0
1502           && ((saved_regs[FLAGS_REGNUM]
1503                && (read_memory_integer (saved_regs[FLAGS_REGNUM],
1504                                         TARGET_PTR_BIT / 8)
1505                    & 0x2))
1506               || (saved_regs[FLAGS_REGNUM] == 0
1507                   && read_register (FLAGS_REGNUM) & 0x2)))
1508         {
1509           u = find_unwind_entry (DEPRECATED_FRAME_SAVED_PC (frame));
1510           if (!u)
1511             {
1512               return read_memory_integer (saved_regs[DEPRECATED_FP_REGNUM],
1513                                           TARGET_PTR_BIT / 8);
1514             }
1515           else
1516             {
1517               return frame_base - (u->Total_frame_size << 3);
1518             }
1519         }
1520
1521       /* The value in %r3 was never saved into the stack (thus %r3 still
1522          holds the value of the previous frame pointer).  */
1523       return deprecated_read_fp ();
1524     }
1525 }
1526 \f
1527
1528 /* To see if a frame chain is valid, see if the caller looks like it
1529    was compiled with gcc. */
1530
1531 int
1532 hppa_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
1533 {
1534   struct minimal_symbol *msym_us;
1535   struct minimal_symbol *msym_start;
1536   struct unwind_table_entry *u, *next_u = NULL;
1537   struct frame_info *next;
1538
1539   u = find_unwind_entry (get_frame_pc (thisframe));
1540
1541   if (u == NULL)
1542     return 1;
1543
1544   /* We can't just check that the same of msym_us is "_start", because
1545      someone idiotically decided that they were going to make a Ltext_end
1546      symbol with the same address.  This Ltext_end symbol is totally
1547      indistinguishable (as nearly as I can tell) from the symbol for a function
1548      which is (legitimately, since it is in the user's namespace)
1549      named Ltext_end, so we can't just ignore it.  */
1550   msym_us = lookup_minimal_symbol_by_pc (DEPRECATED_FRAME_SAVED_PC (thisframe));
1551   msym_start = lookup_minimal_symbol ("_start", NULL, NULL);
1552   if (msym_us
1553       && msym_start
1554       && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
1555     return 0;
1556
1557   /* Grrrr.  Some new idiot decided that they don't want _start for the
1558      PRO configurations; $START$ calls main directly....  Deal with it.  */
1559   msym_start = lookup_minimal_symbol ("$START$", NULL, NULL);
1560   if (msym_us
1561       && msym_start
1562       && SYMBOL_VALUE_ADDRESS (msym_us) == SYMBOL_VALUE_ADDRESS (msym_start))
1563     return 0;
1564
1565   next = get_next_frame (thisframe);
1566   if (next)
1567     next_u = find_unwind_entry (get_frame_pc (next));
1568
1569   /* If this frame does not save SP, has no stack, isn't a stub,
1570      and doesn't "call" an interrupt routine or signal handler caller,
1571      then its not valid.  */
1572   if (u->Save_SP || u->Total_frame_size || u->stub_unwind.stub_type != 0
1573       || (get_next_frame (thisframe) && (get_frame_type (get_next_frame (thisframe)) == SIGTRAMP_FRAME))
1574       || (next_u && next_u->HP_UX_interrupt_marker))
1575     return 1;
1576
1577   if (pc_in_linker_stub (get_frame_pc (thisframe)))
1578     return 1;
1579
1580   return 0;
1581 }
1582
1583 /* These functions deal with saving and restoring register state
1584    around a function call in the inferior.  They keep the stack
1585    double-word aligned;  eventually, on an hp700, the stack will have
1586    to be aligned to a 64-byte boundary.  */
1587
1588 void
1589 hppa_push_dummy_frame (void)
1590 {
1591   CORE_ADDR sp, pc, pcspace;
1592   int regnum;
1593   CORE_ADDR int_buffer;
1594   double freg_buffer;
1595
1596   pc = hppa_target_read_pc (inferior_ptid);
1597   int_buffer = read_register (FLAGS_REGNUM);
1598   if (int_buffer & 0x2)
1599     {
1600       const unsigned int sid = (pc >> 30) & 0x3;
1601       if (sid == 0)
1602         pcspace = read_register (SR4_REGNUM);
1603       else
1604         pcspace = read_register (SR4_REGNUM + 4 + sid);
1605     }
1606   else
1607     pcspace = read_register (PCSQ_HEAD_REGNUM);
1608
1609   /* Space for "arguments"; the RP goes in here. */
1610   sp = read_register (SP_REGNUM) + 48;
1611   int_buffer = read_register (RP_REGNUM) | 0x3;
1612
1613   /* The 32bit and 64bit ABIs save the return pointer into different
1614      stack slots.  */
1615   if (DEPRECATED_REGISTER_SIZE == 8)
1616     write_memory (sp - 16, (char *) &int_buffer, DEPRECATED_REGISTER_SIZE);
1617   else
1618     write_memory (sp - 20, (char *) &int_buffer, DEPRECATED_REGISTER_SIZE);
1619
1620   int_buffer = deprecated_read_fp ();
1621   write_memory (sp, (char *) &int_buffer, DEPRECATED_REGISTER_SIZE);
1622
1623   write_register (DEPRECATED_FP_REGNUM, sp);
1624
1625   sp += 2 * DEPRECATED_REGISTER_SIZE;
1626
1627   for (regnum = 1; regnum < 32; regnum++)
1628     if (regnum != RP_REGNUM && regnum != DEPRECATED_FP_REGNUM)
1629       sp = push_word (sp, read_register (regnum));
1630
1631   /* This is not necessary for the 64bit ABI.  In fact it is dangerous.  */
1632   if (DEPRECATED_REGISTER_SIZE != 8)
1633     sp += 4;
1634
1635   for (regnum = FP0_REGNUM; regnum < NUM_REGS; regnum++)
1636     {
1637       deprecated_read_register_bytes (DEPRECATED_REGISTER_BYTE (regnum),
1638                                       (char *) &freg_buffer, 8);
1639       sp = push_bytes (sp, (char *) &freg_buffer, 8);
1640     }
1641   sp = push_word (sp, read_register (IPSW_REGNUM));
1642   sp = push_word (sp, read_register (SAR_REGNUM));
1643   sp = push_word (sp, pc);
1644   sp = push_word (sp, pcspace);
1645   sp = push_word (sp, pc + 4);
1646   sp = push_word (sp, pcspace);
1647   write_register (SP_REGNUM, sp);
1648 }
1649
1650 static void
1651 find_dummy_frame_regs (struct frame_info *frame,
1652                        CORE_ADDR frame_saved_regs[])
1653 {
1654   CORE_ADDR fp = get_frame_base (frame);
1655   int i;
1656
1657   /* The 32bit and 64bit ABIs save RP into different locations.  */
1658   if (DEPRECATED_REGISTER_SIZE == 8)
1659     frame_saved_regs[RP_REGNUM] = (fp - 16) & ~0x3;
1660   else
1661     frame_saved_regs[RP_REGNUM] = (fp - 20) & ~0x3;
1662
1663   frame_saved_regs[DEPRECATED_FP_REGNUM] = fp;
1664
1665   frame_saved_regs[1] = fp + (2 * DEPRECATED_REGISTER_SIZE);
1666
1667   for (fp += 3 * DEPRECATED_REGISTER_SIZE, i = 3; i < 32; i++)
1668     {
1669       if (i != DEPRECATED_FP_REGNUM)
1670         {
1671           frame_saved_regs[i] = fp;
1672           fp += DEPRECATED_REGISTER_SIZE;
1673         }
1674     }
1675
1676   /* This is not necessary or desirable for the 64bit ABI.  */
1677   if (DEPRECATED_REGISTER_SIZE != 8)
1678     fp += 4;
1679
1680   for (i = FP0_REGNUM; i < NUM_REGS; i++, fp += 8)
1681     frame_saved_regs[i] = fp;
1682
1683   frame_saved_regs[IPSW_REGNUM] = fp;
1684   frame_saved_regs[SAR_REGNUM] = fp + DEPRECATED_REGISTER_SIZE;
1685   frame_saved_regs[PCOQ_HEAD_REGNUM] = fp + 2 * DEPRECATED_REGISTER_SIZE;
1686   frame_saved_regs[PCSQ_HEAD_REGNUM] = fp + 3 * DEPRECATED_REGISTER_SIZE;
1687   frame_saved_regs[PCOQ_TAIL_REGNUM] = fp + 4 * DEPRECATED_REGISTER_SIZE;
1688   frame_saved_regs[PCSQ_TAIL_REGNUM] = fp + 5 * DEPRECATED_REGISTER_SIZE;
1689 }
1690
1691 void
1692 hppa_pop_frame (void)
1693 {
1694   struct frame_info *frame = get_current_frame ();
1695   CORE_ADDR fp, npc, target_pc;
1696   int regnum;
1697   CORE_ADDR *fsr;
1698   double freg_buffer;
1699
1700   fp = get_frame_base (frame);
1701   hppa_frame_init_saved_regs (frame);
1702   fsr = deprecated_get_frame_saved_regs (frame);
1703
1704 #ifndef NO_PC_SPACE_QUEUE_RESTORE
1705   if (fsr[IPSW_REGNUM]) /* Restoring a call dummy frame */
1706     restore_pc_queue (fsr);
1707 #endif
1708
1709   for (regnum = 31; regnum > 0; regnum--)
1710     if (fsr[regnum])
1711       write_register (regnum, read_memory_integer (fsr[regnum],
1712                                                    DEPRECATED_REGISTER_SIZE));
1713
1714   for (regnum = NUM_REGS - 1; regnum >= FP0_REGNUM; regnum--)
1715     if (fsr[regnum])
1716       {
1717         read_memory (fsr[regnum], (char *) &freg_buffer, 8);
1718         deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (regnum),
1719                                          (char *) &freg_buffer, 8);
1720       }
1721
1722   if (fsr[IPSW_REGNUM])
1723     write_register (IPSW_REGNUM,
1724                     read_memory_integer (fsr[IPSW_REGNUM],
1725                                          DEPRECATED_REGISTER_SIZE));
1726
1727   if (fsr[SAR_REGNUM])
1728     write_register (SAR_REGNUM,
1729                     read_memory_integer (fsr[SAR_REGNUM],
1730                                          DEPRECATED_REGISTER_SIZE));
1731
1732   /* If the PC was explicitly saved, then just restore it.  */
1733   if (fsr[PCOQ_TAIL_REGNUM])
1734     {
1735       npc = read_memory_integer (fsr[PCOQ_TAIL_REGNUM],
1736                                  DEPRECATED_REGISTER_SIZE);
1737       write_register (PCOQ_TAIL_REGNUM, npc);
1738     }
1739   /* Else use the value in %rp to set the new PC.  */
1740   else
1741     {
1742       npc = read_register (RP_REGNUM);
1743       write_pc (npc);
1744     }
1745
1746   write_register (DEPRECATED_FP_REGNUM, read_memory_integer (fp, DEPRECATED_REGISTER_SIZE));
1747
1748   if (fsr[IPSW_REGNUM]) /* call dummy */
1749     write_register (SP_REGNUM, fp - 48);
1750   else
1751     write_register (SP_REGNUM, fp);
1752
1753   /* The PC we just restored may be inside a return trampoline.  If so
1754      we want to restart the inferior and run it through the trampoline.
1755
1756      Do this by setting a momentary breakpoint at the location the
1757      trampoline returns to. 
1758
1759      Don't skip through the trampoline if we're popping a dummy frame.  */
1760   target_pc = SKIP_TRAMPOLINE_CODE (npc & ~0x3) & ~0x3;
1761   if (target_pc && !fsr[IPSW_REGNUM])
1762     {
1763       struct symtab_and_line sal;
1764       struct breakpoint *breakpoint;
1765       struct cleanup *old_chain;
1766
1767       /* Set up our breakpoint.   Set it to be silent as the MI code
1768          for "return_command" will print the frame we returned to.  */
1769       sal = find_pc_line (target_pc, 0);
1770       sal.pc = target_pc;
1771       breakpoint = set_momentary_breakpoint (sal, null_frame_id, bp_finish);
1772       breakpoint->silent = 1;
1773
1774       /* So we can clean things up.  */
1775       old_chain = make_cleanup_delete_breakpoint (breakpoint);
1776
1777       /* Start up the inferior.  */
1778       clear_proceed_status ();
1779       proceed_to_finish = 1;
1780       proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1781
1782       /* Perform our cleanups.  */
1783       do_cleanups (old_chain);
1784     }
1785   flush_cached_frames ();
1786 }
1787
1788 /* After returning to a dummy on the stack, restore the instruction
1789    queue space registers. */
1790
1791 static int
1792 restore_pc_queue (CORE_ADDR *fsr)
1793 {
1794   CORE_ADDR pc = read_pc ();
1795   CORE_ADDR new_pc = read_memory_integer (fsr[PCOQ_HEAD_REGNUM],
1796                                           TARGET_PTR_BIT / 8);
1797   struct target_waitstatus w;
1798   int insn_count;
1799
1800   /* Advance past break instruction in the call dummy. */
1801   write_register (PCOQ_HEAD_REGNUM, pc + 4);
1802   write_register (PCOQ_TAIL_REGNUM, pc + 8);
1803
1804   /* HPUX doesn't let us set the space registers or the space
1805      registers of the PC queue through ptrace. Boo, hiss.
1806      Conveniently, the call dummy has this sequence of instructions
1807      after the break:
1808      mtsp r21, sr0
1809      ble,n 0(sr0, r22)
1810
1811      So, load up the registers and single step until we are in the
1812      right place. */
1813
1814   write_register (21, read_memory_integer (fsr[PCSQ_HEAD_REGNUM],
1815                                            DEPRECATED_REGISTER_SIZE));
1816   write_register (22, new_pc);
1817
1818   for (insn_count = 0; insn_count < 3; insn_count++)
1819     {
1820       /* FIXME: What if the inferior gets a signal right now?  Want to
1821          merge this into wait_for_inferior (as a special kind of
1822          watchpoint?  By setting a breakpoint at the end?  Is there
1823          any other choice?  Is there *any* way to do this stuff with
1824          ptrace() or some equivalent?).  */
1825       resume (1, 0);
1826       target_wait (inferior_ptid, &w);
1827
1828       if (w.kind == TARGET_WAITKIND_SIGNALLED)
1829         {
1830           stop_signal = w.value.sig;
1831           terminal_ours_for_output ();
1832           printf_unfiltered ("\nProgram terminated with signal %s, %s.\n",
1833                              target_signal_to_name (stop_signal),
1834                              target_signal_to_string (stop_signal));
1835           gdb_flush (gdb_stdout);
1836           return 0;
1837         }
1838     }
1839   target_terminal_ours ();
1840   target_fetch_registers (-1);
1841   return 1;
1842 }
1843
1844
1845 #ifdef PA20W_CALLING_CONVENTIONS
1846
1847 /* This function pushes a stack frame with arguments as part of the
1848    inferior function calling mechanism.
1849
1850    This is the version for the PA64, in which later arguments appear
1851    at higher addresses.  (The stack always grows towards higher
1852    addresses.)
1853
1854    We simply allocate the appropriate amount of stack space and put
1855    arguments into their proper slots.  The call dummy code will copy
1856    arguments into registers as needed by the ABI.
1857
1858    This ABI also requires that the caller provide an argument pointer
1859    to the callee, so we do that too.  */
1860    
1861 CORE_ADDR
1862 hppa_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1863                      int struct_return, CORE_ADDR struct_addr)
1864 {
1865   /* array of arguments' offsets */
1866   int *offset = (int *) alloca (nargs * sizeof (int));
1867
1868   /* array of arguments' lengths: real lengths in bytes, not aligned to
1869      word size */
1870   int *lengths = (int *) alloca (nargs * sizeof (int));
1871
1872   /* The value of SP as it was passed into this function after
1873      aligning.  */
1874   CORE_ADDR orig_sp = DEPRECATED_STACK_ALIGN (sp);
1875
1876   /* The number of stack bytes occupied by the current argument.  */
1877   int bytes_reserved;
1878
1879   /* The total number of bytes reserved for the arguments.  */
1880   int cum_bytes_reserved = 0;
1881
1882   /* Similarly, but aligned.  */
1883   int cum_bytes_aligned = 0;
1884   int i;
1885
1886   /* Iterate over each argument provided by the user.  */
1887   for (i = 0; i < nargs; i++)
1888     {
1889       struct type *arg_type = VALUE_TYPE (args[i]);
1890
1891       /* Integral scalar values smaller than a register are padded on
1892          the left.  We do this by promoting them to full-width,
1893          although the ABI says to pad them with garbage.  */
1894       if (is_integral_type (arg_type)
1895           && TYPE_LENGTH (arg_type) < DEPRECATED_REGISTER_SIZE)
1896         {
1897           args[i] = value_cast ((TYPE_UNSIGNED (arg_type)
1898                                  ? builtin_type_unsigned_long
1899                                  : builtin_type_long),
1900                                 args[i]);
1901           arg_type = VALUE_TYPE (args[i]);
1902         }
1903
1904       lengths[i] = TYPE_LENGTH (arg_type);
1905
1906       /* Align the size of the argument to the word size for this
1907          target.  */
1908       bytes_reserved = (lengths[i] + DEPRECATED_REGISTER_SIZE - 1) & -DEPRECATED_REGISTER_SIZE;
1909
1910       offset[i] = cum_bytes_reserved;
1911
1912       /* Aggregates larger than eight bytes (the only types larger
1913          than eight bytes we have) are aligned on a 16-byte boundary,
1914          possibly padded on the right with garbage.  This may leave an
1915          empty word on the stack, and thus an unused register, as per
1916          the ABI.  */
1917       if (bytes_reserved > 8)
1918         {
1919           /* Round up the offset to a multiple of two slots.  */
1920           int new_offset = ((offset[i] + 2*DEPRECATED_REGISTER_SIZE-1)
1921                             & -(2*DEPRECATED_REGISTER_SIZE));
1922
1923           /* Note the space we've wasted, if any.  */
1924           bytes_reserved += new_offset - offset[i];
1925           offset[i] = new_offset;
1926         }
1927
1928       cum_bytes_reserved += bytes_reserved;
1929     }
1930
1931   /* CUM_BYTES_RESERVED already accounts for all the arguments
1932      passed by the user.  However, the ABIs mandate minimum stack space
1933      allocations for outgoing arguments.
1934
1935      The ABIs also mandate minimum stack alignments which we must
1936      preserve.  */
1937   cum_bytes_aligned = DEPRECATED_STACK_ALIGN (cum_bytes_reserved);
1938   sp += max (cum_bytes_aligned, REG_PARM_STACK_SPACE);
1939
1940   /* Now write each of the args at the proper offset down the stack.  */
1941   for (i = 0; i < nargs; i++)
1942     write_memory (orig_sp + offset[i], VALUE_CONTENTS (args[i]), lengths[i]);
1943
1944   /* If a structure has to be returned, set up register 28 to hold its
1945      address */
1946   if (struct_return)
1947     write_register (28, struct_addr);
1948
1949   /* For the PA64 we must pass a pointer to the outgoing argument list.
1950      The ABI mandates that the pointer should point to the first byte of
1951      storage beyond the register flushback area.
1952
1953      However, the call dummy expects the outgoing argument pointer to
1954      be passed in register %r4.  */
1955   write_register (4, orig_sp + REG_PARM_STACK_SPACE);
1956
1957   /* ?!? This needs further work.  We need to set up the global data
1958      pointer for this procedure.  This assumes the same global pointer
1959      for every procedure.   The call dummy expects the dp value to
1960      be passed in register %r6.  */
1961   write_register (6, read_register (27));
1962   
1963   /* The stack will have 64 bytes of additional space for a frame marker.  */
1964   return sp + 64;
1965 }
1966
1967 #else
1968
1969 /* This function pushes a stack frame with arguments as part of the
1970    inferior function calling mechanism.
1971
1972    This is the version of the function for the 32-bit PA machines, in
1973    which later arguments appear at lower addresses.  (The stack always
1974    grows towards higher addresses.)
1975
1976    We simply allocate the appropriate amount of stack space and put
1977    arguments into their proper slots.  The call dummy code will copy
1978    arguments into registers as needed by the ABI. */
1979    
1980 CORE_ADDR
1981 hppa_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1982                      int struct_return, CORE_ADDR struct_addr)
1983 {
1984   /* array of arguments' offsets */
1985   int *offset = (int *) alloca (nargs * sizeof (int));
1986
1987   /* array of arguments' lengths: real lengths in bytes, not aligned to
1988      word size */
1989   int *lengths = (int *) alloca (nargs * sizeof (int));
1990
1991   /* The number of stack bytes occupied by the current argument.  */
1992   int bytes_reserved;
1993
1994   /* The total number of bytes reserved for the arguments.  */
1995   int cum_bytes_reserved = 0;
1996
1997   /* Similarly, but aligned.  */
1998   int cum_bytes_aligned = 0;
1999   int i;
2000
2001   /* Iterate over each argument provided by the user.  */
2002   for (i = 0; i < nargs; i++)
2003     {
2004       lengths[i] = TYPE_LENGTH (VALUE_TYPE (args[i]));
2005
2006       /* Align the size of the argument to the word size for this
2007          target.  */
2008       bytes_reserved = (lengths[i] + DEPRECATED_REGISTER_SIZE - 1) & -DEPRECATED_REGISTER_SIZE;
2009
2010       offset[i] = (cum_bytes_reserved
2011                    + (lengths[i] > 4 ? bytes_reserved : lengths[i]));
2012
2013       /* If the argument is a double word argument, then it needs to be
2014          double word aligned.  */
2015       if ((bytes_reserved == 2 * DEPRECATED_REGISTER_SIZE)
2016           && (offset[i] % 2 * DEPRECATED_REGISTER_SIZE))
2017         {
2018           int new_offset = 0;
2019           /* BYTES_RESERVED is already aligned to the word, so we put
2020              the argument at one word more down the stack.
2021
2022              This will leave one empty word on the stack, and one unused
2023              register as mandated by the ABI.  */
2024           new_offset = ((offset[i] + 2 * DEPRECATED_REGISTER_SIZE - 1)
2025                         & -(2 * DEPRECATED_REGISTER_SIZE));
2026
2027           if ((new_offset - offset[i]) >= 2 * DEPRECATED_REGISTER_SIZE)
2028             {
2029               bytes_reserved += DEPRECATED_REGISTER_SIZE;
2030               offset[i] += DEPRECATED_REGISTER_SIZE;
2031             }
2032         }
2033
2034       cum_bytes_reserved += bytes_reserved;
2035
2036     }
2037
2038   /* CUM_BYTES_RESERVED already accounts for all the arguments passed
2039      by the user.  However, the ABI mandates minimum stack space
2040      allocations for outgoing arguments.
2041
2042      The ABI also mandates minimum stack alignments which we must
2043      preserve.  */
2044   cum_bytes_aligned = DEPRECATED_STACK_ALIGN (cum_bytes_reserved);
2045   sp += max (cum_bytes_aligned, REG_PARM_STACK_SPACE);
2046
2047   /* Now write each of the args at the proper offset down the stack.
2048      ?!? We need to promote values to a full register instead of skipping
2049      words in the stack.  */
2050   for (i = 0; i < nargs; i++)
2051     write_memory (sp - offset[i], VALUE_CONTENTS (args[i]), lengths[i]);
2052
2053   /* If a structure has to be returned, set up register 28 to hold its
2054      address */
2055   if (struct_return)
2056     write_register (28, struct_addr);
2057
2058   /* The stack will have 32 bytes of additional space for a frame marker.  */
2059   return sp + 32;
2060 }
2061
2062 #endif
2063
2064 /* elz: Used to lookup a symbol in the shared libraries.
2065    This function calls shl_findsym, indirectly through a
2066    call to __d_shl_get. __d_shl_get is in end.c, which is always
2067    linked in by the hp compilers/linkers. 
2068    The call to shl_findsym cannot be made directly because it needs
2069    to be active in target address space. 
2070    inputs: - minimal symbol pointer for the function we want to look up
2071    - address in target space of the descriptor for the library
2072    where we want to look the symbol up.
2073    This address is retrieved using the 
2074    som_solib_get_solib_by_pc function (somsolib.c). 
2075    output: - real address in the library of the function.          
2076    note: the handle can be null, in which case shl_findsym will look for
2077    the symbol in all the loaded shared libraries.
2078    files to look at if you need reference on this stuff:
2079    dld.c, dld_shl_findsym.c
2080    end.c
2081    man entry for shl_findsym */
2082
2083 CORE_ADDR
2084 find_stub_with_shl_get (struct minimal_symbol *function, CORE_ADDR handle)
2085 {
2086   struct symbol *get_sym, *symbol2;
2087   struct minimal_symbol *buff_minsym, *msymbol;
2088   struct type *ftype;
2089   struct value **args;
2090   struct value *funcval;
2091   struct value *val;
2092
2093   int x, namelen, err_value, tmp = -1;
2094   CORE_ADDR endo_buff_addr, value_return_addr, errno_return_addr;
2095   CORE_ADDR stub_addr;
2096
2097
2098   args = alloca (sizeof (struct value *) * 8);          /* 6 for the arguments and one null one??? */
2099   funcval = find_function_in_inferior ("__d_shl_get");
2100   get_sym = lookup_symbol ("__d_shl_get", NULL, VAR_DOMAIN, NULL, NULL);
2101   buff_minsym = lookup_minimal_symbol ("__buffer", NULL, NULL);
2102   msymbol = lookup_minimal_symbol ("__shldp", NULL, NULL);
2103   symbol2 = lookup_symbol ("__shldp", NULL, VAR_DOMAIN, NULL, NULL);
2104   endo_buff_addr = SYMBOL_VALUE_ADDRESS (buff_minsym);
2105   namelen = strlen (DEPRECATED_SYMBOL_NAME (function));
2106   value_return_addr = endo_buff_addr + namelen;
2107   ftype = check_typedef (SYMBOL_TYPE (get_sym));
2108
2109   /* do alignment */
2110   if ((x = value_return_addr % 64) != 0)
2111     value_return_addr = value_return_addr + 64 - x;
2112
2113   errno_return_addr = value_return_addr + 64;
2114
2115
2116   /* set up stuff needed by __d_shl_get in buffer in end.o */
2117
2118   target_write_memory (endo_buff_addr, DEPRECATED_SYMBOL_NAME (function), namelen);
2119
2120   target_write_memory (value_return_addr, (char *) &tmp, 4);
2121
2122   target_write_memory (errno_return_addr, (char *) &tmp, 4);
2123
2124   target_write_memory (SYMBOL_VALUE_ADDRESS (msymbol),
2125                        (char *) &handle, 4);
2126
2127   /* now prepare the arguments for the call */
2128
2129   args[0] = value_from_longest (TYPE_FIELD_TYPE (ftype, 0), 12);
2130   args[1] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 1), SYMBOL_VALUE_ADDRESS (msymbol));
2131   args[2] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 2), endo_buff_addr);
2132   args[3] = value_from_longest (TYPE_FIELD_TYPE (ftype, 3), TYPE_PROCEDURE);
2133   args[4] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 4), value_return_addr);
2134   args[5] = value_from_pointer (TYPE_FIELD_TYPE (ftype, 5), errno_return_addr);
2135
2136   /* now call the function */
2137
2138   val = call_function_by_hand (funcval, 6, args);
2139
2140   /* now get the results */
2141
2142   target_read_memory (errno_return_addr, (char *) &err_value, sizeof (err_value));
2143
2144   target_read_memory (value_return_addr, (char *) &stub_addr, sizeof (stub_addr));
2145   if (stub_addr <= 0)
2146     error ("call to __d_shl_get failed, error code is %d", err_value);
2147
2148   return (stub_addr);
2149 }
2150
2151 /* Cover routine for find_stub_with_shl_get to pass to catch_errors */
2152 static int
2153 cover_find_stub_with_shl_get (void *args_untyped)
2154 {
2155   args_for_find_stub *args = args_untyped;
2156   args->return_val = find_stub_with_shl_get (args->msym, args->solib_handle);
2157   return 0;
2158 }
2159
2160 /* Insert the specified number of args and function address
2161    into a call sequence of the above form stored at DUMMYNAME.
2162
2163    On the hppa we need to call the stack dummy through $$dyncall.
2164    Therefore our version of DEPRECATED_FIX_CALL_DUMMY takes an extra
2165    argument, real_pc, which is the location where gdb should start up
2166    the inferior to do the function call.
2167
2168    This has to work across several versions of hpux, bsd, osf1.  It has to
2169    work regardless of what compiler was used to build the inferior program.
2170    It should work regardless of whether or not end.o is available.  It has
2171    to work even if gdb can not call into the dynamic loader in the inferior
2172    to query it for symbol names and addresses.
2173
2174    Yes, all those cases should work.  Luckily code exists to handle most
2175    of them.  The complexity is in selecting exactly what scheme should
2176    be used to perform the inferior call.
2177
2178    At the current time this routine is known not to handle cases where
2179    the program was linked with HP's compiler without including end.o.
2180
2181    Please contact Jeff Law (law@cygnus.com) before changing this code.  */
2182
2183 CORE_ADDR
2184 hppa_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
2185                      struct value **args, struct type *type, int gcc_p)
2186 {
2187   CORE_ADDR dyncall_addr;
2188   struct minimal_symbol *msymbol;
2189   struct minimal_symbol *trampoline;
2190   int flags = read_register (FLAGS_REGNUM);
2191   struct unwind_table_entry *u = NULL;
2192   CORE_ADDR new_stub = 0;
2193   CORE_ADDR solib_handle = 0;
2194
2195   /* Nonzero if we will use GCC's PLT call routine.  This routine must be
2196      passed an import stub, not a PLABEL.  It is also necessary to set %r19
2197      (the PIC register) before performing the call.
2198
2199      If zero, then we are using __d_plt_call (HP's PLT call routine) or we
2200      are calling the target directly.  When using __d_plt_call we want to
2201      use a PLABEL instead of an import stub.  */
2202   int using_gcc_plt_call = 1;
2203
2204 #ifdef GDB_TARGET_IS_HPPA_20W
2205   /* We currently use completely different code for the PA2.0W inferior
2206      function call sequences.  This needs to be cleaned up.  */
2207   {
2208     CORE_ADDR pcsqh, pcsqt, pcoqh, pcoqt, sr5;
2209     struct target_waitstatus w;
2210     int inst1, inst2;
2211     char buf[4];
2212     int status;
2213     struct objfile *objfile;
2214
2215     /* We can not modify the PC space queues directly, so we start
2216        up the inferior and execute a couple instructions to set the
2217        space queues so that they point to the call dummy in the stack.  */
2218     pcsqh = read_register (PCSQ_HEAD_REGNUM);
2219     sr5 = read_register (SR5_REGNUM);
2220     if (1)
2221       {
2222         pcoqh = read_register (PCOQ_HEAD_REGNUM);
2223         pcoqt = read_register (PCOQ_TAIL_REGNUM);
2224         if (target_read_memory (pcoqh, buf, 4) != 0)
2225           error ("Couldn't modify space queue\n");
2226         inst1 = extract_unsigned_integer (buf, 4);
2227
2228         if (target_read_memory (pcoqt, buf, 4) != 0)
2229           error ("Couldn't modify space queue\n");
2230         inst2 = extract_unsigned_integer (buf, 4);
2231
2232         /* BVE (r1) */
2233         *((int *) buf) = 0xe820d000;
2234         if (target_write_memory (pcoqh, buf, 4) != 0)
2235           error ("Couldn't modify space queue\n");
2236
2237         /* NOP */
2238         *((int *) buf) = 0x08000240;
2239         if (target_write_memory (pcoqt, buf, 4) != 0)
2240           {
2241             *((int *) buf) = inst1;
2242             target_write_memory (pcoqh, buf, 4);
2243             error ("Couldn't modify space queue\n");
2244           }
2245
2246         write_register (1, pc);
2247
2248         /* Single step twice, the BVE instruction will set the space queue
2249            such that it points to the PC value written immediately above
2250            (ie the call dummy).  */
2251         resume (1, 0);
2252         target_wait (inferior_ptid, &w);
2253         resume (1, 0);
2254         target_wait (inferior_ptid, &w);
2255
2256         /* Restore the two instructions at the old PC locations.  */
2257         *((int *) buf) = inst1;
2258         target_write_memory (pcoqh, buf, 4);
2259         *((int *) buf) = inst2;
2260         target_write_memory (pcoqt, buf, 4);
2261       }
2262
2263     /* The call dummy wants the ultimate destination address initially
2264        in register %r5.  */
2265     write_register (5, fun);
2266
2267     /* We need to see if this objfile has a different DP value than our
2268        own (it could be a shared library for example).  */
2269     ALL_OBJFILES (objfile)
2270       {
2271         struct obj_section *s;
2272         obj_private_data_t *obj_private;
2273
2274         /* See if FUN is in any section within this shared library.  */
2275         for (s = objfile->sections; s < objfile->sections_end; s++)
2276           if (s->addr <= fun && fun < s->endaddr)
2277             break;
2278
2279         if (s >= objfile->sections_end)
2280           continue;
2281
2282         obj_private = (obj_private_data_t *) objfile->obj_private;
2283         
2284         /* The DP value may be different for each objfile.  But within an
2285            objfile each function uses the same dp value.  Thus we do not need
2286            to grope around the opd section looking for dp values.
2287
2288            ?!? This is not strictly correct since we may be in a shared library
2289            and want to call back into the main program.  To make that case
2290            work correctly we need to set obj_private->dp for the main program's
2291            objfile, then remove this conditional.  */
2292         if (obj_private->dp)
2293           write_register (27, obj_private->dp);
2294         break;
2295       }
2296     return pc;
2297   }
2298 #endif
2299
2300 #ifndef GDB_TARGET_IS_HPPA_20W
2301   /* Prefer __gcc_plt_call over the HP supplied routine because
2302      __gcc_plt_call works for any number of arguments.  */
2303   trampoline = NULL;
2304   if (lookup_minimal_symbol ("__gcc_plt_call", NULL, NULL) == NULL)
2305     using_gcc_plt_call = 0;
2306
2307   msymbol = lookup_minimal_symbol ("$$dyncall", NULL, NULL);
2308   if (msymbol == NULL)
2309     error ("Can't find an address for $$dyncall trampoline");
2310
2311   dyncall_addr = SYMBOL_VALUE_ADDRESS (msymbol);
2312
2313   /* FUN could be a procedure label, in which case we have to get
2314      its real address and the value of its GOT/DP if we plan to
2315      call the routine via gcc_plt_call.  */
2316   if ((fun & 0x2) && using_gcc_plt_call)
2317     {
2318       /* Get the GOT/DP value for the target function.  It's
2319          at *(fun+4).  Note the call dummy is *NOT* allowed to
2320          trash %r19 before calling the target function.  */
2321       write_register (19, read_memory_integer ((fun & ~0x3) + 4,
2322                                                DEPRECATED_REGISTER_SIZE));
2323
2324       /* Now get the real address for the function we are calling, it's
2325          at *fun.  */
2326       fun = (CORE_ADDR) read_memory_integer (fun & ~0x3,
2327                                              TARGET_PTR_BIT / 8);
2328     }
2329   else
2330     {
2331
2332 #ifndef GDB_TARGET_IS_PA_ELF
2333       /* FUN could be an export stub, the real address of a function, or
2334          a PLABEL.  When using gcc's PLT call routine we must call an import
2335          stub rather than the export stub or real function for lazy binding
2336          to work correctly
2337
2338          If we are using the gcc PLT call routine, then we need to
2339          get the import stub for the target function.  */
2340       if (using_gcc_plt_call && som_solib_get_got_by_pc (fun))
2341         {
2342           struct objfile *objfile;
2343           struct minimal_symbol *funsymbol, *stub_symbol;
2344           CORE_ADDR newfun = 0;
2345
2346           funsymbol = lookup_minimal_symbol_by_pc (fun);
2347           if (!funsymbol)
2348             error ("Unable to find minimal symbol for target function.\n");
2349
2350           /* Search all the object files for an import symbol with the
2351              right name. */
2352           ALL_OBJFILES (objfile)
2353           {
2354             stub_symbol
2355               = lookup_minimal_symbol_solib_trampoline
2356               (DEPRECATED_SYMBOL_NAME (funsymbol), objfile);
2357
2358             if (!stub_symbol)
2359               stub_symbol = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (funsymbol),
2360                                                    NULL, objfile);
2361
2362             /* Found a symbol with the right name.  */
2363             if (stub_symbol)
2364               {
2365                 struct unwind_table_entry *u;
2366                 /* It must be a shared library trampoline.  */
2367                 if (MSYMBOL_TYPE (stub_symbol) != mst_solib_trampoline)
2368                   continue;
2369
2370                 /* It must also be an import stub.  */
2371                 u = find_unwind_entry (SYMBOL_VALUE (stub_symbol));
2372                 if (u == NULL
2373                     || (u->stub_unwind.stub_type != IMPORT
2374 #ifdef GDB_NATIVE_HPUX_11
2375                         /* Sigh.  The hpux 10.20 dynamic linker will blow
2376                            chunks if we perform a call to an unbound function
2377                            via the IMPORT_SHLIB stub.  The hpux 11.00 dynamic
2378                            linker will blow chunks if we do not call the
2379                            unbound function via the IMPORT_SHLIB stub.
2380
2381                            We currently have no way to select bevahior on just
2382                            the target.  However, we only support HPUX/SOM in
2383                            native mode.  So we conditinalize on a native
2384                            #ifdef.  Ugly.  Ugly.  Ugly  */
2385                         && u->stub_unwind.stub_type != IMPORT_SHLIB
2386 #endif
2387                         ))
2388                   continue;
2389
2390                 /* OK.  Looks like the correct import stub.  */
2391                 newfun = SYMBOL_VALUE (stub_symbol);
2392                 fun = newfun;
2393
2394                 /* If we found an IMPORT stub, then we want to stop
2395                    searching now.  If we found an IMPORT_SHLIB, we want
2396                    to continue the search in the hopes that we will find
2397                    an IMPORT stub.  */
2398                 if (u->stub_unwind.stub_type == IMPORT)
2399                   break;
2400               }
2401           }
2402
2403           /* Ouch.  We did not find an import stub.  Make an attempt to
2404              do the right thing instead of just croaking.  Most of the
2405              time this will actually work.  */
2406           if (newfun == 0)
2407             write_register (19, som_solib_get_got_by_pc (fun));
2408
2409           u = find_unwind_entry (fun);
2410           if (u
2411               && (u->stub_unwind.stub_type == IMPORT
2412                   || u->stub_unwind.stub_type == IMPORT_SHLIB))
2413             trampoline = lookup_minimal_symbol ("__gcc_plt_call", NULL, NULL);
2414
2415           /* If we found the import stub in the shared library, then we have
2416              to set %r19 before we call the stub.  */
2417           if (u && u->stub_unwind.stub_type == IMPORT_SHLIB)
2418             write_register (19, som_solib_get_got_by_pc (fun));
2419         }
2420 #endif
2421     }
2422
2423   /* If we are calling into another load module then have sr4export call the
2424      magic __d_plt_call routine which is linked in from end.o.
2425
2426      You can't use _sr4export to make the call as the value in sp-24 will get
2427      fried and you end up returning to the wrong location.  You can't call the
2428      target as the code to bind the PLT entry to a function can't return to a
2429      stack address.
2430
2431      Also, query the dynamic linker in the inferior to provide a suitable
2432      PLABEL for the target function.  */
2433   if (!using_gcc_plt_call)
2434     {
2435       CORE_ADDR new_fun;
2436
2437       /* Get a handle for the shared library containing FUN.  Given the
2438          handle we can query the shared library for a PLABEL.  */
2439       solib_handle = som_solib_get_solib_by_pc (fun);
2440
2441       if (solib_handle)
2442         {
2443           struct minimal_symbol *fmsymbol = lookup_minimal_symbol_by_pc (fun);
2444
2445           trampoline = lookup_minimal_symbol ("__d_plt_call", NULL, NULL);
2446
2447           if (trampoline == NULL)
2448             {
2449               error ("Can't find an address for __d_plt_call or __gcc_plt_call trampoline\nSuggest linking executable with -g or compiling with gcc.");
2450             }
2451
2452           /* This is where sr4export will jump to.  */
2453           new_fun = SYMBOL_VALUE_ADDRESS (trampoline);
2454
2455           /* If the function is in a shared library, then call __d_shl_get to
2456              get a PLABEL for the target function.  */
2457           new_stub = find_stub_with_shl_get (fmsymbol, solib_handle);
2458
2459           if (new_stub == 0)
2460             error ("Can't find an import stub for %s", DEPRECATED_SYMBOL_NAME (fmsymbol));
2461
2462           /* We have to store the address of the stub in __shlib_funcptr.  */
2463           msymbol = lookup_minimal_symbol ("__shlib_funcptr", NULL,
2464                                            (struct objfile *) NULL);
2465
2466           if (msymbol == NULL)
2467             error ("Can't find an address for __shlib_funcptr");
2468           target_write_memory (SYMBOL_VALUE_ADDRESS (msymbol),
2469                                (char *) &new_stub, 4);
2470
2471           /* We want sr4export to call __d_plt_call, so we claim it is
2472              the final target.  Clear trampoline.  */
2473           fun = new_fun;
2474           trampoline = NULL;
2475         }
2476     }
2477
2478   /* Store upper 21 bits of function address into ldil.  fun will either be
2479      the final target (most cases) or __d_plt_call when calling into a shared
2480      library and __gcc_plt_call is not available.  */
2481   store_unsigned_integer
2482     (&dummy[FUNC_LDIL_OFFSET],
2483      INSTRUCTION_SIZE,
2484      deposit_21 (fun >> 11,
2485                  extract_unsigned_integer (&dummy[FUNC_LDIL_OFFSET],
2486                                            INSTRUCTION_SIZE)));
2487
2488   /* Store lower 11 bits of function address into ldo */
2489   store_unsigned_integer
2490     (&dummy[FUNC_LDO_OFFSET],
2491      INSTRUCTION_SIZE,
2492      deposit_14 (fun & MASK_11,
2493                  extract_unsigned_integer (&dummy[FUNC_LDO_OFFSET],
2494                                            INSTRUCTION_SIZE)));
2495 #ifdef SR4EXPORT_LDIL_OFFSET
2496
2497   {
2498     CORE_ADDR trampoline_addr;
2499
2500     /* We may still need sr4export's address too.  */
2501
2502     if (trampoline == NULL)
2503       {
2504         msymbol = lookup_minimal_symbol ("_sr4export", NULL, NULL);
2505         if (msymbol == NULL)
2506           error ("Can't find an address for _sr4export trampoline");
2507
2508         trampoline_addr = SYMBOL_VALUE_ADDRESS (msymbol);
2509       }
2510     else
2511       trampoline_addr = SYMBOL_VALUE_ADDRESS (trampoline);
2512
2513
2514     /* Store upper 21 bits of trampoline's address into ldil */
2515     store_unsigned_integer
2516       (&dummy[SR4EXPORT_LDIL_OFFSET],
2517        INSTRUCTION_SIZE,
2518        deposit_21 (trampoline_addr >> 11,
2519                    extract_unsigned_integer (&dummy[SR4EXPORT_LDIL_OFFSET],
2520                                              INSTRUCTION_SIZE)));
2521
2522     /* Store lower 11 bits of trampoline's address into ldo */
2523     store_unsigned_integer
2524       (&dummy[SR4EXPORT_LDO_OFFSET],
2525        INSTRUCTION_SIZE,
2526        deposit_14 (trampoline_addr & MASK_11,
2527                    extract_unsigned_integer (&dummy[SR4EXPORT_LDO_OFFSET],
2528                                              INSTRUCTION_SIZE)));
2529   }
2530 #endif
2531
2532   write_register (22, pc);
2533
2534   /* If we are in a syscall, then we should call the stack dummy
2535      directly.  $$dyncall is not needed as the kernel sets up the
2536      space id registers properly based on the value in %r31.  In
2537      fact calling $$dyncall will not work because the value in %r22
2538      will be clobbered on the syscall exit path. 
2539
2540      Similarly if the current PC is in a shared library.  Note however,
2541      this scheme won't work if the shared library isn't mapped into
2542      the same space as the stack.  */
2543   if (flags & 2)
2544     return pc;
2545 #ifndef GDB_TARGET_IS_PA_ELF
2546   else if (som_solib_get_got_by_pc (hppa_target_read_pc (inferior_ptid)))
2547     return pc;
2548 #endif
2549   else
2550     return dyncall_addr;
2551 #endif
2552 }
2553
2554 /* If the pid is in a syscall, then the FP register is not readable.
2555    We'll return zero in that case, rather than attempting to read it
2556    and cause a warning. */
2557
2558 CORE_ADDR
2559 hppa_read_fp (int pid)
2560 {
2561   int flags = read_register (FLAGS_REGNUM);
2562
2563   if (flags & 2)
2564     {
2565       return (CORE_ADDR) 0;
2566     }
2567
2568   /* This is the only site that may directly read_register () the FP
2569      register.  All others must use deprecated_read_fp (). */
2570   return read_register (DEPRECATED_FP_REGNUM);
2571 }
2572
2573 CORE_ADDR
2574 hppa_target_read_fp (void)
2575 {
2576   return hppa_read_fp (PIDGET (inferior_ptid));
2577 }
2578
2579 /* Get the PC from %r31 if currently in a syscall.  Also mask out privilege
2580    bits.  */
2581
2582 CORE_ADDR
2583 hppa_target_read_pc (ptid_t ptid)
2584 {
2585   int flags = read_register_pid (FLAGS_REGNUM, ptid);
2586
2587   /* The following test does not belong here.  It is OS-specific, and belongs
2588      in native code.  */
2589   /* Test SS_INSYSCALL */
2590   if (flags & 2)
2591     return read_register_pid (31, ptid) & ~0x3;
2592
2593   return read_register_pid (PC_REGNUM, ptid) & ~0x3;
2594 }
2595
2596 /* Write out the PC.  If currently in a syscall, then also write the new
2597    PC value into %r31.  */
2598
2599 void
2600 hppa_target_write_pc (CORE_ADDR v, ptid_t ptid)
2601 {
2602   int flags = read_register_pid (FLAGS_REGNUM, ptid);
2603
2604   /* The following test does not belong here.  It is OS-specific, and belongs
2605      in native code.  */
2606   /* If in a syscall, then set %r31.  Also make sure to get the 
2607      privilege bits set correctly.  */
2608   /* Test SS_INSYSCALL */
2609   if (flags & 2)
2610     write_register_pid (31, v | 0x3, ptid);
2611
2612   write_register_pid (PC_REGNUM, v, ptid);
2613   write_register_pid (PCOQ_TAIL_REGNUM, v + 4, ptid);
2614 }
2615
2616 /* return the alignment of a type in bytes. Structures have the maximum
2617    alignment required by their fields. */
2618
2619 static int
2620 hppa_alignof (struct type *type)
2621 {
2622   int max_align, align, i;
2623   CHECK_TYPEDEF (type);
2624   switch (TYPE_CODE (type))
2625     {
2626     case TYPE_CODE_PTR:
2627     case TYPE_CODE_INT:
2628     case TYPE_CODE_FLT:
2629       return TYPE_LENGTH (type);
2630     case TYPE_CODE_ARRAY:
2631       return hppa_alignof (TYPE_FIELD_TYPE (type, 0));
2632     case TYPE_CODE_STRUCT:
2633     case TYPE_CODE_UNION:
2634       max_align = 1;
2635       for (i = 0; i < TYPE_NFIELDS (type); i++)
2636         {
2637           /* Bit fields have no real alignment. */
2638           /* if (!TYPE_FIELD_BITPOS (type, i)) */
2639           if (!TYPE_FIELD_BITSIZE (type, i))    /* elz: this should be bitsize */
2640             {
2641               align = hppa_alignof (TYPE_FIELD_TYPE (type, i));
2642               max_align = max (max_align, align);
2643             }
2644         }
2645       return max_align;
2646     default:
2647       return 4;
2648     }
2649 }
2650
2651 /* Print the register regnum, or all registers if regnum is -1 */
2652
2653 void
2654 pa_do_registers_info (int regnum, int fpregs)
2655 {
2656   char *raw_regs = alloca (DEPRECATED_REGISTER_BYTES);
2657   int i;
2658
2659   /* Make a copy of gdb's save area (may cause actual
2660      reads from the target). */
2661   for (i = 0; i < NUM_REGS; i++)
2662     frame_register_read (deprecated_selected_frame, i,
2663                          raw_regs + DEPRECATED_REGISTER_BYTE (i));
2664
2665   if (regnum == -1)
2666     pa_print_registers (raw_regs, regnum, fpregs);
2667   else if (regnum < FP4_REGNUM)
2668     {
2669       long reg_val[2];
2670
2671       /* Why is the value not passed through "extract_signed_integer"
2672          as in "pa_print_registers" below? */
2673       pa_register_look_aside (raw_regs, regnum, &reg_val[0]);
2674
2675       if (!is_pa_2)
2676         {
2677           printf_unfiltered ("%s %lx\n", REGISTER_NAME (regnum), reg_val[1]);
2678         }
2679       else
2680         {
2681           /* Fancy % formats to prevent leading zeros. */
2682           if (reg_val[0] == 0)
2683             printf_unfiltered ("%s %lx\n", REGISTER_NAME (regnum), reg_val[1]);
2684           else
2685             printf_unfiltered ("%s %lx%8.8lx\n", REGISTER_NAME (regnum),
2686                                reg_val[0], reg_val[1]);
2687         }
2688     }
2689   else
2690     /* Note that real floating point values only start at
2691        FP4_REGNUM.  FP0 and up are just status and error
2692        registers, which have integral (bit) values. */
2693     pa_print_fp_reg (regnum);
2694 }
2695
2696 /********** new function ********************/
2697 void
2698 pa_do_strcat_registers_info (int regnum, int fpregs, struct ui_file *stream,
2699                              enum precision_type precision)
2700 {
2701   char *raw_regs = alloca (DEPRECATED_REGISTER_BYTES);
2702   int i;
2703
2704   /* Make a copy of gdb's save area (may cause actual
2705      reads from the target). */
2706   for (i = 0; i < NUM_REGS; i++)
2707     frame_register_read (deprecated_selected_frame, i,
2708                          raw_regs + DEPRECATED_REGISTER_BYTE (i));
2709
2710   if (regnum == -1)
2711     pa_strcat_registers (raw_regs, regnum, fpregs, stream);
2712
2713   else if (regnum < FP4_REGNUM)
2714     {
2715       long reg_val[2];
2716
2717       /* Why is the value not passed through "extract_signed_integer"
2718          as in "pa_print_registers" below? */
2719       pa_register_look_aside (raw_regs, regnum, &reg_val[0]);
2720
2721       if (!is_pa_2)
2722         {
2723           fprintf_unfiltered (stream, "%s %lx", REGISTER_NAME (regnum), reg_val[1]);
2724         }
2725       else
2726         {
2727           /* Fancy % formats to prevent leading zeros. */
2728           if (reg_val[0] == 0)
2729             fprintf_unfiltered (stream, "%s %lx", REGISTER_NAME (regnum),
2730                                 reg_val[1]);
2731           else
2732             fprintf_unfiltered (stream, "%s %lx%8.8lx", REGISTER_NAME (regnum),
2733                                 reg_val[0], reg_val[1]);
2734         }
2735     }
2736   else
2737     /* Note that real floating point values only start at
2738        FP4_REGNUM.  FP0 and up are just status and error
2739        registers, which have integral (bit) values. */
2740     pa_strcat_fp_reg (regnum, stream, precision);
2741 }
2742
2743 /* If this is a PA2.0 machine, fetch the real 64-bit register
2744    value.  Otherwise use the info from gdb's saved register area.
2745
2746    Note that reg_val is really expected to be an array of longs,
2747    with two elements. */
2748 static void
2749 pa_register_look_aside (char *raw_regs, int regnum, long *raw_val)
2750 {
2751   static int know_which = 0;    /* False */
2752
2753   int regaddr;
2754   unsigned int offset;
2755   int i;
2756   int start;
2757
2758
2759   char buf[MAX_REGISTER_SIZE];
2760   long long reg_val;
2761
2762   if (!know_which)
2763     {
2764       if (CPU_PA_RISC2_0 == sysconf (_SC_CPU_VERSION))
2765         {
2766           is_pa_2 = (1 == 1);
2767         }
2768
2769       know_which = 1;           /* True */
2770     }
2771
2772   raw_val[0] = 0;
2773   raw_val[1] = 0;
2774
2775   if (!is_pa_2)
2776     {
2777       raw_val[1] = *(long *) (raw_regs + DEPRECATED_REGISTER_BYTE (regnum));
2778       return;
2779     }
2780
2781   /* Code below copied from hppah-nat.c, with fixes for wide
2782      registers, using different area of save_state, etc. */
2783   if (regnum == FLAGS_REGNUM || regnum >= FP0_REGNUM ||
2784       !HAVE_STRUCT_SAVE_STATE_T || !HAVE_STRUCT_MEMBER_SS_WIDE)
2785     {
2786       /* Use narrow regs area of save_state and default macro. */
2787       offset = U_REGS_OFFSET;
2788       regaddr = register_addr (regnum, offset);
2789       start = 1;
2790     }
2791   else
2792     {
2793       /* Use wide regs area, and calculate registers as 8 bytes wide.
2794
2795          We'd like to do this, but current version of "C" doesn't
2796          permit "offsetof":
2797
2798          offset  = offsetof(save_state_t, ss_wide);
2799
2800          Note that to avoid "C" doing typed pointer arithmetic, we
2801          have to cast away the type in our offset calculation:
2802          otherwise we get an offset of 1! */
2803
2804       /* NB: save_state_t is not available before HPUX 9.
2805          The ss_wide field is not available previous to HPUX 10.20,
2806          so to avoid compile-time warnings, we only compile this for
2807          PA 2.0 processors.  This control path should only be followed
2808          if we're debugging a PA 2.0 processor, so this should not cause
2809          problems. */
2810
2811       /* #if the following code out so that this file can still be
2812          compiled on older HPUX boxes (< 10.20) which don't have
2813          this structure/structure member.  */
2814 #if HAVE_STRUCT_SAVE_STATE_T == 1 && HAVE_STRUCT_MEMBER_SS_WIDE == 1
2815       save_state_t temp;
2816
2817       offset = ((int) &temp.ss_wide) - ((int) &temp);
2818       regaddr = offset + regnum * 8;
2819       start = 0;
2820 #endif
2821     }
2822
2823   for (i = start; i < 2; i++)
2824     {
2825       errno = 0;
2826       raw_val[i] = call_ptrace (PT_RUREGS, PIDGET (inferior_ptid),
2827                                 (PTRACE_ARG3_TYPE) regaddr, 0);
2828       if (errno != 0)
2829         {
2830           /* Warning, not error, in case we are attached; sometimes the
2831              kernel doesn't let us at the registers.  */
2832           char *err = safe_strerror (errno);
2833           char *msg = alloca (strlen (err) + 128);
2834           sprintf (msg, "reading register %s: %s", REGISTER_NAME (regnum), err);
2835           warning (msg);
2836           goto error_exit;
2837         }
2838
2839       regaddr += sizeof (long);
2840     }
2841
2842   if (regnum == PCOQ_HEAD_REGNUM || regnum == PCOQ_TAIL_REGNUM)
2843     raw_val[1] &= ~0x3;         /* I think we're masking out space bits */
2844
2845 error_exit:
2846   ;
2847 }
2848
2849 /* "Info all-reg" command */
2850
2851 static void
2852 pa_print_registers (char *raw_regs, int regnum, int fpregs)
2853 {
2854   int i, j;
2855   /* Alas, we are compiled so that "long long" is 32 bits */
2856   long raw_val[2];
2857   long long_val;
2858   int rows = 48, columns = 2;
2859
2860   for (i = 0; i < rows; i++)
2861     {
2862       for (j = 0; j < columns; j++)
2863         {
2864           /* We display registers in column-major order.  */
2865           int regnum = i + j * rows;
2866
2867           /* Q: Why is the value passed through "extract_signed_integer",
2868              while above, in "pa_do_registers_info" it isn't?
2869              A: ? */
2870           pa_register_look_aside (raw_regs, regnum, &raw_val[0]);
2871
2872           /* Even fancier % formats to prevent leading zeros
2873              and still maintain the output in columns. */
2874           if (!is_pa_2)
2875             {
2876               /* Being big-endian, on this machine the low bits
2877                  (the ones we want to look at) are in the second longword. */
2878               long_val = extract_signed_integer (&raw_val[1], 4);
2879               printf_filtered ("%10.10s: %8lx   ",
2880                                REGISTER_NAME (regnum), long_val);
2881             }
2882           else
2883             {
2884               /* raw_val = extract_signed_integer(&raw_val, 8); */
2885               if (raw_val[0] == 0)
2886                 printf_filtered ("%10.10s:         %8lx   ",
2887                                  REGISTER_NAME (regnum), raw_val[1]);
2888               else
2889                 printf_filtered ("%10.10s: %8lx%8.8lx   ",
2890                                  REGISTER_NAME (regnum),
2891                                  raw_val[0], raw_val[1]);
2892             }
2893         }
2894       printf_unfiltered ("\n");
2895     }
2896
2897   if (fpregs)
2898     for (i = FP4_REGNUM; i < NUM_REGS; i++)     /* FP4_REGNUM == 72 */
2899       pa_print_fp_reg (i);
2900 }
2901
2902 /************* new function ******************/
2903 static void
2904 pa_strcat_registers (char *raw_regs, int regnum, int fpregs,
2905                      struct ui_file *stream)
2906 {
2907   int i, j;
2908   long raw_val[2];              /* Alas, we are compiled so that "long long" is 32 bits */
2909   long long_val;
2910   enum precision_type precision;
2911
2912   precision = unspecified_precision;
2913
2914   for (i = 0; i < 18; i++)
2915     {
2916       for (j = 0; j < 4; j++)
2917         {
2918           /* Q: Why is the value passed through "extract_signed_integer",
2919              while above, in "pa_do_registers_info" it isn't?
2920              A: ? */
2921           pa_register_look_aside (raw_regs, i + (j * 18), &raw_val[0]);
2922
2923           /* Even fancier % formats to prevent leading zeros
2924              and still maintain the output in columns. */
2925           if (!is_pa_2)
2926             {
2927               /* Being big-endian, on this machine the low bits
2928                  (the ones we want to look at) are in the second longword. */
2929               long_val = extract_signed_integer (&raw_val[1], 4);
2930               fprintf_filtered (stream, "%8.8s: %8lx  ",
2931                                 REGISTER_NAME (i + (j * 18)), long_val);
2932             }
2933           else
2934             {
2935               /* raw_val = extract_signed_integer(&raw_val, 8); */
2936               if (raw_val[0] == 0)
2937                 fprintf_filtered (stream, "%8.8s:         %8lx  ",
2938                                   REGISTER_NAME (i + (j * 18)), raw_val[1]);
2939               else
2940                 fprintf_filtered (stream, "%8.8s: %8lx%8.8lx  ",
2941                                   REGISTER_NAME (i + (j * 18)), raw_val[0],
2942                                   raw_val[1]);
2943             }
2944         }
2945       fprintf_unfiltered (stream, "\n");
2946     }
2947
2948   if (fpregs)
2949     for (i = FP4_REGNUM; i < NUM_REGS; i++)     /* FP4_REGNUM == 72 */
2950       pa_strcat_fp_reg (i, stream, precision);
2951 }
2952
2953 static void
2954 pa_print_fp_reg (int i)
2955 {
2956   char raw_buffer[MAX_REGISTER_SIZE];
2957   char virtual_buffer[MAX_REGISTER_SIZE];
2958
2959   /* Get 32bits of data.  */
2960   frame_register_read (deprecated_selected_frame, i, raw_buffer);
2961
2962   /* Put it in the buffer.  No conversions are ever necessary.  */
2963   memcpy (virtual_buffer, raw_buffer, DEPRECATED_REGISTER_RAW_SIZE (i));
2964
2965   fputs_filtered (REGISTER_NAME (i), gdb_stdout);
2966   print_spaces_filtered (8 - strlen (REGISTER_NAME (i)), gdb_stdout);
2967   fputs_filtered ("(single precision)     ", gdb_stdout);
2968
2969   val_print (DEPRECATED_REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0, gdb_stdout, 0,
2970              1, 0, Val_pretty_default);
2971   printf_filtered ("\n");
2972
2973   /* If "i" is even, then this register can also be a double-precision
2974      FP register.  Dump it out as such.  */
2975   if ((i % 2) == 0)
2976     {
2977       /* Get the data in raw format for the 2nd half.  */
2978       frame_register_read (deprecated_selected_frame, i + 1, raw_buffer);
2979
2980       /* Copy it into the appropriate part of the virtual buffer.  */
2981       memcpy (virtual_buffer + DEPRECATED_REGISTER_RAW_SIZE (i), raw_buffer,
2982               DEPRECATED_REGISTER_RAW_SIZE (i));
2983
2984       /* Dump it as a double.  */
2985       fputs_filtered (REGISTER_NAME (i), gdb_stdout);
2986       print_spaces_filtered (8 - strlen (REGISTER_NAME (i)), gdb_stdout);
2987       fputs_filtered ("(double precision)     ", gdb_stdout);
2988
2989       val_print (builtin_type_double, virtual_buffer, 0, 0, gdb_stdout, 0,
2990                  1, 0, Val_pretty_default);
2991       printf_filtered ("\n");
2992     }
2993 }
2994
2995 /*************** new function ***********************/
2996 static void
2997 pa_strcat_fp_reg (int i, struct ui_file *stream, enum precision_type precision)
2998 {
2999   char raw_buffer[MAX_REGISTER_SIZE];
3000   char virtual_buffer[MAX_REGISTER_SIZE];
3001
3002   fputs_filtered (REGISTER_NAME (i), stream);
3003   print_spaces_filtered (8 - strlen (REGISTER_NAME (i)), stream);
3004
3005   /* Get 32bits of data.  */
3006   frame_register_read (deprecated_selected_frame, i, raw_buffer);
3007
3008   /* Put it in the buffer.  No conversions are ever necessary.  */
3009   memcpy (virtual_buffer, raw_buffer, DEPRECATED_REGISTER_RAW_SIZE (i));
3010
3011   if (precision == double_precision && (i % 2) == 0)
3012     {
3013
3014       char raw_buf[MAX_REGISTER_SIZE];
3015
3016       /* Get the data in raw format for the 2nd half.  */
3017       frame_register_read (deprecated_selected_frame, i + 1, raw_buf);
3018
3019       /* Copy it into the appropriate part of the virtual buffer.  */
3020       memcpy (virtual_buffer + DEPRECATED_REGISTER_RAW_SIZE (i), raw_buf,
3021               DEPRECATED_REGISTER_RAW_SIZE (i));
3022
3023       val_print (builtin_type_double, virtual_buffer, 0, 0, stream, 0,
3024                  1, 0, Val_pretty_default);
3025
3026     }
3027   else
3028     {
3029       val_print (DEPRECATED_REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0, stream, 0,
3030                  1, 0, Val_pretty_default);
3031     }
3032
3033 }
3034
3035 /* Return one if PC is in the call path of a trampoline, else return zero.
3036
3037    Note we return one for *any* call trampoline (long-call, arg-reloc), not
3038    just shared library trampolines (import, export).  */
3039
3040 int
3041 hppa_in_solib_call_trampoline (CORE_ADDR pc, char *name)
3042 {
3043   struct minimal_symbol *minsym;
3044   struct unwind_table_entry *u;
3045   static CORE_ADDR dyncall = 0;
3046   static CORE_ADDR sr4export = 0;
3047
3048 #ifdef GDB_TARGET_IS_HPPA_20W
3049   /* PA64 has a completely different stub/trampoline scheme.  Is it
3050      better?  Maybe.  It's certainly harder to determine with any
3051      certainty that we are in a stub because we can not refer to the
3052      unwinders to help. 
3053
3054      The heuristic is simple.  Try to lookup the current PC value in th
3055      minimal symbol table.  If that fails, then assume we are not in a
3056      stub and return.
3057
3058      Then see if the PC value falls within the section bounds for the
3059      section containing the minimal symbol we found in the first
3060      step.  If it does, then assume we are not in a stub and return.
3061
3062      Finally peek at the instructions to see if they look like a stub.  */
3063   {
3064     struct minimal_symbol *minsym;
3065     asection *sec;
3066     CORE_ADDR addr;
3067     int insn, i;
3068
3069     minsym = lookup_minimal_symbol_by_pc (pc);
3070     if (! minsym)
3071       return 0;
3072
3073     sec = SYMBOL_BFD_SECTION (minsym);
3074
3075     if (bfd_get_section_vma (sec->owner, sec) <= pc
3076         && pc < (bfd_get_section_vma (sec->owner, sec)
3077                  + bfd_section_size (sec->owner, sec)))
3078       return 0;
3079
3080     /* We might be in a stub.  Peek at the instructions.  Stubs are 3
3081        instructions long. */
3082     insn = read_memory_integer (pc, 4);
3083
3084     /* Find out where we think we are within the stub.  */
3085     if ((insn & 0xffffc00e) == 0x53610000)
3086       addr = pc;
3087     else if ((insn & 0xffffffff) == 0xe820d000)
3088       addr = pc - 4;
3089     else if ((insn & 0xffffc00e) == 0x537b0000)
3090       addr = pc - 8;
3091     else
3092       return 0;
3093
3094     /* Now verify each insn in the range looks like a stub instruction.  */
3095     insn = read_memory_integer (addr, 4);
3096     if ((insn & 0xffffc00e) != 0x53610000)
3097       return 0;
3098         
3099     /* Now verify each insn in the range looks like a stub instruction.  */
3100     insn = read_memory_integer (addr + 4, 4);
3101     if ((insn & 0xffffffff) != 0xe820d000)
3102       return 0;
3103     
3104     /* Now verify each insn in the range looks like a stub instruction.  */
3105     insn = read_memory_integer (addr + 8, 4);
3106     if ((insn & 0xffffc00e) != 0x537b0000)
3107       return 0;
3108
3109     /* Looks like a stub.  */
3110     return 1;
3111   }
3112 #endif
3113
3114   /* FIXME XXX - dyncall and sr4export must be initialized whenever we get a
3115      new exec file */
3116
3117   /* First see if PC is in one of the two C-library trampolines.  */
3118   if (!dyncall)
3119     {
3120       minsym = lookup_minimal_symbol ("$$dyncall", NULL, NULL);
3121       if (minsym)
3122         dyncall = SYMBOL_VALUE_ADDRESS (minsym);
3123       else
3124         dyncall = -1;
3125     }
3126
3127   if (!sr4export)
3128     {
3129       minsym = lookup_minimal_symbol ("_sr4export", NULL, NULL);
3130       if (minsym)
3131         sr4export = SYMBOL_VALUE_ADDRESS (minsym);
3132       else
3133         sr4export = -1;
3134     }
3135
3136   if (pc == dyncall || pc == sr4export)
3137     return 1;
3138
3139   minsym = lookup_minimal_symbol_by_pc (pc);
3140   if (minsym && strcmp (DEPRECATED_SYMBOL_NAME (minsym), ".stub") == 0)
3141     return 1;
3142
3143   /* Get the unwind descriptor corresponding to PC, return zero
3144      if no unwind was found.  */
3145   u = find_unwind_entry (pc);
3146   if (!u)
3147     return 0;
3148
3149   /* If this isn't a linker stub, then return now.  */
3150   if (u->stub_unwind.stub_type == 0)
3151     return 0;
3152
3153   /* By definition a long-branch stub is a call stub.  */
3154   if (u->stub_unwind.stub_type == LONG_BRANCH)
3155     return 1;
3156
3157   /* The call and return path execute the same instructions within
3158      an IMPORT stub!  So an IMPORT stub is both a call and return
3159      trampoline.  */
3160   if (u->stub_unwind.stub_type == IMPORT)
3161     return 1;
3162
3163   /* Parameter relocation stubs always have a call path and may have a
3164      return path.  */
3165   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
3166       || u->stub_unwind.stub_type == EXPORT)
3167     {
3168       CORE_ADDR addr;
3169
3170       /* Search forward from the current PC until we hit a branch
3171          or the end of the stub.  */
3172       for (addr = pc; addr <= u->region_end; addr += 4)
3173         {
3174           unsigned long insn;
3175
3176           insn = read_memory_integer (addr, 4);
3177
3178           /* Does it look like a bl?  If so then it's the call path, if
3179              we find a bv or be first, then we're on the return path.  */
3180           if ((insn & 0xfc00e000) == 0xe8000000)
3181             return 1;
3182           else if ((insn & 0xfc00e001) == 0xe800c000
3183                    || (insn & 0xfc000000) == 0xe0000000)
3184             return 0;
3185         }
3186
3187       /* Should never happen.  */
3188       warning ("Unable to find branch in parameter relocation stub.\n");
3189       return 0;
3190     }
3191
3192   /* Unknown stub type.  For now, just return zero.  */
3193   return 0;
3194 }
3195
3196 /* Return one if PC is in the return path of a trampoline, else return zero.
3197
3198    Note we return one for *any* call trampoline (long-call, arg-reloc), not
3199    just shared library trampolines (import, export).  */
3200
3201 int
3202 hppa_in_solib_return_trampoline (CORE_ADDR pc, char *name)
3203 {
3204   struct unwind_table_entry *u;
3205
3206   /* Get the unwind descriptor corresponding to PC, return zero
3207      if no unwind was found.  */
3208   u = find_unwind_entry (pc);
3209   if (!u)
3210     return 0;
3211
3212   /* If this isn't a linker stub or it's just a long branch stub, then
3213      return zero.  */
3214   if (u->stub_unwind.stub_type == 0 || u->stub_unwind.stub_type == LONG_BRANCH)
3215     return 0;
3216
3217   /* The call and return path execute the same instructions within
3218      an IMPORT stub!  So an IMPORT stub is both a call and return
3219      trampoline.  */
3220   if (u->stub_unwind.stub_type == IMPORT)
3221     return 1;
3222
3223   /* Parameter relocation stubs always have a call path and may have a
3224      return path.  */
3225   if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
3226       || u->stub_unwind.stub_type == EXPORT)
3227     {
3228       CORE_ADDR addr;
3229
3230       /* Search forward from the current PC until we hit a branch
3231          or the end of the stub.  */
3232       for (addr = pc; addr <= u->region_end; addr += 4)
3233         {
3234           unsigned long insn;
3235
3236           insn = read_memory_integer (addr, 4);
3237
3238           /* Does it look like a bl?  If so then it's the call path, if
3239              we find a bv or be first, then we're on the return path.  */
3240           if ((insn & 0xfc00e000) == 0xe8000000)
3241             return 0;
3242           else if ((insn & 0xfc00e001) == 0xe800c000
3243                    || (insn & 0xfc000000) == 0xe0000000)
3244             return 1;
3245         }
3246
3247       /* Should never happen.  */
3248       warning ("Unable to find branch in parameter relocation stub.\n");
3249       return 0;
3250     }
3251
3252   /* Unknown stub type.  For now, just return zero.  */
3253   return 0;
3254
3255 }
3256
3257 /* Figure out if PC is in a trampoline, and if so find out where
3258    the trampoline will jump to.  If not in a trampoline, return zero.
3259
3260    Simple code examination probably is not a good idea since the code
3261    sequences in trampolines can also appear in user code.
3262
3263    We use unwinds and information from the minimal symbol table to
3264    determine when we're in a trampoline.  This won't work for ELF
3265    (yet) since it doesn't create stub unwind entries.  Whether or
3266    not ELF will create stub unwinds or normal unwinds for linker
3267    stubs is still being debated.
3268
3269    This should handle simple calls through dyncall or sr4export,
3270    long calls, argument relocation stubs, and dyncall/sr4export
3271    calling an argument relocation stub.  It even handles some stubs
3272    used in dynamic executables.  */
3273
3274 CORE_ADDR
3275 hppa_skip_trampoline_code (CORE_ADDR pc)
3276 {
3277   long orig_pc = pc;
3278   long prev_inst, curr_inst, loc;
3279   static CORE_ADDR dyncall = 0;
3280   static CORE_ADDR dyncall_external = 0;
3281   static CORE_ADDR sr4export = 0;
3282   struct minimal_symbol *msym;
3283   struct unwind_table_entry *u;
3284
3285   /* FIXME XXX - dyncall and sr4export must be initialized whenever we get a
3286      new exec file */
3287
3288   if (!dyncall)
3289     {
3290       msym = lookup_minimal_symbol ("$$dyncall", NULL, NULL);
3291       if (msym)
3292         dyncall = SYMBOL_VALUE_ADDRESS (msym);
3293       else
3294         dyncall = -1;
3295     }
3296
3297   if (!dyncall_external)
3298     {
3299       msym = lookup_minimal_symbol ("$$dyncall_external", NULL, NULL);
3300       if (msym)
3301         dyncall_external = SYMBOL_VALUE_ADDRESS (msym);
3302       else
3303         dyncall_external = -1;
3304     }
3305
3306   if (!sr4export)
3307     {
3308       msym = lookup_minimal_symbol ("_sr4export", NULL, NULL);
3309       if (msym)
3310         sr4export = SYMBOL_VALUE_ADDRESS (msym);
3311       else
3312         sr4export = -1;
3313     }
3314
3315   /* Addresses passed to dyncall may *NOT* be the actual address
3316      of the function.  So we may have to do something special.  */
3317   if (pc == dyncall)
3318     {
3319       pc = (CORE_ADDR) read_register (22);
3320
3321       /* If bit 30 (counting from the left) is on, then pc is the address of
3322          the PLT entry for this function, not the address of the function
3323          itself.  Bit 31 has meaning too, but only for MPE.  */
3324       if (pc & 0x2)
3325         pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, TARGET_PTR_BIT / 8);
3326     }
3327   if (pc == dyncall_external)
3328     {
3329       pc = (CORE_ADDR) read_register (22);
3330       pc = (CORE_ADDR) read_memory_integer (pc & ~0x3, TARGET_PTR_BIT / 8);
3331     }
3332   else if (pc == sr4export)
3333     pc = (CORE_ADDR) (read_register (22));
3334
3335   /* Get the unwind descriptor corresponding to PC, return zero
3336      if no unwind was found.  */
3337   u = find_unwind_entry (pc);
3338   if (!u)
3339     return 0;
3340
3341   /* If this isn't a linker stub, then return now.  */
3342   /* elz: attention here! (FIXME) because of a compiler/linker 
3343      error, some stubs which should have a non zero stub_unwind.stub_type 
3344      have unfortunately a value of zero. So this function would return here
3345      as if we were not in a trampoline. To fix this, we go look at the partial
3346      symbol information, which reports this guy as a stub.
3347      (FIXME): Unfortunately, we are not that lucky: it turns out that the 
3348      partial symbol information is also wrong sometimes. This is because 
3349      when it is entered (somread.c::som_symtab_read()) it can happen that
3350      if the type of the symbol (from the som) is Entry, and the symbol is
3351      in a shared library, then it can also be a trampoline.  This would
3352      be OK, except that I believe the way they decide if we are ina shared library
3353      does not work. SOOOO..., even if we have a regular function w/o trampolines
3354      its minimal symbol can be assigned type mst_solib_trampoline.
3355      Also, if we find that the symbol is a real stub, then we fix the unwind
3356      descriptor, and define the stub type to be EXPORT.
3357      Hopefully this is correct most of the times. */
3358   if (u->stub_unwind.stub_type == 0)
3359     {
3360
3361 /* elz: NOTE (FIXME!) once the problem with the unwind information is fixed
3362    we can delete all the code which appears between the lines */
3363 /*--------------------------------------------------------------------------*/
3364       msym = lookup_minimal_symbol_by_pc (pc);
3365
3366       if (msym == NULL || MSYMBOL_TYPE (msym) != mst_solib_trampoline)
3367         return orig_pc == pc ? 0 : pc & ~0x3;
3368
3369       else if (msym != NULL && MSYMBOL_TYPE (msym) == mst_solib_trampoline)
3370         {
3371           struct objfile *objfile;
3372           struct minimal_symbol *msymbol;
3373           int function_found = 0;
3374
3375           /* go look if there is another minimal symbol with the same name as 
3376              this one, but with type mst_text. This would happen if the msym
3377              is an actual trampoline, in which case there would be another
3378              symbol with the same name corresponding to the real function */
3379
3380           ALL_MSYMBOLS (objfile, msymbol)
3381           {
3382             if (MSYMBOL_TYPE (msymbol) == mst_text
3383                 && DEPRECATED_STREQ (DEPRECATED_SYMBOL_NAME (msymbol), DEPRECATED_SYMBOL_NAME (msym)))
3384               {
3385                 function_found = 1;
3386                 break;
3387               }
3388           }
3389
3390           if (function_found)
3391             /* the type of msym is correct (mst_solib_trampoline), but
3392                the unwind info is wrong, so set it to the correct value */
3393             u->stub_unwind.stub_type = EXPORT;
3394           else
3395             /* the stub type info in the unwind is correct (this is not a
3396                trampoline), but the msym type information is wrong, it
3397                should be mst_text. So we need to fix the msym, and also
3398                get out of this function */
3399             {
3400               MSYMBOL_TYPE (msym) = mst_text;
3401               return orig_pc == pc ? 0 : pc & ~0x3;
3402             }
3403         }
3404
3405 /*--------------------------------------------------------------------------*/
3406     }
3407
3408   /* It's a stub.  Search for a branch and figure out where it goes.
3409      Note we have to handle multi insn branch sequences like ldil;ble.
3410      Most (all?) other branches can be determined by examining the contents
3411      of certain registers and the stack.  */
3412
3413   loc = pc;
3414   curr_inst = 0;
3415   prev_inst = 0;
3416   while (1)
3417     {
3418       /* Make sure we haven't walked outside the range of this stub.  */
3419       if (u != find_unwind_entry (loc))
3420         {
3421           warning ("Unable to find branch in linker stub");
3422           return orig_pc == pc ? 0 : pc & ~0x3;
3423         }
3424
3425       prev_inst = curr_inst;
3426       curr_inst = read_memory_integer (loc, 4);
3427
3428       /* Does it look like a branch external using %r1?  Then it's the
3429          branch from the stub to the actual function.  */
3430       if ((curr_inst & 0xffe0e000) == 0xe0202000)
3431         {
3432           /* Yup.  See if the previous instruction loaded
3433              a value into %r1.  If so compute and return the jump address.  */
3434           if ((prev_inst & 0xffe00000) == 0x20200000)
3435             return (extract_21 (prev_inst) + extract_17 (curr_inst)) & ~0x3;
3436           else
3437             {
3438               warning ("Unable to find ldil X,%%r1 before ble Y(%%sr4,%%r1).");
3439               return orig_pc == pc ? 0 : pc & ~0x3;
3440             }
3441         }
3442
3443       /* Does it look like a be 0(sr0,%r21)? OR 
3444          Does it look like a be, n 0(sr0,%r21)? OR 
3445          Does it look like a bve (r21)? (this is on PA2.0)
3446          Does it look like a bve, n(r21)? (this is also on PA2.0)
3447          That's the branch from an
3448          import stub to an export stub.
3449
3450          It is impossible to determine the target of the branch via
3451          simple examination of instructions and/or data (consider
3452          that the address in the plabel may be the address of the
3453          bind-on-reference routine in the dynamic loader).
3454
3455          So we have try an alternative approach.
3456
3457          Get the name of the symbol at our current location; it should
3458          be a stub symbol with the same name as the symbol in the
3459          shared library.
3460
3461          Then lookup a minimal symbol with the same name; we should
3462          get the minimal symbol for the target routine in the shared
3463          library as those take precedence of import/export stubs.  */
3464       if ((curr_inst == 0xe2a00000) ||
3465           (curr_inst == 0xe2a00002) ||
3466           (curr_inst == 0xeaa0d000) ||
3467           (curr_inst == 0xeaa0d002))
3468         {
3469           struct minimal_symbol *stubsym, *libsym;
3470
3471           stubsym = lookup_minimal_symbol_by_pc (loc);
3472           if (stubsym == NULL)
3473             {
3474               warning ("Unable to find symbol for 0x%lx", loc);
3475               return orig_pc == pc ? 0 : pc & ~0x3;
3476             }
3477
3478           libsym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (stubsym), NULL, NULL);
3479           if (libsym == NULL)
3480             {
3481               warning ("Unable to find library symbol for %s\n",
3482                        DEPRECATED_SYMBOL_NAME (stubsym));
3483               return orig_pc == pc ? 0 : pc & ~0x3;
3484             }
3485
3486           return SYMBOL_VALUE (libsym);
3487         }
3488
3489       /* Does it look like bl X,%rp or bl X,%r0?  Another way to do a
3490          branch from the stub to the actual function.  */
3491       /*elz */
3492       else if ((curr_inst & 0xffe0e000) == 0xe8400000
3493                || (curr_inst & 0xffe0e000) == 0xe8000000
3494                || (curr_inst & 0xffe0e000) == 0xe800A000)
3495         return (loc + extract_17 (curr_inst) + 8) & ~0x3;
3496
3497       /* Does it look like bv (rp)?   Note this depends on the
3498          current stack pointer being the same as the stack
3499          pointer in the stub itself!  This is a branch on from the
3500          stub back to the original caller.  */
3501       /*else if ((curr_inst & 0xffe0e000) == 0xe840c000) */
3502       else if ((curr_inst & 0xffe0f000) == 0xe840c000)
3503         {
3504           /* Yup.  See if the previous instruction loaded
3505              rp from sp - 8.  */
3506           if (prev_inst == 0x4bc23ff1)
3507             return (read_memory_integer
3508                     (read_register (SP_REGNUM) - 8, 4)) & ~0x3;
3509           else
3510             {
3511               warning ("Unable to find restore of %%rp before bv (%%rp).");
3512               return orig_pc == pc ? 0 : pc & ~0x3;
3513             }
3514         }
3515
3516       /* elz: added this case to capture the new instruction
3517          at the end of the return part of an export stub used by
3518          the PA2.0: BVE, n (rp) */
3519       else if ((curr_inst & 0xffe0f000) == 0xe840d000)
3520         {
3521           return (read_memory_integer
3522                   (read_register (SP_REGNUM) - 24, TARGET_PTR_BIT / 8)) & ~0x3;
3523         }
3524
3525       /* What about be,n 0(sr0,%rp)?  It's just another way we return to
3526          the original caller from the stub.  Used in dynamic executables.  */
3527       else if (curr_inst == 0xe0400002)
3528         {
3529           /* The value we jump to is sitting in sp - 24.  But that's
3530              loaded several instructions before the be instruction.
3531              I guess we could check for the previous instruction being
3532              mtsp %r1,%sr0 if we want to do sanity checking.  */
3533           return (read_memory_integer
3534                   (read_register (SP_REGNUM) - 24, TARGET_PTR_BIT / 8)) & ~0x3;
3535         }
3536
3537       /* Haven't found the branch yet, but we're still in the stub.
3538          Keep looking.  */
3539       loc += 4;
3540     }
3541 }
3542
3543
3544 /* For the given instruction (INST), return any adjustment it makes
3545    to the stack pointer or zero for no adjustment. 
3546
3547    This only handles instructions commonly found in prologues.  */
3548
3549 static int
3550 prologue_inst_adjust_sp (unsigned long inst)
3551 {
3552   /* This must persist across calls.  */
3553   static int save_high21;
3554
3555   /* The most common way to perform a stack adjustment ldo X(sp),sp */
3556   if ((inst & 0xffffc000) == 0x37de0000)
3557     return extract_14 (inst);
3558
3559   /* stwm X,D(sp) */
3560   if ((inst & 0xffe00000) == 0x6fc00000)
3561     return extract_14 (inst);
3562
3563   /* std,ma X,D(sp) */
3564   if ((inst & 0xffe00008) == 0x73c00008)
3565     return (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);
3566
3567   /* addil high21,%r1; ldo low11,(%r1),%r30)
3568      save high bits in save_high21 for later use.  */
3569   if ((inst & 0xffe00000) == 0x28200000)
3570     {
3571       save_high21 = extract_21 (inst);
3572       return 0;
3573     }
3574
3575   if ((inst & 0xffff0000) == 0x343e0000)
3576     return save_high21 + extract_14 (inst);
3577
3578   /* fstws as used by the HP compilers.  */
3579   if ((inst & 0xffffffe0) == 0x2fd01220)
3580     return extract_5_load (inst);
3581
3582   /* No adjustment.  */
3583   return 0;
3584 }
3585
3586 /* Return nonzero if INST is a branch of some kind, else return zero.  */
3587
3588 static int
3589 is_branch (unsigned long inst)
3590 {
3591   switch (inst >> 26)
3592     {
3593     case 0x20:
3594     case 0x21:
3595     case 0x22:
3596     case 0x23:
3597     case 0x27:
3598     case 0x28:
3599     case 0x29:
3600     case 0x2a:
3601     case 0x2b:
3602     case 0x2f:
3603     case 0x30:
3604     case 0x31:
3605     case 0x32:
3606     case 0x33:
3607     case 0x38:
3608     case 0x39:
3609     case 0x3a:
3610     case 0x3b:
3611       return 1;
3612
3613     default:
3614       return 0;
3615     }
3616 }
3617
3618 /* Return the register number for a GR which is saved by INST or
3619    zero it INST does not save a GR.  */
3620
3621 static int
3622 inst_saves_gr (unsigned long inst)
3623 {
3624   /* Does it look like a stw?  */
3625   if ((inst >> 26) == 0x1a || (inst >> 26) == 0x1b
3626       || (inst >> 26) == 0x1f
3627       || ((inst >> 26) == 0x1f
3628           && ((inst >> 6) == 0xa)))
3629     return extract_5R_store (inst);
3630
3631   /* Does it look like a std?  */
3632   if ((inst >> 26) == 0x1c
3633       || ((inst >> 26) == 0x03
3634           && ((inst >> 6) & 0xf) == 0xb))
3635     return extract_5R_store (inst);
3636
3637   /* Does it look like a stwm?  GCC & HPC may use this in prologues. */
3638   if ((inst >> 26) == 0x1b)
3639     return extract_5R_store (inst);
3640
3641   /* Does it look like sth or stb?  HPC versions 9.0 and later use these
3642      too.  */
3643   if ((inst >> 26) == 0x19 || (inst >> 26) == 0x18
3644       || ((inst >> 26) == 0x3
3645           && (((inst >> 6) & 0xf) == 0x8
3646               || (inst >> 6) & 0xf) == 0x9))
3647     return extract_5R_store (inst);
3648
3649   return 0;
3650 }
3651
3652 /* Return the register number for a FR which is saved by INST or
3653    zero it INST does not save a FR.
3654
3655    Note we only care about full 64bit register stores (that's the only
3656    kind of stores the prologue will use).
3657
3658    FIXME: What about argument stores with the HP compiler in ANSI mode? */
3659
3660 static int
3661 inst_saves_fr (unsigned long inst)
3662 {
3663   /* is this an FSTD ? */
3664   if ((inst & 0xfc00dfc0) == 0x2c001200)
3665     return extract_5r_store (inst);
3666   if ((inst & 0xfc000002) == 0x70000002)
3667     return extract_5R_store (inst);
3668   /* is this an FSTW ? */
3669   if ((inst & 0xfc00df80) == 0x24001200)
3670     return extract_5r_store (inst);
3671   if ((inst & 0xfc000002) == 0x7c000000)
3672     return extract_5R_store (inst);
3673   return 0;
3674 }
3675
3676 /* Advance PC across any function entry prologue instructions
3677    to reach some "real" code. 
3678
3679    Use information in the unwind table to determine what exactly should
3680    be in the prologue.  */
3681
3682
3683 CORE_ADDR
3684 skip_prologue_hard_way (CORE_ADDR pc)
3685 {
3686   char buf[4];
3687   CORE_ADDR orig_pc = pc;
3688   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
3689   unsigned long args_stored, status, i, restart_gr, restart_fr;
3690   struct unwind_table_entry *u;
3691
3692   restart_gr = 0;
3693   restart_fr = 0;
3694
3695 restart:
3696   u = find_unwind_entry (pc);
3697   if (!u)
3698     return pc;
3699
3700   /* If we are not at the beginning of a function, then return now. */
3701   if ((pc & ~0x3) != u->region_start)
3702     return pc;
3703
3704   /* This is how much of a frame adjustment we need to account for.  */
3705   stack_remaining = u->Total_frame_size << 3;
3706
3707   /* Magic register saves we want to know about.  */
3708   save_rp = u->Save_RP;
3709   save_sp = u->Save_SP;
3710
3711   /* An indication that args may be stored into the stack.  Unfortunately
3712      the HPUX compilers tend to set this in cases where no args were
3713      stored too!.  */
3714   args_stored = 1;
3715
3716   /* Turn the Entry_GR field into a bitmask.  */
3717   save_gr = 0;
3718   for (i = 3; i < u->Entry_GR + 3; i++)
3719     {
3720       /* Frame pointer gets saved into a special location.  */
3721       if (u->Save_SP && i == DEPRECATED_FP_REGNUM)
3722         continue;
3723
3724       save_gr |= (1 << i);
3725     }
3726   save_gr &= ~restart_gr;
3727
3728   /* Turn the Entry_FR field into a bitmask too.  */
3729   save_fr = 0;
3730   for (i = 12; i < u->Entry_FR + 12; i++)
3731     save_fr |= (1 << i);
3732   save_fr &= ~restart_fr;
3733
3734   /* Loop until we find everything of interest or hit a branch.
3735
3736      For unoptimized GCC code and for any HP CC code this will never ever
3737      examine any user instructions.
3738
3739      For optimzied GCC code we're faced with problems.  GCC will schedule
3740      its prologue and make prologue instructions available for delay slot
3741      filling.  The end result is user code gets mixed in with the prologue
3742      and a prologue instruction may be in the delay slot of the first branch
3743      or call.
3744
3745      Some unexpected things are expected with debugging optimized code, so
3746      we allow this routine to walk past user instructions in optimized
3747      GCC code.  */
3748   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0
3749          || args_stored)
3750     {
3751       unsigned int reg_num;
3752       unsigned long old_stack_remaining, old_save_gr, old_save_fr;
3753       unsigned long old_save_rp, old_save_sp, next_inst;
3754
3755       /* Save copies of all the triggers so we can compare them later
3756          (only for HPC).  */
3757       old_save_gr = save_gr;
3758       old_save_fr = save_fr;
3759       old_save_rp = save_rp;
3760       old_save_sp = save_sp;
3761       old_stack_remaining = stack_remaining;
3762
3763       status = target_read_memory (pc, buf, 4);
3764       inst = extract_unsigned_integer (buf, 4);
3765
3766       /* Yow! */
3767       if (status != 0)
3768         return pc;
3769
3770       /* Note the interesting effects of this instruction.  */
3771       stack_remaining -= prologue_inst_adjust_sp (inst);
3772
3773       /* There are limited ways to store the return pointer into the
3774          stack.  */
3775       if (inst == 0x6bc23fd9 || inst == 0x0fc212c1)
3776         save_rp = 0;
3777
3778       /* These are the only ways we save SP into the stack.  At this time
3779          the HP compilers never bother to save SP into the stack.  */
3780       if ((inst & 0xffffc000) == 0x6fc10000
3781           || (inst & 0xffffc00c) == 0x73c10008)
3782         save_sp = 0;
3783
3784       /* Are we loading some register with an offset from the argument
3785          pointer?  */
3786       if ((inst & 0xffe00000) == 0x37a00000
3787           || (inst & 0xffffffe0) == 0x081d0240)
3788         {
3789           pc += 4;
3790           continue;
3791         }
3792
3793       /* Account for general and floating-point register saves.  */
3794       reg_num = inst_saves_gr (inst);
3795       save_gr &= ~(1 << reg_num);
3796
3797       /* Ugh.  Also account for argument stores into the stack.
3798          Unfortunately args_stored only tells us that some arguments
3799          where stored into the stack.  Not how many or what kind!
3800
3801          This is a kludge as on the HP compiler sets this bit and it
3802          never does prologue scheduling.  So once we see one, skip past
3803          all of them.   We have similar code for the fp arg stores below.
3804
3805          FIXME.  Can still die if we have a mix of GR and FR argument
3806          stores!  */
3807       if (reg_num >= (TARGET_PTR_BIT == 64 ? 19 : 23) && reg_num <= 26)
3808         {
3809           while (reg_num >= (TARGET_PTR_BIT == 64 ? 19 : 23) && reg_num <= 26)
3810             {
3811               pc += 4;
3812               status = target_read_memory (pc, buf, 4);
3813               inst = extract_unsigned_integer (buf, 4);
3814               if (status != 0)
3815                 return pc;
3816               reg_num = inst_saves_gr (inst);
3817             }
3818           args_stored = 0;
3819           continue;
3820         }
3821
3822       reg_num = inst_saves_fr (inst);
3823       save_fr &= ~(1 << reg_num);
3824
3825       status = target_read_memory (pc + 4, buf, 4);
3826       next_inst = extract_unsigned_integer (buf, 4);
3827
3828       /* Yow! */
3829       if (status != 0)
3830         return pc;
3831
3832       /* We've got to be read to handle the ldo before the fp register
3833          save.  */
3834       if ((inst & 0xfc000000) == 0x34000000
3835           && inst_saves_fr (next_inst) >= 4
3836           && inst_saves_fr (next_inst) <= (TARGET_PTR_BIT == 64 ? 11 : 7))
3837         {
3838           /* So we drop into the code below in a reasonable state.  */
3839           reg_num = inst_saves_fr (next_inst);
3840           pc -= 4;
3841         }
3842
3843       /* Ugh.  Also account for argument stores into the stack.
3844          This is a kludge as on the HP compiler sets this bit and it
3845          never does prologue scheduling.  So once we see one, skip past
3846          all of them.  */
3847       if (reg_num >= 4 && reg_num <= (TARGET_PTR_BIT == 64 ? 11 : 7))
3848         {
3849           while (reg_num >= 4 && reg_num <= (TARGET_PTR_BIT == 64 ? 11 : 7))
3850             {
3851               pc += 8;
3852               status = target_read_memory (pc, buf, 4);
3853               inst = extract_unsigned_integer (buf, 4);
3854               if (status != 0)
3855                 return pc;
3856               if ((inst & 0xfc000000) != 0x34000000)
3857                 break;
3858               status = target_read_memory (pc + 4, buf, 4);
3859               next_inst = extract_unsigned_integer (buf, 4);
3860               if (status != 0)
3861                 return pc;
3862               reg_num = inst_saves_fr (next_inst);
3863             }
3864           args_stored = 0;
3865           continue;
3866         }
3867
3868       /* Quit if we hit any kind of branch.  This can happen if a prologue
3869          instruction is in the delay slot of the first call/branch.  */
3870       if (is_branch (inst))
3871         break;
3872
3873       /* What a crock.  The HP compilers set args_stored even if no
3874          arguments were stored into the stack (boo hiss).  This could
3875          cause this code to then skip a bunch of user insns (up to the
3876          first branch).
3877
3878          To combat this we try to identify when args_stored was bogusly
3879          set and clear it.   We only do this when args_stored is nonzero,
3880          all other resources are accounted for, and nothing changed on
3881          this pass.  */
3882       if (args_stored
3883        && !(save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
3884           && old_save_gr == save_gr && old_save_fr == save_fr
3885           && old_save_rp == save_rp && old_save_sp == save_sp
3886           && old_stack_remaining == stack_remaining)
3887         break;
3888
3889       /* Bump the PC.  */
3890       pc += 4;
3891     }
3892
3893   /* We've got a tenative location for the end of the prologue.  However
3894      because of limitations in the unwind descriptor mechanism we may
3895      have went too far into user code looking for the save of a register
3896      that does not exist.  So, if there registers we expected to be saved
3897      but never were, mask them out and restart.
3898
3899      This should only happen in optimized code, and should be very rare.  */
3900   if (save_gr || (save_fr && !(restart_fr || restart_gr)))
3901     {
3902       pc = orig_pc;
3903       restart_gr = save_gr;
3904       restart_fr = save_fr;
3905       goto restart;
3906     }
3907
3908   return pc;
3909 }
3910
3911
3912 /* Return the address of the PC after the last prologue instruction if
3913    we can determine it from the debug symbols.  Else return zero.  */
3914
3915 static CORE_ADDR
3916 after_prologue (CORE_ADDR pc)
3917 {
3918   struct symtab_and_line sal;
3919   CORE_ADDR func_addr, func_end;
3920   struct symbol *f;
3921
3922   /* If we can not find the symbol in the partial symbol table, then
3923      there is no hope we can determine the function's start address
3924      with this code.  */
3925   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
3926     return 0;
3927
3928   /* Get the line associated with FUNC_ADDR.  */
3929   sal = find_pc_line (func_addr, 0);
3930
3931   /* There are only two cases to consider.  First, the end of the source line
3932      is within the function bounds.  In that case we return the end of the
3933      source line.  Second is the end of the source line extends beyond the
3934      bounds of the current function.  We need to use the slow code to
3935      examine instructions in that case. 
3936
3937      Anything else is simply a bug elsewhere.  Fixing it here is absolutely
3938      the wrong thing to do.  In fact, it should be entirely possible for this
3939      function to always return zero since the slow instruction scanning code
3940      is supposed to *always* work.  If it does not, then it is a bug.  */
3941   if (sal.end < func_end)
3942     return sal.end;
3943   else
3944     return 0;
3945 }
3946
3947 /* To skip prologues, I use this predicate.  Returns either PC itself
3948    if the code at PC does not look like a function prologue; otherwise
3949    returns an address that (if we're lucky) follows the prologue.  If
3950    LENIENT, then we must skip everything which is involved in setting
3951    up the frame (it's OK to skip more, just so long as we don't skip
3952    anything which might clobber the registers which are being saved.
3953    Currently we must not skip more on the alpha, but we might the lenient
3954    stuff some day.  */
3955
3956 CORE_ADDR
3957 hppa_skip_prologue (CORE_ADDR pc)
3958 {
3959   unsigned long inst;
3960   int offset;
3961   CORE_ADDR post_prologue_pc;
3962   char buf[4];
3963
3964   /* See if we can determine the end of the prologue via the symbol table.
3965      If so, then return either PC, or the PC after the prologue, whichever
3966      is greater.  */
3967
3968   post_prologue_pc = after_prologue (pc);
3969
3970   /* If after_prologue returned a useful address, then use it.  Else
3971      fall back on the instruction skipping code.
3972
3973      Some folks have claimed this causes problems because the breakpoint
3974      may be the first instruction of the prologue.  If that happens, then
3975      the instruction skipping code has a bug that needs to be fixed.  */
3976   if (post_prologue_pc != 0)
3977     return max (pc, post_prologue_pc);
3978   else
3979     return (skip_prologue_hard_way (pc));
3980 }
3981
3982 /* Put here the code to store, into the SAVED_REGS, the addresses of
3983    the saved registers of frame described by FRAME_INFO.  This
3984    includes special registers such as pc and fp saved in special ways
3985    in the stack frame.  sp is even more special: the address we return
3986    for it IS the sp for the next frame.  */
3987
3988 void
3989 hppa_frame_find_saved_regs (struct frame_info *frame_info,
3990                             CORE_ADDR frame_saved_regs[])
3991 {
3992   CORE_ADDR pc;
3993   struct unwind_table_entry *u;
3994   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
3995   int status, i, reg;
3996   char buf[4];
3997   int fp_loc = -1;
3998   int final_iteration;
3999
4000   /* Zero out everything.  */
4001   memset (frame_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
4002
4003   /* Call dummy frames always look the same, so there's no need to
4004      examine the dummy code to determine locations of saved registers;
4005      instead, let find_dummy_frame_regs fill in the correct offsets
4006      for the saved registers.  */
4007   if ((get_frame_pc (frame_info) >= get_frame_base (frame_info)
4008        && (get_frame_pc (frame_info)
4009            <= (get_frame_base (frame_info)
4010                /* A call dummy is sized in words, but it is actually a
4011                   series of instructions.  Account for that scaling
4012                   factor.  */
4013                + ((DEPRECATED_REGISTER_SIZE / INSTRUCTION_SIZE)
4014                   * DEPRECATED_CALL_DUMMY_LENGTH)
4015                /* Similarly we have to account for 64bit wide register
4016                   saves.  */
4017                + (32 * DEPRECATED_REGISTER_SIZE)
4018                /* We always consider FP regs 8 bytes long.  */
4019                + (NUM_REGS - FP0_REGNUM) * 8
4020                /* Similarly we have to account for 64bit wide register
4021                   saves.  */
4022                + (6 * DEPRECATED_REGISTER_SIZE)))))
4023     find_dummy_frame_regs (frame_info, frame_saved_regs);
4024
4025   /* Interrupt handlers are special too.  They lay out the register
4026      state in the exact same order as the register numbers in GDB.  */
4027   if (pc_in_interrupt_handler (get_frame_pc (frame_info)))
4028     {
4029       for (i = 0; i < NUM_REGS; i++)
4030         {
4031           /* SP is a little special.  */
4032           if (i == SP_REGNUM)
4033             frame_saved_regs[SP_REGNUM]
4034               = read_memory_integer (get_frame_base (frame_info) + SP_REGNUM * 4,
4035                                      TARGET_PTR_BIT / 8);
4036           else
4037             frame_saved_regs[i] = get_frame_base (frame_info) + i * 4;
4038         }
4039       return;
4040     }
4041
4042 #ifdef FRAME_FIND_SAVED_REGS_IN_SIGTRAMP
4043   /* Handle signal handler callers.  */
4044   if ((get_frame_type (frame_info) == SIGTRAMP_FRAME))
4045     {
4046       FRAME_FIND_SAVED_REGS_IN_SIGTRAMP (frame_info, frame_saved_regs);
4047       return;
4048     }
4049 #endif
4050
4051   /* Get the starting address of the function referred to by the PC
4052      saved in frame.  */
4053   pc = get_frame_func (frame_info);
4054
4055   /* Yow! */
4056   u = find_unwind_entry (pc);
4057   if (!u)
4058     return;
4059
4060   /* This is how much of a frame adjustment we need to account for.  */
4061   stack_remaining = u->Total_frame_size << 3;
4062
4063   /* Magic register saves we want to know about.  */
4064   save_rp = u->Save_RP;
4065   save_sp = u->Save_SP;
4066
4067   /* Turn the Entry_GR field into a bitmask.  */
4068   save_gr = 0;
4069   for (i = 3; i < u->Entry_GR + 3; i++)
4070     {
4071       /* Frame pointer gets saved into a special location.  */
4072       if (u->Save_SP && i == DEPRECATED_FP_REGNUM)
4073         continue;
4074
4075       save_gr |= (1 << i);
4076     }
4077
4078   /* Turn the Entry_FR field into a bitmask too.  */
4079   save_fr = 0;
4080   for (i = 12; i < u->Entry_FR + 12; i++)
4081     save_fr |= (1 << i);
4082
4083   /* The frame always represents the value of %sp at entry to the
4084      current function (and is thus equivalent to the "saved" stack
4085      pointer.  */
4086   frame_saved_regs[SP_REGNUM] = get_frame_base (frame_info);
4087
4088   /* Loop until we find everything of interest or hit a branch.
4089
4090      For unoptimized GCC code and for any HP CC code this will never ever
4091      examine any user instructions.
4092
4093      For optimized GCC code we're faced with problems.  GCC will schedule
4094      its prologue and make prologue instructions available for delay slot
4095      filling.  The end result is user code gets mixed in with the prologue
4096      and a prologue instruction may be in the delay slot of the first branch
4097      or call.
4098
4099      Some unexpected things are expected with debugging optimized code, so
4100      we allow this routine to walk past user instructions in optimized
4101      GCC code.  */
4102   final_iteration = 0;
4103   while ((save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
4104          && pc <= get_frame_pc (frame_info))
4105     {
4106       status = target_read_memory (pc, buf, 4);
4107       inst = extract_unsigned_integer (buf, 4);
4108
4109       /* Yow! */
4110       if (status != 0)
4111         return;
4112
4113       /* Note the interesting effects of this instruction.  */
4114       stack_remaining -= prologue_inst_adjust_sp (inst);
4115
4116       /* There are limited ways to store the return pointer into the
4117          stack.  */
4118       if (inst == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
4119         {
4120           save_rp = 0;
4121           frame_saved_regs[RP_REGNUM] = get_frame_base (frame_info) - 20;
4122         }
4123       else if (inst == 0x0fc212c1) /* std rp,-0x10(sr0,sp) */
4124         {
4125           save_rp = 0;
4126           frame_saved_regs[RP_REGNUM] = get_frame_base (frame_info) - 16;
4127         }
4128
4129       /* Note if we saved SP into the stack.  This also happens to indicate
4130          the location of the saved frame pointer.  */
4131       if (   (inst & 0xffffc000) == 0x6fc10000  /* stw,ma r1,N(sr0,sp) */
4132           || (inst & 0xffffc00c) == 0x73c10008) /* std,ma r1,N(sr0,sp) */
4133         {
4134           frame_saved_regs[DEPRECATED_FP_REGNUM] = get_frame_base (frame_info);
4135           save_sp = 0;
4136         }
4137
4138       /* Account for general and floating-point register saves.  */
4139       reg = inst_saves_gr (inst);
4140       if (reg >= 3 && reg <= 18
4141           && (!u->Save_SP || reg != DEPRECATED_FP_REGNUM))
4142         {
4143           save_gr &= ~(1 << reg);
4144
4145           /* stwm with a positive displacement is a *post modify*.  */
4146           if ((inst >> 26) == 0x1b
4147               && extract_14 (inst) >= 0)
4148             frame_saved_regs[reg] = get_frame_base (frame_info);
4149           /* A std has explicit post_modify forms.  */
4150           else if ((inst & 0xfc00000c0) == 0x70000008)
4151             frame_saved_regs[reg] = get_frame_base (frame_info);
4152           else
4153             {
4154               CORE_ADDR offset;
4155
4156               if ((inst >> 26) == 0x1c)
4157                 offset = (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);
4158               else if ((inst >> 26) == 0x03)
4159                 offset = low_sign_extend (inst & 0x1f, 5);
4160               else
4161                 offset = extract_14 (inst);
4162
4163               /* Handle code with and without frame pointers.  */
4164               if (u->Save_SP)
4165                 frame_saved_regs[reg]
4166                   = get_frame_base (frame_info) + offset;
4167               else
4168                 frame_saved_regs[reg]
4169                   = (get_frame_base (frame_info) + (u->Total_frame_size << 3)
4170                      + offset);
4171             }
4172         }
4173
4174
4175       /* GCC handles callee saved FP regs a little differently.  
4176
4177          It emits an instruction to put the value of the start of
4178          the FP store area into %r1.  It then uses fstds,ma with
4179          a basereg of %r1 for the stores.
4180
4181          HP CC emits them at the current stack pointer modifying
4182          the stack pointer as it stores each register.  */
4183
4184       /* ldo X(%r3),%r1 or ldo X(%r30),%r1.  */
4185       if ((inst & 0xffffc000) == 0x34610000
4186           || (inst & 0xffffc000) == 0x37c10000)
4187         fp_loc = extract_14 (inst);
4188
4189       reg = inst_saves_fr (inst);
4190       if (reg >= 12 && reg <= 21)
4191         {
4192           /* Note +4 braindamage below is necessary because the FP status
4193              registers are internally 8 registers rather than the expected
4194              4 registers.  */
4195           save_fr &= ~(1 << reg);
4196           if (fp_loc == -1)
4197             {
4198               /* 1st HP CC FP register store.  After this instruction
4199                  we've set enough state that the GCC and HPCC code are
4200                  both handled in the same manner.  */
4201               frame_saved_regs[reg + FP4_REGNUM + 4] = get_frame_base (frame_info);
4202               fp_loc = 8;
4203             }
4204           else
4205             {
4206               frame_saved_regs[reg + FP0_REGNUM + 4]
4207                 = get_frame_base (frame_info) + fp_loc;
4208               fp_loc += 8;
4209             }
4210         }
4211
4212       /* Quit if we hit any kind of branch the previous iteration. */
4213       if (final_iteration)
4214         break;
4215
4216       /* We want to look precisely one instruction beyond the branch
4217          if we have not found everything yet.  */
4218       if (is_branch (inst))
4219         final_iteration = 1;
4220
4221       /* Bump the PC.  */
4222       pc += 4;
4223     }
4224 }
4225
4226 /* XXX - deprecated.  This is a compatibility function for targets
4227    that do not yet implement DEPRECATED_FRAME_INIT_SAVED_REGS.  */
4228 /* Find the addresses in which registers are saved in FRAME.  */
4229
4230 void
4231 hppa_frame_init_saved_regs (struct frame_info *frame)
4232 {
4233   if (deprecated_get_frame_saved_regs (frame) == NULL)
4234     frame_saved_regs_zalloc (frame);
4235   hppa_frame_find_saved_regs (frame, deprecated_get_frame_saved_regs (frame));
4236 }
4237
4238 /* Exception handling support for the HP-UX ANSI C++ compiler.
4239    The compiler (aCC) provides a callback for exception events;
4240    GDB can set a breakpoint on this callback and find out what
4241    exception event has occurred. */
4242
4243 /* The name of the hook to be set to point to the callback function */
4244 static char HP_ACC_EH_notify_hook[] = "__eh_notify_hook";
4245 /* The name of the function to be used to set the hook value */
4246 static char HP_ACC_EH_set_hook_value[] = "__eh_set_hook_value";
4247 /* The name of the callback function in end.o */
4248 static char HP_ACC_EH_notify_callback[] = "__d_eh_notify_callback";
4249 /* Name of function in end.o on which a break is set (called by above) */
4250 static char HP_ACC_EH_break[] = "__d_eh_break";
4251 /* Name of flag (in end.o) that enables catching throws */
4252 static char HP_ACC_EH_catch_throw[] = "__d_eh_catch_throw";
4253 /* Name of flag (in end.o) that enables catching catching */
4254 static char HP_ACC_EH_catch_catch[] = "__d_eh_catch_catch";
4255 /* The enum used by aCC */
4256 typedef enum
4257   {
4258     __EH_NOTIFY_THROW,
4259     __EH_NOTIFY_CATCH
4260   }
4261 __eh_notification;
4262
4263 /* Is exception-handling support available with this executable? */
4264 static int hp_cxx_exception_support = 0;
4265 /* Has the initialize function been run? */
4266 int hp_cxx_exception_support_initialized = 0;
4267 /* Similar to above, but imported from breakpoint.c -- non-target-specific */
4268 extern int exception_support_initialized;
4269 /* Address of __eh_notify_hook */
4270 static CORE_ADDR eh_notify_hook_addr = 0;
4271 /* Address of __d_eh_notify_callback */
4272 static CORE_ADDR eh_notify_callback_addr = 0;
4273 /* Address of __d_eh_break */
4274 static CORE_ADDR eh_break_addr = 0;
4275 /* Address of __d_eh_catch_catch */
4276 static CORE_ADDR eh_catch_catch_addr = 0;
4277 /* Address of __d_eh_catch_throw */
4278 static CORE_ADDR eh_catch_throw_addr = 0;
4279 /* Sal for __d_eh_break */
4280 static struct symtab_and_line *break_callback_sal = 0;
4281
4282 /* Code in end.c expects __d_pid to be set in the inferior,
4283    otherwise __d_eh_notify_callback doesn't bother to call
4284    __d_eh_break!  So we poke the pid into this symbol
4285    ourselves.
4286    0 => success
4287    1 => failure  */
4288 int
4289 setup_d_pid_in_inferior (void)
4290 {
4291   CORE_ADDR anaddr;
4292   struct minimal_symbol *msymbol;
4293   char buf[4];                  /* FIXME 32x64? */
4294
4295   /* Slam the pid of the process into __d_pid; failing is only a warning!  */
4296   msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
4297   if (msymbol == NULL)
4298     {
4299       warning ("Unable to find __d_pid symbol in object file.");
4300       warning ("Suggest linking executable with -g (links in /opt/langtools/lib/end.o).");
4301       return 1;
4302     }
4303
4304   anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
4305   store_unsigned_integer (buf, 4, PIDGET (inferior_ptid)); /* FIXME 32x64? */
4306   if (target_write_memory (anaddr, buf, 4))     /* FIXME 32x64? */
4307     {
4308       warning ("Unable to write __d_pid");
4309       warning ("Suggest linking executable with -g (links in /opt/langtools/lib/end.o).");
4310       return 1;
4311     }
4312   return 0;
4313 }
4314
4315 /* Initialize exception catchpoint support by looking for the
4316    necessary hooks/callbacks in end.o, etc., and set the hook value to
4317    point to the required debug function
4318
4319    Return 0 => failure
4320    1 => success          */
4321
4322 static int
4323 initialize_hp_cxx_exception_support (void)
4324 {
4325   struct symtabs_and_lines sals;
4326   struct cleanup *old_chain;
4327   struct cleanup *canonical_strings_chain = NULL;
4328   int i;
4329   char *addr_start;
4330   char *addr_end = NULL;
4331   char **canonical = (char **) NULL;
4332   int thread = -1;
4333   struct symbol *sym = NULL;
4334   struct minimal_symbol *msym = NULL;
4335   struct objfile *objfile;
4336   asection *shlib_info;
4337
4338   /* Detect and disallow recursion.  On HP-UX with aCC, infinite
4339      recursion is a possibility because finding the hook for exception
4340      callbacks involves making a call in the inferior, which means
4341      re-inserting breakpoints which can re-invoke this code */
4342
4343   static int recurse = 0;
4344   if (recurse > 0)
4345     {
4346       hp_cxx_exception_support_initialized = 0;
4347       exception_support_initialized = 0;
4348       return 0;
4349     }
4350
4351   hp_cxx_exception_support = 0;
4352
4353   /* First check if we have seen any HP compiled objects; if not,
4354      it is very unlikely that HP's idiosyncratic callback mechanism
4355      for exception handling debug support will be available!
4356      This will percolate back up to breakpoint.c, where our callers
4357      will decide to try the g++ exception-handling support instead. */
4358   if (!hp_som_som_object_present)
4359     return 0;
4360
4361   /* We have a SOM executable with SOM debug info; find the hooks */
4362
4363   /* First look for the notify hook provided by aCC runtime libs */
4364   /* If we find this symbol, we conclude that the executable must
4365      have HP aCC exception support built in.  If this symbol is not
4366      found, even though we're a HP SOM-SOM file, we may have been
4367      built with some other compiler (not aCC).  This results percolates
4368      back up to our callers in breakpoint.c which can decide to
4369      try the g++ style of exception support instead.
4370      If this symbol is found but the other symbols we require are
4371      not found, there is something weird going on, and g++ support
4372      should *not* be tried as an alternative.
4373
4374      ASSUMPTION: Only HP aCC code will have __eh_notify_hook defined.  
4375      ASSUMPTION: HP aCC and g++ modules cannot be linked together. */
4376
4377   /* libCsup has this hook; it'll usually be non-debuggable */
4378   msym = lookup_minimal_symbol (HP_ACC_EH_notify_hook, NULL, NULL);
4379   if (msym)
4380     {
4381       eh_notify_hook_addr = SYMBOL_VALUE_ADDRESS (msym);
4382       hp_cxx_exception_support = 1;
4383     }
4384   else
4385     {
4386       warning ("Unable to find exception callback hook (%s).", HP_ACC_EH_notify_hook);
4387       warning ("Executable may not have been compiled debuggable with HP aCC.");
4388       warning ("GDB will be unable to intercept exception events.");
4389       eh_notify_hook_addr = 0;
4390       hp_cxx_exception_support = 0;
4391       return 0;
4392     }
4393
4394   /* Next look for the notify callback routine in end.o */
4395   /* This is always available in the SOM symbol dictionary if end.o is linked in */
4396   msym = lookup_minimal_symbol (HP_ACC_EH_notify_callback, NULL, NULL);
4397   if (msym)
4398     {
4399       eh_notify_callback_addr = SYMBOL_VALUE_ADDRESS (msym);
4400       hp_cxx_exception_support = 1;
4401     }
4402   else
4403     {
4404       warning ("Unable to find exception callback routine (%s).", HP_ACC_EH_notify_callback);
4405       warning ("Suggest linking executable with -g (links in /opt/langtools/lib/end.o).");
4406       warning ("GDB will be unable to intercept exception events.");
4407       eh_notify_callback_addr = 0;
4408       return 0;
4409     }
4410
4411 #ifndef GDB_TARGET_IS_HPPA_20W
4412   /* Check whether the executable is dynamically linked or archive bound */
4413   /* With an archive-bound executable we can use the raw addresses we find
4414      for the callback function, etc. without modification. For an executable
4415      with shared libraries, we have to do more work to find the plabel, which
4416      can be the target of a call through $$dyncall from the aCC runtime support
4417      library (libCsup) which is linked shared by default by aCC. */
4418   /* This test below was copied from somsolib.c/somread.c.  It may not be a very
4419      reliable one to test that an executable is linked shared. pai/1997-07-18 */
4420   shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
4421   if (shlib_info && (bfd_section_size (symfile_objfile->obfd, shlib_info) != 0))
4422     {
4423       /* The minsym we have has the local code address, but that's not the
4424          plabel that can be used by an inter-load-module call. */
4425       /* Find solib handle for main image (which has end.o), and use that
4426          and the min sym as arguments to __d_shl_get() (which does the equivalent
4427          of shl_findsym()) to find the plabel. */
4428
4429       args_for_find_stub args;
4430       static char message[] = "Error while finding exception callback hook:\n";
4431
4432       args.solib_handle = som_solib_get_solib_by_pc (eh_notify_callback_addr);
4433       args.msym = msym;
4434       args.return_val = 0;
4435
4436       recurse++;
4437       catch_errors (cover_find_stub_with_shl_get, &args, message,
4438                     RETURN_MASK_ALL);
4439       eh_notify_callback_addr = args.return_val;
4440       recurse--;
4441
4442       exception_catchpoints_are_fragile = 1;
4443
4444       if (!eh_notify_callback_addr)
4445         {
4446           /* We can get here either if there is no plabel in the export list
4447              for the main image, or if something strange happened (?) */
4448           warning ("Couldn't find a plabel (indirect function label) for the exception callback.");
4449           warning ("GDB will not be able to intercept exception events.");
4450           return 0;
4451         }
4452     }
4453   else
4454     exception_catchpoints_are_fragile = 0;
4455 #endif
4456
4457   /* Now, look for the breakpointable routine in end.o */
4458   /* This should also be available in the SOM symbol dict. if end.o linked in */
4459   msym = lookup_minimal_symbol (HP_ACC_EH_break, NULL, NULL);
4460   if (msym)
4461     {
4462       eh_break_addr = SYMBOL_VALUE_ADDRESS (msym);
4463       hp_cxx_exception_support = 1;
4464     }
4465   else
4466     {
4467       warning ("Unable to find exception callback routine to set breakpoint (%s).", HP_ACC_EH_break);
4468       warning ("Suggest linking executable with -g (link in /opt/langtools/lib/end.o).");
4469       warning ("GDB will be unable to intercept exception events.");
4470       eh_break_addr = 0;
4471       return 0;
4472     }
4473
4474   /* Next look for the catch enable flag provided in end.o */
4475   sym = lookup_symbol (HP_ACC_EH_catch_catch, (struct block *) NULL,
4476                        VAR_DOMAIN, 0, (struct symtab **) NULL);
4477   if (sym)                      /* sometimes present in debug info */
4478     {
4479       eh_catch_catch_addr = SYMBOL_VALUE_ADDRESS (sym);
4480       hp_cxx_exception_support = 1;
4481     }
4482   else
4483     /* otherwise look in SOM symbol dict. */
4484     {
4485       msym = lookup_minimal_symbol (HP_ACC_EH_catch_catch, NULL, NULL);
4486       if (msym)
4487         {
4488           eh_catch_catch_addr = SYMBOL_VALUE_ADDRESS (msym);
4489           hp_cxx_exception_support = 1;
4490         }
4491       else
4492         {
4493           warning ("Unable to enable interception of exception catches.");
4494           warning ("Executable may not have been compiled debuggable with HP aCC.");
4495           warning ("Suggest linking executable with -g (link in /opt/langtools/lib/end.o).");
4496           return 0;
4497         }
4498     }
4499
4500   /* Next look for the catch enable flag provided end.o */
4501   sym = lookup_symbol (HP_ACC_EH_catch_catch, (struct block *) NULL,
4502                        VAR_DOMAIN, 0, (struct symtab **) NULL);
4503   if (sym)                      /* sometimes present in debug info */
4504     {
4505       eh_catch_throw_addr = SYMBOL_VALUE_ADDRESS (sym);
4506       hp_cxx_exception_support = 1;
4507     }
4508   else
4509     /* otherwise look in SOM symbol dict. */
4510     {
4511       msym = lookup_minimal_symbol (HP_ACC_EH_catch_throw, NULL, NULL);
4512       if (msym)
4513         {
4514           eh_catch_throw_addr = SYMBOL_VALUE_ADDRESS (msym);
4515           hp_cxx_exception_support = 1;
4516         }
4517       else
4518         {
4519           warning ("Unable to enable interception of exception throws.");
4520           warning ("Executable may not have been compiled debuggable with HP aCC.");
4521           warning ("Suggest linking executable with -g (link in /opt/langtools/lib/end.o).");
4522           return 0;
4523         }
4524     }
4525
4526   /* Set the flags */
4527   hp_cxx_exception_support = 2; /* everything worked so far */
4528   hp_cxx_exception_support_initialized = 1;
4529   exception_support_initialized = 1;
4530
4531   return 1;
4532 }
4533
4534 /* Target operation for enabling or disabling interception of
4535    exception events.
4536    KIND is either EX_EVENT_THROW or EX_EVENT_CATCH
4537    ENABLE is either 0 (disable) or 1 (enable).
4538    Return value is NULL if no support found;
4539    -1 if something went wrong,
4540    or a pointer to a symtab/line struct if the breakpointable
4541    address was found. */
4542
4543 struct symtab_and_line *
4544 child_enable_exception_callback (enum exception_event_kind kind, int enable)
4545 {
4546   char buf[4];
4547
4548   if (!exception_support_initialized || !hp_cxx_exception_support_initialized)
4549     if (!initialize_hp_cxx_exception_support ())
4550       return NULL;
4551
4552   switch (hp_cxx_exception_support)
4553     {
4554     case 0:
4555       /* Assuming no HP support at all */
4556       return NULL;
4557     case 1:
4558       /* HP support should be present, but something went wrong */
4559       return (struct symtab_and_line *) -1;     /* yuck! */
4560       /* there may be other cases in the future */
4561     }
4562
4563   /* Set the EH hook to point to the callback routine */
4564   store_unsigned_integer (buf, 4, enable ? eh_notify_callback_addr : 0);        /* FIXME 32x64 problem */
4565   /* pai: (temp) FIXME should there be a pack operation first? */
4566   if (target_write_memory (eh_notify_hook_addr, buf, 4))        /* FIXME 32x64 problem */
4567     {
4568       warning ("Could not write to target memory for exception event callback.");
4569       warning ("Interception of exception events may not work.");
4570       return (struct symtab_and_line *) -1;
4571     }
4572   if (enable)
4573     {
4574       /* Ensure that __d_pid is set up correctly -- end.c code checks this. :-( */
4575       if (PIDGET (inferior_ptid) > 0)
4576         {
4577           if (setup_d_pid_in_inferior ())
4578             return (struct symtab_and_line *) -1;
4579         }
4580       else
4581         {
4582           warning ("Internal error: Invalid inferior pid?  Cannot intercept exception events.");
4583           return (struct symtab_and_line *) -1;
4584         }
4585     }
4586
4587   switch (kind)
4588     {
4589     case EX_EVENT_THROW:
4590       store_unsigned_integer (buf, 4, enable ? 1 : 0);
4591       if (target_write_memory (eh_catch_throw_addr, buf, 4))    /* FIXME 32x64? */
4592         {
4593           warning ("Couldn't enable exception throw interception.");
4594           return (struct symtab_and_line *) -1;
4595         }
4596       break;
4597     case EX_EVENT_CATCH:
4598       store_unsigned_integer (buf, 4, enable ? 1 : 0);
4599       if (target_write_memory (eh_catch_catch_addr, buf, 4))    /* FIXME 32x64? */
4600         {
4601           warning ("Couldn't enable exception catch interception.");
4602           return (struct symtab_and_line *) -1;
4603         }
4604       break;
4605     default:
4606       error ("Request to enable unknown or unsupported exception event.");
4607     }
4608
4609   /* Copy break address into new sal struct, malloc'ing if needed. */
4610   if (!break_callback_sal)
4611     {
4612       break_callback_sal = (struct symtab_and_line *) xmalloc (sizeof (struct symtab_and_line));
4613     }
4614   init_sal (break_callback_sal);
4615   break_callback_sal->symtab = NULL;
4616   break_callback_sal->pc = eh_break_addr;
4617   break_callback_sal->line = 0;
4618   break_callback_sal->end = eh_break_addr;
4619
4620   return break_callback_sal;
4621 }
4622
4623 /* Record some information about the current exception event */
4624 static struct exception_event_record current_ex_event;
4625 /* Convenience struct */
4626 static struct symtab_and_line null_symtab_and_line =
4627 {NULL, 0, 0, 0};
4628
4629 /* Report current exception event.  Returns a pointer to a record
4630    that describes the kind of the event, where it was thrown from,
4631    and where it will be caught.  More information may be reported
4632    in the future */
4633 struct exception_event_record *
4634 child_get_current_exception_event (void)
4635 {
4636   CORE_ADDR event_kind;
4637   CORE_ADDR throw_addr;
4638   CORE_ADDR catch_addr;
4639   struct frame_info *fi, *curr_frame;
4640   int level = 1;
4641
4642   curr_frame = get_current_frame ();
4643   if (!curr_frame)
4644     return (struct exception_event_record *) NULL;
4645
4646   /* Go up one frame to __d_eh_notify_callback, because at the
4647      point when this code is executed, there's garbage in the
4648      arguments of __d_eh_break. */
4649   fi = find_relative_frame (curr_frame, &level);
4650   if (level != 0)
4651     return (struct exception_event_record *) NULL;
4652
4653   select_frame (fi);
4654
4655   /* Read in the arguments */
4656   /* __d_eh_notify_callback() is called with 3 arguments:
4657      1. event kind catch or throw
4658      2. the target address if known
4659      3. a flag -- not sure what this is. pai/1997-07-17 */
4660   event_kind = read_register (ARG0_REGNUM);
4661   catch_addr = read_register (ARG1_REGNUM);
4662
4663   /* Now go down to a user frame */
4664   /* For a throw, __d_eh_break is called by
4665      __d_eh_notify_callback which is called by
4666      __notify_throw which is called
4667      from user code.
4668      For a catch, __d_eh_break is called by
4669      __d_eh_notify_callback which is called by
4670      <stackwalking stuff> which is called by
4671      __throw__<stuff> or __rethrow_<stuff> which is called
4672      from user code. */
4673   /* FIXME: Don't use such magic numbers; search for the frames */
4674   level = (event_kind == EX_EVENT_THROW) ? 3 : 4;
4675   fi = find_relative_frame (curr_frame, &level);
4676   if (level != 0)
4677     return (struct exception_event_record *) NULL;
4678
4679   select_frame (fi);
4680   throw_addr = get_frame_pc (fi);
4681
4682   /* Go back to original (top) frame */
4683   select_frame (curr_frame);
4684
4685   current_ex_event.kind = (enum exception_event_kind) event_kind;
4686   current_ex_event.throw_sal = find_pc_line (throw_addr, 1);
4687   current_ex_event.catch_sal = find_pc_line (catch_addr, 1);
4688
4689   return &current_ex_event;
4690 }
4691
4692 /* Instead of this nasty cast, add a method pvoid() that prints out a
4693    host VOID data type (remember %p isn't portable).  */
4694
4695 static CORE_ADDR
4696 hppa_pointer_to_address_hack (void *ptr)
4697 {
4698   gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
4699   return POINTER_TO_ADDRESS (builtin_type_void_data_ptr, &ptr);
4700 }
4701
4702 static void
4703 unwind_command (char *exp, int from_tty)
4704 {
4705   CORE_ADDR address;
4706   struct unwind_table_entry *u;
4707
4708   /* If we have an expression, evaluate it and use it as the address.  */
4709
4710   if (exp != 0 && *exp != 0)
4711     address = parse_and_eval_address (exp);
4712   else
4713     return;
4714
4715   u = find_unwind_entry (address);
4716
4717   if (!u)
4718     {
4719       printf_unfiltered ("Can't find unwind table entry for %s\n", exp);
4720       return;
4721     }
4722
4723   printf_unfiltered ("unwind_table_entry (0x%s):\n",
4724                      paddr_nz (hppa_pointer_to_address_hack (u)));
4725
4726   printf_unfiltered ("\tregion_start = ");
4727   print_address (u->region_start, gdb_stdout);
4728
4729   printf_unfiltered ("\n\tregion_end = ");
4730   print_address (u->region_end, gdb_stdout);
4731
4732 #define pif(FLD) if (u->FLD) printf_unfiltered (" "#FLD);
4733
4734   printf_unfiltered ("\n\tflags =");
4735   pif (Cannot_unwind);
4736   pif (Millicode);
4737   pif (Millicode_save_sr0);
4738   pif (Entry_SR);
4739   pif (Args_stored);
4740   pif (Variable_Frame);
4741   pif (Separate_Package_Body);
4742   pif (Frame_Extension_Millicode);
4743   pif (Stack_Overflow_Check);
4744   pif (Two_Instruction_SP_Increment);
4745   pif (Ada_Region);
4746   pif (Save_SP);
4747   pif (Save_RP);
4748   pif (Save_MRP_in_frame);
4749   pif (extn_ptr_defined);
4750   pif (Cleanup_defined);
4751   pif (MPE_XL_interrupt_marker);
4752   pif (HP_UX_interrupt_marker);
4753   pif (Large_frame);
4754
4755   putchar_unfiltered ('\n');
4756
4757 #define pin(FLD) printf_unfiltered ("\t"#FLD" = 0x%x\n", u->FLD);
4758
4759   pin (Region_description);
4760   pin (Entry_FR);
4761   pin (Entry_GR);
4762   pin (Total_frame_size);
4763 }
4764
4765 void
4766 hppa_skip_permanent_breakpoint (void)
4767 {
4768   /* To step over a breakpoint instruction on the PA takes some
4769      fiddling with the instruction address queue.
4770
4771      When we stop at a breakpoint, the IA queue front (the instruction
4772      we're executing now) points at the breakpoint instruction, and
4773      the IA queue back (the next instruction to execute) points to
4774      whatever instruction we would execute after the breakpoint, if it
4775      were an ordinary instruction.  This is the case even if the
4776      breakpoint is in the delay slot of a branch instruction.
4777
4778      Clearly, to step past the breakpoint, we need to set the queue
4779      front to the back.  But what do we put in the back?  What
4780      instruction comes after that one?  Because of the branch delay
4781      slot, the next insn is always at the back + 4.  */
4782   write_register (PCOQ_HEAD_REGNUM, read_register (PCOQ_TAIL_REGNUM));
4783   write_register (PCSQ_HEAD_REGNUM, read_register (PCSQ_TAIL_REGNUM));
4784
4785   write_register (PCOQ_TAIL_REGNUM, read_register (PCOQ_TAIL_REGNUM) + 4);
4786   /* We can leave the tail's space the same, since there's no jump.  */
4787 }
4788
4789 /* Copy the function value from VALBUF into the proper location
4790    for a function return.
4791
4792    Called only in the context of the "return" command.  */
4793
4794 void
4795 hppa32_store_return_value (struct type *type, char *valbuf)
4796 {
4797   /* For software floating point, the return value goes into the
4798      integer registers.  But we do not have any flag to key this on,
4799      so we always store the value into the integer registers.
4800
4801      If its a float value, then we also store it into the floating
4802      point registers.  */
4803   deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (28)
4804                                    + (TYPE_LENGTH (type) > 4
4805                                       ? (8 - TYPE_LENGTH (type))
4806                                       : (4 - TYPE_LENGTH (type))),
4807                                    valbuf, TYPE_LENGTH (type));
4808   if (TYPE_CODE (type) == TYPE_CODE_FLT)
4809     deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (FP4_REGNUM),
4810                                      valbuf, TYPE_LENGTH (type));
4811 }
4812
4813 /* Same as hppa32_store_return_value(), but for the PA64 ABI.  */
4814
4815 void
4816 hppa64_store_return_value (struct type *type, char *valbuf)
4817 {
4818   if (TYPE_CODE (type) == TYPE_CODE_FLT)
4819     deprecated_write_register_bytes
4820       (DEPRECATED_REGISTER_BYTE (FP4_REGNUM)
4821         + DEPRECATED_REGISTER_SIZE - TYPE_LENGTH (type),
4822        valbuf, TYPE_LENGTH (type));
4823   else if (is_integral_type(type))
4824     deprecated_write_register_bytes
4825       (DEPRECATED_REGISTER_BYTE (28)
4826         + DEPRECATED_REGISTER_SIZE - TYPE_LENGTH (type),
4827        valbuf, TYPE_LENGTH (type));
4828   else if (TYPE_LENGTH (type) <= 8)
4829     deprecated_write_register_bytes
4830       (DEPRECATED_REGISTER_BYTE (28),valbuf, TYPE_LENGTH (type));
4831   else if (TYPE_LENGTH (type) <= 16)
4832     {
4833       deprecated_write_register_bytes (DEPRECATED_REGISTER_BYTE (28),valbuf, 8);
4834       deprecated_write_register_bytes
4835         (DEPRECATED_REGISTER_BYTE (29), valbuf + 8, TYPE_LENGTH (type) - 8);
4836     }
4837 }
4838
4839 /* Copy the function's return value into VALBUF.
4840
4841    This function is called only in the context of "target function calls",
4842    ie. when the debugger forces a function to be called in the child, and
4843    when the debugger forces a fucntion to return prematurely via the
4844    "return" command.  */
4845
4846 void
4847 hppa32_extract_return_value (struct type *type, char *regbuf, char *valbuf)
4848 {
4849   if (TYPE_CODE (type) == TYPE_CODE_FLT)
4850     memcpy (valbuf, regbuf + DEPRECATED_REGISTER_BYTE (FP4_REGNUM), TYPE_LENGTH (type));
4851   else
4852     memcpy (valbuf,
4853             (regbuf
4854              + DEPRECATED_REGISTER_BYTE (28)
4855              + (TYPE_LENGTH (type) > 4
4856                 ? (8 - TYPE_LENGTH (type))
4857                 : (4 - TYPE_LENGTH (type)))),
4858             TYPE_LENGTH (type));
4859 }
4860
4861 /* Same as hppa32_extract_return_value but for the PA64 ABI case.  */
4862
4863 void
4864 hppa64_extract_return_value (struct type *type, char *regbuf, char *valbuf)
4865 {
4866   /* RM: Floats are returned in FR4R, doubles in FR4.
4867          Integral values are in r28, padded on the left.
4868          Aggregates less that 65 bits are in r28, right padded.
4869          Aggregates upto 128 bits are in r28 and r29, right padded.  */ 
4870   if (TYPE_CODE (type) == TYPE_CODE_FLT)
4871     memcpy (valbuf,
4872             regbuf + DEPRECATED_REGISTER_BYTE (FP4_REGNUM)
4873              + DEPRECATED_REGISTER_SIZE - TYPE_LENGTH (type),
4874             TYPE_LENGTH (type));
4875   else if (is_integral_type(type))
4876     memcpy (valbuf,
4877             regbuf + DEPRECATED_REGISTER_BYTE (28)
4878              + DEPRECATED_REGISTER_SIZE - TYPE_LENGTH (type),
4879             TYPE_LENGTH (type));
4880   else if (TYPE_LENGTH (type) <= 8)
4881     memcpy (valbuf, regbuf + DEPRECATED_REGISTER_BYTE (28),
4882             TYPE_LENGTH (type));
4883   else if (TYPE_LENGTH (type) <= 16)
4884     {
4885       memcpy (valbuf, regbuf + DEPRECATED_REGISTER_BYTE (28), 8);
4886       memcpy (valbuf + 8, regbuf + DEPRECATED_REGISTER_BYTE (29),
4887               TYPE_LENGTH (type) - 8);
4888     }
4889 }
4890
4891 int
4892 hppa_reg_struct_has_addr (int gcc_p, struct type *type)
4893 {
4894   /* On the PA, any pass-by-value structure > 8 bytes is actually passed
4895      via a pointer regardless of its type or the compiler used.  */
4896   return (TYPE_LENGTH (type) > 8);
4897 }
4898
4899 int
4900 hppa_inner_than (CORE_ADDR lhs, CORE_ADDR rhs)
4901 {
4902   /* Stack grows upward */
4903   return (lhs > rhs);
4904 }
4905
4906 CORE_ADDR
4907 hppa32_stack_align (CORE_ADDR sp)
4908 {
4909   /* elz: adjust the quantity to the next highest value which is
4910      64-bit aligned.  This is used in valops.c, when the sp is adjusted.
4911      On hppa the sp must always be kept 64-bit aligned */
4912   return ((sp % 8) ? (sp + 7) & -8 : sp);
4913 }
4914
4915 CORE_ADDR
4916 hppa64_stack_align (CORE_ADDR sp)
4917 {
4918   /* The PA64 ABI mandates a 16 byte stack alignment.  */
4919   return ((sp % 16) ? (sp + 15) & -16 : sp);
4920 }
4921
4922 int
4923 hppa_pc_requires_run_before_use (CORE_ADDR pc)
4924 {
4925   /* Sometimes we may pluck out a minimal symbol that has a negative address.
4926   
4927      An example of this occurs when an a.out is linked against a foo.sl.
4928      The foo.sl defines a global bar(), and the a.out declares a signature
4929      for bar().  However, the a.out doesn't directly call bar(), but passes
4930      its address in another call.
4931   
4932      If you have this scenario and attempt to "break bar" before running,
4933      gdb will find a minimal symbol for bar() in the a.out.  But that
4934      symbol's address will be negative.  What this appears to denote is
4935      an index backwards from the base of the procedure linkage table (PLT)
4936      into the data linkage table (DLT), the end of which is contiguous
4937      with the start of the PLT.  This is clearly not a valid address for
4938      us to set a breakpoint on.
4939   
4940      Note that one must be careful in how one checks for a negative address.
4941      0xc0000000 is a legitimate address of something in a shared text
4942      segment, for example.  Since I don't know what the possible range
4943      is of these "really, truly negative" addresses that come from the
4944      minimal symbols, I'm resorting to the gross hack of checking the
4945      top byte of the address for all 1's.  Sigh.  */
4946
4947   return (!target_has_stack && (pc & 0xFF000000));
4948 }
4949
4950 int
4951 hppa_instruction_nullified (void)
4952 {
4953   /* brobecker 2002/11/07: Couldn't we use a ULONGEST here? It would
4954      avoid the type cast.  I'm leaving it as is for now as I'm doing
4955      semi-mechanical multiarching-related changes.  */
4956   const int ipsw = (int) read_register (IPSW_REGNUM);
4957   const int flags = (int) read_register (FLAGS_REGNUM);
4958
4959   return ((ipsw & 0x00200000) && !(flags & 0x2));
4960 }
4961
4962 int
4963 hppa_register_raw_size (int reg_nr)
4964 {
4965   /* All registers have the same size.  */
4966   return DEPRECATED_REGISTER_SIZE;
4967 }
4968
4969 /* Index within the register vector of the first byte of the space i
4970    used for register REG_NR.  */
4971
4972 int
4973 hppa_register_byte (int reg_nr)
4974 {
4975   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
4976
4977   return reg_nr * tdep->bytes_per_address;
4978 }
4979
4980 /* Return the GDB type object for the "standard" data type of data
4981    in register N.  */
4982
4983 struct type *
4984 hppa32_register_virtual_type (int reg_nr)
4985 {
4986    if (reg_nr < FP4_REGNUM)
4987      return builtin_type_int;
4988    else
4989      return builtin_type_float;
4990 }
4991
4992 /* Return the GDB type object for the "standard" data type of data
4993    in register N.  hppa64 version.  */
4994
4995 struct type *
4996 hppa64_register_virtual_type (int reg_nr)
4997 {
4998    if (reg_nr < FP4_REGNUM)
4999      return builtin_type_unsigned_long_long;
5000    else
5001      return builtin_type_double;
5002 }
5003
5004 /* Store the address of the place in which to copy the structure the
5005    subroutine will return.  This is called from call_function.  */
5006
5007 void
5008 hppa_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
5009 {
5010   write_register (28, addr);
5011 }
5012
5013 CORE_ADDR
5014 hppa_extract_struct_value_address (char *regbuf)
5015 {
5016   /* Extract from an array REGBUF containing the (raw) register state
5017      the address in which a function should return its structure value,
5018      as a CORE_ADDR (or an expression that can be used as one).  */
5019   /* FIXME: brobecker 2002-12-26.
5020      The current implementation is historical, but we should eventually
5021      implement it in a more robust manner as it relies on the fact that
5022      the address size is equal to the size of an int* _on the host_...
5023      One possible implementation that crossed my mind is to use
5024      extract_address.  */
5025   /* FIXME: cagney/2003-09-27: This function can probably go.  ELZ
5026      writes: We cannot assume on the pa that r28 still contains the
5027      address of the returned structure. Usually this will be
5028      overwritten by the callee.  */
5029   return (*(int *)(regbuf + DEPRECATED_REGISTER_BYTE (28)));
5030 }
5031
5032 /* Return True if REGNUM is not a register available to the user
5033    through ptrace().  */
5034
5035 int
5036 hppa_cannot_store_register (int regnum)
5037 {
5038   return (regnum == 0
5039           || regnum == PCSQ_HEAD_REGNUM
5040           || (regnum >= PCSQ_TAIL_REGNUM && regnum < IPSW_REGNUM)
5041           || (regnum > IPSW_REGNUM && regnum < FP4_REGNUM));
5042
5043 }
5044
5045 CORE_ADDR
5046 hppa_smash_text_address (CORE_ADDR addr)
5047 {
5048   /* The low two bits of the PC on the PA contain the privilege level.
5049      Some genius implementing a (non-GCC) compiler apparently decided
5050      this means that "addresses" in a text section therefore include a
5051      privilege level, and thus symbol tables should contain these bits.
5052      This seems like a bonehead thing to do--anyway, it seems to work
5053      for our purposes to just ignore those bits.  */
5054
5055   return (addr &= ~0x3);
5056 }
5057
5058 /* Get the ith function argument for the current function.  */
5059 CORE_ADDR
5060 hppa_fetch_pointer_argument (struct frame_info *frame, int argi, 
5061                              struct type *type)
5062 {
5063   CORE_ADDR addr;
5064   get_frame_register (frame, R0_REGNUM + 26 - argi, &addr);
5065   return addr;
5066 }
5067
5068 /* Here is a table of C type sizes on hppa with various compiles
5069    and options.  I measured this on PA 9000/800 with HP-UX 11.11
5070    and these compilers:
5071
5072      /usr/ccs/bin/cc    HP92453-01 A.11.01.21
5073      /opt/ansic/bin/cc  HP92453-01 B.11.11.28706.GP
5074      /opt/aCC/bin/aCC   B3910B A.03.45
5075      gcc                gcc 3.3.2 native hppa2.0w-hp-hpux11.11
5076
5077      cc            : 1 2 4 4 8 : 4 8 -- : 4 4
5078      ansic +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
5079      ansic +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
5080      ansic +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
5081      acc   +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
5082      acc   +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
5083      acc   +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
5084      gcc           : 1 2 4 4 8 : 4 8 16 : 4 4
5085
5086    Each line is:
5087
5088      compiler and options
5089      char, short, int, long, long long
5090      float, double, long double
5091      char *, void (*)()
5092
5093    So all these compilers use either ILP32 or LP64 model.
5094    TODO: gcc has more options so it needs more investigation.
5095
5096    For floating point types, see:
5097
5098      http://docs.hp.com/hpux/pdf/B3906-90006.pdf
5099      HP-UX floating-point guide, hpux 11.00
5100
5101    -- chastain 2003-12-18  */
5102
5103 static struct gdbarch *
5104 hppa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
5105 {
5106   struct gdbarch_tdep *tdep;
5107   struct gdbarch *gdbarch;
5108   
5109   /* Try to determine the ABI of the object we are loading.  */
5110   if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
5111     {
5112       /* If it's a SOM file, assume it's HP/UX SOM.  */
5113       if (bfd_get_flavour (info.abfd) == bfd_target_som_flavour)
5114         info.osabi = GDB_OSABI_HPUX_SOM;
5115     }
5116
5117   /* find a candidate among the list of pre-declared architectures.  */
5118   arches = gdbarch_list_lookup_by_info (arches, &info);
5119   if (arches != NULL)
5120     return (arches->gdbarch);
5121
5122   /* If none found, then allocate and initialize one.  */
5123   tdep = XMALLOC (struct gdbarch_tdep);
5124   gdbarch = gdbarch_alloc (&info, tdep);
5125
5126   /* Determine from the bfd_arch_info structure if we are dealing with
5127      a 32 or 64 bits architecture.  If the bfd_arch_info is not available,
5128      then default to a 32bit machine.  */
5129   if (info.bfd_arch_info != NULL)
5130     tdep->bytes_per_address =
5131       info.bfd_arch_info->bits_per_address / info.bfd_arch_info->bits_per_byte;
5132   else
5133     tdep->bytes_per_address = 4;
5134
5135   /* Some parts of the gdbarch vector depend on whether we are running
5136      on a 32 bits or 64 bits target.  */
5137   switch (tdep->bytes_per_address)
5138     {
5139       case 4:
5140         set_gdbarch_num_regs (gdbarch, hppa32_num_regs);
5141         set_gdbarch_register_name (gdbarch, hppa32_register_name);
5142         set_gdbarch_deprecated_register_virtual_type
5143           (gdbarch, hppa32_register_virtual_type);
5144         set_gdbarch_deprecated_call_dummy_length
5145           (gdbarch, hppa32_call_dummy_length);
5146         set_gdbarch_deprecated_stack_align (gdbarch, hppa32_stack_align);
5147         set_gdbarch_deprecated_reg_struct_has_addr
5148           (gdbarch, hppa_reg_struct_has_addr);
5149         set_gdbarch_deprecated_extract_return_value
5150           (gdbarch, hppa32_extract_return_value);
5151         set_gdbarch_use_struct_convention
5152           (gdbarch, hppa32_use_struct_convention);
5153         set_gdbarch_deprecated_store_return_value
5154           (gdbarch, hppa32_store_return_value);
5155         break;
5156       case 8:
5157         set_gdbarch_num_regs (gdbarch, hppa64_num_regs);
5158         set_gdbarch_register_name (gdbarch, hppa64_register_name);
5159         set_gdbarch_deprecated_register_virtual_type
5160           (gdbarch, hppa64_register_virtual_type);
5161         set_gdbarch_deprecated_call_dummy_breakpoint_offset
5162           (gdbarch, hppa64_call_dummy_breakpoint_offset);
5163         set_gdbarch_deprecated_call_dummy_length
5164           (gdbarch, hppa64_call_dummy_length);
5165         set_gdbarch_deprecated_stack_align (gdbarch, hppa64_stack_align);
5166         set_gdbarch_deprecated_extract_return_value
5167           (gdbarch, hppa64_extract_return_value);
5168         set_gdbarch_use_struct_convention
5169           (gdbarch, hppa64_use_struct_convention);
5170         set_gdbarch_deprecated_store_return_value
5171           (gdbarch, hppa64_store_return_value);
5172         break;
5173       default:
5174         internal_error (__FILE__, __LINE__, "Unsupported address size: %d",
5175                         tdep->bytes_per_address);
5176     }
5177
5178   /* The following gdbarch vector elements depend on other parts of this
5179      vector which have been set above, depending on the ABI.  */
5180   set_gdbarch_deprecated_register_bytes
5181     (gdbarch, gdbarch_num_regs (gdbarch) * tdep->bytes_per_address);
5182   set_gdbarch_long_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
5183   set_gdbarch_ptr_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
5184
5185   /* The following gdbarch vector elements are the same in both ILP32
5186      and LP64, but might show differences some day.  */
5187   set_gdbarch_long_long_bit (gdbarch, 64);
5188   set_gdbarch_long_double_bit (gdbarch, 128);
5189   set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_big);
5190
5191   /* The following gdbarch vector elements do not depend on the address
5192      size, or in any other gdbarch element previously set.  */
5193   set_gdbarch_skip_prologue (gdbarch, hppa_skip_prologue);
5194   set_gdbarch_skip_trampoline_code (gdbarch, hppa_skip_trampoline_code);
5195   set_gdbarch_in_solib_call_trampoline (gdbarch, hppa_in_solib_call_trampoline);
5196   set_gdbarch_in_solib_return_trampoline (gdbarch,
5197                                           hppa_in_solib_return_trampoline);
5198   set_gdbarch_deprecated_saved_pc_after_call (gdbarch, hppa_saved_pc_after_call);
5199   set_gdbarch_inner_than (gdbarch, hppa_inner_than);
5200   set_gdbarch_deprecated_register_size (gdbarch, tdep->bytes_per_address);
5201   set_gdbarch_deprecated_fp_regnum (gdbarch, 3);
5202   set_gdbarch_sp_regnum (gdbarch, 30);
5203   set_gdbarch_fp0_regnum (gdbarch, 64);
5204   set_gdbarch_pc_regnum (gdbarch, PCOQ_HEAD_REGNUM);
5205   set_gdbarch_deprecated_register_raw_size (gdbarch, hppa_register_raw_size);
5206   set_gdbarch_deprecated_register_byte (gdbarch, hppa_register_byte);
5207   set_gdbarch_deprecated_register_virtual_size (gdbarch, hppa_register_raw_size);
5208   set_gdbarch_deprecated_max_register_raw_size (gdbarch, tdep->bytes_per_address);
5209   set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 8);
5210   set_gdbarch_deprecated_store_struct_return (gdbarch, hppa_store_struct_return);
5211   set_gdbarch_deprecated_extract_struct_value_address
5212     (gdbarch, hppa_extract_struct_value_address);
5213   set_gdbarch_cannot_store_register (gdbarch, hppa_cannot_store_register);
5214   set_gdbarch_deprecated_init_extra_frame_info (gdbarch, hppa_init_extra_frame_info);
5215   set_gdbarch_deprecated_frame_chain (gdbarch, hppa_frame_chain);
5216   set_gdbarch_deprecated_frame_chain_valid (gdbarch, hppa_frame_chain_valid);
5217   set_gdbarch_frameless_function_invocation
5218     (gdbarch, hppa_frameless_function_invocation);
5219   set_gdbarch_deprecated_frame_saved_pc (gdbarch, hppa_frame_saved_pc);
5220   set_gdbarch_frame_args_skip (gdbarch, 0);
5221   set_gdbarch_deprecated_push_dummy_frame (gdbarch, hppa_push_dummy_frame);
5222   set_gdbarch_deprecated_pop_frame (gdbarch, hppa_pop_frame);
5223   /* set_gdbarch_deprecated_fix_call_dummy (gdbarch, hppa_fix_call_dummy); */
5224   set_gdbarch_deprecated_push_arguments (gdbarch, hppa_push_arguments);
5225   set_gdbarch_addr_bits_remove (gdbarch, hppa_smash_text_address);
5226   set_gdbarch_smash_text_address (gdbarch, hppa_smash_text_address);
5227   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
5228   set_gdbarch_read_pc (gdbarch, hppa_target_read_pc);
5229   set_gdbarch_write_pc (gdbarch, hppa_target_write_pc);
5230   set_gdbarch_deprecated_target_read_fp (gdbarch, hppa_target_read_fp);
5231
5232   /* Helper for function argument information.  */
5233   set_gdbarch_fetch_pointer_argument (gdbarch, hppa_fetch_pointer_argument);
5234
5235   set_gdbarch_print_insn (gdbarch, print_insn_hppa);
5236
5237   /* When a hardware watchpoint triggers, we'll move the inferior past
5238      it by removing all eventpoints; stepping past the instruction
5239      that caused the trigger; reinserting eventpoints; and checking
5240      whether any watched location changed.  */
5241   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
5242
5243   /* Hook in ABI-specific overrides, if they have been registered.  */
5244   gdbarch_init_osabi (info, gdbarch);
5245
5246   return gdbarch;
5247 }
5248
5249 static void
5250 hppa_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
5251 {
5252    /* Nothing to print for the moment.  */
5253 }
5254
5255 void
5256 _initialize_hppa_tdep (void)
5257 {
5258   struct cmd_list_element *c;
5259   void break_at_finish_command (char *arg, int from_tty);
5260   void tbreak_at_finish_command (char *arg, int from_tty);
5261   void break_at_finish_at_depth_command (char *arg, int from_tty);
5262
5263   gdbarch_register (bfd_arch_hppa, hppa_gdbarch_init, hppa_dump_tdep);
5264
5265   add_cmd ("unwind", class_maintenance, unwind_command,
5266            "Print unwind table entry at given address.",
5267            &maintenanceprintlist);
5268
5269   deprecate_cmd (add_com ("xbreak", class_breakpoint, 
5270                           break_at_finish_command,
5271                           concat ("Set breakpoint at procedure exit. \n\
5272 Argument may be function name, or \"*\" and an address.\n\
5273 If function is specified, break at end of code for that function.\n\
5274 If an address is specified, break at the end of the function that contains \n\
5275 that exact address.\n",
5276                    "With no arg, uses current execution address of selected stack frame.\n\
5277 This is useful for breaking on return to a stack frame.\n\
5278 \n\
5279 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
5280 \n\
5281 Do \"help breakpoints\" for info on other commands dealing with breakpoints.", NULL)), NULL);
5282   deprecate_cmd (add_com_alias ("xb", "xbreak", class_breakpoint, 1), NULL);
5283   deprecate_cmd (add_com_alias ("xbr", "xbreak", class_breakpoint, 1), NULL);
5284   deprecate_cmd (add_com_alias ("xbre", "xbreak", class_breakpoint, 1), NULL);
5285   deprecate_cmd (add_com_alias ("xbrea", "xbreak", class_breakpoint, 1), NULL);
5286
5287   deprecate_cmd (c = add_com ("txbreak", class_breakpoint, 
5288                               tbreak_at_finish_command,
5289 "Set temporary breakpoint at procedure exit.  Either there should\n\
5290 be no argument or the argument must be a depth.\n"), NULL);
5291   set_cmd_completer (c, location_completer);
5292   
5293   if (xdb_commands)
5294     deprecate_cmd (add_com ("bx", class_breakpoint, 
5295                             break_at_finish_at_depth_command,
5296 "Set breakpoint at procedure exit.  Either there should\n\
5297 be no argument or the argument must be a depth.\n"), NULL);
5298 }
5299