• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Internal definitions for libdwfl.
2    Copyright (C) 2005, 2006, 2007, 2008 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4 
5    Red Hat elfutils is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by the
7    Free Software Foundation; version 2 of the License.
8 
9    Red Hat elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with Red Hat elfutils; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17 
18    In addition, as a special exception, Red Hat, Inc. gives You the
19    additional right to link the code of Red Hat elfutils with code licensed
20    under any Open Source Initiative certified open source license
21    (http://www.opensource.org/licenses/index.php) which requires the
22    distribution of source code with any binary distribution and to
23    distribute linked combinations of the two.  Non-GPL Code permitted under
24    this exception must only link to the code of Red Hat elfutils through
25    those well defined interfaces identified in the file named EXCEPTION
26    found in the source code files (the "Approved Interfaces").  The files
27    of Non-GPL Code may instantiate templates or use macros or inline
28    functions from the Approved Interfaces without causing the resulting
29    work to be covered by the GNU General Public License.  Only Red Hat,
30    Inc. may make changes or additions to the list of Approved Interfaces.
31    Red Hat's grant of this exception is conditioned upon your not adding
32    any new exceptions.  If you wish to add a new Approved Interface or
33    exception, please contact Red Hat.  You must obey the GNU General Public
34    License in all respects for all of the Red Hat elfutils code and other
35    code used in conjunction with Red Hat elfutils except the Non-GPL Code
36    covered by this exception.  If you modify this file, you may extend this
37    exception to your version of the file, but you are not obligated to do
38    so.  If you do not wish to provide this exception without modification,
39    you must delete this exception statement from your version and license
40    this file solely under the GPL without exception.
41 
42    Red Hat elfutils is an included package of the Open Invention Network.
43    An included package of the Open Invention Network is a package for which
44    Open Invention Network licensees cross-license their patents.  No patent
45    license is granted, either expressly or impliedly, by designation as an
46    included package.  Should you wish to participate in the Open Invention
47    Network licensing program, please visit www.openinventionnetwork.com
48    <http://www.openinventionnetwork.com>.  */
49 
50 #ifndef _LIBDWFLP_H
51 #define _LIBDWFLP_H	1
52 
53 #ifndef PACKAGE_NAME
54 # include <config.h>
55 #endif
56 #include <libdwfl.h>
57 #include <libebl.h>
58 #include <assert.h>
59 #include <errno.h>
60 #include <stdbool.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 #include "../libdw/libdwP.h"	/* We need its INTDECLs.  */
65 
66 /* gettext helper macros.  */
67 #define _(Str) dgettext ("elfutils", Str)
68 
69 #define DWFL_ERRORS							      \
70   DWFL_ERROR (NOERROR, N_("no error"))					      \
71   DWFL_ERROR (UNKNOWN_ERROR, N_("unknown error"))			      \
72   DWFL_ERROR (NOMEM, N_("out of memory"))				      \
73   DWFL_ERROR (ERRNO, N_("See errno"))					      \
74   DWFL_ERROR (LIBELF, N_("See elf_errno"))				      \
75   DWFL_ERROR (LIBDW, N_("See dwarf_errno"))				      \
76   DWFL_ERROR (LIBEBL, N_("See ebl_errno (XXX missing)"))		      \
77   DWFL_ERROR (UNKNOWN_MACHINE, N_("no support library found for machine"))    \
78   DWFL_ERROR (NOREL, N_("Callbacks missing for ET_REL file"))		      \
79   DWFL_ERROR (BADRELTYPE, N_("Unsupported relocation type"))		      \
80   DWFL_ERROR (BADRELOFF, N_("r_offset is bogus"))			      \
81   DWFL_ERROR (BADSTROFF, N_("offset out of range"))			      \
82   DWFL_ERROR (RELUNDEF, N_("relocation refers to undefined symbol"))	      \
83   DWFL_ERROR (CB, N_("Callback returned failure"))			      \
84   DWFL_ERROR (NO_DWARF, N_("No DWARF information found"))		      \
85   DWFL_ERROR (NO_SYMTAB, N_("No symbol table found"))			      \
86   DWFL_ERROR (NO_PHDR, N_("No ELF program headers"))			      \
87   DWFL_ERROR (OVERLAP, N_("address range overlaps an existing module"))	      \
88   DWFL_ERROR (ADDR_OUTOFRANGE, N_("address out of range"))		      \
89   DWFL_ERROR (NO_MATCH, N_("no matching address range"))		      \
90   DWFL_ERROR (TRUNCATED, N_("image truncated"))				      \
91   DWFL_ERROR (ALREADY_ELF, N_("ELF file opened"))			      \
92   DWFL_ERROR (BADELF, N_("not a valid ELF file"))			      \
93   DWFL_ERROR (WEIRD_TYPE, N_("cannot handle DWARF type description"))
94 
95 #define DWFL_ERROR(name, text) DWFL_E_##name,
96 typedef enum { DWFL_ERRORS DWFL_E_NUM } Dwfl_Error;
97 #undef	DWFL_ERROR
98 
99 #define OTHER_ERROR(name)	((unsigned int) DWFL_E_##name << 16)
100 #define DWFL_E(name, errno)	(OTHER_ERROR (name) | (errno))
101 
102 extern int __libdwfl_canon_error (Dwfl_Error) internal_function;
103 extern void __libdwfl_seterrno (Dwfl_Error) internal_function;
104 
105 struct Dwfl
106 {
107   const Dwfl_Callbacks *callbacks;
108 
109   Dwfl_Module *modulelist;    /* List in order used by full traversals.  */
110 
111   GElf_Addr offline_next_address;
112 
113   GElf_Addr segment_align;	/* Smallest granularity of segments.  */
114 
115   /* Binary search table in three parallel malloc'd arrays.  */
116   size_t lookup_elts;		/* Elements in use.  */
117   size_t lookup_alloc;		/* Elements allococated.  */
118   GElf_Addr *lookup_addr;	/* Start address of segment.  */
119   Dwfl_Module **lookup_module;	/* Module associated with segment, or null.  */
120   int *lookup_segndx;		/* User segment index, or -1.  */
121 
122   /* Cache from last dwfl_report_segment call.  */
123   const void *lookup_tail_ident;
124   GElf_Off lookup_tail_vaddr;
125   GElf_Off lookup_tail_offset;
126   int lookup_tail_ndx;
127 };
128 
129 #define OFFLINE_REDZONE		0x10000
130 
131 struct dwfl_file
132 {
133   char *name;
134   int fd;
135   bool valid;			/* The build ID note has been matched.  */
136   bool relocated;		/* Partial relocation of all sections done.  */
137 
138   Elf *elf;
139   GElf_Addr bias;		/* Actual load address - p_vaddr.  */
140 };
141 
142 struct Dwfl_Module
143 {
144   Dwfl *dwfl;
145   struct Dwfl_Module *next;	/* Link on Dwfl.modulelist.  */
146 
147   void *userdata;
148 
149   char *name;			/* Iterator name for this module.  */
150   GElf_Addr low_addr, high_addr;
151 
152   void *build_id_bits;		/* malloc'd copy of build ID bits.  */
153   GElf_Addr build_id_vaddr;	/* Address where they reside, 0 if unknown.  */
154   int build_id_len;		/* -1 for prior failure, 0 if unset.  */
155 
156   struct dwfl_file main, debug;
157   Ebl *ebl;
158   GElf_Half e_type;		/* GElf_Ehdr.e_type cache.  */
159   Dwfl_Error elferr;		/* Previous failure to open main file.  */
160 
161   struct dwfl_relocation *reloc_info; /* Relocatable sections.  */
162 
163   struct dwfl_file *symfile;	/* Either main or debug.  */
164   Elf_Data *symdata;		/* Data in the ELF symbol table section.  */
165   size_t syments;		/* sh_size / sh_entsize of that section.  */
166   Elf_Data *symstrdata;		/* Data for its string table.  */
167   Elf_Data *symxndxdata;	/* Data in the extended section index table. */
168   Dwfl_Error symerr;		/* Previous failure to load symbols.  */
169 
170   Dwarf *dw;			/* libdw handle for its debugging info.  */
171   Dwfl_Error dwerr;		/* Previous failure to load info.  */
172 
173   /* Known CU's in this module.  */
174   struct dwfl_cu *first_cu, **cu;
175   unsigned int ncu;
176 
177   void *lazy_cu_root;		/* Table indexed by Dwarf_Off of CU.  */
178   unsigned int lazycu;		/* Possible users, deleted when none left.  */
179 
180   struct dwfl_arange *aranges;	/* Mapping of addresses in module to CUs.  */
181   unsigned int naranges;
182 
183   int segment;			/* Index of first segment table entry.  */
184   bool gc;			/* Mark/sweep flag.  */
185 };
186 
187 
188 
189 /* Information cached about each CU in Dwfl_Module.dw.  */
190 struct dwfl_cu
191 {
192   /* This caches libdw information about the CU.  It's also the
193      address passed back to users, so we take advantage of the
194      fact that it's placed first to cast back.  */
195   Dwarf_Die die;
196 
197   Dwfl_Module *mod;		/* Pointer back to containing module.  */
198 
199   struct dwfl_cu *next;		/* CU immediately following in the file.  */
200 
201   struct Dwfl_Lines *lines;
202 };
203 
204 struct Dwfl_Lines
205 {
206   struct dwfl_cu *cu;
207 
208   /* This is what the opaque Dwfl_Line * pointers we pass to users are.
209      We need to recover pointers to our struct dwfl_cu and a record in
210      libdw's Dwarf_Line table.  To minimize the memory used in addition
211      to libdw's Dwarf_Lines buffer, we just point to our own index in
212      this table, and have one pointer back to the CU.  The indices here
213      match those in libdw's Dwarf_CU.lines->info table.  */
214   struct Dwfl_Line
215   {
216     unsigned int idx;		/* My index in the dwfl_cu.lines table.  */
217   } idx[0];
218 };
219 
220 static inline struct dwfl_cu *
dwfl_linecu_inline(const Dwfl_Line * line)221 dwfl_linecu_inline (const Dwfl_Line *line)
222 {
223   const struct Dwfl_Lines *lines = ((const void *) line
224 				    - offsetof (struct Dwfl_Lines,
225 						idx[line->idx]));
226   return lines->cu;
227 }
228 #define dwfl_linecu dwfl_linecu_inline
229 
230 /* This describes a contiguous address range that lies in a single CU.
231    We condense runs of Dwarf_Arange entries for the same CU into this.  */
232 struct dwfl_arange
233 {
234   struct dwfl_cu *cu;
235   size_t arange;		/* Index in Dwarf_Aranges.  */
236 };
237 
238 
239 
240 extern void __libdwfl_module_free (Dwfl_Module *mod) internal_function;
241 
242 
243 /* Process relocations in debugging sections in an ET_REL file.
244    FILE must be opened with ELF_C_READ_MMAP_PRIVATE or ELF_C_READ,
245    to make it possible to relocate the data in place (or ELF_C_RDWR or
246    ELF_C_RDWR_MMAP if you intend to modify the Elf file on disk).  After
247    this, dwarf_begin_elf on FILE will read the relocated data.
248 
249    When DEBUG is false, apply partial relocation to all sections.  */
250 extern Dwfl_Error __libdwfl_relocate (Dwfl_Module *mod, Elf *file, bool debug)
251   internal_function;
252 
253 /* Process (simple) relocations in arbitrary section TSCN of an ET_REL file.
254    RELOCSCN is SHT_REL or SHT_RELA and TSCN is its sh_info target section.  */
255 extern Dwfl_Error __libdwfl_relocate_section (Dwfl_Module *mod, Elf *relocated,
256 					      Elf_Scn *relocscn, Elf_Scn *tscn,
257 					      bool partial)
258   internal_function;
259 
260 /* Adjust *VALUE from section-relative to absolute.
261    MOD->dwfl->callbacks->section_address is called to determine the actual
262    address of a loaded section.  */
263 extern Dwfl_Error __libdwfl_relocate_value (Dwfl_Module *mod, Elf *elf,
264 					    size_t *shstrndx_cache,
265 					    Elf32_Word shndx,
266 					    GElf_Addr *value)
267      internal_function;
268 
269 
270 /* Ensure that MOD->ebl is set up.  */
271 extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
272 
273 /* Iterate through all the CU's in the module.  Start by passing a null
274    LASTCU, and then pass the last *CU returned.  Success return with null
275    *CU no more CUs.  */
276 extern Dwfl_Error __libdwfl_nextcu (Dwfl_Module *mod, struct dwfl_cu *lastcu,
277 				    struct dwfl_cu **cu) internal_function;
278 
279 /* Find the CU by address.  */
280 extern Dwfl_Error __libdwfl_addrcu (Dwfl_Module *mod, Dwarf_Addr addr,
281 				    struct dwfl_cu **cu) internal_function;
282 
283 /* Ensure that CU->lines (and CU->cu->lines) is set up.  */
284 extern Dwfl_Error __libdwfl_cu_getsrclines (struct dwfl_cu *cu)
285   internal_function;
286 
287 /* Look in ELF for an NT_GNU_BUILD_ID note.  If SET is true, store it
288    in MOD and return its length.  If SET is false, instead compare it
289    to that stored in MOD and return 2 if they match, 1 if they do not.
290    Returns -1 for errors, 0 if no note is found.  */
291 extern int __libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
292   internal_function;
293 
294 /* Open a main or debuginfo file by its build ID, returns the fd.  */
295 extern int __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug,
296 				       char **file_name) internal_function;
297 
298 extern uint32_t __libdwfl_crc32 (uint32_t crc, unsigned char *buf, size_t len)
299   attribute_hidden;
300 extern int __libdwfl_crc32_file (int fd, uint32_t *resp) attribute_hidden;
301 
302 
303 /* Meat of dwfl_report_elf, given elf_begin just called.
304    Consumes ELF on success, not on failure.  */
305 extern Dwfl_Module *__libdwfl_report_elf (Dwfl *dwfl, const char *name,
306 					  const char *file_name, int fd,
307 					  Elf *elf, GElf_Addr base)
308   internal_function;
309 
310 /* Meat of dwfl_report_offline.  */
311 extern Dwfl_Module *__libdwfl_report_offline (Dwfl *dwfl, const char *name,
312 					      const char *file_name,
313 					      int fd, bool closefd,
314 					      int (*predicate) (const char *,
315 								const char *))
316   internal_function;
317 
318 
319 /* These are working nicely for --core, but are not ready to be
320    exported interfaces quite yet.  */
321 
322 /* Type of callback function ...
323  */
324 typedef bool Dwfl_Memory_Callback (Dwfl *dwfl, int segndx,
325 				   void **buffer, size_t *buffer_available,
326 				   GElf_Addr vaddr, size_t minread, void *arg);
327 
328 /* Type of callback function ...
329  */
330 typedef bool Dwfl_Module_Callback (Dwfl_Module *mod, void **userdata,
331 				   const char *name, Dwarf_Addr base,
332 				   void **buffer, size_t *buffer_available,
333 				   GElf_Off cost, GElf_Off worthwhile,
334 				   GElf_Off whole, GElf_Off contiguous,
335 				   void *arg, Elf **elfp);
336 
337 /* ...
338  */
339 extern int dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
340 				       Dwfl_Memory_Callback *memory_callback,
341 				       void *memory_callback_arg,
342 				       Dwfl_Module_Callback *read_eagerly,
343 				       void *read_eagerly_arg);
344 
345 /* Report a module for entry in the dynamic linker's struct link_map list.
346    For each link_map entry, if an existing module resides at its address,
347    this just modifies that module's name and suggested file name.  If
348    no such module exists, this calls dwfl_report_elf on the l_name string.
349 
350    If AUXV is not null, it points to AUXV_SIZE bytes of auxiliary vector
351    data as contained in an NT_AUXV note or read from a /proc/pid/auxv
352    file.  When this is available, it guides the search.  If AUXV is null
353    or the memory it points to is not accessible, then this search can
354    only find where to begin if the correct executable file was
355    previously reported and preloaded as with dwfl_report_elf.
356 
357    Returns the number of modules found, or -1 for errors.  */
358 extern int dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
359 				 Dwfl_Memory_Callback *memory_callback,
360 				 void *memory_callback_arg);
361 
362 /* Examine an ET_CORE file and report modules based on its contents.  */
363 extern int dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const GElf_Ehdr *ehdr);
364 
365 
366 /* Avoid PLT entries.  */
367 INTDECL (dwfl_begin)
368 INTDECL (dwfl_errmsg)
369 INTDECL (dwfl_addrmodule)
370 INTDECL (dwfl_addrsegment)
371 INTDECL (dwfl_addrdwarf)
372 INTDECL (dwfl_addrdie)
373 INTDECL (dwfl_core_file_report)
374 INTDECL (dwfl_getmodules)
375 INTDECL (dwfl_module_addrdie)
376 INTDECL (dwfl_module_address_section)
377 INTDECL (dwfl_module_addrsym)
378 INTDECL (dwfl_module_build_id)
379 INTDECL (dwfl_module_getdwarf)
380 INTDECL (dwfl_module_getelf)
381 INTDECL (dwfl_module_getsym)
382 INTDECL (dwfl_module_getsymtab)
383 INTDECL (dwfl_module_getsrc)
384 INTDECL (dwfl_module_report_build_id)
385 INTDECL (dwfl_report_elf)
386 INTDECL (dwfl_report_begin)
387 INTDECL (dwfl_report_begin_add)
388 INTDECL (dwfl_report_module)
389 INTDECL (dwfl_report_segment)
390 INTDECL (dwfl_report_offline)
391 INTDECL (dwfl_report_end)
392 INTDECL (dwfl_build_id_find_elf)
393 INTDECL (dwfl_build_id_find_debuginfo)
394 INTDECL (dwfl_standard_find_debuginfo)
395 INTDECL (dwfl_link_map_report)
396 INTDECL (dwfl_linux_kernel_find_elf)
397 INTDECL (dwfl_linux_kernel_module_section_address)
398 INTDECL (dwfl_linux_proc_report)
399 INTDECL (dwfl_linux_proc_maps_report)
400 INTDECL (dwfl_linux_proc_find_elf)
401 INTDECL (dwfl_linux_kernel_report_kernel)
402 INTDECL (dwfl_linux_kernel_report_modules)
403 INTDECL (dwfl_linux_kernel_report_offline)
404 INTDECL (dwfl_offline_section_address)
405 INTDECL (dwfl_module_relocate_address)
406 
407 /* Leading arguments standard to callbacks passed a Dwfl_Module.  */
408 #define MODCB_ARGS(mod)	(mod), &(mod)->userdata, (mod)->name, (mod)->low_addr
409 #define CBFAIL		(errno ? DWFL_E (ERRNO, errno) : DWFL_E_CB);
410 
411 
412 /* The default used by dwfl_standard_find_debuginfo.  */
413 #define DEFAULT_DEBUGINFO_PATH ":.debug:/usr/lib/debug"
414 
415 
416 #endif	/* libdwflP.h */
417