ppc_retval.c revision 25b3c049e70834cf33790a28643ab058b507b35c
1cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* Function return value location for Linux/PPC ABI.
2cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Copyright (C) 2005, 2006, 2007 Red Hat, Inc.
3cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   This file is part of Red Hat elfutils.
4cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
5cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
6cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   it under the terms of the GNU General Public License as published by the
7cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Free Software Foundation; version 2 of the License.
8cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
9cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
10cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
11cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   General Public License for more details.
13cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
14cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   You should have received a copy of the GNU General Public License along
15cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
16cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
18cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Red Hat elfutils is an included package of the Open Invention Network.
19cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   An included package of the Open Invention Network is a package for which
20cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Open Invention Network licensees cross-license their patents.  No patent
21cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   license is granted, either expressly or impliedly, by designation as an
22cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   included package.  Should you wish to participate in the Open Invention
23cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   Network licensing program, please visit www.openinventionnetwork.com
24cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   <http://www.openinventionnetwork.com>.  */
25cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
26cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#ifdef HAVE_CONFIG_H
27cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng# include <config.h>
28cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#endif
29cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
30cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include <assert.h>
31cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include <dwarf.h>
32cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
33cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#define BACKEND ppc_
34cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#include "libebl_CPU.h"
35cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
36cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
37cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* This is the SVR4 ELF ABI convention, but AIX and Linux do not use it.  */
38cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#define SVR4_STRUCT_RETURN 0
39cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
40cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
41cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* r3, or pair r3, r4.  */
42cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengstatic const Dwarf_Op loc_intreg[] =
43cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  {
44cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    { .atom = DW_OP_reg3 }, { .atom = DW_OP_piece, .number = 4 },
45cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    { .atom = DW_OP_reg4 }, { .atom = DW_OP_piece, .number = 4 },
46cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  };
47cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#define nloc_intreg	1
48cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#define nloc_intregpair	4
49cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
50cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* f1.  */
51cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengstatic const Dwarf_Op loc_fpreg[] =
52cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  {
53cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    { .atom = DW_OP_regx, .number = 33 }
54cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  };
55cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#define nloc_fpreg	1
56cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
57cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng/* The return value is a structure and is actually stored in stack space
58cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   passed in a hidden argument by the caller.  But, the compiler
59cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng   helpfully returns the address of that space in r3.  */
60cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengstatic const Dwarf_Op loc_aggregate[] =
61cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  {
62cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    { .atom = DW_OP_breg3, .number = 0 }
63cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  };
64cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng#define nloc_aggregate 1
65cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
66cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
67cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengint
68cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Chengppc_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
69cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng{
70cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  /* Start with the function's type, and get the DW_AT_type attribute,
71cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng     which is the type of the return value.  */
72cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
73cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwarf_Attribute attr_mem;
74cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwarf_Attribute *attr = dwarf_attr_integrate (functypedie, DW_AT_type,
75cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng						&attr_mem);
76cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  if (attr == NULL)
77cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    /* The function has no return value, like a `void' function in C.  */
78cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    return 0;
79cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
80cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwarf_Die die_mem;
81cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwarf_Die *typedie = dwarf_formref_die (attr, &die_mem);
82cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  int tag = dwarf_tag (typedie);
83cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
84cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  /* Follow typedefs and qualifiers to get to the actual type.  */
85cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  while (tag == DW_TAG_typedef
86cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	 || tag == DW_TAG_const_type || tag == DW_TAG_volatile_type
87cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	 || tag == DW_TAG_restrict_type || tag == DW_TAG_mutable_type)
88cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    {
89cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
90cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      typedie = dwarf_formref_die (attr, &die_mem);
91cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      tag = dwarf_tag (typedie);
92cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    }
93cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
94cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  Dwarf_Word size;
95cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  switch (tag)
96cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    {
97cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case -1:
98cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      return -1;
99cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
100cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_subrange_type:
101cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
102cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	{
103cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
104cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  typedie = dwarf_formref_die (attr, &die_mem);
105cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  tag = dwarf_tag (typedie);
106cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	}
107cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      /* Fall through.  */
108cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
109cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_base_type:
110cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_enumeration_type:
111cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_pointer_type:
112cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_ptr_to_member_type:
113cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
114cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng						 &attr_mem), &size) != 0)
115cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	{
116cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
117cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    size = 4;
118cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  else
119cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    return -1;
120cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	}
121cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (size <= 8)
122cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	{
123cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  if (tag == DW_TAG_base_type)
124cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    {
125cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	      Dwarf_Word encoding;
126cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	      if (dwarf_formudata (dwarf_attr_integrate (typedie,
127cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng							 DW_AT_encoding,
128cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng							 &attr_mem),
129cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng				   &encoding) != 0)
130cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng		return -1;
131cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	      if (encoding == DW_ATE_float)
132cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng		{
133cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng		  *locp = loc_fpreg;
134cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng		  return nloc_fpreg;
135cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng		}
136cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	    }
137cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	intreg:
138cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  *locp = loc_intreg;
139cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  return size <= 4 ? nloc_intreg : nloc_intregpair;
140cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	}
141cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
142cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    aggregate:
143cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      *locp = loc_aggregate;
144cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      return nloc_aggregate;
145cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
146cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_structure_type:
147cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_class_type:
148cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_union_type:
149cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    case DW_TAG_array_type:
150cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      if (SVR4_STRUCT_RETURN
151cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  && dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
152cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng						    &attr_mem), &size) == 0
153cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	  && size > 0 && size <= 8)
154cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng	goto intreg;
155cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng      goto aggregate;
156cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng    }
157cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng
158cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  /* XXX We don't have a good way to return specific errors from ebl calls.
159cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng     This value means we do not understand the type, but it is well-formed
160cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng     DWARF and might be valid.  */
161cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng  return -2;
162cc6695e2684ce93cdf8bd2da63d55d2cf49ff076Ben Cheng}
163