ao-elftohex: Add conditions for skipping ELF sections
[fw/altos] / ao-tools / ao-elftohex / ao-elftohex.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <getopt.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include "ao-hex.h"
24 #include "ao-elf.h"
25 #include "ao-verbose.h"
26
27 static const struct option options[] = {
28         { .name = "verbose", .has_arg = 1, .val = 'v' },
29         { .name = "output", .has_arg = 1, .val = 'o' },
30         { 0, 0, 0, 0},
31 };
32
33 static void usage(char *program)
34 {
35         fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.ihx>] <input.elf>\n", program);
36         exit(1);
37 }
38
39 static int
40 ends_with(char *whole, char *suffix)
41 {
42         int whole_len = strlen(whole);
43         int suffix_len = strlen(suffix);
44
45         if (suffix_len > whole_len)
46                 return 0;
47         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
48 }
49
50 int
51 main (int argc, char **argv)
52 {
53         char                    *input = NULL;
54         char                    *output = NULL;
55         struct ao_hex_image     *image;
56         struct ao_sym           *file_symbols;
57         int                     num_file_symbols;
58         FILE                    *file;
59         int                     c;
60
61         while ((c = getopt_long(argc, argv, "v:o:", options, NULL)) != -1) {
62                 switch (c) {
63                 case 'o':
64                         output = optarg;
65                         break;
66                 case 'v':
67                         ao_verbose = (int) strtol(optarg, NULL, 0);
68                         break;
69                 default:
70                         usage(argv[0]);
71                         break;
72                 }
73         }
74
75         input = argv[optind];
76         if (input == NULL)
77                 usage(argv[0]);
78
79         if (ends_with (input, ".ihx"))
80                 image = ao_hex_load(input, &file_symbols, &num_file_symbols);
81         else
82                 image = ao_load_elf(input, &file_symbols, &num_file_symbols);
83
84         if (!image)
85                 usage(argv[0]);
86
87         if (!output)
88                 file = stdout;
89         else {
90                 file = fopen(output, "w");
91                 if (!file) {
92                         perror(output);
93                         exit(1);
94                 }
95         }
96
97         if (!ao_hex_save(file, image, file_symbols, num_file_symbols)) {
98                 fprintf(stderr, "%s: failed to write hex file\n", output ? output : "<stdout>");
99                 if (output)
100                         unlink(output);
101                 exit(1);
102         }
103         exit(0);
104 }