1/* Return string associated with given attribute.
2   Copyright (C) 2003-2010, 2013, 2017, 2018 Red Hat, Inc.
3   This file is part of elfutils.
4
5   This file is free software; you can redistribute it and/or modify
6   it under the terms of either
7
8     * the GNU Lesser General Public License as published by the Free
9       Software Foundation; either version 3 of the License, or (at
10       your option) any later version
11
12   or
13
14     * the GNU General Public License as published by the Free
15       Software Foundation; either version 2 of the License, or (at
16       your option) any later version
17
18   or both in parallel, as here.
19
20   elfutils is distributed in the hope that it will be useful, but
21   WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   General Public License for more details.
24
25   You should have received copies of the GNU General Public License and
26   the GNU Lesser General Public License along with this program.  If
27   not, see <http://www.gnu.org/licenses/>.  */
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include <dwarf.h>
34#include "libdwP.h"
35
36
37const char *
38dwarf_formstring (Dwarf_Attribute *attrp)
39{
40  /* Ignore earlier errors.  */
41  if (attrp == NULL)
42    return NULL;
43
44  /* We found it.  Now determine where the string is stored.  */
45  if (attrp->form == DW_FORM_string)
46    /* A simple inlined string.  */
47    return (const char *) attrp->valp;
48
49  Dwarf_CU *cu = attrp->cu;
50  Dwarf *dbg = cu->dbg;
51  Dwarf *dbg_ret = ((attrp->form == DW_FORM_GNU_strp_alt
52		     || attrp->form == DW_FORM_strp_sup)
53		    ? INTUSE(dwarf_getalt) (dbg) : dbg);
54
55  if (unlikely (dbg_ret == NULL))
56    {
57      __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK);
58      return NULL;
59    }
60
61  Elf_Data *data = ((attrp->form == DW_FORM_line_strp)
62		    ? dbg_ret->sectiondata[IDX_debug_line_str]
63		    : dbg_ret->sectiondata[IDX_debug_str]);
64  if (data == NULL)
65    {
66      __libdw_seterrno ((attrp->form == DW_FORM_line_strp)
67			? DWARF_E_NO_DEBUG_LINE_STR
68			: DWARF_E_NO_DEBUG_STR);
69      return NULL;
70    }
71
72  uint64_t off;
73  if (attrp->form == DW_FORM_strp
74      || attrp->form == DW_FORM_GNU_strp_alt
75      || attrp->form == DW_FORM_strp_sup)
76    {
77      if (__libdw_read_offset (dbg, dbg_ret, cu_sec_idx (cu),
78			       attrp->valp, cu->offset_size, &off,
79			       IDX_debug_str, 1))
80	return NULL;
81    }
82  else if (attrp->form == DW_FORM_line_strp)
83    {
84      if (__libdw_read_offset (dbg, dbg_ret, cu_sec_idx (cu),
85			       attrp->valp, cu->offset_size, &off,
86			       IDX_debug_line_str, 1))
87	return NULL;
88    }
89  else
90    {
91      Dwarf_Word idx;
92      const unsigned char *datap = attrp->valp;
93      const unsigned char *endp = cu->endp;
94      switch (attrp->form)
95	{
96	case DW_FORM_strx:
97	case DW_FORM_GNU_str_index:
98	  if (datap >= endp)
99	    {
100	    invalid:
101	      __libdw_seterrno (DWARF_E_INVALID_DWARF);
102	      return NULL;
103	    }
104	  get_uleb128 (idx, datap, endp);
105	  break;
106
107	case DW_FORM_strx1:
108	  if (datap >= endp - 1)
109	    goto invalid;
110	  idx = *datap;
111	  break;
112
113	case DW_FORM_strx2:
114	  if (datap >= endp - 2)
115	    goto invalid;
116	  idx = read_2ubyte_unaligned (dbg, datap);
117	  break;
118
119	case DW_FORM_strx3:
120	  if (datap >= endp - 3)
121	    goto invalid;
122	  idx = read_3ubyte_unaligned (dbg, datap);
123	  break;
124
125	case DW_FORM_strx4:
126	  if (datap >= endp - 4)
127	    goto invalid;
128	  idx = read_4ubyte_unaligned (dbg, datap);
129	  break;
130
131	default:
132	  __libdw_seterrno (DWARF_E_NO_STRING);
133	  return NULL;
134	}
135
136      /* So we got an index in the .debug_str_offsets.  Lets see if it
137	 is valid and we can get the actual .debug_str offset.  */
138      Dwarf_Off str_off = __libdw_cu_str_off_base (cu);
139      if (str_off == (Dwarf_Off) -1)
140	return NULL;
141
142      if (dbg->sectiondata[IDX_debug_str_offsets] == NULL)
143	{
144	  __libdw_seterrno (DWARF_E_NO_STR_OFFSETS);
145	  return NULL;
146	}
147
148      /* The section should at least contain room for one offset.  */
149      int offset_size = cu->offset_size;
150      if (cu->offset_size > dbg->sectiondata[IDX_debug_str_offsets]->d_size)
151	{
152	invalid_offset:
153	  __libdw_seterrno (DWARF_E_INVALID_OFFSET);
154	  return NULL;
155	}
156
157      /* And the base offset should be at least inside the section.  */
158      if (str_off > (dbg->sectiondata[IDX_debug_str_offsets]->d_size
159		     - offset_size))
160	goto invalid_offset;
161
162      size_t max_idx = (dbg->sectiondata[IDX_debug_str_offsets]->d_size
163			- offset_size - str_off) / offset_size;
164      if (idx > max_idx)
165	goto invalid_offset;
166
167      datap = (dbg->sectiondata[IDX_debug_str_offsets]->d_buf
168	       + str_off + (idx * offset_size));
169      if (offset_size == 4)
170	off = read_4ubyte_unaligned (dbg, datap);
171      else
172	off = read_8ubyte_unaligned (dbg, datap);
173
174      if (off > dbg->sectiondata[IDX_debug_str]->d_size)
175	goto invalid_offset;
176    }
177
178  return (const char *) data->d_buf + off;
179}
180INTDEF(dwarf_formstring)
181