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