125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Function return value location for Linux/SH ABI.
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   Copyright (C) 2010, 2014 Red Hat, Inc.
303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is part of elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Contributed by Matt Fleming <matt@console-pimps.org>.
525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is free software; you can redistribute it and/or modify
703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   it under the terms of either
825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes     * the GNU Lesser General Public License as published by the Free
1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       Software Foundation; either version 3 of the License, or (at
1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       your option) any later version
1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   or
1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes     * the GNU General Public License as published by the Free
1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       Software Foundation; either version 2 of the License, or (at
1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       your option) any later version
1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   or both in parallel, as here.
2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   elfutils is distributed in the hope that it will be useful, but
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   You should have received copies of the GNU General Public License and
2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   the GNU Lesser General Public License along with this program.  If
2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   not, see <http://www.gnu.org/licenses/>.  */
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h>
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <assert.h>
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <dwarf.h>
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define BACKEND sh_
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libebl_CPU.h"
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* This is the SVR4 ELF ABI convention, but AIX and Linux do not use it.  */
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define SVR4_STRUCT_RETURN 0
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* r0, or pair r0, r1.  */
4625b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_intreg[] =
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg0 }, { .atom = DW_OP_piece, .number = 4 },
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg1 }, { .atom = DW_OP_piece, .number = 4 },
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intreg	1
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intregpair	4
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* fr0 or fr1.  */
5525b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_fpreg[] =
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg25 }, { .atom = DW_OP_piece, .number = 4 },
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg26 }, { .atom = DW_OP_piece, .number = 4 },
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_fpreg	1
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_fpregpair	2
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6325b3c049e70834cf33790a28643ab058b507b35cBen Chengint
6425b3c049e70834cf33790a28643ab058b507b35cBen Chengsh_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Start with the function's type, and get the DW_AT_type attribute,
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng     which is the type of the return value.  */
6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die die_mem, *typedie = &die_mem;
6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  int tag = dwarf_peeled_die_type (functypedie, typedie);
7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (tag <= 0)
7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    return tag;
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Word size;
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  switch (tag)
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case -1:
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return -1;
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_subrange_type:
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  Dwarf_Attribute attr_mem, *attr;
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  typedie = dwarf_formref_die (attr, &die_mem);
8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  tag = DWARF_TAG_OR_RETURN (typedie);
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Fall through.  */
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_base_type:
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_enumeration_type:
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_pointer_type:
9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_ptr_to_member_type:
9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      {
9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	Dwarf_Attribute attr_mem;
9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes						   &attr_mem), &size) != 0)
9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  {
9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
9903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      size = 4;
10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    else
10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      return -1;
10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  }
10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      }
10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (size <= 8)
10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  if (tag == DW_TAG_base_type)
10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    {
10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      Dwarf_Attribute attr_mem;
11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      Dwarf_Word encoding;
11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (dwarf_formudata (dwarf_attr_integrate (typedie,
11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng							 DW_AT_encoding,
11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng							 &attr_mem),
11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng				   &encoding) != 0)
11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng		return -1;
11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (encoding == DW_ATE_float)
11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		{
11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  *locp = loc_fpreg;
11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  return size <= 4 ? nloc_fpreg : nloc_fpregpair;
12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng		}
12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    }
12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  *locp = loc_intreg;
12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  return size <= 4 ? nloc_intreg : nloc_intregpair;
12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* XXX We don't have a good way to return specific errors from ebl calls.
12825b3c049e70834cf33790a28643ab058b507b35cBen Cheng     This value means we do not understand the type, but it is well-formed
12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng     DWARF and might be valid.  */
13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return -2;
13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
132