Debian Codegraph @

src/lib/libdwarf/dwarf_xu_index.c

raw
1 /*
2 Copyright (C) 2014-2021 David Anderson. All Rights Reserved.
3
4 This program is free software; you can redistribute it
5 and/or modify it under the terms of version 2.1 of the
6 GNU Lesser General Public License as published by the Free
7 Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU Lesser General
23 Public License along with this program; if not, write the
24 Free Software Foundation, Inc., 51 Franklin Street - Fifth
25 Floor, Boston MA 02110-1301, USA.
26
27 */
28
29 /* The file and functions have 'xu' because
30 the .debug_cu_index and .debug_tu_index
31 sections have the same layout and this deals with both.
32
33 This is DebugFission, part of DWARF5.
34
35 It allows fast section access in a .dwp object file
36 with debug-information to locate offsets
37 within and between sections.
38
39 See the DWARF5 Standard: section 7.3.5 and
40 examples in Appendix F.3.
41
42 A note about the index field from the index table.
43 See DWARF5 7.5.3.5.
44 The index table array index values are [1,S)
45 These value ae used to call functions requesting
46 values from the offset table and size table.
47
48 Inside the code in this file we subtract 1 and use
49 0 origin as that is how we arranged the
50 table access here.
51 A zero in the index table is an unused signature
52 table signature and unused index.
53
54 By subtracting one and arranging things properly
55 in the offset table and size table we can refer
56 to the tables in an identical simple fashion
57 These tables are thus U rows and N columns.
58 Technically the Offset table physically
59 row zero is a separate set of numbers translating
60 the column number to a DW_SECT* value
61 so callers can request specific bases(offsets)
62 and sizes from the offset and size tables.
63 But we change things a little internally so both
64 tables look zero-origin.
65 */
66
67 #include <config.h>
68
69 #include <string.h> /* memcmp() memcpy() strcmp() */
70
71 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
72 #include "stdafx.h"
73 #endif /* HAVE_STDAFX_H */
74
75 #include "dwarf.h"
76 #include "libdwarf.h"
77 #include "libdwarf_private.h"
78 #include "dwarf_base_types.h"
79 #include "dwarf_opaque.h"
80 #include "dwarf_alloc.h"
81 #include "dwarf_error.h"
82 #include "dwarf_util.h"
83 #include "dwarf_xu_index.h"
84 #include "dwarf_string.h"
85
86 #define HASHSIGNATURELEN 8
87
88 /* zerohashkey used as all-zero-bits for comparison. */
89 static const Dwarf_Sig8 zerohashkey;
90
91 #if 0 /* dump_bytes */
92 static void
93 dump_bytes(char * msg,Dwarf_Small * start, long len)
94 {
95 Dwarf_Small *end = start + len;
96 Dwarf_Small *cur = start;
97
98 printf("%s ",msg);
99 for (; cur < end; cur++) {
100 printf("%02x ", *cur);
101 }
102 printf("\n");
103 }
104 #endif /*0*/
105
106 /* Precondition: headerline_offset + N*32 is within
107 the section. */
108 static int
109 fill_in_offsets_headerline(Dwarf_Debug dbg,
110 Dwarf_Xu_Index_Header xuhdr,
111 Dwarf_Unsigned headerline_offset,
112 Dwarf_Unsigned num_sects,
113 Dwarf_Error *error)
114 {
115 Dwarf_Small *section_start = xuhdr->gx_section_data;
116 Dwarf_Small *section_end = xuhdr->gx_section_data+
117 xuhdr->gx_section_length;
118 Dwarf_Small *data = 0;
119 unsigned i = 0;
120
121 data = section_start +headerline_offset;
122 for ( ; i < num_sects ; ++i) {
123 Dwarf_Unsigned v = 0;
124
125 READ_UNALIGNED_CK(dbg,v, Dwarf_Unsigned,
126 data,SIZEOFT32,
127 error,section_end);
128 data += SIZEOFT32;
129 if (v > DW_SECT_RNGLISTS) {
130 dwarfstring s;
131
132 dwarfstring_constructor(&s);
133 dwarfstring_append_printf_u(&s,
134 "ERROR: DW_DLE_XU_NAME_COL_ERROR The "
135 "section number of %u ",v);
136 dwarfstring_append(&s," is too high. "
137 "Sections 1-8 are listed in "
138 "DWARF5 Table 7.1.");
139 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
140 dwarfstring_string(&s));
141 dwarfstring_destructor(&s);
142 return DW_DLV_ERROR;
143 }
144 xuhdr->gx_section_id[i] = (unsigned long)v;
145 }
146 return DW_DLV_OK;
147 }
148
149 /* Read in a cu or tu section and
150 return overview information.
151
152 For libdwarf-internal lookups
153 dwarf_init*() calls
154 dwarf_get_xu_index_header() when
155 the object file is opened and
156 dwarf_dealloc_xu_header() is called
157 by dwarf_finish(), there is
158 no need for users to do this.
159
160 If one wants to call the various
161 tu/cu functions oneself (possibly to print the
162 .debug_cu_index or .debug_tu_index sections).
163 then you will need to call dwarf_get_xu_index_header()
164 and eventually dwarf_dealloc_xu_header().
165
166 The libdwarf-internal data is kept in Dwarf_Debug
167 fields de_cu_hashindex_data/de_tu_hashindex_data.
168 */
169 int
170 dwarf_get_xu_index_header(Dwarf_Debug dbg,
171 /* Pass in section_type "tu" or "cu" */
172 const char *section_type,
173 Dwarf_Xu_Index_Header *xuptr,
174 Dwarf_Unsigned *version,
175 Dwarf_Unsigned *number_of_columns, /* L section count.*/
176 Dwarf_Unsigned *number_of_CUs, /* U unit count */
177 Dwarf_Unsigned *number_of_slots, /* S slot count */
178 /* Standard says S > U DWARF5 sec 7.3.5.3 */
179 const char **section_name,
180 Dwarf_Error *error)
181 {
182 Dwarf_Xu_Index_Header indexptr = 0;
183 int res = DW_DLV_ERROR;
184 struct Dwarf_Section_s *sect = 0;
185 Dwarf_Unsigned local_version = 0;
186 Dwarf_Unsigned num_secs = 0;
187 Dwarf_Unsigned num_CUs = 0;
188 Dwarf_Unsigned num_slots = 0;
189 Dwarf_Small *data = 0;
190 Dwarf_Unsigned tables_end_offset = 0;
191 Dwarf_Unsigned hash_tab_offset = 0;
192 Dwarf_Unsigned indexes_tab_offset = 0;
193 Dwarf_Unsigned section_offsets_tab_offset = 0;
194 Dwarf_Unsigned section_offsets_headerline_offset = 0;
195 Dwarf_Unsigned section_sizes_tab_offset = 0;
196 unsigned datalen32 = SIZEOFT32;
197 Dwarf_Small *section_end = 0;
198
199 CHECK_DBG(dbg,error,"dwarf_get_xu_index_header()");
200 if (!section_type || !xuptr) {
201 _dwarf_error_string(0,error,DW_DLE_XU_TYPE_ARG_ERROR,
202 "DW_DLE_XU_TYPE_ARG_ERROR: section type or header "
203 "return pointer is not valid");
204 return DW_DLV_ERROR;
205 }
206
207 if (!strcmp(section_type,"cu") ) {
208 sect = &dbg->de_debug_cu_index;
209 } else if (!strcmp(section_type,"tu") ) {
210 sect = &dbg->de_debug_tu_index;
211 } else {
212 _dwarf_error(dbg, error, DW_DLE_XU_TYPE_ARG_ERROR);
213 return DW_DLV_ERROR;
214 }
215 if (!sect->dss_size) {
216 return DW_DLV_NO_ENTRY;
217 }
218 if (!sect->dss_data) {
219 res = _dwarf_load_section(dbg, sect,error);
220 if (res != DW_DLV_OK) {
221 return res;
222 }
223 }
224
225 data = sect->dss_data;
226 section_end = data + sect->dss_size;
227
228 if (sect->dss_size < (4*datalen32) ) {
229 dwarfstring m;
230
231 dwarfstring_constructor(&m);
232 dwarfstring_append_printf_s(&m,
233 "DW_DLE_ERRONEOUS_XU_INDEX_SECTION: "
234 "The size of the %s ",
235 (char *)section_type);
236 dwarfstring_append_printf_u(&m,
237 "is just %u bytes, much to small to be "
238 " a correct section",
239 sect->dss_size);
240 _dwarf_error_string(dbg, error,
241 DW_DLE_ERRONEOUS_XU_INDEX_SECTION,
242 dwarfstring_string(&m));
243 dwarfstring_destructor(&m);
244 return DW_DLV_ERROR;
245 }
246 READ_UNALIGNED_CK(dbg,local_version, Dwarf_Unsigned,
247 data,datalen32,
248 error,section_end);
249 data += datalen32;
250
251 /* reading N */
252 READ_UNALIGNED_CK(dbg,num_secs, Dwarf_Unsigned,
253 data,datalen32,
254 error,section_end);
255 if (num_secs > DW_SECT_RNGLISTS) {
256 dwarfstring m;
257
258 dwarfstring_constructor(&m);
259 dwarfstring_append_printf_s(&m,
260 "DW_DLE_XU_NAME_COL_ERROR: "
261 " %s index section header ",
262 (char *)section_type);
263 dwarfstring_append_printf_u(&m,
264 "shows N, the sections count, "
265 "as %u but only values "
266 " 1 through 8 (DW_SECT_RNGLISTS) are valid.",
267 num_secs);
268 _dwarf_error_string(dbg,error,DW_DLE_XU_NAME_COL_ERROR,
269 dwarfstring_string(&m));
270 dwarfstring_destructor(&m);
271 return DW_DLV_ERROR;
272 }
273 data += datalen32;
274 /* reading U */
275 READ_UNALIGNED_CK(dbg,num_CUs, Dwarf_Unsigned,
276 data,datalen32,
277 error,section_end);
278 data += datalen32;
279 /* reading S */
280 READ_UNALIGNED_CK(dbg,num_slots, Dwarf_Unsigned,
281 data,datalen32,
282 error,section_end);
283 hash_tab_offset = datalen32*4;
284 indexes_tab_offset = hash_tab_offset +
285 (num_slots * HASHSIGNATURELEN);
286 /* Look for corrupt section data. */
287 if (num_slots > sect->dss_size) {
288 dwarfstring m;
289
290 dwarfstring_constructor(&m);
291 dwarfstring_append_printf_s(&m,
292 "DW_DLE_ERRONEOUS_XU_INDEX_SECTION: "
293 "The size of the %s ",(char *)section_type);
294 dwarfstring_append_printf_u(&m,
295 " is just %u bytes,",sect->dss_size);
296 dwarfstring_append_printf_u(&m,
297 "while the number of slots (S) is %u. "
298 "which is clearly wrong",num_slots );
299 _dwarf_error_string(dbg, error,
300 DW_DLE_ERRONEOUS_XU_INDEX_SECTION,
301 dwarfstring_string(&m));
302 dwarfstring_destructor(&m);
303 return DW_DLV_ERROR;
304 }
305 if ( (4*num_slots) > sect->dss_size) {
306 dwarfstring m;
307
308 dwarfstring_constructor(&m);
309 dwarfstring_append_printf_s(&m,
310 "DW_DLE_ERRONEOUS_XU_INDEX_SECTION: "
311 "The size of the %s ",(char *)section_type);
312 dwarfstring_append_printf_u(&m,
313 " is just %u bytes,",sect->dss_size);
314 dwarfstring_append_printf_u(&m,
315 "while the number of slots bytes (S) is at least %u. "
316 "which is clearly wrong",num_slots*4);
317 _dwarf_error_string(dbg, error,
318 DW_DLE_ERRONEOUS_XU_INDEX_SECTION,
319 dwarfstring_string(&m));
320 dwarfstring_destructor(&m);
321 return DW_DLV_ERROR;
322 }
323
324 /* This offset is to 1 row of N columns, each 32bit. */
325 section_offsets_headerline_offset = indexes_tab_offset +
326 (num_slots *datalen32);
327 /* Now we can make the real table part index normally.
328 This offset is to U row of N columns, each 32bit. */
329 section_offsets_tab_offset = section_offsets_headerline_offset
330 + (num_secs*datalen32);
331 if ( num_secs > sect->dss_size) {
332 dwarfstring m;
333
334 dwarfstring_constructor(&m);
335 dwarfstring_append_printf_s(&m,
336 "DW_DLE_ERRONEOUS_XU_INDEX_SECTION: "
337 "The size of the %s ",(char *)section_type);
338 dwarfstring_append_printf_u(&m,
339 " is just %u bytes,",sect->dss_size);
340 dwarfstring_append_printf_u(&m,
341 "while the number of sections/columns (S) is %u. "
342 "which is clearly wrong",num_secs );
343 _dwarf_error_string(dbg, error,
344 DW_DLE_ERRONEOUS_XU_INDEX_SECTION,
345 dwarfstring_string(&m));
346 dwarfstring_destructor(&m);
347 return DW_DLV_ERROR;
348 }
349 if ( (datalen32*num_secs) > sect->dss_size) {
350 dwarfstring m;
351
352 dwarfstring_constructor(&m);
353 dwarfstring_append_printf_s(&m,
354 "DW_DLE_ERRONEOUS_XU_INDEX_SECTION: "
355 "The size of the %s ",(char *)section_type);
356 dwarfstring_append_printf_u(&m,
357 " is just %u bytes,",sect->dss_size);
358 dwarfstring_append_printf_u(&m,
359 "while the number of sections/columns bytes (S)"
360 " is at least %u. "
361 "which is clearly wrong",num_secs*4);
362 _dwarf_error_string(dbg, error,
363 DW_DLE_ERRONEOUS_XU_INDEX_SECTION,
364 dwarfstring_string(&m));
365 dwarfstring_destructor(&m);
366 return DW_DLV_ERROR;
367 }
368 section_sizes_tab_offset = section_offsets_tab_offset +
369 (num_CUs *num_secs* datalen32) ;
370 tables_end_offset = section_sizes_tab_offset +
371 (num_CUs * num_secs * datalen32);
372 if ( tables_end_offset > sect->dss_size) {
373 /* Something is badly wrong here. */
374 dwarfstring m;
375
376 dwarfstring_constructor(&m);
377 dwarfstring_append_printf_u(&m,"ERROR: "
378 "DW_DLE_ERRONEOUS_XU_INDEX_SECTION as the end offset "
379 "0x%lx is greater than ",tables_end_offset);
380 dwarfstring_append_printf_u(&m,"the section size "
381 "0x%lx.",sect->dss_size);
382 _dwarf_error_string(dbg, error,
383 DW_DLE_ERRONEOUS_XU_INDEX_SECTION,
384 dwarfstring_string(&m));
385 dwarfstring_destructor(&m);
386 return DW_DLV_ERROR;
387 }
388 indexptr = (Dwarf_Xu_Index_Header)
389 _dwarf_get_alloc(dbg,DW_DLA_XU_INDEX,1);
390 if (indexptr == NULL) {
391 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
392 return DW_DLV_ERROR;
393 }
394 /* Only "cu" or "tu" allowed, that is checked above.
395 But for safety we just copy the allowed bytes*/
396 indexptr->gx_type[0] = section_type[0];
397 indexptr->gx_type[1] = section_type[1];
398 indexptr->gx_type[2] = 0;
399 indexptr->gx_dbg = dbg;
400 indexptr->gx_section_length = sect->dss_size;
401 indexptr->gx_section_data = sect->dss_data;
402 indexptr->gx_section_name = sect->dss_name;
403 indexptr->gx_version = local_version;
404 indexptr->gx_column_count_sections = num_secs;
405 indexptr->gx_units_in_index = num_CUs;
406 indexptr->gx_slots_in_hash = num_slots;
407 indexptr->gx_hash_table_offset = hash_tab_offset;
408 indexptr->gx_index_table_offset = indexes_tab_offset;
409 indexptr->gx_section_offsets_headerline_offset=
410 section_offsets_headerline_offset;
411 indexptr->gx_section_offsets_offset= section_offsets_tab_offset;
412 indexptr->gx_section_sizes_offset = section_sizes_tab_offset;
413 res = fill_in_offsets_headerline(dbg,indexptr,
414 section_offsets_headerline_offset,
415 num_secs,error);
416 if (res != DW_DLV_OK) {
417 dwarf_dealloc(dbg,indexptr,DW_DLA_XU_INDEX);
418 return res;
419 }
420 *xuptr = indexptr;
421 *version = indexptr->gx_version;
422 *number_of_columns = indexptr->gx_column_count_sections;
423 *number_of_CUs = indexptr->gx_units_in_index;
424 *number_of_slots = indexptr->gx_slots_in_hash;
425 *section_name = indexptr->gx_section_name;
426 return DW_DLV_OK;
427 }
428
429 int dwarf_get_xu_index_section_type(Dwarf_Xu_Index_Header xuhdr,
430 /* the function returns a pointer to
431 the immutable string "tu" or "cu" via
432 this arg. Do not free. */
433 const char **typename,
434 /* the function returns a pointer to
435 the immutable section name. Do not free.
436 .debug_cu_index or .debug_tu_index */
437 const char **sectionname,
438 Dwarf_Error *error)
439 {
440 (void)error;
441 *typename = &xuhdr->gx_type[0];
442 *sectionname = xuhdr->gx_section_name;
443 return DW_DLV_OK;
444 }
445
446 /* Index values 0 to S-1 are valid. */
447 int dwarf_get_xu_hash_entry(Dwarf_Xu_Index_Header xuhdr,
448 Dwarf_Unsigned index,
449 /* returns the hash value. 64 bits. */
450 Dwarf_Sig8 *hash_value,
451
452 /* returns the index into rows of offset/size tables. */
453 Dwarf_Unsigned *index_to_sections,
454 Dwarf_Error *error)
455 {
456 Dwarf_Debug dbg = xuhdr->gx_dbg;
457 Dwarf_Small *hashtab = xuhdr->gx_section_data +
458 xuhdr->gx_hash_table_offset;
459 Dwarf_Small *indextab = xuhdr->gx_section_data +
460 xuhdr->gx_index_table_offset;
461 Dwarf_Small *indexentry = 0;
462 Dwarf_Small *hashentry = 0;
463 Dwarf_Unsigned indexval = 0;
464 Dwarf_Small *section_end = xuhdr->gx_section_data +
465 xuhdr->gx_section_length;
466
467 if (!xuhdr->gx_slots_in_hash) {
468 _dwarf_error_string(dbg, error, DW_DLE_XU_HASH_ROW_ERROR,
469 "DW_DLE_XU_HASH_ROW_ERROR the number of slots is zero"
470 " which seems wrong.");
471 return DW_DLV_ERROR;
472 }
473 if (index >= xuhdr->gx_slots_in_hash) {
474 return DW_DLV_NO_ENTRY;
475 }
476 hashentry = hashtab + (index * HASHSIGNATURELEN);
477 memcpy(hash_value,hashentry,HASHSIGNATURELEN);
478 indexentry = indextab + (index * SIZEOFT32);
479 if (indexentry >= section_end ||
480 (indexentry+SIZEOFT32) > section_end) {
481 dwarfstring m;
482
483 dwarfstring_constructor(&m);
484 dwarfstring_append_printf_u(&m,
485 "DW_DLE_XU_HASH_ROW_ERROR the index passed in, "
486 " %u, results in entry not fitting"
487 " in the hash table (past end of section)",index);
488 _dwarf_error_string(dbg, error, DW_DLE_XU_HASH_ROW_ERROR,
489 dwarfstring_string(&m));
490 dwarfstring_destructor(&m);
491 return DW_DLV_ERROR;
492 }
493 READ_UNALIGNED_CK(dbg,indexval,Dwarf_Unsigned, indexentry,
494 SIZEOFT32,
495 error,section_end);
496 indexentry += SIZEOFT32;
497 if (indexval > xuhdr->gx_units_in_index) {
498 _dwarf_error(dbg, error, DW_DLE_XU_HASH_INDEX_ERROR);
499 return DW_DLV_ERROR;
500 }
501 *index_to_sections = indexval;
502 return DW_DLV_OK;
503 }
504
505 static const char * dwp_secnames[] = {
506 "No name for zero",
507 "DW_SECT_INFO" /* 1 */ /*".debug_info.dwo"*/,
508 "DW_SECT_TYPES" /* 2 */ /*".debug_types.dwo"*/,
509 "DW_SECT_ABBREV" /* 3 */ /*".debug_abbrev.dwo"*/,
510 "DW_SECT_LINE" /* 4 */ /*".debug_line.dwo"*/,
511 "DW_SECT_LOC" /* 5 */ /*".debug_loc.dwo"*/,
512 "DW_SECT_STR_OFFSETS" /* 6 */ /*".debug_str_offsets.dwo"*/,
513 "DW_SECT_MACRO" /* 7 */ /*".debug_macro.dwo"*/,
514 "DW_SECT_RNGLISTS" /* 8 */ /*".debug_rnglists.dwo"*/,
515 "No name > 8",
516 };
517
518 /* Row 0 of the Table of Section Offsets,
519 columns 0 to N-1, are the section id's,
520 and names, such as DW_SECT_INFO (ie, 1) */
521 int
522 dwarf_get_xu_section_names(Dwarf_Xu_Index_Header xuhdr,
523 Dwarf_Unsigned column_index,
524 Dwarf_Unsigned *number,
525 const char **name,
526 Dwarf_Error *error)
527 {
528 Dwarf_Unsigned sec_num = 0;
529 Dwarf_Debug dbg = 0;
530 if (!xuhdr) {
531 _dwarf_error_string(0,error,DW_DLE_XU_TYPE_ARG_ERROR,
532 "DW_DLE_XU_TYPE_ARG_ERROR: "
533 "Dwarf_Xu_Index_Header is NULL");
534 return DW_DLV_ERROR;
535 }
536 dbg = xuhdr->gx_dbg;
537 /* FIXME */
538 CHECK_DBG(dbg,error,"dwarf_get_xu_section_names()");
539 if ( column_index >= xuhdr->gx_column_count_sections) {
540 dwarfstring s;
541
542 dwarfstring_constructor(&s);
543 dwarfstring_append_printf_u(&s,
544 "ERROR: DW_DLE_XU_NAME_COL_ERROR as the "
545 "column index of %u ",column_index);
546 dwarfstring_append_printf_u(&s," is too high. "
547 "There are %u sections.",
548 xuhdr->gx_column_count_sections);
549 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
550 dwarfstring_string(&s));
551 dwarfstring_destructor(&s);
552 return DW_DLV_ERROR;
553 }
554 sec_num = xuhdr->gx_section_id[column_index];
555 if (sec_num < 1) {
556 return DW_DLV_NO_ENTRY;
557 }
558 *number = sec_num;
559 *name = dwp_secnames[sec_num];
560 return DW_DLV_OK;
561 }
562
563 /* Rows 0 to U-1
564 col 0 to L-1
565 are section offset and length values from
566 the Table of Section Offsets and Table of Section Sizes.
567 The formally the table of section offsets is a header
568 line of the section offsets we subtract 1 from
569 the incoming irow_index as our tables are
570 now zero origin. */
571 int
572 dwarf_get_xu_section_offset(Dwarf_Xu_Index_Header xuhdr,
573 Dwarf_Unsigned irow_index,
574 Dwarf_Unsigned column_index,
575 Dwarf_Unsigned *sec_offset,
576 Dwarf_Unsigned *sec_size,
577 Dwarf_Error *error)
578 {
579 /* We use zero origin in the arrays, Users see
580 one origin from the hash table. */
581 Dwarf_Debug dbg = 0;
582 /* get to base of tables first. */
583 Dwarf_Small *offsetrow = 0;
584 Dwarf_Small *sizerow = 0;
585 Dwarf_Small *offsetentry = 0;
586 Dwarf_Small *sizeentry = 0;
587 Dwarf_Unsigned offset = 0;
588 Dwarf_Unsigned size = 0;
589 Dwarf_Unsigned column_count = 0;
590 Dwarf_Small *section_end = 0;
591 Dwarf_Unsigned row_index = irow_index-1;
592
593 if (!xuhdr) {
594 _dwarf_error_string(0,error,DW_DLE_XU_TYPE_ARG_ERROR,
595 "DW_DLE_XU_TYPE_ARG_ERROR: "
596 "Dwarf_Xu_Index_Header pointer is null");
597 return DW_DLV_ERROR;
598 }
599 /* FIXME */
600 dbg = xuhdr->gx_dbg;
601 CHECK_DBG(dbg,error,"dwarf_get_xu_section_offset()");
602 sizerow = xuhdr->gx_section_sizes_offset +
603 xuhdr->gx_section_data;
604 offsetrow = xuhdr->gx_section_offsets_offset +
605 xuhdr->gx_section_data;
606 column_count = xuhdr->gx_column_count_sections;
607 section_end = xuhdr->gx_section_data + xuhdr->gx_section_length;
608
609 if (!irow_index) {
610 dwarfstring s;
611
612 dwarfstring_constructor(&s);
613 dwarfstring_append(&s,
614 "ERROR: DW_DLE_ERRONEOUS_XU_INDEX_SECTION "
615 "The row index passed to dwarf_get_xu_section_offset() "
616 "is zero, which is not a valid row in "
617 " the offset-table or the size table as we think"
618 " of them as 1-origin.");
619 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
620 dwarfstring_string(&s));
621 dwarfstring_destructor(&s);
622 return DW_DLV_ERROR;
623 }
624 if (row_index >= xuhdr->gx_units_in_index) {
625 dwarfstring s;
626
627 dwarfstring_constructor(&s);
628 dwarfstring_append_printf_u(&s,
629 "ERROR: DW_DLE_XU_NAME_COL_ERROR as the "
630 "row index of %u ",row_index);
631 dwarfstring_append_printf_u(&s," is too high. "
632 "Valid units must be < %u ",xuhdr->gx_units_in_index);
633 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
634 dwarfstring_string(&s));
635 dwarfstring_destructor(&s);
636 return DW_DLV_ERROR;
637 }
638
639 if (column_index >= xuhdr->gx_column_count_sections) {
640 dwarfstring s;
641
642 dwarfstring_constructor(&s);
643 dwarfstring_append_printf_u(&s,
644 "ERROR: DW_DLE_XU_NAME_COL_ERROR as the "
645 "column index of %u ",column_index);
646 dwarfstring_append_printf_u(&s," is too high. "
647 "Valid column indexes must be < %u ",
648 xuhdr->gx_column_count_sections);
649 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
650 dwarfstring_string(&s));
651 dwarfstring_destructor(&s);
652 return DW_DLV_ERROR;
653 }
654 /* As noted above we have hidden the extra initial
655 row from the offsets table so it is just
656 0 to U-1. */
657 offsetrow = offsetrow + (row_index*column_count * SIZEOFT32);
658 offsetentry = offsetrow + (column_index * SIZEOFT32);
659
660 sizerow = sizerow + (row_index*column_count * SIZEOFT32);
661 sizeentry = sizerow + (column_index * SIZEOFT32);
662
663 {
664 READ_UNALIGNED_CK(dbg,offset,Dwarf_Unsigned,
665 offsetentry,
666 SIZEOFT32,error,section_end);
667 offsetentry += SIZEOFT32;
668
669 READ_UNALIGNED_CK(dbg,size,Dwarf_Unsigned,
670 sizeentry,
671 SIZEOFT32,error,section_end);
672 sizeentry += SIZEOFT32;
673 }
674 *sec_offset = offset;
675 *sec_size = size;
676 return DW_DLV_OK;
677 }
678
679 static int
680 _dwarf_search_fission_for_key(Dwarf_Debug dbg,
681 Dwarf_Xu_Index_Header xuhdr,
682 Dwarf_Sig8 *key_in,
683 Dwarf_Unsigned * percu_index_out,
684 Dwarf_Error *error)
685 {
686 Dwarf_Unsigned key = 0;
687 Dwarf_Unsigned slots = xuhdr->gx_slots_in_hash;
688 Dwarf_Sig8 hashentry_key;
689 Dwarf_Unsigned percu_index = 0;
690 Dwarf_Unsigned h = 0;
691
692 (void)dbg;
693 hashentry_key = zerohashkey;
694 /* Look for corrupt section data. */
695 if (slots > xuhdr->gx_section_length) {
696 dwarfstring s;
697
698 dwarfstring_constructor(&s);
699 dwarfstring_append_printf_u(&s,
700 "ERROR: DW_DLE_XU_NAME_COL_ERROR as the "
701 "slots count of %u ",slots);
702 dwarfstring_append_printf_u(&s," is too high. "
703 "given the section length of %u\n",
704 xuhdr->gx_section_length);
705 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
706 dwarfstring_string(&s));
707 dwarfstring_destructor(&s);
708 return DW_DLV_ERROR;
709 }
710 if (!slots) {
711 return DW_DLV_NO_ENTRY;
712 }
713 if ( (4*slots) > xuhdr->gx_section_length) {
714 dwarfstring s;
715
716 dwarfstring_constructor(&s);
717 dwarfstring_append_printf_u(&s,
718 "ERROR: DW_DLE_XU_NAME_COL_ERROR as the "
719 "slots count *4 of %u ",slots*4);
720 dwarfstring_append_printf_u(&s," is too high. "
721 "given the section length of %u\n",
722 xuhdr->gx_section_length);
723 _dwarf_error_string(dbg, error, DW_DLE_XU_NAME_COL_ERROR,
724 dwarfstring_string(&s));
725 dwarfstring_destructor(&s);
726 return DW_DLV_ERROR;
727 }
728 if (sizeof(key) != sizeof(*key_in)) {
729 /* The hash won't work right in this case */
730 dwarfstring s;
731
732 dwarfstring_constructor(&s);
733 dwarfstring_append_printf_u(&s,
734 "DW_DLE_XU_HASH_ROW_ERROR "
735 "We require 8 byte keys, not %u byte keys",
736 sizeof(key));
737 _dwarf_error_string(dbg, error, DW_DLE_XU_HASH_ROW_ERROR,
738 dwarfstring_string(&s));
739 dwarfstring_destructor(&s);
740 return DW_DLV_ERROR;
741 }
742 ASNARL(key,key_in,sizeof(*key_in));
743 /* If we built a lookup based on hash key
744 this loop would not be necessary. */
745 for (h = 0; h < slots; ++h) {
746 int res = 0;
747
748 res = dwarf_get_xu_hash_entry(xuhdr,
749 h,&hashentry_key,
750 &percu_index,error);
751 if (res != DW_DLV_OK) {
752 return res;
753 }
754 if (percu_index == 0 &&
755 !memcmp(&hashentry_key,&zerohashkey,sizeof(Dwarf_Sig8))) {
756 continue;
757 }
758 if (!memcmp(key_in,&hashentry_key,sizeof(Dwarf_Sig8))) {
759 /* FOUND */
760 *percu_index_out = percu_index;
761 return DW_DLV_OK;
762 }
763 }
764 return DW_DLV_NO_ENTRY;
765 }
766
767 /* Slow. Consider tsearch. */
768 /* For type units and for CUs. */
769 /* We're finding an index entry refers
770 to a global offset in some CU
771 and hence is unique in the target. */
772 static int
773 _dwarf_search_fission_for_offset(Dwarf_Debug dbg,
774 Dwarf_Xu_Index_Header xuhdr,
775 Dwarf_Unsigned offset,
776 Dwarf_Unsigned dfp_sect_num, /* DW_SECT_INFO or TYPES */
777 Dwarf_Unsigned * percu_index_out,
778 Dwarf_Sig8 * key_out,
779 Dwarf_Error *error)
780 {
781 Dwarf_Unsigned i = 0;
782 Dwarf_Unsigned m = 0;
783 Dwarf_Unsigned secnum_index = 0;
784 Dwarf_Bool found_secnum = FALSE;
785 int res = 0;
786
787 for ( i = 0; i< xuhdr->gx_column_count_sections; i++) {
788 /* We could put the secnums array into xuhdr
789 if looping here is too slow. */
790 const char *name = 0;
791 Dwarf_Unsigned num = 0;
792 res = dwarf_get_xu_section_names(xuhdr,i,&num,&name,error);
793 if (res != DW_DLV_OK) {
794 return res;
795 }
796 if (num == dfp_sect_num) {
797 secnum_index = i;
798 found_secnum = TRUE;
799 break;
800 }
801 }
802 if (FALSE == found_secnum) {
803 _dwarf_error(dbg,error,DW_DLE_FISSION_SECNUM_ERR);
804 return DW_DLV_ERROR;
805 }
806 for ( m = 0; m < xuhdr->gx_slots_in_hash; ++m) {
807 Dwarf_Sig8 hash;
808 Dwarf_Unsigned indexn = 0;
809 Dwarf_Unsigned sec_offset = 0;
810 Dwarf_Unsigned sec_size = 0;
811
812 res = dwarf_get_xu_hash_entry(xuhdr,m,&hash,&indexn,error);
813 if (res != DW_DLV_OK) {
814 return res;
815 }
816 if (indexn == 0 &&
817 !memcmp(&hash,&zerohashkey,sizeof(Dwarf_Sig8))) {
818 /* Empty slot. */
819 continue;
820 }
821
822 res = dwarf_get_xu_section_offset(xuhdr,
823 indexn,secnum_index,&sec_offset,&sec_size,error);
824 if (res != DW_DLV_OK) {
825 return res;
826 }
827 if (sec_offset != offset) {
828 continue;
829 }
830 *percu_index_out = indexn;
831 *key_out = hash;
832 return DW_DLV_OK;
833 }
834 return DW_DLV_NO_ENTRY;
835 }
836
837 static int
838 _dwarf_get_xuhdr(Dwarf_Debug dbg,
839 const char *sigtype,
840 Dwarf_Xu_Index_Header *xuout,
841 Dwarf_Error *error)
842 {
843 if (!strcmp(sigtype,"tu")) {
844 if (!dbg->de_tu_hashindex_data) {
845 return DW_DLV_NO_ENTRY;
846 }
847 *xuout = dbg->de_tu_hashindex_data;
848 } else if (!strcmp(sigtype,"cu")) {
849 if (!dbg->de_cu_hashindex_data) {
850 return DW_DLV_NO_ENTRY;
851 }
852 *xuout = dbg->de_cu_hashindex_data;
853 } else {
854 _dwarf_error(dbg,error,DW_DLE_SIG_TYPE_WRONG_STRING);
855 return DW_DLV_ERROR;
856 }
857 return DW_DLV_OK;
858
859 }
860
861 static int
862 transform_xu_to_dfp(Dwarf_Xu_Index_Header xuhdr,
863 Dwarf_Unsigned percu_index,
864 Dwarf_Sig8 *key,
865 const char *sig_type,
866 Dwarf_Debug_Fission_Per_CU * percu_out,
867 Dwarf_Error *error)
868 {
869 Dwarf_Unsigned i = 0;
870 Dwarf_Unsigned l = 0;
871 Dwarf_Unsigned n = 1;
872 Dwarf_Unsigned max_cols =
873 xuhdr->gx_column_count_sections;/* L */
874 Dwarf_Unsigned secnums[DW_FISSION_SECT_COUNT];
875 int res = 0;
876
877 for ( i = 0; i< max_cols; i++) {
878 /* We could put the secnums array into xuhdr
879 if recreating it is too slow. */
880 const char *name = 0;
881 Dwarf_Unsigned num = 0;
882 res = dwarf_get_xu_section_names(xuhdr,i,&num,&name,error);
883 if (res != DW_DLV_OK) {
884 return res;
885 }
886 secnums[i] = num;
887 }
888 n = percu_index;
889 for (l = 0; l < max_cols; ++l) { /* L */
890 Dwarf_Unsigned sec_off = 0;
891 Dwarf_Unsigned sec_size = 0;
892 Dwarf_Unsigned l_as_sect = secnums[l];
893 res = dwarf_get_xu_section_offset(xuhdr,n,l,
894 &sec_off,&sec_size,error);
895 if (res != DW_DLV_OK) {
896 return res;
897 }
898 percu_out->pcu_offset[l_as_sect] = sec_off;
899 percu_out->pcu_size[l_as_sect] = sec_size;
900 }
901 percu_out->pcu_type = sig_type;
902 percu_out->pcu_index = percu_index;
903 percu_out->pcu_hash = *key;
904 return DW_DLV_OK;
905 }
906
907 /* This should only be called for a CU, never a TU.
908 For a TU the type hash is known while reading
909 the TU Header. Not so for a CU. */
910 int
911 _dwarf_get_debugfission_for_offset(Dwarf_Debug dbg,
912 Dwarf_Off offset_wanted,
913 const char * key_type, /* "cu" or "tu" */
914 struct Dwarf_Debug_Fission_Per_CU_s * percu_out,
915 Dwarf_Error *error)
916 {
917 Dwarf_Xu_Index_Header xuhdr = 0;
918 int sres = 0;
919 Dwarf_Unsigned percu_index = 0;
920 Dwarf_Unsigned sect_index_base = 0;
921 Dwarf_Sig8 key;
922
923 CHECK_DBG(dbg,error,"_dwarf_get_debugfission_for_offset()");
924 sect_index_base = DW_SECT_INFO;
925 key = zerohashkey;
926 sres = _dwarf_get_xuhdr(dbg,key_type, &xuhdr,error);
927 if (sres != DW_DLV_OK) {
928 return sres;
929 }
930 sres = _dwarf_search_fission_for_offset(dbg,
931 xuhdr,offset_wanted, sect_index_base, &percu_index,
932 &key,
933 error);
934 if (sres != DW_DLV_OK) {
935 return sres;
936 }
937 sres = transform_xu_to_dfp(xuhdr,percu_index,&key,
938 key_type,percu_out, error);
939 return sres;
940
941 }
942 int
943 dwarf_get_debugfission_for_key(Dwarf_Debug dbg,
944 Dwarf_Sig8 *key /* pointer to hash signature */,
945 const char *key_type /* "cu" or "tu" */,
946 Dwarf_Debug_Fission_Per_CU * percu_out,
947 Dwarf_Error *error)
948 {
949 int sres = 0;
950 Dwarf_Unsigned percu_index = 0;
951 Dwarf_Xu_Index_Header xuhdr = 0;
952
953 CHECK_DBG(dbg,error,"dwarf_get_debugfission_for_key()");
954 if (!key || !key_type || !percu_out) {
955 _dwarf_error_string(0,error,DW_DLE_XU_TYPE_ARG_ERROR,
956 "DW_DLE_XU_TYPE_ARG_ERROR: dw_key, dw_keytype, or "
957 "Dwarf_Debug_Fission_Per_CU pointer* to return "
958 "is not valid");
959 return DW_DLV_ERROR;
960 }
961
962 sres = _dwarf_load_debug_info(dbg,error);
963 if (sres == DW_DLV_ERROR) {
964 return sres;
965 }
966 sres = _dwarf_load_debug_types(dbg,error);
967 if (sres == DW_DLV_ERROR) {
968 return sres;
969 }
970 /* Returns already existing xuhdr */
971 sres = _dwarf_get_xuhdr(dbg,key_type, &xuhdr,error);
972 if (sres != DW_DLV_OK) {
973 return sres;
974 }
975 /* Search in that xu data. */
976 sres = _dwarf_search_fission_for_key(dbg,
977 xuhdr,key,&percu_index,error);
978 if (sres != DW_DLV_OK) {
979 return sres;
980 }
981 sres = transform_xu_to_dfp(xuhdr,percu_index,key,
982 key_type,percu_out,error);
983 return sres;
984 }
985
986 void
987 dwarf_dealloc_xu_header(Dwarf_Xu_Index_Header indexptr)
988 {
989 if (indexptr) {
990 Dwarf_Debug dbg = indexptr->gx_dbg;
991 dwarf_dealloc(dbg,indexptr,DW_DLA_XU_INDEX);
992 }
993 }