annotate lwlink/objdump.c @ 426:652eee8f0c82

Fixed lw_expr_destroy() to not crash on NULL
author lost@l-w.ca
date Sun, 19 Sep 2010 10:40:37 -0600
parents a741d2e4869f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 objdump.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWLINK
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 A standalone program to dump an object file in a text form to stdout
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 #include "util.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 #ifdef HAVE_CONFIG_H
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 #include "config.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #endif
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 void read_lwobj16v0(unsigned char *filedata, long filesize);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 char *program_name;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
37 char *string_cleanup(char *sym)
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
38 {
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
39 static char symbuf[4096];
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
40 int optr = 0;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
41 while (*sym)
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
42 {
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
43 if (*sym < 33 || *sym > 126)
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
44 {
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
45 int c;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
46 symbuf[optr++] = '\\';
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
47 c = *sym >> 4;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
48 c+= 48;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
49 if (c > 57)
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
50 c += 7;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
51 symbuf[optr++] = c;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
52 c = *sym & 15;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
53 c += 48;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
54 if (c > 57)
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
55 c += 7;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
56 symbuf[optr++] = c;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
57 }
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
58 else if (*sym == '\\')
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
59 {
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
60 symbuf[optr++] = '\\';
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
61 symbuf[optr++] = '\\';
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
62 }
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
63 else
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
64 {
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
65 symbuf[optr++] = *sym;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
66 }
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
67 sym++;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
68 }
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
69 symbuf[optr] = '\0';
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
70 return symbuf;
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
71 }
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
72
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 The logic of reading the entire file into memory is simple. All the symbol
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 names in the file are NUL terminated strings and can be used directly without
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 making additional copies.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 int main(int argc, char **argv)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 int i;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 long size;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 FILE *f;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 long bread;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 unsigned char *filedata;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 program_name = argv[0];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 if (argc != 2)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 fprintf(stderr, "Must specify exactly one input file.\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 f = fopen(argv[1], "rb");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 if (!f)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 fprintf(stderr, "Can't open file %s:", argv[1]);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 fseek(f, 0, SEEK_END);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 size = ftell(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 rewind(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 filedata = lw_malloc(size);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 bread = fread(filedata, 1, size, f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 if (bread < size)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 fprintf(stderr, "Short read on file %s (%ld/%ld):", argv[1], bread, size);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 fclose(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 if (!memcmp(filedata, "LWOBJ16", 8))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 // read v0 LWOBJ16 file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 read_lwobj16v0(filedata, size);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 fprintf(stderr, "%s: unknown file format\n", argv[1]);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 exit(0);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 // this macro is used to bail out if we run off the end of the file data
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 // while parsing - it keeps the code below cleaner
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 #define NEXTBYTE() do { cc++; if (cc > filesize) { fprintf(stderr, "***invalid file format\n"); exit(1); } } while (0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 // this macro is used to refer to the current byte in the stream
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 #define CURBYTE() (filedata[cc < filesize ? cc : filesize - 1])
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 // this one will leave the input pointer past the trailing NUL
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 #define CURSTR() read_lwobj16v0_str(&cc, &filedata, filesize)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 unsigned char *read_lwobj16v0_str(long *cc1, unsigned char **filedata1, long filesize)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 int cc = *cc1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139 unsigned char *filedata = *filedata1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 unsigned char *fp;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 fp = &CURBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 while (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 *cc1 = cc;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146 *filedata1 = filedata;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 return fp;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 // the function below can be switched to dealing with data coming from a
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 // source other than an in-memory byte pool by adjusting the input data
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 // in "fn" and the above two macros
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 void read_lwobj16v0(unsigned char *filedata, long filesize)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 unsigned char *fp;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 long cc;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 int val;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 int bss;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159 static char *opernames[] = {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 "?",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 "PLUS",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 "MINUS",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163 "TIMES",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 "DIVIDE",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 "MOD",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166 "INTDIV",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167 "BWAND",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 "BWOR",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 "BWXOR",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 "AND",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
171 "OR",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 "NEG",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
173 "COM"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 };
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
175 static const int numopers = 13;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
177 // start reading *after* the magic number
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
178 cc = 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
179
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
180 while (1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
181 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
182 bss = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
183
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
184 // bail out if no more sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 if (!(CURBYTE()))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188 fp = CURSTR();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 printf("SECTION %s\n", fp);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
191
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 // read flags
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193 while (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
195 switch (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
197 case 0x01:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198 printf(" FLAG: BSS\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
199 bss = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
200 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
201
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
202 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
203 printf(" FLAG: %02X (unknown)\n", CURBYTE());
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
204 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
205 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
206 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
207 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
208 // skip NUL terminating flags
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
209 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
210
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
211 printf(" Local symbols:\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
212 // now parse the local symbol table
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
213 while (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
214 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
215 fp = CURSTR();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
216
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
217 // fp is the symbol name
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
218 val = (CURBYTE()) << 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
219 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
220 val |= (CURBYTE());
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
221 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
222 // val is now the symbol value
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
223
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
224 printf(" %s=%04X\n", string_cleanup(fp), val);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
225
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
226 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
227 // skip terminating NUL
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
228 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
229
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
230 printf(" Exported symbols\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
231
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
232 // now parse the exported symbol table
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
233 while (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
234 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
235 fp = CURSTR();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
236
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
237 // fp is the symbol name
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
238 val = (CURBYTE()) << 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
239 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
240 val |= (CURBYTE());
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
241 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
242 // val is now the symbol value
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
243
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
244 printf(" %s=%04X\n", string_cleanup(fp), val);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
245 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
246 // skip terminating NUL
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
247 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
248
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
249 // now parse the incomplete references and make a list of
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
250 // external references that need resolution
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
251 printf(" Incomplete references\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
252 while (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
253 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
254 printf(" (");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
255 // parse the expression
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
256 while (CURBYTE())
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
257 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
258 int tt = CURBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
259 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
260 switch (tt)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
261 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
262 case 0x01:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
263 // 16 bit integer
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
264 tt = CURBYTE() << 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
265 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
266 tt |= CURBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
267 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
268 // normalize for negatives...
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
269 if (tt > 0x7fff)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
270 tt -= 0x10000;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
271 printf(" I16=%d", tt);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
272 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
273
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
274 case 0x02:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
275 // external symbol reference
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
276 printf(" ES=%s", string_cleanup(CURSTR()));
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
277 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
278
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
279 case 0x03:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
280 // internal symbol reference
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 378
diff changeset
281 printf(" IS=%s", string_cleanup(CURSTR()));
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
282 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
283
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
284 case 0x04:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
285 // operator
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
286 if (CURBYTE() > 0 && CURBYTE() <= numopers)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
287 printf(" OP=%s", opernames[CURBYTE()]);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
288 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
289 printf(" OP=?");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
290 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
291 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
292
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
293 case 0x05:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
294 // section base reference (NULL internal reference is
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
295 // the section base address
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
296 printf(" SB");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
297 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
298
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
299 case 0xFF:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
300 // section flags
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
301 printf(" FLAGS=%02X", CURBYTE());
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
302 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
303 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
304
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
305 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
306 printf(" ERR");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
307 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
308 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
309 // skip the NUL
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
310 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
311
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
312 // fetch the offset
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
313 val = CURBYTE() << 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
314 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
315 val |= CURBYTE() & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
316 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
317 printf(" ) @ %04X\n", val);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
318 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
319 // skip the NUL terminating the relocations
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
320 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
321
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
322 // now set code location and size and verify that the file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
323 // contains data going to the end of the code (if !SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
324 val = CURBYTE() << 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
325 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
326 val |= CURBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
327 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
328
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
329 printf(" CODE %04X bytes", val);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
330
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
331 // skip the code if we're not in a BSS section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
332 if (!bss)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
333 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
334 int i;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
335 for (i = 0; i < val; i++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
336 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
337 if (! (i % 16))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
338 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
339 printf("\n %04X ", i);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
340 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
341 printf("%02X", CURBYTE());
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
342 NEXTBYTE();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
343 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
344 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
345 printf("\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
346 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
347 }