1 /* Create descriptor from ELF descriptor for processing file.
2 Copyright (C) 2002-2011 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/>. */
42 #include <sys/types.h>
50 # define crc32 loser_crc32
57 static const char dwarf_scnnames[IDX_last][17] =
59 [IDX_debug_info] = ".debug_info",
60 [IDX_debug_types] = ".debug_types",
61 [IDX_debug_abbrev] = ".debug_abbrev",
62 [IDX_debug_aranges] = ".debug_aranges",
63 [IDX_debug_line] = ".debug_line",
64 [IDX_debug_frame] = ".debug_frame",
65 [IDX_debug_loc] = ".debug_loc",
66 [IDX_debug_pubnames] = ".debug_pubnames",
67 [IDX_debug_str] = ".debug_str",
68 [IDX_debug_macinfo] = ".debug_macinfo",
69 [IDX_debug_macro] = ".debug_macro",
70 [IDX_debug_ranges] = ".debug_ranges"
72 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
76 __check_build_id (Dwarf *dw, const uint8_t *build_id, const size_t id_len)
82 Elf_Scn *scn = elf_nextscn (elf, NULL);
89 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
90 if (likely (shdr != NULL) && shdr->sh_type == SHT_NOTE)
96 Elf_Data *data = elf_getdata (scn, NULL);
97 while ((pos = gelf_getnote (data, pos, &nhdr, &name_pos,
99 if (nhdr.n_type == NT_GNU_BUILD_ID
100 && nhdr.n_namesz == sizeof "GNU"
101 && ! memcmp (data->d_buf + name_pos, "GNU", sizeof "GNU"))
102 return (nhdr.n_descsz == id_len
103 && ! memcmp (data->d_buf + desc_pos,
104 build_id, id_len)) ? 0 : 1;
107 while ((scn = elf_nextscn (elf, scn)) != NULL);
112 /* Try to open an debug alt link by name, checking build_id.
113 Marks free_alt on success, return NULL on failure. */
115 try_debugaltlink (Dwarf *result, const char *try_name,
116 const uint8_t *build_id, const size_t id_len)
118 int fd = open (try_name, O_RDONLY);
121 result->alt_dwarf = INTUSE (dwarf_begin) (fd, DWARF_C_READ);
122 if (result->alt_dwarf != NULL)
124 Elf *elf = result->alt_dwarf->elf;
125 if (__check_build_id (result->alt_dwarf, build_id, id_len) == 0
126 && elf_cntl (elf, ELF_C_FDREAD) == 0)
129 result->free_alt = 1;
132 INTUSE (dwarf_end) (result->alt_dwarf);
139 /* For dwz multifile support, ignore if it looks wrong. */
141 open_debugaltlink (Dwarf *result, const char *alt_name,
142 const uint8_t *build_id, const size_t id_len)
144 /* First try the name itself, it is either an absolute path or
145 a relative one. Sadly we don't know relative from where at
147 if (try_debugaltlink (result, alt_name, build_id, id_len) != NULL)
150 /* Lets try based on the build-id. This is somewhat distro specific,
151 we are following the Fedora implementation described at
152 https://fedoraproject.org/wiki/Releases/FeatureBuildId#Find_files_by_build_ID
154 #define DEBUG_PREFIX "/usr/lib/debug/.build-id/"
155 #define PREFIX_LEN sizeof (DEBUG_PREFIX)
156 char id_name[PREFIX_LEN + 1 + id_len * 2 + sizeof ".debug" - 1];
157 strcpy (id_name, DEBUG_PREFIX);
158 int n = snprintf (&id_name[PREFIX_LEN - 1],
159 4, "%02" PRIx8 "/", (uint8_t) build_id[0]);
161 for (size_t i = 1; i < id_len; ++i)
163 n = snprintf (&id_name[PREFIX_LEN - 1 + 3 + (i - 1) * 2],
164 3, "%02" PRIx8, (uint8_t) build_id[i]);
167 strcpy (&id_name[PREFIX_LEN - 1 + 3 + (id_len - 1) * 2],
170 if (try_debugaltlink (result, id_name, build_id, id_len))
173 /* Everything failed, mark this Dwarf as not having an alternate,
174 but don't fail the load. The user may want to set it by hand
176 result->alt_dwarf = NULL;
179 #endif /* ENABLE_DWZ */
182 check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
187 /* Get the section header data. */
188 shdr = gelf_getshdr (scn, &shdr_mem);
190 /* This should never happen. If it does something is
191 wrong in the libelf library. */
194 /* Ignore any SHT_NOBITS sections. Debugging sections should not
195 have been stripped, but in case of a corrupt file we won't try
196 to look at the missing data. */
197 if (unlikely (shdr->sh_type == SHT_NOBITS))
200 /* Make sure the section is part of a section group only iff we
201 really need it. If we are looking for the global (= non-section
202 group debug info) we have to ignore all the info in section
203 groups. If we are looking into a section group we cannot look at
204 a section which isn't part of the section group. */
205 if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
206 /* Ignore the section. */
210 /* We recognize the DWARF section by their names. This is not very
211 safe and stable but the best we can do. */
212 const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx,
216 /* The section name must be valid. Otherwise is the ELF file
218 __libdw_free_zdata (result);
219 __libdw_seterrno (DWARF_E_INVALID_ELF);
225 /* For dwz multifile support, ignore if it looks wrong. */
226 if (strcmp (scnname, ".gnu_debugaltlink") == 0)
228 Elf_Data *data = elf_getdata (scn, NULL);
229 if (data != NULL && data->d_size != 0)
231 const char *alt_name = data->d_buf;
232 const void *build_id = memchr (data->d_buf, '\0', data->d_size);
233 const int id_len = data->d_size - (build_id - data->d_buf + 1);
234 if (alt_name && build_id && id_len > 0)
235 return open_debugaltlink (result, alt_name, build_id + 1, id_len);
238 #endif /* ENABLE_DWZ */
240 /* Recognize the various sections. Most names start with .debug_. */
242 for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
243 if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
245 /* Found it. Remember where the data is. */
246 if (unlikely (result->sectiondata[cnt] != NULL))
247 /* A section appears twice. That's bad. We ignore the section. */
250 /* Get the section data. */
251 Elf_Data *data = elf_getdata (scn, NULL);
252 if (data != NULL && data->d_size != 0)
253 /* Yep, there is actually data available. */
254 result->sectiondata[cnt] = data;
259 else if (scnname[0] == '.' && scnname[1] == 'z'
260 && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0)
262 /* A compressed section. */
264 if (unlikely (result->sectiondata[cnt] != NULL))
265 /* A section appears twice. That's bad. We ignore the section. */
268 /* Get the section data. */
269 Elf_Data *data = elf_getdata (scn, NULL);
270 if (data != NULL && data->d_size != 0)
272 /* There is a 12-byte header of "ZLIB" followed by
273 an 8-byte big-endian size. */
275 if (unlikely (data->d_size < 4 + 8)
276 || unlikely (memcmp (data->d_buf, "ZLIB", 4) != 0))
280 memcpy (&size, data->d_buf + 4, sizeof size);
281 size = be64toh (size);
283 Elf_Data *zdata = malloc (sizeof (Elf_Data) + size);
284 if (unlikely (zdata == NULL))
287 zdata->d_buf = &zdata[1];
288 zdata->d_type = ELF_T_BYTE;
289 zdata->d_version = EV_CURRENT;
290 zdata->d_size = size;
296 .next_in = data->d_buf + 4 + 8,
297 .avail_in = data->d_size - 4 - 8,
298 .next_out = zdata->d_buf,
299 .avail_out = zdata->d_size
301 int zrc = inflateInit (&z);
302 while (z.avail_in > 0 && likely (zrc == Z_OK))
304 z.next_out = zdata->d_buf + (zdata->d_size - z.avail_out);
305 zrc = inflate (&z, Z_FINISH);
306 if (unlikely (zrc != Z_STREAM_END))
311 zrc = inflateReset (&z);
313 if (likely (zrc == Z_OK))
314 zrc = inflateEnd (&z);
316 if (unlikely (zrc != Z_OK) || unlikely (z.avail_out != 0))
320 result->sectiondata[cnt] = zdata;
321 result->sectiondata_gzip_mask |= 1U << cnt;
333 /* Check whether all the necessary DWARF information is available. */
335 valid_p (Dwarf *result)
337 /* We looked at all the sections. Now determine whether all the
338 sections with debugging information we need are there.
340 XXX Which sections are absolutely necessary? Add tests if
341 necessary. For now we require only .debug_info. Hopefully this
343 if (likely (result != NULL)
344 && unlikely (result->sectiondata[IDX_debug_info] == NULL))
346 __libdw_free_zdata (result);
347 __libdw_seterrno (DWARF_E_NO_DWARF);
357 global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr)
361 while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
362 result = check_section (result, ehdr, scn, false);
364 return valid_p (result);
369 scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp)
371 /* SCNGRP is the section descriptor for a section group which might
372 contain debug sections. */
373 Elf_Data *data = elf_getdata (scngrp, NULL);
376 /* We cannot read the section content. Fail! */
377 __libdw_free_zdata (result);
382 /* The content of the section is a number of 32-bit words which
383 represent section indices. The first word is a flag word. */
384 Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
386 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
388 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
391 /* A section group refers to a non-existing section. Should
393 __libdw_free_zdata (result);
394 __libdw_seterrno (DWARF_E_INVALID_ELF);
399 result = check_section (result, ehdr, scn, true);
404 return valid_p (result);
409 dwarf_begin_elf (elf, cmd, scngrp)
417 /* Get the ELF header of the file. We need various pieces of
418 information from it. */
419 ehdr = gelf_getehdr (elf, &ehdr_mem);
422 if (elf_kind (elf) != ELF_K_ELF)
423 __libdw_seterrno (DWARF_E_NOELF);
425 __libdw_seterrno (DWARF_E_GETEHDR_ERROR);
431 /* Default memory allocation size. */
432 size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
434 /* Allocate the data structure. */
435 Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size);
436 if (unlikely (result == NULL)
437 || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
440 __libdw_seterrno (DWARF_E_NOMEM);
444 /* Fill in some values. */
445 if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
446 || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
447 result->other_byte_order = true;
451 /* Initialize the memory handling. */
452 result->mem_default_size = mem_default_size;
453 result->oom_handler = __libdw_oom;
454 result->mem_tail = (struct libdw_memblock *) (result + 1);
455 result->mem_tail->size = (result->mem_default_size
456 - offsetof (struct libdw_memblock, mem));
457 result->mem_tail->remaining = result->mem_tail->size;
458 result->mem_tail->prev = NULL;
460 if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
462 /* If the caller provides a section group we get the DWARF
463 sections only from this setion group. Otherwise we search
464 for the first section with the required name. Further
465 sections with the name are ignored. The DWARF specification
466 does not really say this is allowed. */
468 return global_read (result, elf, ehdr);
470 return scngrp_read (result, elf, ehdr, scngrp);
472 else if (cmd == DWARF_C_WRITE)
474 __libdw_seterrno (DWARF_E_UNIMPL);
479 __libdw_seterrno (DWARF_E_INVALID_CMD);
483 INTDEF(dwarf_begin_elf)