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