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