xref: /aosp_15_r20/external/elfutils/backends/ppc_retval.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Function return value location for Linux/PPC ABI.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2005, 2006, 2007, 2010, 2014 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker 
5*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker    it under the terms of either
7*7304104dSAndroid Build Coastguard Worker 
8*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
9*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
10*7304104dSAndroid Build Coastguard Worker        your option) any later version
11*7304104dSAndroid Build Coastguard Worker 
12*7304104dSAndroid Build Coastguard Worker    or
13*7304104dSAndroid Build Coastguard Worker 
14*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
15*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
16*7304104dSAndroid Build Coastguard Worker        your option) any later version
17*7304104dSAndroid Build Coastguard Worker 
18*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
19*7304104dSAndroid Build Coastguard Worker 
20*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
21*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
22*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
24*7304104dSAndroid Build Coastguard Worker 
25*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
26*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
27*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
28*7304104dSAndroid Build Coastguard Worker 
29*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
30*7304104dSAndroid Build Coastguard Worker # include <config.h>
31*7304104dSAndroid Build Coastguard Worker #endif
32*7304104dSAndroid Build Coastguard Worker 
33*7304104dSAndroid Build Coastguard Worker #include <assert.h>
34*7304104dSAndroid Build Coastguard Worker #include <dwarf.h>
35*7304104dSAndroid Build Coastguard Worker 
36*7304104dSAndroid Build Coastguard Worker #define BACKEND ppc_
37*7304104dSAndroid Build Coastguard Worker #include "libebl_CPU.h"
38*7304104dSAndroid Build Coastguard Worker 
39*7304104dSAndroid Build Coastguard Worker 
40*7304104dSAndroid Build Coastguard Worker /* This is the SVR4 ELF ABI convention, but AIX and Linux do not use it.  */
41*7304104dSAndroid Build Coastguard Worker #define SVR4_STRUCT_RETURN 0
42*7304104dSAndroid Build Coastguard Worker 
43*7304104dSAndroid Build Coastguard Worker 
44*7304104dSAndroid Build Coastguard Worker /* r3, or pair r3, r4, or quad r3-r6.  */
45*7304104dSAndroid Build Coastguard Worker static const Dwarf_Op loc_intreg[] =
46*7304104dSAndroid Build Coastguard Worker   {
47*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_reg3 }, { .atom = DW_OP_piece, .number = 4 },
48*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_reg4 }, { .atom = DW_OP_piece, .number = 4 },
49*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_reg5 }, { .atom = DW_OP_piece, .number = 4 },
50*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_reg6 }, { .atom = DW_OP_piece, .number = 4 },
51*7304104dSAndroid Build Coastguard Worker   };
52*7304104dSAndroid Build Coastguard Worker #define nloc_intreg	1
53*7304104dSAndroid Build Coastguard Worker #define nloc_intregpair	4
54*7304104dSAndroid Build Coastguard Worker #define nloc_intregquad	8
55*7304104dSAndroid Build Coastguard Worker 
56*7304104dSAndroid Build Coastguard Worker /* f1.  */
57*7304104dSAndroid Build Coastguard Worker static const Dwarf_Op loc_fpreg[] =
58*7304104dSAndroid Build Coastguard Worker   {
59*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_regx, .number = 33 }
60*7304104dSAndroid Build Coastguard Worker   };
61*7304104dSAndroid Build Coastguard Worker #define nloc_fpreg	1
62*7304104dSAndroid Build Coastguard Worker 
63*7304104dSAndroid Build Coastguard Worker /* vr2.  */
64*7304104dSAndroid Build Coastguard Worker static const Dwarf_Op loc_vmxreg[] =
65*7304104dSAndroid Build Coastguard Worker   {
66*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_regx, .number = 1124 + 2 }
67*7304104dSAndroid Build Coastguard Worker   };
68*7304104dSAndroid Build Coastguard Worker #define nloc_vmxreg	1
69*7304104dSAndroid Build Coastguard Worker 
70*7304104dSAndroid Build Coastguard Worker /* The return value is a structure and is actually stored in stack space
71*7304104dSAndroid Build Coastguard Worker    passed in a hidden argument by the caller.  But, the compiler
72*7304104dSAndroid Build Coastguard Worker    helpfully returns the address of that space in r3.  */
73*7304104dSAndroid Build Coastguard Worker static const Dwarf_Op loc_aggregate[] =
74*7304104dSAndroid Build Coastguard Worker   {
75*7304104dSAndroid Build Coastguard Worker     { .atom = DW_OP_breg3, .number = 0 }
76*7304104dSAndroid Build Coastguard Worker   };
77*7304104dSAndroid Build Coastguard Worker #define nloc_aggregate 1
78*7304104dSAndroid Build Coastguard Worker 
79*7304104dSAndroid Build Coastguard Worker 
80*7304104dSAndroid Build Coastguard Worker /* XXX We should check the SHT_GNU_ATTRIBUTES bits here (or in ppc_init).  */
81*7304104dSAndroid Build Coastguard Worker static bool
ppc_altivec_abi(void)82*7304104dSAndroid Build Coastguard Worker ppc_altivec_abi (void)
83*7304104dSAndroid Build Coastguard Worker {
84*7304104dSAndroid Build Coastguard Worker   return true;
85*7304104dSAndroid Build Coastguard Worker }
86*7304104dSAndroid Build Coastguard Worker 
87*7304104dSAndroid Build Coastguard Worker int
ppc_return_value_location(Dwarf_Die * functypedie,const Dwarf_Op ** locp)88*7304104dSAndroid Build Coastguard Worker ppc_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
89*7304104dSAndroid Build Coastguard Worker {
90*7304104dSAndroid Build Coastguard Worker   /* Start with the function's type, and get the DW_AT_type attribute,
91*7304104dSAndroid Build Coastguard Worker      which is the type of the return value.  */
92*7304104dSAndroid Build Coastguard Worker   Dwarf_Die die_mem, *typedie = &die_mem;
93*7304104dSAndroid Build Coastguard Worker   int tag = dwarf_peeled_die_type (functypedie, typedie);
94*7304104dSAndroid Build Coastguard Worker   if (tag <= 0)
95*7304104dSAndroid Build Coastguard Worker     return tag;
96*7304104dSAndroid Build Coastguard Worker 
97*7304104dSAndroid Build Coastguard Worker   Dwarf_Word size;
98*7304104dSAndroid Build Coastguard Worker   switch (tag)
99*7304104dSAndroid Build Coastguard Worker     {
100*7304104dSAndroid Build Coastguard Worker     case -1:
101*7304104dSAndroid Build Coastguard Worker       return -1;
102*7304104dSAndroid Build Coastguard Worker 
103*7304104dSAndroid Build Coastguard Worker     case DW_TAG_subrange_type:
104*7304104dSAndroid Build Coastguard Worker       if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
105*7304104dSAndroid Build Coastguard Worker 	{
106*7304104dSAndroid Build Coastguard Worker 	  Dwarf_Attribute attr_mem, *attr;
107*7304104dSAndroid Build Coastguard Worker 	  attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
108*7304104dSAndroid Build Coastguard Worker 	  typedie = dwarf_formref_die (attr, &die_mem);
109*7304104dSAndroid Build Coastguard Worker 	  tag = DWARF_TAG_OR_RETURN (typedie);
110*7304104dSAndroid Build Coastguard Worker 	}
111*7304104dSAndroid Build Coastguard Worker       FALLTHROUGH;
112*7304104dSAndroid Build Coastguard Worker 
113*7304104dSAndroid Build Coastguard Worker     case DW_TAG_base_type:
114*7304104dSAndroid Build Coastguard Worker     case DW_TAG_enumeration_type:
115*7304104dSAndroid Build Coastguard Worker     CASE_POINTER:
116*7304104dSAndroid Build Coastguard Worker       {
117*7304104dSAndroid Build Coastguard Worker 	Dwarf_Attribute attr_mem;
118*7304104dSAndroid Build Coastguard Worker 	if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
119*7304104dSAndroid Build Coastguard Worker 						   &attr_mem), &size) != 0)
120*7304104dSAndroid Build Coastguard Worker 	  {
121*7304104dSAndroid Build Coastguard Worker 	    if (dwarf_is_pointer (tag))
122*7304104dSAndroid Build Coastguard Worker 	      size = 4;
123*7304104dSAndroid Build Coastguard Worker 	    else
124*7304104dSAndroid Build Coastguard Worker 	      return -1;
125*7304104dSAndroid Build Coastguard Worker 	  }
126*7304104dSAndroid Build Coastguard Worker       }
127*7304104dSAndroid Build Coastguard Worker 
128*7304104dSAndroid Build Coastguard Worker       if (size <= 8)
129*7304104dSAndroid Build Coastguard Worker 	{
130*7304104dSAndroid Build Coastguard Worker 	  if (tag == DW_TAG_base_type)
131*7304104dSAndroid Build Coastguard Worker 	    {
132*7304104dSAndroid Build Coastguard Worker 	      Dwarf_Attribute attr_mem;
133*7304104dSAndroid Build Coastguard Worker 	      Dwarf_Word encoding;
134*7304104dSAndroid Build Coastguard Worker 	      if (dwarf_formudata (dwarf_attr_integrate (typedie,
135*7304104dSAndroid Build Coastguard Worker 							 DW_AT_encoding,
136*7304104dSAndroid Build Coastguard Worker 							 &attr_mem),
137*7304104dSAndroid Build Coastguard Worker 				   &encoding) != 0)
138*7304104dSAndroid Build Coastguard Worker 		return -1;
139*7304104dSAndroid Build Coastguard Worker 	      if (encoding == DW_ATE_float)
140*7304104dSAndroid Build Coastguard Worker 		{
141*7304104dSAndroid Build Coastguard Worker 		  *locp = loc_fpreg;
142*7304104dSAndroid Build Coastguard Worker 		  return nloc_fpreg;
143*7304104dSAndroid Build Coastguard Worker 		}
144*7304104dSAndroid Build Coastguard Worker 	    }
145*7304104dSAndroid Build Coastguard Worker 	intreg:
146*7304104dSAndroid Build Coastguard Worker 	  *locp = loc_intreg;
147*7304104dSAndroid Build Coastguard Worker 	  return size <= 4 ? nloc_intreg : nloc_intregpair;
148*7304104dSAndroid Build Coastguard Worker 	}
149*7304104dSAndroid Build Coastguard Worker 
150*7304104dSAndroid Build Coastguard Worker     aggregate:
151*7304104dSAndroid Build Coastguard Worker       *locp = loc_aggregate;
152*7304104dSAndroid Build Coastguard Worker       return nloc_aggregate;
153*7304104dSAndroid Build Coastguard Worker 
154*7304104dSAndroid Build Coastguard Worker     case DW_TAG_array_type:
155*7304104dSAndroid Build Coastguard Worker       {
156*7304104dSAndroid Build Coastguard Worker 	Dwarf_Attribute attr_mem;
157*7304104dSAndroid Build Coastguard Worker 	bool is_vector;
158*7304104dSAndroid Build Coastguard Worker 	if (dwarf_formflag (dwarf_attr_integrate (typedie, DW_AT_GNU_vector,
159*7304104dSAndroid Build Coastguard Worker 						  &attr_mem), &is_vector) == 0
160*7304104dSAndroid Build Coastguard Worker 	    && is_vector
161*7304104dSAndroid Build Coastguard Worker 	    && dwarf_aggregate_size (typedie, &size) == 0)
162*7304104dSAndroid Build Coastguard Worker 	  switch (size)
163*7304104dSAndroid Build Coastguard Worker 	    {
164*7304104dSAndroid Build Coastguard Worker 	    case 16:
165*7304104dSAndroid Build Coastguard Worker 	      if (ppc_altivec_abi ())
166*7304104dSAndroid Build Coastguard Worker 		{
167*7304104dSAndroid Build Coastguard Worker 		  *locp = loc_vmxreg;
168*7304104dSAndroid Build Coastguard Worker 		  return nloc_vmxreg;
169*7304104dSAndroid Build Coastguard Worker 		}
170*7304104dSAndroid Build Coastguard Worker 	      *locp = loc_intreg;
171*7304104dSAndroid Build Coastguard Worker 	      return nloc_intregquad;
172*7304104dSAndroid Build Coastguard Worker 	    }
173*7304104dSAndroid Build Coastguard Worker       }
174*7304104dSAndroid Build Coastguard Worker       FALLTHROUGH;
175*7304104dSAndroid Build Coastguard Worker 
176*7304104dSAndroid Build Coastguard Worker     case DW_TAG_structure_type:
177*7304104dSAndroid Build Coastguard Worker     case DW_TAG_class_type:
178*7304104dSAndroid Build Coastguard Worker     case DW_TAG_union_type:
179*7304104dSAndroid Build Coastguard Worker       if (SVR4_STRUCT_RETURN
180*7304104dSAndroid Build Coastguard Worker 	  && dwarf_aggregate_size (typedie, &size) == 0
181*7304104dSAndroid Build Coastguard Worker 	  && size > 0 && size <= 8)
182*7304104dSAndroid Build Coastguard Worker 	goto intreg;
183*7304104dSAndroid Build Coastguard Worker       goto aggregate;
184*7304104dSAndroid Build Coastguard Worker     }
185*7304104dSAndroid Build Coastguard Worker 
186*7304104dSAndroid Build Coastguard Worker   /* XXX We don't have a good way to return specific errors from ebl calls.
187*7304104dSAndroid Build Coastguard Worker      This value means we do not understand the type, but it is well-formed
188*7304104dSAndroid Build Coastguard Worker      DWARF and might be valid.  */
189*7304104dSAndroid Build Coastguard Worker   return -2;
190*7304104dSAndroid Build Coastguard Worker }
191