propagate from branch 'com.redhat.elfutils.roland.pending' (head 1ac619debea0e3ecfd27...
[platform/upstream/elfutils.git] / libdwfl / libdwflP.h
1 /* Internal definitions for libdwfl.
2    Copyright (C) 2005, 2006 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
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 (BADELF, N_("not a valid ELF file"))                             \
92   DWFL_ERROR (WEIRD_TYPE, N_("cannot handle DWARF type description"))
93
94 #define DWFL_ERROR(name, text) DWFL_E_##name,
95 typedef enum { DWFL_ERRORS DWFL_E_NUM } Dwfl_Error;
96 #undef  DWFL_ERROR
97
98 #define OTHER_ERROR(name)       ((unsigned int) DWFL_E_##name << 16)
99 #define DWFL_E(name, errno)     (OTHER_ERROR (name) | (errno))
100
101 extern int __libdwfl_canon_error (Dwfl_Error error) internal_function;
102 extern void __libdwfl_seterrno (Dwfl_Error error) internal_function;
103
104 struct Dwfl
105 {
106   const Dwfl_Callbacks *callbacks;
107
108   Dwfl_Module *modulelist;    /* List in order used by full traversals.  */
109
110   Dwfl_Module **modules;
111   size_t nmodules;
112
113   GElf_Addr offline_next_address;
114 };
115
116 #define OFFLINE_REDZONE         0x10000
117
118 struct dwfl_file
119 {
120   char *name;
121   int fd;
122
123   Elf *elf;
124   GElf_Addr bias;               /* Actual load address - p_vaddr.  */
125 };
126
127 struct Dwfl_Module
128 {
129   Dwfl *dwfl;
130   struct Dwfl_Module *next;     /* Link on Dwfl.modulelist.  */
131
132   void *userdata;
133
134   char *name;                   /* Iterator name for this module.  */
135   GElf_Addr low_addr, high_addr;
136
137   struct dwfl_file main, debug;
138   Ebl *ebl;
139   GElf_Half e_type;             /* GElf_Ehdr.e_type cache.  */
140   Dwfl_Error elferr;            /* Previous failure to open main file.  */
141
142   struct dwfl_relocation *reloc_info; /* Relocatable sections.  */
143
144   struct dwfl_file *symfile;    /* Either main or debug.  */
145   Elf_Data *symdata;            /* Data in the ELF symbol table section.  */
146   size_t syments;               /* sh_size / sh_entsize of that section.  */
147   Elf_Data *symstrdata;         /* Data for its string table.  */
148   Elf_Data *symxndxdata;        /* Data in the extended section index table. */
149   Dwfl_Error symerr;            /* Previous failure to load symbols.  */
150
151   Dwarf *dw;                    /* libdw handle for its debugging info.  */
152   Dwfl_Error dwerr;             /* Previous failure to load info.  */
153
154   /* Known CU's in this module.  */
155   struct dwfl_cu *first_cu, **cu;
156   unsigned int ncu;
157
158   void *lazy_cu_root;           /* Table indexed by Dwarf_Off of CU.  */
159   unsigned int lazycu;          /* Possible users, deleted when none left.  */
160
161   struct dwfl_arange *aranges;  /* Mapping of addresses in module to CUs.  */
162   unsigned int naranges;
163
164   bool gc;                      /* Mark/sweep flag.  */
165 };
166
167
168
169 /* Information cached about each CU in Dwfl_Module.dw.  */
170 struct dwfl_cu
171 {
172   /* This caches libdw information about the CU.  It's also the
173      address passed back to users, so we take advantage of the
174      fact that it's placed first to cast back.  */
175   Dwarf_Die die;
176
177   Dwfl_Module *mod;             /* Pointer back to containing module.  */
178
179   struct dwfl_cu *next;         /* CU immediately following in the file.  */
180
181   struct Dwfl_Lines *lines;
182 };
183
184 struct Dwfl_Lines
185 {
186   struct dwfl_cu *cu;
187
188   /* This is what the opaque Dwfl_Line * pointers we pass to users are.
189      We need to recover pointers to our struct dwfl_cu and a record in
190      libdw's Dwarf_Line table.  To minimize the memory used in addition
191      to libdw's Dwarf_Lines buffer, we just point to our own index in
192      this table, and have one pointer back to the CU.  The indices here
193      match those in libdw's Dwarf_CU.lines->info table.  */
194   struct Dwfl_Line
195   {
196     unsigned int idx;           /* My index in the dwfl_cu.lines table.  */
197   } idx[0];
198 };
199
200 static inline struct dwfl_cu *
201 dwfl_linecu_inline (const Dwfl_Line *line)
202 {
203   const struct Dwfl_Lines *lines = ((const void *) line
204                                     - offsetof (struct Dwfl_Lines,
205                                                 idx[line->idx]));
206   return lines->cu;
207 }
208 #define dwfl_linecu dwfl_linecu_inline
209
210 /* This describes a contiguous address range that lies in a single CU.
211    We condense runs of Dwarf_Arange entries for the same CU into this.  */
212 struct dwfl_arange
213 {
214   struct dwfl_cu *cu;
215   size_t arange;                /* Index in Dwarf_Aranges.  */
216 };
217
218
219
220 extern void __libdwfl_module_free (Dwfl_Module *mod) internal_function;
221
222
223 /* Process relocations in debugging sections in an ET_REL file.
224    DEBUGFILE must be opened with ELF_C_READ_MMAP_PRIVATE or ELF_C_READ,
225    to make it possible to relocate the data in place (or ELF_C_RDWR or
226    ELF_C_RDWR_MMAP if you intend to modify the Elf file on disk).  After
227    this, dwarf_begin_elf on DEBUGFILE will read the relocated data.  */
228 extern Dwfl_Error __libdwfl_relocate (Dwfl_Module *mod, Elf *debugfile)
229   internal_function;
230
231 /* Adjust *VALUE from section-relative to absolute.
232    MOD->dwfl->callbacks->section_address is called to determine the actual
233    address of a loaded section.  */
234 extern Dwfl_Error __libdwfl_relocate_value (Dwfl_Module *mod,
235                                             size_t m_shstrndx,
236                                             Elf32_Word shndx,
237                                             GElf_Addr *value)
238      internal_function;
239
240
241 /* Ensure that MOD->ebl is set up.  */
242 extern Dwfl_Error __libdwfl_module_getebl (Dwfl_Module *mod) internal_function;
243
244 /* Iterate through all the CU's in the module.  Start by passing a null
245    LASTCU, and then pass the last *CU returned.  Success return with null
246    *CU no more CUs.  */
247 extern Dwfl_Error __libdwfl_nextcu (Dwfl_Module *mod, struct dwfl_cu *lastcu,
248                                     struct dwfl_cu **cu) internal_function;
249
250 /* Find the CU by address.  */
251 extern Dwfl_Error __libdwfl_addrcu (Dwfl_Module *mod, Dwarf_Addr addr,
252                                     struct dwfl_cu **cu) internal_function;
253
254 /* Ensure that CU->lines (and CU->cu->lines) is set up.  */
255 extern Dwfl_Error __libdwfl_cu_getsrclines (struct dwfl_cu *cu)
256      internal_function;
257
258
259 extern uint32_t __libdwfl_crc32 (uint32_t crc, unsigned char *buf, size_t len)
260      attribute_hidden;
261 extern int __libdwfl_crc32_file (int fd, uint32_t *resp) attribute_hidden;
262
263
264
265 /* Avoid PLT entries.  */
266 INTDECL (dwfl_begin)
267 INTDECL (dwfl_errmsg)
268 INTDECL (dwfl_addrmodule)
269 INTDECL (dwfl_addrdwarf)
270 INTDECL (dwfl_addrdie)
271 INTDECL (dwfl_module_addrdie)
272 INTDECL (dwfl_module_getdwarf)
273 INTDECL (dwfl_module_getelf)
274 INTDECL (dwfl_module_getsym)
275 INTDECL (dwfl_module_getsymtab)
276 INTDECL (dwfl_module_getsrc)
277 INTDECL (dwfl_report_elf)
278 INTDECL (dwfl_report_begin)
279 INTDECL (dwfl_report_module)
280 INTDECL (dwfl_report_offline)
281 INTDECL (dwfl_report_end)
282 INTDECL (dwfl_standard_find_debuginfo)
283 INTDECL (dwfl_linux_kernel_find_elf)
284 INTDECL (dwfl_linux_kernel_module_section_address)
285 INTDECL (dwfl_linux_proc_report)
286 INTDECL (dwfl_linux_proc_maps_report)
287 INTDECL (dwfl_linux_proc_find_elf)
288 INTDECL (dwfl_linux_kernel_report_kernel)
289 INTDECL (dwfl_linux_kernel_report_modules)
290 INTDECL (dwfl_linux_kernel_report_offline)
291 INTDECL (dwfl_offline_section_address)
292 INTDECL (dwfl_module_relocate_address)
293
294 /* Leading arguments standard to callbacks passed a Dwfl_Module.  */
295 #define MODCB_ARGS(mod) (mod), &(mod)->userdata, (mod)->name, (mod)->low_addr
296 #define CBFAIL          (errno ? DWFL_E (ERRNO, errno) : DWFL_E_CB);
297
298
299 #endif  /* libdwflP.h */