2007-10-23 Roland McGrath <roland@redhat.com>
[platform/upstream/elfutils.git] / libdwfl / libdwflP.h
1 /* Internal definitions for libdwfl.
2    Copyright (C) 2005, 2006, 2007 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 error) internal_function;
103 extern void __libdwfl_seterrno (Dwfl_Error 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   Dwfl_Module **modules;
112   size_t nmodules;
113
114   GElf_Addr offline_next_address;
115 };
116
117 #define OFFLINE_REDZONE         0x10000
118
119 struct dwfl_file
120 {
121   char *name;
122   int fd;
123   bool valid;                   /* The build ID note has been matched.  */
124   bool relocated;               /* Partial relocation of all sections done.  */
125
126   Elf *elf;
127   GElf_Addr bias;               /* Actual load address - p_vaddr.  */
128 };
129
130 struct Dwfl_Module
131 {
132   Dwfl *dwfl;
133   struct Dwfl_Module *next;     /* Link on Dwfl.modulelist.  */
134
135   void *userdata;
136
137   char *name;                   /* Iterator name for this module.  */
138   GElf_Addr low_addr, high_addr;
139
140   void *build_id_bits;          /* malloc'd copy of build ID bits.  */
141   GElf_Addr build_id_vaddr;     /* Address where they reside, 0 if unknown.  */
142   int build_id_len;             /* -1 for prior failure, 0 if unset.  */
143
144   struct dwfl_file main, debug;
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   Elf_Data *symstrdata;         /* Data for its string table.  */
155   Elf_Data *symxndxdata;        /* Data in the extended section index table. */
156   Dwfl_Error symerr;            /* Previous failure to load symbols.  */
157
158   Dwarf *dw;                    /* libdw handle for its debugging info.  */
159   Dwfl_Error dwerr;             /* Previous failure to load info.  */
160
161   /* Known CU's in this module.  */
162   struct dwfl_cu *first_cu, **cu;
163   unsigned int ncu;
164
165   void *lazy_cu_root;           /* Table indexed by Dwarf_Off of CU.  */
166   unsigned int lazycu;          /* Possible users, deleted when none left.  */
167
168   struct dwfl_arange *aranges;  /* Mapping of addresses in module to CUs.  */
169   unsigned int naranges;
170
171   bool gc;                      /* Mark/sweep flag.  */
172 };
173
174
175
176 /* Information cached about each CU in Dwfl_Module.dw.  */
177 struct dwfl_cu
178 {
179   /* This caches libdw information about the CU.  It's also the
180      address passed back to users, so we take advantage of the
181      fact that it's placed first to cast back.  */
182   Dwarf_Die die;
183
184   Dwfl_Module *mod;             /* Pointer back to containing module.  */
185
186   struct dwfl_cu *next;         /* CU immediately following in the file.  */
187
188   struct Dwfl_Lines *lines;
189 };
190
191 struct Dwfl_Lines
192 {
193   struct dwfl_cu *cu;
194
195   /* This is what the opaque Dwfl_Line * pointers we pass to users are.
196      We need to recover pointers to our struct dwfl_cu and a record in
197      libdw's Dwarf_Line table.  To minimize the memory used in addition
198      to libdw's Dwarf_Lines buffer, we just point to our own index in
199      this table, and have one pointer back to the CU.  The indices here
200      match those in libdw's Dwarf_CU.lines->info table.  */
201   struct Dwfl_Line
202   {
203     unsigned int idx;           /* My index in the dwfl_cu.lines table.  */
204   } idx[0];
205 };
206
207 static inline struct dwfl_cu *
208 dwfl_linecu_inline (const Dwfl_Line *line)
209 {
210   const struct Dwfl_Lines *lines = ((const void *) line
211                                     - offsetof (struct Dwfl_Lines,
212                                                 idx[line->idx]));
213   return lines->cu;
214 }
215 #define dwfl_linecu dwfl_linecu_inline
216
217 /* This describes a contiguous address range that lies in a single CU.
218    We condense runs of Dwarf_Arange entries for the same CU into this.  */
219 struct dwfl_arange
220 {
221   struct dwfl_cu *cu;
222   size_t arange;                /* Index in Dwarf_Aranges.  */
223 };
224
225
226
227 extern void __libdwfl_module_free (Dwfl_Module *mod) internal_function;
228
229
230 /* Process relocations in debugging sections in an ET_REL file.
231    FILE must be opened with ELF_C_READ_MMAP_PRIVATE or ELF_C_READ,
232    to make it possible to relocate the data in place (or ELF_C_RDWR or
233    ELF_C_RDWR_MMAP if you intend to modify the Elf file on disk).  After
234    this, dwarf_begin_elf on FILE will read the relocated data.
235
236    When DEBUG is false, apply partial relocation to all sections.  */
237 extern Dwfl_Error __libdwfl_relocate (Dwfl_Module *mod, Elf *file, bool debug)
238   internal_function;
239
240 /* Process (simple) relocations in arbitrary section TSCN of an ET_REL file.
241    RELOCSCN is SHT_REL or SHT_RELA and TSCN is its sh_info target section.  */
242 extern Dwfl_Error __libdwfl_relocate_section (Dwfl_Module *mod, Elf *relocated,
243                                               Elf_Scn *relocscn, Elf_Scn *tscn,
244                                               bool partial)
245   internal_function;
246
247 /* Adjust *VALUE from section-relative to absolute.
248    MOD->dwfl->callbacks->section_address is called to determine the actual
249    address of a loaded section.  */
250 extern Dwfl_Error __libdwfl_relocate_value (Dwfl_Module *mod, Elf *elf,
251                                             size_t *shstrndx_cache,
252                                             Elf32_Word shndx,
253                                             GElf_Addr *value)
254      internal_function;
255
256
257 /* Ensure that MOD->ebl is set up.  */
258 extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
259
260 /* Iterate through all the CU's in the module.  Start by passing a null
261    LASTCU, and then pass the last *CU returned.  Success return with null
262    *CU no more CUs.  */
263 extern Dwfl_Error __libdwfl_nextcu (Dwfl_Module *mod, struct dwfl_cu *lastcu,
264                                     struct dwfl_cu **cu) internal_function;
265
266 /* Find the CU by address.  */
267 extern Dwfl_Error __libdwfl_addrcu (Dwfl_Module *mod, Dwarf_Addr addr,
268                                     struct dwfl_cu **cu) internal_function;
269
270 /* Ensure that CU->lines (and CU->cu->lines) is set up.  */
271 extern Dwfl_Error __libdwfl_cu_getsrclines (struct dwfl_cu *cu)
272   internal_function;
273
274 /* Look in ELF for an NT_GNU_BUILD_ID note.  If SET is true, store it
275    in MOD and return its length.  If SET is false, instead compare it
276    to that stored in MOD and return 2 if they match, 1 if they do not.
277    Returns -1 for errors, 0 if no note is found.  */
278 extern int __libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
279   internal_function;
280
281 /* Open a main or debuginfo file by its build ID, returns the fd.  */
282 extern int __libdwfl_open_by_build_id (Dwfl_Module *mod, bool debug,
283                                        char **file_name) internal_function;
284
285 extern uint32_t __libdwfl_crc32 (uint32_t crc, unsigned char *buf, size_t len)
286   attribute_hidden;
287 extern int __libdwfl_crc32_file (int fd, uint32_t *resp) attribute_hidden;
288
289
290 /* Meat of dwfl_report_elf, given elf_begin just called.
291    Consumes ELF on success, not on failure.  */
292 extern Dwfl_Module *__libdwfl_report_elf (Dwfl *dwfl, const char *name,
293                                           const char *file_name, int fd,
294                                           Elf *elf, GElf_Addr base)
295   internal_function;
296
297 /* Meat of dwfl_report_offline.  */
298 extern Dwfl_Module *__libdwfl_report_offline (Dwfl *dwfl, const char *name,
299                                               const char *file_name,
300                                               int fd, bool closefd,
301                                               int (*predicate) (const char *,
302                                                                 const char *))
303   internal_function;
304
305
306 /* Avoid PLT entries.  */
307 INTDECL (dwfl_begin)
308 INTDECL (dwfl_errmsg)
309 INTDECL (dwfl_addrmodule)
310 INTDECL (dwfl_addrdwarf)
311 INTDECL (dwfl_addrdie)
312 INTDECL (dwfl_module_addrdie)
313 INTDECL (dwfl_module_addrsym)
314 INTDECL (dwfl_module_build_id)
315 INTDECL (dwfl_module_getdwarf)
316 INTDECL (dwfl_module_getelf)
317 INTDECL (dwfl_module_getsym)
318 INTDECL (dwfl_module_getsymtab)
319 INTDECL (dwfl_module_getsrc)
320 INTDECL (dwfl_module_report_build_id)
321 INTDECL (dwfl_report_elf)
322 INTDECL (dwfl_report_begin)
323 INTDECL (dwfl_report_begin_add)
324 INTDECL (dwfl_report_module)
325 INTDECL (dwfl_report_offline)
326 INTDECL (dwfl_report_end)
327 INTDECL (dwfl_build_id_find_elf)
328 INTDECL (dwfl_build_id_find_debuginfo)
329 INTDECL (dwfl_standard_find_debuginfo)
330 INTDECL (dwfl_linux_kernel_find_elf)
331 INTDECL (dwfl_linux_kernel_module_section_address)
332 INTDECL (dwfl_linux_proc_report)
333 INTDECL (dwfl_linux_proc_maps_report)
334 INTDECL (dwfl_linux_proc_find_elf)
335 INTDECL (dwfl_linux_kernel_report_kernel)
336 INTDECL (dwfl_linux_kernel_report_modules)
337 INTDECL (dwfl_linux_kernel_report_offline)
338 INTDECL (dwfl_offline_section_address)
339 INTDECL (dwfl_module_relocate_address)
340
341 /* Leading arguments standard to callbacks passed a Dwfl_Module.  */
342 #define MODCB_ARGS(mod) (mod), &(mod)->userdata, (mod)->name, (mod)->low_addr
343 #define CBFAIL          (errno ? DWFL_E (ERRNO, errno) : DWFL_E_CB);
344
345
346 /* The default used by dwfl_standard_find_debuginfo.  */
347 #define DEFAULT_DEBUGINFO_PATH ":.debug:/usr/lib/debug"
348
349
350 #endif  /* libdwflP.h */