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