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