125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Convenience functions for handling DWARF descriptions of inline functions.
225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Copyright (C) 2005,2006 Red Hat, Inc.
325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   This file is part of Red Hat elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   it under the terms of the GNU General Public License as published by the
725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Free Software Foundation; version 2 of the License.
825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   You should have received a copy of the GNU General Public License along
1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   In addition, as a special exception, Red Hat, Inc. gives You the
1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   additional right to link the code of Red Hat elfutils with code licensed
2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   under an Open Source Initiative certified open source license
2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   (http://www.opensource.org/licenses/index.php) and to distribute linked
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   combinations including the two.  Non-GPL Code permitted under this
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception must only link to the code of Red Hat elfutils through those
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   well defined interfaces identified in the file named EXCEPTION found in
2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   the source code files (the "Approved Interfaces").  The files of Non-GPL
2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Code may instantiate templates or use macros or inline functions from
2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   the Approved Interfaces without causing the resulting work to be covered
2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   by the GNU General Public License.  Only Red Hat, Inc. may make changes
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   or additions to the list of Approved Interfaces.  Red Hat's grant of
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   this exception is conditioned upon your not adding any new exceptions.
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   If you wish to add a new Approved Interface or exception, please contact
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat.  You must obey the GNU General Public License in all respects
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   for all of the Red Hat elfutils code and other code used in conjunction
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   with Red Hat elfutils except the Non-GPL Code covered by this exception.
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   If you modify this file, you may extend this exception to your version
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   of the file, but you are not obligated to do so.  If you do not wish to
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   provide this exception without modification, you must delete this
3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   exception statement from your version and license this file solely under
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   the GPL without exception.
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is an included package of the Open Invention Network.
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   An included package of the Open Invention Network is a package for which
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Open Invention Network licensees cross-license their patents.  No patent
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   license is granted, either expressly or impliedly, by designation as an
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   included package.  Should you wish to participate in the Open Invention
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Network licensing program, please visit www.openinventionnetwork.com
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   <http://www.openinventionnetwork.com>.  */
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h>
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libdwP.h"
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <dwarf.h>
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5625b3c049e70834cf33790a28643ab058b507b35cBen Chengstruct visitor_info
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  void *die_addr;
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  int (*callback) (Dwarf_Die *, void *);
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  void *arg;
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng};
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6325b3c049e70834cf33790a28643ab058b507b35cBen Chengstatic int
6425b3c049e70834cf33790a28643ab058b507b35cBen Chengscope_visitor (unsigned int depth __attribute__ ((unused)),
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	       struct Dwarf_Die_Chain *die, void *arg)
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  struct visitor_info *const v = arg;
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (INTUSE(dwarf_tag) (&die->die) != DW_TAG_inlined_subroutine)
7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return DWARF_CB_OK;
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Attribute attr_mem;
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Attribute *attr = INTUSE(dwarf_attr) (&die->die, DW_AT_abstract_origin,
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng					      &attr_mem);
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (attr == NULL)
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return DWARF_CB_OK;
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Die origin_mem;
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Die *origin = INTUSE(dwarf_formref_die) (attr, &origin_mem);
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (origin == NULL)
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return DWARF_CB_ABORT;
8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (origin->addr != v->die_addr)
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return DWARF_CB_OK;
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return (*v->callback) (&die->die, v->arg);
8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8925b3c049e70834cf33790a28643ab058b507b35cBen Chengint
9025b3c049e70834cf33790a28643ab058b507b35cBen Chengdwarf_func_inline (Dwarf_Die *func)
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Attribute attr_mem;
9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf_Word val;
9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (INTUSE(dwarf_formudata) (INTUSE(dwarf_attr) (func, DW_AT_inline,
9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng						   &attr_mem),
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng			       &val) == 0)
9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  switch (val)
9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_INL_not_inlined:
10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return 0;
10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_INL_declared_not_inlined:
10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return -1;
10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_INL_inlined:
10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    case DW_INL_declared_inlined:
10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return 1;
10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return 0;
11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
11325b3c049e70834cf33790a28643ab058b507b35cBen Chengint
11425b3c049e70834cf33790a28643ab058b507b35cBen Chengdwarf_func_inline_instances (Dwarf_Die *func,
11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng			     int (*callback) (Dwarf_Die *, void *),
11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng			     void *arg)
11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  struct visitor_info v = { func->addr, callback, arg };
11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  struct Dwarf_Die_Chain cu = { .die = CUDIE (func->cu), .parent = NULL };
12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return __libdw_visit_scopes (0, &cu, &scope_visitor, NULL, &v);
12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
122