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