annotate leo-obj/tool/chararray.c @ 392:35009c936a4a

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