1 /* Common target-dependent code for ppc64 GDB, the GNU debugger.
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "ppc64-tdep.h"
28 /* Macros for matching instructions. Note that, since all the
29 operands are masked off before they're or-ed into the instruction,
30 you can use -1 to make masks. */
32 #define insn_d(opcd, rts, ra, d) \
33 ((((opcd) & 0x3f) << 26) \
34 | (((rts) & 0x1f) << 21) \
35 | (((ra) & 0x1f) << 16) \
38 #define insn_ds(opcd, rts, ra, d, xo) \
39 ((((opcd) & 0x3f) << 26) \
40 | (((rts) & 0x1f) << 21) \
41 | (((ra) & 0x1f) << 16) \
45 #define insn_xfx(opcd, rts, spr, xo) \
46 ((((opcd) & 0x3f) << 26) \
47 | (((rts) & 0x1f) << 21) \
48 | (((spr) & 0x1f) << 16) \
49 | (((spr) & 0x3e0) << 6) \
50 | (((xo) & 0x3ff) << 1))
52 /* If PLT is the address of a 64-bit PowerPC PLT entry,
53 return the function's entry point. */
56 ppc64_plt_entry_point (struct gdbarch *gdbarch, CORE_ADDR plt)
58 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
59 /* The first word of the PLT entry is the function entry point. */
60 return (CORE_ADDR) read_memory_unsigned_integer (plt, 8, byte_order);
63 /* Patterns for the standard linkage functions. These are built by
64 build_plt_stub in bfd/elf64-ppc.c. */
66 /* Old ELFv1 PLT call stub. */
68 static struct ppc_insn_pattern ppc64_standard_linkage1[] =
70 /* addis r12, r2, <any> */
71 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
74 { -1, insn_ds (62, 2, 1, 40, 0), 0 },
76 /* ld r11, <any>(r12) */
77 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
79 /* addis r12, r12, 1 <optional> */
80 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 12, 1), 1 },
82 /* ld r2, <any>(r12) */
83 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
85 /* addis r12, r12, 1 <optional> */
86 { insn_d (-1, -1, -1, -1), insn_d (15, 12, 12, 1), 1 },
89 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 0 },
91 /* ld r11, <any>(r12) <optional> */
92 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 1 },
95 { -1, 0x4e800420, 0 },
100 /* ELFv1 PLT call stub to access PLT entries more than +/- 32k from r2.
101 Also supports older stub with different placement of std 2,40(1),
102 a stub that omits the std 2,40(1), and both versions of power7
103 thread safety read barriers. Note that there are actually two more
104 instructions following "cmpldi r2, 0", "bnectr+" and "b <glink_i>",
105 but there isn't any need to match them. */
107 static struct ppc_insn_pattern ppc64_standard_linkage2[] =
109 /* std r2, 40(r1) <optional> */
110 { -1, insn_ds (62, 2, 1, 40, 0), 1 },
112 /* addis r12, r2, <any> */
113 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
115 /* std r2, 40(r1) <optional> */
116 { -1, insn_ds (62, 2, 1, 40, 0), 1 },
118 /* ld r11, <any>(r12) */
119 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
121 /* addi r12, r12, <any> <optional> */
122 { insn_d (-1, -1, -1, 0), insn_d (14, 12, 12, 0), 1 },
125 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 0 },
127 /* xor r11, r11, r11 <optional> */
128 { -1, 0x7d6b5a78, 1 },
130 /* add r12, r12, r11 <optional> */
131 { -1, 0x7d8c5a14, 1 },
133 /* ld r2, <any>(r12) */
134 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
136 /* ld r11, <any>(r12) <optional> */
137 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 1 },
139 /* bctr <optional> */
140 { -1, 0x4e800420, 1 },
142 /* cmpldi r2, 0 <optional> */
143 { -1, 0x28220000, 1 },
148 /* ELFv1 PLT call stub to access PLT entries within +/- 32k of r2. */
150 static struct ppc_insn_pattern ppc64_standard_linkage3[] =
152 /* std r2, 40(r1) <optional> */
153 { -1, insn_ds (62, 2, 1, 40, 0), 1 },
155 /* ld r11, <any>(r2) */
156 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 2, 0, 0), 0 },
158 /* addi r2, r2, <any> <optional> */
159 { insn_d (-1, -1, -1, 0), insn_d (14, 2, 2, 0), 1 },
162 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467), 0 },
164 /* xor r11, r11, r11 <optional> */
165 { -1, 0x7d6b5a78, 1 },
167 /* add r2, r2, r11 <optional> */
168 { -1, 0x7c425a14, 1 },
170 /* ld r11, <any>(r2) <optional> */
171 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 2, 0, 0), 1 },
173 /* ld r2, <any>(r2) */
174 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 2, 0, 0), 0 },
176 /* bctr <optional> */
177 { -1, 0x4e800420, 1 },
179 /* cmpldi r2, 0 <optional> */
180 { -1, 0x28220000, 1 },
185 /* ELFv1 PLT call stub to access PLT entries more than +/- 32k from r2.
186 A more modern variant of ppc64_standard_linkage2 differing in
189 static struct ppc_insn_pattern ppc64_standard_linkage4[] =
191 /* std r2, 40(r1) <optional> */
192 { -1, insn_ds (62, 2, 1, 40, 0), 1 },
194 /* addis r11, r2, <any> */
195 { insn_d (-1, -1, -1, 0), insn_d (15, 11, 2, 0), 0 },
197 /* ld r12, <any>(r11) */
198 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 12, 11, 0, 0), 0 },
200 /* addi r11, r11, <any> <optional> */
201 { insn_d (-1, -1, -1, 0), insn_d (14, 11, 11, 0), 1 },
204 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
206 /* xor r2, r12, r12 <optional> */
207 { -1, 0x7d826278, 1 },
209 /* add r11, r11, r2 <optional> */
210 { -1, 0x7d6b1214, 1 },
212 /* ld r2, <any>(r11) */
213 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 11, 0, 0), 0 },
215 /* ld r11, <any>(r11) <optional> */
216 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 11, 0, 0), 1 },
218 /* bctr <optional> */
219 { -1, 0x4e800420, 1 },
221 /* cmpldi r2, 0 <optional> */
222 { -1, 0x28220000, 1 },
227 /* ELFv1 PLT call stub to access PLT entries within +/- 32k of r2.
228 A more modern variant of ppc64_standard_linkage3 differing in
231 static struct ppc_insn_pattern ppc64_standard_linkage5[] =
233 /* std r2, 40(r1) <optional> */
234 { -1, insn_ds (62, 2, 1, 40, 0), 1 },
236 /* ld r12, <any>(r2) */
237 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 12, 2, 0, 0), 0 },
239 /* addi r2, r2, <any> <optional> */
240 { insn_d (-1, -1, -1, 0), insn_d (14, 2, 2, 0), 1 },
243 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
245 /* xor r11, r12, r12 <optional> */
246 { -1, 0x7d8b6278, 1 },
248 /* add r2, r2, r11 <optional> */
249 { -1, 0x7c425a14, 1 },
251 /* ld r11, <any>(r2) <optional> */
252 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 2, 0, 0), 1 },
254 /* ld r2, <any>(r2) */
255 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 2, 0, 0), 0 },
257 /* bctr <optional> */
258 { -1, 0x4e800420, 1 },
260 /* cmpldi r2, 0 <optional> */
261 { -1, 0x28220000, 1 },
266 /* ELFv2 PLT call stub to access PLT entries more than +/- 32k from r2. */
268 static struct ppc_insn_pattern ppc64_standard_linkage6[] =
270 /* std r2, 24(r1) <optional> */
271 { -1, insn_ds (62, 2, 1, 24, 0), 1 },
273 /* addis r11, r2, <any> */
274 { insn_d (-1, -1, -1, 0), insn_d (15, 11, 2, 0), 0 },
276 /* ld r12, <any>(r11) */
277 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 12, 11, 0, 0), 0 },
280 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
283 { -1, 0x4e800420, 0 },
288 /* ELFv2 PLT call stub to access PLT entries within +/- 32k of r2. */
290 static struct ppc_insn_pattern ppc64_standard_linkage7[] =
292 /* std r2, 24(r1) <optional> */
293 { -1, insn_ds (62, 2, 1, 24, 0), 1 },
295 /* ld r12, <any>(r2) */
296 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 12, 2, 0, 0), 0 },
299 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
302 { -1, 0x4e800420, 0 },
307 /* ELFv2 PLT call stub to access PLT entries more than +/- 32k from r2,
308 supporting fusion. */
310 static struct ppc_insn_pattern ppc64_standard_linkage8[] =
312 /* std r2, 24(r1) <optional> */
313 { -1, insn_ds (62, 2, 1, 24, 0), 1 },
315 /* addis r12, r2, <any> */
316 { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
318 /* ld r12, <any>(r12) */
319 { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 12, 12, 0, 0), 0 },
322 { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
325 { -1, 0x4e800420, 0 },
330 /* When the dynamic linker is doing lazy symbol resolution, the first
331 call to a function in another object will go like this:
333 - The user's function calls the linkage function:
335 100003d4: 4b ff ff ad bl 10000380 <nnnn.plt_call.printf>
336 100003d8: e8 41 00 28 ld r2,40(r1)
338 - The linkage function loads the entry point and toc pointer from
339 the function descriptor in the PLT, and jumps to it:
341 <nnnn.plt_call.printf>:
342 10000380: f8 41 00 28 std r2,40(r1)
343 10000384: e9 62 80 78 ld r11,-32648(r2)
344 10000388: 7d 69 03 a6 mtctr r11
345 1000038c: e8 42 80 80 ld r2,-32640(r2)
346 10000390: 28 22 00 00 cmpldi r2,0
347 10000394: 4c e2 04 20 bnectr+
348 10000398: 48 00 03 a0 b 10000738 <printf@plt>
350 - But since this is the first time that PLT entry has been used, it
351 sends control to its glink entry. That loads the number of the
352 PLT entry and jumps to the common glink0 code:
355 10000738: 38 00 00 01 li r0,1
356 1000073c: 4b ff ff bc b 100006f8 <__glink_PLTresolve>
358 - The common glink0 code then transfers control to the dynamic
361 100006f0: 0000000000010440 .quad plt0 - (. + 16)
362 <__glink_PLTresolve>:
363 100006f8: 7d 88 02 a6 mflr r12
364 100006fc: 42 9f 00 05 bcl 20,4*cr7+so,10000700
365 10000700: 7d 68 02 a6 mflr r11
366 10000704: e8 4b ff f0 ld r2,-16(r11)
367 10000708: 7d 88 03 a6 mtlr r12
368 1000070c: 7d 82 5a 14 add r12,r2,r11
369 10000710: e9 6c 00 00 ld r11,0(r12)
370 10000714: e8 4c 00 08 ld r2,8(r12)
371 10000718: 7d 69 03 a6 mtctr r11
372 1000071c: e9 6c 00 10 ld r11,16(r12)
373 10000720: 4e 80 04 20 bctr
375 Eventually, this code will figure out how to skip all of this,
376 including the dynamic linker. At the moment, we just get through
377 the linkage function. */
379 /* If the current thread is about to execute a series of instructions
380 at PC matching the ppc64_standard_linkage pattern, and INSN is the result
381 from that pattern match, return the code address to which the
382 standard linkage function will send them. (This doesn't deal with
383 dynamic linker lazy symbol resolution stubs.) */
386 ppc64_standard_linkage1_target (struct frame_info *frame,
387 CORE_ADDR pc, unsigned int *insn)
389 struct gdbarch *gdbarch = get_frame_arch (frame);
390 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
392 /* The address of the PLT entry this linkage function references. */
394 = ((CORE_ADDR) get_frame_register_unsigned (frame,
395 tdep->ppc_gp0_regnum + 2)
396 + (ppc_insn_d_field (insn[0]) << 16)
397 + ppc_insn_ds_field (insn[2]));
399 return ppc64_plt_entry_point (gdbarch, plt);
403 ppc64_standard_linkage2_target (struct frame_info *frame,
404 CORE_ADDR pc, unsigned int *insn)
406 struct gdbarch *gdbarch = get_frame_arch (frame);
407 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
409 /* The address of the PLT entry this linkage function references. */
411 = ((CORE_ADDR) get_frame_register_unsigned (frame,
412 tdep->ppc_gp0_regnum + 2)
413 + (ppc_insn_d_field (insn[1]) << 16)
414 + ppc_insn_ds_field (insn[3]));
416 return ppc64_plt_entry_point (gdbarch, plt);
420 ppc64_standard_linkage3_target (struct frame_info *frame,
421 CORE_ADDR pc, unsigned int *insn)
423 struct gdbarch *gdbarch = get_frame_arch (frame);
424 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
426 /* The address of the PLT entry this linkage function references. */
428 = ((CORE_ADDR) get_frame_register_unsigned (frame,
429 tdep->ppc_gp0_regnum + 2)
430 + ppc_insn_ds_field (insn[1]));
432 return ppc64_plt_entry_point (gdbarch, plt);
436 ppc64_standard_linkage4_target (struct frame_info *frame,
437 CORE_ADDR pc, unsigned int *insn)
439 struct gdbarch *gdbarch = get_frame_arch (frame);
440 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
443 = ((CORE_ADDR) get_frame_register_unsigned (frame, tdep->ppc_gp0_regnum + 2)
444 + (ppc_insn_d_field (insn[1]) << 16)
445 + ppc_insn_ds_field (insn[2]));
447 return ppc64_plt_entry_point (gdbarch, plt);
451 /* Given that we've begun executing a call trampoline at PC, return
452 the entry point of the function the trampoline will go to.
454 When the execution direction is EXEC_REVERSE, scan backward to
455 check whether we are in the middle of a PLT stub. */
458 ppc64_skip_trampoline_code_1 (struct frame_info *frame, CORE_ADDR pc)
460 #define MAX(a,b) ((a) > (b) ? (a) : (b))
461 unsigned int insns[MAX (MAX (MAX (ARRAY_SIZE (ppc64_standard_linkage1),
462 ARRAY_SIZE (ppc64_standard_linkage2)),
463 MAX (ARRAY_SIZE (ppc64_standard_linkage3),
464 ARRAY_SIZE (ppc64_standard_linkage4))),
465 MAX (MAX (ARRAY_SIZE (ppc64_standard_linkage5),
466 ARRAY_SIZE (ppc64_standard_linkage6)),
467 MAX (ARRAY_SIZE (ppc64_standard_linkage7),
468 ARRAY_SIZE (ppc64_standard_linkage8))))
474 /* When reverse-debugging, scan backward to check whether we are
475 in the middle of trampoline code. */
476 if (execution_direction == EXEC_REVERSE)
477 scan_limit = ARRAY_SIZE (insns) - 1;
479 for (i = 0; i < scan_limit; i++)
481 if (i < ARRAY_SIZE (ppc64_standard_linkage8) - 1
482 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage8, insns))
483 pc = ppc64_standard_linkage4_target (frame, pc, insns);
484 else if (i < ARRAY_SIZE (ppc64_standard_linkage7) - 1
485 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage7,
487 pc = ppc64_standard_linkage3_target (frame, pc, insns);
488 else if (i < ARRAY_SIZE (ppc64_standard_linkage6) - 1
489 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage6,
491 pc = ppc64_standard_linkage4_target (frame, pc, insns);
492 else if (i < ARRAY_SIZE (ppc64_standard_linkage5) - 1
493 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage5,
495 && (insns[8] != 0 || insns[9] != 0))
496 pc = ppc64_standard_linkage3_target (frame, pc, insns);
497 else if (i < ARRAY_SIZE (ppc64_standard_linkage4) - 1
498 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage4,
500 && (insns[9] != 0 || insns[10] != 0))
501 pc = ppc64_standard_linkage4_target (frame, pc, insns);
502 else if (i < ARRAY_SIZE (ppc64_standard_linkage3) - 1
503 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage3,
505 && (insns[8] != 0 || insns[9] != 0))
506 pc = ppc64_standard_linkage3_target (frame, pc, insns);
507 else if (i < ARRAY_SIZE (ppc64_standard_linkage2) - 1
508 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage2,
510 && (insns[10] != 0 || insns[11] != 0))
511 pc = ppc64_standard_linkage2_target (frame, pc, insns);
512 else if (i < ARRAY_SIZE (ppc64_standard_linkage1) - 1
513 && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage1,
515 pc = ppc64_standard_linkage1_target (frame, pc, insns);
518 /* Scan backward one more instructions if doesn't match. */
523 /* The PLT descriptor will either point to the already resolved target
524 address, or else to a glink stub. As the latter carry synthetic @plt
525 symbols, find_solib_trampoline_target should be able to resolve them. */
526 target = find_solib_trampoline_target (frame, pc);
527 return target ? target : pc;
533 /* Wrapper of ppc64_skip_trampoline_code_1 checking also
534 ppc_elfv2_skip_entrypoint. */
537 ppc64_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
539 struct gdbarch *gdbarch = get_frame_arch (frame);
541 pc = ppc64_skip_trampoline_code_1 (frame, pc);
542 if (pc != 0 && gdbarch_skip_entrypoint_p (gdbarch))
543 pc = gdbarch_skip_entrypoint (gdbarch, pc);
547 /* Support for convert_from_func_ptr_addr (ARCH, ADDR, TARG) on PPC64
550 Usually a function pointer's representation is simply the address
551 of the function. On GNU/Linux on the PowerPC however, a function
552 pointer may be a pointer to a function descriptor.
554 For PPC64, a function descriptor is a TOC entry, in a data section,
555 which contains three words: the first word is the address of the
556 function, the second word is the TOC pointer (r2), and the third word
557 is the static chain value.
559 Throughout GDB it is currently assumed that a function pointer contains
560 the address of the function, which is not easy to fix. In addition, the
561 conversion of a function address to a function pointer would
562 require allocation of a TOC entry in the inferior's memory space,
563 with all its drawbacks. To be able to call C++ virtual methods in
564 the inferior (which are called via function pointers),
565 find_function_addr uses this function to get the function address
566 from a function pointer.
568 If ADDR points at what is clearly a function descriptor, transform
569 it into the address of the corresponding function, if needed. Be
570 conservative, otherwise GDB will do the transformation on any
571 random addresses such as occur when there is no symbol table. */
574 ppc64_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
576 struct target_ops *targ)
578 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
579 struct target_section *s = target_section_by_addr (targ, addr);
581 /* Check if ADDR points to a function descriptor. */
582 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
584 /* There may be relocations that need to be applied to the .opd
585 section. Unfortunately, this function may be called at a time
586 where these relocations have not yet been performed -- this can
587 happen for example shortly after a library has been loaded with
588 dlopen, but ld.so has not yet applied the relocations.
590 To cope with both the case where the relocation has been applied,
591 and the case where it has not yet been applied, we do *not* read
592 the (maybe) relocated value from target memory, but we instead
593 read the non-relocated value from the BFD, and apply the relocation
596 This makes the assumption that all .opd entries are always relocated
597 by the same offset the section itself was relocated. This should
598 always be the case for GNU/Linux executables and shared libraries.
599 Note that other kind of object files (e.g. those added via
600 add-symbol-files) will currently never end up here anyway, as this
601 function accesses *target* sections only; only the main exec and
602 shared libraries are ever added to the target. */
607 res = bfd_get_section_contents (s->the_bfd_section->owner,
609 &buf, addr - s->addr, 8);
611 return extract_unsigned_integer (buf, 8, byte_order)
612 - bfd_section_vma (s->bfd, s->the_bfd_section) + s->addr;
618 /* A synthetic 'dot' symbols on ppc64 has the udata.p entry pointing
619 back to the original ELF symbol it was derived from. Get the size
623 ppc64_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
625 if ((sym->flags & BSF_SYNTHETIC) != 0 && sym->udata.p != NULL)
627 elf_symbol_type *elf_sym = (elf_symbol_type *) sym->udata.p;
628 SET_MSYMBOL_SIZE (msym, elf_sym->internal_elf_sym.st_size);