Coverage Report

Created: 2025-07-11 06:46

/src/elfutils/backends/aarch64_retval.c
Line
Count
Source (jump to first uncovered line)
1
/* Function return value location for Linux/AArch64 ABI.
2
   Copyright (C) 2013 Red Hat, Inc.
3
   This file is part of elfutils.
4
5
   This file is free software; you can redistribute it and/or modify
6
   it under the terms of either
7
8
     * the GNU Lesser General Public License as published by the Free
9
       Software Foundation; either version 3 of the License, or (at
10
       your option) any later version
11
12
   or
13
14
     * the GNU General Public License as published by the Free
15
       Software Foundation; either version 2 of the License, or (at
16
       your option) any later version
17
18
   or both in parallel, as here.
19
20
   elfutils is distributed in the hope that it will be useful, but
21
   WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
   General Public License for more details.
24
25
   You should have received copies of the GNU General Public License and
26
   the GNU Lesser General Public License along with this program.  If
27
   not, see <http://www.gnu.org/licenses/>.  */
28
29
#ifdef HAVE_CONFIG_H
30
# include <config.h>
31
#endif
32
33
#include <stdio.h>
34
#include <inttypes.h>
35
36
#include <assert.h>
37
#include <dwarf.h>
38
39
#define BACKEND aarch64_
40
#include "libebl_CPU.h"
41
42
static int
43
skip_until (Dwarf_Die *child, int tag)
44
0
{
45
0
  int i;
46
0
  while (DWARF_TAG_OR_RETURN (child) != tag)
47
0
    if ((i = dwarf_siblingof (child, child)) != 0)
48
      /* If there are no members, then this is not a HFA.  Errors
49
   are propagated.  */
50
0
      return i;
51
0
  return 0;
52
0
}
53
54
static int
55
dwarf_bytesize_aux (Dwarf_Die *die, Dwarf_Word *sizep)
56
0
{
57
0
  int bits;
58
0
  if (((bits = 8 * dwarf_bytesize (die)) < 0
59
0
       && (bits = dwarf_bitsize (die)) < 0)
60
0
      || bits % 8 != 0)
61
0
    return -1;
62
63
0
  *sizep = bits / 8;
64
0
  return 0;
65
0
}
66
67
/* HFA (Homogeneous Floating-point Aggregate) is an aggregate type
68
   whose members are all of the same floating-point type, which is
69
   then base type of this HFA.  Instead of being floating-point types
70
   directly, members can instead themselves be HFA.  Such HFA fields
71
   are handled as if their type were HFA base type.
72
73
   This function returns 0 if TYPEDIE is HFA, 1 if it is not, or -1 if
74
   there were errors.  In the former case, *SIZEP contains byte size
75
   of the base type (e.g. 8 for IEEE double).  *COUNT is set to the
76
   number of leaf members of the HFA.  */
77
static int hfa_type (Dwarf_Die *ftypedie, int tag,
78
         Dwarf_Word *sizep, Dwarf_Word *countp);
79
80
/* Return 0 if MEMBDIE refers to a member with a floating-point or HFA
81
   type, or 1 if it's not.  Return -1 for errors.  The meaning of the
82
   remaining arguments is as documented at hfa_type.  */
