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