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 : : /* Internal memory handling. This is basically a simplified thread-local
269 : : reimplementation of obstacks. Unfortunately the standard obstack
270 : : implementation is not usable in libraries. */
271 : : size_t mem_stacks;
272 : : struct libdw_memblock
273 : : {
274 : : size_t size;
275 : : size_t remaining;
276 : : struct libdw_memblock *prev;
277 : : char mem[0];
278 : : } **mem_tails;
279 : :
280 : : /* Default size of allocated memory blocks. */
281 : : size_t mem_default_size;
282 : :
283 : : /* Registered OOM handler. */
284 : : Dwarf_OOM oom_handler;
285 : : };
286 : :
287 : :
288 : : /* Abbreviation representation. */
289 : : struct Dwarf_Abbrev
290 : : {
291 : : Dwarf_Off offset; /* Offset to start of abbrev into .debug_abbrev. */
292 : : unsigned char *attrp; /* Pointer to start of attribute name/form pairs. */
293 : : bool has_children : 1; /* Whether or not the DIE has children. */
294 : : unsigned int code : 31; /* The (unique) abbrev code. */
295 : : unsigned int tag; /* The tag of the DIE. */
296 : : } attribute_packed;
297 : :
298 : : #include "dwarf_abbrev_hash.h"
299 : :
300 : :
301 : : /* Files in line information records. */
302 : : struct Dwarf_Files_s
303 : : {
304 : : unsigned int ndirs;
305 : : unsigned int nfiles;
306 : : struct Dwarf_Fileinfo_s
307 : : {
308 : : char *name;
309 : : Dwarf_Word mtime;
310 : : Dwarf_Word length;
311 : : } info[0];
312 : : /* nfiles of those, followed by char *[ndirs]. */
313 : : };
314 : : typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
315 : :
316 : :
317 : : /* Representation of a row in the line table. */
318 : :
319 : : struct Dwarf_Line_s
320 : : {
321 : : Dwarf_Files *files;
322 : :
323 : : Dwarf_Addr addr;
324 : : unsigned int file;
325 : : int line;
326 : : unsigned short int column;
327 : : unsigned int is_stmt:1;
328 : : unsigned int basic_block:1;
329 : : unsigned int end_sequence:1;
330 : : unsigned int prologue_end:1;
331 : : unsigned int epilogue_begin:1;
332 : : /* The remaining bit fields are not flags, but hold values presumed to be
333 : : small. All the flags and other bit fields should add up to 48 bits
334 : : to give the whole struct a nice round size. */
335 : : unsigned int op_index:8;
336 : : unsigned int isa:8;
337 : : unsigned int discriminator:24;
338 : : /* These are currently only used for the NVIDIA extensions. */
339 : : unsigned int context;
340 : : unsigned int function_name;
341 : : };
342 : :
343 : : struct Dwarf_Lines_s
344 : : {
345 : : size_t nlines;
346 : : struct Dwarf_Line_s info[0];
347 : : };
348 : :
349 : : /* Representation of address ranges. */
350 : : struct Dwarf_Aranges_s
351 : : {
352 : : Dwarf *dbg;
353 : : size_t naranges;
354 : :
355 : : struct Dwarf_Arange_s
356 : : {
357 : : Dwarf_Addr addr;
358 : : Dwarf_Word length;
359 : : Dwarf_Off offset;
360 : : } info[0];
361 : : };
362 : :
363 : : /* DWARF package file unit index. */
364 : : typedef struct Dwarf_Package_Index_s
365 : : {
366 : : Dwarf *dbg;
367 : : uint32_t section_count;
368 : : uint32_t unit_count;
369 : : uint32_t slot_count;
370 : : /* Mapping from DW_SECT_* - 1 to column number in the section tables, or
371 : : UINT32_MAX if not present. */
372 : : uint32_t sections[DW_SECT_RNGLISTS];
373 : : /* Row number of last unit found in the index. */
374 : : uint32_t last_unit_found;
375 : : const unsigned char *hash_table;
376 : : const unsigned char *indices;
377 : : const unsigned char *section_offsets;
378 : : const unsigned char *section_sizes;
379 : : /* If DW_SECT_INFO section offsets were truncated to 32 bits, recovered
380 : : 64-bit offsets. */
381 : : Dwarf_Off *debug_info_offsets;
382 : : } Dwarf_Package_Index;
383 : :
384 : : /* CU representation. */
385 : : struct Dwarf_CU
386 : : {
387 : : Dwarf *dbg;
388 : : Dwarf_Off start;
389 : : Dwarf_Off end;
390 : : /* Row number of this unit in DWARF package file index. */
391 : : uint32_t dwp_row;
392 : : uint8_t address_size;
393 : : uint8_t offset_size;
394 : : uint16_t version;
395 : :
396 : : size_t sec_idx; /* Normally .debug_info, could be .debug_type or "fake". */
397 : :
398 : : /* The unit type if version >= 5. Otherwise 0 for normal CUs (from
399 : : .debug_info) or 1 for v4 type units (from .debug_types). */
400 : : uint8_t unit_type;
401 : :
402 : : /* Zero if the unit type doesn't support a die/type offset and/or id/sig.
403 : : Nonzero if it is a v4 type unit or for DWARFv5 units depending on
404 : : unit_type. */
405 : : size_t subdie_offset;
406 : : uint64_t unit_id8;
407 : :
408 : : /* If this is a skeleton unit this points to the split compile unit.
409 : : Or the other way around if this is a split compile unit. Set to -1
410 : : if not yet searched. Always use __libdw_find_split_unit to access
411 : : this field. */
412 : : struct Dwarf_CU *split;
413 : :
414 : : /* Hash table for the abbreviations. */
415 : : Dwarf_Abbrev_Hash abbrev_hash;
416 : : /* Offset of the first abbreviation. */
417 : : size_t orig_abbrev_offset;
418 : : /* Offset past last read abbreviation. */
419 : : size_t last_abbrev_offset;
420 : :
421 : : /* The srcline information. */
422 : : Dwarf_Lines *lines;
423 : :
424 : : /* The source file information. */
425 : : Dwarf_Files *files;
426 : :
427 : : /* Known location lists. */
428 : : search_tree locs_tree;
429 : :
430 : : /* Base address for use with ranges and locs.
431 : : Don't access directly, call __libdw_cu_base_address. */
432 : : Dwarf_Addr base_address;
433 : :
434 : : /* The offset into the .debug_addr section where index zero begins.
435 : : Don't access directly, call __libdw_cu_addr_base. */
436 : : Dwarf_Off addr_base;
437 : :
438 : : /* The offset into the .debug_str_offsets section where index zero begins.
439 : : Don't access directly, call __libdw_cu_str_off_base. */
440 : : Dwarf_Off str_off_base;
441 : :
442 : : /* The offset into the .debug_ranges section to use for GNU
443 : : DebugFission split units. Don't access directly, call
444 : : __libdw_cu_ranges_base. */
445 : : Dwarf_Off ranges_base;
446 : :
447 : : /* The start of the offset table in .debug_loclists.
448 : : Don't access directly, call __libdw_cu_locs_base. */
449 : : Dwarf_Off locs_base;
450 : :
451 : : /* Synchronize access to the split member of this Dwarf_CU.
452 : : Covers __libdw_find_split_unit. */
453 : : rwlock_define(, split_lock);
454 : :
455 : : /* Synchronize access to the last_abbrev_offset member of a Dwarf_Die
456 : : that refers to this Dwarf_CU. */
457 : : mutex_define(, abbrev_lock);
458 : :
459 : : /* Synchronize access to the lines and files members.
460 : : Covers dwarf_getsrclines and dwarf_getsrcfiles. */
461 : : mutex_define(, src_lock);
462 : :
463 : : /* Synchronize access to the str_off_base of this Dwarf_CU.
464 : : Covers __libdw_str_offsets_base_off. */
465 : : mutex_define(, str_off_base_lock);
466 : :
467 : : /* Synchronize access to is_constant_offset. Should also be held
468 : : when calling __libdw_intern_expression with Dwarf_CU members. */
469 : : mutex_define(, intern_lock);
470 : :
471 : : /* Memory boundaries of this CU. */
472 : : void *startp;
473 : : void *endp;
474 : : };
475 : :
476 : : /* Aliases to avoid PLTs. */
477 : : INTDECL (dwarf_aggregate_size)
478 : : INTDECL (dwarf_attr)
479 : : INTDECL (dwarf_attr_integrate)
480 : : INTDECL (dwarf_begin)
481 : : INTDECL (dwarf_begin_elf)
482 : : INTDECL (dwarf_begin_elf_type)
483 : : INTDECL (dwarf_begin_type)
484 : : INTDECL (dwarf_child)
485 : : INTDECL (dwarf_cu_dwp_section_info)
486 : : INTDECL (dwarf_default_lower_bound)
487 : : INTDECL (dwarf_dieoffset)
488 : : INTDECL (dwarf_diename)
489 : : INTDECL (dwarf_end)
490 : : INTDECL (dwarf_entrypc)
491 : : INTDECL (dwarf_errmsg)
492 : : INTDECL (dwarf_formaddr)
493 : : INTDECL (dwarf_formblock)
494 : : INTDECL (dwarf_formref_die)
495 : : INTDECL (dwarf_formsdata)
496 : : INTDECL (dwarf_formstring)
497 : : INTDECL (dwarf_formudata)
498 : : INTDECL (dwarf_getabbrevattr_data)
499 : : INTDECL (dwarf_getalt)
500 : : INTDECL (dwarf_getarange_addr)
501 : : INTDECL (dwarf_getarangeinfo)
502 : : INTDECL (dwarf_getaranges)
503 : : INTDECL (dwarf_getlocation_die)
504 : : INTDECL (dwarf_getsrcfiles)
505 : : INTDECL (dwarf_getsrclines)
506 : : INTDECL (dwarf_get_type)
507 : : INTDECL (dwarf_get_units)
508 : : INTDECL (dwarf_hasattr)
509 : : INTDECL (dwarf_haschildren)
510 : : INTDECL (dwarf_haspc)
511 : : INTDECL (dwarf_highpc)
512 : : INTDECL (dwarf_language)
513 : : INTDECL (dwarf_language_lower_bound)
514 : : INTDECL (dwarf_lowpc)
515 : : INTDECL (dwarf_nextcu)
516 : : INTDECL (dwarf_next_unit)
517 : : INTDECL (dwarf_offdie)
518 : : INTDECL (dwarf_peel_type)
519 : : INTDECL (dwarf_ranges)
520 : : INTDECL (dwarf_setalt)
521 : : INTDECL (dwarf_siblingof)
522 : : INTDECL (dwarf_srclang)
523 : : INTDECL (dwarf_tag)
524 : :
525 : : #define ISV4TU(cu) ((cu)->version == 4 && (cu)->sec_idx == IDX_debug_types)
526 : :
527 : : /* Compute the offset of a CU's first DIE from the CU offset.
528 : : CU must be a valid/known version/unit_type. */
529 : : static inline Dwarf_Off
530 : 9264266 : __libdw_first_die_from_cu_start (Dwarf_Off cu_start,
531 : : uint8_t offset_size,
532 : : uint16_t version,
533 : : uint8_t unit_type)
534 : : {
535 : : /*
536 : : assert (offset_size == 4 || offset_size == 8);
537 : : assert (version >= 2 && version <= 5);
538 : : assert (unit_type == DW_UT_compile
539 : : || unit_type == DW_UT_partial
540 : : || unit_type == DW_UT_skeleton
541 : : || unit_type == DW_UT_split_compile
542 : : || unit_type == DW_UT_type
543 : : || unit_type == DW_UT_split_type);
544 : : */
545 : :
546 : 9264266 : Dwarf_Off off = cu_start;
547 [ + + ]: 9264266 : if (version < 5)
548 : : {
549 : : /*
550 : : LEN VER OFFSET ADDR
551 : : 4-bytes + 2-bytes + 4-bytes + 1-byte for 32-bit dwarf
552 : : 12-bytes + 2-bytes + 8-bytes + 1-byte for 64-bit dwarf
553 : : or in .debug_types, SIGNATURE TYPE-OFFSET
554 : : 4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes for 32-bit
555 : : 12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes for 64-bit
556 : :
557 : : Note the trick in the computation. If the offset_size is 4
558 : : the '- 4' term changes the '3 *' (or '4 *') into a '2 *' (or '3 *).
559 : : If the offset_size is 8 it accounts for the 4-byte escape value
560 : : used at the start of the length. */
561 [ + + ]: 214010 : if (unit_type != DW_UT_type)
562 : 213900 : off += 3 * offset_size - 4 + 3;
563 : : else
564 : 110 : off += 4 * offset_size - 4 + 3 + 8;
565 : : }
566 : : else
567 : : {
568 : : /*
569 : : LEN VER TYPE ADDR OFFSET SIGNATURE TYPE-OFFSET
570 : : 4-bytes + 2-bytes + 1-byte + 1-byte + 4-bytes + 8-bytes + 4-bytes 32-bit
571 : : 12-bytes + 2-bytes + 1-byte + 1-byte + 8-bytes + 8-bytes + 8-bytes 64-bit
572 : : Both signature and type offset are optional.
573 : :
574 : : Note same 4/8 offset size trick as above.
575 : : We explicitly ignore unknown unit types (see asserts above). */
576 : 9050256 : off += 3 * offset_size - 4 + 4;
577 : 9050256 : if (unit_type == DW_UT_skeleton || unit_type == DW_UT_split_compile
578 [ + + + + ]: 9050256 : || unit_type == DW_UT_type || unit_type == DW_UT_split_type)
579 : : {
580 : 3148 : off += 8;
581 [ + + ]: 3148 : if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
582 : 52 : off += offset_size;
583 : : }
584 : : }
585 : :
586 : 9264266 : return off;
587 : : }
588 : :
589 : : static inline Dwarf_Off
590 : 9151008 : __libdw_first_die_off_from_cu (struct Dwarf_CU *cu)
591 : : {
592 : 9151008 : return __libdw_first_die_from_cu_start (cu->start,
593 : : cu->offset_size,
594 : : cu->version,
595 : : cu->unit_type);
596 : : }
597 : :
598 : : #define CUDIE(fromcu) \
599 : : ((Dwarf_Die) \
600 : : { \
601 : : .cu = (fromcu), \
602 : : .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
603 : : + __libdw_first_die_off_from_cu (fromcu)) \
604 : : })
605 : :
606 : : #define SUBDIE(fromcu) \
607 : : ((Dwarf_Die) \
608 : : { \
609 : : .cu = (fromcu), \
610 : : .addr = ((char *) (fromcu)->dbg->sectiondata[cu_sec_idx (fromcu)]->d_buf \
611 : : + (fromcu)->start + (fromcu)->subdie_offset) \
612 : : })
613 : :
614 : :
615 : : /* Prototype of a single .debug_macro operator. */
616 : : typedef struct
617 : : {
618 : : Dwarf_Word nforms;
619 : : unsigned char const *forms;
620 : : } Dwarf_Macro_Op_Proto;
621 : :
622 : : /* Prototype table. */
623 : : typedef struct
624 : : {
625 : : Dwarf *dbg;
626 : :
627 : : /* Offset of .debug_macro section. */
628 : : Dwarf_Off offset;
629 : :
630 : : /* Offset of associated .debug_line section. */
631 : : Dwarf_Off line_offset;
632 : :
633 : : /* The source file information. */
634 : : Dwarf_Files *files;
635 : :
636 : : /* If this macro unit was opened through dwarf_getmacros or
637 : : dwarf_getmacros_die, this caches value of DW_AT_comp_dir, if
638 : : present. */
639 : : const char *comp_dir;
640 : :
641 : : /* Header length. */
642 : : Dwarf_Half header_len;
643 : :
644 : : uint16_t version;
645 : : uint8_t address_size;
646 : : uint8_t offset_size;
647 : : uint8_t sec_index; /* IDX_debug_macro or IDX_debug_macinfo. */
648 : :
649 : : /* Shows where in TABLE each opcode is defined. Since opcode 0 is
650 : : never used, it stores index of opcode X in X-1'th element. The
651 : : value of 0xff means not stored at all. */
652 : : unsigned char opcodes[255];
653 : :
654 : : /* Individual opcode prototypes. */
655 : : Dwarf_Macro_Op_Proto table[];
656 : : } Dwarf_Macro_Op_Table;
657 : :
658 : : struct Dwarf_Macro_s
659 : : {
660 : : Dwarf_Macro_Op_Table *table;
661 : : Dwarf_Attribute *attributes;
662 : : uint8_t opcode;
663 : : };
664 : :
665 : : static inline Dwarf_Word
666 : 6852 : libdw_macro_nforms (Dwarf_Macro *macro)
667 : : {
668 : 6852 : return macro->table->table[macro->table->opcodes[macro->opcode - 1]].nforms;
669 : : }
670 : :
671 : : /* Returns true for any allowed FORM in the opcode_operands_table as
672 : : mentioned in the DWARF5 spec (6.3.1 Macro Information Header).
673 : : Or those mentioned in DWARF5 spec (6.2.4.2 Vendor-defined Content
674 : : Descriptions) for the directory/file table (plus DW_FORM_strp_sup). */
675 : : static inline bool
676 : 134810 : libdw_valid_user_form (int form)
677 : : {
678 [ - + ]: 134810 : switch (form)
679 : : {
680 : : case DW_FORM_block:
681 : : case DW_FORM_block1:
682 : : case DW_FORM_block2:
683 : : case DW_FORM_block4:
684 : : case DW_FORM_data1:
685 : : case DW_FORM_data2:
686 : : case DW_FORM_data4:
687 : : case DW_FORM_data8:
688 : : case DW_FORM_data16:
689 : : case DW_FORM_flag:
690 : : case DW_FORM_line_strp:
691 : : case DW_FORM_sdata:
692 : : case DW_FORM_sec_offset:
693 : : case DW_FORM_string:
694 : : case DW_FORM_strp:
695 : : case DW_FORM_strp_sup:
696 : : case DW_FORM_strx:
697 : : case DW_FORM_strx1:
698 : : case DW_FORM_strx2:
699 : : case DW_FORM_strx3:
700 : : case DW_FORM_strx4:
701 : : case DW_FORM_udata:
702 : : return true;
703 : 0 : default:
704 : 0 : return false;
705 : : }
706 : : }
707 : :
708 : :
709 : : /* We have to include the file at this point because the inline
710 : : functions access internals of the Dwarf structure. */
711 : : #include "memory-access.h"
712 : :
713 : :
714 : : /* Set error value. */
715 : : extern void __libdw_seterrno (int value) internal_function;
716 : :
717 : :
718 : : /* Memory handling, the easy parts. */
719 : : #define libdw_alloc(dbg, type, tsize, cnt) \
720 : : ({ struct libdw_memblock *_tail = __libdw_alloc_tail(dbg); \
721 : : size_t _required = (tsize) * (cnt); \
722 : : type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
723 : : size_t _padding = ((__alignof (type) \
724 : : - ((uintptr_t) _result & (__alignof (type) - 1))) \
725 : : & (__alignof (type) - 1)); \
726 : : if (unlikely (_tail->remaining < _required + _padding)) \
727 : : _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
728 : : else \
729 : : { \
730 : : _required += _padding; \
731 : : _result = (type *) ((char *) _result + _padding); \
732 : : _tail->remaining -= _required; \
733 : : } \
734 : : _result; })
735 : :
736 : : #define libdw_typed_alloc(dbg, type) \
737 : : libdw_alloc (dbg, type, sizeof (type), 1)
738 : :
739 : : /* Can only be used to undo the last libdw_alloc. */
740 : : #define libdw_unalloc(dbg, type, tsize, cnt) \
741 : : ({ struct libdw_memblock *_tail = __libdw_thread_tail (dbg); \
742 : : size_t _required = (tsize) * (cnt); \
743 : : /* We cannot know the padding, it is lost. */ \
744 : : _tail->remaining += _required; }) \
745 : :
746 : : #define libdw_typed_unalloc(dbg, type) \
747 : : libdw_unalloc (dbg, type, sizeof (type), 1)
748 : :
749 : : /* Callback to choose a thread-local memory allocation stack. */
750 : : extern struct libdw_memblock *__libdw_alloc_tail (Dwarf* dbg)
751 : : __nonnull_attribute__ (1);
752 : :
753 : : extern struct libdw_memblock *__libdw_thread_tail (Dwarf* dbg)
754 : : __nonnull_attribute__ (1);
755 : :
756 : : /* Callback to allocate more. */
757 : : extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
758 : : __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
759 : :
760 : : /* Default OOM handler. */
761 : : extern void __libdw_oom (void) __attribute ((noreturn)) attribute_hidden;
762 : :
763 : : /* Read next unit (or v4 debug type) and return next offset. Doesn't
764 : : create an actual Dwarf_CU just provides necessary header fields. */
765 : : extern int
766 : : internal_function
767 : : __libdw_next_unit (Dwarf *dbg, bool v4_debug_types, Dwarf_Off off,
768 : : Dwarf_Off *next_off, size_t *header_sizep,
769 : : Dwarf_Half *versionp, uint8_t *unit_typep,
770 : : Dwarf_Off *abbrev_offsetp, uint8_t *address_sizep,
771 : : uint8_t *offset_sizep, uint64_t *unit_id8p,
772 : : Dwarf_Off *subdie_offsetp)
773 : : __nonnull_attribute__ (4) internal_function;
774 : :
775 : : /* Allocate the internal data for a unit not seen before. */
776 : : extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
777 : : __nonnull_attribute__ (1) internal_function;
778 : :
779 : : /* Find CU for given offset. */
780 : : extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
781 : : __nonnull_attribute__ (1) internal_function;
782 : :
783 : : /* Find CU for given DIE address. */
784 : : extern struct Dwarf_CU *__libdw_findcu_addr (Dwarf *dbg, void *addr)
785 : : __nonnull_attribute__ (1) internal_function;
786 : :
787 : : /* Find split Dwarf for given DIE address. */
788 : : extern struct Dwarf *__libdw_find_split_dbg_addr (Dwarf *dbg, void *addr)
789 : : __nonnull_attribute__ (1) internal_function;
790 : :
791 : : /* Find the split (or skeleton) unit. */
792 : : extern struct Dwarf_CU *__libdw_find_split_unit (Dwarf_CU *cu)
793 : : internal_function;
794 : :
795 : : /* Find a unit in a DWARF package file for __libdw_intern_next_unit. */
796 : : extern int __libdw_dwp_find_unit (Dwarf *dbg, bool debug_types, Dwarf_Off off,
797 : : uint16_t version, uint8_t unit_type,
798 : : uint64_t unit_id8, uint32_t *unit_rowp,
799 : : Dwarf_Off *abbrev_offsetp)
800 : : __nonnull_attribute__ (1, 7, 8) internal_function;
801 : :
802 : : /* Find the compilation unit in a DWARF package file with the given id. */
803 : : extern Dwarf_CU *__libdw_dwp_findcu_id (Dwarf *dbg, uint64_t unit_id8)
804 : : __nonnull_attribute__ (1) internal_function;
805 : :
806 : : /* Get abbreviation with given code. */
807 : : extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
808 : : unsigned int code)
809 : : __nonnull_attribute__ (1) internal_function;
810 : :
811 : : /* Get abbreviation at given offset. */
812 : : extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
813 : : Dwarf_Off offset, size_t *lengthp)
814 : : __nonnull_attribute__ (1) internal_function;
815 : :
816 : : /* Get abbreviation of given DIE, and optionally set *READP to the DIE memory
817 : : just past the abbreviation code. */
818 : : static inline Dwarf_Abbrev *
819 : : __nonnull_attribute__ (1)
820 : 94588072 : __libdw_dieabbrev (Dwarf_Die *die, const unsigned char **readp)
821 : : {
822 : 94588072 : Dwarf_Abbrev *end_abbrev = DWARF_END_ABBREV;
823 : 94588072 : Dwarf_Abbrev *expected = (Dwarf_Abbrev *) NULL;
824 : :
825 [ + + ]: 94588072 : if (unlikely (die->cu == NULL))
826 : : {
827 : : /* __atomic_* compiler builtin functions are used instead of <stdatomic.h>
828 : : because the builtins can operate on non-_Atomic types.
829 : : Dwarf_Die.abbrev cannot be made _Atomic without possibly breaking ABI
830 : : compatibility. */
831 : 44718 : __atomic_compare_exchange_n (&die->abbrev, &expected, end_abbrev, false,
832 : : __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
833 : 44718 : return end_abbrev;
834 : : }
835 : :
836 : 94543354 : Dwarf_Abbrev *abbrev = __atomic_load_n (&die->abbrev, __ATOMIC_ACQUIRE);
837 [ + + ]: 94543354 : if (abbrev == NULL || readp != NULL)
838 : : {
839 : : /* We need to get the abbreviation or need to read after the code. */
840 : 50567332 : unsigned int code;
841 : 50567332 : const unsigned char *addr = die->addr;
842 [ - + ]: 50567332 : if (addr >= (const unsigned char *) die->cu->endp)
843 : : {
844 : 0 : __atomic_compare_exchange_n (&die->abbrev, &expected,
845 : : end_abbrev, false,
846 : : __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
847 : 0 : return end_abbrev;
848 : : }
849 : :
850 : : /* Get the abbreviation code. */
851 : 50567332 : get_uleb128 (code, addr, die->cu->endp);
852 [ + + ]: 50567332 : if (readp != NULL)
853 : 35915526 : *readp = addr;
854 : :
855 : : /* Find the abbreviation. */
856 [ + + ]: 50567332 : if (abbrev == NULL)
857 : : {
858 : 19911868 : abbrev = __libdw_findabbrev (die->cu, code);
859 : 19911868 : __atomic_compare_exchange_n (&die->abbrev, &expected, abbrev, false,
860 : : __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
861 : : }
862 : : }
863 : :
864 : : return abbrev;
865 : : }
866 : :
867 : : /* Helper functions for form handling. */
868 : : extern size_t __libdw_form_val_compute_len (struct Dwarf_CU *cu,
869 : : unsigned int form,
870 : : const unsigned char *valp)
871 : : __nonnull_attribute__ (1, 3) internal_function;
872 : :
873 : : /* Find the length of a form attribute in DIE/info data. */
874 : : static inline size_t
875 : : __nonnull_attribute__ (1, 3)
876 : 143715916 : __libdw_form_val_len (struct Dwarf_CU *cu, unsigned int form,
877 : : const unsigned char *valp)
878 : : {
879 : : /* Small lookup table of forms with fixed lengths. Absent indexes are
880 : : initialized 0, so any truly desired 0 is set to 0x80 and masked. */
881 : 143715916 : static const uint8_t form_lengths[] =
882 : : {
883 : : [DW_FORM_flag_present] = 0x80,
884 : : [DW_FORM_implicit_const] = 0x80, /* Value is in abbrev, not in info. */
885 : :
886 : : [DW_FORM_flag] = 1,
887 : : [DW_FORM_data1] = 1, [DW_FORM_ref1] = 1,
888 : : [DW_FORM_addrx1] = 1, [DW_FORM_strx1] = 1,
889 : :
890 : : [DW_FORM_data2] = 2, [DW_FORM_ref2] = 2,
891 : : [DW_FORM_addrx2] = 2, [DW_FORM_strx2] = 2,
892 : :
893 : : [DW_FORM_addrx3] = 3, [DW_FORM_strx3] = 3,
894 : :
895 : : [DW_FORM_data4] = 4, [DW_FORM_ref4] = 4, [DW_FORM_ref_sup4] = 4,
896 : : [DW_FORM_addrx4] = 4, [DW_FORM_strx4] = 4,
897 : :
898 : : [DW_FORM_ref_sig8] = 8,
899 : : [DW_FORM_data8] = 8, [DW_FORM_ref8] = 8, [DW_FORM_ref_sup8] = 8,
900 : :
901 : : [DW_FORM_data16] = 16,
902 : : };
903 : :
904 : : /* Return immediately for forms with fixed lengths. */
905 [ + + ]: 143715916 : if (form < sizeof form_lengths / sizeof form_lengths[0])
906 : : {
907 : 143698532 : uint8_t len = form_lengths[form];
908 [ + + ]: 143698532 : if (len != 0)
909 : : {
910 : 98132780 : const unsigned char *endp = cu->endp;
911 : 98132780 : len &= 0x7f; /* Mask to allow 0x80 -> 0. */
912 [ - + ]: 98132780 : if (unlikely (len > (size_t) (endp - valp)))
913 : : {
914 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
915 : 0 : return -1;
916 : : }
917 : : return len;
918 : : }
919 : : }
920 : :
921 : : /* Other forms require some computation. */
922 : 45583136 : return __libdw_form_val_compute_len (cu, form, valp);
923 : : }
924 : :
925 : : /* Helper function for DW_FORM_ref* handling. */
926 : : extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
927 : : __nonnull_attribute__ (1, 2) internal_function;
928 : :
929 : :
930 : : /* Helper function to locate attribute. */
931 : : extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
932 : : unsigned int search_name,
933 : : unsigned int *codep,
934 : : unsigned int *formp)
935 : : __nonnull_attribute__ (1) internal_function;
936 : :
937 : : /* Helper function to access integer attribute. */
938 : : extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
939 : : __nonnull_attribute__ (1, 2) internal_function;
940 : :
941 : : /* Helper function to walk scopes. */
942 : : struct Dwarf_Die_Chain
943 : : {
944 : : Dwarf_Die die;
945 : : struct Dwarf_Die_Chain *parent;
946 : : bool prune; /* The PREVISIT function can set this. */
947 : : };
948 : : extern int __libdw_visit_scopes (unsigned int depth,
949 : : struct Dwarf_Die_Chain *root,
950 : : struct Dwarf_Die_Chain *imports,
951 : : int (*previsit) (unsigned int depth,
952 : : struct Dwarf_Die_Chain *,
953 : : void *arg),
954 : : int (*postvisit) (unsigned int depth,
955 : : struct Dwarf_Die_Chain *,
956 : : void *arg),
957 : : void *arg)
958 : : __nonnull_attribute__ (2, 4) internal_function;
959 : :
960 : : /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's, and cache the
961 : : result (via tsearch). The owner of CACHE (typically a Dwarf_CU or
962 : : Dwarf_CFI_s) must hold a lock when calling this function. */
963 : : extern int __libdw_intern_expression (Dwarf *dbg,
964 : : bool other_byte_order,
965 : : unsigned int address_size,
966 : : unsigned int ref_size,
967 : : search_tree *cache,
968 : : const Dwarf_Block *block,
969 : : bool cfap, bool valuep,
970 : : Dwarf_Op **llbuf, size_t *listlen,
971 : : int sec_index)
972 : : __nonnull_attribute__ (5, 6, 9, 10) internal_function;
973 : :
974 : : extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
975 : : Dwarf_Die *result, bool debug_types)
976 : : internal_function;
977 : :
978 : :
979 : : /* Return error code of last failing function call. This value is kept
980 : : separately for each thread. */
981 : : extern int __dwarf_errno_internal (void);
982 : :
983 : :
984 : : /* Reader hooks. */
985 : :
986 : : /* Relocation hooks return -1 on error (in that case the error code
987 : : must already have been set), 0 if there is no relocation and 1 if a
988 : : relocation was present.*/
989 : :
990 : : static inline int
991 : 612472 : __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
992 : : int sec_index __attribute__ ((unused)),
993 : : const void *addr __attribute__ ((unused)),
994 : : int width __attribute__ ((unused)),
995 : : Dwarf_Addr *val __attribute__ ((unused)))
996 : : {
997 : 612472 : return 0;
998 : : }
999 : :
1000 : : static inline int
1001 : 9590406 : __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
1002 : : int sec_index __attribute__ ((unused)),
1003 : : const void *addr __attribute__ ((unused)),
1004 : : int width __attribute__ ((unused)),
1005 : : Dwarf_Off *val __attribute__ ((unused)))
1006 : : {
1007 : 9590406 : return 0;
1008 : : }
1009 : :
1010 : : static inline Elf_Data *
1011 : 19871000 : __libdw_checked_get_data (Dwarf *dbg, int sec_index)
1012 : : {
1013 : 19871000 : Elf_Data *data = dbg->sectiondata[sec_index];
1014 [ + - ]: 19871000 : if (unlikely (data == NULL)
1015 [ - + ]: 19871000 : || unlikely (data->d_buf == NULL))
1016 : : {
1017 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
1018 : 0 : return NULL;
1019 : : }
1020 : : return data;
1021 : : }
1022 : :
1023 : : static inline int
1024 : 9629358 : __libdw_offset_in_section (Dwarf *dbg, int sec_index,
1025 : : Dwarf_Off offset, size_t size)
1026 : : {
1027 : 9629358 : Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
1028 [ + - ]: 9629358 : if (data == NULL)
1029 : : return -1;
1030 [ + - ]: 9629358 : if (unlikely (offset > data->d_size)
1031 [ + - ]: 9629358 : || unlikely (data->d_size < size)
1032 [ - + ]: 9629358 : || unlikely (offset > data->d_size - size))
1033 : : {
1034 : 0 : __libdw_seterrno (DWARF_E_INVALID_OFFSET);
1035 : 0 : return -1;
1036 : : }
1037 : :
1038 : : return 0;
1039 : : }
1040 : :
1041 : : static inline bool
1042 : 10202878 : __libdw_in_section (Dwarf *dbg, int sec_index,
1043 : : const void *addr, size_t size)
1044 : : {
1045 : 10202878 : Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
1046 [ + - ]: 10202878 : if (data == NULL)
1047 : : return false;
1048 [ + - ]: 10202878 : if (unlikely (addr < data->d_buf)
1049 [ + - ]: 10202878 : || unlikely (data->d_size < size)
1050 [ - + ]: 10202878 : || unlikely ((size_t)(addr - data->d_buf) > data->d_size - size))
1051 : : {
1052 : 0 : __libdw_seterrno (DWARF_E_INVALID_OFFSET);
1053 : 0 : return false;
1054 : : }
1055 : :
1056 : : return true;
1057 : : }
1058 : :
1059 : : #define READ_AND_RELOCATE(RELOC_HOOK, VAL) \
1060 : : ({ \
1061 : : if (!__libdw_in_section (dbg, sec_index, addr, width)) \
1062 : : return -1; \
1063 : : \
1064 : : const unsigned char *orig_addr = addr; \
1065 : : if (width == 4) \
1066 : : VAL = read_4ubyte_unaligned_inc (dbg, addr); \
1067 : : else \
1068 : : VAL = read_8ubyte_unaligned_inc (dbg, addr); \
1069 : : \
1070 : : int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL); \
1071 : : if (status < 0) \
1072 : : return status; \
1073 : : status > 0; \
1074 : : })
1075 : :
1076 : : static inline int
1077 : 38332 : __libdw_read_address_inc (Dwarf *dbg,
1078 : : int sec_index, const unsigned char **addrp,
1079 : : int width, Dwarf_Addr *ret)
1080 : : {
1081 : 38332 : const unsigned char *addr = *addrp;
1082 [ + - + + : 38332 : READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
+ + + + +
- ]
1083 : 38332 : *addrp = addr;
1084 : 38332 : return 0;
1085 : : }
1086 : :
1087 : : static inline int
1088 : 573144 : __libdw_read_address (Dwarf *dbg,
1089 : : int sec_index, const unsigned char *addr,
1090 : : int width, Dwarf_Addr *ret)
1091 : : {
1092 [ + - + + : 573144 : READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
+ + + + -
+ ]
1093 : : return 0;
1094 : : }
1095 : :
1096 : : static inline int
1097 : 113848 : __libdw_read_offset_inc (Dwarf *dbg,
1098 : : int sec_index, const unsigned char **addrp,
1099 : : int width, Dwarf_Off *ret, int sec_ret,
1100 : : size_t size)
1101 : : {
1102 : 113848 : const unsigned char *addr = *addrp;
1103 [ + - + - : 113848 : READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
+ + - - +
- ]
1104 : 113848 : *addrp = addr;
1105 : 113848 : return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
1106 : : }
1107 : :
1108 : : static inline int
1109 : 9476558 : __libdw_read_offset (Dwarf *dbg, Dwarf *dbg_ret,
1110 : : int sec_index, const unsigned char *addr,
1111 : : int width, Dwarf_Off *ret, int sec_ret,
1112 : : size_t size)
1113 : : {
1114 [ + - + - : 9476558 : READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
+ + - - +
- ]
1115 : 9476558 : return __libdw_offset_in_section (dbg_ret, sec_ret, *ret, size);
1116 : : }
1117 : :
1118 : : static inline size_t
1119 : 19239508 : cu_sec_idx (struct Dwarf_CU *cu)
1120 : : {
1121 : 19239508 : return cu->sec_idx;
1122 : : }
1123 : :
1124 : : static inline bool
1125 : 7662222 : is_cudie (Dwarf_Die *cudie)
1126 : : {
1127 [ + + + + ]: 7662222 : return cudie->cu != NULL && CUDIE (cudie->cu).addr == cudie->addr;
1128 : : }
1129 : :
1130 : : /* Read up begin/end pair and increment read pointer.
1131 : : - If it's normal range record, set up *BEGINP and *ENDP and return 0.
1132 : : - If it's base address selection record, set up *BASEP and return 1.
1133 : : - If it's end of rangelist, don't set anything and return 2
1134 : : - If an error occurs, don't set anything and return <0. */
1135 : : int __libdw_read_begin_end_pair_inc (Dwarf_CU *cu, int sec_index,
1136 : : const unsigned char **readp,
1137 : : const unsigned char *readend,
1138 : : int width,
1139 : : Dwarf_Addr *beginp, Dwarf_Addr *endp,
1140 : : Dwarf_Addr *basep)
1141 : : internal_function;
1142 : :
1143 : : const unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
1144 : : int err_nodata,
1145 : : const unsigned char **endpp,
1146 : : Dwarf_Off *offsetp)
1147 : : internal_function;
1148 : :
1149 : : /* Fills in the given attribute to point at an empty location expression. */
1150 : : void __libdw_empty_loc_attr (Dwarf_Attribute *attr)
1151 : : internal_function;
1152 : :
1153 : : /* Load .debug_line unit at DEBUG_LINE_OFFSET. COMP_DIR is a value of
1154 : : DW_AT_comp_dir or NULL if that attribute is not available. Caches
1155 : : the loaded unit and optionally set *LINESP and/or *FILESP (if not
1156 : : NULL) with loaded information. Returns 0 for success or a negative
1157 : : value for failure. */
1158 : : int __libdw_getsrclines (Dwarf *dbg, Dwarf_Off debug_line_offset,
1159 : : const char *comp_dir, unsigned address_size,
1160 : : Dwarf_Lines **linesp, Dwarf_Files **filesp)
1161 : : internal_function
1162 : : __nonnull_attribute__ (1);
1163 : :
1164 : : /* Load .debug_line unit at DEBUG_LINE_OFFSET. COMP_DIR is a value of
1165 : : DW_AT_comp_dir or NULL if that attribute is not available. Caches
1166 : : the loaded unit and set *FILESP with loaded information. Returns 0
1167 : : for success or a negative value for failure. */
1168 : : int __libdw_getsrcfiles (Dwarf *dbg, Dwarf_Off debug_line_offset,
1169 : : const char *comp_dir, unsigned address_size,
1170 : : Dwarf_Files **filesp)
1171 : : internal_function
1172 : : __nonnull_attribute__ (1);
1173 : :
1174 : : /* Load and return value of DW_AT_comp_dir from CUDIE. */
1175 : : const char *__libdw_getcompdir (Dwarf_Die *cudie);
1176 : :
1177 : : /* Get the base address for the CU, fetches it when not yet set.
1178 : : This is used as initial base address for ranges and loclists. */
1179 : : Dwarf_Addr __libdw_cu_base_address (Dwarf_CU *cu);
1180 : :
1181 : : /* Get the address base for the CU, fetches it when not yet set. */
1182 : : static inline Dwarf_Off
1183 : 1994 : __libdw_cu_addr_base (Dwarf_CU *cu)
1184 : : {
1185 [ + + ]: 1994 : if (cu->addr_base == (Dwarf_Off) -1)
1186 : : {
1187 : 208 : Dwarf_Die cu_die = CUDIE(cu);
1188 : 208 : Dwarf_Attribute attr;
1189 : 208 : Dwarf_Off offset = 0;
1190 [ + + ]: 208 : if (dwarf_attr (&cu_die, DW_AT_GNU_addr_base, &attr) != NULL
1191 [ + + ]: 110 : || dwarf_attr (&cu_die, DW_AT_addr_base, &attr) != NULL)
1192 : : {
1193 : 200 : Dwarf_Word off;
1194 [ + - ]: 200 : if (dwarf_formudata (&attr, &off) == 0)
1195 : 200 : offset = off;
1196 : : }
1197 : 208 : cu->addr_base = offset;
1198 : : }
1199 : :
1200 : 1994 : return cu->addr_base;
1201 : : }
1202 : :
1203 : : /* Gets the .debug_str_offsets base offset to use. static inline to
1204 : : be shared between libdw and eu-readelf. */
1205 : : static inline Dwarf_Off
1206 : 25664 : str_offsets_base_off (Dwarf *dbg, Dwarf_CU *cu)
1207 : : {
1208 : : /* If we don't have a CU, then find and use the first one in the
1209 : : debug file (when we support .dwp files, we must actually find the
1210 : : one matching our "caller" - aka macro or line). If we (now) have
1211 : : a cu and str_offsets_base attribute, just use that. Otherwise
1212 : : use the first offset. But we might have to parse the header
1213 : : first, but only if this is version 5. Assume if all else fails,
1214 : : this is version 4, without header. */
1215 : :
1216 [ + + ]: 25664 : if (cu == NULL && dbg != NULL)
1217 : : {
1218 : 9060 : Dwarf_CU *first_cu;
1219 [ + + ]: 9060 : if (dwarf_get_units (dbg, NULL, &first_cu, NULL, NULL, NULL, NULL) == 0)
1220 : 9058 : cu = first_cu;
1221 : : }
1222 : :
1223 : 25664 : Dwarf_Off off = 0;
1224 [ + + ]: 25664 : if (cu != NULL)
1225 : : {
1226 : 25662 : mutex_lock (cu->str_off_base_lock);
1227 [ + + ]: 25662 : if (cu->str_off_base == (Dwarf_Off) -1)
1228 : : {
1229 : 4286 : Dwarf_Off dwp_offset;
1230 [ + - ]: 4286 : if (dwarf_cu_dwp_section_info (cu, DW_SECT_STR_OFFSETS, &dwp_offset,
1231 : : NULL) == 0)
1232 : 4286 : off = dwp_offset;
1233 : 4286 : Dwarf_Die cu_die = CUDIE(cu);
1234 : 4286 : Dwarf_Attribute attr;
1235 [ + + ]: 4286 : if (dwarf_attr (&cu_die, DW_AT_str_offsets_base, &attr) != NULL)
1236 : : {
1237 : 22 : Dwarf_Word base;
1238 [ + - ]: 22 : if (dwarf_formudata (&attr, &base) == 0)
1239 : : {
1240 : 22 : cu->str_off_base = off + base;
1241 : 22 : mutex_unlock (cu->str_off_base_lock);
1242 : 22 : return cu->str_off_base;
1243 : : }
1244 : : }
1245 : : /* For older DWARF simply assume zero (no header). */
1246 [ + + ]: 4264 : if (cu->version < 5)
1247 : : {
1248 : 176 : cu->str_off_base = off;
1249 : 176 : mutex_unlock (cu->str_off_base_lock);
1250 : 176 : return cu->str_off_base;
1251 : : }
1252 : :
1253 [ + + ]: 4088 : if (dbg == NULL)
1254 : 80 : dbg = cu->dbg;
1255 : : }
1256 : : else
1257 : : {
1258 : : mutex_unlock (cu->str_off_base_lock);
1259 : : return cu->str_off_base;
1260 : : }
1261 : : }
1262 : :
1263 : : /* No str_offsets_base attribute, we have to assume "zero".
1264 : : But there could be a header first. */
1265 [ - + ]: 4090 : if (dbg == NULL)
1266 : 0 : goto no_header;
1267 : :
1268 : 4090 : Elf_Data *data = dbg->sectiondata[IDX_debug_str_offsets];
1269 [ + + ]: 4090 : if (data == NULL)
1270 : 4010 : goto no_header;
1271 : :
1272 : 80 : const unsigned char *start;
1273 : 80 : const unsigned char *readp;
1274 : 80 : const unsigned char *readendp;
1275 : 80 : start = readp = (const unsigned char *) data->d_buf;
1276 : 80 : readendp = (const unsigned char *) data->d_buf + data->d_size;
1277 : :
1278 : 80 : uint64_t unit_length;
1279 : 80 : uint16_t version;
1280 : :
1281 [ - + ]: 80 : if (unlikely (readendp - readp < 4))
1282 : 0 : goto no_header;
1283 [ - + ]: 80 : unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1284 [ - + ]: 80 : if (unlikely (unit_length == 0xffffffff))
1285 : : {
1286 [ # # ]: 0 : if (unlikely (readendp - readp < 8))
1287 : 0 : goto no_header;
1288 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1289 : : /* In theory the offset size could be different
1290 : : between CU and str_offsets unit. But we just
1291 : : ignore that here. */
1292 : : }
1293 : :
1294 : : /* We need at least 2-bytes (version) + 2-bytes (padding) =
1295 : : 4 bytes to complete the header. And this unit cannot go
1296 : : beyond the section data. */
1297 [ + - ]: 80 : if (readendp - readp < 4
1298 [ + - ]: 80 : || unit_length < 4
1299 [ - + ]: 80 : || (uint64_t) (readendp - readp) < unit_length)
1300 : 0 : goto no_header;
1301 : :
1302 [ - + ]: 80 : version = read_2ubyte_unaligned_inc (dbg, readp);
1303 [ - + ]: 80 : if (version != 5)
1304 : 0 : goto no_header;
1305 : : /* padding */
1306 : 80 : read_2ubyte_unaligned_inc (dbg, readp);
1307 : :
1308 : 80 : off += (Dwarf_Off) (readp - start);
1309 : :
1310 : 4090 : no_header:
1311 [ + + ]: 4090 : if (cu != NULL)
1312 : : {
1313 : 4088 : cu->str_off_base = off;
1314 : : mutex_unlock (cu->str_off_base_lock);
1315 : : }
1316 : :
1317 : : return off;
1318 : : }
1319 : :
1320 : :
1321 : : /* Get the string offsets base for the CU, fetches it when not yet set. */
1322 : 3436 : static inline Dwarf_Off __libdw_cu_str_off_base (Dwarf_CU *cu)
1323 : : {
1324 : 3436 : return str_offsets_base_off (NULL, cu);
1325 : : }
1326 : :
1327 : :
1328 : : /* Either a direct offset into .debug_ranges for version < 5, or the
1329 : : start of the offset table in .debug_rnglists for version > 5. */
1330 : : static inline Dwarf_Off
1331 : 90 : __libdw_cu_ranges_base (Dwarf_CU *cu)
1332 : : {
1333 [ + + ]: 90 : if (cu->ranges_base == (Dwarf_Off) -1)
1334 : : {
1335 : 18 : Dwarf_Off offset = 0;
1336 : 18 : Dwarf_Die cu_die = CUDIE(cu);
1337 : 18 : Dwarf_Attribute attr;
1338 [ + + ]: 18 : if (cu->version < 5)
1339 : : {
1340 [ + - ]: 12 : if (dwarf_attr (&cu_die, DW_AT_GNU_ranges_base, &attr) != NULL)
1341 : : {
1342 : 12 : Dwarf_Word off;
1343 [ + - ]: 12 : if (dwarf_formudata (&attr, &off) == 0)
1344 : 12 : offset = off;
1345 : : }
1346 : : }
1347 : : else
1348 : : {
1349 : 6 : Dwarf_Off dwp_offset;
1350 [ + - ]: 6 : if (dwarf_cu_dwp_section_info (cu, DW_SECT_RNGLISTS, &dwp_offset,
1351 : : NULL) == 0)
1352 : 6 : offset = dwp_offset;
1353 : :
1354 [ + + ]: 6 : if (dwarf_attr (&cu_die, DW_AT_rnglists_base, &attr) != NULL)
1355 : : {
1356 : 2 : Dwarf_Word off;
1357 [ + - ]: 2 : if (dwarf_formudata (&attr, &off) == 0)
1358 : 2 : offset += off;
1359 : : }
1360 : :
1361 : : /* There wasn't an rnglists_base, if the Dwarf does have a
1362 : : .debug_rnglists section, then it might be we need the
1363 : : base after the first header. */
1364 : 6 : Elf_Data *data = cu->dbg->sectiondata[IDX_debug_rnglists];
1365 [ + + + - ]: 6 : if (offset == dwp_offset && data != NULL)
1366 : : {
1367 : 4 : Dwarf *dbg = cu->dbg;
1368 : 4 : const unsigned char *readp = data->d_buf;
1369 : 4 : const unsigned char *const dataend
1370 : 4 : = (unsigned char *) data->d_buf + data->d_size;
1371 : :
1372 [ - + ]: 4 : if (unlikely (readp > dataend - 4))
1373 : 0 : goto no_header;
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 : if (unlikely (readp > dataend - 4))
1458 : 0 : goto no_header;
1459 [ - + ]: 8 : uint64_t unit_length = read_4ubyte_unaligned_inc (dbg, readp);
1460 : 8 : unsigned int offset_size = 4;
1461 [ - + ]: 8 : if (unlikely (unit_length == 0xffffffff))
1462 : : {
1463 [ # # ]: 0 : if (unlikely (readp > dataend - 8))
1464 : 0 : goto no_header;
1465 : :
1466 [ # # ]: 0 : unit_length = read_8ubyte_unaligned_inc (dbg, readp);
1467 : 0 : offset_size = 8;
1468 : : }
1469 : :
1470 [ + - ]: 8 : if (readp > dataend - 8
1471 [ + - ]: 8 : || unit_length < 8
1472 [ - + ]: 8 : || unit_length > (uint64_t) (dataend - readp))
1473 : 0 : goto no_header;
1474 : :
1475 [ - + ]: 8 : uint16_t version = read_2ubyte_unaligned_inc (dbg, readp);
1476 [ - + ]: 8 : if (version != 5)
1477 : 0 : goto no_header;
1478 : :
1479 : 8 : uint8_t address_size = *readp++;
1480 [ - + ]: 8 : if (address_size != 4 && address_size != 8)
1481 : 0 : goto no_header;
1482 : :
1483 : 8 : uint8_t segment_size = *readp++;
1484 [ - + ]: 8 : if (segment_size != 0)
1485 : 0 : goto no_header;
1486 : :
1487 : 8 : uint32_t offset_entry_count;
1488 [ - + ]: 8 : offset_entry_count = read_4ubyte_unaligned_inc (dbg, readp);
1489 : :
1490 : 8 : const unsigned char *offset_array_start = readp;
1491 [ - + ]: 8 : if (offset_entry_count <= 0)
1492 : 0 : goto no_header;
1493 : :
1494 : 8 : uint64_t needed = offset_entry_count * offset_size;
1495 [ - + ]: 8 : if (unit_length - 8 < needed)
1496 : 0 : goto no_header;
1497 : :
1498 : 8 : offset += (Dwarf_Off) (offset_array_start
1499 : 8 : - (unsigned char *) data->d_buf);
1500 : : }
1501 : :
1502 : 0 : no_header:
1503 : 8 : cu->locs_base = offset;
1504 : : }
1505 : :
1506 : 218 : return cu->locs_base;
1507 : : }
1508 : :
1509 : : /* Helper function for tsearch/tfind split_tree Dwarf. */
1510 : : int __libdw_finddbg_cb (const void *arg1, const void *arg2);
1511 : :
1512 : : /* Link skeleton and split compile units. */
1513 : : static inline void
1514 : 196 : __libdw_link_skel_split (Dwarf_CU *skel, Dwarf_CU *split)
1515 : : {
1516 : 196 : skel->split = split;
1517 : 196 : split->split = skel;
1518 : :
1519 : : /* Get .debug_addr and addr_base greedy.
1520 : : We also need it for the fake addr cu.
1521 : : This needs to be done for each split unit (one per .dwo file, or multiple
1522 : : per .dwp file). */
1523 : 196 : Dwarf *dbg = skel->dbg;
1524 : 196 : Dwarf *sdbg = split->dbg;
1525 [ + - ]: 196 : if (dbg->sectiondata[IDX_debug_addr] != NULL
1526 : : /* If this split file hasn't been linked yet... */
1527 [ + + ]: 196 : && (sdbg->sectiondata[IDX_debug_addr] == NULL
1528 : : /* ... or it was linked to the same skeleton file for another
1529 : : unit... */
1530 [ + - ]: 60 : || (sdbg->sectiondata[IDX_debug_addr]
1531 : : == dbg->sectiondata[IDX_debug_addr])))
1532 : : {
1533 : : /* ... then link the address information for this file and unit. */
1534 : 196 : sdbg->sectiondata[IDX_debug_addr]
1535 : 196 : = dbg->sectiondata[IDX_debug_addr];
1536 : 196 : split->addr_base = __libdw_cu_addr_base (skel);
1537 : 196 : sdbg->fake_addr_cu = dbg->fake_addr_cu;
1538 : : }
1539 : 196 : }
1540 : :
1541 : :
1542 : : /* Given an address index for a CU return the address.
1543 : : Returns -1 and sets libdw_errno if an error occurs. */
1544 : : int __libdw_addrx (Dwarf_CU *cu, Dwarf_Word idx, Dwarf_Addr *addr);
1545 : :
1546 : :
1547 : : /* Helper function to set elfpath field in Dwarf, used from dwarf_begin_elf
1548 : : and libdwfl process_file. */
1549 : : char * __libdw_elfpath (int fd);
1550 : :
1551 : : /* Helper function to set debugdir field in Dwarf after elfpath field has been
1552 : : set. */
1553 : : void __libdw_set_debugdir (Dwarf *dbg);
1554 : :
1555 : :
1556 : : /* Given the directory of a debug file, an absolute or relative dir
1557 : : to look in, and file returns a full path.
1558 : :
1559 : : If the file is absolute (starts with a /) a copy of file is returned.
1560 : : the file isn't absolute, but dir is absolute, then a path that is
1561 : : the concatenation of dir and file is returned. If neither file,
1562 : : nor dir is absolute, the path will be constructed using dir (if not
1563 : : NULL) and file relative to the debugdir (if valid).
1564 : :
1565 : : The debugdir and the dir may be NULL (in which case they aren't used).
1566 : : If file is NULL, or no full path can be constructed NULL is returned.
1567 : :
1568 : : The caller is responsible for freeing the result if not NULL. */
1569 : : char * __libdw_filepath (const char *debugdir, const char *dir,
1570 : : const char *file)
1571 : : internal_function;
1572 : :
1573 : : /* Like dwarf_getaranges but aranges are generated from CU address
1574 : : ranges instead of being read from .debug_aranges.
1575 : :
1576 : : Returns 0 if successful and updates ARANGES and NARANGES. Otherwise
1577 : : returns -1 and sets libdw_errno.
1578 : : */
1579 : : int __libdw_getdieranges (Dwarf *dbg, Dwarf_Aranges **aranges, size_t *naranges);
1580 : : #endif /* libdwP.h */
|