GDB (xrefs)
/tmp/gdb-8.1/gdb/rs6000-lynx178-tdep.c
Go to the documentation of this file.
1 /* Copyright (C) 2012-2018 Free Software Foundation, Inc.
2 
3  This file is part of GDB.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 
18 #include "defs.h"
19 #include "osabi.h"
20 #include "regcache.h"
21 #include "gdbcore.h"
22 #include "gdbtypes.h"
23 #include "infcall.h"
24 #include "ppc-tdep.h"
25 #include "target-float.h"
26 #include "value.h"
27 #include "xcoffread.h"
28 
29 /* Implement the "push_dummy_call" gdbarch method. */
30 
31 static CORE_ADDR
33  struct value *function,
34  struct regcache *regcache, CORE_ADDR bp_addr,
35  int nargs, struct value **args, CORE_ADDR sp,
36  int struct_return, CORE_ADDR struct_addr)
37 {
38  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
39  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
40  int ii;
41  int len = 0;
42  int argno; /* current argument number */
43  int argbytes; /* current argument byte */
44  gdb_byte tmp_buffer[50];
45  int f_argno = 0; /* current floating point argno */
47  CORE_ADDR func_addr = find_function_addr (function, NULL);
48 
49  struct value *arg = 0;
50  struct type *type;
51 
52  ULONGEST saved_sp;
53 
54  /* The calling convention this function implements assumes the
55  processor has floating-point registers. We shouldn't be using it
56  on PPC variants that lack them. */
58 
59  /* The first eight words of ther arguments are passed in registers.
60  Copy them appropriately. */
61  ii = 0;
62 
63  /* If the function is returning a `struct', then the first word
64  (which will be passed in r3) is used for struct return address.
65  In that case we should advance one word and start from r4
66  register to copy parameters. */
67  if (struct_return)
68  {
70  struct_addr);
71  ii++;
72  }
73 
74  /* Effectively indirect call... gcc does...
75 
76  return_val example( float, int);
77 
78  eabi:
79  float in fp0, int in r3
80  offset of stack on overflow 8/16
81  for varargs, must go by type.
82  power open:
83  float in r3&r4, int in r5
84  offset of stack on overflow different
85  both:
86  return in r3 or f0. If no float, must study how gcc emulates floats;
87  pay attention to arg promotion.
88  User may have to cast\args to handle promotion correctly
89  since gdb won't know if prototype supplied or not. */
90 
91  for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
92  {
93  int reg_size = register_size (gdbarch, ii + 3);
94 
95  arg = args[argno];
96  type = check_typedef (value_type (arg));
97  len = TYPE_LENGTH (type);
98 
99  if (TYPE_CODE (type) == TYPE_CODE_FLT)
100  {
101 
102  /* Floating point arguments are passed in fpr's, as well as gpr's.
103  There are 13 fpr's reserved for passing parameters. At this point
104  there is no way we would run out of them.
105 
106  Always store the floating point value using the register's
107  floating-point format. */
108  const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
110  struct type *reg_type = register_type (gdbarch, fp_regnum);
111 
112  gdb_assert (len <= 8);
113 
114  target_float_convert (value_contents (arg), type, reg_val, reg_type);
116  ++f_argno;
117  }
118 
119  if (len > reg_size)
120  {
121 
122  /* Argument takes more than one register. */
123  while (argbytes < len)
124  {
126  memset (word, 0, reg_size);
127  memcpy (word,
128  ((char *) value_contents (arg)) + argbytes,
129  (len - argbytes) > reg_size
130  ? reg_size : len - argbytes);
132  tdep->ppc_gp0_regnum + 3 + ii,
133  word);
134  ++ii, argbytes += reg_size;
135 
136  if (ii >= 8)
137  goto ran_out_of_registers_for_arguments;
138  }
139  argbytes = 0;
140  --ii;
141  }
142  else
143  {
144  /* Argument can fit in one register. No problem. */
146 
147  memset (word, 0, reg_size);
148  memcpy (word, value_contents (arg), len);
149  regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
150  }
151  ++argno;
152  }
153 
154 ran_out_of_registers_for_arguments:
155 
158  &saved_sp);
159 
160  /* Location for 8 parameters are always reserved. */
161  sp -= wordsize * 8;
162 
163  /* Another six words for back chain, TOC register, link register, etc. */
164  sp -= wordsize * 6;
165 
166  /* Stack pointer must be quadword aligned. */
167  sp = align_down (sp, 16);
168 
169  /* If there are more arguments, allocate space for them in
170  the stack, then push them starting from the ninth one. */
171 
172  if ((argno < nargs) || argbytes)
173  {
174  int space = 0, jj;
175 
176  if (argbytes)
177  {
178  space += align_up (len - argbytes, 4);
179  jj = argno + 1;
180  }
181  else
182  jj = argno;
183 
184  for (; jj < nargs; ++jj)
185  {
186  struct value *val = args[jj];
187 
188  space += align_up (TYPE_LENGTH (value_type (val)), 4);
189  }
190 
191  /* Add location required for the rest of the parameters. */
192  space = align_up (space, 16);
193  sp -= space;
194 
195  /* This is another instance we need to be concerned about
196  securing our stack space. If we write anything underneath %sp
197  (r1), we might conflict with the kernel who thinks he is free
198  to use this area. So, update %sp first before doing anything
199  else. */
200 
202  gdbarch_sp_regnum (gdbarch), sp);
203 
204  /* If the last argument copied into the registers didn't fit there
205  completely, push the rest of it into stack. */
206 
207  if (argbytes)
208  {
209  write_memory (sp + 24 + (ii * 4),
210  value_contents (arg) + argbytes,
211  len - argbytes);
212  ++argno;
213  ii += align_up (len - argbytes, 4) / 4;
214  }
215 
216  /* Push the rest of the arguments into stack. */
217  for (; argno < nargs; ++argno)
218  {
219 
220  arg = args[argno];
221  type = check_typedef (value_type (arg));
222  len = TYPE_LENGTH (type);
223 
224 
225  /* Float types should be passed in fpr's, as well as in the
226  stack. */
227  if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
228  {
229 
230  gdb_assert (len <= 8);
231 
233  tdep->ppc_fp0_regnum + 1 + f_argno,
234  value_contents (arg));
235  ++f_argno;
236  }
237 
238  write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
239  ii += align_up (len, 4) / 4;
240  }
241  }
242 
243  /* Set the stack pointer. According to the ABI, the SP is meant to
244  be set _before_ the corresponding stack space is used. On AIX,
245  this even applies when the target has been completely stopped!
246  Not doing this can lead to conflicts with the kernel which thinks
247  that it still has control over this not-yet-allocated stack
248  region. */
250 
251  /* Set back chain properly. */
252  store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
253  write_memory (sp, tmp_buffer, wordsize);
254 
255  /* Point the inferior function call's return address at the dummy's
256  breakpoint. */
258 
260  return sp;
261 }
262 
263 /* Implement the "return_value" gdbarch method. */
264 
265 static enum return_value_convention
267  struct type *valtype, struct regcache *regcache,
268  gdb_byte *readbuf, const gdb_byte *writebuf)
269 {
270  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
271  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
272 
273  /* The calling convention this function implements assumes the
274  processor has floating-point registers. We shouldn't be using it
275  on PowerPC variants that lack them. */
277 
278  /* AltiVec extension: Functions that declare a vector data type as a
279  return value place that return value in VR2. */
280  if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
281  && TYPE_LENGTH (valtype) == 16)
282  {
283  if (readbuf)
284  regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
285  if (writebuf)
286  regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
287 
289  }
290 
291  /* If the called subprogram returns an aggregate, there exists an
292  implicit first argument, whose value is the address of a caller-
293  allocated buffer into which the callee is assumed to store its
294  return value. All explicit parameters are appropriately
295  relabeled. */
296  if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
297  || TYPE_CODE (valtype) == TYPE_CODE_UNION
298  || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
300 
301  /* Scalar floating-point values are returned in FPR1 for float or
302  double, and in FPR1:FPR2 for quadword precision. Fortran
303  complex*8 and complex*16 are returned in FPR1:FPR2, and
304  complex*32 is returned in FPR1:FPR4. */
305  if (TYPE_CODE (valtype) == TYPE_CODE_FLT
306  && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
307  {
308  struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
309  gdb_byte regval[8];
310 
311  /* FIXME: kettenis/2007-01-01: Add support for quadword
312  precision and complex. */
313 
314  if (readbuf)
315  {
316  regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
317  target_float_convert (regval, regtype, readbuf, valtype);
318  }
319  if (writebuf)
320  {
321  target_float_convert (writebuf, valtype, regval, regtype);
322  regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
323  }
324 
326  }
327 
328  /* Values of the types int, long, short, pointer, and char (length
329  is less than or equal to four bytes), as well as bit values of
330  lengths less than or equal to 32 bits, must be returned right
331  justified in GPR3 with signed values sign extended and unsigned
332  values zero extended, as necessary. */
333  if (TYPE_LENGTH (valtype) <= tdep->wordsize)
334  {
335  if (readbuf)
336  {
337  ULONGEST regval;
338 
339  /* For reading we don't have to worry about sign extension. */
341  &regval);
342  store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
343  regval);
344  }
345  if (writebuf)
346  {
347  /* For writing, use unpack_long since that should handle any
348  required sign extension. */
350  unpack_long (valtype, writebuf));
351  }
352 
354  }
355 
356  /* Eight-byte non-floating-point scalar values must be returned in
357  GPR3:GPR4. */
358 
359  if (TYPE_LENGTH (valtype) == 8)
360  {
361  gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
362  gdb_assert (tdep->wordsize == 4);
363 
364  if (readbuf)
365  {
366  gdb_byte regval[8];
367 
368  regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
370  regval + 4);
371  memcpy (readbuf, regval, 8);
372  }
373  if (writebuf)
374  {
375  regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
377  writebuf + 4);
378  }
379 
381  }
382 
384 }
385 
386 /* PowerPC Lynx178 OSABI sniffer. */
387 
388 static enum gdb_osabi
390 {
391  if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
392  return GDB_OSABI_UNKNOWN;
393 
394  /* The only noticeable difference between Lynx178 XCOFF files and
395  AIX XCOFF files comes from the fact that there are no shared
396  libraries on Lynx178. So if the number of import files is
397  different from zero, it cannot be a Lynx178 binary. */
398  if (xcoff_get_n_import_files (abfd) != 0)
399  return GDB_OSABI_UNKNOWN;
400 
401  return GDB_OSABI_LYNXOS178;
402 }
403 
404 /* Callback for powerpc-lynx178 initialization. */
405 
406 static void
408 {
412 }
413 
414 void
416 {
417  gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
418  bfd_target_xcoff_flavour,
420  gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
422 }
423 
bfd_vma CORE_ADDR
Definition: common-types.h:41
int ppc_lr_regnum
Definition: ppc-tdep.h:231
ULONGEST align_down(ULONGEST v, int n)
Definition: utils.c:3005
void * memset(T *s, int c, size_t n)=delete
return_value_convention
Definition: defs.h:247
void gdbarch_register_osabi_sniffer(enum bfd_architecture arch, enum bfd_flavour flavour, enum gdb_osabi(*sniffer_fn)(bfd *))
Definition: osabi.c:219
void target_float_convert(const gdb_byte *from, const struct type *from_type, gdb_byte *to, const struct type *to_type)
struct gdbarch_tdep * gdbarch_tdep(struct gdbarch *gdbarch)
Definition: gdbarch.c:1491
static enum gdb_osabi rs6000_lynx178_osabi_sniffer(bfd *abfd)
void regcache_raw_write_signed(struct regcache *regcache, int regnum, LONGEST val)
Definition: regcache.c:620
void _initialize_rs6000_lynx178_tdep(void)
struct type * check_typedef(struct type *type)
Definition: gdbtypes.c:2421
const gdb_byte * value_contents(struct value *value)
Definition: value.c:1407
#define PPC_MAX_REGISTER_SIZE
Definition: ppc-tdep.h:321
int gdbarch_sp_regnum(struct gdbarch *gdbarch)
Definition: gdbarch.c:2146
enum register_status regcache_cooked_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition: regcache.c:777
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:152
int ppc_gp0_regnum
Definition: ppc-tdep.h:227
struct_return
Definition: arm-tdep.h:88
#define TYPE_VECTOR(t)
Definition: gdbtypes.h:252
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1509
#define TARGET_CHAR_BIT
Definition: host-defs.h:29
Definition: gdbtypes.h:749
static const char * type
Definition: language.c:113
static enum return_value_convention rs6000_lynx178_return_value(struct gdbarch *gdbarch, struct value *function, struct type *valtype, struct regcache *regcache, gdb_byte *readbuf, const gdb_byte *writebuf)
void gdbarch_register_osabi(enum bfd_architecture arch, unsigned long machine, enum gdb_osabi osabi, void(*init_osabi)(struct gdbarch_info, struct gdbarch *))
Definition: osabi.c:143
LONGEST unpack_long(struct type *type, const gdb_byte *valaddr)
Definition: value.c:2880
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: value.c:169
bfd_byte gdb_byte
Definition: common-types.h:38
int ppc_vr0_regnum
Definition: ppc-tdep.h:252
ULONGEST align_up(ULONGEST v, int n)
Definition: utils.c:2997
#define TYPE_CODE(thistype)
Definition: gdbtypes.h:1238
void regcache_cooked_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:806
int wordsize
Definition: ppc-tdep.h:216
int xcoff_get_n_import_files(bfd *abfd)
Definition: xcoffread.c:3156
CORE_ADDR find_function_addr(struct value *function, struct type **retval_type)
Definition: infcall.c:250
static void rs6000_lynx178_init_osabi(struct gdbarch_info info, struct gdbarch *gdbarch)
void regcache_raw_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:640
enum register_status regcache_cooked_read(struct regcache *regcache, int regnum, gdb_byte *buf)
Definition: regcache.c:661
unsigned long long ULONGEST
Definition: common-types.h:53
void target_store_registers(struct regcache *regcache, int regno)
Definition: target.c:3445
int register_size(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:164
static CORE_ADDR rs6000_lynx178_push_dummy_call(struct gdbarch *gdbarch, struct value *function, struct regcache *regcache, CORE_ADDR bp_addr, int nargs, struct value **args, CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr)
void set_gdbarch_long_double_bit(struct gdbarch *gdbarch, int long_double_bit)
Definition: gdbarch.c:1756
struct type * value_type(const struct value *value)
Definition: value.c:1095
void set_gdbarch_return_value(struct gdbarch *gdbarch, gdbarch_return_value_ftype return_value)
Definition: gdbarch.c:2738
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
void set_gdbarch_push_dummy_call(struct gdbarch *gdbarch, gdbarch_push_dummy_call_ftype push_dummy_call)
Definition: gdbarch.c:2381
void write_memory(CORE_ADDR memaddr, const bfd_byte *myaddr, ssize_t len)
Definition: corefile.c:394
int ppc_floating_point_unit_p(struct gdbarch *gdbarch)
Definition: rs6000-tdep.c:218
int ppc_fp0_regnum
Definition: ppc-tdep.h:240
#define wordsize
gdb_osabi
Definition: defs.h:508
void regcache_cooked_write(struct regcache *regcache, int regnum, const gdb_byte *buf)
Definition: regcache.c:873
static void store_unsigned_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, ULONGEST val)
Definition: defs.h:604