ao-elftohex: Add '-n' option to elide symbol table from output
[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         { .name = "nosym", .has_arg = 0, .val = 'n' },
31         { 0, 0, 0, 0},
32 };
33
34 static void usage(char *program)
35 {
36         fprintf(stderr, "usage: %s [--verbose=<level>] [--output=<output.ihx>] <input.elf>\n", program);
37         exit(1);
38 }
39
40 static int
41 ends_with(char *whole, char *suffix)
42 {
43         int whole_len = strlen(whole);
44         int suffix_len = strlen(suffix);
45
46         if (suffix_len > whole_len)
47                 return 0;
48         return strcmp(whole + whole_len - suffix_len, suffix) == 0;
49 }
50
51 int
52 main (int argc, char **argv)
53 {
54         char                    *input = NULL;
55         char                    *output = NULL;
56         struct ao_hex_image     *full_image = NULL;
57         struct ao_sym           *file_symbols = NULL;
58         int                     num_file_symbols;
59         FILE                    *file;
60         int                     c;
61         int                     i;
62         int                     nosym = 0;
63
64         while ((c = getopt_long(argc, argv, "nv:o:", options, NULL)) != -1) {
65                 switch (c) {
66                 case 'o':
67                         output = optarg;
68                         break;
69                 case 'v':
70                         ao_verbose = (int) strtol(optarg, NULL, 0);
71                         break;
72                 case 'n':
73                         nosym = 1;
74                         break;
75                 default:
76                         usage(argv[0]);
77                         break;
78                 }
79         }
80
81         if (optind >= argc)
82                 usage(argv[0]);
83
84         for (i = optind; i < argc; i++) {
85                 struct ao_hex_image *image;
86
87                 input = argv[i];
88
89                 free(file_symbols);
90                 num_file_symbols = 0;
91                 if (ends_with (input, ".ihx"))
92                         image = ao_hex_load(input, &file_symbols, &num_file_symbols);
93                 else
94                         image = ao_load_elf(input, &file_symbols, &num_file_symbols);
95
96                 if (!image) {
97                         fprintf(stderr, "Failed to load %s\n", input);
98                         usage(argv[0]);
99                 }
100
101                 if (nosym) {
102                         free(file_symbols);
103                         file_symbols = NULL;
104                         num_file_symbols = 0;
105                 }
106
107                 if (full_image) {
108                         full_image = ao_hex_image_cat(full_image, image);
109                         if (!full_image) {
110                                 fprintf(stderr, "Can't merge image %s\n", input);
111                                 usage(argv[0]);
112                         }
113                 } else
114                         full_image = image;
115         }
116
117         if (!output)
118                 file = stdout;
119         else {
120                 file = fopen(output, "w");
121                 if (!file) {
122                         perror(output);
123                         exit(1);
124                 }
125         }
126
127         if (!ao_hex_save(file, full_image, file_symbols, num_file_symbols)) {
128                 fprintf(stderr, "%s: failed to write hex file\n", output ? output : "<stdout>");
129                 if (output)
130                         unlink(output);
131                 exit(1);
132         }
133         exit(0);
134 }