1 /* Create descriptor from ELF descriptor for processing file.
2 Copyright (C) 2002-2011 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
9
10 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
42
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
50
51 #ifdef HAVE_CONFIG_H
52 # include <config.h>
53 #endif
54
55 #include <stdbool.h>
56 #include <stddef.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <sys/stat.h>
61
62 #include "libdwP.h"
63
64 #if USE_ZLIB
65 # include <endian.h>
66 # define crc32 loser_crc32
67 # include <zlib.h>
68 # undef crc32
69 #endif
70
71
72 /* Section names. */
73 static const char dwarf_scnnames[IDX_last][17] =
74 {
75 [IDX_debug_info] = ".debug_info",
76 [IDX_debug_types] = ".debug_types",
77 [IDX_debug_abbrev] = ".debug_abbrev",
78 [IDX_debug_aranges] = ".debug_aranges",
79 [IDX_debug_line] = ".debug_line",
80 [IDX_debug_frame] = ".debug_frame",
81 [IDX_debug_loc] = ".debug_loc",
82 [IDX_debug_pubnames] = ".debug_pubnames",
83 [IDX_debug_str] = ".debug_str",
84 [IDX_debug_macinfo] = ".debug_macinfo",
85 [IDX_debug_ranges] = ".debug_ranges"
86 };
87 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
88
89
90 static Dwarf *
check_section(Dwarf * result,GElf_Ehdr * ehdr,Elf_Scn * scn,bool inscngrp)91 check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
92 {
93 GElf_Shdr shdr_mem;
94 GElf_Shdr *shdr;
95
96 /* Get the section header data. */
97 shdr = gelf_getshdr (scn, &shdr_mem);
98 if (shdr == NULL)
99 /* This should never happen. If it does something is
100 wrong in the libelf library. */
101 abort ();
102
103 /* Ignore any SHT_NOBITS sections. Debugging sections should not
104 have been stripped, but in case of a corrupt file we won't try
105 to look at the missing data. */
106 if (unlikely (shdr->sh_type == SHT_NOBITS))
107 return result;
108
109 /* Make sure the section is part of a section group only iff we
110 really need it. If we are looking for the global (= non-section
111 group debug info) we have to ignore all the info in section
112 groups. If we are looking into a section group we cannot look at
113 a section which isn't part of the section group. */
114 if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
115 /* Ignore the section. */
116 return result;
117
118
119 /* We recognize the DWARF section by their names. This is not very
120 safe and stable but the best we can do. */
121 const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx,
122 shdr->sh_name);
123 if (scnname == NULL)
124 {
125 /* The section name must be valid. Otherwise is the ELF file
126 invalid. */
127 __libdw_free_zdata (result);
128 __libdw_seterrno (DWARF_E_INVALID_ELF);
129 free (result);
130 return NULL;
131 }
132
133
134 /* Recognize the various sections. Most names start with .debug_. */
135 size_t cnt;
136 for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
137 if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
138 {
139 /* Found it. Remember where the data is. */
140 if (unlikely (result->sectiondata[cnt] != NULL))
141 /* A section appears twice. That's bad. We ignore the section. */
142 break;
143
144 /* Get the section data. */
145 Elf_Data *data = elf_getdata (scn, NULL);
146 if (data != NULL && data->d_size != 0)
147 /* Yep, there is actually data available. */
148 result->sectiondata[cnt] = data;
149
150 break;
151 }
152 #if USE_ZLIB
153 else if (scnname[0] == '.' && scnname[1] == 'z'
154 && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0)
155 {
156 /* A compressed section. */
157
158 if (unlikely (result->sectiondata[cnt] != NULL))
159 /* A section appears twice. That's bad. We ignore the section. */
160 break;
161
162 /* Get the section data. */
163 Elf_Data *data = elf_getdata (scn, NULL);
164 if (data != NULL && data->d_size != 0)
165 {
166 /* There is a 12-byte header of "ZLIB" followed by
167 an 8-byte big-endian size. */
168
169 if (unlikely (data->d_size < 4 + 8)
170 || unlikely (memcmp (data->d_buf, "ZLIB", 4) != 0))
171 break;
172
173 uint64_t size;
174 memcpy (&size, data->d_buf + 4, sizeof size);
175 size = be64toh (size);
176
177 Elf_Data *zdata = malloc (sizeof (Elf_Data) + size);
178 if (unlikely (zdata == NULL))
179 break;
180
181 zdata->d_buf = &zdata[1];
182 zdata->d_type = ELF_T_BYTE;
183 zdata->d_version = EV_CURRENT;
184 zdata->d_size = size;
185 zdata->d_off = 0;
186 zdata->d_align = 1;
187
188 z_stream z =
189 {
190 .next_in = data->d_buf + 4 + 8,
191 .avail_in = data->d_size - 4 - 8,
192 .next_out = zdata->d_buf,
193 .avail_out = zdata->d_size
194 };
195 int zrc = inflateInit (&z);
196 while (z.avail_in > 0 && likely (zrc == Z_OK))
197 {
198 z.next_out = zdata->d_buf + (zdata->d_size - z.avail_out);
199 zrc = inflate (&z, Z_FINISH);
200 if (unlikely (zrc != Z_STREAM_END))
201 {
202 zrc = Z_DATA_ERROR;
203 break;
204 }
205 zrc = inflateReset (&z);
206 }
207 if (likely (zrc == Z_OK))
208 zrc = inflateEnd (&z);
209
210 if (unlikely (zrc != Z_OK) || unlikely (z.avail_out != 0))
211 free (zdata);
212 else
213 {
214 result->sectiondata[cnt] = zdata;
215 result->sectiondata_gzip_mask |= 1U << cnt;
216 }
217 }
218
219 break;
220 }
221 #endif
222
223 return result;
224 }
225
226
227 /* Check whether all the necessary DWARF information is available. */
228 static Dwarf *
valid_p(Dwarf * result)229 valid_p (Dwarf *result)
230 {
231 /* We looked at all the sections. Now determine whether all the
232 sections with debugging information we need are there.
233
234 XXX Which sections are absolutely necessary? Add tests if
235 necessary. For now we require only .debug_info. Hopefully this
236 is correct. */
237 if (likely (result != NULL)
238 && unlikely (result->sectiondata[IDX_debug_info] == NULL))
239 {
240 __libdw_free_zdata (result);
241 __libdw_seterrno (DWARF_E_NO_DWARF);
242 free (result);
243 result = NULL;
244 }
245
246 return result;
247 }
248
249
250 static Dwarf *
global_read(Dwarf * result,Elf * elf,GElf_Ehdr * ehdr)251 global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr)
252 {
253 Elf_Scn *scn = NULL;
254
255 while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
256 result = check_section (result, ehdr, scn, false);
257
258 return valid_p (result);
259 }
260
261
262 static Dwarf *
scngrp_read(Dwarf * result,Elf * elf,GElf_Ehdr * ehdr,Elf_Scn * scngrp)263 scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp)
264 {
265 /* SCNGRP is the section descriptor for a section group which might
266 contain debug sections. */
267 Elf_Data *data = elf_getdata (scngrp, NULL);
268 if (data == NULL)
269 {
270 /* We cannot read the section content. Fail! */
271 __libdw_free_zdata (result);
272 free (result);
273 return NULL;
274 }
275
276 /* The content of the section is a number of 32-bit words which
277 represent section indices. The first word is a flag word. */
278 Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
279 size_t cnt;
280 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
281 {
282 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
283 if (scn == NULL)
284 {
285 /* A section group refers to a non-existing section. Should
286 never happen. */
287 __libdw_free_zdata (result);
288 __libdw_seterrno (DWARF_E_INVALID_ELF);
289 free (result);
290 return NULL;
291 }
292
293 result = check_section (result, ehdr, scn, true);
294 if (result == NULL)
295 break;
296 }
297
298 return valid_p (result);
299 }
300
301
302 Dwarf *
dwarf_begin_elf(elf,cmd,scngrp)303 dwarf_begin_elf (elf, cmd, scngrp)
304 Elf *elf;
305 Dwarf_Cmd cmd;
306 Elf_Scn *scngrp;
307 {
308 GElf_Ehdr *ehdr;
309 GElf_Ehdr ehdr_mem;
310
311 /* Get the ELF header of the file. We need various pieces of
312 information from it. */
313 ehdr = gelf_getehdr (elf, &ehdr_mem);
314 if (ehdr == NULL)
315 {
316 if (elf_kind (elf) != ELF_K_ELF)
317 __libdw_seterrno (DWARF_E_NOELF);
318 else
319 __libdw_seterrno (DWARF_E_GETEHDR_ERROR);
320
321 return NULL;
322 }
323
324
325 /* Default memory allocation size. */
326 size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
327
328 /* Allocate the data structure. */
329 Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size);
330 if (unlikely (result == NULL)
331 || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
332 {
333 free (result);
334 __libdw_seterrno (DWARF_E_NOMEM);
335 return NULL;
336 }
337
338 /* Fill in some values. */
339 if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
340 || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
341 result->other_byte_order = true;
342
343 result->elf = elf;
344
345 /* Initialize the memory handling. */
346 result->mem_default_size = mem_default_size;
347 result->oom_handler = __libdw_oom;
348 result->mem_tail = (struct libdw_memblock *) (result + 1);
349 result->mem_tail->size = (result->mem_default_size
350 - offsetof (struct libdw_memblock, mem));
351 result->mem_tail->remaining = result->mem_tail->size;
352 result->mem_tail->prev = NULL;
353
354 if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
355 {
356 /* If the caller provides a section group we get the DWARF
357 sections only from this setion group. Otherwise we search
358 for the first section with the required name. Further
359 sections with the name are ignored. The DWARF specification
360 does not really say this is allowed. */
361 if (scngrp == NULL)
362 return global_read (result, elf, ehdr);
363 else
364 return scngrp_read (result, elf, ehdr, scngrp);
365 }
366 else if (cmd == DWARF_C_WRITE)
367 {
368 __libdw_seterrno (DWARF_E_UNIMPL);
369 free (result);
370 return NULL;
371 }
372
373 __libdw_seterrno (DWARF_E_INVALID_CMD);
374 free (result);
375 return NULL;
376 }
377 INTDEF(dwarf_begin_elf)
378