1/* Return string associated with given attribute.
2   Copyright (C) 2003, 2004 Red Hat, Inc.
3   Written by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5   This program is Open Source software; you can redistribute it and/or
6   modify it under the terms of the Open Software License version 1.0 as
7   published by the Open Source Initiative.
8
9   You should have received a copy of the Open Software License along
10   with this program; if not, you may obtain a copy of the Open Software
11   License version 1.0 from http://www.opensource.org/licenses/osl.php or
12   by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13   3001 King Ranch Road, Ukiah, CA 95482.   */
14
15#ifdef HAVE_CONFIG_H
16# include <config.h>
17#endif
18
19#include <dwarf.h>
20#include "libdwP.h"
21
22
23const char *
24dwarf_formstring (attrp)
25     Dwarf_Attribute *attrp;
26{
27  /* Ignore earlier errors.  */
28  if (attrp == NULL)
29    return NULL;
30
31  /* We found it.  Now determine where the string is stored.  */
32  if (attrp->form == DW_FORM_string)
33    /* A simple inlined string.  */
34    return (const char *) attrp->valp;
35
36  Dwarf *dbg = attrp->cu->dbg;
37
38  if (unlikely (attrp->form != DW_FORM_strp)
39      || dbg->sectiondata[IDX_debug_str] == NULL)
40    {
41    invalid_error:
42      __libdw_seterrno (DWARF_E_NO_STRING);
43      return NULL;
44    }
45
46  uint64_t off;
47  // XXX We need better boundary checks.
48  if (attrp->cu->offset_size == 8)
49    off = read_8ubyte_unaligned (dbg, attrp->valp);
50  else
51    off = read_4ubyte_unaligned (dbg, attrp->valp);
52
53  if (off  >= dbg->sectiondata[IDX_debug_str]->d_size)
54    goto invalid_error;
55
56  return (const char *) dbg->sectiondata[IDX_debug_str]->d_buf + off;
57}
58