83
static int
84
member_is_fp (Dwarf_Die *membdie, Dwarf_Word *sizep, Dwarf_Word *countp)
85
0
{
86
0
  Dwarf_Die typedie;
87
0
  int tag = dwarf_peeled_die_type (membdie, &typedie);
88
0
  switch (tag)
89
0
    {
90
0
    case DW_TAG_base_type:;
91
0
      Dwarf_Word encoding;
92
0
      Dwarf_Attribute attr_mem;
93
0
      if (dwarf_attr_integrate (&typedie, DW_AT_encoding, &attr_mem) == NULL
94
0
    || dwarf_formudata (&attr_mem, &encoding) != 0)
95
0
  return -1;
96
97
0
      switch (encoding)
98
0
  {
99
0
  case DW_ATE_complex_float:
100
0
    *countp = 2;
101
0
    break;
102
103
0
  case DW_ATE_float:
104
0
    *countp = 1;
105
0
    break;
106
107
0
  default:
108
0
    return 1;
109
0
  }
110
111
0
      if (dwarf_bytesize_aux (&typedie, sizep) < 0)
112
0
  return -1;
113
114
0
      *sizep /= *countp;
115
0
      return 0;
116
117
0
    case DW_TAG_structure_type:
118
0
    case DW_TAG_union_type:
119
0
    case DW_TAG_array_type:
120
0
      return hfa_type (&typedie, tag, sizep, countp);
121
0
    }
122
123
0
  return 1;
124
0
}
125
126
static int
127
hfa_type (Dwarf_Die *ftypedie, int tag, Dwarf_Word *sizep, Dwarf_Word *countp)
128
0
{
129
0
  assert (tag == DW_TAG_structure_type || tag == DW_TAG_class_type
130
0
    || tag == DW_TAG_union_type || tag == DW_TAG_array_type);
131
132
0
  int i;
133
0
  if (tag == DW_TAG_array_type)
134
0
    {
135
0
      Dwarf_Word tot_size;
136
0
      if (dwarf_aggregate_size (ftypedie, &tot_size) < 0)
137
0
  return -1;
138
139
      /* For vector types, we don't care about the underlying
140
   type, but only about the vector type itself.  */
141
0
      bool vec;
142
0
      Dwarf_Attribute attr_mem;
143
0
      if (dwarf_formflag (dwarf_attr_integrate (ftypedie, DW_AT_GNU_vector,
144
0
            &attr_mem), &vec) == 0
145
0
    && vec)
146
0
  {
147
0
    *sizep = tot_size;
148
0
    *countp = 1;
149
150
0
    return 0;
151
0
  }
152
153
0
      if ((i = member_is_fp (ftypedie, sizep, countp)) == 0)
154
0
  {
155
0
    *countp = tot_size / *sizep;
156
0
    return 0;
157
0
  }
158
159
0
      return i;
160
0
    }
161
162
  /* Find first DW_TAG_member and determine its type.  */
163
0
  Dwarf_Die member;
164
0
  if ((i = dwarf_child (ftypedie, &member) != 0))
165
0
    return i;
166
167
0
  if ((i = skip_until (&member, DW_TAG_member)) != 0)
168
0
    return i;
169
170
0
  *countp = 0;
171
0
  if ((i = member_is_fp (&member, sizep, countp)) != 0)
172
0
    return i;
173
174
0
  while ((i = dwarf_siblingof (&member, &member)) == 0
175
0
   && (i = skip_until (&member, DW_TAG_member)) == 0)
176
0
    {
177
0
      Dwarf_Word size, count;
178
0
      if ((i = member_is_fp (&member, &size, &count)) != 0)
179
0
  return i;
180
181
0
      if (*sizep != size)
182
0
  return 1;
183
184
0
      *countp += count;
185
0
    }
186
187
  /* At this point we already have at least one FP member, which means
188
     FTYPEDIE is an HFA.  So either return 0, or propagate error.  */
189
0
  return i < 0 ? i : 0;
190
0
}
191
192
static int
193
pass_in_gpr (const Dwarf_Op **locp, Dwarf_Word size)
194
0
{
195
0
  static const Dwarf_Op loc[] =
196
0
    {
197
0
      { .atom = DW_OP_reg0 }, { .atom = DW_OP_piece, .number = 8 },
198
0
      { .atom = DW_OP_reg1 }, { .atom = DW_OP_piece, .number = 8 }
199
0
    };
200
201
0
  *locp = loc;
202
0
  return size <= 8 ? 1 : 4;
203
0
}
204
205
static int
206
pass_by_ref (const Dwarf_Op **locp)
207
0
{
208
0
  static const Dwarf_Op loc[] = { { .atom = DW_OP_breg0 } };
209
210
0
  *locp = loc;
211
0
  return 1;
212
0
}
213
214
static int
215
pass_hfa (const Dwarf_Op **locp, Dwarf_Word size, Dwarf_Word count)
216
0
{
217
0
  assert (count >= 1 && count <= 4);
218
0
  assert (size == 2 || size == 4 || size == 8 || size == 16);
219
220
0
#define DEFINE_FPREG(NAME, SIZE)    \
221
0
  static const Dwarf_Op NAME[] = {    \
222
0
    { .atom = DW_OP_regx, .number = 64 }, \
223
0
    { .atom = DW_OP_piece, .number = SIZE },  \
224
0
    { .atom = DW_OP_regx, .number = 65 }, \
225
0
    { .atom = DW_OP_piece, .number = SIZE },  \
226
0
    { .atom = DW_OP_regx, .number = 66 }, \
227
0
    { .atom = DW_OP_piece, .number = SIZE },  \
228
0
    { .atom = DW_OP_regx, .number = 67 }, \
229
0
    { .atom = DW_OP_piece, .number = SIZE } \
230
0
  }
231
232
0
  switch (size)
233
0
    {
234
0
    case 2:;
235
0
      DEFINE_FPREG (loc_hfa_2, 2);
236
0
      *locp = loc_hfa_2;
237
0
      break;
238
239
0
    case 4:;
240
0
      DEFINE_FPREG (loc_hfa_4, 4);
241
0
      *locp = loc_hfa_4;
242
0
      break;
243
244
0
    case 8:;
245
0
      DEFINE_FPREG (loc_hfa_8, 8);
246
0
      *locp = loc_hfa_8;
247
0
      break;
248
249
0
    case 16:;
250
0
      DEFINE_FPREG (loc_hfa_16, 16);
251
0
      *locp = loc_hfa_16;
252
0
      break;
253
0
    }
254
0
#undef DEFINE_FPREG
255
256
0
  return count == 1 ? 1 : 2 * count;
257
0
}
258
259
static int
260
pass_in_simd (const Dwarf_Op **locp)
261
0
{
262
  /* This is like passing single-element HFA.  Size doesn't matter, so
263
     pretend it's for example double.  */
264
0
  return pass_hfa (locp, 8, 1);
265
0
}
266
267
int
268
aarch64_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
269
0
{
270
  /* Start with the function's type, and get the DW_AT_type attribute,
271
     which is the type of the return value.  */
272
0
  Dwarf_Die typedie;
273
0
  int tag = dwarf_peeled_die_type (functypedie, &typedie);
274
0
  if (tag <= 0)
275
0
    return tag;
276
277
0
  Dwarf_Word size = (Dwarf_Word)-1;
278
279
  /* If the argument type is a Composite Type that is larger than 16
280
     bytes, then the argument is copied to memory allocated by the
281
     caller and the argument is replaced by a pointer to the copy.  */
282
0
  if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type
283
0
      || tag == DW_TAG_class_type || tag == DW_TAG_array_type)
