Branch data Line data Source code
1 : : /* Internal definitions for libdwfl.
2 : : Copyright (C) 2005-2015, 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 _LIBDWFLP_H
30 : : #define _LIBDWFLP_H 1
31 : :
32 : : #include <libdwfl.h>
33 : : #include <libebl.h>
34 : : #include <assert.h>
35 : : #include <dirent.h>
36 : : #include <errno.h>
37 : : #include <stdbool.h>
38 : : #include <stdlib.h>
39 : : #include <string.h>
40 : :
41 : : #include "libdwP.h" /* We need its INTDECLs. */
42 : : #include "libdwelfP.h"
43 : : #include "eu-search.h"
44 : :
45 : : typedef struct Dwfl_Process Dwfl_Process;
46 : :
47 : : #define DWFL_ERRORS \
48 : : DWFL_ERROR (NOERROR, N_("no error")) \
49 : : DWFL_ERROR (UNKNOWN_ERROR, N_("unknown error")) \
50 : : DWFL_ERROR (NOMEM, N_("out of memory")) \
51 : : DWFL_ERROR (ERRNO, N_("See errno")) \
52 : : DWFL_ERROR (LIBELF, N_("See elf_errno")) \
53 : : DWFL_ERROR (LIBDW, N_("See dwarf_errno")) \
54 : : DWFL_ERROR (LIBEBL, N_("See ebl_errno (XXX missing)")) \
55 : : DWFL_ERROR (ZLIB, N_("gzip decompression failed")) \
56 : : DWFL_ERROR (BZLIB, N_("bzip2 decompression failed")) \
57 : : DWFL_ERROR (LZMA, N_("LZMA decompression failed")) \
58 : : DWFL_ERROR (ZSTD, N_("zstd decompression failed")) \
59 : : DWFL_ERROR (UNKNOWN_MACHINE, N_("no support library found for machine")) \
60 : : DWFL_ERROR (NOREL, N_("Callbacks missing for ET_REL file")) \
61 : : DWFL_ERROR (BADRELTYPE, N_("Unsupported relocation type")) \
62 : : DWFL_ERROR (BADRELOFF, N_("r_offset is bogus")) \
63 : : DWFL_ERROR (BADSTROFF, N_("offset out of range")) \
64 : : DWFL_ERROR (RELUNDEF, N_("relocation refers to undefined symbol")) \
65 : : DWFL_ERROR (CB, N_("Callback returned failure")) \
66 : : DWFL_ERROR (NO_DWARF, N_("No DWARF information found")) \
67 : : DWFL_ERROR (NO_SYMTAB, N_("No symbol table found")) \
68 : : DWFL_ERROR (NO_PHDR, N_("No ELF program headers")) \
69 : : DWFL_ERROR (OVERLAP, N_("address range overlaps an existing module")) \
70 : : DWFL_ERROR (ADDR_OUTOFRANGE, N_("address out of range")) \
71 : : DWFL_ERROR (NO_MATCH, N_("no matching address range")) \
72 : : DWFL_ERROR (TRUNCATED, N_("image truncated")) \
73 : : DWFL_ERROR (ALREADY_ELF, N_("ELF file opened")) \
74 : : DWFL_ERROR (BADELF, N_("not a valid ELF file")) \
75 : : DWFL_ERROR (WEIRD_TYPE, N_("cannot handle DWARF type description")) \
76 : : DWFL_ERROR (WRONG_ID_ELF, N_("ELF file does not match build ID")) \
77 : : DWFL_ERROR (BAD_PRELINK, N_("corrupt .gnu.prelink_undo section data")) \
78 : : DWFL_ERROR (LIBEBL_BAD, N_("Internal error due to ebl")) \
79 : : DWFL_ERROR (CORE_MISSING, N_("Missing data in core file")) \
80 : : DWFL_ERROR (INVALID_REGISTER, N_("Invalid register")) \
81 : : DWFL_ERROR (REGISTER_VAL_UNKNOWN, N_("Unknown register value")) \
82 : : DWFL_ERROR (PROCESS_MEMORY_READ, N_("Error reading process memory")) \
83 : : DWFL_ERROR (PROCESS_NO_ARCH, N_("Couldn't find architecture of any ELF")) \
84 : : DWFL_ERROR (PARSE_PROC, N_("Error parsing /proc filesystem")) \
85 : : DWFL_ERROR (INVALID_DWARF, N_("Invalid DWARF")) \
86 : : DWFL_ERROR (UNSUPPORTED_DWARF, N_("Unsupported DWARF")) \
87 : : DWFL_ERROR (NEXT_THREAD_FAIL, N_("Unable to find more threads")) \
88 : : DWFL_ERROR (ATTACH_STATE_CONFLICT, N_("Dwfl already has attached state")) \
89 : : DWFL_ERROR (NO_ATTACH_STATE, N_("Dwfl has no attached state")) \
90 : : DWFL_ERROR (NO_UNWIND, N_("Unwinding not supported for this architecture")) \
91 : : DWFL_ERROR (INVALID_ARGUMENT, N_("Invalid argument")) \
92 : : DWFL_ERROR (NO_CORE_FILE, N_("Not an ET_CORE ELF file"))
93 : :
94 : : #define DWFL_ERROR(name, text) DWFL_E_##name,
95 : : typedef enum { DWFL_ERRORS DWFL_E_NUM } Dwfl_Error;
96 : : #undef DWFL_ERROR
97 : :
98 : : #define OTHER_ERROR(name) ((unsigned int) DWFL_E_##name << 16)
99 : : #define DWFL_E(name, errno) (OTHER_ERROR (name) | (errno))
100 : :
101 : : extern int __libdwfl_canon_error (Dwfl_Error) internal_function;
102 : : extern void __libdwfl_seterrno (Dwfl_Error) internal_function;
103 : :
104 : : /* Resources we might keep for the user about the core file that the
105 : : Dwfl might have been created from. Can currently only be set
106 : : through std-argp. */
107 : : struct Dwfl_User_Core
108 : : {
109 : : char *executable_for_core; /* --executable if --core was specified. */
110 : : Elf *core; /* non-NULL if we need to free it. */
111 : : int fd; /* close if >= 0. */
112 : : };
113 : :
114 : : struct Dwfl
115 : : {
116 : : const Dwfl_Callbacks *callbacks;
117 : : #ifdef ENABLE_LIBDEBUGINFOD
118 : : debuginfod_client *debuginfod;
119 : : #endif
120 : : Dwfl_Module *modulelist; /* List in order used by full traversals. */
121 : :
122 : : Dwfl_Process *process;
123 : : Dwfl_Error attacherr; /* Previous error attaching process. */
124 : :
125 : : GElf_Addr offline_next_address;
126 : :
127 : : GElf_Addr segment_align; /* Smallest granularity of segments. */
128 : :
129 : : /* Binary search table in three parallel malloc'd arrays. */
130 : : size_t lookup_elts; /* Elements in use. */
131 : : size_t lookup_alloc; /* Elements allococated. */
132 : : GElf_Addr *lookup_addr; /* Start address of segment. */
133 : : Dwfl_Module **lookup_module; /* Module associated with segment, or null. */
134 : : int *lookup_segndx; /* User segment index, or -1. */
135 : : int next_segndx;
136 : :
137 : : struct Dwfl_User_Core *user_core;
138 : : char *sysroot; /* sysroot, or NULL to search standard system
139 : : paths */
140 : : };
141 : :
142 : : #define OFFLINE_REDZONE 0x10000
143 : :
144 : : struct dwfl_file
145 : : {
146 : : char *name;
147 : : int fd;
148 : : bool valid; /* The build ID note has been matched. */
149 : : bool relocated; /* Partial relocation of all sections done. */
150 : :
151 : : Elf *elf;
152 : :
153 : : /* This is the lowest p_vaddr in this ELF file, aligned to p_align.
154 : : For a file without phdrs, this is zero. */
155 : : GElf_Addr vaddr;
156 : :
157 : : /* This is an address chosen for synchronization between the main file
158 : : and the debug file. See dwfl_module_getdwarf.c for how it's chosen. */
159 : : GElf_Addr address_sync;
160 : : };
161 : :
162 : : struct Dwfl_Module
163 : : {
164 : : Dwfl *dwfl;
165 : : struct Dwfl_Module *next; /* Link on Dwfl.modulelist. */
166 : :
167 : : void *userdata;
168 : :
169 : : char *name; /* Iterator name for this module. */
170 : : GElf_Addr low_addr, high_addr;
171 : :
172 : : struct dwfl_file main, debug, aux_sym;
173 : : GElf_Addr main_bias;
174 : : Ebl *ebl;
175 : : GElf_Half e_type; /* GElf_Ehdr.e_type cache. */
176 : : Dwfl_Error elferr; /* Previous failure to open main file. */
177 : :
178 : : struct dwfl_relocation *reloc_info; /* Relocatable sections. */
179 : :
180 : : struct dwfl_file *symfile; /* Either main or debug. */
181 : : Elf_Data *symdata; /* Data in the ELF symbol table section. */
182 : : Elf_Data *aux_symdata; /* Data in the auxiliary ELF symbol table. */
183 : : size_t syments; /* sh_size / sh_entsize of that section. */
184 : : size_t aux_syments; /* sh_size / sh_entsize of aux_sym section. */
185 : : int first_global; /* Index of first global symbol of table. */
186 : : int aux_first_global; /* Index of first global of aux_sym table. */
187 : : Elf_Data *symstrdata; /* Data for its string table. */
188 : : Elf_Data *aux_symstrdata; /* Data for aux_sym string table. */
189 : : Elf_Data *symxndxdata; /* Data in the extended section index table. */
190 : : Elf_Data *aux_symxndxdata; /* Data in the extended auxiliary table. */
191 : :
192 : : char *elfpath; /* The path where we found the main Elf. */
193 : :
194 : : Dwarf *dw; /* libdw handle for its debugging info. */
195 : : Dwarf *alt; /* Dwarf used for dwarf_setalt, or NULL. */
196 : : int alt_fd; /* descriptor, only valid when alt != NULL. */
197 : : Elf *alt_elf; /* Elf for alt Dwarf. */
198 : :
199 : : Dwfl_Error symerr; /* Previous failure to load symbols. */
200 : : Dwfl_Error dwerr; /* Previous failure to load DWARF. */
201 : :
202 : : /* Known CU's in this module. */
203 : : struct dwfl_cu *first_cu, **cu;
204 : :
205 : : search_tree lazy_cu_tree; /* Table indexed by Dwarf_Off of CU. */
206 : :
207 : : struct dwfl_arange *aranges; /* Mapping of addresses in module to CUs. */
208 : :
209 : : void *build_id_bits; /* malloc'd copy of build ID bits. */
210 : : GElf_Addr build_id_vaddr; /* Address where they reside, 0 if unknown. */
211 : : int build_id_len; /* -1 for prior failure, 0 if unset. */
212 : :
213 : : unsigned int ncu;
214 : : unsigned int lazycu; /* Possible users, deleted when none left. */
215 : : unsigned int naranges;
216 : :
217 : : Dwarf_CFI *dwarf_cfi; /* Cached DWARF CFI for this module. */
218 : : Dwarf_CFI *eh_cfi; /* Cached EH CFI for this module. */
219 : :
220 : : int segment; /* Index of first segment table entry. */
221 : : bool gc; /* Mark/sweep flag. */
222 : : bool is_executable; /* Use Dwfl::executable_for_core? */
223 : : };
224 : :
225 : : /* This holds information common for all the threads/tasks/TIDs of one process
226 : : for backtraces. */
227 : :
228 : : struct Dwfl_Process
229 : : {
230 : : struct Dwfl *dwfl;
231 : : pid_t pid;
232 : : const Dwfl_Thread_Callbacks *callbacks;
233 : : void *callbacks_arg;
234 : : struct ebl *ebl;
235 : : bool ebl_close:1;
236 : : };
237 : :
238 : : /* See its typedef in libdwfl.h. */
239 : :
240 : : struct Dwfl_Thread
241 : : {
242 : : Dwfl_Process *process;
243 : : pid_t tid;
244 : : /* Bottom (innermost) frame while we're initializing, NULL afterwards. */
245 : : Dwfl_Frame *unwound;
246 : : void *callbacks_arg;
247 : :
248 : : /* Data for handling AARCH64 (currently limited to demangling PAC from
249 : : return addresses). */
250 : : struct {
251 : : Dwarf_Addr pauth_insn_mask;
252 : : } aarch64;
253 : : };
254 : :
255 : : /* See its typedef in libdwfl.h. */
256 : :
257 : : struct Dwfl_Frame
258 : : {
259 : : Dwfl_Thread *thread;
260 : : /* Previous (outer) frame. */
261 : : Dwfl_Frame *unwound;
262 : : bool signal_frame : 1;
263 : : bool initial_frame : 1;
264 : : enum
265 : : {
266 : : /* This structure is still being initialized or there was an error
267 : : initializing it. */
268 : : DWFL_FRAME_STATE_ERROR,
269 : : /* PC field is valid. */
270 : : DWFL_FRAME_STATE_PC_SET,
271 : : /* PC field is undefined, this means the next (inner) frame was the
272 : : outermost frame. */
273 : : DWFL_FRAME_STATE_PC_UNDEFINED
274 : : } pc_state;
275 : : /* Either initialized from appropriate REGS element or on some archs
276 : : initialized separately as the return address has no DWARF register. */
277 : : Dwarf_Addr pc;
278 : : /* (1 << X) bitmask where 0 <= X < ebl_frame_nregs. */
279 : : uint64_t regs_set[3];
280 : : /* REGS array size is ebl_frame_nregs.
281 : : REGS_SET tells which of the REGS are valid. */
282 : : Dwarf_Addr regs[];
283 : : };
284 : :
285 : : /* Fetch value from Dwfl_Frame->regs indexed by DWARF REGNO. The
286 : : function returns 0 on success, -1 on error (invalid DWARF register
287 : : number), 1 if the value of the register in the frame is unknown.
288 : : Even on error, no error code is set. */
289 : : int __libdwfl_frame_reg_get (Dwfl_Frame *state, unsigned regno,
290 : : Dwarf_Addr *val)
291 : : internal_function;
292 : :
293 : : /* Store value to Dwfl_Frame->regs indexed by DWARF REGNO.
294 : : No error code is set if the function returns FALSE. */
295 : : bool __libdwfl_frame_reg_set (Dwfl_Frame *state, unsigned regno,
296 : : Dwarf_Addr val)
297 : : internal_function;
298 : :
299 : : /* Information cached about each CU in Dwfl_Module.dw. */
300 : : struct dwfl_cu
301 : : {
302 : : /* This caches libdw information about the CU. It's also the
303 : : address passed back to users, so we take advantage of the
304 : : fact that it's placed first to cast back. */
305 : : Dwarf_Die die;
306 : :
307 : : Dwfl_Module *mod; /* Pointer back to containing module. */
308 : :
309 : : struct dwfl_cu *next; /* CU immediately following in the file. */
310 : :
311 : : struct Dwfl_Lines *lines;
312 : : };
313 : :
314 : : struct Dwfl_Lines
315 : : {
316 : : struct dwfl_cu *cu;
317 : :
318 : : /* This is what the opaque Dwfl_Line * pointers we pass to users are.
319 : : We need to recover pointers to our struct dwfl_cu and a record in
320 : : libdw's Dwarf_Line table. To minimize the memory used in addition
321 : : to libdw's Dwarf_Lines buffer, we just point to our own index in
322 : : this table, and have one pointer back to the CU. The indices here
323 : : match those in libdw's Dwarf_CU.lines->info table. */
324 : : struct Dwfl_Line
325 : : {
326 : : unsigned int idx; /* My index in the dwfl_cu.lines table. */
327 : : } idx[0];
328 : : };
329 : :
330 : : static inline struct dwfl_cu *
331 : 2498044 : dwfl_linecu_inline (const Dwfl_Line *line)
332 : : {
333 [ + + - - : 2496782 : const struct Dwfl_Lines *lines = ((const void *) line
- - + + +
+ + - ]
334 : 2498044 : - offsetof (struct Dwfl_Lines,
335 : : idx[line->idx]));
336 [ + + ]: 2496782 : return lines->cu;
337 : : }
338 : : #define dwfl_linecu dwfl_linecu_inline
339 : :
340 : : static inline GElf_Addr
341 : 34334126 : dwfl_adjusted_address (Dwfl_Module *mod, GElf_Addr addr)
342 : : {
343 [ # # ]: 34334126 : return addr + mod->main_bias;
344 : : }
345 : :
346 : : static inline GElf_Addr
347 : 200952 : dwfl_deadjust_address (Dwfl_Module *mod, GElf_Addr addr)
348 : : {
349 : 200952 : return addr - mod->main_bias;
350 : : }
351 : :
352 : : static inline Dwarf_Addr
353 : 1680554 : dwfl_adjusted_dwarf_addr (Dwfl_Module *mod, Dwarf_Addr addr)
354 : : {
355 : 1680554 : return dwfl_adjusted_address (mod, (addr
356 : 1680554 : - mod->debug.address_sync
357 : 1680554 : + mod->main.address_sync));
358 : : }
359 : :
360 : : static inline Dwarf_Addr
361 : 900 : dwfl_deadjust_dwarf_addr (Dwfl_Module *mod, Dwarf_Addr addr)
362 : : {
363 : 900 : return (dwfl_deadjust_address (mod, addr)
364 : 900 : - mod->main.address_sync
365 : 900 : + mod->debug.address_sync);
366 : : }
367 : :
368 : : static inline Dwarf_Addr
369 : 44470 : dwfl_adjusted_aux_sym_addr (Dwfl_Module *mod, Dwarf_Addr addr)
370 : : {
371 : 44470 : return dwfl_adjusted_address (mod, (addr
372 : 44470 : - mod->aux_sym.address_sync
373 : 44470 : + mod->main.address_sync));
374 : : }
375 : :
376 : : static inline Dwarf_Addr
377 : 0 : dwfl_deadjust_aux_sym_addr (Dwfl_Module *mod, Dwarf_Addr addr)
378 : : {
379 : 0 : return (dwfl_deadjust_address (mod, addr)
380 : 0 : - mod->main.address_sync
381 : 0 : + mod->aux_sym.address_sync);
382 : : }
383 : :
384 : : static inline GElf_Addr
385 : 32960154 : dwfl_adjusted_st_value (Dwfl_Module *mod, Elf *symelf, GElf_Addr addr)
386 : : {
387 [ + + ]: 32960154 : if (symelf == mod->main.elf)
388 : 32523916 : return dwfl_adjusted_address (mod, addr);
389 [ + + ]: 436238 : if (symelf == mod->debug.elf)
390 : 391768 : return dwfl_adjusted_dwarf_addr (mod, addr);
391 : 44470 : return dwfl_adjusted_aux_sym_addr (mod, addr);
392 : : }
393 : :
394 : : static inline GElf_Addr
395 : 200052 : dwfl_deadjust_st_value (Dwfl_Module *mod, Elf *symelf, GElf_Addr addr)
396 : : {
397 [ + - ]: 200052 : if (symelf == mod->main.elf)
398 : 200052 : return dwfl_deadjust_address (mod, addr);
399 [ # # ]: 0 : if (symelf == mod->debug.elf)
400 : 0 : return dwfl_deadjust_dwarf_addr (mod, addr);
401 : 0 : return dwfl_deadjust_aux_sym_addr (mod, addr);
402 : : }
403 : :
404 : : /* This describes a contiguous address range that lies in a single CU.
405 : : We condense runs of Dwarf_Arange entries for the same CU into this. */
406 : : struct dwfl_arange
407 : : {
408 : : struct dwfl_cu *cu;
409 : : size_t arange; /* Index in Dwarf_Aranges. */
410 : : };
411 : :
412 : : #define __LIBDWFL_REMOTE_MEM_CACHE_SIZE 4096
413 : : /* Structure for caching remote memory reads as used by __libdwfl_pid_arg. */
414 : : struct __libdwfl_remote_mem_cache
415 : : {
416 : : Dwarf_Addr addr; /* Remote address. */
417 : : Dwarf_Off len; /* Zero if cleared, otherwise likely 4K. */
418 : : unsigned char buf[__LIBDWFL_REMOTE_MEM_CACHE_SIZE]; /* The actual cache. */
419 : : };
420 : :
421 : : /* Structure used for keeping track of ptrace attaching a thread.
422 : : Shared by linux-pid-attach and linux-proc-maps. If it has been setup
423 : : then get the instance through __libdwfl_get_pid_arg. */
424 : : struct __libdwfl_pid_arg
425 : : {
426 : : /* /proc/PID/task/. */
427 : : DIR *dir;
428 : : /* Elf for /proc/PID/exe. Set to NULL if it couldn't be opened. */
429 : : Elf *elf;
430 : : /* Remote memory cache, NULL if there is no memory cached.
431 : : Should be cleared on detachment (because that makes the thread
432 : : runnable and the cache invalid). */
433 : : struct __libdwfl_remote_mem_cache *mem_cache;
434 : : /* fd for /proc/PID/exe. Set to -1 if it couldn't be opened. */
435 : : int elf_fd;
436 : : /* It is 0 if not used. */
437 : : pid_t tid_attached;
438 : : /* Valid only if TID_ATTACHED is not zero. */
439 : : bool tid_was_stopped;
440 : : /* True if threads are ptrace stopped by caller. */
441 : : bool assume_ptrace_stopped;
442 : : };
443 : :
444 : : /* If DWfl is not NULL and a Dwfl_Process has been setup that has
445 : : Dwfl_Thread_Callbacks set to pid_thread_callbacks, then return the
446 : : callbacks_arg, which will be a struct __libdwfl_pid_arg. Otherwise
447 : : returns NULL. */
448 : : extern struct __libdwfl_pid_arg *__libdwfl_get_pid_arg (Dwfl *dwfl)
449 : : internal_function;
450 : :
451 : : /* Makes sure the given tid is attached. On success returns true and
452 : : sets tid_was_stopped. */
453 : : extern bool __libdwfl_ptrace_attach (pid_t tid, bool *tid_was_stoppedp)
454 : : internal_function;
455 : :
456 : : /* Detaches a tid that was attached through
457 : : __libdwfl_ptrace_attach. Must be given the tid_was_stopped as set
458 : : by __libdwfl_ptrace_attach. */
459 : : extern void __libdwfl_ptrace_detach (pid_t tid, bool tid_was_stopped)
460 : : internal_function;
461 : :
462 : :
463 : : /* Internal wrapper for old dwfl_module_getsym and new dwfl_module_getsym_info.
464 : : adjust_st_value set to true returns adjusted SYM st_value, set to false
465 : : it will not adjust SYM at all, but does match against resolved *ADDR. */
466 : : extern const char *__libdwfl_getsym (Dwfl_Module *mod, int ndx, GElf_Sym *sym,
467 : : GElf_Addr *addr, GElf_Word *shndxp,
468 : : Elf **elfp, Dwarf_Addr *biasp,
469 : : bool *resolved, bool adjust_st_value)
470 : : internal_function;
471 : :
472 : : extern void __libdwfl_module_free (Dwfl_Module *mod) internal_function;
473 : :
474 : : /* Find the main ELF file, update MOD->elferr and/or MOD->main.elf. */
475 : : extern void __libdwfl_getelf (Dwfl_Module *mod) internal_function;
476 : :
477 : : /* Process relocations in debugging sections in an ET_REL file.
478 : : FILE must be opened with ELF_C_READ_MMAP_PRIVATE or ELF_C_READ,
479 : : to make it possible to relocate the data in place (or ELF_C_RDWR or
480 : : ELF_C_RDWR_MMAP if you intend to modify the Elf file on disk). After
481 : : this, dwarf_begin_elf on FILE will read the relocated data.
482 : :
483 : : When DEBUG is false, apply partial relocation to all sections. */
484 : : extern Dwfl_Error __libdwfl_relocate (Dwfl_Module *mod, Elf *file, bool debug)
485 : : internal_function;
486 : :
487 : : /* Find the section index in mod->main.elf that contains the given
488 : : *ADDR. Adjusts *ADDR to be section relative on success, returns
489 : : SHN_UNDEF on failure. */
490 : : extern size_t __libdwfl_find_section_ndx (Dwfl_Module *mod, Dwarf_Addr *addr)
491 : : internal_function;
492 : :
493 : : /* Process (simple) relocations in arbitrary section TSCN of an ET_REL file.
494 : : RELOCSCN is SHT_REL or SHT_RELA and TSCN is its sh_info target section. */
495 : : extern Dwfl_Error __libdwfl_relocate_section (Dwfl_Module *mod, Elf *relocated,
496 : : Elf_Scn *relocscn, Elf_Scn *tscn,
497 : : bool partial)
498 : : internal_function;
499 : :
500 : : /* Adjust *VALUE from section-relative to absolute.
501 : : MOD->dwfl->callbacks->section_address is called to determine the actual
502 : : address of a loaded section. */
503 : : extern Dwfl_Error __libdwfl_relocate_value (Dwfl_Module *mod, Elf *elf,
504 : : size_t *shstrndx_cache,
505 : : Elf32_Word shndx,
506 : : GElf_Addr *value)
507 : : internal_function;
508 : :
509 : : /* Ensure that MOD->ebl is set up. */
510 : : extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
511 : :
512 : : /* Install a new Dwarf_CFI in *SLOT (MOD->eh_cfi or MOD->dwarf_cfi). */
513 : : extern Dwarf_CFI *__libdwfl_set_cfi (Dwfl_Module *mod, Dwarf_CFI **slot,
514 : : Dwarf_CFI *cfi)
515 : : internal_function;
516 : :
517 : : /* Iterate through all the CU's in the module. Start by passing a null
518 : : LASTCU, and then pass the last *CU returned. Success return with null
519 : : *CU no more CUs. */
520 : : extern Dwfl_Error __libdwfl_nextcu (Dwfl_Module *mod, struct dwfl_cu *lastcu,
521 : : struct dwfl_cu **cu) internal_function;
522 : :
523 : : /* Find the CU by address. */
524 : : extern Dwfl_Error __libdwfl_addrcu (Dwfl_Module *mod, Dwarf_Addr addr,
525 : : struct dwfl_cu **cu) internal_function;
526 : :
527 : : /* Ensure that CU->lines (and CU->cu->lines) is set up. */
528 : : extern Dwfl_Error __libdwfl_cu_getsrclines (struct dwfl_cu *cu)
529 : : internal_function;
530 : :
531 : : /* Look in ELF for an NT_GNU_BUILD_ID note. Store it to BUILD_ID_BITS,
532 : : its vaddr in ELF to BUILD_ID_VADDR (it is unrelocated, even if MOD is not
533 : : NULL) and store length to BUILD_ID_LEN. Returns -1 for errors, 1 if it was
534 : : stored and 0 if no note is found. MOD may be NULL, MOD must be non-NULL
535 : : only if ELF is ET_REL. */
536 : : extern int __libdwfl_find_elf_build_id (Dwfl_Module *mod, Elf *elf,
537 : : const void **build_id_bits,
538 : : GElf_Addr *build_id_elfaddr,
539 : : int *build_id_len)
540 : : internal_function;
541 : :
542 : : /* Look in ELF for an NT_GNU_BUILD_ID note. If SET is true, store it
543 : : in MOD and return its length. If SET is false, instead compare it
544 : : to that stored in MOD and return 2 if they match, 1 if they do not.
545 : : Returns -1 for errors, 0 if no note is found. */
546 : : extern int __libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
547 : : internal_function;
548 : :
549 : : /* Open a main or debuginfo file by its build ID, returns the fd. */
550 : : extern int __libdwfl_open_mod_by_build_id (Dwfl_Module *mod, bool debug,
551 : : char **file_name) internal_function;
552 : :
553 : : /* Same, but takes an explicit build_id, can also be used for alt debug. */
554 : : extern int __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug,
555 : : char **file_name, const size_t id_len,
556 : : const uint8_t *id) internal_function;
557 : :
558 : : extern uint32_t __libdwfl_crc32 (uint32_t crc, unsigned char *buf, size_t len)
559 : : attribute_hidden;
560 : : extern int __libdwfl_crc32_file (int fd, uint32_t *resp) attribute_hidden;
561 : :
562 : :
563 : : /* Given ELF and some parameters return TRUE if the *P return value parameters
564 : : have been successfully filled in. Any of the *P parameters can be NULL. */
565 : : extern bool __libdwfl_elf_address_range (Elf *elf, GElf_Addr base,
566 : : bool add_p_vaddr, bool sanity,
567 : : GElf_Addr *vaddrp,
568 : : GElf_Addr *address_syncp,
569 : : GElf_Addr *startp, GElf_Addr *endp,
570 : : GElf_Addr *biasp, GElf_Half *e_typep)
571 : : internal_function;
572 : :
573 : : /* Meat of dwfl_report_elf, given elf_begin just called.
574 : : Consumes ELF on success, not on failure. */
575 : : extern Dwfl_Module *__libdwfl_report_elf (Dwfl *dwfl, const char *name,
576 : : const char *file_name, int fd,
577 : : Elf *elf, GElf_Addr base,
578 : : bool add_p_vaddr, bool sanity)
579 : : internal_function;
580 : :
581 : : /* Meat of dwfl_report_offline. */
582 : : extern Dwfl_Module *__libdwfl_report_offline (Dwfl *dwfl, const char *name,
583 : : const char *file_name,
584 : : int fd, bool closefd,
585 : : int (*predicate) (const char *,
586 : : const char *))
587 : : internal_function;
588 : :
589 : : /* Free PROCESS. Unlink and free also any structures it references. */
590 : : extern void __libdwfl_process_free (Dwfl_Process *process)
591 : : internal_function;
592 : :
593 : : /* Update STATE->unwound for the unwound frame.
594 : : On error STATE->unwound == NULL
595 : : or STATE->unwound->pc_state == DWFL_FRAME_STATE_ERROR;
596 : : in such case dwfl_errno () is set.
597 : : If STATE->unwound->pc_state == DWFL_FRAME_STATE_PC_UNDEFINED
598 : : then STATE was the last valid frame. */
599 : : extern void __libdwfl_frame_unwind (Dwfl_Frame *state)
600 : : internal_function;
601 : :
602 : : /* Align segment START downwards or END upwards addresses according to DWFL. */
603 : : extern GElf_Addr __libdwfl_segment_start (Dwfl *dwfl, GElf_Addr start)
604 : : internal_function;
605 : : extern GElf_Addr __libdwfl_segment_end (Dwfl *dwfl, GElf_Addr end)
606 : : internal_function;
607 : :
608 : : /* Decompression wrappers: decompress whole file into memory. */
609 : : extern Dwfl_Error __libdw_gunzip (int fd, off_t start_offset,
610 : : void *mapped, size_t mapped_size,
611 : : void **whole, size_t *whole_size)
612 : : internal_function;
613 : : extern Dwfl_Error __libdw_bunzip2 (int fd, off_t start_offset,
614 : : void *mapped, size_t mapped_size,
615 : : void **whole, size_t *whole_size)
616 : : internal_function;
617 : : extern Dwfl_Error __libdw_unlzma (int fd, off_t start_offset,
618 : : void *mapped, size_t mapped_size,
619 : : void **whole, size_t *whole_size)
620 : : internal_function;
621 : : extern Dwfl_Error __libdw_unzstd (int fd, off_t start_offset,
622 : : void *mapped, size_t mapped_size,
623 : : void **whole, size_t *whole_size)
624 : : internal_function;
625 : :
626 : : /* Skip the image header before a file image: updates *START_OFFSET. */
627 : : extern Dwfl_Error __libdw_image_header (int fd, off_t *start_offset,
628 : : void *mapped, size_t mapped_size)
629 : : internal_function;
630 : :
631 : : /* Open Elf handle on *FDP. This handles decompression and checks
632 : : elf_kind. Succeed only for ELF_K_ELF, or also ELF_K_AR if ARCHIVE_OK.
633 : : Returns DWFL_E_NOERROR and sets *ELFP on success, resets *FDP to -1 if
634 : : it's no longer used. Resets *FDP on failure too iff CLOSE_ON_FAIL. */
635 : : extern Dwfl_Error __libdw_open_file (int *fdp, Elf **elfp,
636 : : bool close_on_fail, bool archive_ok)
637 : : internal_function;
638 : :
639 : : /* Same as __libdw_open_file, but opens Elf handle from memory region. */
640 : : extern Dwfl_Error __libdw_open_elf_memory (char *data, size_t size, Elf **elfp,
641 : : bool archive_ok)
642 : : internal_function;
643 : :
644 : : /* Same as __libdw_open_file, but never closes the given file
645 : : descriptor and ELF_K_AR is always an acceptable type. */
646 : : extern Dwfl_Error __libdw_open_elf (int fd, Elf **elfp) internal_function;
647 : :
648 : : /* Fetch PT_DYNAMIC P_VADDR from ELF and store it to *VADDRP. Return success.
649 : : *VADDRP is not modified if the function fails. */
650 : : extern bool __libdwfl_dynamic_vaddr_get (Elf *elf, GElf_Addr *vaddrp)
651 : : internal_function;
652 : :
653 : : #ifdef ENABLE_LIBDEBUGINFOD
654 : : /* Internal interface to libdebuginfod (if installed). */
655 : : int
656 : : __libdwfl_debuginfod_find_executable (Dwfl *dwfl,
657 : : const unsigned char *build_id_bits,
658 : : size_t build_id_len);
659 : : int
660 : : __libdwfl_debuginfod_find_debuginfo (Dwfl *dwfl,
661 : : const unsigned char *build_id_bits,
662 : : size_t build_id_len);
663 : : void
664 : : __libdwfl_debuginfod_end (debuginfod_client *c);
665 : : #endif
666 : :
667 : :
668 : : /* These are working nicely for --core, but are not ready to be
669 : : exported interfaces quite yet. */
670 : :
671 : : /* Type of callback function ...
672 : : */
673 : : typedef bool Dwfl_Memory_Callback (Dwfl *dwfl, int segndx,
674 : : void **buffer, size_t *buffer_available,
675 : : GElf_Addr vaddr, size_t minread, void *arg);
676 : :
677 : : /* Type of callback function ...
678 : : */
679 : : typedef bool Dwfl_Module_Callback (Dwfl_Module *mod, void **userdata,
680 : : const char *name, Dwarf_Addr base,
681 : : void **buffer, size_t *buffer_available,
682 : : GElf_Off cost, GElf_Off worthwhile,
683 : : GElf_Off whole, GElf_Off contiguous,
684 : : void *arg, Elf **elfp);
685 : :
686 : : /* One shared library (or executable) info from DT_DEBUG link map. */
687 : : struct r_debug_info_module
688 : : {
689 : : struct r_debug_info_module *next;
690 : : /* FD is -1 iff ELF is NULL. */
691 : : int fd;
692 : : Elf *elf;
693 : : GElf_Addr l_ld;
694 : : /* START and END are both zero if not valid. */
695 : : GElf_Addr start, end;
696 : : bool disk_file_has_build_id;
697 : : char name[0];
698 : : };
699 : :
700 : : /* Information gathered from DT_DEBUG by dwfl_link_map_report hinted to
701 : : dwfl_segment_report_module. */
702 : : struct r_debug_info
703 : : {
704 : : struct r_debug_info_module *module;
705 : : };
706 : :
707 : : /* ...
708 : : */
709 : : extern int dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
710 : : const char *executable,
711 : : Dwfl_Memory_Callback *memory_callback,
712 : : void *memory_callback_arg,
713 : : Dwfl_Module_Callback *read_eagerly,
714 : : void *read_eagerly_arg,
715 : : size_t maxread,
716 : : const void *note_file,
717 : : size_t note_file_size,
718 : : const struct r_debug_info *r_debug_info);
719 : :
720 : : /* Report a module for entry in the dynamic linker's struct link_map list.
721 : : For each link_map entry, if an existing module resides at its address,
722 : : this just modifies that module's name and suggested file name. If
723 : : no such module exists, this calls dwfl_report_elf on the l_name string.
724 : :
725 : : If AUXV is not null, it points to AUXV_SIZE bytes of auxiliary vector
726 : : data as contained in an NT_AUXV note or read from a /proc/pid/auxv
727 : : file. When this is available, it guides the search. If AUXV is null
728 : : or the memory it points to is not accessible, then this search can
729 : : only find where to begin if the correct executable file was
730 : : previously reported and preloaded as with dwfl_report_elf.
731 : :
732 : : Fill in R_DEBUG_INFO if it is not NULL. It should be cleared by the
733 : : caller, this function does not touch fields it does not need to modify.
734 : : If R_DEBUG_INFO is not NULL then no modules get added to DWFL, caller
735 : : has to add them from filled in R_DEBUG_INFO.
736 : :
737 : : Returns the number of modules found, or -1 for errors. */
738 : : extern int dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
739 : : Dwfl_Memory_Callback *memory_callback,
740 : : void *memory_callback_arg,
741 : : struct r_debug_info *r_debug_info);
742 : :
743 : :
744 : : /* Avoid PLT entries. */
745 : : INTDECL (dwfl_begin)
746 : : INTDECL (dwfl_errmsg)
747 : : INTDECL (dwfl_errno)
748 : : INTDECL (dwfl_addrmodule)
749 : : INTDECL (dwfl_addrsegment)
750 : : INTDECL (dwfl_addrdwarf)
751 : : INTDECL (dwfl_addrdie)
752 : : INTDECL (dwfl_core_file_attach)
753 : : INTDECL (dwfl_core_file_report)
754 : : INTDECL (dwfl_getmodules)
755 : : INTDECL (dwfl_module_addrdie)
756 : : INTDECL (dwfl_module_address_section)
757 : : INTDECL (dwfl_module_addrinfo)
758 : : INTDECL (dwfl_module_addrsym)
759 : : INTDECL (dwfl_module_build_id)
760 : : INTDECL (dwfl_module_getdwarf)
761 : : INTDECL (dwfl_module_getelf)
762 : : INTDECL (dwfl_module_getsym)
763 : : INTDECL (dwfl_module_getsym_info)
764 : : INTDECL (dwfl_module_getsymtab)
765 : : INTDECL (dwfl_module_getsymtab_first_global)
766 : : INTDECL (dwfl_module_getsrc)
767 : : INTDECL (dwfl_module_report_build_id)
768 : : INTDECL (dwfl_report_elf)
769 : : INTDECL (dwfl_report_begin)
770 : : INTDECL (dwfl_report_begin_add)
771 : : INTDECL (dwfl_report_module)
772 : : INTDECL (dwfl_report_segment)
773 : : INTDECL (dwfl_report_offline)
774 : : INTDECL (dwfl_report_offline_memory)
775 : : INTDECL (dwfl_report_end)
776 : : INTDECL (dwfl_build_id_find_elf)
777 : : INTDECL (dwfl_build_id_find_debuginfo)
778 : : INTDECL (dwfl_standard_find_debuginfo)
779 : : INTDECL (dwfl_link_map_report)
780 : : INTDECL (dwfl_linux_kernel_find_elf)
781 : : INTDECL (dwfl_linux_kernel_module_section_address)
782 : : INTDECL (dwfl_linux_proc_attach)
783 : : INTDECL (dwfl_linux_proc_report)
784 : : INTDECL (dwfl_linux_proc_maps_report)
785 : : INTDECL (dwfl_linux_proc_find_elf)
786 : : INTDECL (dwfl_linux_kernel_report_kernel)
787 : : INTDECL (dwfl_linux_kernel_report_modules)
788 : : INTDECL (dwfl_linux_kernel_report_offline)
789 : : INTDECL (dwfl_offline_section_address)
790 : : INTDECL (dwfl_module_relocate_address)
791 : : INTDECL (dwfl_module_dwarf_cfi)
792 : : INTDECL (dwfl_module_eh_cfi)
793 : : INTDECL (dwfl_attach_state)
794 : : INTDECL (dwfl_pid)
795 : : INTDECL (dwfl_thread_dwfl)
796 : : INTDECL (dwfl_thread_tid)
797 : : INTDECL (dwfl_frame_thread)
798 : : INTDECL (dwfl_thread_state_registers)
799 : : INTDECL (dwfl_thread_state_register_pc)
800 : : INTDECL (dwfl_getthread_frames)
801 : : INTDECL (dwfl_getthreads)
802 : : INTDECL (dwfl_thread_getframes)
803 : : INTDECL (dwfl_frame_pc)
804 : : INTDECL (dwfl_frame_reg)
805 : : INTDECL (dwfl_get_debuginfod_client)
806 : :
807 : : /* Leading arguments standard to callbacks passed a Dwfl_Module. */
808 : : #define MODCB_ARGS(mod) (mod), &(mod)->userdata, (mod)->name, (mod)->low_addr
809 : : #define CBFAIL (errno ? DWFL_E (ERRNO, errno) : DWFL_E_CB);
810 : :
811 : :
812 : : /* The default used by dwfl_standard_find_debuginfo. */
813 : : #define DEFAULT_DEBUGINFO_PATH ":.debug:/usr/lib/debug"
814 : :
815 : :
816 : : #endif /* libdwflP.h */
|