125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Return string associated with given attribute.
203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   Copyright (C) 2003-2010, 2013 Red Hat, Inc.
303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes   This file is part of elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Written by Ulrich Drepper <drepper@redhat.com>, 2003.
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 <dwarf.h>
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "libdwP.h"
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3825b3c049e70834cf33790a28643ab058b507b35cBen Chengconst char *
3925b3c049e70834cf33790a28643ab058b507b35cBen Chengdwarf_formstring (attrp)
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng     Dwarf_Attribute *attrp;
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Ignore earlier errors.  */
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (attrp == NULL)
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return NULL;
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* We found it.  Now determine where the string is stored.  */
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (attrp->form == DW_FORM_string)
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    /* A simple inlined string.  */
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return (const char *) attrp->valp;
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  Dwarf *dbg = attrp->cu->dbg;
5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  Dwarf *dbg_ret = attrp->form == DW_FORM_GNU_strp_alt ? dbg->alt_dwarf : dbg;
5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (unlikely (dbg_ret == NULL))
5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    {
5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK);
5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      return NULL;
5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes    }
5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (unlikely (attrp->form != DW_FORM_strp
6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes		   && attrp->form != DW_FORM_GNU_strp_alt)
6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes      || dbg_ret->sectiondata[IDX_debug_str] == NULL)
6425b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng      __libdw_seterrno (DWARF_E_NO_STRING);
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng      return NULL;
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  uint64_t off;
7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  if (__libdw_read_offset (dbg, dbg_ret, cu_sec_idx (attrp->cu), attrp->valp,
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng			   attrp->cu->offset_size, &off, IDX_debug_str, 1))
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return NULL;
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes  return (const char *) dbg_ret->sectiondata[IDX_debug_str]->d_buf + off;
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
7625b3c049e70834cf33790a28643ab058b507b35cBen ChengINTDEF(dwarf_formstring)
77