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 : : /* If DW_SECT_INFO section offsets were truncated to 32 bits, recovered
378 : : 64-bit offsets. */
379 : : Dwarf_Off *debug_info_offsets;
380 : : } Dwarf_Package_Index;
381 : :
382 : : /* CU representation. */
383 : : struct Dwarf_CU
384 : : {
385 : : Dwarf *dbg;
386 : : Dwarf_Off start;
387 : : Dwarf_Off end;
388 : : /* Row number of this unit in DWARF package file index. */
389 : : uint32_t dwp_row;
390 : : uint8_t address_size;
391 : : uint8_t offset_size;
392 : : uint16_t version;
393 : :
394 : : size_t sec_idx; /* Normally .debug_info, could be .debug_type or "fake". */
395 : :
396 : : /* The unit type if version >= 5. Otherwise 0 for normal CUs (from
397 : : .debug_info) or 1 for v4 type units (from .debug_types). */
398 : : uint8_t unit_type;
399 : :
400 : : /* Zero if the unit type doesn't support a die/type offset and/or id/sig.
401 : : Nonzero if it is a v4 type unit or for DWARFv5 units depending on
402 : : unit_type. */
403 : : size_t subdie_offset;
404 : : uint64_t unit_id8;
405 : :
406 : : /* If this is a skeleton unit this points to the split compile unit.
407 : : Or the other way around if this is a split compile unit. Set to -1
408 : : if not yet searched. Always use __libdw_find_split_unit to access
409 : : this field. */
410 : : struct Dwarf_CU *split;
411 : :
412 : : /* Hash table for the abbreviations. */
413 : : Dwarf_Abbrev_Hash abbrev_hash;
414 : : /* Offset of the first abbreviation. */
415 : : size_t orig_abbrev_offset;
416 : : /* Offset past last read abbreviation. */
417 : : size_t last_abbrev_offset;
418 : :
419 : : /* The srcline information. */
420 : : Dwarf_Lines *lines;
421 : :
422 : : /* The source file information. */
423 : : Dwarf_Files *files;
424 : :
425 : : /* Known location lists. */
426 : : void *locs;
427 : :
428 : : /* Base address for use with ranges and locs.
429 : : Don't access directly, call __libdw_cu_base_address. */
430 : : Dwarf_Addr base_address;
431 : :
432 : : /* The offset into the .debug_addr section where index zero begins.
433 : : Don't access directly, call __libdw_cu_addr_base. */
434 : : Dwarf_Off addr_base;
435 : :
436 : : /* The offset into the .debug_str_offsets section where index zero begins.
437 : : Don't access directly, call __libdw_cu_str_off_base. */
438 : : Dwarf_Off str_off_base;
439 : :
440 : : /* The offset into the .debug_ranges section to use for GNU
441 : : DebugFission split units. Don't access directly, call
442 : : __libdw_cu_ranges_base. */
443 : : Dwarf_Off ranges_base;
444 : :
445 : : /* The start of the offset table in .debug_loclists.
446 : : Don't access directly, call __libdw_cu_locs_base. */
447 : : Dwarf_Off locs_base;
448 : :
449 : : /* Memory boundaries of this CU. */
450 : : void *startp;
451 : : void *endp;
452 : : };
453 : :
454 : : /* Aliases to avoid PLTs. */
455 : : INTDECL (dwarf_aggregate_size)
456 : : INTDECL (dwarf_attr)
457 : : INTDECL (dwarf_attr_integrate)
458 : : INTDECL (dwarf_begin)
459 : : INTDECL (dwarf_begin_elf)
460 : : INTDECL (dwarf_child)
461 : : INTDECL (dwarf_cu_dwp_section_info)
462 : : INTDECL (dwarf_default_lower_bound)
463 : : INTDECL (dwarf_dieoffset)
464 : : INTDECL (dwarf_diename)
465 : : INTDECL (dwarf_end)
466 : : INTDECL (dwarf_entrypc)
467 : : INTDECL (dwarf_errmsg)
468 : : INTDECL (dwarf_formaddr)
469 : : INTDECL (dwarf_formblock)
470 : : INTDECL (dwarf_formref_die)
471 : : INTDECL (dwarf_formsdata)
472 : : INTDECL (dwarf_formstring)
473 : : INTDECL (dwarf_formudata)
474 : : INTDECL (dwarf_getabbrevattr_data)
475 : : INTDECL (dwarf_getalt)
476 : : INTDECL (dwarf_getarange_addr)
477 : : INTDECL (dwarf_getarangeinfo)
478 : : INTDECL (dwarf_getaranges)
479 : : INTDECL (dwarf_getlocation_die)
480 : : INTDECL (dwarf_getsrcfiles)
481 : : INTDECL (dwarf_getsrclines)
482 : : INTDECL (dwarf_get_units)
483 : : INTDECL (dwarf_hasattr)
484 : : INTDECL (dwarf_haschildren)
485 : : INTDECL (dwarf_haspc)
486 : : INTDECL (dwarf_highpc)
487 : : INTDECL (dwarf_lowpc)
488 : : INTDECL (dwarf_nextcu)
489 : : INTDECL (dwarf_next_unit)
490 : : INTDECL (dwarf_offdie)
491 : : INTDECL (dwarf_peel_type)
492 : : INTDECL (dwarf_ranges)
493 : : INTDECL (dwarf_setalt)
494 : : INTDECL (dwarf_siblingof)
495 : : INTDECL (dwarf_srclang)
496 : : INTDECL (dwarf_tag)
497 : :
498 : : #define ISV4TU(cu) ((cu)->version == 4 && (cu)->sec_idx == IDX_debug_types)
499 : :
500 : : /* Compute the offset of a CU's first DIE from the CU offset.
501 : : CU must be a valid/known version/unit_type. */
502 : : static inline Dwarf_Off
503 : 7778134 : __libdw_first_die_from_cu_start (Dwarf_Off cu_start,
504 : : uint8_t offset_size,
505 : : uint16_t version,
506 : : uint8_t unit_type)
507 : : {
508 : : /*
509 : : assert (offset_size == 4 || offset_size == 8);
510 : : assert (version >= 2 && version <= 5);
511 : : assert (unit_type == DW_UT_compile
512 : : || unit_type == DW_UT_partial
513 : : || unit_type == DW_UT_skeleton
514 : : || unit_type == DW_UT_split_compile
515 : : || unit_type == DW_UT_type
516 : : || unit_type == DW_UT_split_type);
517 : : */
518 : :
519 : 7778134 : Dwarf_Off off = cu_start;
520 [ + + ]: 7778134 : if (version < 5)
521 : : {
522 : : /*
523 : : LEN VER OFFSET ADDR
524 : : 4-bytes + 2-bytes + 4-bytes + 1-byte for 32-bit dwarf
525 : : 12-bytes + 2-bytes + 8-bytes + 1-byte for 64-bit dwarf
526 : : or in .debug_types, SIGNATURE TYPE-OFFSET
527 : : 4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes for 32-bit
528 : : 12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes for 64-bit
529 : :
530 : : Note the trick in the computation. If the offset_size is 4
531 : : the '- 4' term changes the '3 *' (or '4 *') into a '2 *' (or '3 *).
532 : : If the offset_size is 8 it accounts for the 4-byte escape value
533 : : used at the start of the length. */
534 [ + + ]: 213212 : if (unit_type != DW_UT_type)
535 : 213106 : off += 3 * offset_size - 4 + 3;
536 : : else
537 : 106 : off += 4 * offset_size - 4 + 3 + 8;
538 : : }
539 : : else
540 : : {
541 : : /*
542 : : LEN VER TYPE ADDR OFFSET SIGNATURE TYPE-OFFSET
543 : : 4-bytes + 2-bytes + 1-byte + 1-byte + 4-bytes + 8-bytes + 4-bytes 32-bit
544 : : 12-bytes + 2-bytes + 1-byte + 1-byte + 8-bytes + 8-bytes + 8-bytes 64-bit
545 : : Both signature and type offset are optional.
546 : :
547 : : Note same 4/8 offset size trick as above.
548 : : We explicitly ignore unknown unit types (see asserts above). */
549 : 7564922 : off += 3 * offset_size - 4 + 4;
550 : 7564922 : if (unit_type == DW_UT_skeleton || unit_type == DW_UT_split_compile
551 [ + + + + ]: 7564922 : || unit_type == DW_UT_type || unit_type == DW_UT_split_type)
552 : : {
553 : 2924 : off += 8;
554 [ + + ]: 2924 : if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
555 : 38 : off += offset_size;
556 : : }
557 : : }
558 : :
559 : 7778134 : return off;
560 : : }
561 : :
562 : : static inline Dwarf_Off
563 : 7649288 : __libdw_first_die_off_from_cu (struct Dwarf_CU *cu)
564 : : {
565 : 7649288 : return __libdw_first_die_from_cu_start (cu->start,
566 : 7649288 : cu->offset_size,
567 : 7649288 : cu->version,
568 : 7649288 : cu->unit_type);
569 : : }
570 : :
571 : : #define CUDIE(fromcu) \
572 : : ((Dwarf_Die) \
573 : : { \
574 : : .cu = (fromcu), \
575 : : .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
576 : : + __libdw_first_die_off_from_cu (fromcu)) \
577 : : })
578 : :
579 : : #define SUBDIE(fromcu) \
580 : : ((Dwarf_Die) \
581 : : { \
582 : : .cu = (fromcu), \
583 : : .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
584 : : + (fromcu)->start + (fromcu)->subdie_offset) \
585 : : })
586 : :
587 : :
588 : : /* Prototype of a single .debug_macro operator. */
589 : : typedef struct
590 : : {
591 : : Dwarf_Word nforms;
592 : : unsigned char const *forms;
593 : : } Dwarf_Macro_Op_Proto;
594 : :
595 : : /* Prototype table. */
596 : : typedef struct
597 : : {
598 : : Dwarf *dbg;
599 : :
600 : : /* Offset of .debug_macro section. */
601 : : Dwarf_Off offset;
602 : :
603 : : /* Offset of associated .debug_line section. */
604 : : Dwarf_Off line_offset;
605 : :
606 : : /* The source file information. */
607 : : Dwarf_Files *files;
608 : :
609 : : /* If this macro unit was opened through dwarf_getmacros or
610 : : dwarf_getmacros_die, this caches value of DW_AT_comp_dir, if
611 : : present. */
612 : : const char *comp_dir;
613 : :
614 : : /* Header length. */
615 : : Dwarf_Half header_len;
616 : :
617 : : uint16_t version;
618 : : uint8_t address_size;
619 : : uint8_t offset_size;
620 : : uint8_t sec_index; /* IDX_debug_macro or IDX_debug_macinfo. */
621 : :
622 : : /* Shows where in TABLE each opcode is defined. Since opcode 0 is
623 : : never used, it stores index of opcode X in X-1'th element. The
624 : : value of 0xff means not stored at all. */
625 : : unsigned char opcodes[255];
626 : :
627 : : /* Individual opcode prototypes. */
628 : : Dwarf_Macro_Op_Proto table[];
629 : : } Dwarf_Macro_Op_Table;
630 : :
631 : : struct Dwarf_Macro_s
632 : : {
633 : : Dwarf_Macro_Op_Table *table;
634 : : Dwarf_Attribute *attributes;
635 : : uint8_t opcode;
636 : : };
637 : :
638 : : static inline Dwarf_Word
639 : 6852 : libdw_macro_nforms (Dwarf_Macro *macro)
640 : : {
641 [ + - ]: 6852 : return macro->table->table[macro->table->opcodes[macro->opcode - 1]].nforms;
642 : : }
643 : :
644 : : /* Returns true for any allowed FORM in the opcode_operands_table as
645 : : mentioned in the DWARF5 spec (6.3.1 Macro Information Header).
646 : : Or those mentioned in DWARF5 spec (6.2.4.2 Vendor-defined Content
647 : : Descriptions) for the directory/file table (plus DW_FORM_strp_sup). */
648 : : static inline bool
649 : 143788 : libdw_valid_user_form (int form)
650 : : {
651 [ - + ]: 143788 : switch (form)
652 : : {
653 : : case DW_FORM_block:
654 : : case DW_FORM_block1:
655 : : case DW_FORM_block2:
656 : : case DW_FORM_block4:
657 : : case DW_FORM_data1:
658 : : case DW_FORM_data2:
659 : : case DW_FORM_data4:
660 : : case DW_FORM_data8:
661 : : case DW_FORM_data16:
662 : : case DW_FORM_flag:
663 : : case DW_FORM_line_strp:
664 : : case DW_FORM_sdata:
665 : : case DW_FORM_sec_offset:
666 : : case DW_FORM_string:
667 : : case DW_FORM_strp:
668 : : case DW_FORM_strp_sup:
669 : : case DW_FORM_strx:
670 : : case DW_FORM_strx1:
671 : : case DW_FORM_strx2:
672 : : case DW_FORM_strx3:
673 : : case DW_FORM_strx4:
674 : : case DW_FORM_udata:
675 : : return true;
676 : 0 : default:
677 : 0 : return false;
678 : : }
679 : : }
680 : :
681 : :
682 : : /* We have to include the file at this point because the inline
683 : : functions access internals of the Dwarf structure. */
684 : : #include "memory-access.h"
685 : :
686 : :
687 : : /* Set error value. */
688 : : extern void __libdw_seterrno (int value) internal_function;
689 : :
690 : :
691 : : /* Memory handling, the easy parts. */
692 : : #define libdw_alloc(dbg, type, tsize, cnt) \
693 : : ({ struct libdw_memblock *_tail = __libdw_alloc_tail(dbg); \
694 : : size_t _required = (tsize) * (cnt); \
695 : : type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
696 : : size_t _padding = ((__alignof (type) \
697 : : - ((uintptr_t) _result & (__alignof (type) - 1))) \
698 : : & (__alignof (type) - 1)); \
699 : : if (unlikely (_tail->remaining < _required + _padding)) \
700 : : _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
701 : : else \
702 : : { \
703 : : _required += _padding; \
704 : : _result = (type *) ((char *) _result + _padding); \
705 : : _tail->remaining -= _required; \
706 : : } \
707 : : _result; })
708 : :
709 : : #define libdw_typed_alloc(dbg, type) \
710 : : libdw_alloc (dbg, type, sizeof (type), 1)
711 : :
712 : : /* Can only be used to undo the last libdw_alloc. */
713 : : #define libdw_unalloc(dbg, type, tsize, cnt) \
714 : : ({ struct libdw_memblock *_tail = __libdw_thread_tail (dbg); \
715 : : size_t _required = (tsize) * (cnt); \
716 : : /* We cannot know the padding, it is lost. */ \
717 : : _tail->remaining += _required; }) \
718 : :
719 : : #define libdw_typed_unalloc(dbg, type) \
720 : : libdw_unalloc (dbg, type, sizeof (type), 1)
721 : :
722 : : /* Callback to choose a thread-local memory allocation stack. */
723 : : extern struct libdw_memblock *__libdw_alloc_tail (Dwarf* dbg)
724 : : __nonnull_attribute__ (1);
725 : :
726 : : extern struct libdw_memblock *__libdw_thread_tail (Dwarf* dbg)
727 : : __nonnull_attribute__ (1);
728 : :
729 : : /* Callback to allocate more. */
730 : : extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
731 : : __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
732 : :
733 : : /* Default OOM handler. */
734 : : extern void __libdw_oom (void) __attribute ((noreturn)) attribute_hidden;
735 : :
736 : : /* Read next unit (or v4 debug type) and return next offset. Doesn't
737 : : create an actual Dwarf_CU just provides necessary header fields. */
738 : : extern int
739 : : internal_function
740 : : __libdw_next_unit (Dwarf *dbg, bool v4_debug_types, Dwarf_Off off,
741 : : Dwarf_Off *next_off, size_t *header_sizep,
742 : : Dwarf_Half *versionp, uint8_t *unit_typep,
743 : : Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
744 : : uint8_t *offset_sizep, uint64_t *unit_id8p,
745 : : Dwarf_Off *subdie_offsetp)
746 : : __nonnull_attribute__ (4) internal_function;
747 : :
748 : : /* Allocate the internal data for a unit not seen before. */
749 : : extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
750 : : __nonnull_attribute__ (1) internal_function;
751 : :
752 : : /* Find CU for given offset. */
753 : : extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
754 : : __nonnull_attribute__ (1) internal_function;
755 : :
756 : : /* Find CU for given DIE address. */
757 : : extern struct Dwarf_CU *__libdw_findcu_addr (Dwarf *dbg, void *addr)
758 : : __nonnull_attribute__ (1) internal_function;
759 : :
760 : : /* Find split Dwarf for given DIE address. */
761 : : extern struct Dwarf *__libdw_find_split_dbg_addr (Dwarf *dbg, void *addr)
762 : : __nonnull_attribute__ (1) internal_function;
763 : :
764 : : /* Find the split (or skeleton) unit. */
765 : : extern struct Dwarf_CU *__libdw_find_split_unit (Dwarf_CU *cu)
766 : : internal_function;
767 : :
768 : : /* Find a unit in a DWARF package file for __libdw_intern_next_unit. */
769 : : extern int __libdw_dwp_find_unit (Dwarf *dbg, bool debug_types, Dwarf_Off off,
770 : : uint16_t version, uint8_t unit_type,
771 : : uint64_t unit_id8, uint32_t *unit_rowp,
772 : : Dwarf_Off *abbrev_offsetp)
773 : : __nonnull_attribute__ (1, 7, 8) internal_function;
774 : :
775 : : /* Find the compilation unit in a DWARF package file with the given id. */
776 : : extern Dwarf_CU *__libdw_dwp_findcu_id (Dwarf *dbg, uint64_t unit_id8)
777 : : __nonnull_attribute__ (1) internal_function;
778 : :
779 : : /* Get abbreviation with given code. */
780 : : extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
781 : : unsigned int code)
782 : : __nonnull_attribute__ (1) internal_function;
783 : :
784 : : /* Get abbreviation at given offset. */
785 : : extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
786 : : Dwarf_Off offset, size_t *lengthp,
787 : : Dwarf_Abbrev *result)
788 : : __nonnull_attribute__ (1) internal_function;
789 : :
790 : : /* Get abbreviation of given DIE, and optionally set *READP to the DIE memory
791 : : just past the abbreviation code. */
792 : : static inline Dwarf_Abbrev *
793 : : __nonnull_attribute__ (1)
794 : 136308532 : __libdw_dieabbrev (Dwarf_Die *die, const unsigned char **readp)
795 : : {
796 : : /* Do we need to get the abbreviation, or need to read after the code? */
797 [ + + + + ]: 136308532 : if (die->abbrev == NULL || readp != NULL)
798 : : {
799 : : /* Get the abbreviation code. */
800 : 74510462 : unsigned int code;
801 : 74510462 : const unsigned char *addr = die->addr;
802 [ + + - + ]: 74510462 : if (unlikely (die->cu == NULL
803 : : || addr >= (const unsigned char *) die->cu->endp))
804 : 23616 : return die->abbrev = DWARF_END_ABBREV;
805 : 74486846 : get_uleb128 (code, addr, die->cu->endp);
806 [ + + ]: 74486846 : if (readp != NULL)
807 : 48958794 : *readp = addr;
808 : :
809 : : /* Find the abbreviation. */
810 [ + + ]: 74486846 : if (die->abbrev == NULL)
811 : 33261870 : die->abbrev = __libdw_findabbrev (die->cu, code);
812 : : }
813 : 136284916 : return die->abbrev;
814 : : }
815 : :
816 : : /* Helper functions for form handling. */
817 : : extern size_t __libdw_form_val_compute_len (struct Dwarf_CU *cu,
818 : : unsigned int form,
819 : : const unsigned char *valp)
820 : : __nonnull_attribute__ (1, 3) internal_function;
821 : :
822 : : /* Find the length of a form attribute in DIE/info data. */
823 : : static inline size_t
824 : : __nonnull_attribute__ (1, 3)
825 : 192106222 : __libdw_form_val_len (struct Dwarf_CU *cu, unsigned int form,
826 : : const unsigned char *valp)
827 : : {
828 : : /* Small lookup table of forms with fixed lengths. Absent indexes are
829 : : initialized 0, so any truly desired 0 is set to 0x80 and masked. */
830 : 192106222 : static const uint8_t form_lengths[] =
831 : : {
832 : : [DW_FORM_flag_present] = 0x80,
833 : : [DW_FORM_implicit_const] = 0x80, /* Value is in abbrev, not in info. */
834 : :
835 : : [DW_FORM_flag] = 1,
836 : : [DW_FORM_data1] = 1, [DW_FORM_ref1] = 1,
837 : : [DW_FORM_addrx1] = 1, [DW_FORM_strx1] = 1,
838 : :
839 : : [DW_FORM_data2] = 2, [DW_FORM_ref2] = 2,
840 : : [DW_FORM_addrx2] = 2, [DW_FORM_strx2] = 2,
841 : :
842 : : [DW_FORM_addrx3] = 3, [DW_FORM_strx3] = 3,
843 : :
844 : : [DW_FORM_data4] = 4, [DW_FORM_ref4] = 4, [DW_FORM_ref_sup4] = 4,
845 : : [DW_FORM_addrx4] = 4, [DW_FORM_strx4] = 4,
846 : :
847 : : [DW_FORM_ref_sig8] = 8,
848 : : [DW_FORM_data8] = 8, [DW_FORM_ref8] = 8, [DW_FORM_ref_sup8] = 8,
849 : :
850 : : [DW_FORM_data16] = 16,
851 : : };
852 : :
853 : : /* Return immediately for forms with fixed lengths. */
854 [ + + ]: 192106222 : if (form < sizeof form_lengths / sizeof form_lengths[0])
855 : : {
856 : 192089322 : uint8_t len = form_lengths[form];
857 [ + + ]: 192089322 : if (len != 0)
858 : : {
859 : 138496512 : const unsigned char *endp = cu->endp;
860 : 138496512 : len &= 0x7f; /* Mask to allow 0x80 -> 0. */
861 [ - + ]: 138496512 : if (unlikely (len > (size_t) (endp - valp)))
862 : : {
863 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
864 : 0 : return -1;
865 : : }
866 : : return len;
867 : : }
868 : : }
869 : :
870 : : /* Other forms require some computation. */
871 : 53609710 : return __libdw_form_val_compute_len (cu, form, valp);
872 : : }
873 : :
874 : : /* Helper function for DW_FORM_ref* handling. */
875 : : extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
876 : : __nonnull_attribute__ (1, 2) internal_function;
877 : :
878 : :
879 : : /* Helper function to locate attribute. */
880 : : extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
881 : : unsigned int search_name,
882 : : unsigned int *codep,
883 : : unsigned int *formp)
884 : : __nonnull_attribute__ (1) internal_function;
885 : :
886 : : /* Helper function to access integer attribute. */
887 : : extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
888 : : __nonnull_attribute__ (1, 2) internal_function;
889 : :
890 : : /* Helper function to walk scopes. */
891 : : struct Dwarf_Die_Chain
892 : : {
893 : : Dwarf_Die die;
894 : : struct Dwarf_Die_Chain *parent;
895 : : bool prune; /* The PREVISIT function can set this. */
896 : : };
897 : : extern int __libdw_visit_scopes (unsigned int depth,
898 : : struct Dwarf_Die_Chain *root,
899 : : struct Dwarf_Die_Chain *imports,
900 : : int (*previsit) (unsigned int depth,
901 : : struct Dwarf_Die_Chain *,
902 : : void *arg),
903 : : int (*postvisit) (unsigned int depth,
904 : : struct Dwarf_Die_Chain *,
905 : : void *arg),
906 : : void *arg)
907 : : __nonnull_attribute__ (2, 4) internal_function;
908 : :
909 : : /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's,
910 : : and cache the result (via tsearch). */
911 : : extern int __libdw_intern_expression (Dwarf *dbg,
912 : : bool other_byte_order,
913 : : unsigned int address_size,
914 : : unsigned int ref_size,
915 : : void **cache, const Dwarf_Block *block,
916 : : bool cfap, bool valuep,
917 : : Dwarf_Op **llbuf, size_t *listlen,
918 : : int sec_index)
919 : : __nonnull_attribute__ (5, 6, 9, 10) internal_function;
920 : :
921 : : extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
922 : : Dwarf_Die *result, bool debug_types)
923 : : internal_function;
924 : :
925 : :
926 : : /* Return error code of last failing function call. This value is kept
927 : : separately for each thread. */
928 : : extern int __dwarf_errno_internal (void);
929 : :
930 : :
931 : : /* Reader hooks. */
932 : :
933 : : /* Relocation hooks return -1 on error (in that case the error code
934 : : must already have been set), 0 if there is no relocation and 1 if a
935 : : relocation was present.*/
936 : :
937 : : static inline int
938 : : __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
939 : : int sec_index __attribute__ ((unused)),
940 : : const void *addr __attribute__ ((unused)),
941 : : int width __attribute__ ((unused)),
942 : : Dwarf_Addr *val __attribute__ ((unused)))
943 : : {
944 : : return 0;
945 : : }
946 : :
947 : : static inline int
948 : : __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
949 : : int sec_index __attribute__ ((unused)),
950 : : const void *addr __attribute__ ((unused)),
951 : : int width __attribute__ ((unused)),
952 : : Dwarf_Off *val __attribute__ ((unused)))
953 : : {
954 : : return 0;
955 : : }
956 : :
957 : : static inline Elf_Data *
958 : 9373762 : __libdw_checked_get_data (Dwarf *dbg, int sec_index)
959 : : {
960 : 9373762 : Elf_Data *data = dbg->sectiondata[sec_index];
961 [ + - ]: 9373762 : if (unlikely (data == NULL)
962 [ - + ]: 9373762 : || unlikely (data->d_buf == NULL))
963 : : {
964 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
965 : 0 : return NULL;
966 : : }
967 : : return data;
968 : : }
969 : :
970 : : static inline int
971 : 4423486 : __libdw_offset_in_section (Dwarf *dbg, int sec_index,
972 : : Dwarf_Off offset, size_t size)
973 : : {
974 : 4423486 : Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
975 [ - + ]: 4423486 : if (data == NULL)
976 : : return -1;
977 [ + - ]: 4423486 : if (unlikely (offset > data->d_size)
978 [ + - ]: 4423486 : || unlikely (data->d_size < size)
979 [ - + ]: 4423486 : || unlikely (offset > data->d_size - size))
980 : : {
981 : 0 : __libdw_seterrno (DWARF_E_INVALID_OFFSET);
982 : 0 : return -1;
983 : : }
984 : :
985 : : return 0;
986 : : }
987 : :
988 : : static inline bool
989 : 4908422 : __libdw_in_section (Dwarf *dbg, int sec_index,
990 : : const void *addr, size_t size)
991 : : {
992 : 4908422 : Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
993 [ - + ]: 4908422 : if (data == NULL)
994 : : return false;
995 [ + - ]: 4908422 : if (unlikely (addr < data->d_buf)
996 [ + - ]: 4908422 : || unlikely (data->d_size < size)
997 [ - + ]: 4908422 : || unlikely ((size_t)(addr - data->d_buf) > data->d_size - size))
998 : : {
999 : 0 : __libdw_seterrno (DWARF_E_INVALID_OFFSET);
1000 : 0 : return false;
1001 : : }
1002 : :
1003 : : return true;
1004 : : }
1005 : :
1006 : : #define READ_AND_RELOCATE(RELOC_HOOK, VAL) \
1007 : : ({ \
1008 : : if (!__libdw_in_section (dbg, sec_index, addr, width)) \
1009 : : return -1; \
1010 : : \
1011 : : const unsigned char *orig_addr = addr; \
1012 : : if (width == 4) \
1013 : : VAL = read_4ubyte_unaligned_inc (dbg, addr); \
1014 : : else \
1015 : : VAL = read_8ubyte_unaligned_inc (dbg, addr); \
1016 : : \
1017 : : int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL); \
1018 : : if (status < 0) \
1019 : : return status; \
1020 : : status > 0; \
1021 : : })
1022 : :
1023 : : static inline int
1024 : 115816 : __libdw_read_address_inc (Dwarf *dbg,
1025 : : int sec_index, const unsigned char **addrp,
1026 : : int width, Dwarf_Addr *ret)
1027 : : {
1028 : 115816 : const unsigned char *addr = *addrp;
1029 [ + - + + : 115816 : READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
+ + + + ]
1030 : 115816 : *addrp = addr;
1031 : 115816 : return 0;
1032 : : }
1033 : :
1034 : : static inline int
1035 : 410202 : __libdw_read_address (Dwarf *dbg,
1036 : : int sec_index, const unsigned char *addr,
1037 : : int width, Dwarf_Addr *ret)
1038 : : {
1039 [ + - + + : 410202 : READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
+ + + + ]
1040 : : return 0;
1041 : : }
1042 : :
1043 : : static inline int
1044 : 137212 : __libdw_read_offset_inc (Dwarf *dbg,
1045 : : int sec_index, const unsigned char **addrp,
1046 : : int width, Dwarf_Off *ret, int sec_ret,
1047 : : size_t size)
1048 : : {
1049 : 137212 : const unsigned char *addr = *addrp;
1050 [ + - + - : 137212 : READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
+ + - - ]
1051 : 137212 : *addrp = addr;
1052 : 137212 : return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
1053 : : }
1054 : :
1055 : : static inline int
1056 : 4244216 : __libdw_read_offset (Dwarf *dbg, Dwarf *dbg_ret,
1057 : : int sec_index, const unsigned char *addr,
1058 : : int width, Dwarf_Off *ret, int sec_ret,
1059 : : size_t size)
1060 : : {
1061 [ + - + - : 4244216 : READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
+ + - - ]
1062 : 4244216 : return __libdw_offset_in_section (dbg_ret, sec_ret, *ret, size);
1063 : : }
1064 : :
1065 : : static inline size_t
1066 : 12100590 : cu_sec_idx (struct Dwarf_CU *cu)
1067 : : {
1068 [ + + ]: 10310586 : return cu->sec_idx;
1069 : : }
1070 : :
1071 : : static inline bool
1072 : 6149510 : is_cudie (Dwarf_Die *cudie)
1073 : : {
1074 [ + + + + ]: 6149510 : return cudie->cu != NULL && CUDIE (cudie->cu).addr == cudie->addr;
1075 : : }
1076 : :
1077 : : /* Read up begin/end pair and increment read pointer.
1078 : : - If it's normal range record, set up *BEGINP and *ENDP and return 0.
1079 : : - If it's base address selection record, set up *BASEP and return 1.
1080 : : - If it's end of rangelist, don't set anything and return 2
1081 : : - If an error occurs, don't set anything and return <0. */
1082 : : int __libdw_read_begin_end_pair_inc (Dwarf_CU *cu, int sec_index,
1083 : : const unsigned char **readp,
1084 : : const unsigned char *readend,
1085 : : int width,
1086 : : Dwarf_Addr *beginp, Dwarf_Addr *endp,
1087 : : Dwarf_Addr *basep)
1088 : : internal_function;
1089 : :
1090 : : const unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
1091 : : int err_nodata,
1092 : : const unsigned char **endpp,
1093 : : Dwarf_Off *offsetp)
1094 : : internal_function;
1095 : :
1096 : : /* Fills in the given attribute to point at an empty location expression. */
1097 : : void __libdw_empty_loc_attr (Dwarf_Attribute *attr)
1098 : : internal_function;
1099 : :
1100 : : /* Load .debug_line unit at DEBUG_LINE_OFFSET. COMP_DIR is a value of
1101 : : DW_AT_comp_dir or NULL if that attribute is not available. Caches
1102 : : the loaded unit and optionally set *LINESP and/or *FILESP (if not
1103 : : NULL) with loaded information. Returns 0 for success or a negative
1104 : : value for failure. */
1105 : : int __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
1106 : : const char *comp_dir, unsigned address_size,
1107 : : Dwarf_Lines **linesp, Dwarf_Files **filesp)
1108 : : internal_function
1109 : : __nonnull_attribute__ (1);
1110 : :
1111 : : /* Load .debug_line unit at DEBUG_LINE_OFFSET. COMP_DIR is a value of
1112 : : DW_AT_comp_dir or NULL if that attribute is not available. Caches
1113 : : the loaded unit and set *FILESP with loaded information. Returns 0
1114 : : for success or a negative value for failure. */
1115 : : int __libdw_getsrcfiles (Dwarf *dbg, Dwarf_Off debug_line_offset,
1116 : : const char *comp_dir, unsigned address_size,
1117 : : Dwarf_Files **filesp)
1118 : : internal_function
1119 : : __nonnull_attribute__ (1);
1120 : :
1121 : : /* Load and return value of DW_AT_comp_dir from CUDIE. */
1122 : : const char *__libdw_getcompdir (Dwarf_Die *cudie);
1123 : :
1124 : : /* Get the base address for the CU, fetches it when not yet set.
1125 : : This is used as initial base address for ranges and loclists. */
1126 : : Dwarf_Addr __libdw_cu_base_address (Dwarf_CU *cu);
1127 : :
1128 : : /* Get the address base for the CU, fetches it when not yet set. */
1129 : : static inline Dwarf_Off
1130 : 1964 : __libdw_cu_addr_base (Dwarf_CU *cu)
1131 : : {
1132 [ + + ]: 1964 : if (cu->addr_base == (Dwarf_Off) -1)
1133 : : {
1134 : 182 : Dwarf_Die cu_die = CUDIE(cu);
1135 : 182 : Dwarf_Attribute attr;
1136 : 182 : Dwarf_Off offset = 0;
1137 [ + + ]: 182 : if (dwarf_attr (&cu_die, DW_AT_GNU_addr_base, &attr) != NULL
1138 [ + - ]: 90 : || dwarf_attr (&cu_die, DW_AT_addr_base, &attr) != NULL)
1139 : : {
1140 : 182 : Dwarf_Word off;
1141 [ + - ]: 182 : if (dwarf_formudata (&attr, &off) == 0)
1142 : 182 : offset = off;
1143 : : }
1144 : 182 : cu->addr_base = offset;
1145 : : }
1146 : :
1147 : 1964 : return cu->addr_base;
1148 : : }
1149 : :
1150 : : /* Gets the .debug_str_offsets base offset to use. static inline to
1151 : : be shared between libdw and eu-readelf. */
1152 : : static inline Dwarf_Off
1153 : 13952 : str_offsets_base_off (Dwarf *dbg, Dwarf_CU *cu)
1154 : : {
1155 : : /* If we don't have a CU, then find and use the first one in the
1156 : : debug file (when we support .dwp files, we must actually find the
1157 : : one matching our "caller" - aka macro or line). If we (now) have
1158 : : a cu and str_offsets_base attribute, just use that. Otherwise
1159 : : use the first offset. But we might have to parse the header
1160 : : first, but only if this is version 5. Assume if all else fails,
1161 : : this is version 4, without header. */
1162 : :
1163 [ + + ]: 13952 : if (cu == NULL && dbg != NULL)
1164 : : {
1165 : 3886 : Dwarf_CU *first_cu;
1166 [ + + ]: 3886 : if (dwarf_get_units (dbg, NULL, &first_cu, NULL, NULL, NULL, NULL) == 0)
1167 : 3884 : cu = first_cu;
1168 : : }
1169 : :
1170 : 13952 : Dwarf_Off off = 0;
1171 [ + + ]: 13952 : if (cu != NULL)
1172 : : {
1173 [ + + ]: 13950 : if (cu->str_off_base == (Dwarf_Off) -1)
1174 : : {
1175 : 286 : Dwarf_Off dwp_offset;
1176 [ + - ]: 286 : if (dwarf_cu_dwp_section_info (cu, DW_SECT_STR_OFFSETS, &dwp_offset,
1177 : : NULL) == 0)
1178 : 286 : off = dwp_offset;
1179 : 286 : Dwarf_Die cu_die = CUDIE(cu);
1180 : 286 : Dwarf_Attribute attr;
1181 [ + + ]: 286 : if (dwarf_attr (&cu_die, DW_AT_str_offsets_base, &attr) != NULL)
1182 : : {
1183 : 18 : Dwarf_Word base;
1184 [ + - ]: 18 : if (dwarf_formudata (&attr, &base) == 0)
1185 : : {
1186 : 18 : cu->str_off_base = off + base;
1187 : 18 : return cu->str_off_base;
1188 : : }
1189 : : }
1190 : : /* For older DWARF simply assume zero (no header). */
1191 [ + + ]: 268 : if (cu->version < 5)
1192 : : {
1193 : 166 : cu->str_off_base = off;
1194 : 166 : return cu->str_off_base;
1195 : : }
1196 : :
1197 [ + + ]: 102 : if (dbg == NULL)
1198 : 68 : dbg = cu->dbg;
1199 : : }
1200 : : else
1201 : : return cu->str_off_base;
1202 : : }
1203 : :
1204 : : /* No str_offsets_base attribute, we have to assume "zero".
1205 : : But there could be a header first. */
1206 [ - + ]: 104 : if (dbg == NULL)
1207 : 0 : goto no_header;
1208 : :
1209 : 104 : Elf_Data *data = dbg->sectiondata[IDX_debug_str_offsets];
1210 [ + + ]: 104 : if (data == NULL)
1211 : 36 : goto no_header;
1212 : :
1213 : 68 : const unsigned char *start;
1214 : 68 : const unsigned char *readp;
1215 : 68 : const unsigned char *readendp;
1216 : 68 : start = readp = (const unsigned char *) data->d_buf;
1217 : 68 : readendp = (const unsigned char *) data->d_buf + data->d_size;
1218 : :
1219 : 68 : uint64_t unit_length;
1220 : 68 : uint16_t version;
1221 : :
1222 [ - + ]: 68 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1223 [ - + ]: 68 : if (unlikely (unit_length == 0xffffffff))
1224 : : {
1225 [ # # ]: 0 : if (unlikely (readendp - readp < 8))
1226 : 0 : goto no_header;
1227 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1228 : : /* In theory the offset size could be different
1229 : : between CU and str_offsets unit. But we just
1230 : : ignore that here. */
1231 : : }
1232 : :
1233 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
1234 : : 4 bytes to complete the header. And this unit cannot go
1235 : : beyond the section data. */
1236 [ + - ]: 68 : if (readendp - readp < 4
1237 [ + - ]: 68 : || unit_length < 4
1238 [ - + ]: 68 : || (uint64_t) (readendp - readp) < unit_length)
1239 : 0 : goto no_header;
1240 : :
1241 [ - + ]: 68 : version = read_2ubyte_unaligned_inc (dbg, readp);
1242 [ - + ]: 68 : if (version != 5)
1243 : 0 : goto no_header;
1244 : : /* padding */
1245 : 68 : read_2ubyte_unaligned_inc (dbg, readp);
1246 : :
1247 : 68 : off += (Dwarf_Off) (readp - start);
1248 : :
1249 : 104 : no_header:
1250 [ + + ]: 104 : if (cu != NULL)
1251 : 102 : cu->str_off_base = off;
1252 : :
1253 : : return off;
1254 : : }
1255 : :
1256 : :
1257 : : /* Get the string offsets base for the CU, fetches it when not yet set. */
1258 : 3402 : static inline Dwarf_Off __libdw_cu_str_off_base (Dwarf_CU *cu)
1259 : : {
1260 : 3402 : return str_offsets_base_off (NULL, cu);
1261 : : }
1262 : :
1263 : :
1264 : : /* Either a direct offset into .debug_ranges for version < 5, or the
1265 : : start of the offset table in .debug_rnglists for version > 5. */
1266 : : static inline Dwarf_Off
1267 : 90 : __libdw_cu_ranges_base (Dwarf_CU *cu)
1268 : : {
1269 [ + + ]: 90 : if (cu->ranges_base == (Dwarf_Off) -1)
1270 : : {
1271 : 18 : Dwarf_Off offset = 0;
1272 : 18 : Dwarf_Die cu_die = CUDIE(cu);
1273 : 18 : Dwarf_Attribute attr;
1274 [ + + ]: 18 : if (cu->version < 5)
1275 : : {
1276 [ + - ]: 12 : if (dwarf_attr (&cu_die, DW_AT_GNU_ranges_base, &attr) != NULL)
1277 : : {
1278 : 12 : Dwarf_Word off;
1279 [ + - ]: 12 : if (dwarf_formudata (&attr, &off) == 0)
1280 : 12 : offset = off;
1281 : : }
1282 : : }
1283 : : else
1284 : : {
1285 : 6 : Dwarf_Off dwp_offset;
1286 [ + - ]: 6 : if (dwarf_cu_dwp_section_info (cu, DW_SECT_RNGLISTS, &dwp_offset,
1287 : : NULL) == 0)
1288 : 6 : offset = dwp_offset;
1289 : :
1290 [ + + ]: 6 : if (dwarf_attr (&cu_die, DW_AT_rnglists_base, &attr) != NULL)
1291 : : {
1292 : 2 : Dwarf_Word off;
1293 [ + - ]: 2 : if (dwarf_formudata (&attr, &off) == 0)
1294 : 2 : offset += off;
1295 : : }
1296 : :
1297 : : /* There wasn't an rnglists_base, if the Dwarf does have a
1298 : : .debug_rnglists section, then it might be we need the
1299 : : base after the first header. */
1300 : 6 : Elf_Data *data = cu->dbg->sectiondata[IDX_debug_rnglists];
1301 [ + + + - ]: 6 : if (offset == dwp_offset && data != NULL)
1302 : : {
1303 : 4 : Dwarf *dbg = cu->dbg;
1304 : 4 : const unsigned char *readp = data->d_buf;
1305 : 4 : const unsigned char *const dataend
1306 : 4 : = (unsigned char *) data->d_buf + data->d_size;
1307 : :
1308 [ - + ]: 4 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1309 : 4 : unsigned int offset_size = 4;
1310 [ - + ]: 4 : if (unlikely (unit_length == 0xffffffff))
1311 : : {
1312 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
1313 : 0 : goto no_header;
1314 : :
1315 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1316 : 0 : offset_size = 8;
1317 : : }
1318 : :
1319 [ + - ]: 4 : if (readp > dataend - 8
1320 [ + - ]: 4 : || unit_length < 8
1321 [ - + ]: 4 : || unit_length > (uint64_t) (dataend - readp))
1322 : 0 : goto no_header;
1323 : :
1324 [ - + ]: 4 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
1325 [ - + ]: 4 : if (version != 5)
1326 : 0 : goto no_header;
1327 : :
1328 : 4 : uint8_t address_size = *readp++;
1329 [ - + ]: 4 : if (address_size != 4 && address_size != 8)
1330 : 0 : goto no_header;
1331 : :
1332 : 4 : uint8_t segment_size = *readp++;
1333 [ - + ]: 4 : if (segment_size != 0)
1334 : 0 : goto no_header;
1335 : :
1336 : 4 : uint32_t offset_entry_count;
1337 [ - + ]: 4 : offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
1338 : :
1339 : 4 : const unsigned char *offset_array_start = readp;
1340 [ - + ]: 4 : if (offset_entry_count <= 0)
1341 : 0 : goto no_header;
1342 : :
1343 : 4 : uint64_t needed = offset_entry_count * offset_size;
1344 [ - + ]: 4 : if (unit_length - 8 < needed)
1345 : 0 : goto no_header;
1346 : :
1347 : 4 : offset += (Dwarf_Off) (offset_array_start
1348 : 4 : - (unsigned char *) data->d_buf);
1349 : : }
1350 : : }
1351 : 18 : no_header:
1352 : 18 : cu->ranges_base = offset;
1353 : : }
1354 : :
1355 : 90 : return cu->ranges_base;
1356 : : }
1357 : :
1358 : :
1359 : : /* The start of the offset table in .debug_loclists for DWARF5. */
1360 : : static inline Dwarf_Off
1361 : 218 : __libdw_cu_locs_base (Dwarf_CU *cu)
1362 : : {
1363 [ + + ]: 218 : if (cu->locs_base == (Dwarf_Off) -1)
1364 : : {
1365 : 8 : Dwarf_Off offset = 0;
1366 : 8 : Dwarf_Off dwp_offset;
1367 [ + - ]: 8 : if (dwarf_cu_dwp_section_info (cu, DW_SECT_LOCLISTS, &dwp_offset, NULL)
1368 : : == 0)
1369 : 8 : offset = dwp_offset;
1370 : :
1371 : 8 : Dwarf_Die cu_die = CUDIE(cu);
1372 : 8 : Dwarf_Attribute attr;
1373 [ + + ]: 8 : if (dwarf_attr (&cu_die, DW_AT_loclists_base, &attr) != NULL)
1374 : : {
1375 : 4 : Dwarf_Word off;
1376 [ + - ]: 4 : if (dwarf_formudata (&attr, &off) == 0)
1377 : 4 : offset += off;
1378 : : }
1379 : :
1380 : : /* There wasn't an loclists_base, if the Dwarf does have a
1381 : : .debug_loclists section, then it might be we need the
1382 : : base after the first header. */
1383 : 8 : Elf_Data *data = cu->dbg->sectiondata[IDX_debug_loclists];
1384 [ - + - + ]: 8 : if (offset == dwp_offset && data != NULL)
1385 : : {
1386 : 8 : Dwarf *dbg = cu->dbg;
1387 : 8 : const unsigned char *readp = data->d_buf;
1388 : 8 : const unsigned char *const dataend
1389 : 8 : = (unsigned char *) data->d_buf + data->d_size;
1390 : :
1391 [ - + ]: 8 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1392 : 8 : unsigned int offset_size = 4;
1393 [ - + ]: 8 : if (unlikely (unit_length == 0xffffffff))
1394 : : {
1395 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
1396 : 0 : goto no_header;
1397 : :
1398 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1399 : 0 : offset_size = 8;
1400 : : }
1401 : :
1402 [ + - ]: 8 : if (readp > dataend - 8
1403 [ + - ]: 8 : || unit_length < 8
1404 [ - + ]: 8 : || unit_length > (uint64_t) (dataend - readp))
1405 : 0 : goto no_header;
1406 : :
1407 [ - + ]: 8 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
1408 [ - + ]: 8 : if (version != 5)
1409 : 0 : goto no_header;
1410 : :
1411 : 8 : uint8_t address_size = *readp++;
1412 [ - + ]: 8 : if (address_size != 4 && address_size != 8)
1413 : 0 : goto no_header;
1414 : :
1415 : 8 : uint8_t segment_size = *readp++;
1416 [ - + ]: 8 : if (segment_size != 0)
1417 : 0 : goto no_header;
1418 : :
1419 : 8 : uint32_t offset_entry_count;
1420 [ - + ]: 8 : offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
1421 : :
1422 : 8 : const unsigned char *offset_array_start = readp;
1423 [ - + ]: 8 : if (offset_entry_count <= 0)
1424 : 0 : goto no_header;
1425 : :
1426 : 8 : uint64_t needed = offset_entry_count * offset_size;
1427 [ - + ]: 8 : if (unit_length - 8 < needed)
1428 : 0 : goto no_header;
1429 : :
1430 : 8 : offset += (Dwarf_Off) (offset_array_start
1431 : 8 : - (unsigned char *) data->d_buf);
1432 : : }
1433 : :
1434 : 0 : no_header:
1435 : 8 : cu->locs_base = offset;
1436 : : }
1437 : :
1438 : 218 : return cu->locs_base;
1439 : : }
1440 : :
1441 : : /* Helper function for tsearch/tfind split_tree Dwarf. */
1442 : : int __libdw_finddbg_cb (const void *arg1, const void *arg2);
1443 : :
1444 : : /* Link skeleton and split compile units. */
1445 : : static inline void
1446 : 170 : __libdw_link_skel_split (Dwarf_CU *skel, Dwarf_CU *split)
1447 : : {
1448 : 170 : skel->split = split;
1449 : 170 : split->split = skel;
1450 : :
1451 : : /* Get .debug_addr and addr_base greedy.
1452 : : We also need it for the fake addr cu.
1453 : : This needs to be done for each split unit (one per .dwo file, or multiple
1454 : : per .dwp file). */
1455 : 170 : Dwarf *dbg = skel->dbg;
1456 : 170 : Dwarf *sdbg = split->dbg;
1457 [ + - ]: 170 : if (dbg->sectiondata[IDX_debug_addr] != NULL
1458 : : /* If this split file hasn't been linked yet... */
1459 [ + + ]: 170 : && (sdbg->sectiondata[IDX_debug_addr] == NULL
1460 : : /* ... or it was linked to the same skeleton file for another
1461 : : unit... */
1462 [ + - ]: 44 : || (sdbg->sectiondata[IDX_debug_addr]
1463 : : == dbg->sectiondata[IDX_debug_addr])))
1464 : : {
1465 : : /* ... then link the address information for this file and unit. */
1466 : 170 : sdbg->sectiondata[IDX_debug_addr]
1467 : 170 : = dbg->sectiondata[IDX_debug_addr];
1468 : 170 : split->addr_base = __libdw_cu_addr_base (skel);
1469 : 170 : sdbg->fake_addr_cu = dbg->fake_addr_cu;
1470 : : }
1471 : 170 : }
1472 : :
1473 : :
1474 : : /* Given an address index for a CU return the address.
1475 : : Returns -1 and sets libdw_errno if an error occurs. */
1476 : : int __libdw_addrx (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr);
1477 : :
1478 : :
1479 : : /* Helper function to set elfpath field in Dwarf, used from dwarf_begin_elf
1480 : : and libdwfl process_file. */
1481 : : char * __libdw_elfpath (int fd);
1482 : :
1483 : : /* Helper function to set debugdir field in Dwarf after elfpath field has been
1484 : : set. */
1485 : : void __libdw_set_debugdir (Dwarf *dbg);
1486 : :
1487 : :
1488 : : /* Given the directory of a debug file, an absolute or relative dir
1489 : : to look in, and file returns a full path.
1490 : :
1491 : : If the file is absolute (starts with a /) a copy of file is returned.
1492 : : the file isn't absolute, but dir is absolute, then a path that is
1493 : : the concatenation of dir and file is returned. If neither file,
1494 : : nor dir is absolute, the path will be constructed using dir (if not
1495 : : NULL) and file relative to the debugdir (if valid).
1496 : :
1497 : : The debugdir and the dir may be NULL (in which case they aren't used).
1498 : : If file is NULL, or no full path can be constructed NULL is returned.
1499 : :
1500 : : The caller is responsible for freeing the result if not NULL. */
1501 : : char * __libdw_filepath (const char *debugdir, const char *dir,
1502 : : const char *file)
1503 : : internal_function;
1504 : :
1505 : : /* Like dwarf_getaranges but aranges are generated from CU address
1506 : : ranges instead of being read from .debug_aranges.
1507 : :
1508 : : Returns 0 if successful and updates ARANGES and NARANGES. Otherwise
1509 : : returns -1 and sets libdw_errno.
1510 : : */
1511 : : int __libdw_getdieranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges);
1512 : : #endif /* libdwP.h */
|