comparison leo-obj/tool/chararray.c @ 184:069b79b36228

tiobjd: chararray extraction command implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 04 Aug 2014 21:17:06 +0000
parents
children
comparison
equal deleted inserted replaced
183:827b8977d3c2 184:069b79b36228
1 /*
2 * Some COFF objects in TI's firmware semi-src are the result of compiling
3 * a *.c file that contains nothing more than a const char array. Here
4 * we support the extraction of these char arrays, to enable recompilation
5 * with our choice of compiler toolchain.
6 */
7
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include "intstruct.h"
14 #include "coffconst.h"
15 #include "globals.h"
16
17 extern unsigned get_u16(), get_u32();
18
19 cmd_chararray(argc, argv)
20 char **argv;
21 {
22 int c, sflag = 0;
23 unsigned n, array_len;
24 char *array_name;
25 struct internal_syment *sym;
26 u_char *dp;
27 extern int optind;
28 extern char *optarg;
29
30 while ((c = getopt(argc, argv, "s:")) != EOF)
31 switch (c) {
32 case 's':
33 array_len = strtoul(optarg, 0, 16);
34 sflag = 1;
35 continue;
36 default:
37 /* error msg already printed */
38 exit(1);
39 }
40 if (argc != optind + 1) {
41 fprintf(stderr, "tiobjd chararray: name argument required\n");
42 exit(1);
43 }
44 array_name = argv[optind];
45
46 get_int_section_table();
47 get_int_symbol_table();
48 for (n = 0; n < nsymtab; n++) {
49 sym = symtab[n];
50 if (!sym)
51 continue;
52 if (sym->class != C_EXT)
53 continue;
54 if (sym->name[0] != '_')
55 continue;
56 if (!strcmp(sym->name + 1, array_name))
57 goto found;
58 }
59 fprintf(stderr, "error: found no global symbol named %s\n", array_name);
60 exit(1);
61
62 found: if (!sym->section) {
63 fprintf(stderr, "error: %s is not defined in a section\n",
64 array_name);
65 exit(1);
66 }
67 if (!sflag) {
68 if (sym->type != 0x003C) {
69 fprintf(stderr,
70 "error: %s has type != array of u_char\n",
71 array_name);
72 exit(1);
73 }
74 if (!sym->aux) {
75 fprintf(stderr, "error: %s has no Aux record\n",
76 array_name);
77 exit(1);
78 }
79 array_len = get_u16(sym->aux + 8);
80 }
81 dp = filemap + sym->section->data_offset + sym->value;
82 printf("const unsigned char %s[%u] = {", array_name, array_len);
83 for (n = 0; n < array_len; n++) {
84 if (!(n & 15))
85 putchar('\n');
86 printf("0x%02X,", *dp++);
87 }
88 puts("\n};");
89 exit(0);
90 }