1 /* Create descriptor from ELF descriptor for processing file.
2 Copyright (C) 2002-2011, 2014, 2015 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
41 #include <sys/types.h>
49 static const char dwarf_scnnames[IDX_last][18] =
51 [IDX_debug_info] = ".debug_info",
52 [IDX_debug_types] = ".debug_types",
53 [IDX_debug_abbrev] = ".debug_abbrev",
54 [IDX_debug_aranges] = ".debug_aranges",
55 [IDX_debug_line] = ".debug_line",
56 [IDX_debug_frame] = ".debug_frame",
57 [IDX_debug_loc] = ".debug_loc",
58 [IDX_debug_pubnames] = ".debug_pubnames",
59 [IDX_debug_str] = ".debug_str",
60 [IDX_debug_macinfo] = ".debug_macinfo",
61 [IDX_debug_macro] = ".debug_macro",
62 [IDX_debug_ranges] = ".debug_ranges",
63 [IDX_gnu_debugaltlink] = ".gnu_debugaltlink"
65 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
68 check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
73 /* Get the section header data. */
74 shdr = gelf_getshdr (scn, &shdr_mem);
76 /* We may read /proc/PID/mem with only program headers mapped and section
77 headers out of the mapped pages. */
80 /* Ignore any SHT_NOBITS sections. Debugging sections should not
81 have been stripped, but in case of a corrupt file we won't try
82 to look at the missing data. */
83 if (unlikely (shdr->sh_type == SHT_NOBITS))
86 /* Make sure the section is part of a section group only iff we
87 really need it. If we are looking for the global (= non-section
88 group debug info) we have to ignore all the info in section
89 groups. If we are looking into a section group we cannot look at
90 a section which isn't part of the section group. */
91 if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
92 /* Ignore the section. */
96 /* We recognize the DWARF section by their names. This is not very
97 safe and stable but the best we can do. */
98 const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx,
102 /* The section name must be valid. Otherwise is the ELF file
105 Dwarf_Sig8_Hash_free (&result->sig8_hash);
106 __libdw_seterrno (DWARF_E_INVALID_ELF);
111 /* Recognize the various sections. Most names start with .debug_. */
113 bool gnu_compressed = false;
114 for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
115 if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
117 else if (scnname[0] == '.' && scnname[1] == 'z'
118 && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0)
120 gnu_compressed = true;
124 if (cnt >= ndwarf_scnnames)
125 /* Not a debug section; ignore it. */
128 if (unlikely (result->sectiondata[cnt] != NULL))
129 /* A section appears twice. That's bad. We ignore the section. */
132 /* We cannot know whether or not a GNU compressed section has already
133 been uncompressed or not, so ignore any errors. */
135 elf_compress_gnu (scn, 0, 0);
137 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
139 if (elf_compress (scn, 0, 0) < 0)
141 /* If we failed to decompress the section and it's the
142 debug_info section, then fail with specific error rather
143 than the generic NO_DWARF. Without debug_info we can't do
144 anything (see also valid_p()). */
145 if (cnt == IDX_debug_info)
147 Dwarf_Sig8_Hash_free (&result->sig8_hash);
148 __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
156 /* Get the section data. */
157 Elf_Data *data = elf_getdata (scn, NULL);
161 if (data->d_buf == NULL || data->d_size == 0)
162 /* No data actually available, ignore it. */
165 /* We can now read the section data into results. */
166 result->sectiondata[cnt] = data;
172 /* Check whether all the necessary DWARF information is available. */
174 valid_p (Dwarf *result)
176 /* We looked at all the sections. Now determine whether all the
177 sections with debugging information we need are there.
179 XXX Which sections are absolutely necessary? Add tests if
180 necessary. For now we require only .debug_info. Hopefully this
182 if (likely (result != NULL)
183 && unlikely (result->sectiondata[IDX_debug_info] == NULL))
185 Dwarf_Sig8_Hash_free (&result->sig8_hash);
186 __libdw_seterrno (DWARF_E_NO_DWARF);
191 if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL)
193 result->fake_loc_cu = (Dwarf_CU *) calloc (1, sizeof (Dwarf_CU));
194 if (unlikely (result->fake_loc_cu == NULL))
196 Dwarf_Sig8_Hash_free (&result->sig8_hash);
197 __libdw_seterrno (DWARF_E_NOMEM);
203 result->fake_loc_cu->dbg = result;
204 result->fake_loc_cu->startp
205 = result->sectiondata[IDX_debug_loc]->d_buf;
206 result->fake_loc_cu->endp
207 = (result->sectiondata[IDX_debug_loc]->d_buf
208 + result->sectiondata[IDX_debug_loc]->d_size);
217 global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr)
221 while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
222 result = check_section (result, ehdr, scn, false);
224 return valid_p (result);
229 scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp)
232 GElf_Shdr *shdr = gelf_getshdr (scngrp, &shdr_mem);
235 Dwarf_Sig8_Hash_free (&result->sig8_hash);
236 __libdw_seterrno (DWARF_E_INVALID_ELF);
241 if ((shdr->sh_flags & SHF_COMPRESSED) != 0
242 && elf_compress (scngrp, 0, 0) < 0)
244 Dwarf_Sig8_Hash_free (&result->sig8_hash);
245 __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
250 /* SCNGRP is the section descriptor for a section group which might
251 contain debug sections. */
252 Elf_Data *data = elf_getdata (scngrp, NULL);
255 /* We cannot read the section content. Fail! */
256 Dwarf_Sig8_Hash_free (&result->sig8_hash);
261 /* The content of the section is a number of 32-bit words which
262 represent section indices. The first word is a flag word. */
263 Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
265 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
267 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
270 /* A section group refers to a non-existing section. Should
272 Dwarf_Sig8_Hash_free (&result->sig8_hash);
273 __libdw_seterrno (DWARF_E_INVALID_ELF);
278 result = check_section (result, ehdr, scn, true);
283 return valid_p (result);
288 dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
293 /* Get the ELF header of the file. We need various pieces of
294 information from it. */
295 ehdr = gelf_getehdr (elf, &ehdr_mem);
298 if (elf_kind (elf) != ELF_K_ELF)
299 __libdw_seterrno (DWARF_E_NOELF);
301 __libdw_seterrno (DWARF_E_GETEHDR_ERROR);
307 /* Default memory allocation size. */
308 size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
309 assert (sizeof (struct Dwarf) < mem_default_size);
311 /* Allocate the data structure. */
312 Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size);
313 if (unlikely (result == NULL)
314 || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
317 __libdw_seterrno (DWARF_E_NOMEM);
321 /* Fill in some values. */
322 if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
323 || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
324 result->other_byte_order = true;
328 /* Initialize the memory handling. */
329 result->mem_default_size = mem_default_size;
330 result->oom_handler = __libdw_oom;
331 result->mem_tail = (struct libdw_memblock *) (result + 1);
332 result->mem_tail->size = (result->mem_default_size
333 - offsetof (struct libdw_memblock, mem));
334 result->mem_tail->remaining = result->mem_tail->size;
335 result->mem_tail->prev = NULL;
337 if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
339 /* If the caller provides a section group we get the DWARF
340 sections only from this setion group. Otherwise we search
341 for the first section with the required name. Further
342 sections with the name are ignored. The DWARF specification
343 does not really say this is allowed. */
345 return global_read (result, elf, ehdr);
347 return scngrp_read (result, elf, ehdr, scngrp);
349 else if (cmd == DWARF_C_WRITE)
351 Dwarf_Sig8_Hash_free (&result->sig8_hash);
352 __libdw_seterrno (DWARF_E_UNIMPL);
357 Dwarf_Sig8_Hash_free (&result->sig8_hash);
358 __libdw_seterrno (DWARF_E_INVALID_CMD);
362 INTDEF(dwarf_begin_elf)