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.
4
5   This file is free software; you can redistribute it and/or modify
6   it under the terms of either
7
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
11
12   or
13
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
17
18   or both in parallel, as here.
19
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.
24
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/>.  */
28
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
33#include <system.h>
34
35#include <assert.h>
36#include <stdbool.h>
37#include <stddef.h>
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <fcntl.h>
44
45#include "libelfP.h"
46#include "libdwP.h"
47
48
49/* Section names.  (Note .debug_str_offsets is the largest 19 chars.)  */
50static const char dwarf_scnnames[IDX_last][19] =
51{
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"
70};
71#define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
72
73static enum dwarf_type
74scn_dwarf_type (Dwarf *result, size_t shstrndx, Elf_Scn *scn)
75{
76  GElf_Shdr shdr_mem;
77  GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
78  if (shdr == NULL)
79    return TYPE_UNKNOWN;
80
81  const char *scnname = elf_strptr (result->elf, shstrndx,
82				    shdr->sh_name);
83  if (scnname != NULL)
84    {
85      if (startswith (scnname, ".gnu.debuglto_.debug"))
86	return TYPE_GNU_LTO;
87      else if (startswith (scnname, ".debug_") || startswith (scnname, ".zdebug_"))
88	{
89	  size_t len = strlen (scnname);
90	  if (strcmp (scnname + len - 4, ".dwo") == 0)
91	    return TYPE_DWO;
92	  else
93	    return TYPE_PLAIN;
94	}
95    }
96  return TYPE_UNKNOWN;
97}
98static Dwarf *
99check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp)
100{
101  GElf_Shdr shdr_mem;
102  GElf_Shdr *shdr;
103
104  /* Get the section header data.  */
105  shdr = gelf_getshdr (scn, &shdr_mem);
106  if (shdr == NULL)
107    /* We may read /proc/PID/mem with only program headers mapped and section
108       headers out of the mapped pages.  */
109    goto err;
110
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))
115    return result;
116
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.  */
124    return result;
125
126
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,
130				    shdr->sh_name);
131  if (scnname == NULL)
132    {
133      /* The section name must be valid.  Otherwise is the ELF file
134	 invalid.  */
135    err:
136      Dwarf_Sig8_Hash_free (&result->sig8_hash);
137      __libdw_seterrno (DWARF_E_INVALID_ELF);
138      free (result);
139      return NULL;
140    }
141
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.  */
147  size_t cnt;
148  bool gnu_compressed = false;
149  for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
150    {
151      size_t dbglen = strlen (dwarf_scnnames[cnt]);
152      size_t scnlen = strlen (scnname);
153      if (strncmp (scnname, dwarf_scnnames[cnt], dbglen) == 0
154	  && (dbglen == scnlen
155	      || (scnlen == dbglen + 4
156		  && strstr (scnname, ".dwo") == scnname + dbglen)))
157	{
158	  if (dbglen == scnlen)
159	    {
160	      if (result->type == TYPE_PLAIN)
161		break;
162	    }
163	  else if (result->type == TYPE_DWO)
164	    break;
165	}
166      else if (scnname[0] == '.' && scnname[1] == 'z'
167	       && (strncmp (&scnname[2], &dwarf_scnnames[cnt][1],
168			    dbglen - 1) == 0
169		   && (scnlen == dbglen + 1
170		       || (scnlen == dbglen + 5
171			   && strstr (scnname,
172				      ".dwo") == scnname + dbglen + 1))))
173	{
174	  if (scnlen == dbglen + 1)
175	    {
176	      if (result->type == TYPE_PLAIN)
177		{
178		  gnu_compressed = true;
179		  break;
180		}
181	    }
182	  else if (result->type <= TYPE_DWO)
183	    {
184	      gnu_compressed = true;
185	      break;
186	    }
187	}
188      else if (scnlen > 14 /* .gnu.debuglto_ prefix. */
189	       && startswith (scnname, ".gnu.debuglto_")
190	       && strcmp (&scnname[14], dwarf_scnnames[cnt]) == 0)
191	{
192	  if (result->type == TYPE_GNU_LTO)
193	    break;
194	}
195    }
196
197  if (cnt >= ndwarf_scnnames)
198    /* Not a debug section; ignore it. */
199    return result;
200
201  if (unlikely (result->sectiondata[cnt] != NULL))
202    /* A section appears twice.  That's bad.  We ignore the section.  */
203    return result;
204
205  /* We cannot know whether or not a GNU compressed section has already
206     been uncompressed or not, so ignore any errors.  */
207  if (gnu_compressed)
208    elf_compress_gnu (scn, 0, 0);
209
210  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
211    {
212      if (elf_compress (scn, 0, 0) < 0)
213	{
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().  */
217	  return result;
218	}
219    }
220
221  /* Get the section data.  */
222  Elf_Data *data = elf_getdata (scn, NULL);
223  if (data == NULL)
224    goto err;
225
226  if (data->d_buf == NULL || data->d_size == 0)
227    /* No data actually available, ignore it. */
228    return result;
229
230  /* We can now read the section data into results. */
231  result->sectiondata[cnt] = data;
232
233  return result;
234}
235
236
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.  */
239char *
240__libdw_debugdir (int fd)
241{
242  /* strlen ("/proc/self/fd/") = 14 + strlen (<MAXINT>) = 10 + 1 = 25.  */
243  char devfdpath[25];
244  sprintf (devfdpath, "/proc/self/fd/%u", fd);
245  char *fdpath = realpath (devfdpath, NULL);
246  char *fddir;
247  if (fdpath != NULL && fdpath[0] == '/'
248      && (fddir = strrchr (fdpath, '/')) != NULL)
249    {
250      *++fddir = '\0';
251      return fdpath;
252    }
253  return NULL;
254}
255
256
257/* Check whether all the necessary DWARF information is available.  */
258static Dwarf *
259valid_p (Dwarf *result)
260{
261  /* We looked at all the sections.  Now determine whether all the
262     sections with debugging information we need are there.
263
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))
269    {
270      Dwarf_Sig8_Hash_free (&result->sig8_hash);
271      __libdw_seterrno (DWARF_E_NO_DWARF);
272      free (result);
273      result = NULL;
274    }
275
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;
279  if (result != NULL)
280    {
281      GElf_Ehdr ehdr;
282      if (gelf_getehdr (result->elf, &ehdr) == NULL)
283	{
284	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
285	  __libdw_seterrno (DWARF_E_INVALID_ELF);
286	  free (result);
287	  result = NULL;
288	}
289      else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
290	elf_addr_size = 4;
291    }
292
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)
297    {
298      result->fake_loc_cu = malloc (sizeof (Dwarf_CU));
299      if (unlikely (result->fake_loc_cu == NULL))
300	{
301	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
302	  __libdw_seterrno (DWARF_E_NOMEM);
303	  free (result);
304	  result = NULL;
305	}
306      else
307	{
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;
320	}
321    }
322
323  if (result != NULL && result->sectiondata[IDX_debug_loclists] != NULL)
324    {
325      result->fake_loclists_cu = malloc (sizeof (Dwarf_CU));
326      if (unlikely (result->fake_loclists_cu == NULL))
327	{
328	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
329	  __libdw_seterrno (DWARF_E_NOMEM);
330	  free (result->fake_loc_cu);
331	  free (result);
332	  result = NULL;
333	}
334      else
335	{
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;
348	}
349    }
350
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)
356    {
357      result->fake_addr_cu = malloc (sizeof (Dwarf_CU));
358      if (unlikely (result->fake_addr_cu == NULL))
359	{
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);
364	  free (result);
365	  result = NULL;
366	}
367      else
368	{
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;
381	}
382    }
383
384  if (result != NULL)
385    result->debugdir = __libdw_debugdir (result->elf->fildes);
386
387  return result;
388}
389
390
391static Dwarf *
392global_read (Dwarf *result, Elf *elf, size_t shstrndx)
393{
394  Elf_Scn *scn = NULL;
395
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)
399    {
400      enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
401      if (type > result->type)
402	result->type = type;
403    }
404
405  scn = NULL;
406  while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
407    result = check_section (result, shstrndx, scn, false);
408
409  return valid_p (result);
410}
411
412
413static Dwarf *
414scngrp_read (Dwarf *result, Elf *elf, size_t shstrndx, Elf_Scn *scngrp)
415{
416  GElf_Shdr shdr_mem;
417  GElf_Shdr *shdr = gelf_getshdr (scngrp, &shdr_mem);
418  if (shdr == NULL)
419    {
420      Dwarf_Sig8_Hash_free (&result->sig8_hash);
421      __libdw_seterrno (DWARF_E_INVALID_ELF);
422      free (result);
423      return NULL;
424    }
425
426  if ((shdr->sh_flags & SHF_COMPRESSED) != 0
427      && elf_compress (scngrp, 0, 0) < 0)
428    {
429      Dwarf_Sig8_Hash_free (&result->sig8_hash);
430      __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
431      free (result);
432      return NULL;
433    }
434
435  /* SCNGRP is the section descriptor for a section group which might
436     contain debug sections.  */
437  Elf_Data *data = elf_getdata (scngrp, NULL);
438  if (data == NULL)
439    {
440      /* We cannot read the section content.  Fail!  */
441      Dwarf_Sig8_Hash_free (&result->sig8_hash);
442      free (result);
443      return NULL;
444    }
445
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;
449  size_t cnt;
450
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)
454    {
455      Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
456      if (scn == NULL)
457	{
458	  /* A section group refers to a non-existing section.  Should
459	     never happen.  */
460	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
461	  __libdw_seterrno (DWARF_E_INVALID_ELF);
462	  free (result);
463	  return NULL;
464	}
465
466      enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
467      if (type > result->type)
468	result->type = type;
469    }
470
471  for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size && result != NULL; ++cnt)
472    {
473      Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
474      assert (scn != NULL); // checked above
475      result = check_section (result, shstrndx, scn, true);
476      if (result == NULL)
477	break;
478    }
479
480  return valid_p (result);
481}
482
483
484Dwarf *
485dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
486{
487  GElf_Ehdr *ehdr;
488  GElf_Ehdr ehdr_mem;
489
490  /* Get the ELF header of the file.  We need various pieces of
491     information from it.  */
492  ehdr = gelf_getehdr (elf, &ehdr_mem);
493  if (ehdr == NULL)
494    {
495      if (elf_kind (elf) != ELF_K_ELF)
496	__libdw_seterrno (DWARF_E_NOELF);
497      else
498	__libdw_seterrno (DWARF_E_GETEHDR_ERROR);
499
500      return NULL;
501    }
502
503
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);
507
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))
512    {
513      free (result);
514      __libdw_seterrno (DWARF_E_NOMEM);
515      return NULL;
516    }
517
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;
522
523  result->elf = elf;
524  result->alt_fd = -1;
525
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)
531    {
532      free (result);
533      __libdw_seterrno (DWARF_E_NOMEM); /* no memory.  */
534      return NULL;
535    }
536  result->mem_stacks = 0;
537  result->mem_tails = NULL;
538
539  if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
540    {
541      /* All sections are recognized by name, so pass the section header
542	 string index along to easily get the section names.  */
543      size_t shstrndx;
544      if (elf_getshdrstrndx (elf, &shstrndx) != 0)
545	{
546	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
547	  __libdw_seterrno (DWARF_E_INVALID_ELF);
548	  free (result);
549	  return NULL;
550	}
551
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.  */
557      if (scngrp == NULL)
558	return global_read (result, elf, shstrndx);
559      else
560	return scngrp_read (result, elf, shstrndx, scngrp);
561    }
562  else if (cmd == DWARF_C_WRITE)
563    {
564      Dwarf_Sig8_Hash_free (&result->sig8_hash);
565      __libdw_seterrno (DWARF_E_UNIMPL);
566      free (result);
567      return NULL;
568    }
569
570  Dwarf_Sig8_Hash_free (&result->sig8_hash);
571  __libdw_seterrno (DWARF_E_INVALID_CMD);
572  free (result);
573  return NULL;
574}
575INTDEF(dwarf_begin_elf)
576