1 /* Internal definitions for libdwfl.
2 Copyright (C) 2005-2011 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
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
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
18 or both in parallel, as here.
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.
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/>. */
43 #include "../libdw/libdwP.h" /* We need its INTDECLs. */
45 /* gettext helper macros. */
46 #define _(Str) dgettext ("elfutils", Str)
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"))
79 #define DWFL_ERROR(name, text) DWFL_E_##name,
80 typedef enum { DWFL_ERRORS DWFL_E_NUM } Dwfl_Error;
83 #define OTHER_ERROR(name) ((unsigned int) DWFL_E_##name << 16)
84 #define DWFL_E(name, errno) (OTHER_ERROR (name) | (errno))
86 extern int __libdwfl_canon_error (Dwfl_Error) internal_function;
87 extern void __libdwfl_seterrno (Dwfl_Error) internal_function;
91 const Dwfl_Callbacks *callbacks;
93 Dwfl_Module *modulelist; /* List in order used by full traversals. */
95 GElf_Addr offline_next_address;
97 GElf_Addr segment_align; /* Smallest granularity of segments. */
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. */
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;
113 #define OFFLINE_REDZONE 0x10000
119 bool valid; /* The build ID note has been matched. */
120 bool relocated; /* Partial relocation of all sections done. */
124 /* This is the lowest p_vaddr in this ELF file, aligned to p_align.
125 For a file without phdrs, this is zero. */
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;
136 struct Dwfl_Module *next; /* Link on Dwfl.modulelist. */
140 char *name; /* Iterator name for this module. */
141 GElf_Addr low_addr, high_addr;
143 struct dwfl_file main, debug;
146 GElf_Half e_type; /* GElf_Ehdr.e_type cache. */
147 Dwfl_Error elferr; /* Previous failure to open main file. */
149 struct dwfl_relocation *reloc_info; /* Relocatable sections. */
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. */
158 Dwarf *dw; /* libdw handle for its debugging info. */
160 Dwfl_Error symerr; /* Previous failure to load symbols. */
161 Dwfl_Error dwerr; /* Previous failure to load DWARF. */
163 /* Known CU's in this module. */
164 struct dwfl_cu *first_cu, **cu;
166 void *lazy_cu_root; /* Table indexed by Dwarf_Off of CU. */
168 struct dwfl_arange *aranges; /* Mapping of addresses in module to CUs. */
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. */
175 unsigned int lazycu; /* Possible users, deleted when none left. */
176 unsigned int naranges;
178 Dwarf_CFI *dwarf_cfi; /* Cached DWARF CFI for this module. */
179 Dwarf_CFI *eh_cfi; /* Cached EH CFI for this module. */
181 int segment; /* Index of first segment table entry. */
182 bool gc; /* Mark/sweep flag. */
187 /* Information cached about each CU in Dwfl_Module.dw. */
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. */
195 Dwfl_Module *mod; /* Pointer back to containing module. */
197 struct dwfl_cu *next; /* CU immediately following in the file. */
199 struct Dwfl_Lines *lines;
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. */
214 unsigned int idx; /* My index in the dwfl_cu.lines table. */
218 static inline struct dwfl_cu *
219 dwfl_linecu_inline (const Dwfl_Line *line)
221 const struct Dwfl_Lines *lines = ((const void *) line
222 - offsetof (struct Dwfl_Lines,
226 #define dwfl_linecu dwfl_linecu_inline
228 static inline GElf_Addr
229 dwfl_adjusted_address (Dwfl_Module *mod, GElf_Addr addr)
231 return addr + mod->main_bias;
234 static inline GElf_Addr
235 dwfl_deadjust_address (Dwfl_Module *mod, GElf_Addr addr)
237 return addr - mod->main_bias;
240 static inline Dwarf_Addr
241 dwfl_adjusted_dwarf_addr (Dwfl_Module *mod, Dwarf_Addr addr)
243 return dwfl_adjusted_address (mod, (addr
244 - mod->debug.address_sync
245 + mod->main.address_sync));
248 static inline Dwarf_Addr
249 dwfl_deadjust_dwarf_addr (Dwfl_Module *mod, Dwarf_Addr addr)
251 return (dwfl_deadjust_address (mod, addr)
252 - mod->main.address_sync
253 + mod->debug.address_sync);
256 static inline GElf_Addr
257 dwfl_adjusted_st_value (Dwfl_Module *mod, GElf_Addr addr)
259 if (mod->symfile == &mod->main)
260 return dwfl_adjusted_address (mod, addr);
261 return dwfl_adjusted_dwarf_addr (mod, addr);
264 static inline GElf_Addr
265 dwfl_deadjust_st_value (Dwfl_Module *mod, GElf_Addr addr)
267 if (mod->symfile == &mod->main)
268 return dwfl_deadjust_address (mod, addr);
269 return dwfl_deadjust_dwarf_addr (mod, addr);
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. */
277 size_t arange; /* Index in Dwarf_Aranges. */
282 extern void __libdwfl_module_free (Dwfl_Module *mod) internal_function;
284 /* Find the main ELF file, update MOD->elferr and/or MOD->main.elf. */
285 extern void __libdwfl_getelf (Dwfl_Module *mod) internal_function;
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.
293 When DEBUG is false, apply partial relocation to all sections. */
294 extern Dwfl_Error __libdwfl_relocate (Dwfl_Module *mod, Elf *file, bool debug)
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,
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,
314 /* Ensure that MOD->ebl is set up. */
315 extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
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,
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
325 extern Dwfl_Error __libdwfl_nextcu (Dwfl_Module *mod, struct dwfl_cu *lastcu,
326 struct dwfl_cu **cu) internal_function;
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;
332 /* Ensure that CU->lines (and CU->cu->lines) is set up. */
333 extern Dwfl_Error __libdwfl_cu_getsrclines (struct dwfl_cu *cu)
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)
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;
347 extern uint32_t __libdwfl_crc32 (uint32_t crc, unsigned char *buf, size_t len)
349 extern int __libdwfl_crc32_file (int fd, uint32_t *resp) attribute_hidden;
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)
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 *,
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)
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)
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)
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)
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)
394 /* These are working nicely for --core, but are not ready to be
395 exported interfaces quite yet. */
397 /* Type of callback function ...
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);
403 /* Type of callback function ...
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);
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);
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.
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.
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);
438 /* Avoid PLT entries. */
440 INTDECL (dwfl_errmsg)
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)
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);
487 /* The default used by dwfl_standard_find_debuginfo. */
488 #define DEFAULT_DEBUGINFO_PATH ":.debug:/usr/lib/debug"
491 #endif /* libdwflP.h */