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.
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.
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.
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.
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.
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>. */
66 # define crc32 loser_crc32
73 static const char dwarf_scnnames[IDX_last][17] =
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"
87 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
91 check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
96 /* Get the section header data. */
97 shdr = gelf_getshdr (scn, &shdr_mem);
99 /* This should never happen. If it does something is
100 wrong in the libelf library. */
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))
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. */
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,
125 /* The section name must be valid. Otherwise is the ELF file
127 __libdw_free_zdata (result);
128 __libdw_seterrno (DWARF_E_INVALID_ELF);
134 /* Recognize the various sections. Most names start with .debug_. */
136 for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
137 if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
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. */
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;
153 else if (scnname[0] == '.' && scnname[1] == 'z'
154 && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0)
156 /* A compressed section. */
158 if (unlikely (result->sectiondata[cnt] != NULL))
159 /* A section appears twice. That's bad. We ignore the section. */
162 /* Get the section data. */
163 Elf_Data *data = elf_getdata (scn, NULL);
164 if (data != NULL && data->d_size != 0)
166 /* There is a 12-byte header of "ZLIB" followed by
167 an 8-byte big-endian size. */
169 if (unlikely (data->d_size < 4 + 8)
170 || unlikely (memcmp (data->d_buf, "ZLIB", 4) != 0))
174 memcpy (&size, data->d_buf + 4, sizeof size);
175 size = be64toh (size);
177 Elf_Data *zdata = malloc (sizeof (Elf_Data) + size);
178 if (unlikely (zdata == NULL))
181 zdata->d_buf = &zdata[1];
182 zdata->d_type = ELF_T_BYTE;
183 zdata->d_version = EV_CURRENT;
184 zdata->d_size = size;
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
195 int zrc = inflateInit (&z);
196 while (z.avail_in > 0 && likely (zrc == Z_OK))
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))
205 zrc = inflateReset (&z);
207 if (likely (zrc == Z_OK))
208 zrc = inflateEnd (&z);
210 if (unlikely (zrc != Z_OK) || unlikely (z.avail_out != 0))
214 result->sectiondata[cnt] = zdata;
215 result->sectiondata_gzip_mask |= 1U << cnt;
227 /* Check whether all the necessary DWARF information is available. */
229 valid_p (Dwarf *result)
231 /* We looked at all the sections. Now determine whether all the
232 sections with debugging information we need are there.
234 XXX Which sections are absolutely necessary? Add tests if
235 necessary. For now we require only .debug_info. Hopefully this
237 if (likely (result != NULL)
238 && unlikely (result->sectiondata[IDX_debug_info] == NULL))
240 __libdw_free_zdata (result);
241 __libdw_seterrno (DWARF_E_NO_DWARF);
251 global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr)
255 while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
256 result = check_section (result, ehdr, scn, false);
258 return valid_p (result);
263 scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp)
265 /* SCNGRP is the section descriptor for a section group which might
266 contain debug sections. */
267 Elf_Data *data = elf_getdata (scngrp, NULL);
270 /* We cannot read the section content. Fail! */
271 __libdw_free_zdata (result);
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;
280 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
282 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
285 /* A section group refers to a non-existing section. Should
287 __libdw_free_zdata (result);
288 __libdw_seterrno (DWARF_E_INVALID_ELF);
293 result = check_section (result, ehdr, scn, true);
298 return valid_p (result);
303 dwarf_begin_elf (elf, cmd, scngrp)
311 /* Get the ELF header of the file. We need various pieces of
312 information from it. */
313 ehdr = gelf_getehdr (elf, &ehdr_mem);
316 if (elf_kind (elf) != ELF_K_ELF)
317 __libdw_seterrno (DWARF_E_NOELF);
319 __libdw_seterrno (DWARF_E_GETEHDR_ERROR);
325 /* Default memory allocation size. */
326 size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
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))
334 __libdw_seterrno (DWARF_E_NOMEM);
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;
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;
354 if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
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. */
362 return global_read (result, elf, ehdr);
364 return scngrp_read (result, elf, ehdr, scngrp);
366 else if (cmd == DWARF_C_WRITE)
368 __libdw_seterrno (DWARF_E_UNIMPL);
373 __libdw_seterrno (DWARF_E_INVALID_CMD);
377 INTDEF(dwarf_begin_elf)