284
0
    {
285
0
      Dwarf_Word base_size, count;
286
0
      switch (hfa_type (&typedie, tag, &base_size, &count))
287
0
  {
288
0
  default:
289
0
    return -1;
290
291
0
  case 0:
292
0
    assert (count > 0);
293
0
    if (count <= 4)
294
0
      return pass_hfa (locp, base_size, count);
295
0
    FALLTHROUGH;
296
297
0
  case 1:
298
    /* Not a HFA.  */
299
0
    if (dwarf_aggregate_size (&typedie, &size) < 0)
300
0
      return -1;
301
0
    if (size > 16)
302
0
      return pass_by_ref (locp);
303
0
  }
304
0
    }
305
306
0
  if (tag == DW_TAG_base_type || dwarf_is_pointer (tag))
307
0
    {
308
0
      if (dwarf_bytesize_aux (&typedie, &size) < 0)
309
0
  {
310
0
    if (dwarf_is_pointer (tag))
311
0
      size = 8;
312
0
    else
313
0
      return -1;
314
0
  }
315
316
0
      Dwarf_Attribute attr_mem;
317
0
      if (tag == DW_TAG_base_type)
318
0
  {
319
0
    Dwarf_Word encoding;
320
0
    if (dwarf_formudata (dwarf_attr_integrate (&typedie, DW_AT_encoding,
321
0
                 &attr_mem),
322
0
             &encoding) != 0)
323
0
      return -1;
324
325
0
    switch (encoding)
326
0
      {
327
        /* If the argument is a Half-, Single-, Double- or Quad-
328
     precision Floating-point [...] the argument is allocated
329
     to the least significant bits of register v[NSRN].  */
330
0
      case DW_ATE_float:
331
0
        switch (size)
332
0
    {
333
0
    case 2: /* half */
334
0
    case 4: /* single */
335
0
    case 8: /* double */
336
0
    case 16: /* quad */
337
0
      return pass_in_simd (locp);
338
339
0
    default:
340
0
      return -2;
341
0
    }
342
343
0
      case DW_ATE_complex_float:
344
0
        switch (size)
345
0
    {
346
0
    case 8: /* float _Complex */
347
0
    case 16: /* double _Complex */
348
0
    case 32: /* long double _Complex */
349
0
      return pass_hfa (locp, size / 2, 2);
350
351
0
    default:
352
0
      return -2;
353
0
    }
354
355
        /* If the argument is an Integral or Pointer Type, the
356
     size of the argument is less than or equal to 8 bytes
357
     [...] the argument is copied to the least significant
358
     bits in x[NGRN].  */
359
0
      case DW_ATE_boolean:
360
0
      case DW_ATE_signed:
361
0
      case DW_ATE_unsigned:
362
0
      case DW_ATE_unsigned_char:
363
0
      case DW_ATE_signed_char:
364
0
        return pass_in_gpr (locp, size);
365
0
      }
366
367
0
    return -2;
368
0
  }
369
0
      else
370
0
  return pass_in_gpr (locp, size);
371
0
    }
372
373
0
  *locp = NULL;
374
0
  return 0;
375
0
}