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