File: eu_search_cfi.c

package info (click to toggle)
elfutils 0.194-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,680 kB
  • sloc: ansic: 114,970; sh: 35,537; cpp: 4,998; makefile: 1,986; yacc: 1,388; lex: 130; asm: 77; sed: 39; awk: 35
file content (201 lines) | stat: -rw-r--r-- 5,253 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Test program for eu_search_cfi
   Copyright (C) 2023 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see<http://www.gnu.org/licenses/>.  */

#include <config.h>
#include <assert.h>
#include <inttypes.h>
#include ELFUTILS_HEADER(dw)
#include <dwarf.h>
#include <argp.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "system.h"
#include <pthread.h>

static void handle_section (char *name, const unsigned char e_ident[],
			   Elf_Scn *scn, const bool is_eh);
static void *thread_work (void *arg);

typedef struct
{
  char *name;
  const unsigned char *e_ident;
  Elf_Scn * scn;
  bool is_eh;
}
ThreadData;

static void *thread_work (void *arg)
{
  ThreadData *data = (ThreadData *) arg;
  handle_section (data->name, data->e_ident, data->scn, data->is_eh);
  return NULL;
}

static void handle_section (char *name, const unsigned char e_ident[],
    Elf_Scn *scn, const bool is_eh)
{
  if (is_eh)
    printf (".eh_frame\n");
  else
    printf (".debug_frame\n");

  GElf_Shdr mem;
  GElf_Shdr *shdr = gelf_getshdr (scn, &mem);

  if (shdr == NULL)
    error (EXIT_FAILURE, 0, "Couldn't get section header: %s",
	  elf_errmsg (-1));

  if ((shdr->sh_flags &SHF_COMPRESSED) != 0)
    {
      if (elf_compress (scn, 0, 0) < 0)
	error (EXIT_FAILURE, 0, "Couldn't decompress section: %s",
	      elf_errmsg (-1));
    }
  else if (name[0] == '.' && name[1] == 'z')
    {
      if (elf_compress_gnu (scn, 0, 0) < 0)
	error (EXIT_FAILURE, 0, "Couldn't decompress section: %s",
	      elf_errmsg (-1));
    }

  Elf_Data *data = elf_getdata (scn, NULL);
  if (data == NULL || data->d_buf == NULL)
    error (EXIT_FAILURE, 0, "no section data");

  int res;
  Dwarf_Off off;
  Dwarf_Off next_off = 0;
  Dwarf_CFI_Entry entry;
  while ((res = dwarf_next_cfi (e_ident, data, is_eh, off = next_off,
			       &next_off, &entry)) == 0)
    {
      printf ("[%" PRId64 "] ", off);
      if (dwarf_cfi_cie_p (&entry))
	printf ("CIE augmentation=\"%s\"\n", entry.cie.augmentation);
      else
	{
	  printf ("FDE cie=[%" PRId64 "]\n", entry.fde.CIE_pointer);

	  Dwarf_Off cie_off = entry.fde.CIE_pointer;
	  Dwarf_Off cie_off_next;
	  Dwarf_CFI_Entry cie_entry;
	  if (dwarf_next_cfi (e_ident, data, is_eh, cie_off, &cie_off_next,
			     &cie_entry) != 0
	      || !dwarf_cfi_cie_p (&cie_entry))
	    error (EXIT_FAILURE, 0, "FDE doesn't point to CIE");
    }
  }

  if (res < 0)
    error (EXIT_FAILURE, 0, "dwarf_next_cfi failed: %s\n",
	  dwarf_errmsg (-1));
}

int main (int argc, char *argv[])
{
  if (argc != 2)
    error (EXIT_FAILURE, 0, "need file name argument");

  const char *file = argv[1];
  printf ("%s\n", file);

  int fd = open (file, O_RDONLY);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "cannot open input file `%s'", file);

  elf_version (EV_CURRENT);

  Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
  if (elf == NULL)
    error (EXIT_FAILURE, 0, "cannot create ELF descriptor: %s",
	   elf_errmsg (-1));

  size_t esize;
  const unsigned char *ident
    = (const unsigned char *) elf_getident (elf, &esize);

  if (ident == NULL || esize < EI_NIDENT)
    error (EXIT_FAILURE, 0, "no, or too small, ELF ident");

  GElf_Ehdr ehdr;
  if (gelf_getehdr (elf, &ehdr) == NULL)
    error (EXIT_FAILURE, 0, "cannot get the ELF header: %s\n", elf_errmsg (-1));

  size_t strndx = ehdr.e_shstrndx;

  int num_threads = 4;
  pthread_t threads[num_threads];
  ThreadData thread_data;

  Elf_Scn *scn = NULL;
  while ((scn = elf_nextscn (elf, scn)) != NULL)
    {
      GElf_Shdr shdr;
      if (gelf_getshdr (scn, &shdr) != NULL)
	{
	  char *name = elf_strptr (elf, strndx, (size_t) shdr.sh_name);
	  if (name != NULL && shdr.sh_type == SHT_PROGBITS)
	    {
	      bool is_eh = false;
	      if (strcmp (name, ".eh_frame") == 0)
		is_eh = true;
	      else if (strcmp (name, ".debug_frame") == 0
		       || strcmp (name, ".zdebug_frame") == 0)
		is_eh = false;
	      else
		continue;

	      thread_data.name = name;
	      thread_data.e_ident = ident;
	      thread_data.scn = scn;
	      thread_data.is_eh = is_eh;

	      for (int i = 0; i < num_threads; i++)
		if (pthread_create (&threads[i], NULL, thread_work,
				    &thread_data) != 0)
		  {
		    perror ("pthread_create");

		    for (int j = 0; j < i; j++)
		      pthread_cancel (threads[j]);

		    elf_end (elf);
		    close (fd);
		    return 1;
		  }

	      for (int i = 0; i < num_threads; i++)
		if (pthread_join (threads[i], NULL) != 0)
		  perror ("pthread_join");
	    }
	}
    }

  elf_end (elf);
  close (fd);

  return 0;
}