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