ede51255 |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | |
5 | #include "my_assert.h" |
6 | |
7 | static int my_isblank(char c) |
8 | { |
9 | return c == '\t' || c == ' ' || c == '\r' || c == '\n'; |
10 | } |
11 | |
12 | static char *sskip(char *s) |
13 | { |
14 | while (my_isblank(*s)) |
15 | s++; |
16 | |
17 | return s; |
18 | } |
19 | |
20 | static char *next_word(char *w, size_t wsize, char *s) |
21 | { |
22 | size_t i; |
23 | |
24 | s = sskip(s); |
25 | |
26 | for (i = 0; i < wsize - 1; i++) { |
27 | if (*s == 0 || my_isblank(s[i])) |
28 | break; |
29 | w[i] = s[i]; |
30 | } |
31 | w[i] = 0; |
32 | |
33 | if (*s != 0 && !my_isblank(s[i])) |
34 | printf("warning: '%s' truncated\n", w); |
35 | |
36 | return s + i; |
37 | } |
38 | |
39 | struct sl_item { |
40 | char *name; |
368deb23 |
41 | unsigned int callsites:1; |
8022df01 |
42 | unsigned int found:1; |
ede51255 |
43 | }; |
44 | |
45 | static int cmp_sym(const void *p1_, const void *p2_) |
46 | { |
47 | const struct sl_item *p1 = p1_, *p2 = p2_; |
48 | const char *s1 = p1->name, *s2 = p2->name; |
49 | int i; |
50 | |
51 | for (i = 0; ; i++) { |
52 | if (s1[i] == s2[i]) |
53 | continue; |
54 | |
55 | if (s1[i] == 0) { |
56 | if (s2[i] == 0 || s2[i] == '@') |
57 | break; |
58 | } |
59 | |
60 | if (s2[i] == 0 || s1[i] == '@') |
61 | break; |
62 | |
63 | return s1[i] - s2[i]; |
64 | } |
65 | |
66 | return 0; |
67 | } |
68 | |
69 | static int cmp_sym_sort(const void *p1_, const void *p2_) |
70 | { |
71 | const struct sl_item *p1 = p1_, *p2 = p2_; |
72 | const char *s1 = p1->name, *s2 = p2->name; |
73 | int ret; |
74 | |
75 | ret = cmp_sym(p1_, p2_); |
76 | if (ret == 0) { |
77 | printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2); |
78 | exit(1); |
79 | } |
80 | return ret; |
81 | } |
82 | |
368deb23 |
83 | void read_list(struct sl_item **sl_in, int *cnt, int *alloc, |
84 | FILE *f, int callsites) |
ede51255 |
85 | { |
86 | struct sl_item *sl = *sl_in; |
87 | int c = *cnt; |
88 | char line[256]; |
89 | char word[256]; |
90 | char *r; |
91 | |
92 | while (fgets(line, sizeof(line), f) != NULL) { |
93 | r = next_word(word, sizeof(word), line); |
94 | if (r == word) |
95 | continue; |
96 | |
97 | sl[c].name = strdup(word); |
368deb23 |
98 | sl[c].callsites = callsites; |
8022df01 |
99 | sl[c].found = 0; |
ede51255 |
100 | c++; |
101 | |
102 | if (c >= *alloc) { |
103 | *alloc *= 2; |
104 | sl = realloc(sl, *alloc * sizeof(sl[0])); |
105 | my_assert_not(sl, NULL); |
8022df01 |
106 | memset(sl + c, 0, (*alloc - c) * sizeof(sl[0])); |
ede51255 |
107 | } |
108 | } |
109 | |
110 | *sl_in = sl; |
111 | *cnt = c; |
112 | } |
113 | |
8022df01 |
114 | const char *sym_use(const struct sl_item *sym) |
115 | { |
116 | static char buf[256+3]; |
117 | int ret; |
118 | |
119 | ret = snprintf(buf, sizeof(buf), "%s%s", |
368deb23 |
120 | sym->callsites ? "" : "rm_", sym->name); |
8022df01 |
121 | if (ret >= sizeof(buf)) { |
122 | printf("truncation detected: '%s'\n", buf); |
123 | exit(1); |
124 | } |
125 | |
126 | return buf; |
127 | } |
128 | |
ede51255 |
129 | int main(int argc, char *argv[]) |
130 | { |
8022df01 |
131 | struct sl_item *symlist, *sym, ssym = { NULL, }; |
368deb23 |
132 | int patch_callsites = 0; |
ede51255 |
133 | FILE *fout, *fin, *f; |
134 | int symlist_alloc; |
135 | int symlist_cnt; |
136 | char line[256]; |
137 | char word[256]; |
138 | char word2[256]; |
139 | char word3[256]; |
140 | char word4[256]; |
141 | char *p; |
8022df01 |
142 | int i; |
ede51255 |
143 | |
368deb23 |
144 | if (argc < 4) { |
145 | // -c - patch callsites |
146 | printf("usage:\n%s <asmf_out> <asmf_in> [[-c] <listf>]*>\n", |
ede51255 |
147 | argv[0]); |
148 | return 1; |
149 | } |
150 | |
151 | symlist_alloc = 16; |
152 | symlist_cnt = 0; |
8022df01 |
153 | symlist = calloc(symlist_alloc, sizeof(symlist[0])); |
ede51255 |
154 | my_assert_not(symlist, NULL); |
155 | |
368deb23 |
156 | for (i = 3; i < argc; i++) { |
157 | if (!strcmp(argv[i], "-c")) { |
158 | patch_callsites = 1; |
159 | continue; |
160 | } |
161 | |
162 | f = fopen(argv[i], "r"); |
163 | my_assert_not(f, NULL); |
164 | read_list(&symlist, &symlist_cnt, &symlist_alloc, |
165 | f, patch_callsites); |
166 | fclose(f); |
ede51255 |
167 | |
368deb23 |
168 | patch_callsites = 0; |
169 | } |
ede51255 |
170 | |
171 | qsort(symlist, symlist_cnt, sizeof(symlist[0]), cmp_sym_sort); |
172 | |
8022df01 |
173 | #if 0 |
174 | printf("symlist:\n"); |
175 | for (i = 0; i < symlist_cnt; i++) |
368deb23 |
176 | printf("%d '%s'\n", symlist[i].callsites, symlist[i].name); |
8022df01 |
177 | #endif |
ede51255 |
178 | |
179 | fin = fopen(argv[2], "r"); |
180 | my_assert_not(fin, NULL); |
181 | |
182 | fout = fopen(argv[1], "w"); |
183 | my_assert_not(fout, NULL); |
184 | |
185 | while (fgets(line, sizeof(line), fin)) |
186 | { |
187 | p = sskip(line); |
188 | if (*p == 0 || *p == ';') |
189 | goto pass; |
190 | |
191 | p = sskip(next_word(word, sizeof(word), p)); |
192 | if (*p == 0 || *p == ';') |
193 | goto pass; // need at least 2 words |
194 | |
195 | p = next_word(word2, sizeof(word2), p); |
196 | |
197 | if (!strcasecmp(word2, "proc") || !strcasecmp(word2, "endp")) { |
198 | ssym.name = word; |
199 | sym = bsearch(&ssym, symlist, symlist_cnt, |
200 | sizeof(symlist[0]), cmp_sym); |
201 | if (sym != NULL) { |
8022df01 |
202 | sym->found = 1; |
ede51255 |
203 | fprintf(fout, "rm_%s\t%s%s", word, word2, p); |
204 | continue; |
205 | } |
206 | } |
207 | |
208 | if (!strcasecmp(word, "call") || !strcasecmp(word, "jmp")) { |
209 | ssym.name = word2; |
210 | sym = bsearch(&ssym, symlist, symlist_cnt, |
211 | sizeof(symlist[0]), cmp_sym); |
368deb23 |
212 | if (sym != NULL && sym->callsites) { |
8022df01 |
213 | fprintf(fout, "\t\t%s\t%s%s", word, |
214 | sym_use(sym), p); |
ede51255 |
215 | continue; |
216 | } |
217 | } |
218 | |
219 | p = sskip(p); |
220 | if (*p == 0 || *p == ';') |
221 | goto pass; // need at least 3 words |
222 | |
223 | p = next_word(word3, sizeof(word3), p); |
224 | |
225 | if (!strcasecmp(word, "dd") && !strcasecmp(word2, "offset")) { |
226 | ssym.name = word3; |
227 | sym = bsearch(&ssym, symlist, symlist_cnt, |
228 | sizeof(symlist[0]), cmp_sym); |
368deb23 |
229 | if (sym != NULL && sym->callsites) { |
8022df01 |
230 | fprintf(fout, "\t\tdd offset %s%s", |
231 | sym_use(sym), p); |
ede51255 |
232 | continue; |
233 | } |
234 | } |
235 | |
236 | p = sskip(p); |
237 | if (*p == 0 || *p == ';') |
238 | goto pass; // need at least 4 words |
239 | |
240 | p = next_word(word4, sizeof(word4), p); |
241 | |
242 | if (!strcasecmp(word2, "dd") && !strcasecmp(word3, "offset")) { |
243 | ssym.name = word4; |
244 | sym = bsearch(&ssym, symlist, symlist_cnt, |
245 | sizeof(symlist[0]), cmp_sym); |
368deb23 |
246 | if (sym != NULL && sym->callsites) { |
8022df01 |
247 | fprintf(fout, "%s\tdd offset %s%s", word, |
248 | sym_use(sym), p); |
ede51255 |
249 | continue; |
250 | } |
251 | } |
252 | |
253 | pass: |
254 | fwrite(line, 1, strlen(line), fout); |
255 | } |
256 | |
8022df01 |
257 | for (i = 0; i < symlist_cnt; i++) { |
258 | if (!symlist[i].found) |
259 | printf("warning: sym '%s' not found\n", symlist[i].name); |
260 | } |
261 | |
ede51255 |
262 | fclose(fin); |
263 | fclose(fout); |
264 | |
265 | return 0; |
266 | } |