* exec.c: #include "arch-utils.h"
[deliverable/binutils-gdb.git] / gdb / hppa-hpux-tdep.c
CommitLineData
b1acf338 1/* Target-dependent code for HP-UX on PA-RISC.
ef6e7e13 2
9b254dd1
DJ
3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
273f8429 5
b1acf338 6 This file is part of GDB.
273f8429 7
b1acf338
MK
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
b1acf338 11 (at your option) any later version.
273f8429 12
b1acf338
MK
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
273f8429 17
b1acf338 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
273f8429
JB
20
21#include "defs.h"
22#include "arch-utils.h"
60e1ff27 23#include "gdbcore.h"
273f8429 24#include "osabi.h"
222e5d1d 25#include "frame.h"
43613416
RC
26#include "frame-unwind.h"
27#include "trad-frame.h"
4c02c60c
AC
28#include "symtab.h"
29#include "objfiles.h"
30#include "inferior.h"
31#include "infcall.h"
90f943f1 32#include "observer.h"
acf86d54
RC
33#include "hppa-tdep.h"
34#include "solib-som.h"
35#include "solib-pa64.h"
08d53055 36#include "regset.h"
e7b17823 37#include "regcache.h"
60250e8b 38#include "exceptions.h"
08d53055
MK
39
40#include "gdb_string.h"
4c02c60c 41
77d18ded
RC
42#define IS_32BIT_TARGET(_gdbarch) \
43 ((gdbarch_tdep (_gdbarch))->bytes_per_address == 4)
44
27b08a0c
RC
45/* Bit in the `ss_flag' member of `struct save_state' that indicates
46 that the 64-bit register values are live. From
47 <machine/save_state.h>. */
48#define HPPA_HPUX_SS_WIDEREGS 0x40
49
50/* Offsets of various parts of `struct save_state'. From
51 <machine/save_state.h>. */
52#define HPPA_HPUX_SS_FLAGS_OFFSET 0
53#define HPPA_HPUX_SS_NARROW_OFFSET 4
54#define HPPA_HPUX_SS_FPBLOCK_OFFSET 256
55#define HPPA_HPUX_SS_WIDE_OFFSET 640
56
57/* The size of `struct save_state. */
58#define HPPA_HPUX_SAVE_STATE_SIZE 1152
59
60/* The size of `struct pa89_save_state', which corresponds to PA-RISC
61 1.1, the lowest common denominator that we support. */
62#define HPPA_HPUX_PA89_SAVE_STATE_SIZE 512
63
64
273f8429
JB
65/* Forward declarations. */
66extern void _initialize_hppa_hpux_tdep (void);
67extern initialize_file_ftype _initialize_hppa_hpux_tdep;
68
77d18ded
RC
69static int
70in_opd_section (CORE_ADDR pc)
71{
72 struct obj_section *s;
73 int retval = 0;
74
75 s = find_pc_section (pc);
76
77 retval = (s != NULL
78 && s->the_bfd_section->name != NULL
79 && strcmp (s->the_bfd_section->name, ".opd") == 0);
80 return (retval);
81}
82
abc485a1
RC
83/* Return one if PC is in the call path of a trampoline, else return zero.
84
85 Note we return one for *any* call trampoline (long-call, arg-reloc), not
86 just shared library trampolines (import, export). */
87
88static int
89hppa32_hpux_in_solib_call_trampoline (CORE_ADDR pc, char *name)
90{
91 struct minimal_symbol *minsym;
92 struct unwind_table_entry *u;
abc485a1
RC
93
94 /* First see if PC is in one of the two C-library trampolines. */
3388d7ff
RC
95 if (pc == hppa_symbol_address("$$dyncall")
96 || pc == hppa_symbol_address("_sr4export"))
abc485a1
RC
97 return 1;
98
99 minsym = lookup_minimal_symbol_by_pc (pc);
100 if (minsym && strcmp (DEPRECATED_SYMBOL_NAME (minsym), ".stub") == 0)
101 return 1;
102
103 /* Get the unwind descriptor corresponding to PC, return zero
104 if no unwind was found. */
105 u = find_unwind_entry (pc);
106 if (!u)
107 return 0;
108
109 /* If this isn't a linker stub, then return now. */
110 if (u->stub_unwind.stub_type == 0)
111 return 0;
112
113 /* By definition a long-branch stub is a call stub. */
114 if (u->stub_unwind.stub_type == LONG_BRANCH)
115 return 1;
116
117 /* The call and return path execute the same instructions within
118 an IMPORT stub! So an IMPORT stub is both a call and return
119 trampoline. */
120 if (u->stub_unwind.stub_type == IMPORT)
121 return 1;
122
123 /* Parameter relocation stubs always have a call path and may have a
124 return path. */
125 if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
126 || u->stub_unwind.stub_type == EXPORT)
127 {
128 CORE_ADDR addr;
129
130 /* Search forward from the current PC until we hit a branch
131 or the end of the stub. */
132 for (addr = pc; addr <= u->region_end; addr += 4)
133 {
134 unsigned long insn;
135
136 insn = read_memory_integer (addr, 4);
137
138 /* Does it look like a bl? If so then it's the call path, if
139 we find a bv or be first, then we're on the return path. */
140 if ((insn & 0xfc00e000) == 0xe8000000)
141 return 1;
142 else if ((insn & 0xfc00e001) == 0xe800c000
143 || (insn & 0xfc000000) == 0xe0000000)
144 return 0;
145 }
146
147 /* Should never happen. */
8a3fe4f8 148 warning (_("Unable to find branch in parameter relocation stub."));
abc485a1
RC
149 return 0;
150 }
151
152 /* Unknown stub type. For now, just return zero. */
153 return 0;
154}
155
156static int
157hppa64_hpux_in_solib_call_trampoline (CORE_ADDR pc, char *name)
158{
159 /* PA64 has a completely different stub/trampoline scheme. Is it
160 better? Maybe. It's certainly harder to determine with any
161 certainty that we are in a stub because we can not refer to the
162 unwinders to help.
163
164 The heuristic is simple. Try to lookup the current PC value in th
165 minimal symbol table. If that fails, then assume we are not in a
166 stub and return.
167
168 Then see if the PC value falls within the section bounds for the
169 section containing the minimal symbol we found in the first
170 step. If it does, then assume we are not in a stub and return.
171
172 Finally peek at the instructions to see if they look like a stub. */
173 struct minimal_symbol *minsym;
174 asection *sec;
175 CORE_ADDR addr;
176 int insn, i;
177
178 minsym = lookup_minimal_symbol_by_pc (pc);
179 if (! minsym)
180 return 0;
181
182 sec = SYMBOL_BFD_SECTION (minsym);
183
184 if (bfd_get_section_vma (sec->owner, sec) <= pc
185 && pc < (bfd_get_section_vma (sec->owner, sec)
186 + bfd_section_size (sec->owner, sec)))
187 return 0;
188
189 /* We might be in a stub. Peek at the instructions. Stubs are 3
190 instructions long. */
191 insn = read_memory_integer (pc, 4);
192
193 /* Find out where we think we are within the stub. */
194 if ((insn & 0xffffc00e) == 0x53610000)
195 addr = pc;
196 else if ((insn & 0xffffffff) == 0xe820d000)
197 addr = pc - 4;
198 else if ((insn & 0xffffc00e) == 0x537b0000)
199 addr = pc - 8;
200 else
201 return 0;
202
203 /* Now verify each insn in the range looks like a stub instruction. */
204 insn = read_memory_integer (addr, 4);
205 if ((insn & 0xffffc00e) != 0x53610000)
206 return 0;
207
208 /* Now verify each insn in the range looks like a stub instruction. */
209 insn = read_memory_integer (addr + 4, 4);
210 if ((insn & 0xffffffff) != 0xe820d000)
211 return 0;
212
213 /* Now verify each insn in the range looks like a stub instruction. */
214 insn = read_memory_integer (addr + 8, 4);
215 if ((insn & 0xffffc00e) != 0x537b0000)
216 return 0;
217
218 /* Looks like a stub. */
219 return 1;
220}
221
222/* Return one if PC is in the return path of a trampoline, else return zero.
223
224 Note we return one for *any* call trampoline (long-call, arg-reloc), not
225 just shared library trampolines (import, export). */
226
227static int
228hppa_hpux_in_solib_return_trampoline (CORE_ADDR pc, char *name)
229{
230 struct unwind_table_entry *u;
231
232 /* Get the unwind descriptor corresponding to PC, return zero
233 if no unwind was found. */
234 u = find_unwind_entry (pc);
235 if (!u)
236 return 0;
237
238 /* If this isn't a linker stub or it's just a long branch stub, then
239 return zero. */
240 if (u->stub_unwind.stub_type == 0 || u->stub_unwind.stub_type == LONG_BRANCH)
241 return 0;
242
243 /* The call and return path execute the same instructions within
244 an IMPORT stub! So an IMPORT stub is both a call and return
245 trampoline. */
246 if (u->stub_unwind.stub_type == IMPORT)
247 return 1;
248
249 /* Parameter relocation stubs always have a call path and may have a
250 return path. */
251 if (u->stub_unwind.stub_type == PARAMETER_RELOCATION
252 || u->stub_unwind.stub_type == EXPORT)
253 {
254 CORE_ADDR addr;
255
256 /* Search forward from the current PC until we hit a branch
257 or the end of the stub. */
258 for (addr = pc; addr <= u->region_end; addr += 4)
259 {
260 unsigned long insn;
261
262 insn = read_memory_integer (addr, 4);
263
264 /* Does it look like a bl? If so then it's the call path, if
265 we find a bv or be first, then we're on the return path. */
266 if ((insn & 0xfc00e000) == 0xe8000000)
267 return 0;
268 else if ((insn & 0xfc00e001) == 0xe800c000
269 || (insn & 0xfc000000) == 0xe0000000)
270 return 1;
271 }
272
273 /* Should never happen. */
8a3fe4f8 274 warning (_("Unable to find branch in parameter relocation stub."));
abc485a1
RC
275 return 0;
276 }
277
278 /* Unknown stub type. For now, just return zero. */
279 return 0;
280
281}
282
283/* Figure out if PC is in a trampoline, and if so find out where
284 the trampoline will jump to. If not in a trampoline, return zero.
285
286 Simple code examination probably is not a good idea since the code
287 sequences in trampolines can also appear in user code.
288
289 We use unwinds and information from the minimal symbol table to
290 determine when we're in a trampoline. This won't work for ELF
291 (yet) since it doesn't create stub unwind entries. Whether or
292 not ELF will create stub unwinds or normal unwinds for linker
293 stubs is still being debated.
294
295 This should handle simple calls through dyncall or sr4export,
296 long calls, argument relocation stubs, and dyncall/sr4export
297 calling an argument relocation stub. It even handles some stubs
298 used in dynamic executables. */
299
300static CORE_ADDR
52f729a7 301hppa_hpux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
abc485a1 302{
464963c9 303 struct gdbarch *gdbarch = get_frame_arch (frame);
abc485a1
RC
304 long orig_pc = pc;
305 long prev_inst, curr_inst, loc;
abc485a1
RC
306 struct minimal_symbol *msym;
307 struct unwind_table_entry *u;
308
abc485a1
RC
309 /* Addresses passed to dyncall may *NOT* be the actual address
310 of the function. So we may have to do something special. */
3388d7ff 311 if (pc == hppa_symbol_address("$$dyncall"))
abc485a1 312 {
52f729a7 313 pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
abc485a1
RC
314
315 /* If bit 30 (counting from the left) is on, then pc is the address of
316 the PLT entry for this function, not the address of the function
317 itself. Bit 31 has meaning too, but only for MPE. */
318 if (pc & 0x2)
819844ad 319 pc = (CORE_ADDR) read_memory_integer
464963c9 320 (pc & ~0x3, gdbarch_ptr_bit (gdbarch) / 8);
abc485a1 321 }
3388d7ff 322 if (pc == hppa_symbol_address("$$dyncall_external"))
abc485a1 323 {
52f729a7 324 pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
819844ad 325 pc = (CORE_ADDR) read_memory_integer
464963c9 326 (pc & ~0x3, gdbarch_ptr_bit (gdbarch) / 8);
abc485a1 327 }
3388d7ff 328 else if (pc == hppa_symbol_address("_sr4export"))
52f729a7 329 pc = (CORE_ADDR) get_frame_register_unsigned (frame, 22);
abc485a1
RC
330
331 /* Get the unwind descriptor corresponding to PC, return zero
332 if no unwind was found. */
333 u = find_unwind_entry (pc);
334 if (!u)
335 return 0;
336
337 /* If this isn't a linker stub, then return now. */
338 /* elz: attention here! (FIXME) because of a compiler/linker
339 error, some stubs which should have a non zero stub_unwind.stub_type
340 have unfortunately a value of zero. So this function would return here
341 as if we were not in a trampoline. To fix this, we go look at the partial
342 symbol information, which reports this guy as a stub.
343 (FIXME): Unfortunately, we are not that lucky: it turns out that the
344 partial symbol information is also wrong sometimes. This is because
345 when it is entered (somread.c::som_symtab_read()) it can happen that
346 if the type of the symbol (from the som) is Entry, and the symbol is
347 in a shared library, then it can also be a trampoline. This would
348 be OK, except that I believe the way they decide if we are ina shared library
349 does not work. SOOOO..., even if we have a regular function w/o trampolines
350 its minimal symbol can be assigned type mst_solib_trampoline.
351 Also, if we find that the symbol is a real stub, then we fix the unwind
352 descriptor, and define the stub type to be EXPORT.
353 Hopefully this is correct most of the times. */
354 if (u->stub_unwind.stub_type == 0)
355 {
356
357/* elz: NOTE (FIXME!) once the problem with the unwind information is fixed
358 we can delete all the code which appears between the lines */
359/*--------------------------------------------------------------------------*/
360 msym = lookup_minimal_symbol_by_pc (pc);
361
362 if (msym == NULL || MSYMBOL_TYPE (msym) != mst_solib_trampoline)
363 return orig_pc == pc ? 0 : pc & ~0x3;
364
365 else if (msym != NULL && MSYMBOL_TYPE (msym) == mst_solib_trampoline)
366 {
367 struct objfile *objfile;
368 struct minimal_symbol *msymbol;
369 int function_found = 0;
370
371 /* go look if there is another minimal symbol with the same name as
372 this one, but with type mst_text. This would happen if the msym
373 is an actual trampoline, in which case there would be another
374 symbol with the same name corresponding to the real function */
375
376 ALL_MSYMBOLS (objfile, msymbol)
377 {
378 if (MSYMBOL_TYPE (msymbol) == mst_text
379 && DEPRECATED_STREQ (DEPRECATED_SYMBOL_NAME (msymbol), DEPRECATED_SYMBOL_NAME (msym)))
380 {
381 function_found = 1;
382 break;
383 }
384 }
385
386 if (function_found)
387 /* the type of msym is correct (mst_solib_trampoline), but
388 the unwind info is wrong, so set it to the correct value */
389 u->stub_unwind.stub_type = EXPORT;
390 else
391 /* the stub type info in the unwind is correct (this is not a
392 trampoline), but the msym type information is wrong, it
393 should be mst_text. So we need to fix the msym, and also
394 get out of this function */
395 {
396 MSYMBOL_TYPE (msym) = mst_text;
397 return orig_pc == pc ? 0 : pc & ~0x3;
398 }
399 }
400
401/*--------------------------------------------------------------------------*/
402 }
403
404 /* It's a stub. Search for a branch and figure out where it goes.
405 Note we have to handle multi insn branch sequences like ldil;ble.
406 Most (all?) other branches can be determined by examining the contents
407 of certain registers and the stack. */
408
409 loc = pc;
410 curr_inst = 0;
411 prev_inst = 0;
412 while (1)
413 {
414 /* Make sure we haven't walked outside the range of this stub. */
415 if (u != find_unwind_entry (loc))
416 {
8a3fe4f8 417 warning (_("Unable to find branch in linker stub"));
abc485a1
RC
418 return orig_pc == pc ? 0 : pc & ~0x3;
419 }
420
421 prev_inst = curr_inst;
422 curr_inst = read_memory_integer (loc, 4);
423
424 /* Does it look like a branch external using %r1? Then it's the
425 branch from the stub to the actual function. */
426 if ((curr_inst & 0xffe0e000) == 0xe0202000)
427 {
428 /* Yup. See if the previous instruction loaded
429 a value into %r1. If so compute and return the jump address. */
430 if ((prev_inst & 0xffe00000) == 0x20200000)
431 return (hppa_extract_21 (prev_inst) + hppa_extract_17 (curr_inst)) & ~0x3;
432 else
433 {
8a3fe4f8 434 warning (_("Unable to find ldil X,%%r1 before ble Y(%%sr4,%%r1)."));
abc485a1
RC
435 return orig_pc == pc ? 0 : pc & ~0x3;
436 }
437 }
438
439 /* Does it look like a be 0(sr0,%r21)? OR
440 Does it look like a be, n 0(sr0,%r21)? OR
441 Does it look like a bve (r21)? (this is on PA2.0)
442 Does it look like a bve, n(r21)? (this is also on PA2.0)
443 That's the branch from an
444 import stub to an export stub.
445
446 It is impossible to determine the target of the branch via
447 simple examination of instructions and/or data (consider
448 that the address in the plabel may be the address of the
449 bind-on-reference routine in the dynamic loader).
450
451 So we have try an alternative approach.
452
453 Get the name of the symbol at our current location; it should
454 be a stub symbol with the same name as the symbol in the
455 shared library.
456
457 Then lookup a minimal symbol with the same name; we should
458 get the minimal symbol for the target routine in the shared
459 library as those take precedence of import/export stubs. */
460 if ((curr_inst == 0xe2a00000) ||
461 (curr_inst == 0xe2a00002) ||
462 (curr_inst == 0xeaa0d000) ||
463 (curr_inst == 0xeaa0d002))
464 {
465 struct minimal_symbol *stubsym, *libsym;
466
467 stubsym = lookup_minimal_symbol_by_pc (loc);
468 if (stubsym == NULL)
469 {
8a3fe4f8 470 warning (_("Unable to find symbol for 0x%lx"), loc);
abc485a1
RC
471 return orig_pc == pc ? 0 : pc & ~0x3;
472 }
473
474 libsym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (stubsym), NULL, NULL);
475 if (libsym == NULL)
476 {
8a3fe4f8 477 warning (_("Unable to find library symbol for %s."),
abc485a1
RC
478 DEPRECATED_SYMBOL_NAME (stubsym));
479 return orig_pc == pc ? 0 : pc & ~0x3;
480 }
481
482 return SYMBOL_VALUE (libsym);
483 }
484
485 /* Does it look like bl X,%rp or bl X,%r0? Another way to do a
486 branch from the stub to the actual function. */
487 /*elz */
488 else if ((curr_inst & 0xffe0e000) == 0xe8400000
489 || (curr_inst & 0xffe0e000) == 0xe8000000
490 || (curr_inst & 0xffe0e000) == 0xe800A000)
491 return (loc + hppa_extract_17 (curr_inst) + 8) & ~0x3;
492
493 /* Does it look like bv (rp)? Note this depends on the
494 current stack pointer being the same as the stack
495 pointer in the stub itself! This is a branch on from the
496 stub back to the original caller. */
497 /*else if ((curr_inst & 0xffe0e000) == 0xe840c000) */
498 else if ((curr_inst & 0xffe0f000) == 0xe840c000)
499 {
500 /* Yup. See if the previous instruction loaded
501 rp from sp - 8. */
502 if (prev_inst == 0x4bc23ff1)
52f729a7
UW
503 {
504 CORE_ADDR sp;
505 sp = get_frame_register_unsigned (frame, HPPA_SP_REGNUM);
506 return read_memory_integer (sp - 8, 4) & ~0x3;
507 }
abc485a1
RC
508 else
509 {
8a3fe4f8 510 warning (_("Unable to find restore of %%rp before bv (%%rp)."));
abc485a1
RC
511 return orig_pc == pc ? 0 : pc & ~0x3;
512 }
513 }
514
515 /* elz: added this case to capture the new instruction
516 at the end of the return part of an export stub used by
517 the PA2.0: BVE, n (rp) */
518 else if ((curr_inst & 0xffe0f000) == 0xe840d000)
519 {
520 return (read_memory_integer
52f729a7 521 (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
464963c9 522 gdbarch_ptr_bit (gdbarch) / 8)) & ~0x3;
abc485a1
RC
523 }
524
525 /* What about be,n 0(sr0,%rp)? It's just another way we return to
526 the original caller from the stub. Used in dynamic executables. */
527 else if (curr_inst == 0xe0400002)
528 {
529 /* The value we jump to is sitting in sp - 24. But that's
530 loaded several instructions before the be instruction.
531 I guess we could check for the previous instruction being
532 mtsp %r1,%sr0 if we want to do sanity checking. */
533 return (read_memory_integer
52f729a7 534 (get_frame_register_unsigned (frame, HPPA_SP_REGNUM) - 24,
464963c9 535 gdbarch_ptr_bit (gdbarch) / 8)) & ~0x3;
abc485a1
RC
536 }
537
538 /* Haven't found the branch yet, but we're still in the stub.
539 Keep looking. */
540 loc += 4;
541 }
542}
543
6d350bb5
UW
544static void
545hppa_skip_permanent_breakpoint (struct regcache *regcache)
5aac166f
RC
546{
547 /* To step over a breakpoint instruction on the PA takes some
548 fiddling with the instruction address queue.
549
550 When we stop at a breakpoint, the IA queue front (the instruction
551 we're executing now) points at the breakpoint instruction, and
552 the IA queue back (the next instruction to execute) points to
553 whatever instruction we would execute after the breakpoint, if it
554 were an ordinary instruction. This is the case even if the
555 breakpoint is in the delay slot of a branch instruction.
556
557 Clearly, to step past the breakpoint, we need to set the queue
558 front to the back. But what do we put in the back? What
559 instruction comes after that one? Because of the branch delay
560 slot, the next insn is always at the back + 4. */
5aac166f 561
6d350bb5
UW
562 ULONGEST pcoq_tail, pcsq_tail;
563 regcache_cooked_read_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, &pcoq_tail);
564 regcache_cooked_read_unsigned (regcache, HPPA_PCSQ_TAIL_REGNUM, &pcsq_tail);
565
566 regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_HEAD_REGNUM, pcoq_tail);
567 regcache_cooked_write_unsigned (regcache, HPPA_PCSQ_HEAD_REGNUM, pcsq_tail);
568
569 regcache_cooked_write_unsigned (regcache, HPPA_PCOQ_TAIL_REGNUM, pcoq_tail + 4);
5aac166f
RC
570 /* We can leave the tail's space the same, since there's no jump. */
571}
abc485a1 572
4c02c60c 573
43613416
RC
574/* Signal frames. */
575struct hppa_hpux_sigtramp_unwind_cache
576{
577 CORE_ADDR base;
578 struct trad_frame_saved_reg *saved_regs;
579};
580
581static int hppa_hpux_tramp_reg[] = {
582 HPPA_SAR_REGNUM,
583 HPPA_PCOQ_HEAD_REGNUM,
584 HPPA_PCSQ_HEAD_REGNUM,
585 HPPA_PCOQ_TAIL_REGNUM,
586 HPPA_PCSQ_TAIL_REGNUM,
587 HPPA_EIEM_REGNUM,
588 HPPA_IIR_REGNUM,
589 HPPA_ISR_REGNUM,
590 HPPA_IOR_REGNUM,
591 HPPA_IPSW_REGNUM,
592 -1,
593 HPPA_SR4_REGNUM,
594 HPPA_SR4_REGNUM + 1,
595 HPPA_SR4_REGNUM + 2,
596 HPPA_SR4_REGNUM + 3,
597 HPPA_SR4_REGNUM + 4,
598 HPPA_SR4_REGNUM + 5,
599 HPPA_SR4_REGNUM + 6,
600 HPPA_SR4_REGNUM + 7,
601 HPPA_RCR_REGNUM,
602 HPPA_PID0_REGNUM,
603 HPPA_PID1_REGNUM,
604 HPPA_CCR_REGNUM,
605 HPPA_PID2_REGNUM,
606 HPPA_PID3_REGNUM,
607 HPPA_TR0_REGNUM,
608 HPPA_TR0_REGNUM + 1,
609 HPPA_TR0_REGNUM + 2,
610 HPPA_CR27_REGNUM
611};
612
613static struct hppa_hpux_sigtramp_unwind_cache *
614hppa_hpux_sigtramp_frame_unwind_cache (struct frame_info *next_frame,
615 void **this_cache)
616
617{
618 struct gdbarch *gdbarch = get_frame_arch (next_frame);
619 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
620 struct hppa_hpux_sigtramp_unwind_cache *info;
621 unsigned int flag;
27b08a0c
RC
622 CORE_ADDR sp, scptr, off;
623 int i, incr, szoff;
43613416
RC
624
625 if (*this_cache)
626 return *this_cache;
627
628 info = FRAME_OBSTACK_ZALLOC (struct hppa_hpux_sigtramp_unwind_cache);
629 *this_cache = info;
630 info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
631
632 sp = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
633
27b08a0c
RC
634 if (IS_32BIT_TARGET (gdbarch))
635 scptr = sp - 1352;
636 else
637 scptr = sp - 1520;
638
43613416
RC
639 off = scptr;
640
641 /* See /usr/include/machine/save_state.h for the structure of the save_state_t
642 structure. */
643
27b08a0c
RC
644 flag = read_memory_unsigned_integer(scptr + HPPA_HPUX_SS_FLAGS_OFFSET, 4);
645
646 if (!(flag & HPPA_HPUX_SS_WIDEREGS))
43613416
RC
647 {
648 /* Narrow registers. */
27b08a0c 649 off = scptr + HPPA_HPUX_SS_NARROW_OFFSET;
43613416
RC
650 incr = 4;
651 szoff = 0;
652 }
653 else
654 {
655 /* Wide registers. */
27b08a0c 656 off = scptr + HPPA_HPUX_SS_WIDE_OFFSET + 8;
43613416
RC
657 incr = 8;
658 szoff = (tdep->bytes_per_address == 4 ? 4 : 0);
659 }
660
661 for (i = 1; i < 32; i++)
662 {
663 info->saved_regs[HPPA_R0_REGNUM + i].addr = off + szoff;
664 off += incr;
665 }
666
01926a69 667 for (i = 0; i < ARRAY_SIZE (hppa_hpux_tramp_reg); i++)
43613416
RC
668 {
669 if (hppa_hpux_tramp_reg[i] > 0)
670 info->saved_regs[hppa_hpux_tramp_reg[i]].addr = off + szoff;
27b08a0c 671
43613416
RC
672 off += incr;
673 }
674
675 /* TODO: fp regs */
676
677 info->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
678
679 return info;
680}
681
682static void
683hppa_hpux_sigtramp_frame_this_id (struct frame_info *next_frame,
684 void **this_prologue_cache,
685 struct frame_id *this_id)
686{
687 struct hppa_hpux_sigtramp_unwind_cache *info
688 = hppa_hpux_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
689 *this_id = frame_id_build (info->base, frame_pc_unwind (next_frame));
690}
691
692static void
693hppa_hpux_sigtramp_frame_prev_register (struct frame_info *next_frame,
a7aad9aa
MK
694 void **this_prologue_cache,
695 int regnum, int *optimizedp,
696 enum lval_type *lvalp,
697 CORE_ADDR *addrp,
698 int *realnump, gdb_byte *valuep)
43613416
RC
699{
700 struct hppa_hpux_sigtramp_unwind_cache *info
701 = hppa_hpux_sigtramp_frame_unwind_cache (next_frame, this_prologue_cache);
702 hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
703 optimizedp, lvalp, addrp, realnump, valuep);
704}
705
706static const struct frame_unwind hppa_hpux_sigtramp_frame_unwind = {
707 SIGTRAMP_FRAME,
708 hppa_hpux_sigtramp_frame_this_id,
709 hppa_hpux_sigtramp_frame_prev_register
710};
711
712static const struct frame_unwind *
713hppa_hpux_sigtramp_unwind_sniffer (struct frame_info *next_frame)
714{
765697c9 715 struct unwind_table_entry *u;
43613416 716 CORE_ADDR pc = frame_pc_unwind (next_frame);
43613416 717
765697c9 718 u = find_unwind_entry (pc);
43613416 719
a717134b
MK
720 /* If this is an export stub, try to get the unwind descriptor for
721 the actual function itself. */
722 if (u && u->stub_unwind.stub_type == EXPORT)
723 {
724 gdb_byte buf[HPPA_INSN_SIZE];
725 unsigned long insn;
726
727 if (!safe_frame_unwind_memory (next_frame, u->region_start,
728 buf, sizeof buf))
729 return NULL;
730
731 insn = extract_unsigned_integer (buf, sizeof buf);
732 if ((insn & 0xffe0e000) == 0xe8400000)
733 u = find_unwind_entry(u->region_start + hppa_extract_17 (insn) + 8);
734 }
735
765697c9 736 if (u && u->HP_UX_interrupt_marker)
43613416
RC
737 return &hppa_hpux_sigtramp_frame_unwind;
738
739 return NULL;
740}
741
c268433a 742static CORE_ADDR
77d18ded 743hppa32_hpux_find_global_pointer (struct value *function)
c268433a
RC
744{
745 CORE_ADDR faddr;
746
747 faddr = value_as_address (function);
748
749 /* Is this a plabel? If so, dereference it to get the gp value. */
750 if (faddr & 2)
751 {
752 int status;
753 char buf[4];
754
755 faddr &= ~3;
756
757 status = target_read_memory (faddr + 4, buf, sizeof (buf));
758 if (status == 0)
759 return extract_unsigned_integer (buf, sizeof (buf));
760 }
761
61aff869 762 return gdbarch_tdep (current_gdbarch)->solib_get_got_by_pc (faddr);
c268433a
RC
763}
764
765static CORE_ADDR
77d18ded 766hppa64_hpux_find_global_pointer (struct value *function)
c268433a 767{
77d18ded
RC
768 CORE_ADDR faddr;
769 char buf[32];
770
771 faddr = value_as_address (function);
772
773 if (in_opd_section (faddr))
774 {
775 target_read_memory (faddr, buf, sizeof (buf));
776 return extract_unsigned_integer (&buf[24], 8);
777 }
778 else
c268433a 779 {
77d18ded
RC
780 return gdbarch_tdep (current_gdbarch)->solib_get_got_by_pc (faddr);
781 }
782}
783
784static unsigned int ldsid_pattern[] = {
785 0x000010a0, /* ldsid (rX),rY */
786 0x00001820, /* mtsp rY,sr0 */
787 0xe0000000 /* be,n (sr0,rX) */
788};
789
790static CORE_ADDR
791hppa_hpux_search_pattern (CORE_ADDR start, CORE_ADDR end,
792 unsigned int *patterns, int count)
793{
d275c051
MK
794 int num_insns = (end - start + HPPA_INSN_SIZE) / HPPA_INSN_SIZE;
795 unsigned int *insns;
796 gdb_byte *buf;
77d18ded 797 int offset, i;
77d18ded 798
d275c051
MK
799 buf = alloca (num_insns * HPPA_INSN_SIZE);
800 insns = alloca (num_insns * sizeof (unsigned int));
c268433a 801
d275c051
MK
802 read_memory (start, buf, num_insns * HPPA_INSN_SIZE);
803 for (i = 0; i < num_insns; i++, buf += HPPA_INSN_SIZE)
804 insns[i] = extract_unsigned_integer (buf, HPPA_INSN_SIZE);
c268433a 805
d275c051 806 for (offset = 0; offset <= num_insns - count; offset++)
77d18ded
RC
807 {
808 for (i = 0; i < count; i++)
c268433a 809 {
d275c051 810 if ((insns[offset + i] & patterns[i]) != patterns[i])
77d18ded
RC
811 break;
812 }
813 if (i == count)
814 break;
815 }
d275c051
MK
816
817 if (offset <= num_insns - count)
818 return start + offset * HPPA_INSN_SIZE;
77d18ded
RC
819 else
820 return 0;
821}
c268433a 822
77d18ded
RC
823static CORE_ADDR
824hppa32_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
825 int *argreg)
826{
827 struct objfile *obj;
828 struct obj_section *sec;
829 struct hppa_objfile_private *priv;
830 struct frame_info *frame;
831 struct unwind_table_entry *u;
832 CORE_ADDR addr, rp;
833 char buf[4];
834 unsigned int insn;
835
836 sec = find_pc_section (pc);
837 obj = sec->objfile;
838 priv = objfile_data (obj, hppa_objfile_priv_data);
839
840 if (!priv)
841 priv = hppa_init_objfile_priv_data (obj);
842 if (!priv)
8a3fe4f8 843 error (_("Internal error creating objfile private data."));
77d18ded
RC
844
845 /* Use the cached value if we have one. */
846 if (priv->dummy_call_sequence_addr != 0)
847 {
848 *argreg = priv->dummy_call_sequence_reg;
849 return priv->dummy_call_sequence_addr;
850 }
c268433a 851
77d18ded
RC
852 /* First try a heuristic; if we are in a shared library call, our return
853 pointer is likely to point at an export stub. */
854 frame = get_current_frame ();
855 rp = frame_unwind_register_unsigned (frame, 2);
856 u = find_unwind_entry (rp);
857 if (u && u->stub_unwind.stub_type == EXPORT)
858 {
859 addr = hppa_hpux_search_pattern (u->region_start, u->region_end,
860 ldsid_pattern,
861 ARRAY_SIZE (ldsid_pattern));
862 if (addr)
863 goto found_pattern;
864 }
c268433a 865
77d18ded
RC
866 /* Next thing to try is to look for an export stub. */
867 if (priv->unwind_info)
868 {
869 int i;
c268433a 870
77d18ded
RC
871 for (i = 0; i < priv->unwind_info->last; i++)
872 {
873 struct unwind_table_entry *u;
874 u = &priv->unwind_info->table[i];
875 if (u->stub_unwind.stub_type == EXPORT)
876 {
877 addr = hppa_hpux_search_pattern (u->region_start, u->region_end,
878 ldsid_pattern,
879 ARRAY_SIZE (ldsid_pattern));
880 if (addr)
881 {
882 goto found_pattern;
883 }
c268433a
RC
884 }
885 }
77d18ded 886 }
c268433a 887
77d18ded
RC
888 /* Finally, if this is the main executable, try to locate a sequence
889 from noshlibs */
890 addr = hppa_symbol_address ("noshlibs");
891 sec = find_pc_section (addr);
892
893 if (sec && sec->objfile == obj)
894 {
895 CORE_ADDR start, end;
896
897 find_pc_partial_function (addr, NULL, &start, &end);
898 if (start != 0 && end != 0)
c268433a 899 {
77d18ded
RC
900 addr = hppa_hpux_search_pattern (start, end, ldsid_pattern,
901 ARRAY_SIZE (ldsid_pattern));
902 if (addr)
903 goto found_pattern;
c268433a 904 }
77d18ded
RC
905 }
906
907 /* Can't find a suitable sequence. */
908 return 0;
909
910found_pattern:
911 target_read_memory (addr, buf, sizeof (buf));
912 insn = extract_unsigned_integer (buf, sizeof (buf));
913 priv->dummy_call_sequence_addr = addr;
914 priv->dummy_call_sequence_reg = (insn >> 21) & 0x1f;
915
916 *argreg = priv->dummy_call_sequence_reg;
917 return priv->dummy_call_sequence_addr;
918}
919
920static CORE_ADDR
921hppa64_hpux_search_dummy_call_sequence (struct gdbarch *gdbarch, CORE_ADDR pc,
922 int *argreg)
923{
924 struct objfile *obj;
925 struct obj_section *sec;
926 struct hppa_objfile_private *priv;
927 CORE_ADDR addr;
928 struct minimal_symbol *msym;
929 int i;
930
931 sec = find_pc_section (pc);
932 obj = sec->objfile;
933 priv = objfile_data (obj, hppa_objfile_priv_data);
934
935 if (!priv)
936 priv = hppa_init_objfile_priv_data (obj);
937 if (!priv)
8a3fe4f8 938 error (_("Internal error creating objfile private data."));
77d18ded
RC
939
940 /* Use the cached value if we have one. */
941 if (priv->dummy_call_sequence_addr != 0)
942 {
943 *argreg = priv->dummy_call_sequence_reg;
944 return priv->dummy_call_sequence_addr;
945 }
946
947 /* FIXME: Without stub unwind information, locating a suitable sequence is
948 fairly difficult. For now, we implement a very naive and inefficient
949 scheme; try to read in blocks of code, and look for a "bve,n (rp)"
950 instruction. These are likely to occur at the end of functions, so
951 we only look at the last two instructions of each function. */
952 for (i = 0, msym = obj->msymbols; i < obj->minimal_symbol_count; i++, msym++)
953 {
954 CORE_ADDR begin, end;
955 char *name;
d275c051 956 gdb_byte buf[2 * HPPA_INSN_SIZE];
77d18ded
RC
957 int offset;
958
959 find_pc_partial_function (SYMBOL_VALUE_ADDRESS (msym), &name,
960 &begin, &end);
961
81092a3e 962 if (name == NULL || begin == 0 || end == 0)
77d18ded
RC
963 continue;
964
d275c051 965 if (target_read_memory (end - sizeof (buf), buf, sizeof (buf)) == 0)
c268433a 966 {
d275c051 967 for (offset = 0; offset < sizeof (buf); offset++)
77d18ded
RC
968 {
969 unsigned int insn;
970
d275c051 971 insn = extract_unsigned_integer (buf + offset, HPPA_INSN_SIZE);
77d18ded
RC
972 if (insn == 0xe840d002) /* bve,n (rp) */
973 {
d275c051 974 addr = (end - sizeof (buf)) + offset;
77d18ded
RC
975 goto found_pattern;
976 }
977 }
978 }
979 }
980
981 /* Can't find a suitable sequence. */
982 return 0;
983
984found_pattern:
985 priv->dummy_call_sequence_addr = addr;
986 /* Right now we only look for a "bve,l (rp)" sequence, so the register is
987 always HPPA_RP_REGNUM. */
988 priv->dummy_call_sequence_reg = HPPA_RP_REGNUM;
989
990 *argreg = priv->dummy_call_sequence_reg;
991 return priv->dummy_call_sequence_addr;
992}
993
994static CORE_ADDR
995hppa_hpux_find_import_stub_for_addr (CORE_ADDR funcaddr)
996{
997 struct objfile *objfile;
998 struct minimal_symbol *funsym, *stubsym;
999 CORE_ADDR stubaddr;
1000
1001 funsym = lookup_minimal_symbol_by_pc (funcaddr);
1002 stubaddr = 0;
1003
1004 ALL_OBJFILES (objfile)
1005 {
1006 stubsym = lookup_minimal_symbol_solib_trampoline
1007 (SYMBOL_LINKAGE_NAME (funsym), objfile);
1008
1009 if (stubsym)
1010 {
1011 struct unwind_table_entry *u;
1012
1013 u = find_unwind_entry (SYMBOL_VALUE (stubsym));
1014 if (u == NULL
1015 || (u->stub_unwind.stub_type != IMPORT
1016 && u->stub_unwind.stub_type != IMPORT_SHLIB))
1017 continue;
1018
1019 stubaddr = SYMBOL_VALUE (stubsym);
1020
1021 /* If we found an IMPORT stub, then we can stop searching;
1022 if we found an IMPORT_SHLIB, we want to continue the search
1023 in the hopes that we will find an IMPORT stub. */
1024 if (u->stub_unwind.stub_type == IMPORT)
1025 break;
1026 }
1027 }
1028
1029 return stubaddr;
1030}
1031
1032static int
1033hppa_hpux_sr_for_addr (CORE_ADDR addr)
1034{
1035 int sr;
1036 /* The space register to use is encoded in the top 2 bits of the address. */
1037 sr = addr >> (gdbarch_tdep (current_gdbarch)->bytes_per_address * 8 - 2);
1038 return sr + 4;
1039}
1040
1041static CORE_ADDR
1042hppa_hpux_find_dummy_bpaddr (CORE_ADDR addr)
1043{
1044 /* In order for us to restore the space register to its starting state,
1045 we need the dummy trampoline to return to the an instruction address in
1046 the same space as where we started the call. We used to place the
1047 breakpoint near the current pc, however, this breaks nested dummy calls
1048 as the nested call will hit the breakpoint address and terminate
1049 prematurely. Instead, we try to look for an address in the same space to
1050 put the breakpoint.
1051
1052 This is similar in spirit to putting the breakpoint at the "entry point"
1053 of an executable. */
1054
1055 struct obj_section *sec;
1056 struct unwind_table_entry *u;
1057 struct minimal_symbol *msym;
1058 CORE_ADDR func;
1059 int i;
1060
1061 sec = find_pc_section (addr);
1062 if (sec)
1063 {
1064 /* First try the lowest address in the section; we can use it as long
1065 as it is "regular" code (i.e. not a stub) */
1066 u = find_unwind_entry (sec->addr);
1067 if (!u || u->stub_unwind.stub_type == 0)
1068 return sec->addr;
1069
1070 /* Otherwise, we need to find a symbol for a regular function. We
1071 do this by walking the list of msymbols in the objfile. The symbol
1072 we find should not be the same as the function that was passed in. */
1073
1074 /* FIXME: this is broken, because we can find a function that will be
1075 called by the dummy call target function, which will still not
1076 work. */
1077
1078 find_pc_partial_function (addr, NULL, &func, NULL);
1079 for (i = 0, msym = sec->objfile->msymbols;
1080 i < sec->objfile->minimal_symbol_count;
1081 i++, msym++)
1082 {
1083 u = find_unwind_entry (SYMBOL_VALUE_ADDRESS (msym));
1084 if (func != SYMBOL_VALUE_ADDRESS (msym)
1085 && (!u || u->stub_unwind.stub_type == 0))
1086 return SYMBOL_VALUE_ADDRESS (msym);
c268433a 1087 }
77d18ded 1088 }
c268433a 1089
8a3fe4f8
AC
1090 warning (_("Cannot find suitable address to place dummy breakpoint; nested "
1091 "calls may fail."));
77d18ded
RC
1092 return addr - 4;
1093}
1094
1095static CORE_ADDR
1096hppa_hpux_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
82585c72 1097 CORE_ADDR funcaddr,
77d18ded
RC
1098 struct value **args, int nargs,
1099 struct type *value_type,
e4fd649a
UW
1100 CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
1101 struct regcache *regcache)
77d18ded
RC
1102{
1103 CORE_ADDR pc, stubaddr;
9846e541 1104 int argreg = 0;
77d18ded
RC
1105
1106 pc = read_pc ();
1107
1108 /* Note: we don't want to pass a function descriptor here; push_dummy_call
1109 fills in the PIC register for us. */
1110 funcaddr = gdbarch_convert_from_func_ptr_addr (gdbarch, funcaddr, NULL);
1111
1112 /* The simple case is where we call a function in the same space that we are
1113 currently in; in that case we don't really need to do anything. */
1114 if (hppa_hpux_sr_for_addr (pc) == hppa_hpux_sr_for_addr (funcaddr))
1115 {
1116 /* Intraspace call. */
1117 *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
1118 *real_pc = funcaddr;
e4fd649a 1119 regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, *bp_addr);
77d18ded
RC
1120
1121 return sp;
1122 }
1123
1124 /* In order to make an interspace call, we need to go through a stub.
1125 gcc supplies an appropriate stub called "__gcc_plt_call", however, if
1126 an application is compiled with HP compilers then this stub is not
1127 available. We used to fallback to "__d_plt_call", however that stub
1128 is not entirely useful for us because it doesn't do an interspace
1129 return back to the caller. Also, on hppa64-hpux, there is no
1130 __gcc_plt_call available. In order to keep the code uniform, we
1131 instead don't use either of these stubs, but instead write our own
1132 onto the stack.
1133
1134 A problem arises since the stack is located in a different space than
1135 code, so in order to branch to a stack stub, we will need to do an
1136 interspace branch. Previous versions of gdb did this by modifying code
1137 at the current pc and doing single-stepping to set the pcsq. Since this
1138 is highly undesirable, we use a different scheme:
1139
1140 All we really need to do the branch to the stub is a short instruction
1141 sequence like this:
1142
1143 PA1.1:
1144 ldsid (rX),r1
1145 mtsp r1,sr0
1146 be,n (sr0,rX)
1147
1148 PA2.0:
1149 bve,n (sr0,rX)
1150
1151 Instead of writing these sequences ourselves, we can find it in
1152 the instruction stream that belongs to the current space. While this
1153 seems difficult at first, we are actually guaranteed to find the sequences
1154 in several places:
1155
1156 For 32-bit code:
1157 - in export stubs for shared libraries
1158 - in the "noshlibs" routine in the main module
1159
1160 For 64-bit code:
1161 - at the end of each "regular" function
1162
1163 We cache the address of these sequences in the objfile's private data
1164 since these operations can potentially be quite expensive.
1165
1166 So, what we do is:
1167 - write a stack trampoline
1168 - look for a suitable instruction sequence in the current space
1169 - point the sequence at the trampoline
1170 - set the return address of the trampoline to the current space
1171 (see hppa_hpux_find_dummy_call_bpaddr)
1172 - set the continuing address of the "dummy code" as the sequence.
1173
1174*/
1175
1176 if (IS_32BIT_TARGET (gdbarch))
1177 {
1178 static unsigned int hppa32_tramp[] = {
1179 0x0fdf1291, /* stw r31,-8(,sp) */
1180 0x02c010a1, /* ldsid (,r22),r1 */
1181 0x00011820, /* mtsp r1,sr0 */
1182 0xe6c00000, /* be,l 0(sr0,r22),%sr0,%r31 */
1183 0x081f0242, /* copy r31,rp */
1184 0x0fd11082, /* ldw -8(,sp),rp */
1185 0x004010a1, /* ldsid (,rp),r1 */
1186 0x00011820, /* mtsp r1,sr0 */
1187 0xe0400000, /* be 0(sr0,rp) */
1188 0x08000240 /* nop */
1189 };
1190
1191 /* for hppa32, we must call the function through a stub so that on
1192 return it can return to the space of our trampoline. */
1193 stubaddr = hppa_hpux_find_import_stub_for_addr (funcaddr);
1194 if (stubaddr == 0)
8a3fe4f8
AC
1195 error (_("Cannot call external function not referenced by application "
1196 "(no import stub).\n"));
e4fd649a 1197 regcache_cooked_write_unsigned (regcache, 22, stubaddr);
77d18ded
RC
1198
1199 write_memory (sp, (char *)&hppa32_tramp, sizeof (hppa32_tramp));
1200
1201 *bp_addr = hppa_hpux_find_dummy_bpaddr (pc);
e4fd649a 1202 regcache_cooked_write_unsigned (regcache, 31, *bp_addr);
c268433a 1203
77d18ded
RC
1204 *real_pc = hppa32_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
1205 if (*real_pc == 0)
8a3fe4f8 1206 error (_("Cannot make interspace call from here."));
77d18ded 1207
e4fd649a 1208 regcache_cooked_write_unsigned (regcache, argreg, sp);
77d18ded
RC
1209
1210 sp += sizeof (hppa32_tramp);
c268433a
RC
1211 }
1212 else
1213 {
77d18ded
RC
1214 static unsigned int hppa64_tramp[] = {
1215 0xeac0f000, /* bve,l (r22),%r2 */
1216 0x0fdf12d1, /* std r31,-8(,sp) */
1217 0x0fd110c2, /* ldd -8(,sp),rp */
1218 0xe840d002, /* bve,n (rp) */
1219 0x08000240 /* nop */
1220 };
1221
1222 /* for hppa64, we don't need to call through a stub; all functions
1223 return via a bve. */
e4fd649a 1224 regcache_cooked_write_unsigned (regcache, 22, funcaddr);
77d18ded
RC
1225 write_memory (sp, (char *)&hppa64_tramp, sizeof (hppa64_tramp));
1226
1227 *bp_addr = pc - 4;
e4fd649a 1228 regcache_cooked_write_unsigned (regcache, 31, *bp_addr);
c268433a 1229
77d18ded
RC
1230 *real_pc = hppa64_hpux_search_dummy_call_sequence (gdbarch, pc, &argreg);
1231 if (*real_pc == 0)
8a3fe4f8 1232 error (_("Cannot make interspace call from here."));
c268433a 1233
e4fd649a 1234 regcache_cooked_write_unsigned (regcache, argreg, sp);
c268433a 1235
77d18ded 1236 sp += sizeof (hppa64_tramp);
c268433a
RC
1237 }
1238
77d18ded 1239 sp = gdbarch_frame_align (gdbarch, sp);
c268433a
RC
1240
1241 return sp;
1242}
77d18ded 1243
cc72850f
MK
1244\f
1245
08d53055
MK
1246static void
1247hppa_hpux_supply_ss_narrow (struct regcache *regcache,
1248 int regnum, const char *save_state)
1249{
1250 const char *ss_narrow = save_state + HPPA_HPUX_SS_NARROW_OFFSET;
1251 int i, offset = 0;
1252
1253 for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
1254 {
1255 if (regnum == i || regnum == -1)
1256 regcache_raw_supply (regcache, i, ss_narrow + offset);
1257
1258 offset += 4;
1259 }
1260}
1261
1262static void
1263hppa_hpux_supply_ss_fpblock (struct regcache *regcache,
1264 int regnum, const char *save_state)
1265{
1266 const char *ss_fpblock = save_state + HPPA_HPUX_SS_FPBLOCK_OFFSET;
1267 int i, offset = 0;
1268
1269 /* FIXME: We view the floating-point state as 64 single-precision
1270 registers for 32-bit code, and 32 double-precision register for
1271 64-bit code. This distinction is artificial and should be
1272 eliminated. If that ever happens, we should remove the if-clause
1273 below. */
1274
1275 if (register_size (get_regcache_arch (regcache), HPPA_FP0_REGNUM) == 4)
1276 {
1277 for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 64; i++)
1278 {
1279 if (regnum == i || regnum == -1)
1280 regcache_raw_supply (regcache, i, ss_fpblock + offset);
1281
1282 offset += 4;
1283 }
1284 }
1285 else
1286 {
1287 for (i = HPPA_FP0_REGNUM; i < HPPA_FP0_REGNUM + 32; i++)
1288 {
1289 if (regnum == i || regnum == -1)
1290 regcache_raw_supply (regcache, i, ss_fpblock + offset);
1291
1292 offset += 8;
1293 }
1294 }
1295}
1296
1297static void
1298hppa_hpux_supply_ss_wide (struct regcache *regcache,
1299 int regnum, const char *save_state)
1300{
1301 const char *ss_wide = save_state + HPPA_HPUX_SS_WIDE_OFFSET;
1302 int i, offset = 8;
1303
1304 if (register_size (get_regcache_arch (regcache), HPPA_R1_REGNUM) == 4)
1305 offset += 4;
1306
1307 for (i = HPPA_R1_REGNUM; i < HPPA_FP0_REGNUM; i++)
1308 {
1309 if (regnum == i || regnum == -1)
1310 regcache_raw_supply (regcache, i, ss_wide + offset);
1311
1312 offset += 8;
1313 }
1314}
1315
1316static void
1317hppa_hpux_supply_save_state (const struct regset *regset,
1318 struct regcache *regcache,
1319 int regnum, const void *regs, size_t len)
1320{
1321 const char *proc_info = regs;
1322 const char *save_state = proc_info + 8;
1323 ULONGEST flags;
1324
1325 flags = extract_unsigned_integer (save_state + HPPA_HPUX_SS_FLAGS_OFFSET, 4);
1326 if (regnum == -1 || regnum == HPPA_FLAGS_REGNUM)
1327 {
1328 struct gdbarch *arch = get_regcache_arch (regcache);
1329 size_t size = register_size (arch, HPPA_FLAGS_REGNUM);
1330 char buf[8];
1331
1332 store_unsigned_integer (buf, size, flags);
1333 regcache_raw_supply (regcache, HPPA_FLAGS_REGNUM, buf);
1334 }
1335
1336 /* If the SS_WIDEREGS flag is set, we really do need the full
1337 `struct save_state'. */
1338 if (flags & HPPA_HPUX_SS_WIDEREGS && len < HPPA_HPUX_SAVE_STATE_SIZE)
8a3fe4f8 1339 error (_("Register set contents too small"));
08d53055
MK
1340
1341 if (flags & HPPA_HPUX_SS_WIDEREGS)
1342 hppa_hpux_supply_ss_wide (regcache, regnum, save_state);
1343 else
1344 hppa_hpux_supply_ss_narrow (regcache, regnum, save_state);
1345
1346 hppa_hpux_supply_ss_fpblock (regcache, regnum, save_state);
1347}
1348
1349/* HP-UX register set. */
1350
1351static struct regset hppa_hpux_regset =
1352{
1353 NULL,
1354 hppa_hpux_supply_save_state
1355};
1356
1357static const struct regset *
1358hppa_hpux_regset_from_core_section (struct gdbarch *gdbarch,
1359 const char *sect_name, size_t sect_size)
1360{
1361 if (strcmp (sect_name, ".reg") == 0
1362 && sect_size >= HPPA_HPUX_PA89_SAVE_STATE_SIZE + 8)
1363 return &hppa_hpux_regset;
1364
1365 return NULL;
1366}
1367\f
1368
cc72850f
MK
1369/* Bit in the `ss_flag' member of `struct save_state' that indicates
1370 the state was saved from a system call. From
1371 <machine/save_state.h>. */
1372#define HPPA_HPUX_SS_INSYSCALL 0x02
1373
1374static CORE_ADDR
61a1198a 1375hppa_hpux_read_pc (struct regcache *regcache)
cc72850f
MK
1376{
1377 ULONGEST flags;
1378
1379 /* If we're currently in a system call return the contents of %r31. */
61a1198a 1380 regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
cc72850f 1381 if (flags & HPPA_HPUX_SS_INSYSCALL)
61a1198a
UW
1382 {
1383 ULONGEST pc;
1384 regcache_cooked_read_unsigned (regcache, HPPA_R31_REGNUM, &pc);
1385 return pc & ~0x3;
1386 }
cc72850f 1387
61a1198a 1388 return hppa_read_pc (regcache);
cc72850f
MK
1389}
1390
1391static void
61a1198a 1392hppa_hpux_write_pc (struct regcache *regcache, CORE_ADDR pc)
cc72850f
MK
1393{
1394 ULONGEST flags;
1395
1396 /* If we're currently in a system call also write PC into %r31. */
61a1198a 1397 regcache_cooked_read_unsigned (regcache, HPPA_FLAGS_REGNUM, &flags);
cc72850f 1398 if (flags & HPPA_HPUX_SS_INSYSCALL)
61a1198a 1399 regcache_cooked_write_unsigned (regcache, HPPA_R31_REGNUM, pc | 0x3);
cc72850f 1400
61a1198a 1401 return hppa_write_pc (regcache, pc);
cc72850f
MK
1402}
1403
1404static CORE_ADDR
1405hppa_hpux_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1406{
1407 ULONGEST flags;
1408
1409 /* If we're currently in a system call return the contents of %r31. */
1410 flags = frame_unwind_register_unsigned (next_frame, HPPA_FLAGS_REGNUM);
1411 if (flags & HPPA_HPUX_SS_INSYSCALL)
1412 return frame_unwind_register_unsigned (next_frame, HPPA_R31_REGNUM) & ~0x3;
1413
1414 return hppa_unwind_pc (gdbarch, next_frame);
1415}
1416\f
c268433a 1417
f77a2124
RC
1418/* Given the current value of the pc, check to see if it is inside a stub, and
1419 if so, change the value of the pc to point to the caller of the stub.
1420 NEXT_FRAME is the next frame in the current list of frames.
1421 BASE contains to stack frame base of the current frame.
1422 SAVE_REGS is the register file stored in the frame cache. */
1423static void
1424hppa_hpux_unwind_adjust_stub (struct frame_info *next_frame, CORE_ADDR base,
1425 struct trad_frame_saved_reg *saved_regs)
1426{
464963c9 1427 struct gdbarch *gdbarch = get_frame_arch (next_frame);
f77a2124
RC
1428 int optimized, realreg;
1429 enum lval_type lval;
1430 CORE_ADDR addr;
1431 char buffer[sizeof(ULONGEST)];
1432 ULONGEST val;
1433 CORE_ADDR stubpc;
1434 struct unwind_table_entry *u;
1435
1436 trad_frame_get_prev_register (next_frame, saved_regs,
1437 HPPA_PCOQ_HEAD_REGNUM,
1438 &optimized, &lval, &addr, &realreg, buffer);
1439 val = extract_unsigned_integer (buffer,
1440 register_size (get_frame_arch (next_frame),
1441 HPPA_PCOQ_HEAD_REGNUM));
1442
1443 u = find_unwind_entry (val);
1444 if (u && u->stub_unwind.stub_type == EXPORT)
1445 {
819844ad 1446 stubpc = read_memory_integer
464963c9 1447 (base - 24, gdbarch_ptr_bit (gdbarch) / 8);
f77a2124
RC
1448 trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
1449 }
1450 else if (hppa_symbol_address ("__gcc_plt_call")
1451 == get_pc_function_start (val))
1452 {
819844ad 1453 stubpc = read_memory_integer
464963c9 1454 (base - 8, gdbarch_ptr_bit (gdbarch) / 8);
f77a2124
RC
1455 trad_frame_set_value (saved_regs, HPPA_PCOQ_HEAD_REGNUM, stubpc);
1456 }
1457}
1458
7d773d96
JB
1459static void
1460hppa_hpux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1461{
abc485a1
RC
1462 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1463
77d18ded 1464 if (IS_32BIT_TARGET (gdbarch))
84674fe1 1465 tdep->in_solib_call_trampoline = hppa32_hpux_in_solib_call_trampoline;
abc485a1 1466 else
84674fe1 1467 tdep->in_solib_call_trampoline = hppa64_hpux_in_solib_call_trampoline;
abc485a1 1468
f77a2124
RC
1469 tdep->unwind_adjust_stub = hppa_hpux_unwind_adjust_stub;
1470
3cd36e7c
MK
1471 set_gdbarch_in_solib_return_trampoline
1472 (gdbarch, hppa_hpux_in_solib_return_trampoline);
abc485a1 1473 set_gdbarch_skip_trampoline_code (gdbarch, hppa_hpux_skip_trampoline_code);
43613416 1474
c268433a
RC
1475 set_gdbarch_push_dummy_code (gdbarch, hppa_hpux_push_dummy_code);
1476 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1477
cc72850f
MK
1478 set_gdbarch_read_pc (gdbarch, hppa_hpux_read_pc);
1479 set_gdbarch_write_pc (gdbarch, hppa_hpux_write_pc);
1480 set_gdbarch_unwind_pc (gdbarch, hppa_hpux_unwind_pc);
6d350bb5
UW
1481 set_gdbarch_skip_permanent_breakpoint
1482 (gdbarch, hppa_skip_permanent_breakpoint);
cc72850f 1483
08d53055
MK
1484 set_gdbarch_regset_from_core_section
1485 (gdbarch, hppa_hpux_regset_from_core_section);
1486
43613416 1487 frame_unwind_append_sniffer (gdbarch, hppa_hpux_sigtramp_unwind_sniffer);
7d773d96 1488}
60e1ff27 1489
273f8429
JB
1490static void
1491hppa_hpux_som_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1492{
fdd72f95
RC
1493 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1494
1495 tdep->is_elf = 0;
c268433a 1496
77d18ded
RC
1497 tdep->find_global_pointer = hppa32_hpux_find_global_pointer;
1498
7d773d96 1499 hppa_hpux_init_abi (info, gdbarch);
d542061a 1500 som_solib_select (gdbarch);
273f8429
JB
1501}
1502
1503static void
1504hppa_hpux_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1505{
fdd72f95
RC
1506 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1507
1508 tdep->is_elf = 1;
77d18ded
RC
1509 tdep->find_global_pointer = hppa64_hpux_find_global_pointer;
1510
7d773d96 1511 hppa_hpux_init_abi (info, gdbarch);
d542061a 1512 pa64_solib_select (gdbarch);
273f8429
JB
1513}
1514
08d53055
MK
1515static enum gdb_osabi
1516hppa_hpux_core_osabi_sniffer (bfd *abfd)
1517{
1518 if (strcmp (bfd_get_target (abfd), "hpux-core") == 0)
1519 return GDB_OSABI_HPUX_SOM;
6b79fde8
RC
1520 else if (strcmp (bfd_get_target (abfd), "elf64-hppa") == 0)
1521 {
1522 asection *section;
1523
1524 section = bfd_get_section_by_name (abfd, ".kernel");
1525 if (section)
1526 {
1527 bfd_size_type size;
1528 char *contents;
1529
1530 size = bfd_section_size (abfd, section);
1531 contents = alloca (size);
1532 if (bfd_get_section_contents (abfd, section, contents,
1533 (file_ptr) 0, size)
1534 && strcmp (contents, "HP-UX") == 0)
1535 return GDB_OSABI_HPUX_ELF;
1536 }
1537 }
08d53055
MK
1538
1539 return GDB_OSABI_UNKNOWN;
1540}
1541
273f8429
JB
1542void
1543_initialize_hppa_hpux_tdep (void)
1544{
08d53055
MK
1545 /* BFD doesn't set a flavour for HP-UX style core files. It doesn't
1546 set the architecture either. */
1547 gdbarch_register_osabi_sniffer (bfd_arch_unknown,
1548 bfd_target_unknown_flavour,
1549 hppa_hpux_core_osabi_sniffer);
6b79fde8
RC
1550 gdbarch_register_osabi_sniffer (bfd_arch_hppa,
1551 bfd_target_elf_flavour,
1552 hppa_hpux_core_osabi_sniffer);
08d53055 1553
05816f70 1554 gdbarch_register_osabi (bfd_arch_hppa, 0, GDB_OSABI_HPUX_SOM,
273f8429 1555 hppa_hpux_som_init_abi);
51db5742 1556 gdbarch_register_osabi (bfd_arch_hppa, bfd_mach_hppa20w, GDB_OSABI_HPUX_ELF,
273f8429
JB
1557 hppa_hpux_elf_init_abi);
1558}
This page took 0.420324 seconds and 4 git commands to generate.