125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Function return value location for SPARC.
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   Copyright (C) 2006-2010, 2014 Red Hat, Inc.
303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is part of elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is free software; you can redistribute it and/or modify
603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   it under the terms of either
725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes     * the GNU Lesser General Public License as published by the Free
903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       Software Foundation; either version 3 of the License, or (at
1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       your option) any later version
1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   or
1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes     * the GNU General Public License as published by the Free
1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       Software Foundation; either version 2 of the License, or (at
1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes       your option) any later version
1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   or both in parallel, as here.
1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   elfutils is distributed in the hope that it will be useful, but
2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   You should have received copies of the GNU General Public License and
2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   the GNU Lesser General Public License along with this program.  If
2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   not, see <http://www.gnu.org/licenses/>.  */
2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h>
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <assert.h>
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <dwarf.h>
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define BACKEND sparc_
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libebl_CPU.h"
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* %o0, or pair %o0, %o1.  */
4125b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_intreg[] =
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg8 }, { .atom = DW_OP_piece, .number = 4 },
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg9 }, { .atom = DW_OP_piece, .number = 4 },
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intreg	1
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intregpair	4
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* %f0 or pair %f0, %f1, or quad %f0..%f3.  */
5025b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_fpreg[] =
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_regx, .number = 32 }, { .atom = DW_OP_piece, .number = 4 },
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_regx, .number = 33 }, { .atom = DW_OP_piece, .number = 4 },
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_regx, .number = 34 }, { .atom = DW_OP_piece, .number = 4 },
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_regx, .number = 35 }, { .atom = DW_OP_piece, .number = 4 },
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_fpreg	1
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_fpregpair	4
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_fpregquad	8
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* The return value is a structure and is actually stored in stack space
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   passed in a hidden argument by the caller.  But, the compiler
6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   helpfully returns the address of that space in %o0.  */
6425b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_aggregate[] =
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_breg8, .number = 0 }
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_aggregate 1
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7025b3c049e70834cf33790a28643ab058b507b35cBen Chengint
7125b3c049e70834cf33790a28643ab058b507b35cBen Chengsparc_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Start with the function's type, and get the DW_AT_type attribute,
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng     which is the type of the return value.  */
7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die die_mem, *typedie = &die_mem;
7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  int tag = dwarf_peeled_die_type (functypedie, typedie);
7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (tag <= 0)
7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    return tag;
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Word size;
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  switch (tag)
8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case -1:
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return -1;
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_subrange_type:
8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  Dwarf_Attribute attr_mem, *attr;
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  typedie = dwarf_formref_die (attr, &die_mem);
9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  tag = DWARF_TAG_OR_RETURN (typedie);
9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Fall through.  */
9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_base_type:
9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_enumeration_type:
9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_pointer_type:
9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_ptr_to_member_type:
10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      {
10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	Dwarf_Attribute attr_mem;
10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes						   &attr_mem), &size) != 0)
10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  {
10503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    uint8_t asize;
10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    Dwarf_Die cudie;
10703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    if ((tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
10803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes		&& dwarf_diecu (typedie, &cudie, &asize, NULL) != NULL)
10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      size = asize;
11003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    else
11103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      return -1;
11203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  }
11303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      }
11403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (tag == DW_TAG_base_type)
11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
11703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  Dwarf_Attribute attr_mem;
11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  Dwarf_Word encoding;
11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding,
12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng						     &attr_mem),
12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng			       &encoding) != 0)
12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    return -1;
12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  if (encoding == DW_ATE_float)
12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    {
12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      *locp = loc_fpreg;
12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (size <= 4)
12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		return nloc_fpreg;
12825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (size <= 8)
12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		return nloc_fpregpair;
13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (size <= 16)
13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng		return nloc_fpregquad;
13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    }
13325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (size <= 8)
13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	intreg:
13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  *locp = loc_intreg;
13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  return size <= 4 ? nloc_intreg : nloc_intregpair;
13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng    aggregate:
14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      *locp = loc_aggregate;
14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return nloc_aggregate;
14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_structure_type:
14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_class_type:
14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_union_type:
14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_array_type:
14925b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (dwarf_aggregate_size (typedie, &size) == 0
15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  && size > 0 && size <= 8)
15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	goto intreg;
15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      goto aggregate;
15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* XXX We don't have a good way to return specific errors from ebl calls.
15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng     This value means we do not understand the type, but it is well-formed
15725b3c049e70834cf33790a28643ab058b507b35cBen Cheng     DWARF and might be valid.  */
15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return -2;
15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
160