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