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.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
38 dwarf_formstring (Dwarf_Attribute *attrp)
40 /* Ignore earlier errors. */
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;
49 Dwarf_CU *cu = attrp->cu;
51 Dwarf *dbg_ret = ((attrp->form == DW_FORM_GNU_strp_alt
52 || attrp->form == DW_FORM_strp_sup)
53 ? INTUSE(dwarf_getalt) (dbg) : dbg);
55 if (unlikely (dbg_ret == NULL))
57 __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK);
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]);
66 __libdw_seterrno ((attrp->form == DW_FORM_line_strp)
67 ? DWARF_E_NO_DEBUG_LINE_STR
68 : DWARF_E_NO_DEBUG_STR);
73 if (attrp->form == DW_FORM_strp
74 || attrp->form == DW_FORM_GNU_strp_alt
75 || attrp->form == DW_FORM_strp_sup)
77 if (__libdw_read_offset (dbg, dbg_ret, cu_sec_idx (cu),
78 attrp->valp, cu->offset_size, &off,
82 else if (attrp->form == DW_FORM_line_strp)
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))
92 const unsigned char *datap = attrp->valp;
93 const unsigned char *endp = cu->endp;
97 case DW_FORM_GNU_str_index:
101 __libdw_seterrno (DWARF_E_INVALID_DWARF);
104 get_uleb128 (idx, datap, endp);
108 if (datap >= endp - 1)
114 if (datap >= endp - 2)
116 idx = read_2ubyte_unaligned (dbg, datap);
120 if (datap >= endp - 3)
122 idx = read_3ubyte_unaligned (dbg, datap);
126 if (datap >= endp - 4)
128 idx = read_4ubyte_unaligned (dbg, datap);
132 __libdw_seterrno (DWARF_E_NO_STRING);
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)
142 if (dbg->sectiondata[IDX_debug_str_offsets] == NULL)
144 __libdw_seterrno (DWARF_E_NO_STR_OFFSETS);
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)
153 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
157 /* And the base offset should be at least inside the section. */
158 if (str_off > (dbg->sectiondata[IDX_debug_str_offsets]->d_size
162 size_t max_idx = (dbg->sectiondata[IDX_debug_str_offsets]->d_size
163 - offset_size - str_off) / offset_size;
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);
172 off = read_8ubyte_unaligned (dbg, datap);
174 if (off > dbg->sectiondata[IDX_debug_str]->d_size)
178 return (const char *) data->d_buf + off;
180 INTDEF(dwarf_formstring)