Menu

[r4191]: / trunk / libdwarf / libdwarf_str.c  Maximize  Restore  History

Download this file

302 lines (241 with data), 7.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*-
* Copyright (c) 2009,2010,2023 Kai Wang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "_libdwarf.h"
ELFTC_VCSID("$Id$");
#define _INIT_DWARF_STRTAB_SIZE 1024
int
_dwarf_strtab_add(Dwarf_Debug dbg, char *string, uint64_t *off,
Dwarf_Error *error)
{
size_t len;
assert(dbg != NULL && string != NULL);
len = strlen(string) + 1;
while (dbg->dbg_strtab_size + len > dbg->dbg_strtab_cap) {
dbg->dbg_strtab_cap *= 2;
dbg->dbg_strtab = realloc(dbg->dbg_strtab,
(size_t) dbg->dbg_strtab_cap);
if (dbg->dbg_strtab == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
}
if (off != NULL)
*off = dbg->dbg_strtab_size;
memcpy(&dbg->dbg_strtab[dbg->dbg_strtab_size], string, len - 1);
dbg->dbg_strtab_size += len;
dbg->dbg_strtab[dbg->dbg_strtab_size - 1] = '\0';
return (DW_DLE_NONE);
}
char *
_dwarf_strtab_get_table(Dwarf_Debug dbg)
{
assert(dbg != NULL);
return (dbg->dbg_strtab);
}
char *
_dwarf_strtab_get_line_table(Dwarf_Debug dbg)
{
assert(dbg != NULL);
return (dbg->dbg_line_strtab);
}
static int
_dwarf_str_offsets_init(Dwarf_Debug dbg, Dwarf_Error *error)
{
Dwarf_Section *ds;
Dwarf_StrOffsets *str_off;
Dwarf_Half version;
uint64_t offset, length;
int dwarf_size;
assert(dbg != NULL);
dbg->dbg_str_offsets = NULL;
if ((ds = _dwarf_find_section(dbg, ".debug_str_offsets")) == NULL)
return (DW_DLE_NONE);
offset = 0;
/* Read in the table header. */
length = dbg->read(ds->ds_data, &offset, 4);
if (length == 0xffffffff) {
dwarf_size = 8;
length = dbg->read(ds->ds_data, &offset, 8);
} else
dwarf_size = 4;
version = dbg->read(ds->ds_data, &offset, 2);
if (version != 5) {
DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
return (DW_DLE_VERSION_STAMP_ERROR);
}
if ((str_off = calloc(1, sizeof(*str_off))) == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
/* 2 byte padding. */
offset += 2;
str_off->so_length = length;
str_off->so_version = version;
str_off->so_header_size = offset;
str_off->so_dwarf_size = dwarf_size;
str_off->so_data = ds->ds_data;
dbg->dbg_str_offsets = str_off;
return (DW_DLE_NONE);
}
static int
_dwarf_find_cu_str_offsets_base(Dwarf_Debug dbg, Dwarf_CU cu,
uint64_t *offset_basep, Dwarf_Error *error)
{
Dwarf_Attribute at;
Dwarf_Die cu_die;
int ret;
assert(dbg->dbg_str_offsets != NULL);
if (!cu->cu_stroff_base_valid) {
if ((ret = dwarf_siblingof(dbg, 0, &cu_die, error)) !=
DW_DLV_OK) {
DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
return (DW_DLE_NO_ENTRY);
}
if ((at = _dwarf_attr_find(cu_die, DW_AT_str_offsets_base)) !=
NULL) {
cu->cu_stroff_base = at->u[0].u64;
} else {
/*
* No DW_AT_str_offsets_base found. Use header size
* instead.
*/
cu->cu_stroff_base =
dbg->dbg_str_offsets->so_header_size;
}
cu->cu_stroff_base_valid = 1;
dwarf_dealloc(dbg, cu_die, DW_DLA_DIE);
}
*offset_basep = cu->cu_stroff_base;
return (DW_DLE_NONE);
}
int
_dwarf_read_indexed_str(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t index,
char **str_p, Dwarf_Error *error)
{
Dwarf_StrOffsets *so;
uint64_t offset, offsets_base, strtab_offset;
int ret;
if (dbg->dbg_str_offsets == NULL) {
if ((ret = _dwarf_str_offsets_init(dbg, error)) != DW_DLE_NONE)
return (ret);
if (dbg->dbg_str_offsets == NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY);
return (DW_DLE_NO_ENTRY);
}
}
if ((ret = _dwarf_find_cu_str_offsets_base(dbg, cu, &offsets_base,
error)) != DW_DLE_NONE)
return (ret);
so = dbg->dbg_str_offsets;
/* Caculate where to read from string offsets array. */
offset = offsets_base + so->so_dwarf_size * index;
/* Read from the str offsets array to get offset into string table. */
strtab_offset = dbg->read(so->so_data, &offset, so->so_dwarf_size);
*str_p = _dwarf_strtab_get_table(dbg) + strtab_offset;
return (DW_DLE_NONE);
}
int
_dwarf_strtab_init(Dwarf_Debug dbg, Dwarf_Error *error)
{
Dwarf_Section *ds;
assert(dbg != NULL);
if (dbg->dbg_mode == DW_DLC_READ || dbg->dbg_mode == DW_DLC_RDWR) {
ds = _dwarf_find_section(dbg, ".debug_str");
if (ds == NULL) {
dbg->dbg_strtab = NULL;
dbg->dbg_strtab_cap = dbg->dbg_strtab_size = 0;
return (DW_DLE_NONE);
}
dbg->dbg_strtab_cap = dbg->dbg_strtab_size = ds->ds_size;
if (dbg->dbg_mode == DW_DLC_RDWR) {
if ((dbg->dbg_strtab = malloc((size_t) ds->ds_size)) ==
NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
memcpy(dbg->dbg_strtab, ds->ds_data, ds->ds_size);
} else
dbg->dbg_strtab = (char *) ds->ds_data;
ds = _dwarf_find_section(dbg, ".debug_line_str");
if (ds != NULL) {
dbg->dbg_line_strtab = (char *) ds->ds_data;
}
} else {
/* DW_DLC_WRITE */
dbg->dbg_strtab_cap = _INIT_DWARF_STRTAB_SIZE;
dbg->dbg_strtab_size = 0;
if ((dbg->dbg_strtab = malloc((size_t) dbg->dbg_strtab_cap)) ==
NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
dbg->dbg_strtab[0] = '\0';
}
return (DW_DLE_NONE);
}
void
_dwarf_str_offsets_cleanup(Dwarf_Debug dbg)
{
assert(dbg != NULL);
if (dbg->dbg_str_offsets)
free(dbg->dbg_str_offsets);
}
void
_dwarf_strtab_cleanup(Dwarf_Debug dbg)
{
assert(dbg != NULL);
if (dbg->dbg_mode == DW_DLC_RDWR || dbg->dbg_mode == DW_DLC_WRITE)
free(dbg->dbg_strtab);
}
int
_dwarf_strtab_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
{
Dwarf_P_Section ds;
int ret;
assert(dbg != NULL);
if ((ret = _dwarf_section_init(dbg, &ds, ".debug_str", 0, error)) !=
DW_DLE_NONE)
return (ret);
if (dbg->dbg_strtab_size > ds->ds_cap) {
ds->ds_data = realloc(ds->ds_data,
(size_t) dbg->dbg_strtab_size);
if (ds->ds_data == NULL) {
_dwarf_section_free(dbg, &ds);
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
ds->ds_cap = dbg->dbg_strtab_size;
}
memcpy(ds->ds_data, dbg->dbg_strtab, dbg->dbg_strtab_size);
ds->ds_size = dbg->dbg_strtab_size;
/*
* Inform application the creation of .debug_str ELF section.
* Note that .debug_str use a different format than usual ELF
* string table, so it should not have SHT_STRTAB as its type.
*/
ret = _dwarf_section_callback(dbg, ds, SHT_PROGBITS, 0, 0, 0, error);
return (ret);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.