125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Function return value location for Linux/PPC ABI.
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   Copyright (C) 2005, 2006, 2007, 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 ppc_
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libebl_CPU.h"
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* This is the SVR4 ELF ABI convention, but AIX and Linux do not use it.  */
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define SVR4_STRUCT_RETURN 0
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* r3, or pair r3, r4, or quad r3-r6.  */
4525b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_intreg[] =
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg3 }, { .atom = DW_OP_piece, .number = 4 },
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg4 }, { .atom = DW_OP_piece, .number = 4 },
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg5 }, { .atom = DW_OP_piece, .number = 4 },
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_reg6 }, { .atom = DW_OP_piece, .number = 4 },
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intreg	1
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intregpair	4
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_intregquad	8
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* f1.  */
5725b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_fpreg[] =
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_regx, .number = 33 }
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_fpreg	1
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* vr2.  */
6425b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_vmxreg[] =
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_regx, .number = 1124 + 2 }
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_vmxreg	1
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* The return value is a structure and is actually stored in stack space
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   passed in a hidden argument by the caller.  But, the compiler
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   helpfully returns the address of that space in r3.  */
7325b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic const Dwarf_Op loc_aggregate[] =
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  {
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    { .atom = DW_OP_breg3, .number = 0 }
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  };
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define nloc_aggregate 1
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* XXX We should check the SHT_GNU_ATTRIBUTES bits here (or in ppc_init).  */
8125b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic bool
8225b3c049e70834cf33790a28643ab058b507b35cBen Chengppc_altivec_abi (void)
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return true;
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8725b3c049e70834cf33790a28643ab058b507b35cBen Chengint
8825b3c049e70834cf33790a28643ab058b507b35cBen Chengppc_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Start with the function's type, and get the DW_AT_type attribute,
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng     which is the type of the return value.  */
9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf_Die die_mem, *typedie = &die_mem;
9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  int tag = dwarf_peeled_die_type (functypedie, typedie);
9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (tag <= 0)
9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    return tag;
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Word size;
9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  switch (tag)
9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case -1:
10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return -1;
10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_subrange_type:
10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  Dwarf_Attribute attr_mem, *attr;
10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  typedie = dwarf_formref_die (attr, &die_mem);
10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  tag = DWARF_TAG_OR_RETURN (typedie);
11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Fall through.  */
11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_base_type:
11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_enumeration_type:
11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_pointer_type:
11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_ptr_to_member_type:
11703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      {
11803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	Dwarf_Attribute attr_mem;
11903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
12003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes						   &attr_mem), &size) != 0)
12103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  {
12203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
12303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      size = 4;
12403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	    else
12503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      return -1;
12603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	  }
12703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      }
12803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (size <= 8)
13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	{
13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  if (tag == DW_TAG_base_type)
13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    {
13303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	      Dwarf_Attribute attr_mem;
13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      Dwarf_Word encoding;
13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (dwarf_formudata (dwarf_attr_integrate (typedie,
13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng							 DW_AT_encoding,
13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng							 &attr_mem),
13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng				   &encoding) != 0)
13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		return -1;
14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (encoding == DW_ATE_float)
14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng		{
14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  *locp = loc_fpreg;
14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  return nloc_fpreg;
14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng		}
14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    }
14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	intreg:
14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  *locp = loc_intreg;
14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  return size <= 4 ? nloc_intreg : nloc_intregpair;
14925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	}
15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng    aggregate:
15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng      *locp = loc_aggregate;
15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return nloc_aggregate;
15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_array_type:
15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      {
15703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes	Dwarf_Attribute attr_mem;
15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng	bool is_vector;
15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	if (dwarf_formflag (dwarf_attr_integrate (typedie, DW_AT_GNU_vector,
16025b3c049e70834cf33790a28643ab058b507b35cBen Cheng						  &attr_mem), &is_vector) == 0
16125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    && is_vector
16225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    && dwarf_aggregate_size (typedie, &size) == 0)
16325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  switch (size)
16425b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    {
16525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    case 16:
16625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      if (ppc_altivec_abi ())
16725b3c049e70834cf33790a28643ab058b507b35cBen Cheng		{
16825b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  *locp = loc_vmxreg;
16925b3c049e70834cf33790a28643ab058b507b35cBen Cheng		  return nloc_vmxreg;
17025b3c049e70834cf33790a28643ab058b507b35cBen Cheng		}
17125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      *locp = loc_intreg;
17225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	      return nloc_intregquad;
17325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	    }
17425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      }
17525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      /* Fall through.  */
17625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
17725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_structure_type:
17825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_class_type:
17925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_TAG_union_type:
18025b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (SVR4_STRUCT_RETURN
18125b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  && dwarf_aggregate_size (typedie, &size) == 0
18225b3c049e70834cf33790a28643ab058b507b35cBen Cheng	  && size > 0 && size <= 8)
18325b3c049e70834cf33790a28643ab058b507b35cBen Cheng	goto intreg;
18425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      goto aggregate;
18525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
18625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
18725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* XXX We don't have a good way to return specific errors from ebl calls.
18825b3c049e70834cf33790a28643ab058b507b35cBen Cheng     This value means we do not understand the type, but it is well-formed
18925b3c049e70834cf33790a28643ab058b507b35cBen Cheng     DWARF and might be valid.  */
19025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return -2;
19125b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
192