blob: 096d955d683882f09a396f81c907c4d4004aebb1 [file] [log] [blame]
Ben Cheng25b3c042013-11-20 14:45:36 -08001/* Function return value location for Linux/SH ABI.
2 Copyright (C) 2010 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Contributed by Matt Fleming <matt@console-pimps.org>.
5
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
9
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19 Red Hat elfutils is an included package of the Open Invention Network.
20 An included package of the Open Invention Network is a package for which
21 Open Invention Network licensees cross-license their patents. No patent
22 license is granted, either expressly or impliedly, by designation as an
23 included package. Should you wish to participate in the Open Invention
24 Network licensing program, please visit www.openinventionnetwork.com
25 <http://www.openinventionnetwork.com>. */
26
27#ifdef HAVE_CONFIG_H
28# include <config.h>
29#endif
30
31#include <assert.h>
32#include <dwarf.h>
33
34#define BACKEND sh_
35#include "libebl_CPU.h"
36
37
38/* This is the SVR4 ELF ABI convention, but AIX and Linux do not use it. */
39#define SVR4_STRUCT_RETURN 0
40
41
42/* r0, or pair r0, r1. */
43static const Dwarf_Op loc_intreg[] =
44 {
45 { .atom = DW_OP_reg0 }, { .atom = DW_OP_piece, .number = 4 },
46 { .atom = DW_OP_reg1 }, { .atom = DW_OP_piece, .number = 4 },
47 };
48#define nloc_intreg 1
49#define nloc_intregpair 4
50
51/* fr0 or fr1. */
52static const Dwarf_Op loc_fpreg[] =
53 {
54 { .atom = DW_OP_reg25 }, { .atom = DW_OP_piece, .number = 4 },
55 { .atom = DW_OP_reg26 }, { .atom = DW_OP_piece, .number = 4 },
56 };
57#define nloc_fpreg 1
58#define nloc_fpregpair 2
59
60int
61sh_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
62{
63 /* Start with the function's type, and get the DW_AT_type attribute,
64 which is the type of the return value. */
65
66 Dwarf_Attribute attr_mem;
67 Dwarf_Attribute *attr = dwarf_attr_integrate (functypedie, DW_AT_type,
68 &attr_mem);
69 if (attr == NULL)
70 /* The function has no return value, like a `void' function in C. */
71 return 0;
72
73 Dwarf_Die die_mem;
74 Dwarf_Die *typedie = dwarf_formref_die (attr, &die_mem);
75 int tag = dwarf_tag (typedie);
76
77 /* Follow typedefs and qualifiers to get to the actual type. */
78 while (tag == DW_TAG_typedef
79 || tag == DW_TAG_const_type || tag == DW_TAG_volatile_type
80 || tag == DW_TAG_restrict_type || tag == DW_TAG_mutable_type)
81 {
82 attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
83 typedie = dwarf_formref_die (attr, &die_mem);
84 tag = dwarf_tag (typedie);
85 }
86
87 Dwarf_Word size;
88 switch (tag)
89 {
90 case -1:
91 return -1;
92
93 case DW_TAG_subrange_type:
94 if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
95 {
96 attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
97 typedie = dwarf_formref_die (attr, &die_mem);
98 tag = dwarf_tag (typedie);
99 }
100 /* Fall through. */
101
102 case DW_TAG_base_type:
103 case DW_TAG_enumeration_type:
104 case DW_TAG_pointer_type:
105 case DW_TAG_ptr_to_member_type:
106 if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
107 &attr_mem), &size) != 0)
108 {
109 if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
110 size = 4;
111 else
112 return -1;
113 }
114 if (size <= 8)
115 {
116 if (tag == DW_TAG_base_type)
117 {
118 Dwarf_Word encoding;
119 if (dwarf_formudata (dwarf_attr_integrate (typedie,
120 DW_AT_encoding,
121 &attr_mem),
122 &encoding) != 0)
123 return -1;
124 if (encoding == DW_ATE_float)
125 {
126 *locp = loc_fpreg;
127 return size <= 4 ? nloc_fpreg : nloc_fpregpair;
128 }
129 }
130 *locp = loc_intreg;
131 return size <= 4 ? nloc_intreg : nloc_intregpair;
132 }
133 }
134
135 /* XXX We don't have a good way to return specific errors from ebl calls.
136 This value means we do not understand the type, but it is well-formed
137 DWARF and might be valid. */
138 return -2;
139}