Debian Codegraph @

src/lib/libdwarf/dwarf_gdbindex.c

raw
1 /*
2
3 Copyright (C) 2014-2020 David Anderson. All Rights Reserved.
4
5 This program is free software; you can redistribute it
6 and/or modify it under the terms of version 2.1 of the
7 GNU Lesser General Public License as published by the Free
8 Software Foundation.
9
10 This program is distributed in the hope that it would be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14
15 Further, this software is distributed without any warranty
16 that it is free of the rightful claim of any third person
17 regarding infringement or the like. Any license provided
18 herein, whether implied or otherwise, applies only to this
19 software file. Patent licenses, if any, provided herein
20 do not apply to combinations of this program with other
21 software, or any other product whatsoever.
22
23 You should have received a copy of the GNU Lesser General
24 Public License along with this program; if not, write the
25 Free Software Foundation, Inc., 51 Franklin Street - Fifth
26 Floor, Boston MA 02110-1301, USA.
27 */
28 /* see
29 https://sourceware.org/gdb/onlinedocs/gdb/\
30 Index-Section-Format.html#Index-Section-Format
31 */
32
33 #include <config.h>
34
35 #include <string.h> /* memcpy() */
36
37 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
38 #include "stdafx.h"
39 #endif /* HAVE_STDAFX_H */
40
41 #ifdef HAVE_STDINT_H
42 #include <stdint.h> /* uintptr_t */
43 #endif /* HAVE_STDINT_H */
44
45 #include "dwarf.h"
46 #include "libdwarf.h"
47 #include "libdwarf_private.h"
48 #include "dwarf_base_types.h"
49 #include "dwarf_opaque.h"
50 #include "dwarf_alloc.h"
51 #include "dwarf_error.h"
52 #include "dwarf_util.h"
53 #include "dwarf_string.h"
54 #include "dwarf_memcpy_swap.h"
55 #include "dwarf_gdbindex.h"
56
57 /* The dwarf_util macro READ_UNALIGNED
58 cannot be directly used because
59 gdb defines the section contents of
60 .gdb_index as little-endian always.
61 */
62
63 #if WORDS_BIGENDIAN /* meaning on this host */
64 #define READ_GDBINDEX(dest,desttype, source, length) \
65 do { \
66 desttype _ltmp = 0; \
67 _dwarf_memcpy_swap_bytes((((char *)(&_ltmp)) \
68 + sizeof(_ltmp) - (length)), \
69 (source), (length)) ; \
70 (dest) = (desttype)_ltmp; \
71 } while (0)
72 #else /* little-endian on this host */
73 #define READ_GDBINDEX(dest,desttype, source, length) \
74 do { \
75 desttype _ltmp = 0; \
76 memcpy(((char *)(&_ltmp)) , \
77 (source), (length)) ; \
78 (dest) = (desttype)_ltmp; \
79 } while (0)
80 #endif
81
82 struct dwarf_64bitpair {
83 gdbindex_64 offset;
84 gdbindex_64 length;
85 };
86
87 static void
88 emit_no_value_msg(Dwarf_Debug dbg,
89 int errnum,
90 const char * errstr_text,
91 Dwarf_Error *error)
92 {
93 _dwarf_error_string(dbg,error,errnum,
94 (char *)errstr_text);
95 }
96
97 static void
98 emit_one_value_msg(Dwarf_Debug dbg,
99 int errnum,
100 const char * errstr_text,
101 Dwarf_Unsigned value,
102 Dwarf_Error *error)
103 {
104 dwarfstring m;
105
106 dwarfstring_constructor(&m);
107 dwarfstring_append_printf_u(&m,
108 (char *)errstr_text,value);
109 _dwarf_error_string(dbg,error,errnum,
110 dwarfstring_string(&m));
111 dwarfstring_destructor(&m);
112 }
113
114 static int
115 set_base(Dwarf_Debug dbg,
116 struct Dwarf_Gdbindex_array_instance_s * hdr,
117 Dwarf_Small *start,
118 Dwarf_Small *end,
119 /* entrylen is the length of a single struct as seen
120 in the object. */
121 Dwarf_Unsigned entrylen,
122 /* The size of each field in the struct in the object. */
123 Dwarf_Unsigned fieldlen,
124 enum gdbindex_type_e type,
125 Dwarf_Error * error)
126 {
127
128 if (type == git_std || type == git_cuvec) {
129 /* cuvec is sort of a fake as a simple
130 section, but a useful one. */
131 Dwarf_Unsigned count = 0;
132 if ( end < start) {
133 _dwarf_error(dbg, error,DW_DLE_GDB_INDEX_COUNT_ERROR);
134 return DW_DLV_ERROR;
135 }
136 count = end - start;
137 count = count / entrylen;
138 hdr->dg_type = type;
139 hdr->dg_base = start;
140 hdr->dg_count = count;
141 hdr->dg_entry_length = entrylen;
142 hdr->dg_fieldlen = (unsigned)fieldlen;
143 } else {
144 /* address area. */
145 /* 64bit, 64bit, offset. Then 32bit pad. */
146 Dwarf_Unsigned count = 0;
147 hdr->dg_base = start;
148 if ( end < start) {
149 _dwarf_error(dbg, error,
150 DW_DLE_GDB_INDEX_COUNT_ADDR_ERROR);
151 return DW_DLV_ERROR;
152 }
153 /* entry length includes pad. */
154 hdr->dg_entry_length = 2*sizeof(gdbindex_64) +
155 DWARF_32BIT_SIZE;
156 count = end - start;
157 count = count / hdr->dg_entry_length;
158 hdr->dg_count = count;
159 /* The dg_fieldlen is a fake, the fields are not
160 all the same length. */
161 hdr->dg_fieldlen = DWARF_32BIT_SIZE;
162 hdr->dg_type = type;
163 }
164 return DW_DLV_OK;
165 }
166
167 int
168 dwarf_gdbindex_header(Dwarf_Debug dbg,
169 Dwarf_Gdbindex * gdbindexptr,
170 Dwarf_Unsigned * version,
171 Dwarf_Unsigned * cu_list_offset,
172 Dwarf_Unsigned * types_cu_list_offset,
173 Dwarf_Unsigned * address_area_offset,
174 Dwarf_Unsigned * symbol_table_offset,
175 Dwarf_Unsigned * constant_pool_offset,
176 Dwarf_Unsigned * section_size,
177 const char ** section_name,
178 Dwarf_Error * error)
179 {
180 Dwarf_Gdbindex indexptr = 0;
181 int res = DW_DLV_ERROR;
182 Dwarf_Small *data = 0;
183 Dwarf_Small *startdata = 0;
184 Dwarf_Unsigned version_in = 0;
185
186 CHECK_DBG(dbg,error,"dwarf_gdbindex_header()");
187 if (!dbg->de_debug_gdbindex.dss_size) {
188 return DW_DLV_NO_ENTRY;
189 }
190 if (!dbg->de_debug_gdbindex.dss_data) {
191 res = _dwarf_load_section(dbg, &dbg->de_debug_gdbindex,error);
192 if (res != DW_DLV_OK) {
193 return res;
194 }
195 }
196 data = dbg->de_debug_gdbindex.dss_data;
197 if (!data) {
198 /* Should be impossible, dwarf_load_section() would
199 return DW_DLV_ERROR if dss_data could not be
200 set non-null */
201 _dwarf_error_string(dbg, error,
202 DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
203 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION: "
204 "We have non-zero (section) dss_size but "
205 "null dss_data pointer");
206 return DW_DLV_ERROR;
207 }
208 startdata = data;
209
210 if (dbg->de_debug_gdbindex.dss_size < (DWARF_32BIT_SIZE*6)) {
211 _dwarf_error(dbg, error, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION);
212 return DW_DLV_ERROR;
213 }
214 indexptr = (Dwarf_Gdbindex)_dwarf_get_alloc(dbg,
215 DW_DLA_GDBINDEX,1);
216 if (indexptr == NULL) {
217 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
218 "DW_DLE_ALLOC_FAIL: allocating Dwarf_Gdbindex");
219 return DW_DLV_ERROR;
220 }
221 READ_GDBINDEX(version_in,Dwarf_Unsigned,
222 data, DWARF_32BIT_SIZE);
223 indexptr->gi_version = version_in;
224
225 indexptr->gi_dbg = dbg;
226 indexptr->gi_section_data = startdata;
227 indexptr->gi_section_length = dbg->de_debug_gdbindex.dss_size;
228 /* 7 and lower are different format in some way */
229 if (indexptr->gi_version != 8 &&
230 indexptr->gi_version != 7) {
231 emit_one_value_msg(dbg, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
232 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION: "
233 " version number %u is not"
234 " supported",
235 indexptr->gi_version,error);
236 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
237 return DW_DLV_ERROR;
238 }
239 data += DWARF_32BIT_SIZE;
240 READ_GDBINDEX(indexptr->gi_cu_list_offset ,Dwarf_Unsigned,
241 data, DWARF_32BIT_SIZE);
242 if (indexptr->gi_cu_list_offset > indexptr->gi_section_length) {
243 emit_one_value_msg(dbg, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
244 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION"
245 " cu list offset of %u is too large for the section",
246 indexptr->gi_cu_list_offset,error);
247 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
248 return DW_DLV_ERROR;
249 }
250 data += DWARF_32BIT_SIZE;
251 READ_GDBINDEX(indexptr->gi_types_cu_list_offset ,Dwarf_Unsigned,
252 data, DWARF_32BIT_SIZE);
253 if (indexptr->gi_types_cu_list_offset >
254 indexptr->gi_section_length) {
255 emit_one_value_msg(dbg, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
256 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION"
257 " types cu list offset of %u is too "
258 "large for the section",
259 indexptr->gi_cu_list_offset,error);
260 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
261 return DW_DLV_ERROR;
262 }
263 data += DWARF_32BIT_SIZE;
264 READ_GDBINDEX(indexptr->gi_address_area_offset ,Dwarf_Unsigned,
265 data, DWARF_32BIT_SIZE);
266 if (indexptr->gi_address_area_offset >
267 indexptr->gi_section_length) {
268 emit_one_value_msg(dbg, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
269 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION"
270 " address area offset of %u is too "
271 "large for the section",
272 indexptr->gi_address_area_offset ,error);
273 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
274 return DW_DLV_ERROR;
275 }
276 data += DWARF_32BIT_SIZE;
277 READ_GDBINDEX(indexptr->gi_symbol_table_offset ,Dwarf_Unsigned,
278 data, DWARF_32BIT_SIZE);
279 if (indexptr->gi_symbol_table_offset >
280 indexptr->gi_section_length) {
281 emit_one_value_msg(dbg, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
282 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION"
283 " symbol table offset of %u is too "
284 "large for the section",
285 indexptr->gi_symbol_table_offset,error);
286 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
287 return DW_DLV_ERROR;
288 }
289 data += DWARF_32BIT_SIZE;
290 READ_GDBINDEX(indexptr->gi_constant_pool_offset ,Dwarf_Unsigned,
291 data, DWARF_32BIT_SIZE);
292 if (indexptr->gi_constant_pool_offset >
293 indexptr->gi_section_length) {
294 emit_one_value_msg(dbg, DW_DLE_ERRONEOUS_GDB_INDEX_SECTION,
295 "DW_DLE_ERRONEOUS_GDB_INDEX_SECTION"
296 " constant pool offset of %u is too "
297 "large for the section",
298 indexptr->gi_constant_pool_offset,error);
299 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
300 return DW_DLV_ERROR;
301 }
302 data += DWARF_32BIT_SIZE;
303
304 res = set_base(dbg,&indexptr->gi_culisthdr,
305 startdata + indexptr->gi_cu_list_offset,
306 startdata + indexptr->gi_types_cu_list_offset,
307 2*sizeof(gdbindex_64),
308 sizeof(gdbindex_64),
309 git_std,error);
310 if (res == DW_DLV_ERROR) {
311 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
312 return res;
313 }
314 res = set_base(dbg,&indexptr->gi_typesculisthdr,
315 startdata+ indexptr->gi_types_cu_list_offset,
316 startdata+ indexptr->gi_address_area_offset,
317 3*sizeof(gdbindex_64),
318 sizeof(gdbindex_64),
319 git_std,error);
320 if (res == DW_DLV_ERROR) {
321 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
322 return res;
323 }
324 res = set_base(dbg,&indexptr->gi_addressareahdr,
325 startdata + indexptr->gi_address_area_offset,
326 startdata + indexptr->gi_symbol_table_offset,
327 3*sizeof(gdbindex_64),
328 sizeof(gdbindex_64),
329 git_address,error);
330 if (res == DW_DLV_ERROR) {
331 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
332 return res;
333 }
334 res = set_base(dbg,&indexptr->gi_symboltablehdr,
335 startdata + indexptr->gi_symbol_table_offset,
336 startdata + indexptr->gi_constant_pool_offset,
337 2*DWARF_32BIT_SIZE,
338 DWARF_32BIT_SIZE,
339 git_std,error);
340 if (res == DW_DLV_ERROR) {
341 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
342 return res;
343 }
344 res = set_base(dbg,&indexptr->gi_cuvectorhdr,
345 startdata + indexptr->gi_constant_pool_offset,
346 /* There is no real single vector size.
347 but we'll use the entire rest as if there was. */
348 startdata + indexptr->gi_section_length,
349 DWARF_32BIT_SIZE,
350 DWARF_32BIT_SIZE,
351 git_cuvec,error);
352 if (res == DW_DLV_ERROR) {
353 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
354 return res;
355 }
356 *gdbindexptr = indexptr;
357 *version = indexptr->gi_version;
358 *cu_list_offset = indexptr->gi_cu_list_offset;
359 *types_cu_list_offset = indexptr->gi_types_cu_list_offset;
360 *address_area_offset = indexptr->gi_address_area_offset;
361 *symbol_table_offset = indexptr->gi_symbol_table_offset;
362 *constant_pool_offset = indexptr->gi_constant_pool_offset;
363 *section_size = indexptr->gi_section_length;
364 *section_name = dbg->de_debug_gdbindex.dss_name;
365 return DW_DLV_OK;
366 }
367
368 int
369 dwarf_gdbindex_culist_array(Dwarf_Gdbindex gdbindexptr,
370 Dwarf_Unsigned * list_length,
371 Dwarf_Error * error)
372 {
373 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
374 _dwarf_error_string(NULL, error,
375 DW_DLE_GDB_INDEX_INDEX_ERROR,
376 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
377 " passed in NULL inindexptr to"
378 " dwarf_gdbindex_culist_array");
379 return DW_DLV_ERROR;
380 }
381 (void)error;
382 *list_length = gdbindexptr->gi_culisthdr.dg_count;
383 return DW_DLV_OK;
384 }
385
386 /* entryindex: 0 to list_length-1 */
387 int
388 dwarf_gdbindex_culist_entry(Dwarf_Gdbindex gdbindexptr,
389 Dwarf_Unsigned entryindex,
390 Dwarf_Unsigned * cu_offset,
391 Dwarf_Unsigned * cu_length,
392 Dwarf_Error * error)
393 {
394 Dwarf_Small * base = 0;
395 Dwarf_Small * endptr = 0;
396 Dwarf_Unsigned offset = 0;
397 Dwarf_Unsigned length = 0;
398 Dwarf_Unsigned max = 0;
399 unsigned fieldlen = 0;
400
401 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
402 _dwarf_error_string(NULL, error,
403 DW_DLE_GDB_INDEX_INDEX_ERROR,
404 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
405 " passed in NULL inindexptr to"
406 " dwarf_gdbindex_culist_entry");
407 return DW_DLV_ERROR;
408 }
409 max = gdbindexptr->gi_culisthdr.dg_count;
410 fieldlen = gdbindexptr->gi_culisthdr.dg_fieldlen;
411 if (entryindex >= max) {
412 return DW_DLV_NO_ENTRY;
413 }
414 endptr = gdbindexptr->gi_section_data +
415 gdbindexptr->gi_section_length;
416
417 base = gdbindexptr->gi_culisthdr.dg_base;
418 base += entryindex*gdbindexptr->gi_culisthdr.dg_entry_length;
419 if ((base + 2*fieldlen) >endptr) {
420 Dwarf_Debug dbg = 0;
421
422 dbg = gdbindexptr->gi_dbg;
423 emit_one_value_msg(dbg, DW_DLE_GDB_INDEX_INDEX_ERROR,
424 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
425 " end offset of data for index %u is past the"
426 " end of the section",
427 entryindex,error);
428 return DW_DLV_ERROR;
429 }
430
431 READ_GDBINDEX(offset ,Dwarf_Unsigned,
432 base,
433 fieldlen);
434 READ_GDBINDEX(length ,Dwarf_Unsigned,
435 base+ fieldlen,
436 fieldlen);
437 *cu_offset = offset;
438 *cu_length = length;
439 return DW_DLV_OK;
440 }
441
442 int
443 dwarf_gdbindex_types_culist_array(Dwarf_Gdbindex gdbindexptr,
444 Dwarf_Unsigned * list_length,
445 Dwarf_Error * error)
446 {
447 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
448 _dwarf_error_string(NULL, error,
449 DW_DLE_GDB_INDEX_INDEX_ERROR,
450 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
451 " passed in NULL inindexptr to"
452 " dwarf_types_culist_entry");
453 return DW_DLV_ERROR;
454 }
455
456 (void)error;
457 *list_length = gdbindexptr->gi_typesculisthdr.dg_count;
458 return DW_DLV_OK;
459 }
460
461 /* entryindex: 0 to list_length-1 */
462 int
463 dwarf_gdbindex_types_culist_entry(Dwarf_Gdbindex gdbindexptr,
464 Dwarf_Unsigned entryindex,
465 Dwarf_Unsigned * t_offset,
466 Dwarf_Unsigned * t_length,
467 Dwarf_Unsigned * t_signature,
468 Dwarf_Error * error)
469 {
470 Dwarf_Unsigned max = 0;
471 Dwarf_Small * base = 0;
472 Dwarf_Small * endptr = 0;
473 Dwarf_Unsigned offset = 0;
474 Dwarf_Unsigned length = 0;
475 Dwarf_Unsigned signature = 0;
476 unsigned fieldlen = 0;
477
478 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
479 _dwarf_error_string(NULL, error,
480 DW_DLE_GDB_INDEX_INDEX_ERROR,
481 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
482 " passed in NULL inindexptr to"
483 " dwarf_gdbindex_types_culist_entry");
484 return DW_DLV_ERROR;
485 }
486 fieldlen = gdbindexptr->gi_typesculisthdr.dg_fieldlen;
487 max = gdbindexptr->gi_typesculisthdr.dg_count;
488 endptr = gdbindexptr->gi_section_data +
489 gdbindexptr->gi_section_length;
490
491 if (entryindex >= max) {
492 return DW_DLV_NO_ENTRY;
493 }
494 base = gdbindexptr->gi_typesculisthdr.dg_base;
495 base += entryindex*gdbindexptr->gi_typesculisthdr.dg_entry_length;
496 if ((base + 3*fieldlen) >endptr) {
497 Dwarf_Debug dbg = gdbindexptr->gi_dbg;
498 emit_one_value_msg(dbg, DW_DLE_GDB_INDEX_INDEX_ERROR,
499 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
500 " end offset of data for type index %u is past the"
501 " end of the section",
502 entryindex,error);
503 return DW_DLV_ERROR;
504 }
505 READ_GDBINDEX(offset ,Dwarf_Unsigned,
506 base,
507 fieldlen);
508 READ_GDBINDEX(length ,Dwarf_Unsigned,
509 base+ (1*fieldlen),
510 fieldlen);
511 READ_GDBINDEX(signature ,Dwarf_Unsigned,
512 base+ (2*fieldlen),
513 fieldlen);
514 *t_offset = offset;
515 *t_length = length;
516 *t_signature = signature;
517 return DW_DLV_OK;
518 }
519
520 int
521 dwarf_gdbindex_addressarea(Dwarf_Gdbindex gdbindexptr,
522 Dwarf_Unsigned * list_length,
523 Dwarf_Error * error)
524 {
525 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
526 _dwarf_error_string(NULL, error,
527 DW_DLE_GDB_INDEX_INDEX_ERROR,
528 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
529 " passed in NULL inindexptr to"
530 " dwarf_gdbindex_addressarea");
531 return DW_DLV_ERROR;
532 }
533 (void)error;
534 *list_length = gdbindexptr->gi_addressareahdr.dg_count;
535 return DW_DLV_OK;
536 }
537
538 /* entryindex: 0 to addressarea_list_length-1 */
539 int
540 dwarf_gdbindex_addressarea_entry(
541 Dwarf_Gdbindex gdbindexptr,
542 Dwarf_Unsigned entryindex,
543 Dwarf_Unsigned * low_address,
544 Dwarf_Unsigned * high_address,
545 Dwarf_Unsigned * cu_index,
546 Dwarf_Error * error)
547 {
548 Dwarf_Unsigned max = 0;
549 Dwarf_Small * base = 0;
550 Dwarf_Small * endptr = 0;
551 Dwarf_Unsigned lowaddr = 0;
552 Dwarf_Unsigned highaddr = 0;
553 Dwarf_Unsigned cuindex = 0;
554 Dwarf_Unsigned fieldslen = 0;
555
556 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
557 _dwarf_error_string(NULL, error,
558 DW_DLE_GDB_INDEX_INDEX_ERROR,
559 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
560 " passed in NULL inindexptr to"
561 " dwarf_gdbindex_addressarea_entry");
562 return DW_DLV_ERROR;
563 }
564 max = gdbindexptr->gi_addressareahdr.dg_count;
565 if (entryindex >= max) {
566 _dwarf_error(gdbindexptr->gi_dbg, error,
567 DW_DLE_GDB_INDEX_INDEX_ERROR);
568 return DW_DLV_ERROR;
569 }
570 base = gdbindexptr->gi_addressareahdr.dg_base;
571 base += entryindex*gdbindexptr->gi_addressareahdr.dg_entry_length;
572 endptr = gdbindexptr->gi_section_data +
573 gdbindexptr->gi_section_length;
574 fieldslen = 2*sizeof(gdbindex_64) + DWARF_32BIT_SIZE;
575 if ((base + fieldslen) > endptr) {
576 Dwarf_Debug dbg = gdbindexptr->gi_dbg;
577 emit_one_value_msg(dbg, DW_DLE_GDB_INDEX_INDEX_ERROR,
578 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
579 " end offset of data for "
580 " dwarf_gdbindex_addressarea_entry %u is past the"
581 " end of the section",
582 entryindex,error);
583 return DW_DLV_ERROR;
584 }
585
586 READ_GDBINDEX(lowaddr ,Dwarf_Unsigned,
587 base,
588 sizeof(gdbindex_64));
589 READ_GDBINDEX(highaddr ,Dwarf_Unsigned,
590 base+ (1*sizeof(gdbindex_64)),
591 sizeof(gdbindex_64));
592 READ_GDBINDEX(cuindex ,Dwarf_Unsigned,
593 base+ (2*sizeof(gdbindex_64)),
594 DWARF_32BIT_SIZE);
595 *low_address = lowaddr;
596 *high_address = highaddr;
597 *cu_index = cuindex;
598 return DW_DLV_OK;
599 }
600
601 int
602 dwarf_gdbindex_symboltable_array(Dwarf_Gdbindex gdbindexptr,
603 Dwarf_Unsigned * list_length,
604 Dwarf_Error * error)
605 {
606 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
607 _dwarf_error_string(NULL, error,
608 DW_DLE_GDB_INDEX_INDEX_ERROR,
609 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
610 " passed in NULL inindexptr to"
611 " dwarf_gdbindex_symboltable_array");
612 return DW_DLV_ERROR;
613 }
614 (void)error;
615 *list_length = gdbindexptr->gi_symboltablehdr.dg_count;
616 return DW_DLV_OK;
617 }
618
619 /* entryindex: 0 to symtab_list_length-1 */
620 int
621 dwarf_gdbindex_symboltable_entry(
622 Dwarf_Gdbindex gdbindexptr,
623 Dwarf_Unsigned entryindex,
624 Dwarf_Unsigned * string_offset,
625 Dwarf_Unsigned * cu_vector_offset,
626 Dwarf_Error * error)
627 {
628 Dwarf_Unsigned max = 0;
629 Dwarf_Small * base = 0;
630 Dwarf_Small * endptr = 0;
631 Dwarf_Unsigned symoffset = 0;
632 Dwarf_Unsigned cuoffset = 0;
633 unsigned fieldlen = 0;
634
635 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
636 _dwarf_error_string(NULL, error,
637 DW_DLE_GDB_INDEX_INDEX_ERROR,
638 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
639 " passed in NULL inindexptr to"
640 " dwarf_gdbindex_symboltable_entry");
641 return DW_DLV_ERROR;
642 }
643 max = gdbindexptr->gi_symboltablehdr.dg_count;
644 fieldlen = gdbindexptr->gi_symboltablehdr.dg_fieldlen;
645 if (entryindex >= max) {
646 return DW_DLV_NO_ENTRY;
647 }
648 base = gdbindexptr->gi_symboltablehdr.dg_base;
649 base += entryindex*gdbindexptr->gi_symboltablehdr.dg_entry_length;
650 endptr = gdbindexptr->gi_section_data +
651 gdbindexptr->gi_section_length;
652
653 if (( base + 2*fieldlen) >endptr) {
654 Dwarf_Debug dbg = gdbindexptr->gi_dbg;
655 emit_one_value_msg(dbg, DW_DLE_GDB_INDEX_INDEX_ERROR,
656 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
657 " end offset of data for symboltable entry "
658 "%u is past the"
659 " end of the section",
660 entryindex,error);
661 return DW_DLV_ERROR;
662 }
663 READ_GDBINDEX(symoffset ,Dwarf_Unsigned,
664 base,
665 fieldlen);
666 READ_GDBINDEX(cuoffset ,Dwarf_Unsigned,
667 base + fieldlen,
668 fieldlen);
669 *string_offset = symoffset;
670 *cu_vector_offset = cuoffset;
671 return DW_DLV_OK;
672 }
673
674 int
675 dwarf_gdbindex_cuvector_length(Dwarf_Gdbindex gdbindexptr,
676 Dwarf_Unsigned cuvector_offset,
677 Dwarf_Unsigned * innercount,
678 Dwarf_Error * error)
679 {
680 Dwarf_Small *base = 0;
681 Dwarf_Small *endptr = 0;
682 Dwarf_Unsigned fieldlen = 0;
683 Dwarf_Unsigned val = 0;
684
685 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
686 _dwarf_error_string(NULL, error,
687 DW_DLE_GDB_INDEX_INDEX_ERROR,
688 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
689 " passed in NULL indexptr to"
690 " dwarf_gdbindex_cuvector_length");
691 return DW_DLV_ERROR;
692 }
693
694 base = gdbindexptr->gi_cuvectorhdr.dg_base;
695 endptr = gdbindexptr->gi_section_data +
696 gdbindexptr->gi_section_length;
697 fieldlen = gdbindexptr->gi_cuvectorhdr.dg_entry_length;
698 base += cuvector_offset;
699 if (( base + fieldlen) >endptr) {
700 Dwarf_Debug dbg = gdbindexptr->gi_dbg;
701 emit_no_value_msg(dbg, DW_DLE_GDB_INDEX_INDEX_ERROR,
702 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
703 " end offset of count of gdbindex cuvector "
704 " is past the"
705 " end of the section",
706 error);
707 return DW_DLV_ERROR;
708 }
709 READ_GDBINDEX(val,Dwarf_Unsigned,
710 base,
711 fieldlen);
712 *innercount = val;
713 return DW_DLV_OK;
714 }
715
716 int
717 dwarf_gdbindex_cuvector_inner_attributes(Dwarf_Gdbindex gdbindexptr,
718 Dwarf_Unsigned cuvector_offset,
719 Dwarf_Unsigned innerindex,
720 /* The attr_value is a field of bits. For expanded version
721 use dwarf_gdbindex_instance_expand_value() */
722 Dwarf_Unsigned * attributes,
723 Dwarf_Error * error)
724 {
725 Dwarf_Small *base = 0;
726 Dwarf_Small *endptr = 0;
727 Dwarf_Unsigned fieldlen = 0;
728 Dwarf_Unsigned val = 0;
729
730 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
731 _dwarf_error_string(NULL, error,
732 DW_DLE_GDB_INDEX_INDEX_ERROR,
733 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
734 " passed in NULL indexptr to"
735 " dwarf_gdbindex_cuvector_length");
736 return DW_DLV_ERROR;
737 }
738 base = gdbindexptr->gi_cuvectorhdr.dg_base;
739 base += cuvector_offset;
740 endptr = gdbindexptr->gi_section_data +
741 gdbindexptr->gi_section_length;
742 fieldlen = gdbindexptr->gi_cuvectorhdr.dg_entry_length;
743 /* The initial 4 bytes is not part of the array,
744 it is some sort of count. Get past it.*/
745 base += fieldlen;
746 base += fieldlen*innerindex;
747 if ((base+fieldlen) >= endptr) {
748 Dwarf_Debug dbg = gdbindexptr->gi_dbg;
749 emit_one_value_msg(dbg, DW_DLE_GDB_INDEX_INDEX_ERROR,
750 "DW_DLE_GDB_INDEX_INDEX_ERROR:"
751 " end offset of data for cuvector_inner_attribute "
752 "%u is past the"
753 " end of the section",
754 innerindex,error);
755 return DW_DLV_ERROR;
756 }
757
758 READ_GDBINDEX(val ,Dwarf_Unsigned,
759 base,
760 fieldlen);
761 *attributes = val;
762 return DW_DLV_OK;
763 }
764
765 int
766 dwarf_gdbindex_cuvector_instance_expand_value(
767 Dwarf_Gdbindex gdbindexptr,
768 Dwarf_Unsigned value,
769 Dwarf_Unsigned * cu_index,
770 Dwarf_Unsigned * symbol_kind,
771 Dwarf_Unsigned * is_static,
772 Dwarf_Error * error)
773 {
774 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
775 _dwarf_error_string(NULL, error, DW_DLE_DBG_NULL,
776 "DW_DLE_DBG_NULL: The call to "
777 "dwarf_gdbindex_cuvector_instance_expand_value"
778 " provides no dbg pointer");
779 return DW_DLV_ERROR;
780 }
781 *cu_index = value & 0xffffff;
782 *symbol_kind = (value >> 28) & 0x7;
783 *is_static = (value >> 31) & 1;
784 return DW_DLV_OK;
785
786 }
787
788 /* The strings in the pool follow (in memory) the cu index
789 set and are NUL terminated. */
790 int
791 dwarf_gdbindex_string_by_offset(Dwarf_Gdbindex gdbindexptr,
792 Dwarf_Unsigned stringoffsetinpool,
793 const char ** string_ptr,
794 Dwarf_Error * error)
795 {
796 Dwarf_Small *pooldata = 0;
797 Dwarf_Small *section_end = 0;
798 Dwarf_Small *stringitself = 0;
799 Dwarf_Debug dbg = 0;
800 Dwarf_Unsigned fulloffset = 0;
801 int res = 0;
802
803 if (!gdbindexptr || !gdbindexptr->gi_dbg) {
804 emit_no_value_msg(NULL,DW_DLE_GDB_INDEX_INDEX_ERROR,
805 "DW_DLE_GDB_INDEX_INDEX_ERROR: "
806 "The gdbindex pointer to "
807 "dwarf_gdbindex_string_by_offset()"
808 " is NULL",error);
809 return DW_DLV_ERROR;
810 }
811 dbg = gdbindexptr->gi_dbg;
812 if (IS_INVALID_DBG(dbg)) {
813 emit_no_value_msg(NULL,DW_DLE_GDB_INDEX_INDEX_ERROR,
814 "DW_DLE_GDB_INDEX_INDEX_ERROR: "
815 "The gdbindex Dwarf_Debug in"
816 "dwarf_gdbindex_string_by_offset()"
817 " is NULL",error);
818 return DW_DLV_ERROR;
819 }
820 section_end = gdbindexptr->gi_section_data +
821 gdbindexptr->gi_section_length;
822 fulloffset = gdbindexptr->gi_constant_pool_offset
823 + stringoffsetinpool;
824 stringitself = gdbindexptr->gi_section_data + fulloffset;
825 if (stringitself > section_end) {
826 emit_one_value_msg(dbg,DW_DLE_GDBINDEX_STRING_ERROR,
827 "DW_DLE_GDBINDEX_STRING_ERROR: "
828 "The dwarf_gdbindex_string_by_offset() "
829 "string starts past the end of the "
830 "section at section_offset 0x%"
831 DW_PR_XZEROS DW_PR_DUx ".",
832 fulloffset,
833 error);
834 return DW_DLV_ERROR;
835 }
836 res = _dwarf_check_string_valid(dbg,pooldata,
837 stringitself, section_end,
838 DW_DLE_GDBINDEX_STRING_ERROR,
839 error);
840 if (res != DW_DLV_OK) {
841 return res;
842 }
843 *string_ptr = (const char *)stringitself;
844 return DW_DLV_OK;
845 }
846
847 void
848 dwarf_dealloc_gdbindex(Dwarf_Gdbindex indexptr)
849 {
850 if (indexptr) {
851 Dwarf_Debug dbg = indexptr->gi_dbg;
852 dwarf_dealloc(dbg,indexptr,DW_DLA_GDBINDEX);
853 }
854 }