1 /* Create descriptor from ELF descriptor for processing file.
2 Copyright (C) 2002-2011, 2014, 2015, 2017, 2018 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
41 #include <sys/types.h>
49 /* Section names. (Note .debug_str_offsets is the largest 19 chars.) */
50 static const char dwarf_scnnames[IDX_last][19] =
52 [IDX_debug_info] = ".debug_info",
53 [IDX_debug_types] = ".debug_types",
54 [IDX_debug_abbrev] = ".debug_abbrev",
55 [IDX_debug_addr] = ".debug_addr",
56 [IDX_debug_aranges] = ".debug_aranges",
57 [IDX_debug_line] = ".debug_line",
58 [IDX_debug_line_str] = ".debug_line_str",
59 [IDX_debug_frame] = ".debug_frame",
60 [IDX_debug_loc] = ".debug_loc",
61 [IDX_debug_loclists] = ".debug_loclists",
62 [IDX_debug_pubnames] = ".debug_pubnames",
63 [IDX_debug_str] = ".debug_str",
64 [IDX_debug_str_offsets] = ".debug_str_offsets",
65 [IDX_debug_macinfo] = ".debug_macinfo",
66 [IDX_debug_macro] = ".debug_macro",
67 [IDX_debug_ranges] = ".debug_ranges",
68 [IDX_debug_rnglists] = ".debug_rnglists",
69 [IDX_gnu_debugaltlink] = ".gnu_debugaltlink"
71 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
73 static enum dwarf_type
74 scn_dwarf_type (Dwarf *result, size_t shstrndx, Elf_Scn *scn)
77 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
81 const char *scnname = elf_strptr (result->elf, shstrndx,
85 if (startswith (scnname, ".gnu.debuglto_.debug"))
87 else if (startswith (scnname, ".debug_") || startswith (scnname, ".zdebug_"))
89 size_t len = strlen (scnname);
90 if (strcmp (scnname + len - 4, ".dwo") == 0)
99 check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp)
104 /* Get the section header data. */
105 shdr = gelf_getshdr (scn, &shdr_mem);
107 /* We may read /proc/PID/mem with only program headers mapped and section
108 headers out of the mapped pages. */
111 /* Ignore any SHT_NOBITS sections. Debugging sections should not
112 have been stripped, but in case of a corrupt file we won't try
113 to look at the missing data. */
114 if (unlikely (shdr->sh_type == SHT_NOBITS))
117 /* Make sure the section is part of a section group only iff we
118 really need it. If we are looking for the global (= non-section
119 group debug info) we have to ignore all the info in section
120 groups. If we are looking into a section group we cannot look at
121 a section which isn't part of the section group. */
122 if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
123 /* Ignore the section. */
127 /* We recognize the DWARF section by their names. This is not very
128 safe and stable but the best we can do. */
129 const char *scnname = elf_strptr (result->elf, shstrndx,
133 /* The section name must be valid. Otherwise is the ELF file
136 Dwarf_Sig8_Hash_free (&result->sig8_hash);
137 __libdw_seterrno (DWARF_E_INVALID_ELF);
142 /* Recognize the various sections. Most names start with .debug_.
143 They might be compressed (and start with .z). Or end with .dwo
144 for split dwarf sections. Or start with .gnu.debuglto_ for
145 LTO debug sections. We should only use one consistent set at
146 a time. We prefer PLAIN over DWO over LTO. */
148 bool gnu_compressed = false;
149 for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
151 size_t dbglen = strlen (dwarf_scnnames[cnt]);
152 size_t scnlen = strlen (scnname);
153 if (strncmp (scnname, dwarf_scnnames[cnt], dbglen) == 0
155 || (scnlen == dbglen + 4
156 && strstr (scnname, ".dwo") == scnname + dbglen)))
158 if (dbglen == scnlen)
160 if (result->type == TYPE_PLAIN)
163 else if (result->type == TYPE_DWO)
166 else if (scnname[0] == '.' && scnname[1] == 'z'
167 && (strncmp (&scnname[2], &dwarf_scnnames[cnt][1],
169 && (scnlen == dbglen + 1
170 || (scnlen == dbglen + 5
172 ".dwo") == scnname + dbglen + 1))))
174 if (scnlen == dbglen + 1)
176 if (result->type == TYPE_PLAIN)
178 gnu_compressed = true;
182 else if (result->type <= TYPE_DWO)
184 gnu_compressed = true;
188 else if (scnlen > 14 /* .gnu.debuglto_ prefix. */
189 && startswith (scnname, ".gnu.debuglto_")
190 && strcmp (&scnname[14], dwarf_scnnames[cnt]) == 0)
192 if (result->type == TYPE_GNU_LTO)
197 if (cnt >= ndwarf_scnnames)
198 /* Not a debug section; ignore it. */
201 if (unlikely (result->sectiondata[cnt] != NULL))
202 /* A section appears twice. That's bad. We ignore the section. */
205 /* We cannot know whether or not a GNU compressed section has already
206 been uncompressed or not, so ignore any errors. */
208 elf_compress_gnu (scn, 0, 0);
210 if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
212 if (elf_compress (scn, 0, 0) < 0)
214 /* It would be nice if we could fail with a specific error.
215 But we don't know if this was an essential section or not.
216 So just continue for now. See also valid_p(). */
221 /* Get the section data. */
222 Elf_Data *data = elf_getdata (scn, NULL);
226 if (data->d_buf == NULL || data->d_size == 0)
227 /* No data actually available, ignore it. */
230 /* We can now read the section data into results. */
231 result->sectiondata[cnt] = data;
237 /* Helper function to set debugdir field. We want to cache the dir
238 where we found this Dwarf ELF file to locate alt and dwo files. */
240 __libdw_debugdir (int fd)
242 /* strlen ("/proc/self/fd/") = 14 + strlen (<MAXINT>) = 10 + 1 = 25. */
244 sprintf (devfdpath, "/proc/self/fd/%u", fd);
245 char *fdpath = realpath (devfdpath, NULL);
247 if (fdpath != NULL && fdpath[0] == '/'
248 && (fddir = strrchr (fdpath, '/')) != NULL)
257 /* Check whether all the necessary DWARF information is available. */
259 valid_p (Dwarf *result)
261 /* We looked at all the sections. Now determine whether all the
262 sections with debugging information we need are there.
264 Require at least one section that can be read "standalone". */
265 if (likely (result != NULL)
266 && unlikely (result->sectiondata[IDX_debug_info] == NULL
267 && result->sectiondata[IDX_debug_line] == NULL
268 && result->sectiondata[IDX_debug_frame] == NULL))
270 Dwarf_Sig8_Hash_free (&result->sig8_hash);
271 __libdw_seterrno (DWARF_E_NO_DWARF);
276 /* We are setting up some "fake" CUs, which need an address size.
277 Check the ELF class to come up with something reasonable. */
278 int elf_addr_size = 8;
282 if (gelf_getehdr (result->elf, &ehdr) == NULL)
284 Dwarf_Sig8_Hash_free (&result->sig8_hash);
285 __libdw_seterrno (DWARF_E_INVALID_ELF);
289 else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
293 /* For dwarf_location_attr () we need a "fake" CU to indicate
294 where the "fake" attribute data comes from. This is a block
295 inside the .debug_loc or .debug_loclists section. */
296 if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL)
298 result->fake_loc_cu = malloc (sizeof (Dwarf_CU));
299 if (unlikely (result->fake_loc_cu == NULL))
301 Dwarf_Sig8_Hash_free (&result->sig8_hash);
302 __libdw_seterrno (DWARF_E_NOMEM);
308 result->fake_loc_cu->sec_idx = IDX_debug_loc;
309 result->fake_loc_cu->dbg = result;
310 result->fake_loc_cu->startp
311 = result->sectiondata[IDX_debug_loc]->d_buf;
312 result->fake_loc_cu->endp
313 = (result->sectiondata[IDX_debug_loc]->d_buf
314 + result->sectiondata[IDX_debug_loc]->d_size);
315 result->fake_loc_cu->locs = NULL;
316 result->fake_loc_cu->address_size = elf_addr_size;
317 result->fake_loc_cu->offset_size = 4;
318 result->fake_loc_cu->version = 4;
319 result->fake_loc_cu->split = NULL;
323 if (result != NULL && result->sectiondata[IDX_debug_loclists] != NULL)
325 result->fake_loclists_cu = malloc (sizeof (Dwarf_CU));
326 if (unlikely (result->fake_loclists_cu == NULL))
328 Dwarf_Sig8_Hash_free (&result->sig8_hash);
329 __libdw_seterrno (DWARF_E_NOMEM);
330 free (result->fake_loc_cu);
336 result->fake_loclists_cu->sec_idx = IDX_debug_loclists;
337 result->fake_loclists_cu->dbg = result;
338 result->fake_loclists_cu->startp
339 = result->sectiondata[IDX_debug_loclists]->d_buf;
340 result->fake_loclists_cu->endp
341 = (result->sectiondata[IDX_debug_loclists]->d_buf
342 + result->sectiondata[IDX_debug_loclists]->d_size);
343 result->fake_loclists_cu->locs = NULL;
344 result->fake_loclists_cu->address_size = elf_addr_size;
345 result->fake_loclists_cu->offset_size = 4;
346 result->fake_loclists_cu->version = 5;
347 result->fake_loclists_cu->split = NULL;
351 /* For DW_OP_constx/GNU_const_index and DW_OP_addrx/GNU_addr_index
352 the dwarf_location_attr () will need a "fake" address CU to
353 indicate where the attribute data comes from. This is a just
354 inside the .debug_addr section, if it exists. */
355 if (result != NULL && result->sectiondata[IDX_debug_addr] != NULL)
357 result->fake_addr_cu = malloc (sizeof (Dwarf_CU));
358 if (unlikely (result->fake_addr_cu == NULL))
360 Dwarf_Sig8_Hash_free (&result->sig8_hash);
361 __libdw_seterrno (DWARF_E_NOMEM);
362 free (result->fake_loc_cu);
363 free (result->fake_loclists_cu);
369 result->fake_addr_cu->sec_idx = IDX_debug_addr;
370 result->fake_addr_cu->dbg = result;
371 result->fake_addr_cu->startp
372 = result->sectiondata[IDX_debug_addr]->d_buf;
373 result->fake_addr_cu->endp
374 = (result->sectiondata[IDX_debug_addr]->d_buf
375 + result->sectiondata[IDX_debug_addr]->d_size);
376 result->fake_addr_cu->locs = NULL;
377 result->fake_addr_cu->address_size = elf_addr_size;
378 result->fake_addr_cu->offset_size = 4;
379 result->fake_addr_cu->version = 5;
380 result->fake_addr_cu->split = NULL;
385 result->debugdir = __libdw_debugdir (result->elf->fildes);
392 global_read (Dwarf *result, Elf *elf, size_t shstrndx)
396 /* First check the type (PLAIN, DWO, LTO) we are looking for. We
397 prefer PLAIN if available over DWO, over LTO. */
398 while ((scn = elf_nextscn (elf, scn)) != NULL && result->type != TYPE_PLAIN)
400 enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
401 if (type > result->type)
406 while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
407 result = check_section (result, shstrndx, scn, false);
409 return valid_p (result);
414 scngrp_read (Dwarf *result, Elf *elf, size_t shstrndx, Elf_Scn *scngrp)
417 GElf_Shdr *shdr = gelf_getshdr (scngrp, &shdr_mem);
420 Dwarf_Sig8_Hash_free (&result->sig8_hash);
421 __libdw_seterrno (DWARF_E_INVALID_ELF);
426 if ((shdr->sh_flags & SHF_COMPRESSED) != 0
427 && elf_compress (scngrp, 0, 0) < 0)
429 Dwarf_Sig8_Hash_free (&result->sig8_hash);
430 __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
435 /* SCNGRP is the section descriptor for a section group which might
436 contain debug sections. */
437 Elf_Data *data = elf_getdata (scngrp, NULL);
440 /* We cannot read the section content. Fail! */
441 Dwarf_Sig8_Hash_free (&result->sig8_hash);
446 /* The content of the section is a number of 32-bit words which
447 represent section indices. The first word is a flag word. */
448 Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
451 /* First check the type (PLAIN, DWO, LTO) we are looking for. We
452 prefer PLAIN if available over DWO, over LTO. */
453 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
455 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
458 /* A section group refers to a non-existing section. Should
460 Dwarf_Sig8_Hash_free (&result->sig8_hash);
461 __libdw_seterrno (DWARF_E_INVALID_ELF);
466 enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
467 if (type > result->type)
471 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size && result != NULL; ++cnt)
473 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
474 assert (scn != NULL); // checked above
475 result = check_section (result, shstrndx, scn, true);
480 return valid_p (result);
485 dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
490 /* Get the ELF header of the file. We need various pieces of
491 information from it. */
492 ehdr = gelf_getehdr (elf, &ehdr_mem);
495 if (elf_kind (elf) != ELF_K_ELF)
496 __libdw_seterrno (DWARF_E_NOELF);
498 __libdw_seterrno (DWARF_E_GETEHDR_ERROR);
504 /* Default memory allocation size. */
505 size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
506 assert (sizeof (struct Dwarf) < mem_default_size);
508 /* Allocate the data structure. */
509 Dwarf *result = calloc (1, sizeof (Dwarf));
510 if (unlikely (result == NULL)
511 || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
514 __libdw_seterrno (DWARF_E_NOMEM);
518 /* Fill in some values. */
519 if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
520 || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
521 result->other_byte_order = true;
526 /* Initialize the memory handling. Initial blocks are allocated on first
527 actual allocation. */
528 result->mem_default_size = mem_default_size;
529 result->oom_handler = __libdw_oom;
530 if (pthread_rwlock_init(&result->mem_rwl, NULL) != 0)
533 __libdw_seterrno (DWARF_E_NOMEM); /* no memory. */
536 result->mem_stacks = 0;
537 result->mem_tails = NULL;
539 if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
541 /* All sections are recognized by name, so pass the section header
542 string index along to easily get the section names. */
544 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
546 Dwarf_Sig8_Hash_free (&result->sig8_hash);
547 __libdw_seterrno (DWARF_E_INVALID_ELF);
552 /* If the caller provides a section group we get the DWARF
553 sections only from this section group. Otherwise we search
554 for the first section with the required name. Further
555 sections with the name are ignored. The DWARF specification
556 does not really say this is allowed. */
558 return global_read (result, elf, shstrndx);
560 return scngrp_read (result, elf, shstrndx, scngrp);
562 else if (cmd == DWARF_C_WRITE)
564 Dwarf_Sig8_Hash_free (&result->sig8_hash);
565 __libdw_seterrno (DWARF_E_UNIMPL);
570 Dwarf_Sig8_Hash_free (&result->sig8_hash);
571 __libdw_seterrno (DWARF_E_INVALID_CMD);
575 INTDEF(dwarf_begin_elf)