1 /* Test program for dwelf_elf_e_machine_string
2    Copyright (C) 2019 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 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <assert.h>
23 #include <errno.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 #include ELFUTILS_HEADER(dwelf)
32 
33 int
main(int argc,char ** argv)34 main (int argc, char **argv)
35 {
36   int i;
37   for (i = 1; i < argc; i++)
38     {
39       long val;
40       int em;
41       const char *machine;
42 
43       errno = 0;
44       if (strncmp ("0x", argv[i], 2) == 0)
45 	val = strtol (&argv[i][2], NULL, 16);
46       else
47 	val = strtol (argv[i], NULL, 10);
48 
49       if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
50            || (errno != 0 && val == 0))
51 	{
52           perror ("strtol");
53           exit (EXIT_FAILURE);
54 	}
55 
56       em = val;
57       assert (em == val);
58 
59       machine = dwelf_elf_e_machine_string (em);
60       printf ("0x%x %s\n", em, machine);
61       assert (machine != NULL);
62     }
63 
64   return 0;
65 }
66