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