• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program for dwarf_next_cfi
2    Copyright (C) 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 the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 #include <assert.h>
20 #include <inttypes.h>
21 #include ELFUTILS_HEADER(dw)
22 #include <dwarf.h>
23 #include <argp.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <locale.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include "system.h"
34 
35 void
handle_section(char * name,const unsigned char e_ident[],Elf_Scn * scn,const bool is_eh)36 handle_section (char *name, const unsigned char e_ident[],
37 		Elf_Scn *scn, const bool is_eh)
38 {
39   if (is_eh)
40     printf (".eh_frame\n");
41   else
42     printf (".debug_frame\n");
43 
44   GElf_Shdr mem;
45   GElf_Shdr *shdr = gelf_getshdr (scn, &mem);
46   if (shdr == NULL)
47     error (EXIT_FAILURE, 0, "Couldn't get section header: %s",
48 	   elf_errmsg (-1));
49   if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
50     {
51       if (elf_compress (scn, 0, 0) < 0)
52 	error (EXIT_FAILURE, 0, "Couldn't decompress section: %s",
53 	       elf_errmsg (-1));
54     }
55   else if (name[0] == '.' && name[1] == 'z')
56     {
57       if (elf_compress_gnu (scn, 0, 0) < 0)
58 	error (EXIT_FAILURE, 0, "Couldn't decompress section: %s",
59 	       elf_errmsg (-1));
60     }
61 
62   Elf_Data *data = elf_getdata (scn, NULL);
63   if (data == NULL || data->d_buf == NULL)
64     error (EXIT_FAILURE, 0, "no section data");
65 
66   int res;
67   Dwarf_Off off;
68   Dwarf_Off next_off = 0;
69   Dwarf_CFI_Entry entry;
70   while ((res = dwarf_next_cfi (e_ident, data, is_eh, off = next_off,
71 				&next_off, &entry)) == 0)
72     {
73       printf ("[%" PRId64 "] ", off);
74       if (dwarf_cfi_cie_p (&entry))
75 	printf ("CIE augmentation=\"%s\"\n", entry.cie.augmentation);
76       else
77 	{
78 	  printf ("FDE cie=[%" PRId64 "]\n", entry.fde.CIE_pointer);
79 
80 	  Dwarf_Off cie_off = entry.fde.CIE_pointer;
81 	  Dwarf_Off cie_off_next;
82 	  Dwarf_CFI_Entry cie_entry;
83 	  if (dwarf_next_cfi (e_ident, data, is_eh, cie_off, &cie_off_next,
84 			      &cie_entry) != 0
85 	      || !dwarf_cfi_cie_p (&cie_entry))
86 	    error (EXIT_FAILURE, 0, "FDE doesn't point to CIE");
87 	}
88     }
89 
90   if (res < 0)
91     error (EXIT_FAILURE, 0, "dwarf_next_cfi failed: %s\n",
92 	   dwarf_errmsg (-1));
93 }
94 
95 int
main(int argc,char * argv[])96 main (int argc, char *argv[])
97 {
98   if (argc != 2)
99     error (EXIT_FAILURE, 0, "need file name argument");
100 
101   const char *file = argv[1];
102   printf ("%s\n", file);
103 
104   int fd = open (file, O_RDONLY);
105   if (fd == -1)
106     error (EXIT_FAILURE, errno, "cannot open input file `%s'", file);
107 
108   elf_version (EV_CURRENT);
109 
110   Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
111   if (elf == NULL)
112     error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s",
113 	   elf_errmsg (-1));
114 
115   size_t esize;
116   const unsigned char *ident = (const unsigned char *) elf_getident (elf,
117 								     &esize);
118   if (ident == NULL || esize < EI_NIDENT)
119     error (EXIT_FAILURE, 0, "no, or too small, ELF ident");
120 
121   GElf_Ehdr ehdr;
122   if (gelf_getehdr (elf, &ehdr) == NULL)
123     error (EXIT_FAILURE, 0, "cannot get the ELF header: %s\n",
124 	   elf_errmsg (-1));
125 
126   size_t strndx = ehdr.e_shstrndx;
127 
128   Elf_Scn *scn = NULL;
129   while ((scn = elf_nextscn (elf, scn)) != NULL)
130     {
131       GElf_Shdr shdr;
132       if (gelf_getshdr (scn, &shdr) != NULL)
133 	{
134 	  char *name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
135 	  if (name != NULL && shdr.sh_type == SHT_PROGBITS)
136 	    {
137 	      if (strcmp (name, ".eh_frame") == 0)
138 		handle_section (name, ident, scn, true);
139 	      if (strcmp (name, ".debug_frame") == 0
140 		  || strcmp (name, ".zdebug_frame") == 0)
141 		handle_section (name, ident, scn, false);
142 	    }
143 	}
144     }
145 
146   elf_end (elf);
147   close (fd);
148 
149   return 0;
150 }
151