• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <endian.h>
46 
47 #include "libelfP.h"
48 #include "libdwP.h"
49 
50 
51 /* Section names.  (Note .debug_str_offsets is the largest 19 chars.)  */
52 static const char dwarf_scnnames[IDX_last][19] =
53 {
54   [IDX_debug_info] = ".debug_info",
55   [IDX_debug_types] = ".debug_types",
56   [IDX_debug_abbrev] = ".debug_abbrev",
57   [IDX_debug_addr] = ".debug_addr",
58   [IDX_debug_aranges] = ".debug_aranges",
59   [IDX_debug_line] = ".debug_line",
60   [IDX_debug_line_str] = ".debug_line_str",
61   [IDX_debug_frame] = ".debug_frame",
62   [IDX_debug_loc] = ".debug_loc",
63   [IDX_debug_loclists] = ".debug_loclists",
64   [IDX_debug_pubnames] = ".debug_pubnames",
65   [IDX_debug_str] = ".debug_str",
66   [IDX_debug_str_offsets] = ".debug_str_offsets",
67   [IDX_debug_macinfo] = ".debug_macinfo",
68   [IDX_debug_macro] = ".debug_macro",
69   [IDX_debug_ranges] = ".debug_ranges",
70   [IDX_debug_rnglists] = ".debug_rnglists",
71   [IDX_gnu_debugaltlink] = ".gnu_debugaltlink"
72 };
73 #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
74 
75 static enum dwarf_type
scn_dwarf_type(Dwarf * result,size_t shstrndx,Elf_Scn * scn)76 scn_dwarf_type (Dwarf *result, size_t shstrndx, Elf_Scn *scn)
77 {
78   GElf_Shdr shdr_mem;
79   GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
80   if (shdr == NULL)
81     return TYPE_UNKNOWN;
82 
83   const char *scnname = elf_strptr (result->elf, shstrndx,
84 				    shdr->sh_name);
85   if (scnname != NULL)
86     {
87       if (startswith (scnname, ".gnu.debuglto_.debug"))
88 	return TYPE_GNU_LTO;
89       else if (startswith (scnname, ".debug_") || startswith (scnname, ".zdebug_"))
90 	{
91 	  size_t len = strlen (scnname);
92 	  if (strcmp (scnname + len - 4, ".dwo") == 0)
93 	    return TYPE_DWO;
94 	  else
95 	    return TYPE_PLAIN;
96 	}
97     }
98   return TYPE_UNKNOWN;
99 }
100 static Dwarf *
check_section(Dwarf * result,size_t shstrndx,Elf_Scn * scn,bool inscngrp)101 check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp)
102 {
103   GElf_Shdr shdr_mem;
104   GElf_Shdr *shdr;
105 
106   /* Get the section header data.  */
107   shdr = gelf_getshdr (scn, &shdr_mem);
108   if (shdr == NULL)
109     /* We may read /proc/PID/mem with only program headers mapped and section
110        headers out of the mapped pages.  */
111     goto err;
112 
113   /* Ignore any SHT_NOBITS sections.  Debugging sections should not
114      have been stripped, but in case of a corrupt file we won't try
115      to look at the missing data.  */
116   if (unlikely (shdr->sh_type == SHT_NOBITS))
117     return result;
118 
119   /* Make sure the section is part of a section group only iff we
120      really need it.  If we are looking for the global (= non-section
121      group debug info) we have to ignore all the info in section
122      groups.  If we are looking into a section group we cannot look at
123      a section which isn't part of the section group.  */
124   if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
125     /* Ignore the section.  */
126     return result;
127 
128 
129   /* We recognize the DWARF section by their names.  This is not very
130      safe and stable but the best we can do.  */
131   const char *scnname = elf_strptr (result->elf, shstrndx,
132 				    shdr->sh_name);
133   if (scnname == NULL)
134     {
135       /* The section name must be valid.  Otherwise is the ELF file
136 	 invalid.  */
137     err:
138       Dwarf_Sig8_Hash_free (&result->sig8_hash);
139       __libdw_seterrno (DWARF_E_INVALID_ELF);
140       free (result);
141       return NULL;
142     }
143 
144   /* Recognize the various sections.  Most names start with .debug_.
145      They might be compressed (and start with .z).  Or end with .dwo
146      for split dwarf sections.  Or start with .gnu.debuglto_ for
147      LTO debug sections.  We should only use one consistent set at
148      a time.  We prefer PLAIN over DWO over LTO.  */
149   size_t cnt;
150   bool gnu_compressed = false;
151   for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
152     {
153       size_t dbglen = strlen (dwarf_scnnames[cnt]);
154       size_t scnlen = strlen (scnname);
155       if (strncmp (scnname, dwarf_scnnames[cnt], dbglen) == 0
156 	  && (dbglen == scnlen
157 	      || (scnlen == dbglen + 4
158 		  && strstr (scnname, ".dwo") == scnname + dbglen)))
159 	{
160 	  if (dbglen == scnlen)
161 	    {
162 	      if (result->type == TYPE_PLAIN)
163 		break;
164 	    }
165 	  else if (result->type == TYPE_DWO)
166 	    break;
167 	}
168       else if (scnname[0] == '.' && scnname[1] == 'z'
169 	       && (strncmp (&scnname[2], &dwarf_scnnames[cnt][1],
170 			    dbglen - 1) == 0
171 		   && (scnlen == dbglen + 1
172 		       || (scnlen == dbglen + 5
173 			   && strstr (scnname,
174 				      ".dwo") == scnname + dbglen + 1))))
175 	{
176 	  if (scnlen == dbglen + 1)
177 	    {
178 	      if (result->type == TYPE_PLAIN)
179 		{
180 		  gnu_compressed = true;
181 		  break;
182 		}
183 	    }
184 	  else if (result->type <= TYPE_DWO)
185 	    {
186 	      gnu_compressed = true;
187 	      break;
188 	    }
189 	}
190       else if (scnlen > 14 /* .gnu.debuglto_ prefix. */
191 	       && startswith (scnname, ".gnu.debuglto_")
192 	       && strcmp (&scnname[14], dwarf_scnnames[cnt]) == 0)
193 	{
194 	  if (result->type == TYPE_GNU_LTO)
195 	    break;
196 	}
197     }
198 
199   if (cnt >= ndwarf_scnnames)
200     /* Not a debug section; ignore it. */
201     return result;
202 
203   if (unlikely (result->sectiondata[cnt] != NULL))
204     /* A section appears twice.  That's bad.  We ignore the section.  */
205     return result;
206 
207   /* We cannot know whether or not a GNU compressed section has already
208      been uncompressed or not, so ignore any errors.  */
209   if (gnu_compressed)
210     elf_compress_gnu (scn, 0, 0);
211 
212   if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
213     {
214       if (elf_compress (scn, 0, 0) < 0)
215 	{
216 	  /* It would be nice if we could fail with a specific error.
217 	     But we don't know if this was an essential section or not.
218 	     So just continue for now. See also valid_p().  */
219 	  return result;
220 	}
221     }
222 
223   /* Get the section data.  */
224   Elf_Data *data = elf_getdata (scn, NULL);
225   if (data == NULL)
226     goto err;
227 
228   if (data->d_buf == NULL || data->d_size == 0)
229     /* No data actually available, ignore it. */
230     return result;
231 
232   /* We can now read the section data into results. */
233   result->sectiondata[cnt] = data;
234 
235   return result;
236 }
237 
238 
239 /* Helper function to set debugdir field.  We want to cache the dir
240    where we found this Dwarf ELF file to locate alt and dwo files.  */
241 char *
__libdw_debugdir(int fd)242 __libdw_debugdir (int fd)
243 {
244   /* strlen ("/proc/self/fd/") = 14 + strlen (<MAXINT>) = 10 + 1 = 25.  */
245   char devfdpath[25];
246   sprintf (devfdpath, "/proc/self/fd/%u", fd);
247   char *fdpath = realpath (devfdpath, NULL);
248   char *fddir;
249   if (fdpath != NULL && fdpath[0] == '/'
250       && (fddir = strrchr (fdpath, '/')) != NULL)
251     {
252       *++fddir = '\0';
253       return fdpath;
254     }
255   return NULL;
256 }
257 
258 
259 /* Check whether all the necessary DWARF information is available.  */
260 static Dwarf *
valid_p(Dwarf * result)261 valid_p (Dwarf *result)
262 {
263   /* We looked at all the sections.  Now determine whether all the
264      sections with debugging information we need are there.
265 
266      Require at least one section that can be read "standalone".  */
267   if (likely (result != NULL)
268       && unlikely (result->sectiondata[IDX_debug_info] == NULL
269 		   && result->sectiondata[IDX_debug_line] == NULL
270 		   && result->sectiondata[IDX_debug_frame] == NULL))
271     {
272       Dwarf_Sig8_Hash_free (&result->sig8_hash);
273       __libdw_seterrno (DWARF_E_NO_DWARF);
274       free (result);
275       result = NULL;
276     }
277 
278   /* We are setting up some "fake" CUs, which need an address size.
279      Check the ELF class to come up with something reasonable.  */
280   int elf_addr_size = 8;
281   if (result != NULL)
282     {
283       GElf_Ehdr ehdr;
284       if (gelf_getehdr (result->elf, &ehdr) == NULL)
285 	{
286 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
287 	  __libdw_seterrno (DWARF_E_INVALID_ELF);
288 	  free (result);
289 	  result = NULL;
290 	}
291       else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
292 	elf_addr_size = 4;
293     }
294 
295   /* For dwarf_location_attr () we need a "fake" CU to indicate
296      where the "fake" attribute data comes from.  This is a block
297      inside the .debug_loc or .debug_loclists section.  */
298   if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL)
299     {
300       result->fake_loc_cu = malloc (sizeof (Dwarf_CU));
301       if (unlikely (result->fake_loc_cu == NULL))
302 	{
303 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
304 	  __libdw_seterrno (DWARF_E_NOMEM);
305 	  free (result);
306 	  result = NULL;
307 	}
308       else
309 	{
310 	  result->fake_loc_cu->sec_idx = IDX_debug_loc;
311 	  result->fake_loc_cu->dbg = result;
312 	  result->fake_loc_cu->startp
313 	    = result->sectiondata[IDX_debug_loc]->d_buf;
314 	  result->fake_loc_cu->endp
315 	    = (result->sectiondata[IDX_debug_loc]->d_buf
316 	       + result->sectiondata[IDX_debug_loc]->d_size);
317 	  result->fake_loc_cu->locs = NULL;
318 	  result->fake_loc_cu->address_size = elf_addr_size;
319 	  result->fake_loc_cu->offset_size = 4;
320 	  result->fake_loc_cu->version = 4;
321 	  result->fake_loc_cu->split = NULL;
322 	}
323     }
324 
325   if (result != NULL && result->sectiondata[IDX_debug_loclists] != NULL)
326     {
327       result->fake_loclists_cu = malloc (sizeof (Dwarf_CU));
328       if (unlikely (result->fake_loclists_cu == NULL))
329 	{
330 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
331 	  __libdw_seterrno (DWARF_E_NOMEM);
332 	  free (result->fake_loc_cu);
333 	  free (result);
334 	  result = NULL;
335 	}
336       else
337 	{
338 	  result->fake_loclists_cu->sec_idx = IDX_debug_loclists;
339 	  result->fake_loclists_cu->dbg = result;
340 	  result->fake_loclists_cu->startp
341 	    = result->sectiondata[IDX_debug_loclists]->d_buf;
342 	  result->fake_loclists_cu->endp
343 	    = (result->sectiondata[IDX_debug_loclists]->d_buf
344 	       + result->sectiondata[IDX_debug_loclists]->d_size);
345 	  result->fake_loclists_cu->locs = NULL;
346 	  result->fake_loclists_cu->address_size = elf_addr_size;
347 	  result->fake_loclists_cu->offset_size = 4;
348 	  result->fake_loclists_cu->version = 5;
349 	  result->fake_loclists_cu->split = NULL;
350 	}
351     }
352 
353   /* For DW_OP_constx/GNU_const_index and DW_OP_addrx/GNU_addr_index
354      the dwarf_location_attr () will need a "fake" address CU to
355      indicate where the attribute data comes from.  This is a just
356      inside the .debug_addr section, if it exists.  */
357   if (result != NULL && result->sectiondata[IDX_debug_addr] != NULL)
358     {
359       result->fake_addr_cu = malloc (sizeof (Dwarf_CU));
360       if (unlikely (result->fake_addr_cu == NULL))
361 	{
362 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
363 	  __libdw_seterrno (DWARF_E_NOMEM);
364 	  free (result->fake_loc_cu);
365 	  free (result->fake_loclists_cu);
366 	  free (result);
367 	  result = NULL;
368 	}
369       else
370 	{
371 	  result->fake_addr_cu->sec_idx = IDX_debug_addr;
372 	  result->fake_addr_cu->dbg = result;
373 	  result->fake_addr_cu->startp
374 	    = result->sectiondata[IDX_debug_addr]->d_buf;
375 	  result->fake_addr_cu->endp
376 	    = (result->sectiondata[IDX_debug_addr]->d_buf
377 	       + result->sectiondata[IDX_debug_addr]->d_size);
378 	  result->fake_addr_cu->locs = NULL;
379 	  result->fake_addr_cu->address_size = elf_addr_size;
380 	  result->fake_addr_cu->offset_size = 4;
381 	  result->fake_addr_cu->version = 5;
382 	  result->fake_addr_cu->split = NULL;
383 	}
384     }
385 
386   if (result != NULL)
387     result->debugdir = __libdw_debugdir (result->elf->fildes);
388 
389   return result;
390 }
391 
392 
393 static Dwarf *
global_read(Dwarf * result,Elf * elf,size_t shstrndx)394 global_read (Dwarf *result, Elf *elf, size_t shstrndx)
395 {
396   Elf_Scn *scn = NULL;
397 
398   /* First check the type (PLAIN, DWO, LTO) we are looking for.  We
399      prefer PLAIN if available over DWO, over LTO.  */
400   while ((scn = elf_nextscn (elf, scn)) != NULL && result->type != TYPE_PLAIN)
401     {
402       enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
403       if (type > result->type)
404 	result->type = type;
405     }
406 
407   scn = NULL;
408   while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL)
409     result = check_section (result, shstrndx, scn, false);
410 
411   return valid_p (result);
412 }
413 
414 
415 static Dwarf *
scngrp_read(Dwarf * result,Elf * elf,size_t shstrndx,Elf_Scn * scngrp)416 scngrp_read (Dwarf *result, Elf *elf, size_t shstrndx, Elf_Scn *scngrp)
417 {
418   GElf_Shdr shdr_mem;
419   GElf_Shdr *shdr = gelf_getshdr (scngrp, &shdr_mem);
420   if (shdr == NULL)
421     {
422       Dwarf_Sig8_Hash_free (&result->sig8_hash);
423       __libdw_seterrno (DWARF_E_INVALID_ELF);
424       free (result);
425       return NULL;
426     }
427 
428   if ((shdr->sh_flags & SHF_COMPRESSED) != 0
429       && elf_compress (scngrp, 0, 0) < 0)
430     {
431       Dwarf_Sig8_Hash_free (&result->sig8_hash);
432       __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
433       free (result);
434       return NULL;
435     }
436 
437   /* SCNGRP is the section descriptor for a section group which might
438      contain debug sections.  */
439   Elf_Data *data = elf_getdata (scngrp, NULL);
440   if (data == NULL)
441     {
442       /* We cannot read the section content.  Fail!  */
443       Dwarf_Sig8_Hash_free (&result->sig8_hash);
444       free (result);
445       return NULL;
446     }
447 
448   /* The content of the section is a number of 32-bit words which
449      represent section indices.  The first word is a flag word.  */
450   Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
451   size_t cnt;
452 
453   /* First check the type (PLAIN, DWO, LTO) we are looking for.  We
454      prefer PLAIN if available over DWO, over LTO.  */
455   for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
456     {
457       Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
458       if (scn == NULL)
459 	{
460 	  /* A section group refers to a non-existing section.  Should
461 	     never happen.  */
462 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
463 	  __libdw_seterrno (DWARF_E_INVALID_ELF);
464 	  free (result);
465 	  return NULL;
466 	}
467 
468       enum dwarf_type type = scn_dwarf_type (result, shstrndx, scn);
469       if (type > result->type)
470 	result->type = type;
471     }
472 
473   for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size && result != NULL; ++cnt)
474     {
475       Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
476       assert (scn != NULL); // checked above
477       result = check_section (result, shstrndx, scn, true);
478       if (result == NULL)
479 	break;
480     }
481 
482   return valid_p (result);
483 }
484 
485 
486 Dwarf *
dwarf_begin_elf(Elf * elf,Dwarf_Cmd cmd,Elf_Scn * scngrp)487 dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
488 {
489   GElf_Ehdr *ehdr;
490   GElf_Ehdr ehdr_mem;
491 
492   /* Get the ELF header of the file.  We need various pieces of
493      information from it.  */
494   ehdr = gelf_getehdr (elf, &ehdr_mem);
495   if (ehdr == NULL)
496     {
497       if (elf_kind (elf) != ELF_K_ELF)
498 	__libdw_seterrno (DWARF_E_NOELF);
499       else
500 	__libdw_seterrno (DWARF_E_GETEHDR_ERROR);
501 
502       return NULL;
503     }
504 
505 
506   /* Default memory allocation size.  */
507   size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
508   assert (sizeof (struct Dwarf) < mem_default_size);
509 
510   /* Allocate the data structure.  */
511   Dwarf *result = calloc (1, sizeof (Dwarf));
512   if (unlikely (result == NULL)
513       || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
514     {
515       free (result);
516       __libdw_seterrno (DWARF_E_NOMEM);
517       return NULL;
518     }
519 
520   /* Fill in some values.  */
521   if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
522       || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
523     result->other_byte_order = true;
524 
525   result->elf = elf;
526   result->alt_fd = -1;
527 
528   /* Initialize the memory handling.  Initial blocks are allocated on first
529      actual allocation.  */
530   result->mem_default_size = mem_default_size;
531   result->oom_handler = __libdw_oom;
532   if (pthread_rwlock_init(&result->mem_rwl, NULL) != 0)
533     {
534       free (result);
535       __libdw_seterrno (DWARF_E_NOMEM); /* no memory.  */
536       return NULL;
537     }
538   result->mem_stacks = 0;
539   result->mem_tails = NULL;
540 
541   if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
542     {
543       /* All sections are recognized by name, so pass the section header
544 	 string index along to easily get the section names.  */
545       size_t shstrndx;
546       if (elf_getshdrstrndx (elf, &shstrndx) != 0)
547 	{
548 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
549 	  __libdw_seterrno (DWARF_E_INVALID_ELF);
550 	  free (result);
551 	  return NULL;
552 	}
553 
554       /* If the caller provides a section group we get the DWARF
555 	 sections only from this section group.  Otherwise we search
556 	 for the first section with the required name.  Further
557 	 sections with the name are ignored.  The DWARF specification
558 	 does not really say this is allowed.  */
559       if (scngrp == NULL)
560 	return global_read (result, elf, shstrndx);
561       else
562 	return scngrp_read (result, elf, shstrndx, scngrp);
563     }
564   else if (cmd == DWARF_C_WRITE)
565     {
566       Dwarf_Sig8_Hash_free (&result->sig8_hash);
567       __libdw_seterrno (DWARF_E_UNIMPL);
568       free (result);
569       return NULL;
570     }
571 
572   Dwarf_Sig8_Hash_free (&result->sig8_hash);
573   __libdw_seterrno (DWARF_E_INVALID_CMD);
574   free (result);
575   return NULL;
576 }
577 INTDEF(dwarf_begin_elf)
578