1161561Smarcel/* Target-dependent code for PowerPC systems running FreeBSD.
2161561Smarcel
3161561Smarcel   Copyright 2002, 2003, 2004 Free Software Foundation, Inc.
4161561Smarcel
5161561Smarcel   Contributed by Wasabi Systems, Inc.
6161561Smarcel
7161561Smarcel   This file is part of GDB.
8161561Smarcel
9161561Smarcel   This program is free software; you can redistribute it and/or modify
10161561Smarcel   it under the terms of the GNU General Public License as published by
11161561Smarcel   the Free Software Foundation; either version 2 of the License, or
12161561Smarcel   (at your option) any later version.
13161561Smarcel
14161561Smarcel   This program is distributed in the hope that it will be useful,
15161561Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
16161561Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17161561Smarcel   GNU General Public License for more details.
18161561Smarcel
19161561Smarcel   You should have received a copy of the GNU General Public License
20161561Smarcel   along with this program; if not, write to the Free Software
21161561Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
22161561Smarcel   Boston, MA 02111-1307, USA.  */
23161561Smarcel
24161561Smarcel#include "defs.h"
25161561Smarcel#include "gdbcore.h"
26161561Smarcel#include "regcache.h"
27161561Smarcel#include "target.h"
28161561Smarcel#include "breakpoint.h"
29161561Smarcel#include "value.h"
30223082Sandreast#include "gdb_string.h"
31161561Smarcel#include "osabi.h"
32223082Sandreast#include "regset.h"
33161561Smarcel
34161561Smarcel#include "ppc-tdep.h"
35161561Smarcel#include "ppcfbsd-tdep.h"
36161561Smarcel#include "trad-frame.h"
37161561Smarcel#include "gdb_assert.h"
38161561Smarcel#include "solib-svr4.h"
39161561Smarcel
40209867Snwhitehorn#define REG_FIXREG_OFFSET(x)	((x) * sizeof(register_t))
41209867Snwhitehorn#define REG_LR_OFFSET		(32 * sizeof(register_t))
42209867Snwhitehorn#define REG_CR_OFFSET		(33 * sizeof(register_t))
43209867Snwhitehorn#define REG_XER_OFFSET		(34 * sizeof(register_t))
44209867Snwhitehorn#define REG_CTR_OFFSET		(35 * sizeof(register_t))
45209867Snwhitehorn#define REG_PC_OFFSET		(36 * sizeof(register_t))
46209867Snwhitehorn#define SIZEOF_STRUCT_REG	(37 * sizeof(register_t))
47161561Smarcel
48161561Smarcel#define FPREG_FPR_OFFSET(x)	((x) * 8)
49161561Smarcel#define FPREG_FPSCR_OFFSET	(32 * 8)
50161561Smarcel#define SIZEOF_STRUCT_FPREG	(33 * 8)
51161561Smarcel
52161561Smarcelvoid
53161561Smarcelppcfbsd_supply_reg (char *regs, int regno)
54161561Smarcel{
55161561Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
56161561Smarcel  int i;
57161561Smarcel
58161561Smarcel  for (i = tdep->ppc_gp0_regnum; i <= tdep->ppc_gplast_regnum; i++)
59161561Smarcel    {
60161561Smarcel      if (regno == i || regno == -1)
61161561Smarcel	regcache_raw_supply (current_regcache, i, regs +
62161561Smarcel			     REG_FIXREG_OFFSET (i - tdep->ppc_gp0_regnum));
63161561Smarcel    }
64161561Smarcel
65161561Smarcel  if (regno == tdep->ppc_lr_regnum || regno == -1)
66161561Smarcel    regcache_raw_supply (current_regcache, tdep->ppc_lr_regnum,
67161561Smarcel			 regs + REG_LR_OFFSET);
68161561Smarcel
69161561Smarcel  if (regno == tdep->ppc_cr_regnum || regno == -1)
70161561Smarcel    regcache_raw_supply (current_regcache, tdep->ppc_cr_regnum,
71161561Smarcel			 regs + REG_CR_OFFSET);
72161561Smarcel
73161561Smarcel  if (regno == tdep->ppc_xer_regnum || regno == -1)
74161561Smarcel    regcache_raw_supply (current_regcache, tdep->ppc_xer_regnum,
75161561Smarcel			 regs + REG_XER_OFFSET);
76161561Smarcel
77161561Smarcel  if (regno == tdep->ppc_ctr_regnum || regno == -1)
78161561Smarcel    regcache_raw_supply (current_regcache, tdep->ppc_ctr_regnum,
79161561Smarcel			 regs + REG_CTR_OFFSET);
80161561Smarcel
81161561Smarcel  if (regno == PC_REGNUM || regno == -1)
82161561Smarcel    regcache_raw_supply (current_regcache, PC_REGNUM,
83161561Smarcel			 regs + REG_PC_OFFSET);
84161561Smarcel}
85223082Sandreaststatic void
86223082Sandreastppcfbsd_supply_gregset (const struct regset *regset,
87223082Sandreast			struct regcache *regcache,
88223082Sandreast			int regnum, void *gregs, size_t size)
89223082Sandreast{
90223082Sandreast  ppcfbsd_supply_reg (gregs, -1);
91223082Sandreast}
92161561Smarcel
93223082Sandreaststatic struct regset ppcfbsd_gregset = {
94223082Sandreast  NULL, (void*)ppcfbsd_supply_gregset
95223082Sandreast};
96223082Sandreast
97161561Smarcelvoid
98161561Smarcelppcfbsd_fill_reg (char *regs, int regno)
99161561Smarcel{
100161561Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
101161561Smarcel  int i;
102161561Smarcel
103161561Smarcel  for (i = tdep->ppc_gp0_regnum; i <= tdep->ppc_gplast_regnum; i++)
104161561Smarcel    {
105161561Smarcel      if (regno == i || regno == -1)
106161561Smarcel	regcache_raw_collect (current_regcache, i, regs +
107161561Smarcel			      REG_FIXREG_OFFSET (i - tdep->ppc_gp0_regnum));
108161561Smarcel    }
109161561Smarcel
110161561Smarcel  if (regno == tdep->ppc_lr_regnum || regno == -1)
111161561Smarcel    regcache_raw_collect (current_regcache, tdep->ppc_lr_regnum,
112161561Smarcel			  regs + REG_LR_OFFSET);
113161561Smarcel
114161561Smarcel  if (regno == tdep->ppc_cr_regnum || regno == -1)
115161561Smarcel    regcache_raw_collect (current_regcache, tdep->ppc_cr_regnum,
116161561Smarcel			  regs + REG_CR_OFFSET);
117161561Smarcel
118161561Smarcel  if (regno == tdep->ppc_xer_regnum || regno == -1)
119161561Smarcel    regcache_raw_collect (current_regcache, tdep->ppc_xer_regnum,
120161561Smarcel			  regs + REG_XER_OFFSET);
121161561Smarcel
122161561Smarcel  if (regno == tdep->ppc_ctr_regnum || regno == -1)
123161561Smarcel    regcache_raw_collect (current_regcache, tdep->ppc_ctr_regnum,
124161561Smarcel			  regs + REG_CTR_OFFSET);
125161561Smarcel
126161561Smarcel  if (regno == PC_REGNUM || regno == -1)
127161561Smarcel    regcache_raw_collect (current_regcache, PC_REGNUM, regs + REG_PC_OFFSET);
128161561Smarcel}
129161561Smarcel
130161561Smarcelvoid
131161561Smarcelppcfbsd_supply_fpreg (char *fpregs, int regno)
132161561Smarcel{
133161561Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
134161561Smarcel  int i;
135161561Smarcel
136161561Smarcel  /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
137161561Smarcel     point registers.  Traditionally, GDB's register set has still
138161561Smarcel     listed the floating point registers for such machines, so this
139161561Smarcel     code is harmless.  However, the new E500 port actually omits the
140161561Smarcel     floating point registers entirely from the register set --- they
141161561Smarcel     don't even have register numbers assigned to them.
142161561Smarcel
143161561Smarcel     It's not clear to me how best to update this code, so this assert
144161561Smarcel     will alert the first person to encounter the NetBSD/E500
145161561Smarcel     combination to the problem.  */
146161561Smarcel  gdb_assert (ppc_floating_point_unit_p (current_gdbarch));
147161561Smarcel
148161561Smarcel  for (i = FP0_REGNUM; i <= FPLAST_REGNUM; i++)
149161561Smarcel    {
150161561Smarcel      if (regno == i || regno == -1)
151161561Smarcel	regcache_raw_supply (current_regcache, i, fpregs +
152161561Smarcel			     FPREG_FPR_OFFSET (i - FP0_REGNUM));
153161561Smarcel    }
154161561Smarcel
155161561Smarcel  if (regno == tdep->ppc_fpscr_regnum || regno == -1)
156161561Smarcel    regcache_raw_supply (current_regcache, tdep->ppc_fpscr_regnum,
157161561Smarcel			 fpregs + FPREG_FPSCR_OFFSET);
158161561Smarcel}
159161561Smarcel
160223082Sandreaststatic void
161223082Sandreastppcfbsd_supply_fpregset (const struct regset *regset,
162223082Sandreast			 struct regcache * regcache,
163223082Sandreast			 int regnum, void *fpset, size_t size)
164223082Sandreast{
165223082Sandreast  ppcfbsd_supply_fpreg (fpset, -1);
166223082Sandreast}
167223082Sandreast
168223082Sandreast
169223082Sandreaststatic struct regset ppcfbsd_fpregset =
170223082Sandreast{
171223082Sandreast  NULL, (void*)ppcfbsd_supply_fpregset
172223082Sandreast};
173223082Sandreast
174161561Smarcelvoid
175161561Smarcelppcfbsd_fill_fpreg (char *fpregs, int regno)
176161561Smarcel{
177161561Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
178161561Smarcel  int i;
179161561Smarcel
180161561Smarcel  /* FIXME: jimb/2004-05-05: Some PPC variants don't have floating
181161561Smarcel     point registers.  Traditionally, GDB's register set has still
182161561Smarcel     listed the floating point registers for such machines, so this
183161561Smarcel     code is harmless.  However, the new E500 port actually omits the
184161561Smarcel     floating point registers entirely from the register set --- they
185161561Smarcel     don't even have register numbers assigned to them.
186161561Smarcel
187161561Smarcel     It's not clear to me how best to update this code, so this assert
188161561Smarcel     will alert the first person to encounter the NetBSD/E500
189161561Smarcel     combination to the problem.  */
190161561Smarcel  gdb_assert (ppc_floating_point_unit_p (current_gdbarch));
191161561Smarcel
192161561Smarcel  for (i = FP0_REGNUM; i <= FPLAST_REGNUM; i++)
193161561Smarcel    {
194161561Smarcel      if (regno == i || regno == -1)
195161561Smarcel	regcache_raw_collect (current_regcache, i, fpregs +
196161561Smarcel			      FPREG_FPR_OFFSET (i - FP0_REGNUM));
197161561Smarcel    }
198161561Smarcel
199161561Smarcel  if (regno == tdep->ppc_fpscr_regnum || regno == -1)
200161561Smarcel    regcache_raw_collect (current_regcache, tdep->ppc_fpscr_regnum,
201161561Smarcel			  fpregs + FPREG_FPSCR_OFFSET);
202161561Smarcel}
203161561Smarcel
204223082Sandreast/* Return the appropriate register set for the core section identified
205223082Sandreast   by SECT_NAME and SECT_SIZE.  */
206223082Sandreast
207223082Sandreastconst struct regset *
208223082Sandreastppcfbsd_regset_from_core_section (struct gdbarch *gdbarch,
209223082Sandreast				const char *sect_name, size_t sect_size)
210161561Smarcel{
211223082Sandreast  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
212161561Smarcel
213223082Sandreast  if (strcmp (sect_name, ".reg") == 0 && sect_size >= SIZEOF_STRUCT_REG)
214223082Sandreast    return &ppcfbsd_gregset;
215161561Smarcel
216223082Sandreast  if (strcmp (sect_name, ".reg2") == 0 && sect_size >= SIZEOF_STRUCT_FPREG)
217223082Sandreast    return &ppcfbsd_fpregset;
218161561Smarcel
219223082Sandreast  return NULL;
220223082Sandreast}
221161561Smarcel
222223082Sandreast
223223082Sandreast/* Macros for matching instructions.  Note that, since all the
224223082Sandreast   operands are masked off before they're or-ed into the instruction,
225223082Sandreast   you can use -1 to make masks.  */
226223082Sandreast
227223082Sandreast#define insn_d(opcd, rts, ra, d)                \
228223082Sandreast  ((((opcd) & 0x3f) << 26)                      \
229223082Sandreast   | (((rts) & 0x1f) << 21)                     \
230223082Sandreast   | (((ra) & 0x1f) << 16)                      \
231223082Sandreast   | ((d) & 0xffff))
232223082Sandreast
233223082Sandreast#define insn_ds(opcd, rts, ra, d, xo)           \
234223082Sandreast  ((((opcd) & 0x3f) << 26)                      \
235223082Sandreast   | (((rts) & 0x1f) << 21)                     \
236223082Sandreast   | (((ra) & 0x1f) << 16)                      \
237223082Sandreast   | ((d) & 0xfffc)                             \
238223082Sandreast   | ((xo) & 0x3))
239223082Sandreast
240223082Sandreast#define insn_xfx(opcd, rts, spr, xo)            \
241223082Sandreast  ((((opcd) & 0x3f) << 26)                      \
242223082Sandreast   | (((rts) & 0x1f) << 21)                     \
243223082Sandreast   | (((spr) & 0x1f) << 16)                     \
244223082Sandreast   | (((spr) & 0x3e0) << 6)                     \
245223082Sandreast   | (((xo) & 0x3ff) << 1))
246223082Sandreast
247223082Sandreast/* Read a PPC instruction from memory.  PPC instructions are always
248223082Sandreast   big-endian, no matter what endianness the program is running in, so
249223082Sandreast   we can't use read_memory_integer or one of its friends here.  */
250223082Sandreaststatic unsigned int
251223082Sandreastread_insn (CORE_ADDR pc)
252223082Sandreast{
253223082Sandreast  unsigned char buf[4];
254223082Sandreast
255223082Sandreast  read_memory (pc, buf, 4);
256223082Sandreast  return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
257161561Smarcel}
258161561Smarcel
259223082Sandreast
260223082Sandreast/* An instruction to match.  */
261223082Sandreaststruct insn_pattern
262161561Smarcel{
263223082Sandreast  unsigned int mask;            /* mask the insn with this... */
264223082Sandreast  unsigned int data;            /* ...and see if it matches this. */
265223082Sandreast  int optional;                 /* If non-zero, this insn may be absent.  */
266223082Sandreast};
267223082Sandreast
268223082Sandreast/* Return non-zero if the instructions at PC match the series
269223082Sandreast   described in PATTERN, or zero otherwise.  PATTERN is an array of
270223082Sandreast   'struct insn_pattern' objects, terminated by an entry whose mask is
271223082Sandreast   zero.
272223082Sandreast
273223082Sandreast   When the match is successful, fill INSN[i] with what PATTERN[i]
274223082Sandreast   matched.  If PATTERN[i] is optional, and the instruction wasn't
275223082Sandreast   present, set INSN[i] to 0 (which is not a valid PPC instruction).
276223082Sandreast   INSN should have as many elements as PATTERN.  Note that, if
277223082Sandreast   PATTERN contains optional instructions which aren't present in
278223082Sandreast   memory, then INSN will have holes, so INSN[i] isn't necessarily the
279223082Sandreast   i'th instruction in memory.  */
280223082Sandreaststatic int
281223082Sandreastinsns_match_pattern (CORE_ADDR pc,
282223082Sandreast                     struct insn_pattern *pattern,
283223082Sandreast                     unsigned int *insn)
284223082Sandreast{
285223082Sandreast  int i;
286223082Sandreast
287223082Sandreast  for (i = 0; pattern[i].mask; i++)
288161561Smarcel    {
289223082Sandreast      insn[i] = read_insn (pc);
290223082Sandreast      if ((insn[i] & pattern[i].mask) == pattern[i].data)
291223082Sandreast        pc += 4;
292223082Sandreast      else if (pattern[i].optional)
293223082Sandreast        insn[i] = 0;
294161561Smarcel      else
295223082Sandreast        return 0;
296223082Sandreast    }
297161561Smarcel
298223082Sandreast  return 1;
299223082Sandreast}
300161561Smarcel
301223082Sandreast
302223082Sandreast/* Return the 'd' field of the d-form instruction INSN, properly
303223082Sandreast   sign-extended.  */
304223082Sandreaststatic CORE_ADDR
305223082Sandreastinsn_d_field (unsigned int insn)
306223082Sandreast{
307223082Sandreast  return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
308161561Smarcel}
309161561Smarcel
310223082Sandreast
311223082Sandreast/* Return the 'ds' field of the ds-form instruction INSN, with the two
312223082Sandreast   zero bits concatenated at the right, and properly
313223082Sandreast   sign-extended.  */
314223082Sandreaststatic CORE_ADDR
315223082Sandreastinsn_ds_field (unsigned int insn)
316161561Smarcel{
317223082Sandreast  return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
318223082Sandreast}
319161561Smarcel
320223082Sandreast
321223082Sandreast/* If DESC is the address of a 64-bit PowerPC FreeBSD function
322223082Sandreast   descriptor, return the descriptor's entry point.  */
323223082Sandreaststatic CORE_ADDR
324223082Sandreastppc64_desc_entry_point (CORE_ADDR desc)
325161561Smarcel{
326223082Sandreast  /* The first word of the descriptor is the entry point.  */
327223082Sandreast  return (CORE_ADDR) read_memory_unsigned_integer (desc, 8);
328223082Sandreast}
329161561Smarcel
330223082Sandreast
331223082Sandreast/* Pattern for the standard linkage function.  These are built by
332223082Sandreast   build_plt_stub in elf64-ppc.c, whose GLINK argument is always
333223082Sandreast   zero.  */
334223082Sandreaststatic struct insn_pattern ppc64_standard_linkage[] =
335223082Sandreast  {
336223082Sandreast    /* addis r12, r2, <any> */
337223082Sandreast    { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
338223082Sandreast
339223082Sandreast    /* std r2, 40(r1) */
340223082Sandreast    { -1, insn_ds (62, 2, 1, 40, 0), 0 },
341223082Sandreast
342223082Sandreast    /* ld r11, <any>(r12) */
343223082Sandreast    { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
344223082Sandreast
345223082Sandreast    /* addis r12, r12, 1 <optional> */
346223082Sandreast    { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
347223082Sandreast
348223082Sandreast    /* ld r2, <any>(r12) */
349223082Sandreast    { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 2, 12, 0, 0), 0 },
350223082Sandreast
351223082Sandreast    /* addis r12, r12, 1 <optional> */
352223082Sandreast    { insn_d (-1, -1, -1, -1), insn_d (15, 12, 2, 1), 1 },
353223082Sandreast
354223082Sandreast    /* mtctr r11 */
355223082Sandreast    { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 11, 9, 467),
356223082Sandreast      0 },
357223082Sandreast
358223082Sandreast    /* ld r11, <any>(r12) */
359223082Sandreast    { insn_ds (-1, -1, -1, 0, -1), insn_ds (58, 11, 12, 0, 0), 0 },
360223082Sandreast
361223082Sandreast    /* bctr */
362223082Sandreast    { -1, 0x4e800420, 0 },
363223082Sandreast
364223082Sandreast    { 0, 0, 0 }
365223082Sandreast  };
366223082Sandreast#define PPC64_STANDARD_LINKAGE_LEN \
367223082Sandreast  (sizeof (ppc64_standard_linkage) / sizeof (ppc64_standard_linkage[0]))
368223082Sandreast
369223082Sandreast/* When the dynamic linker is doing lazy symbol resolution, the first
370223082Sandreast   call to a function in another object will go like this:
371223082Sandreast
372223082Sandreast   - The user's function calls the linkage function:
373223082Sandreast
374223082Sandreast     100007c4:	4b ff fc d5 	bl	10000498
375223082Sandreast     100007c8:	e8 41 00 28 	ld	r2,40(r1)
376223082Sandreast
377223082Sandreast   - The linkage function loads the entry point (and other stuff) from
378223082Sandreast     the function descriptor in the PLT, and jumps to it:
379223082Sandreast
380223082Sandreast     10000498:	3d 82 00 00 	addis	r12,r2,0
381223082Sandreast     1000049c:	f8 41 00 28 	std	r2,40(r1)
382223082Sandreast     100004a0:	e9 6c 80 98 	ld	r11,-32616(r12)
383223082Sandreast     100004a4:	e8 4c 80 a0 	ld	r2,-32608(r12)
384223082Sandreast     100004a8:	7d 69 03 a6 	mtctr	r11
385223082Sandreast     100004ac:	e9 6c 80 a8 	ld	r11,-32600(r12)
386223082Sandreast     100004b0:	4e 80 04 20 	bctr
387223082Sandreast
388223082Sandreast   - But since this is the first time that PLT entry has been used, it
389223082Sandreast     sends control to its glink entry.  That loads the number of the
390223082Sandreast     PLT entry and jumps to the common glink0 code:
391223082Sandreast
392223082Sandreast     10000c98:	38 00 00 00 	li	r0,0
393223082Sandreast     10000c9c:	4b ff ff dc 	b	10000c78
394223082Sandreast
395223082Sandreast   - The common glink0 code then transfers control to the dynamic
396223082Sandreast     linker's fixup code:
397223082Sandreast
398223082Sandreast     10000c78:	e8 41 00 28 	ld	r2,40(r1)
399223082Sandreast     10000c7c:	3d 82 00 00 	addis	r12,r2,0
400223082Sandreast     10000c80:	e9 6c 80 80 	ld	r11,-32640(r12)
401223082Sandreast     10000c84:	e8 4c 80 88 	ld	r2,-32632(r12)
402223082Sandreast     10000c88:	7d 69 03 a6 	mtctr	r11
403223082Sandreast     10000c8c:	e9 6c 80 90 	ld	r11,-32624(r12)
404223082Sandreast     10000c90:	4e 80 04 20 	bctr
405223082Sandreast
406223082Sandreast   Eventually, this code will figure out how to skip all of this,
407223082Sandreast   including the dynamic linker.  At the moment, we just get through
408223082Sandreast   the linkage function.  */
409223082Sandreast
410223082Sandreast/* If the current thread is about to execute a series of instructions
411223082Sandreast   at PC matching the ppc64_standard_linkage pattern, and INSN is the result
412223082Sandreast   from that pattern match, return the code address to which the
413223082Sandreast   standard linkage function will send them.  (This doesn't deal with
414223082Sandreast   dynamic linker lazy symbol resolution stubs.)  */
415223082Sandreaststatic CORE_ADDR
416223082Sandreastppc64_standard_linkage_target (CORE_ADDR pc, unsigned int *insn)
417223082Sandreast{
418223082Sandreast  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
419223082Sandreast
420223082Sandreast  /* The address of the function descriptor this linkage function
421223082Sandreast     references.  */
422223082Sandreast  CORE_ADDR desc
423223082Sandreast    = ((CORE_ADDR) read_register (tdep->ppc_gp0_regnum + 2)
424223082Sandreast       + (insn_d_field (insn[0]) << 16)
425223082Sandreast       + insn_ds_field (insn[2]));
426223082Sandreast
427223082Sandreast  /* The first word of the descriptor is the entry point.  Return that.  */
428223082Sandreast  return ppc64_desc_entry_point (desc);
429223082Sandreast}
430223082Sandreast
431223082Sandreast
432223082Sandreast/* Given that we've begun executing a call trampoline at PC, return
433223082Sandreast   the entry point of the function the trampoline will go to.  */
434223082Sandreaststatic CORE_ADDR
435223082Sandreastppc64_skip_trampoline_code (CORE_ADDR pc)
436223082Sandreast{
437223082Sandreast  unsigned int ppc64_standard_linkage_insn[PPC64_STANDARD_LINKAGE_LEN];
438223082Sandreast
439223082Sandreast  if (insns_match_pattern (pc, ppc64_standard_linkage,
440223082Sandreast                           ppc64_standard_linkage_insn))
441223082Sandreast    return ppc64_standard_linkage_target (pc, ppc64_standard_linkage_insn);
442223082Sandreast  else
443223082Sandreast    return 0;
444223082Sandreast}
445223082Sandreast
446223082Sandreast
447223082Sandreast/* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG) on PPC64
448223082Sandreast   GNU/Linux and FreeBSD.
449223082Sandreast
450223082Sandreast   Usually a function pointer's representation is simply the address
451223082Sandreast   of the function. On GNU/Linux on the 64-bit PowerPC however, a
452223082Sandreast   function pointer is represented by a pointer to a TOC entry. This
453223082Sandreast   TOC entry contains three words, the first word is the address of
454223082Sandreast   the function, the second word is the TOC pointer (r2), and the
455223082Sandreast   third word is the static chain value.  Throughout GDB it is
456223082Sandreast   currently assumed that a function pointer contains the address of
457223082Sandreast   the function, which is not easy to fix.  In addition, the
458223082Sandreast   conversion of a function address to a function pointer would
459223082Sandreast   require allocation of a TOC entry in the inferior's memory space,
460223082Sandreast   with all its drawbacks.  To be able to call C++ virtual methods in
461223082Sandreast   the inferior (which are called via function pointers),
462223082Sandreast   find_function_addr uses this function to get the function address
463223082Sandreast   from a function pointer.  */
464223082Sandreast
465223082Sandreast/* If ADDR points at what is clearly a function descriptor, transform
466223082Sandreast   it into the address of the corresponding function.  Be
467223082Sandreast   conservative, otherwize GDB will do the transformation on any
468223082Sandreast   random addresses such as occures when there is no symbol table.  */
469223082Sandreast
470223082Sandreaststatic CORE_ADDR
471223082Sandreastppc64_fbsd_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
472223082Sandreast				       CORE_ADDR addr,
473223082Sandreast				       struct target_ops *targ)
474223082Sandreast{
475223082Sandreast  struct section_table *s = target_section_by_addr (targ, addr);
476223082Sandreast
477223082Sandreast  /* Check if ADDR points to a function descriptor.  */
478223082Sandreast  if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
479223082Sandreast    return get_target_memory_unsigned (targ, addr, 8);
480223082Sandreast
481223082Sandreast  return addr;
482223082Sandreast}
483223082Sandreast
484161561Smarcelstatic int
485161561Smarcelppcfbsd_pc_in_sigtramp (CORE_ADDR pc, char *func_name)
486161561Smarcel{
487161561Smarcel  return (pc >= 0x7fffef00U) ? 1 : 0;
488161561Smarcel}
489161561Smarcel
490161561Smarcel/* NetBSD is confused.  It appears that 1.5 was using the correct SVr4
491161561Smarcel   convention but, 1.6 switched to the below broken convention.  For
492161561Smarcel   the moment use the broken convention.  Ulgh!.  */
493161561Smarcel
494161561Smarcelstatic enum return_value_convention
495161561Smarcelppcfbsd_return_value (struct gdbarch *gdbarch, struct type *valtype,
496161561Smarcel		      struct regcache *regcache, void *readbuf,
497161561Smarcel		      const void *writebuf)
498161561Smarcel{
499161561Smarcel  if ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
500161561Smarcel       || TYPE_CODE (valtype) == TYPE_CODE_UNION)
501161561Smarcel      && !((TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 8))
502161561Smarcel      && !(TYPE_LENGTH (valtype) == 1
503161561Smarcel	   || TYPE_LENGTH (valtype) == 2
504161561Smarcel	   || TYPE_LENGTH (valtype) == 4
505161561Smarcel	   || TYPE_LENGTH (valtype) == 8))
506161561Smarcel    return RETURN_VALUE_STRUCT_CONVENTION;
507161561Smarcel  else
508161561Smarcel    return ppc_sysv_abi_broken_return_value (gdbarch, valtype, regcache,
509161561Smarcel					     readbuf, writebuf);
510161561Smarcel}
511161561Smarcel
512161561Smarcelstatic void
513161561Smarcelppcfbsd_init_abi (struct gdbarch_info info,
514161561Smarcel                  struct gdbarch *gdbarch)
515161561Smarcel{
516223082Sandreast  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
517223082Sandreast
518223082Sandreast  /* FreeBSD doesn't support the 128-bit `long double' from the psABI. */
519223082Sandreast  set_gdbarch_long_double_bit (gdbarch, 64);
520223082Sandreast
521161561Smarcel  set_gdbarch_pc_in_sigtramp (gdbarch, ppcfbsd_pc_in_sigtramp);
522223082Sandreast
523223082Sandreast  if (tdep->wordsize == 4)
524223082Sandreast    {
525223082Sandreast      set_gdbarch_return_value (gdbarch, ppcfbsd_return_value);
526223082Sandreast      set_solib_svr4_fetch_link_map_offsets (gdbarch,
527223082Sandreast					     svr4_ilp32_fetch_link_map_offsets);
528223082Sandreast    }
529223082Sandreast
530223082Sandreast  if (tdep->wordsize == 8)
531223082Sandreast    {
532223082Sandreast      set_gdbarch_convert_from_func_ptr_addr
533223082Sandreast        (gdbarch, ppc64_fbsd_convert_from_func_ptr_addr);
534223082Sandreast
535223082Sandreast      set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
536223082Sandreast
537223082Sandreast      set_solib_svr4_fetch_link_map_offsets (gdbarch,
538223082Sandreast					     svr4_lp64_fetch_link_map_offsets);
539223082Sandreast    }
540223082Sandreast
541223082Sandreast  set_gdbarch_regset_from_core_section (gdbarch,
542223082Sandreast					ppcfbsd_regset_from_core_section);
543161561Smarcel}
544161561Smarcel
545161561Smarcelvoid
546161561Smarcel_initialize_ppcfbsd_tdep (void)
547161561Smarcel{
548223082Sandreast  gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc,
549223082Sandreast			  GDB_OSABI_FREEBSD_ELF, ppcfbsd_init_abi);
550223082Sandreast  gdbarch_register_osabi (bfd_arch_powerpc, bfd_mach_ppc64,
551223082Sandreast			  GDB_OSABI_FREEBSD_ELF, ppcfbsd_init_abi);
552161561Smarcel  gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_FREEBSD_ELF,
553161561Smarcel			  ppcfbsd_init_abi);
554161561Smarcel}
555