Branch data Line data Source code
1 : : /* Internal definitions for libdw.
2 : : Copyright (C) 2002-2011, 2013-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 : : #ifndef _LIBDWP_H
30 : : #define _LIBDWP_H 1
31 : :
32 : : #include <libintl.h>
33 : : #include <stdbool.h>
34 : : #include <pthread.h>
35 : :
36 : : #include <libdw.h>
37 : : #include <dwarf.h>
38 : :
39 : :
40 : : /* Known location expressions already decoded. */
41 : : struct loc_s
42 : : {
43 : : void *addr;
44 : : Dwarf_Op *loc;
45 : : size_t nloc;
46 : : };
47 : :
48 : : /* Known DW_OP_implicit_value blocks already decoded.
49 : : This overlaps struct loc_s exactly, but only the
50 : : first member really has to match. */
51 : : struct loc_block_s
52 : : {
53 : : void *addr;
54 : : unsigned char *data;
55 : : size_t length;
56 : : };
57 : :
58 : : /* Already decoded .debug_line units. */
59 : : struct files_lines_s
60 : : {
61 : : Dwarf_Off debug_line_offset;
62 : : Dwarf_Files *files;
63 : : Dwarf_Lines *lines;
64 : : };
65 : :
66 : : /* Valid indices for the section data. */
67 : : enum
68 : : {
69 : : IDX_debug_info = 0,
70 : : IDX_debug_types,
71 : : IDX_debug_abbrev,
72 : : IDX_debug_aranges,
73 : : IDX_debug_addr,
74 : : IDX_debug_line,
75 : : IDX_debug_line_str,
76 : : IDX_debug_frame,
77 : : IDX_debug_loc,
78 : : IDX_debug_loclists,
79 : : IDX_debug_pubnames,
80 : : IDX_debug_str,
81 : : IDX_debug_str_offsets,
82 : : IDX_debug_macinfo,
83 : : IDX_debug_macro,
84 : : IDX_debug_ranges,
85 : : IDX_debug_rnglists,
86 : : IDX_gnu_debugaltlink,
87 : : IDX_last
88 : : };
89 : :
90 : :
91 : : /* Error values. */
92 : : enum
93 : : {
94 : : DWARF_E_NOERROR = 0,
95 : : DWARF_E_UNKNOWN_ERROR,
96 : : DWARF_E_INVALID_ACCESS,
97 : : DWARF_E_NO_REGFILE,
98 : : DWARF_E_IO_ERROR,
99 : : DWARF_E_INVALID_ELF,
100 : : DWARF_E_NO_DWARF,
101 : : DWARF_E_COMPRESSED_ERROR,
102 : : DWARF_E_NOELF,
103 : : DWARF_E_GETEHDR_ERROR,
104 : : DWARF_E_NOMEM,
105 : : DWARF_E_UNIMPL,
106 : : DWARF_E_INVALID_CMD,
107 : : DWARF_E_INVALID_VERSION,
108 : : DWARF_E_INVALID_FILE,
109 : : DWARF_E_NO_ENTRY,
110 : : DWARF_E_INVALID_DWARF,
111 : : DWARF_E_NO_STRING,
112 : : DWARF_E_NO_DEBUG_STR,
113 : : DWARF_E_NO_DEBUG_LINE_STR,
114 : : DWARF_E_NO_STR_OFFSETS,
115 : : DWARF_E_NO_ADDR,
116 : : DWARF_E_NO_CONSTANT,
117 : : DWARF_E_NO_REFERENCE,
118 : : DWARF_E_INVALID_REFERENCE,
119 : : DWARF_E_NO_DEBUG_LINE,
120 : : DWARF_E_INVALID_DEBUG_LINE,
121 : : DWARF_E_TOO_BIG,
122 : : DWARF_E_VERSION,
123 : : DWARF_E_INVALID_DIR_IDX,
124 : : DWARF_E_ADDR_OUTOFRANGE,
125 : : DWARF_E_NO_DEBUG_LOC,
126 : : DWARF_E_NO_DEBUG_LOCLISTS,
127 : : DWARF_E_NO_LOC_VALUE,
128 : : DWARF_E_NO_BLOCK,
129 : : DWARF_E_INVALID_LINE_IDX,
130 : : DWARF_E_INVALID_ARANGE_IDX,
131 : : DWARF_E_NO_MATCH,
132 : : DWARF_E_NO_FLAG,
133 : : DWARF_E_INVALID_OFFSET,
134 : : DWARF_E_NO_DEBUG_RANGES,
135 : : DWARF_E_NO_DEBUG_RNGLISTS,
136 : : DWARF_E_INVALID_CFI,
137 : : DWARF_E_NO_ALT_DEBUGLINK,
138 : : DWARF_E_INVALID_OPCODE,
139 : : DWARF_E_NOT_CUDIE,
140 : : DWARF_E_UNKNOWN_LANGUAGE,
141 : : DWARF_E_NO_DEBUG_ADDR,
142 : : };
143 : :
144 : :
145 : : #include "dwarf_sig8_hash.h"
146 : :
147 : : /* The type of Dwarf object, sorted by preference
148 : : (if there is a higher order type, we pick that one over the others). */
149 : : enum dwarf_type
150 : : {
151 : : TYPE_UNKNOWN = 0,
152 : : TYPE_GNU_LTO = 16,
153 : : TYPE_DWO = 32,
154 : : TYPE_PLAIN = 64,
155 : : };
156 : :
157 : : /* This is the structure representing the debugging state. */
158 : : struct Dwarf
159 : : {
160 : : /* The underlying ELF file. */
161 : : Elf *elf;
162 : :
163 : : /* The (absolute) path to the ELF dir, if known. To help locating
164 : : alt and dwo files. */
165 : : char *debugdir;
166 : :
167 : : /* dwz alternate DWARF file. */
168 : : Dwarf *alt_dwarf;
169 : :
170 : : /* The section data. */
171 : : Elf_Data *sectiondata[IDX_last];
172 : :
173 : : /* True if the file has a byte order different from the host. */
174 : : bool other_byte_order;
175 : :
176 : : /* If true, we allocated the ELF descriptor ourselves. */
177 : : bool free_elf;
178 : :
179 : : /* If >= 0, we allocated the alt_dwarf ourselves and must end it and
180 : : close this file descriptor. */
181 : : int alt_fd;
182 : :
183 : : /* Information for traversing the .debug_pubnames section. This is
184 : : an array and separately allocated with malloc. */
185 : : struct pubnames_s
186 : : {
187 : : Dwarf_Off cu_offset;
188 : : Dwarf_Off set_start;
189 : : unsigned int cu_header_size;
190 : : int address_len;
191 : : } *pubnames_sets;
192 : : size_t pubnames_nsets;
193 : :
194 : : /* Search tree for the CUs. */
195 : : void *cu_tree;
196 : : Dwarf_Off next_cu_offset;
197 : :
198 : : /* Search tree and sig8 hash table for .debug_types type units. */
199 : : void *tu_tree;
200 : : Dwarf_Off next_tu_offset;
201 : : Dwarf_Sig8_Hash sig8_hash;
202 : :
203 : : /* Search tree for split Dwarf associated with CUs in this debug. */
204 : : void *split_tree;
205 : :
206 : : /* Search tree for .debug_macro operator tables. */
207 : : void *macro_ops;
208 : :
209 : : /* Search tree for decoded .debug_line units. */
210 : : void *files_lines;
211 : :
212 : : /* Address ranges. */
213 : : Dwarf_Aranges *aranges;
214 : :
215 : : /* Cached info from the CFI section. */
216 : : struct Dwarf_CFI_s *cfi;
217 : :
218 : : /* Fake loc CU. Used when synthesizing attributes for Dwarf_Ops that
219 : : came from a location list entry in dwarf_getlocation_attr.
220 : : Depending on version this is the .debug_loc or .debug_loclists
221 : : section (could be both if mixing CUs with different DWARF versions). */
222 : : struct Dwarf_CU *fake_loc_cu;
223 : : struct Dwarf_CU *fake_loclists_cu;
224 : :
225 : : /* Similar for addrx/constx, which will come from .debug_addr section. */
226 : : struct Dwarf_CU *fake_addr_cu;
227 : :
228 : : enum dwarf_type type;
229 : :
230 : : /* Supporting lock for internal memory handling. Ensures threads that have
231 : : an entry in the mem_tails array are not disturbed by new threads doing
232 : : allocations for this Dwarf. */
233 : : pthread_rwlock_t mem_rwl;
234 : :
235 : : /* Internal memory handling. This is basically a simplified thread-local
236 : : reimplementation of obstacks. Unfortunately the standard obstack
237 : : implementation is not usable in libraries. */
238 : : size_t mem_stacks;
239 : : struct libdw_memblock
240 : : {
241 : : size_t size;
242 : : size_t remaining;
243 : : struct libdw_memblock *prev;
244 : : char mem[0];
245 : : } **mem_tails;
246 : :
247 : : /* Default size of allocated memory blocks. */
248 : : size_t mem_default_size;
249 : :
250 : : /* Registered OOM handler. */
251 : : Dwarf_OOM oom_handler;
252 : : };
253 : :
254 : :
255 : : /* Abbreviation representation. */
256 : : struct Dwarf_Abbrev
257 : : {
258 : : Dwarf_Off offset; /* Offset to start of abbrev into .debug_abbrev. */
259 : : unsigned char *attrp; /* Pointer to start of attribute name/form pairs. */
260 : : bool has_children : 1; /* Whether or not the DIE has children. */
261 : : unsigned int code : 31; /* The (unique) abbrev code. */
262 : : unsigned int tag; /* The tag of the DIE. */
263 : : } attribute_packed;
264 : :
265 : : #include "dwarf_abbrev_hash.h"
266 : :
267 : :
268 : : /* Files in line information records. */
269 : : struct Dwarf_Files_s
270 : : {
271 : : unsigned int ndirs;
272 : : unsigned int nfiles;
273 : : struct Dwarf_Fileinfo_s
274 : : {
275 : : char *name;
276 : : Dwarf_Word mtime;
277 : : Dwarf_Word length;
278 : : } info[0];
279 : : /* nfiles of those, followed by char *[ndirs]. */
280 : : };
281 : : typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
282 : :
283 : :
284 : : /* Representation of a row in the line table. */
285 : :
286 : : struct Dwarf_Line_s
287 : : {
288 : : Dwarf_Files *files;
289 : :
290 : : Dwarf_Addr addr;
291 : : unsigned int file;
292 : : int line;
293 : : unsigned short int column;
294 : : unsigned int is_stmt:1;
295 : : unsigned int basic_block:1;
296 : : unsigned int end_sequence:1;
297 : : unsigned int prologue_end:1;
298 : : unsigned int epilogue_begin:1;
299 : : /* The remaining bit fields are not flags, but hold values presumed to be
300 : : small. All the flags and other bit fields should add up to 48 bits
301 : : to give the whole struct a nice round size. */
302 : : unsigned int op_index:8;
303 : : unsigned int isa:8;
304 : : unsigned int discriminator:24;
305 : : /* These are currently only used for the NVIDIA extensions. */
306 : : unsigned int context;
307 : : unsigned int function_name;
308 : : };
309 : :
310 : : struct Dwarf_Lines_s
311 : : {
312 : : size_t nlines;
313 : : struct Dwarf_Line_s info[0];
314 : : };
315 : :
316 : : /* Representation of address ranges. */
317 : : struct Dwarf_Aranges_s
318 : : {
319 : : Dwarf *dbg;
320 : : size_t naranges;
321 : :
322 : : struct Dwarf_Arange_s
323 : : {
324 : : Dwarf_Addr addr;
325 : : Dwarf_Word length;
326 : : Dwarf_Off offset;
327 : : } info[0];
328 : : };
329 : :
330 : :
331 : : /* CU representation. */
332 : : struct Dwarf_CU
333 : : {
334 : : Dwarf *dbg;
335 : : Dwarf_Off start;
336 : : Dwarf_Off end;
337 : : uint8_t address_size;
338 : : uint8_t offset_size;
339 : : uint16_t version;
340 : :
341 : : size_t sec_idx; /* Normally .debug_info, could be .debug_type or "fake". */
342 : :
343 : : /* The unit type if version >= 5. Otherwise 0 for normal CUs (from
344 : : .debug_info) or 1 for v4 type units (from .debug_types). */
345 : : uint8_t unit_type;
346 : :
347 : : /* Zero if the unit type doesn't support a die/type offset and/or id/sig.
348 : : Nonzero if it is a v4 type unit or for DWARFv5 units depending on
349 : : unit_type. */
350 : : size_t subdie_offset;
351 : : uint64_t unit_id8;
352 : :
353 : : /* If this is a skeleton unit this points to the split compile unit.
354 : : Or the other way around if this is a split compile unit. Set to -1
355 : : if not yet searched. Always use __libdw_find_split_unit to access
356 : : this field. */
357 : : struct Dwarf_CU *split;
358 : :
359 : : /* Hash table for the abbreviations. */
360 : : Dwarf_Abbrev_Hash abbrev_hash;
361 : : /* Offset of the first abbreviation. */
362 : : size_t orig_abbrev_offset;
363 : : /* Offset past last read abbreviation. */
364 : : size_t last_abbrev_offset;
365 : :
366 : : /* The srcline information. */
367 : : Dwarf_Lines *lines;
368 : :
369 : : /* The source file information. */
370 : : Dwarf_Files *files;
371 : :
372 : : /* Known location lists. */
373 : : void *locs;
374 : :
375 : : /* Base address for use with ranges and locs.
376 : : Don't access directly, call __libdw_cu_base_address. */
377 : : Dwarf_Addr base_address;
378 : :
379 : : /* The offset into the .debug_addr section where index zero begins.
380 : : Don't access directly, call __libdw_cu_addr_base. */
381 : : Dwarf_Off addr_base;
382 : :
383 : : /* The offset into the .debug_str_offsets section where index zero begins.
384 : : Don't access directly, call __libdw_cu_str_off_base. */
385 : : Dwarf_Off str_off_base;
386 : :
387 : : /* The offset into the .debug_ranges section to use for GNU
388 : : DebugFission split units. Don't access directly, call
389 : : __libdw_cu_ranges_base. */
390 : : Dwarf_Off ranges_base;
391 : :
392 : : /* The start of the offset table in .debug_loclists.
393 : : Don't access directly, call __libdw_cu_locs_base. */
394 : : Dwarf_Off locs_base;
395 : :
396 : : /* Memory boundaries of this CU. */
397 : : void *startp;
398 : : void *endp;
399 : : };
400 : :
401 : : #define ISV4TU(cu) ((cu)->version == 4 && (cu)->sec_idx == IDX_debug_types)
402 : :
403 : : /* Compute the offset of a CU's first DIE from the CU offset.
404 : : CU must be a valid/known version/unit_type. */
405 : : static inline Dwarf_Off
406 : 3062444 : __libdw_first_die_from_cu_start (Dwarf_Off cu_start,
407 : : uint8_t offset_size,
408 : : uint16_t version,
409 : : uint8_t unit_type)
410 : : {
411 : : /*
412 : : assert (offset_size == 4 || offset_size == 8);
413 : : assert (version >= 2 && version <= 5);
414 : : assert (unit_type == DW_UT_compile
415 : : || unit_type == DW_UT_partial
416 : : || unit_type == DW_UT_skeleton
417 : : || unit_type == DW_UT_split_compile
418 : : || unit_type == DW_UT_type
419 : : || unit_type == DW_UT_split_type);
420 : : */
421 : :
422 : 3062444 : Dwarf_Off off = cu_start;
423 [ + + ]: 3062444 : if (version < 5)
424 : : {
425 : : /*
426 : : LEN VER OFFSET ADDR
427 : : 4-bytes + 2-bytes + 4-bytes + 1-byte for 32-bit dwarf
428 : : 12-bytes + 2-bytes + 8-bytes + 1-byte for 64-bit dwarf
429 : : or in .debug_types, SIGNATURE TYPE-OFFSET
430 : : 4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes for 32-bit
431 : : 12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes for 64-bit
432 : :
433 : : Note the trick in the computation. If the offset_size is 4
434 : : the '- 4' term changes the '3 *' (or '4 *') into a '2 *' (or '3 *).
435 : : If the offset_size is 8 it accounts for the 4-byte escape value
436 : : used at the start of the length. */
437 [ + + ]: 3061657 : if (unit_type != DW_UT_type)
438 : 3061606 : off += 3 * offset_size - 4 + 3;
439 : : else
440 : 51 : off += 4 * offset_size - 4 + 3 + 8;
441 : : }
442 : : else
443 : : {
444 : : /*
445 : : LEN VER TYPE ADDR OFFSET SIGNATURE TYPE-OFFSET
446 : : 4-bytes + 2-bytes + 1-byte + 1-byte + 4-bytes + 8-bytes + 4-bytes 32-bit
447 : : 12-bytes + 2-bytes + 1-byte + 1-byte + 8-bytes + 8-bytes + 8-bytes 64-bit
448 : : Both signature and type offset are optional.
449 : :
450 : : Note same 4/8 offset size trick as above.
451 : : We explicitly ignore unknown unit types (see asserts above). */
452 : 787 : off += 3 * offset_size - 4 + 4;
453 : 787 : if (unit_type == DW_UT_skeleton || unit_type == DW_UT_split_compile
454 [ + + - + ]: 787 : || unit_type == DW_UT_type || unit_type == DW_UT_split_type)
455 : : {
456 : 601 : off += 8;
457 [ - + ]: 601 : if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
458 : 0 : off += offset_size;
459 : : }
460 : : }
461 : :
462 : 3062444 : return off;
463 : : }
464 : :
465 : : static inline Dwarf_Off
466 : 3025005 : __libdw_first_die_off_from_cu (struct Dwarf_CU *cu)
467 : : {
468 : 3025005 : return __libdw_first_die_from_cu_start (cu->start,
469 : : cu->offset_size,
470 : : cu->version,
471 : : cu->unit_type);
472 : : }
473 : :
474 : : #define CUDIE(fromcu) \
475 : : ((Dwarf_Die) \
476 : : { \
477 : : .cu = (fromcu), \
478 : : .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
479 : : + __libdw_first_die_off_from_cu (fromcu)) \
480 : : })
481 : :
482 : : #define SUBDIE(fromcu) \
483 : : ((Dwarf_Die) \
484 : : { \
485 : : .cu = (fromcu), \
486 : : .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
487 : : + (fromcu)->start + (fromcu)->subdie_offset) \
488 : : })
489 : :
490 : :
491 : : /* Prototype of a single .debug_macro operator. */
492 : : typedef struct
493 : : {
494 : : Dwarf_Word nforms;
495 : : unsigned char const *forms;
496 : : } Dwarf_Macro_Op_Proto;
497 : :
498 : : /* Prototype table. */
499 : : typedef struct
500 : : {
501 : : /* Offset of .debug_macro section. */
502 : : Dwarf_Off offset;
503 : :
504 : : /* Offset of associated .debug_line section. */
505 : : Dwarf_Off line_offset;
506 : :
507 : : /* The source file information. */
508 : : Dwarf_Files *files;
509 : :
510 : : /* If this macro unit was opened through dwarf_getmacros or
511 : : dwarf_getmacros_die, this caches value of DW_AT_comp_dir, if
512 : : present. */
513 : : const char *comp_dir;
514 : :
515 : : /* Header length. */
516 : : Dwarf_Half header_len;
517 : :
518 : : uint16_t version;
519 : : bool is_64bit;
520 : : uint8_t sec_index; /* IDX_debug_macro or IDX_debug_macinfo. */
521 : :
522 : : /* Shows where in TABLE each opcode is defined. Since opcode 0 is
523 : : never used, it stores index of opcode X in X-1'th element. The
524 : : value of 0xff means not stored at all. */
525 : : unsigned char opcodes[255];
526 : :
527 : : /* Individual opcode prototypes. */
528 : : Dwarf_Macro_Op_Proto table[];
529 : : } Dwarf_Macro_Op_Table;
530 : :
531 : : struct Dwarf_Macro_s
532 : : {
533 : : Dwarf_Macro_Op_Table *table;
534 : : Dwarf_Attribute *attributes;
535 : : uint8_t opcode;
536 : : };
537 : :
538 : : static inline Dwarf_Word
539 : 648 : libdw_macro_nforms (Dwarf_Macro *macro)
540 : : {
541 [ + - ]: 648 : return macro->table->table[macro->table->opcodes[macro->opcode - 1]].nforms;
542 : : }
543 : :
544 : : /* Returns true for any allowed FORM in the opcode_operands_table as
545 : : mentioned in the DWARF5 spec (6.3.1 Macro Information Header).
546 : : Or those mentioned in DWARF5 spec (6.2.4.2 Vendor-defined Content
547 : : Descriptions) for the directory/file table (plus DW_FORM_strp_sup). */
548 : : static inline bool
549 : 1394 : libdw_valid_user_form (int form)
550 : : {
551 [ - + ]: 1394 : switch (form)
552 : : {
553 : : case DW_FORM_block:
554 : : case DW_FORM_block1:
555 : : case DW_FORM_block2:
556 : : case DW_FORM_block4:
557 : : case DW_FORM_data1:
558 : : case DW_FORM_data2:
559 : : case DW_FORM_data4:
560 : : case DW_FORM_data8:
561 : : case DW_FORM_data16:
562 : : case DW_FORM_flag:
563 : : case DW_FORM_line_strp:
564 : : case DW_FORM_sdata:
565 : : case DW_FORM_sec_offset:
566 : : case DW_FORM_string:
567 : : case DW_FORM_strp:
568 : : case DW_FORM_strp_sup:
569 : : case DW_FORM_strx:
570 : : case DW_FORM_strx1:
571 : : case DW_FORM_strx2:
572 : : case DW_FORM_strx3:
573 : : case DW_FORM_strx4:
574 : : case DW_FORM_udata:
575 : : return true;
576 : 0 : default:
577 : 0 : return false;
578 : : }
579 : : }
580 : :
581 : :
582 : : /* We have to include the file at this point because the inline
583 : : functions access internals of the Dwarf structure. */
584 : : #include "memory-access.h"
585 : :
586 : :
587 : : /* Set error value. */
588 : : extern void __libdw_seterrno (int value) internal_function;
589 : :
590 : :
591 : : /* Memory handling, the easy parts. */
592 : : #define libdw_alloc(dbg, type, tsize, cnt) \
593 : : ({ struct libdw_memblock *_tail = __libdw_alloc_tail(dbg); \
594 : : size_t _required = (tsize) * (cnt); \
595 : : type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
596 : : size_t _padding = ((__alignof (type) \
597 : : - ((uintptr_t) _result & (__alignof (type) - 1))) \
598 : : & (__alignof (type) - 1)); \
599 : : if (unlikely (_tail->remaining < _required + _padding)) \
600 : : _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
601 : : else \
602 : : { \
603 : : _required += _padding; \
604 : : _result = (type *) ((char *) _result + _padding); \
605 : : _tail->remaining -= _required; \
606 : : } \
607 : : _result; })
608 : :
609 : : #define libdw_typed_alloc(dbg, type) \
610 : : libdw_alloc (dbg, type, sizeof (type), 1)
611 : :
612 : : /* Can only be used to undo the last libdw_alloc. */
613 : : #define libdw_unalloc(dbg, type, tsize, cnt) \
614 : : ({ struct libdw_memblock *_tail = __libdw_thread_tail (dbg); \
615 : : size_t _required = (tsize) * (cnt); \
616 : : /* We cannot know the padding, it is lost. */ \
617 : : _tail->remaining += _required; }) \
618 : :
619 : : #define libdw_typed_unalloc(dbg, type) \
620 : : libdw_unalloc (dbg, type, sizeof (type), 1)
621 : :
622 : : /* Callback to choose a thread-local memory allocation stack. */
623 : : extern struct libdw_memblock *__libdw_alloc_tail (Dwarf* dbg)
624 : : __nonnull_attribute__ (1);
625 : :
626 : : extern struct libdw_memblock *__libdw_thread_tail (Dwarf* dbg)
627 : : __nonnull_attribute__ (1);
628 : :
629 : : /* Callback to allocate more. */
630 : : extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
631 : : __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
632 : :
633 : : /* Default OOM handler. */
634 : : extern void __libdw_oom (void) __attribute ((noreturn)) attribute_hidden;
635 : :
636 : : /* Read next unit (or v4 debug type) and return next offset. Doesn't
637 : : create an actual Dwarf_CU just provides necessary header fields. */
638 : : extern int
639 : : internal_function
640 : : __libdw_next_unit (Dwarf *dbg, bool v4_debug_types, Dwarf_Off off,
641 : : Dwarf_Off *next_off, size_t *header_sizep,
642 : : Dwarf_Half *versionp, uint8_t *unit_typep,
643 : : Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
644 : : uint8_t *offset_sizep, uint64_t *unit_id8p,
645 : : Dwarf_Off *subdie_offsetp)
646 : : __nonnull_attribute__ (4) internal_function;
647 : :
648 : : /* Allocate the internal data for a unit not seen before. */
649 : : extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
650 : : __nonnull_attribute__ (1) internal_function;
651 : :
652 : : /* Find CU for given offset. */
653 : : extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
654 : : __nonnull_attribute__ (1) internal_function;
655 : :
656 : : /* Find CU for given DIE address. */
657 : : extern struct Dwarf_CU *__libdw_findcu_addr (Dwarf *dbg, void *addr)
658 : : __nonnull_attribute__ (1) internal_function;
659 : :
660 : : /* Find split Dwarf for given DIE address. */
661 : : extern struct Dwarf *__libdw_find_split_dbg_addr (Dwarf *dbg, void *addr)
662 : : __nonnull_attribute__ (1) internal_function;
663 : :
664 : : /* Find the split (or skeleton) unit. */
665 : : extern struct Dwarf_CU *__libdw_find_split_unit (Dwarf_CU *cu)
666 : : internal_function;
667 : :
668 : : /* Get abbreviation with given code. */
669 : : extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
670 : : unsigned int code)
671 : : __nonnull_attribute__ (1) internal_function;
672 : :
673 : : /* Get abbreviation at given offset. */
674 : : extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
675 : : Dwarf_Off offset, size_t *lengthp,
676 : : Dwarf_Abbrev *result)
677 : : __nonnull_attribute__ (1) internal_function;
678 : :
679 : : /* Get abbreviation of given DIE, and optionally set *READP to the DIE memory
680 : : just past the abbreviation code. */
681 : : static inline Dwarf_Abbrev *
682 : : __nonnull_attribute__ (1)
683 : 68113852 : __libdw_dieabbrev (Dwarf_Die *die, const unsigned char **readp)
684 : : {
685 : : /* Do we need to get the abbreviation, or need to read after the code? */
686 [ + + + + ]: 68113852 : if (die->abbrev == NULL || readp != NULL)
687 : : {
688 : : /* Get the abbreviation code. */
689 : 37064807 : unsigned int code;
690 : 37064807 : const unsigned char *addr = die->addr;
691 [ + + - + ]: 37064807 : if (unlikely (die->cu == NULL
692 : : || addr >= (const unsigned char *) die->cu->endp))
693 : 11138 : return die->abbrev = DWARF_END_ABBREV;
694 : 37053669 : get_uleb128 (code, addr, die->cu->endp);
695 [ + + ]: 37053669 : if (readp != NULL)
696 : 24201490 : *readp = addr;
697 : :
698 : : /* Find the abbreviation. */
699 [ + + ]: 37053669 : if (die->abbrev == NULL)
700 : 16953189 : die->abbrev = __libdw_findabbrev (die->cu, code);
701 : : }
702 : 68102714 : return die->abbrev;
703 : : }
704 : :
705 : : /* Helper functions for form handling. */
706 : : extern size_t __libdw_form_val_compute_len (struct Dwarf_CU *cu,
707 : : unsigned int form,
708 : : const unsigned char *valp)
709 : : __nonnull_attribute__ (1, 3) internal_function;
710 : :
711 : : /* Find the length of a form attribute in DIE/info data. */
712 : : static inline size_t
713 : : __nonnull_attribute__ (1, 3)
714 : 94289944 : __libdw_form_val_len (struct Dwarf_CU *cu, unsigned int form,
715 : : const unsigned char *valp)
716 : : {
717 : : /* Small lookup table of forms with fixed lengths. Absent indexes are
718 : : initialized 0, so any truly desired 0 is set to 0x80 and masked. */
719 : 94289944 : static const uint8_t form_lengths[] =
720 : : {
721 : : [DW_FORM_flag_present] = 0x80,
722 : : [DW_FORM_implicit_const] = 0x80, /* Value is in abbrev, not in info. */
723 : :
724 : : [DW_FORM_flag] = 1,
725 : : [DW_FORM_data1] = 1, [DW_FORM_ref1] = 1,
726 : : [DW_FORM_addrx1] = 1, [DW_FORM_strx1] = 1,
727 : :
728 : : [DW_FORM_data2] = 2, [DW_FORM_ref2] = 2,
729 : : [DW_FORM_addrx2] = 2, [DW_FORM_strx2] = 2,
730 : :
731 : : [DW_FORM_addrx3] = 3, [DW_FORM_strx3] = 3,
732 : :
733 : : [DW_FORM_data4] = 4, [DW_FORM_ref4] = 4, [DW_FORM_ref_sup4] = 4,
734 : : [DW_FORM_addrx4] = 4, [DW_FORM_strx4] = 4,
735 : :
736 : : [DW_FORM_ref_sig8] = 8,
737 : : [DW_FORM_data8] = 8, [DW_FORM_ref8] = 8, [DW_FORM_ref_sup8] = 8,
738 : :
739 : : [DW_FORM_data16] = 16,
740 : : };
741 : :
742 : : /* Return immediately for forms with fixed lengths. */
743 [ + + ]: 94289944 : if (form < sizeof form_lengths / sizeof form_lengths[0])
744 : : {
745 : 94287336 : uint8_t len = form_lengths[form];
746 [ + + ]: 94287336 : if (len != 0)
747 : : {
748 : 67011125 : const unsigned char *endp = cu->endp;
749 : 67011125 : len &= 0x7f; /* Mask to allow 0x80 -> 0. */
750 [ - + ]: 67011125 : if (unlikely (len > (size_t) (endp - valp)))
751 : : {
752 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
753 : 0 : return -1;
754 : : }
755 : : return len;
756 : : }
757 : : }
758 : :
759 : : /* Other forms require some computation. */
760 : 27278819 : return __libdw_form_val_compute_len (cu, form, valp);
761 : : }
762 : :
763 : : /* Helper function for DW_FORM_ref* handling. */
764 : : extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
765 : : __nonnull_attribute__ (1, 2) internal_function;
766 : :
767 : :
768 : : /* Helper function to locate attribute. */
769 : : extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
770 : : unsigned int search_name,
771 : : unsigned int *codep,
772 : : unsigned int *formp)
773 : : __nonnull_attribute__ (1) internal_function;
774 : :
775 : : /* Helper function to access integer attribute. */
776 : : extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
777 : : __nonnull_attribute__ (1, 2) internal_function;
778 : :
779 : : /* Helper function to walk scopes. */
780 : : struct Dwarf_Die_Chain
781 : : {
782 : : Dwarf_Die die;
783 : : struct Dwarf_Die_Chain *parent;
784 : : bool prune; /* The PREVISIT function can set this. */
785 : : };
786 : : extern int __libdw_visit_scopes (unsigned int depth,
787 : : struct Dwarf_Die_Chain *root,
788 : : struct Dwarf_Die_Chain *imports,
789 : : int (*previsit) (unsigned int depth,
790 : : struct Dwarf_Die_Chain *,
791 : : void *arg),
792 : : int (*postvisit) (unsigned int depth,
793 : : struct Dwarf_Die_Chain *,
794 : : void *arg),
795 : : void *arg)
796 : : __nonnull_attribute__ (2, 4) internal_function;
797 : :
798 : : /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's,
799 : : and cache the result (via tsearch). */
800 : : extern int __libdw_intern_expression (Dwarf *dbg,
801 : : bool other_byte_order,
802 : : unsigned int address_size,
803 : : unsigned int ref_size,
804 : : void **cache, const Dwarf_Block *block,
805 : : bool cfap, bool valuep,
806 : : Dwarf_Op **llbuf, size_t *listlen,
807 : : int sec_index)
808 : : __nonnull_attribute__ (5, 6, 9, 10) internal_function;
809 : :
810 : : extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
811 : : Dwarf_Die *result, bool debug_types)
812 : : internal_function;
813 : :
814 : :
815 : : /* Return error code of last failing function call. This value is kept
816 : : separately for each thread. */
817 : : extern int __dwarf_errno_internal (void);
818 : :
819 : :
820 : : /* Reader hooks. */
821 : :
822 : : /* Relocation hooks return -1 on error (in that case the error code
823 : : must already have been set), 0 if there is no relocation and 1 if a
824 : : relocation was present.*/
825 : :
826 : : static inline int
827 : 5352767 : __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
828 : : int sec_index __attribute__ ((unused)),
829 : : const void *addr __attribute__ ((unused)),
830 : : int width __attribute__ ((unused)),
831 : : Dwarf_Addr *val __attribute__ ((unused)))
832 : : {
833 [ + + ]: 5352736 : return 0;
834 : : }
835 : :
836 : : static inline int
837 : 1647353 : __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
838 : : int sec_index __attribute__ ((unused)),
839 : : const void *addr __attribute__ ((unused)),
840 : : int width __attribute__ ((unused)),
841 : : Dwarf_Off *val __attribute__ ((unused)))
842 : : {
843 : 1647353 : return 0;
844 : : }
845 : :
846 : : static inline Elf_Data *
847 : 9023288 : __libdw_checked_get_data (Dwarf *dbg, int sec_index)
848 : : {
849 : 9023288 : Elf_Data *data = dbg->sectiondata[sec_index];
850 [ + - ]: 9023288 : if (unlikely (data == NULL)
851 [ - + ]: 9023288 : || unlikely (data->d_buf == NULL))
852 : : {
853 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
854 : 0 : return NULL;
855 : : }
856 : : return data;
857 : : }
858 : :
859 : : static inline int
860 : 1656177 : __libdw_offset_in_section (Dwarf *dbg, int sec_index,
861 : : Dwarf_Off offset, size_t size)
862 : : {
863 : 1656177 : Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
864 [ + - ]: 1656177 : if (data == NULL)
865 : : return -1;
866 [ + - ]: 1656177 : if (unlikely (offset > data->d_size)
867 [ + - ]: 1656177 : || unlikely (data->d_size < size)
868 [ - + ]: 1656177 : || unlikely (offset > data->d_size - size))
869 : : {
870 : 0 : __libdw_seterrno (DWARF_E_INVALID_OFFSET);
871 : 0 : return -1;
872 : : }
873 : :
874 : : return 0;
875 : : }
876 : :
877 : : static inline bool
878 : 7358363 : __libdw_in_section (Dwarf *dbg, int sec_index,
879 : : const void *addr, size_t size)
880 : : {
881 : 7358363 : Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
882 [ + - ]: 7358363 : if (data == NULL)
883 : : return false;
884 [ + - ]: 7358363 : if (unlikely (addr < data->d_buf)
885 [ + - ]: 7358363 : || unlikely (data->d_size < size)
886 [ - + ]: 7358363 : || unlikely ((size_t)(addr - data->d_buf) > data->d_size - size))
887 : : {
888 : 0 : __libdw_seterrno (DWARF_E_INVALID_OFFSET);
889 : 0 : return false;
890 : : }
891 : :
892 : : return true;
893 : : }
894 : :
895 : : #define READ_AND_RELOCATE(RELOC_HOOK, VAL) \
896 : : ({ \
897 : : if (!__libdw_in_section (dbg, sec_index, addr, width)) \
898 : : return -1; \
899 : : \
900 : : const unsigned char *orig_addr = addr; \
901 : : if (width == 4) \
902 : : VAL = read_4ubyte_unaligned_inc (dbg, addr); \
903 : : else \
904 : : VAL = read_8ubyte_unaligned_inc (dbg, addr); \
905 : : \
906 : : int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL); \
907 : : if (status < 0) \
908 : : return status; \
909 : : status > 0; \
910 : : })
911 : :
912 : : static inline int
913 : 23747 : __libdw_read_address_inc (Dwarf *dbg,
914 : : int sec_index, const unsigned char **addrp,
915 : : int width, Dwarf_Addr *ret)
916 : : {
917 : 23747 : const unsigned char *addr = *addrp;
918 [ + - + + : 47494 : READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
+ + + + ]
919 : 23747 : *addrp = addr;
920 : 23747 : return 0;
921 : : }
922 : :
923 : : static inline int
924 : 358243 : __libdw_read_address (Dwarf *dbg,
925 : : int sec_index, const unsigned char *addr,
926 : : int width, Dwarf_Addr *ret)
927 : : {
928 [ + - + + : 716486 : READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
+ + + + ]
929 : : return 0;
930 : : }
931 : :
932 : : static inline int
933 : 41092 : __libdw_read_offset_inc (Dwarf *dbg,
934 : : int sec_index, const unsigned char **addrp,
935 : : int width, Dwarf_Off *ret, int sec_ret,
936 : : size_t size)
937 : : {
938 : 41092 : const unsigned char *addr = *addrp;
939 [ + - + - : 82184 : READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
+ + - - ]
940 : 41092 : *addrp = addr;
941 : 41092 : return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
942 : : }
943 : :
944 : : static inline int
945 : 1606261 : __libdw_read_offset (Dwarf *dbg, Dwarf *dbg_ret,
946 : : int sec_index, const unsigned char *addr,
947 : : int width, Dwarf_Off *ret, int sec_ret,
948 : : size_t size)
949 : : {
950 [ + - + - : 3212522 : READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
+ + - - ]
951 : 1606261 : return __libdw_offset_in_section (dbg_ret, sec_ret, *ret, size);
952 : : }
953 : :
954 : : static inline size_t
955 : 4942325 : cu_sec_idx (struct Dwarf_CU *cu)
956 : : {
957 [ - + + + : 4153555 : return cu->sec_idx;
+ - - + ]
958 : : }
959 : :
960 : : static inline bool
961 : 2210351 : is_cudie (Dwarf_Die *cudie)
962 : : {
963 [ + + + + ]: 2210351 : return cudie->cu != NULL && CUDIE (cudie->cu).addr == cudie->addr;
964 : : }
965 : :
966 : : /* Read up begin/end pair and increment read pointer.
967 : : - If it's normal range record, set up *BEGINP and *ENDP and return 0.
968 : : - If it's base address selection record, set up *BASEP and return 1.
969 : : - If it's end of rangelist, don't set anything and return 2
970 : : - If an error occurs, don't set anything and return <0. */
971 : : int __libdw_read_begin_end_pair_inc (Dwarf_CU *cu, int sec_index,
972 : : const unsigned char **readp,
973 : : const unsigned char *readend,
974 : : int width,
975 : : Dwarf_Addr *beginp, Dwarf_Addr *endp,
976 : : Dwarf_Addr *basep)
977 : : internal_function;
978 : :
979 : : const unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
980 : : int err_nodata,
981 : : const unsigned char **endpp,
982 : : Dwarf_Off *offsetp)
983 : : internal_function;
984 : :
985 : : /* Fills in the given attribute to point at an empty location expression. */
986 : : void __libdw_empty_loc_attr (Dwarf_Attribute *attr)
987 : : internal_function;
988 : :
989 : : /* Load .debug_line unit at DEBUG_LINE_OFFSET. COMP_DIR is a value of
990 : : DW_AT_comp_dir or NULL if that attribute is not available. Caches
991 : : the loaded unit and optionally set *LINESP and/or *FILESP (if not
992 : : NULL) with loaded information. Returns 0 for success or a negative
993 : : value for failure. */
994 : : int __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
995 : : const char *comp_dir, unsigned address_size,
996 : : Dwarf_Lines **linesp, Dwarf_Files **filesp)
997 : : internal_function
998 : : __nonnull_attribute__ (1);
999 : :
1000 : : /* Load and return value of DW_AT_comp_dir from CUDIE. */
1001 : : const char *__libdw_getcompdir (Dwarf_Die *cudie);
1002 : :
1003 : : /* Get the base address for the CU, fetches it when not yet set.
1004 : : This is used as initial base address for ranges and loclists. */
1005 : : Dwarf_Addr __libdw_cu_base_address (Dwarf_CU *cu);
1006 : :
1007 : : /* Get the address base for the CU, fetches it when not yet set. */
1008 : : static inline Dwarf_Off
1009 : 635 : __libdw_cu_addr_base (Dwarf_CU *cu)
1010 : : {
1011 [ + + ]: 635 : if (cu->addr_base == (Dwarf_Off) -1)
1012 : : {
1013 : 50 : Dwarf_Die cu_die = CUDIE(cu);
1014 : 50 : Dwarf_Attribute attr;
1015 : 50 : Dwarf_Off offset = 0;
1016 [ + + ]: 50 : if (dwarf_attr (&cu_die, DW_AT_GNU_addr_base, &attr) != NULL
1017 [ + - ]: 24 : || dwarf_attr (&cu_die, DW_AT_addr_base, &attr) != NULL)
1018 : : {
1019 : 50 : Dwarf_Word off;
1020 [ + - ]: 50 : if (dwarf_formudata (&attr, &off) == 0)
1021 : 50 : offset = off;
1022 : : }
1023 : 50 : cu->addr_base = offset;
1024 : : }
1025 : :
1026 : 635 : return cu->addr_base;
1027 : : }
1028 : :
1029 : : /* Gets the .debug_str_offsets base offset to use. static inline to
1030 : : be shared between libdw and eu-readelf. */
1031 : : static inline Dwarf_Off
1032 : 2502 : str_offsets_base_off (Dwarf *dbg, Dwarf_CU *cu)
1033 : : {
1034 : : /* If we don't have a CU, then find and use the first one in the
1035 : : debug file (when we support .dwp files, we must actually find the
1036 : : one matching our "caller" - aka macro or line). If we (now) have
1037 : : a cu and str_offsets_base attribute, just use that. Otherwise
1038 : : use the first offset. But we might have to parse the header
1039 : : first, but only if this is version 5. Assume if all else fails,
1040 : : this is version 4, without header. */
1041 : :
1042 [ + + ]: 2502 : if (cu == NULL && dbg != NULL)
1043 : : {
1044 : 1818 : Dwarf_CU *first_cu;
1045 [ + + ]: 1818 : if (dwarf_get_units (dbg, NULL, &first_cu,
1046 : : NULL, NULL, NULL, NULL) == 0)
1047 : 1817 : cu = first_cu;
1048 : : }
1049 : :
1050 [ + + ]: 2502 : if (cu != NULL)
1051 : : {
1052 [ + + ]: 2501 : if (cu->str_off_base == (Dwarf_Off) -1)
1053 : : {
1054 : 88 : Dwarf_Die cu_die = CUDIE(cu);
1055 : 88 : Dwarf_Attribute attr;
1056 [ + + ]: 88 : if (dwarf_attr (&cu_die, DW_AT_str_offsets_base, &attr) != NULL)
1057 : : {
1058 : 2 : Dwarf_Word off;
1059 [ + - ]: 2 : if (dwarf_formudata (&attr, &off) == 0)
1060 : : {
1061 : 2 : cu->str_off_base = off;
1062 : 2 : return cu->str_off_base;
1063 : : }
1064 : : }
1065 : : /* For older DWARF simply assume zero (no header). */
1066 [ + + ]: 86 : if (cu->version < 5)
1067 : : {
1068 : 71 : cu->str_off_base = 0;
1069 : 71 : return cu->str_off_base;
1070 : : }
1071 : :
1072 [ + + ]: 15 : if (dbg == NULL)
1073 : 14 : dbg = cu->dbg;
1074 : : }
1075 : : else
1076 : : return cu->str_off_base;
1077 : : }
1078 : :
1079 : : /* No str_offsets_base attribute, we have to assume "zero".
1080 : : But there could be a header first. */
1081 : 16 : Dwarf_Off off = 0;
1082 [ - + ]: 16 : if (dbg == NULL)
1083 : 0 : goto no_header;
1084 : :
1085 : 16 : Elf_Data *data = dbg->sectiondata[IDX_debug_str_offsets];
1086 [ + + ]: 16 : if (data == NULL)
1087 : 2 : goto no_header;
1088 : :
1089 : 14 : const unsigned char *start;
1090 : 14 : const unsigned char *readp;
1091 : 14 : const unsigned char *readendp;
1092 : 14 : start = readp = (const unsigned char *) data->d_buf;
1093 : 14 : readendp = (const unsigned char *) data->d_buf + data->d_size;
1094 : :
1095 : 14 : uint64_t unit_length;
1096 : 14 : uint16_t version;
1097 : :
1098 [ - + ]: 14 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1099 [ - + ]: 14 : if (unlikely (unit_length == 0xffffffff))
1100 : : {
1101 [ # # ]: 0 : if (unlikely (readendp - readp < 8))
1102 : 0 : goto no_header;
1103 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1104 : : /* In theory the offset size could be different
1105 : : between CU and str_offsets unit. But we just
1106 : : ignore that here. */
1107 : : }
1108 : :
1109 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
1110 : : 4 bytes to complete the header. And this unit cannot go
1111 : : beyond the section data. */
1112 [ + - ]: 14 : if (readendp - readp < 4
1113 [ + - ]: 14 : || unit_length < 4
1114 [ - + ]: 14 : || (uint64_t) (readendp - readp) < unit_length)
1115 : 0 : goto no_header;
1116 : :
1117 [ - + ]: 14 : version = read_2ubyte_unaligned_inc (dbg, readp);
1118 [ - + ]: 14 : if (version != 5)
1119 : 0 : goto no_header;
1120 : : /* padding */
1121 : 14 : read_2ubyte_unaligned_inc (dbg, readp);
1122 : :
1123 : 14 : off = (Dwarf_Off) (readp - start);
1124 : :
1125 : 16 : no_header:
1126 [ + + ]: 16 : if (cu != NULL)
1127 : 15 : cu->str_off_base = off;
1128 : :
1129 : : return off;
1130 : : }
1131 : :
1132 : :
1133 : : /* Get the string offsets base for the CU, fetches it when not yet set. */
1134 : 150 : static inline Dwarf_Off __libdw_cu_str_off_base (Dwarf_CU *cu)
1135 : : {
1136 : 150 : return str_offsets_base_off (NULL, cu);
1137 : : }
1138 : :
1139 : :
1140 : : /* Either a direct offset into .debug_ranges for version < 5, or the
1141 : : start of the offset table in .debug_rnglists for version > 5. */
1142 : : static inline Dwarf_Off
1143 : 14 : __libdw_cu_ranges_base (Dwarf_CU *cu)
1144 : : {
1145 [ + + ]: 14 : if (cu->ranges_base == (Dwarf_Off) -1)
1146 : : {
1147 : 5 : Dwarf_Off offset = 0;
1148 : 5 : Dwarf_Die cu_die = CUDIE(cu);
1149 : 5 : Dwarf_Attribute attr;
1150 [ + + ]: 5 : if (cu->version < 5)
1151 : : {
1152 [ + - ]: 4 : if (dwarf_attr (&cu_die, DW_AT_GNU_ranges_base, &attr) != NULL)
1153 : : {
1154 : 4 : Dwarf_Word off;
1155 [ + - ]: 4 : if (dwarf_formudata (&attr, &off) == 0)
1156 : 4 : offset = off;
1157 : : }
1158 : : }
1159 : : else
1160 : : {
1161 [ + - ]: 1 : if (dwarf_attr (&cu_die, DW_AT_rnglists_base, &attr) != NULL)
1162 : : {
1163 : 1 : Dwarf_Word off;
1164 [ + - ]: 1 : if (dwarf_formudata (&attr, &off) == 0)
1165 : 1 : offset = off;
1166 : : }
1167 : :
1168 : : /* There wasn't an rnglists_base, if the Dwarf does have a
1169 : : .debug_rnglists section, then it might be we need the
1170 : : base after the first header. */
1171 : 1 : Elf_Data *data = cu->dbg->sectiondata[IDX_debug_rnglists];
1172 [ + - ]: 1 : if (offset == 0 && data != NULL)
1173 : : {
1174 : 0 : Dwarf *dbg = cu->dbg;
1175 : 0 : const unsigned char *readp = data->d_buf;
1176 : 0 : const unsigned char *const dataend
1177 : 0 : = (unsigned char *) data->d_buf + data->d_size;
1178 : :
1179 [ # # ]: 0 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1180 : 0 : unsigned int offset_size = 4;
1181 [ # # ]: 0 : if (unlikely (unit_length == 0xffffffff))
1182 : : {
1183 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
1184 : 0 : goto no_header;
1185 : :
1186 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1187 : 0 : offset_size = 8;
1188 : : }
1189 : :
1190 [ # # ]: 0 : if (readp > dataend - 8
1191 [ # # ]: 0 : || unit_length < 8
1192 [ # # ]: 0 : || unit_length > (uint64_t) (dataend - readp))
1193 : 0 : goto no_header;
1194 : :
1195 [ # # ]: 0 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
1196 [ # # ]: 0 : if (version != 5)
1197 : 0 : goto no_header;
1198 : :
1199 : 0 : uint8_t address_size = *readp++;
1200 [ # # ]: 0 : if (address_size != 4 && address_size != 8)
1201 : 0 : goto no_header;
1202 : :
1203 : 0 : uint8_t segment_size = *readp++;
1204 [ # # ]: 0 : if (segment_size != 0)
1205 : 0 : goto no_header;
1206 : :
1207 : 0 : uint32_t offset_entry_count;
1208 [ # # ]: 0 : offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
1209 : :
1210 : 0 : const unsigned char *offset_array_start = readp;
1211 [ # # ]: 0 : if (offset_entry_count <= 0)
1212 : 0 : goto no_header;
1213 : :
1214 : 0 : uint64_t needed = offset_entry_count * offset_size;
1215 [ # # ]: 0 : if (unit_length - 8 < needed)
1216 : 0 : goto no_header;
1217 : :
1218 : 0 : offset = (Dwarf_Off) (offset_array_start
1219 : 0 : - (unsigned char *) data->d_buf);
1220 : : }
1221 : : }
1222 : 1 : no_header:
1223 : 5 : cu->ranges_base = offset;
1224 : : }
1225 : :
1226 : 14 : return cu->ranges_base;
1227 : : }
1228 : :
1229 : :
1230 : : /* The start of the offset table in .debug_loclists for DWARF5. */
1231 : : static inline Dwarf_Off
1232 : 67 : __libdw_cu_locs_base (Dwarf_CU *cu)
1233 : : {
1234 [ + + ]: 67 : if (cu->locs_base == (Dwarf_Off) -1)
1235 : : {
1236 : 2 : Dwarf_Off offset = 0;
1237 : 2 : Dwarf_Die cu_die = CUDIE(cu);
1238 : 2 : Dwarf_Attribute attr;
1239 [ + - ]: 2 : if (dwarf_attr (&cu_die, DW_AT_loclists_base, &attr) != NULL)
1240 : : {
1241 : 2 : Dwarf_Word off;
1242 [ + - ]: 2 : if (dwarf_formudata (&attr, &off) == 0)
1243 : 2 : offset = off;
1244 : : }
1245 : :
1246 : : /* There wasn't an loclists_base, if the Dwarf does have a
1247 : : .debug_loclists section, then it might be we need the
1248 : : base after the first header. */
1249 : 2 : Elf_Data *data = cu->dbg->sectiondata[IDX_debug_loclists];
1250 [ - + ]: 2 : if (offset == 0 && data != NULL)
1251 : : {
1252 : 2 : Dwarf *dbg = cu->dbg;
1253 : 2 : const unsigned char *readp = data->d_buf;
1254 : 2 : const unsigned char *const dataend
1255 : 2 : = (unsigned char *) data->d_buf + data->d_size;
1256 : :
1257 [ - + ]: 2 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1258 : 2 : unsigned int offset_size = 4;
1259 [ - + ]: 2 : if (unlikely (unit_length == 0xffffffff))
1260 : : {
1261 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
1262 : 0 : goto no_header;
1263 : :
1264 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1265 : 0 : offset_size = 8;
1266 : : }
1267 : :
1268 [ + - ]: 2 : if (readp > dataend - 8
1269 [ + - ]: 2 : || unit_length < 8
1270 [ - + ]: 2 : || unit_length > (uint64_t) (dataend - readp))
1271 : 0 : goto no_header;
1272 : :
1273 [ - + ]: 2 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
1274 [ - + ]: 2 : if (version != 5)
1275 : 0 : goto no_header;
1276 : :
1277 : 2 : uint8_t address_size = *readp++;
1278 [ - + ]: 2 : if (address_size != 4 && address_size != 8)
1279 : 0 : goto no_header;
1280 : :
1281 : 2 : uint8_t segment_size = *readp++;
1282 [ - + ]: 2 : if (segment_size != 0)
1283 : 0 : goto no_header;
1284 : :
1285 : 2 : uint32_t offset_entry_count;
1286 [ - + ]: 2 : offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
1287 : :
1288 : 2 : const unsigned char *offset_array_start = readp;
1289 [ - + ]: 2 : if (offset_entry_count <= 0)
1290 : 0 : goto no_header;
1291 : :
1292 : 2 : uint64_t needed = offset_entry_count * offset_size;
1293 [ - + ]: 2 : if (unit_length - 8 < needed)
1294 : 0 : goto no_header;
1295 : :
1296 : 2 : offset = (Dwarf_Off) (offset_array_start
1297 : 2 : - (unsigned char *) data->d_buf);
1298 : : }
1299 : :
1300 : 0 : no_header:
1301 : 2 : cu->locs_base = offset;
1302 : : }
1303 : :
1304 : 67 : return cu->locs_base;
1305 : : }
1306 : :
1307 : : /* Helper function for tsearch/tfind split_tree Dwarf. */
1308 : : int __libdw_finddbg_cb (const void *arg1, const void *arg2);
1309 : :
1310 : : /* Link skeleton and split compile units. */
1311 : : static inline void
1312 : 48 : __libdw_link_skel_split (Dwarf_CU *skel, Dwarf_CU *split)
1313 : : {
1314 : 48 : skel->split = split;
1315 : 48 : split->split = skel;
1316 : :
1317 : : /* Get .debug_addr and addr_base greedy.
1318 : : We also need it for the fake addr cu.
1319 : : There is only one per split debug. */
1320 : 48 : Dwarf *dbg = skel->dbg;
1321 : 48 : Dwarf *sdbg = split->dbg;
1322 [ + - ]: 48 : if (sdbg->sectiondata[IDX_debug_addr] == NULL
1323 [ + - ]: 48 : && dbg->sectiondata[IDX_debug_addr] != NULL)
1324 : : {
1325 : 48 : sdbg->sectiondata[IDX_debug_addr]
1326 : 48 : = dbg->sectiondata[IDX_debug_addr];
1327 : 48 : split->addr_base = __libdw_cu_addr_base (skel);
1328 : 48 : sdbg->fake_addr_cu = dbg->fake_addr_cu;
1329 : : }
1330 : 48 : }
1331 : :
1332 : :
1333 : : /* Given an address index for a CU return the address.
1334 : : Returns -1 and sets libdw_errno if an error occurs. */
1335 : : int __libdw_addrx (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr);
1336 : :
1337 : :
1338 : : /* Helper function to set debugdir field in Dwarf, used from dwarf_begin_elf
1339 : : and libdwfl process_file. */
1340 : : char * __libdw_debugdir (int fd);
1341 : :
1342 : :
1343 : : /* Given the directory of a debug file, an absolute or relative dir
1344 : : to look in, and file returns a full path.
1345 : :
1346 : : If the file is absolute (starts with a /) a copy of file is returned.
1347 : : the file isn't absolute, but dir is absolute, then a path that is
1348 : : the concatenation of dir and file is returned. If neither file,
1349 : : nor dir is absolute, the path will be constructed using dir (if not
1350 : : NULL) and file relative to the debugdir (if valid).
1351 : :
1352 : : The debugdir and the dir may be NULL (in which case they aren't used).
1353 : : If file is NULL, or no full path can be constructed NULL is returned.
1354 : :
1355 : : The caller is responsible for freeing the result if not NULL. */
1356 : : char * __libdw_filepath (const char *debugdir, const char *dir,
1357 : : const char *file)
1358 : : internal_function;
1359 : :
1360 : :
1361 : : /* Aliases to avoid PLTs. */
1362 : : INTDECL (dwarf_aggregate_size)
1363 : : INTDECL (dwarf_attr)
1364 : : INTDECL (dwarf_attr_integrate)
1365 : : INTDECL (dwarf_begin)
1366 : : INTDECL (dwarf_begin_elf)
1367 : : INTDECL (dwarf_child)
1368 : : INTDECL (dwarf_default_lower_bound)
1369 : : INTDECL (dwarf_dieoffset)
1370 : : INTDECL (dwarf_diename)
1371 : : INTDECL (dwarf_end)
1372 : : INTDECL (dwarf_entrypc)
1373 : : INTDECL (dwarf_errmsg)
1374 : : INTDECL (dwarf_formaddr)
1375 : : INTDECL (dwarf_formblock)
1376 : : INTDECL (dwarf_formref_die)
1377 : : INTDECL (dwarf_formsdata)
1378 : : INTDECL (dwarf_formstring)
1379 : : INTDECL (dwarf_formudata)
1380 : : INTDECL (dwarf_getabbrevattr_data)
1381 : : INTDECL (dwarf_getalt)
1382 : : INTDECL (dwarf_getarange_addr)
1383 : : INTDECL (dwarf_getarangeinfo)
1384 : : INTDECL (dwarf_getaranges)
1385 : : INTDECL (dwarf_getlocation_die)
1386 : : INTDECL (dwarf_getsrcfiles)
1387 : : INTDECL (dwarf_getsrclines)
1388 : : INTDECL (dwarf_hasattr)
1389 : : INTDECL (dwarf_haschildren)
1390 : : INTDECL (dwarf_haspc)
1391 : : INTDECL (dwarf_highpc)
1392 : : INTDECL (dwarf_lowpc)
1393 : : INTDECL (dwarf_nextcu)
1394 : : INTDECL (dwarf_next_unit)
1395 : : INTDECL (dwarf_offdie)
1396 : : INTDECL (dwarf_peel_type)
1397 : : INTDECL (dwarf_ranges)
1398 : : INTDECL (dwarf_setalt)
1399 : : INTDECL (dwarf_siblingof)
1400 : : INTDECL (dwarf_srclang)
1401 : : INTDECL (dwarf_tag)
1402 : :
1403 : : #endif /* libdwP.h */
|