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