Branch data Line data Source code
1 : : /* Internal definitions for libdwfl.
2 : : Copyright (C) 2005-2011 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 : : #ifndef PACKAGE_NAME
33 : : # include <config.h>
34 : : #endif
35 : : #include <libdwfl.h>
36 : : #include <libebl.h>
37 : : #include <assert.h>
38 : : #include <errno.h>
39 : : #include <stdbool.h>
40 : : #include <stdlib.h>
41 : : #include <string.h>
42 : :
43 : : #include "../libdw/libdwP.h" /* We need its INTDECLs. */
44 : :
45 : : /* gettext helper macros. */
46 : : #define _(Str) dgettext ("elfutils", Str)
47 : :
48 : : #define DWFL_ERRORS \
49 : : DWFL_ERROR (NOERROR, N_("no error")) \
50 : : DWFL_ERROR (UNKNOWN_ERROR, N_("unknown error")) \
51 : : DWFL_ERROR (NOMEM, N_("out of memory")) \
52 : : DWFL_ERROR (ERRNO, N_("See errno")) \
53 : : DWFL_ERROR (LIBELF, N_("See elf_errno")) \
54 : : DWFL_ERROR (LIBDW, N_("See dwarf_errno")) \
55 : : DWFL_ERROR (LIBEBL, N_("See ebl_errno (XXX missing)")) \
56 : : DWFL_ERROR (ZLIB, N_("gzip decompression failed")) \
57 : : DWFL_ERROR (BZLIB, N_("bzip2 decompression failed")) \
58 : : DWFL_ERROR (LZMA, N_("LZMA 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 : :
79 : : #define DWFL_ERROR(name, text) DWFL_E_##name,
80 : : typedef enum { DWFL_ERRORS DWFL_E_NUM } Dwfl_Error;
81 : : #undef DWFL_ERROR
82 : :
83 : : #define OTHER_ERROR(name) ((unsigned int) DWFL_E_##name << 16)
84 : : #define DWFL_E(name, errno) (OTHER_ERROR (name) | (errno))
85 : :
86 : : extern int __libdwfl_canon_error (Dwfl_Error) internal_function;
87 : : extern void __libdwfl_seterrno (Dwfl_Error) internal_function;
88 : :
89 : : struct Dwfl
90 : : {
91 : : const Dwfl_Callbacks *callbacks;
92 : :
93 : : Dwfl_Module *modulelist; /* List in order used by full traversals. */
94 : :
95 : : GElf_Addr offline_next_address;
96 : :
97 : : GElf_Addr segment_align; /* Smallest granularity of segments. */
98 : :
99 : : /* Binary search table in three parallel malloc'd arrays. */
100 : : size_t lookup_elts; /* Elements in use. */
101 : : size_t lookup_alloc; /* Elements allococated. */
102 : : GElf_Addr *lookup_addr; /* Start address of segment. */
103 : : Dwfl_Module **lookup_module; /* Module associated with segment, or null. */
104 : : int *lookup_segndx; /* User segment index, or -1. */
105 : :
106 : : /* Cache from last dwfl_report_segment call. */
107 : : const void *lookup_tail_ident;
108 : : GElf_Off lookup_tail_vaddr;
109 : : GElf_Off lookup_tail_offset;
110 : : int lookup_tail_ndx;
111 : : };
112 : :
113 : : #define OFFLINE_REDZONE 0x10000
114 : :
115 : : struct dwfl_file
116 : : {
117 : : char *name;
118 : : int fd;
119 : : bool valid; /* The build ID note has been matched. */
120 : : bool relocated; /* Partial relocation of all sections done. */
121 : :
122 : : Elf *elf;
123 : :
124 : : /* This is the lowest p_vaddr in this ELF file, aligned to p_align.
125 : : For a file without phdrs, this is zero. */
126 : : GElf_Addr vaddr;
127 : :
128 : : /* This is an address chosen for synchronization between the main file
129 : : and the debug file. See dwfl_module_getdwarf.c for how it's chosen. */
130 : : GElf_Addr address_sync;
131 : : };
132 : :
133 : : struct Dwfl_Module
134 : : {
135 : : Dwfl *dwfl;
136 : : struct Dwfl_Module *next; /* Link on Dwfl.modulelist. */
137 : :
138 : : void *userdata;
139 : :
140 : : char *name; /* Iterator name for this module. */
141 : : GElf_Addr low_addr, high_addr;
142 : :
143 : : struct dwfl_file main, debug;
144 : : GElf_Addr main_bias;
145 : : Ebl *ebl;
146 : : GElf_Half e_type; /* GElf_Ehdr.e_type cache. */
147 : : Dwfl_Error elferr; /* Previous failure to open main file. */
148 : :
149 : : struct dwfl_relocation *reloc_info; /* Relocatable sections. */
150 : :
151 : : struct dwfl_file *symfile; /* Either main or debug. */
152 : : Elf_Data *symdata; /* Data in the ELF symbol table section. */
153 : : size_t syments; /* sh_size / sh_entsize of that section. */
154 : : int first_global; /* Index of first global symbol of table. */
155 : : Elf_Data *symstrdata; /* Data for its string table. */
156 : : Elf_Data *symxndxdata; /* Data in the extended section index table. */
157 : :
158 : : Dwarf *dw; /* libdw handle for its debugging info. */
159 : :
160 : : Dwfl_Error symerr; /* Previous failure to load symbols. */
161 : : Dwfl_Error dwerr; /* Previous failure to load DWARF. */
162 : :
163 : : /* Known CU's in this module. */
164 : : struct dwfl_cu *first_cu, **cu;
165 : :
166 : : void *lazy_cu_root; /* Table indexed by Dwarf_Off of CU. */
167 : :
168 : : struct dwfl_arange *aranges; /* Mapping of addresses in module to CUs. */
169 : :
170 : : void *build_id_bits; /* malloc'd copy of build ID bits. */
171 : : GElf_Addr build_id_vaddr; /* Address where they reside, 0 if unknown. */
172 : : int build_id_len; /* -1 for prior failure, 0 if unset. */
173 : :
174 : : unsigned int ncu;
175 : : unsigned int lazycu; /* Possible users, deleted when none left. */
176 : : unsigned int naranges;
177 : :
178 : : Dwarf_CFI *dwarf_cfi; /* Cached DWARF CFI for this module. */
179 : : Dwarf_CFI *eh_cfi; /* Cached EH CFI for this module. */
180 : :
181 : : int segment; /* Index of first segment table entry. */
182 : : bool gc; /* Mark/sweep flag. */
183 : : };
184 : :
185 : :
186 : :
187 : : /* Information cached about each CU in Dwfl_Module.dw. */
188 : : struct dwfl_cu
189 : : {
190 : : /* This caches libdw information about the CU. It's also the
191 : : address passed back to users, so we take advantage of the
192 : : fact that it's placed first to cast back. */
193 : : Dwarf_Die die;
194 : :
195 : : Dwfl_Module *mod; /* Pointer back to containing module. */
196 : :
197 : : struct dwfl_cu *next; /* CU immediately following in the file. */
198 : :
199 : : struct Dwfl_Lines *lines;
200 : : };
201 : :
202 : : struct Dwfl_Lines
203 : : {
204 : : struct dwfl_cu *cu;
205 : :
206 : : /* This is what the opaque Dwfl_Line * pointers we pass to users are.
207 : : We need to recover pointers to our struct dwfl_cu and a record in
208 : : libdw's Dwarf_Line table. To minimize the memory used in addition
209 : : to libdw's Dwarf_Lines buffer, we just point to our own index in
210 : : this table, and have one pointer back to the CU. The indices here
211 : : match those in libdw's Dwarf_CU.lines->info table. */
212 : : struct Dwfl_Line
213 : : {
214 : : unsigned int idx; /* My index in the dwfl_cu.lines table. */
215 : : } idx[0];
216 : : };
217 : :
218 : : static inline struct dwfl_cu *
219 : : dwfl_linecu_inline (const Dwfl_Line *line)
220 : : {
221 : 1949 : const struct Dwfl_Lines *lines = ((const void *) line
222 : 1309 : - offsetof (struct Dwfl_Lines,
223 : : idx[line->idx]));
224 : 1309 : return lines->cu;
225 : : }
226 : : #define dwfl_linecu dwfl_linecu_inline
227 : :
228 : : static inline GElf_Addr
229 : : dwfl_adjusted_address (Dwfl_Module *mod, GElf_Addr addr)
230 : : {
231 : 371762116 : return addr + mod->main_bias;
232 : : }
233 : :
234 : : static inline GElf_Addr
235 : : dwfl_deadjust_address (Dwfl_Module *mod, GElf_Addr addr)
236 : : {
237 : 77133 : return addr - mod->main_bias;
238 : : }
239 : :
240 : : static inline Dwarf_Addr
241 : : dwfl_adjusted_dwarf_addr (Dwfl_Module *mod, Dwarf_Addr addr)
242 : : {
243 : 18308 : return dwfl_adjusted_address (mod, (addr
244 : 9154 : - mod->debug.address_sync
245 : 9154 : + mod->main.address_sync));
246 : : }
247 : :
248 : : static inline Dwarf_Addr
249 : : dwfl_deadjust_dwarf_addr (Dwfl_Module *mod, Dwarf_Addr addr)
250 : : {
251 : 108 : return (dwfl_deadjust_address (mod, addr)
252 : 62 : - mod->main.address_sync
253 : 62 : + mod->debug.address_sync);
254 : : }
255 : :
256 : : static inline GElf_Addr
257 : : dwfl_adjusted_st_value (Dwfl_Module *mod, GElf_Addr addr)
258 : : {
259 [ + + ]: 371753045 : if (mod->symfile == &mod->main)
260 : 371752763 : return dwfl_adjusted_address (mod, addr);
261 : : return dwfl_adjusted_dwarf_addr (mod, addr);
262 : : }
263 : :
264 : : static inline GElf_Addr
265 : : dwfl_deadjust_st_value (Dwfl_Module *mod, GElf_Addr addr)
266 : : {
267 [ + + ]: 77103 : if (mod->symfile == &mod->main)
268 : 77087 : return dwfl_deadjust_address (mod, addr);
269 : : return dwfl_deadjust_dwarf_addr (mod, addr);
270 : : }
271 : :
272 : : /* This describes a contiguous address range that lies in a single CU.
273 : : We condense runs of Dwarf_Arange entries for the same CU into this. */
274 : : struct dwfl_arange
275 : : {
276 : : struct dwfl_cu *cu;
277 : : size_t arange; /* Index in Dwarf_Aranges. */
278 : : };
279 : :
280 : :
281 : :
282 : : extern void __libdwfl_module_free (Dwfl_Module *mod) internal_function;
283 : :
284 : : /* Find the main ELF file, update MOD->elferr and/or MOD->main.elf. */
285 : : extern void __libdwfl_getelf (Dwfl_Module *mod) internal_function;
286 : :
287 : : /* Process relocations in debugging sections in an ET_REL file.
288 : : FILE must be opened with ELF_C_READ_MMAP_PRIVATE or ELF_C_READ,
289 : : to make it possible to relocate the data in place (or ELF_C_RDWR or
290 : : ELF_C_RDWR_MMAP if you intend to modify the Elf file on disk). After
291 : : this, dwarf_begin_elf on FILE will read the relocated data.
292 : :
293 : : When DEBUG is false, apply partial relocation to all sections. */
294 : : extern Dwfl_Error __libdwfl_relocate (Dwfl_Module *mod, Elf *file, bool debug)
295 : : internal_function;
296 : :
297 : : /* Process (simple) relocations in arbitrary section TSCN of an ET_REL file.
298 : : RELOCSCN is SHT_REL or SHT_RELA and TSCN is its sh_info target section. */
299 : : extern Dwfl_Error __libdwfl_relocate_section (Dwfl_Module *mod, Elf *relocated,
300 : : Elf_Scn *relocscn, Elf_Scn *tscn,
301 : : bool partial)
302 : : internal_function;
303 : :
304 : : /* Adjust *VALUE from section-relative to absolute.
305 : : MOD->dwfl->callbacks->section_address is called to determine the actual
306 : : address of a loaded section. */
307 : : extern Dwfl_Error __libdwfl_relocate_value (Dwfl_Module *mod, Elf *elf,
308 : : size_t *shstrndx_cache,
309 : : Elf32_Word shndx,
310 : : GElf_Addr *value)
311 : : internal_function;
312 : :
313 : :
314 : : /* Ensure that MOD->ebl is set up. */
315 : : extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
316 : :
317 : : /* Install a new Dwarf_CFI in *SLOT (MOD->eh_cfi or MOD->dwarf_cfi). */
318 : : extern Dwarf_CFI *__libdwfl_set_cfi (Dwfl_Module *mod, Dwarf_CFI **slot,
319 : : Dwarf_CFI *cfi)
320 : : internal_function;
321 : :
322 : : /* Iterate through all the CU's in the module. Start by passing a null
323 : : LASTCU, and then pass the last *CU returned. Success return with null
324 : : *CU no more CUs. */
325 : : extern Dwfl_Error __libdwfl_nextcu (Dwfl_Module *mod, struct dwfl_cu *lastcu,
326 : : struct dwfl_cu **cu) internal_function;
327 : :
328 : : /* Find the CU by address. */
329 : : extern Dwfl_Error __libdwfl_addrcu (Dwfl_Module *mod, Dwarf_Addr addr,
330 : : struct dwfl_cu **cu) internal_function;
331 : :
332 : : /* Ensure that CU->lines (and CU->cu->lines) is set up. */
333 : : extern Dwfl_Error __libdwfl_cu_getsrclines (struct dwfl_cu *cu)
334 : : internal_function;
335 : :
336 : : /* Look in ELF for an NT_GNU_BUILD_ID note. If SET is true, store it
337 : : in MOD and return its length. If SET is false, instead compare it
338 : : to that stored in MOD and return 2 if they match, 1 if they do not.
339 : : Returns -1 for errors, 0 if no note is found. */
340 : : extern int __libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
341 : : internal_function;
342 : :
343 : : /* Open a main or debuginfo file by its build ID, returns the fd. */
344 : : extern int __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug,
345 : : char **file_name) internal_function;
346 : :
347 : : extern uint32_t __libdwfl_crc32 (uint32_t crc, unsigned char *buf, size_t len)
348 : : attribute_hidden;
349 : : extern int __libdwfl_crc32_file (int fd, uint32_t *resp) attribute_hidden;
350 : :
351 : :
352 : : /* Meat of dwfl_report_elf, given elf_begin just called.
353 : : Consumes ELF on success, not on failure. */
354 : : extern Dwfl_Module *__libdwfl_report_elf (Dwfl *dwfl, const char *name,
355 : : const char *file_name, int fd,
356 : : Elf *elf, GElf_Addr base, bool sanity)
357 : : internal_function;
358 : :
359 : : /* Meat of dwfl_report_offline. */
360 : : extern Dwfl_Module *__libdwfl_report_offline (Dwfl *dwfl, const char *name,
361 : : const char *file_name,
362 : : int fd, bool closefd,
363 : : int (*predicate) (const char *,
364 : : const char *))
365 : : internal_function;
366 : :
367 : : /* Decompression wrappers: decompress whole file into memory. */
368 : : extern Dwfl_Error __libdw_gunzip (int fd, off64_t start_offset,
369 : : void *mapped, size_t mapped_size,
370 : : void **whole, size_t *whole_size)
371 : : internal_function;
372 : : extern Dwfl_Error __libdw_bunzip2 (int fd, off64_t start_offset,
373 : : void *mapped, size_t mapped_size,
374 : : void **whole, size_t *whole_size)
375 : : internal_function;
376 : : extern Dwfl_Error __libdw_unlzma (int fd, off64_t start_offset,
377 : : void *mapped, size_t mapped_size,
378 : : void **whole, size_t *whole_size)
379 : : internal_function;
380 : :
381 : : /* Skip the image header before a file image: updates *START_OFFSET. */
382 : : extern Dwfl_Error __libdw_image_header (int fd, off64_t *start_offset,
383 : : void *mapped, size_t mapped_size)
384 : : internal_function;
385 : :
386 : : /* Open Elf handle on *FDP. This handles decompression and checks
387 : : elf_kind. Succeed only for ELF_K_ELF, or also ELF_K_AR if ARCHIVE_OK.
388 : : Returns DWFL_E_NOERROR and sets *ELFP on success, resets *FDP to -1 if
389 : : it's no longer used. Resets *FDP on failure too iff CLOSE_ON_FAIL. */
390 : : extern Dwfl_Error __libdw_open_file (int *fdp, Elf **elfp,
391 : : bool close_on_fail, bool archive_ok)
392 : : internal_function;
393 : :
394 : : /* These are working nicely for --core, but are not ready to be
395 : : exported interfaces quite yet. */
396 : :
397 : : /* Type of callback function ...
398 : : */
399 : : typedef bool Dwfl_Memory_Callback (Dwfl *dwfl, int segndx,
400 : : void **buffer, size_t *buffer_available,
401 : : GElf_Addr vaddr, size_t minread, void *arg);
402 : :
403 : : /* Type of callback function ...
404 : : */
405 : : typedef bool Dwfl_Module_Callback (Dwfl_Module *mod, void **userdata,
406 : : const char *name, Dwarf_Addr base,
407 : : void **buffer, size_t *buffer_available,
408 : : GElf_Off cost, GElf_Off worthwhile,
409 : : GElf_Off whole, GElf_Off contiguous,
410 : : void *arg, Elf **elfp);
411 : :
412 : : /* ...
413 : : */
414 : : extern int dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
415 : : Dwfl_Memory_Callback *memory_callback,
416 : : void *memory_callback_arg,
417 : : Dwfl_Module_Callback *read_eagerly,
418 : : void *read_eagerly_arg);
419 : :
420 : : /* Report a module for entry in the dynamic linker's struct link_map list.
421 : : For each link_map entry, if an existing module resides at its address,
422 : : this just modifies that module's name and suggested file name. If
423 : : no such module exists, this calls dwfl_report_elf on the l_name string.
424 : :
425 : : If AUXV is not null, it points to AUXV_SIZE bytes of auxiliary vector
426 : : data as contained in an NT_AUXV note or read from a /proc/pid/auxv
427 : : file. When this is available, it guides the search. If AUXV is null
428 : : or the memory it points to is not accessible, then this search can
429 : : only find where to begin if the correct executable file was
430 : : previously reported and preloaded as with dwfl_report_elf.
431 : :
432 : : Returns the number of modules found, or -1 for errors. */
433 : : extern int dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
434 : : Dwfl_Memory_Callback *memory_callback,
435 : : void *memory_callback_arg);
436 : :
437 : :
438 : : /* Avoid PLT entries. */
439 : : INTDECL (dwfl_begin)
440 : : INTDECL (dwfl_errmsg)
441 : : INTDECL (dwfl_errno)
442 : : INTDECL (dwfl_addrmodule)
443 : : INTDECL (dwfl_addrsegment)
444 : : INTDECL (dwfl_addrdwarf)
445 : : INTDECL (dwfl_addrdie)
446 : : INTDECL (dwfl_core_file_report)
447 : : INTDECL (dwfl_getmodules)
448 : : INTDECL (dwfl_module_addrdie)
449 : : INTDECL (dwfl_module_address_section)
450 : : INTDECL (dwfl_module_addrsym)
451 : : INTDECL (dwfl_module_build_id)
452 : : INTDECL (dwfl_module_getdwarf)
453 : : INTDECL (dwfl_module_getelf)
454 : : INTDECL (dwfl_module_getsym)
455 : : INTDECL (dwfl_module_getsymtab)
456 : : INTDECL (dwfl_module_getsrc)
457 : : INTDECL (dwfl_module_report_build_id)
458 : : INTDECL (dwfl_report_elf)
459 : : INTDECL (dwfl_report_begin)
460 : : INTDECL (dwfl_report_begin_add)
461 : : INTDECL (dwfl_report_module)
462 : : INTDECL (dwfl_report_segment)
463 : : INTDECL (dwfl_report_offline)
464 : : INTDECL (dwfl_report_end)
465 : : INTDECL (dwfl_build_id_find_elf)
466 : : INTDECL (dwfl_build_id_find_debuginfo)
467 : : INTDECL (dwfl_standard_find_debuginfo)
468 : : INTDECL (dwfl_link_map_report)
469 : : INTDECL (dwfl_linux_kernel_find_elf)
470 : : INTDECL (dwfl_linux_kernel_module_section_address)
471 : : INTDECL (dwfl_linux_proc_report)
472 : : INTDECL (dwfl_linux_proc_maps_report)
473 : : INTDECL (dwfl_linux_proc_find_elf)
474 : : INTDECL (dwfl_linux_kernel_report_kernel)
475 : : INTDECL (dwfl_linux_kernel_report_modules)
476 : : INTDECL (dwfl_linux_kernel_report_offline)
477 : : INTDECL (dwfl_offline_section_address)
478 : : INTDECL (dwfl_module_relocate_address)
479 : : INTDECL (dwfl_module_dwarf_cfi)
480 : : INTDECL (dwfl_module_eh_cfi)
481 : :
482 : : /* Leading arguments standard to callbacks passed a Dwfl_Module. */
483 : : #define MODCB_ARGS(mod) (mod), &(mod)->userdata, (mod)->name, (mod)->low_addr
484 : : #define CBFAIL (errno ? DWFL_E (ERRNO, errno) : DWFL_E_CB);
485 : :
486 : :
487 : : /* The default used by dwfl_standard_find_debuginfo. */
488 : : #define DEFAULT_DEBUGINFO_PATH ":.debug:/usr/lib/debug"
489 : :
490 : :
491 : : #endif /* libdwflP.h */
|