reloc patching, avoid int3 overfill
[ia32rtools.git] / tools / asmproc.c
CommitLineData
ede51255 1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "my_assert.h"
6
7static int my_isblank(char c)
8{
9 return c == '\t' || c == ' ' || c == '\r' || c == '\n';
10}
11
12static char *sskip(char *s)
13{
14 while (my_isblank(*s))
15 s++;
16
17 return s;
18}
19
20static 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
39struct sl_item {
40 char *name;
8022df01 41 unsigned int is_replace:1;
42 unsigned int found:1;
ede51255 43};
44
45static 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
8022df01 51 // replace is with syms from C with '_' prepended
52 if (p1->is_replace)
53 s1++;
54 if (p2->is_replace)
55 s2++;
56
ede51255 57 for (i = 0; ; i++) {
58 if (s1[i] == s2[i])
59 continue;
60
61 if (s1[i] == 0) {
62 if (s2[i] == 0 || s2[i] == '@')
63 break;
64 }
65
66 if (s2[i] == 0 || s1[i] == '@')
67 break;
68
69 return s1[i] - s2[i];
70 }
71
72 return 0;
73}
74
75static int cmp_sym_sort(const void *p1_, const void *p2_)
76{
77 const struct sl_item *p1 = p1_, *p2 = p2_;
78 const char *s1 = p1->name, *s2 = p2->name;
79 int ret;
80
81 ret = cmp_sym(p1_, p2_);
82 if (ret == 0) {
83 printf("%s: dupe sym: '%s' '%s'\n", __func__, s1, s2);
84 exit(1);
85 }
86 return ret;
87}
88
89void read_list(struct sl_item **sl_in, int *cnt, int *alloc, FILE *f, int is_repl)
90{
91 struct sl_item *sl = *sl_in;
92 int c = *cnt;
93 char line[256];
94 char word[256];
95 char *r;
96
97 while (fgets(line, sizeof(line), f) != NULL) {
98 r = next_word(word, sizeof(word), line);
99 if (r == word)
100 continue;
101
102 sl[c].name = strdup(word);
103 sl[c].is_replace = is_repl;
8022df01 104 sl[c].found = 0;
ede51255 105 c++;
106
107 if (c >= *alloc) {
108 *alloc *= 2;
109 sl = realloc(sl, *alloc * sizeof(sl[0]));
110 my_assert_not(sl, NULL);
8022df01 111 memset(sl + c, 0, (*alloc - c) * sizeof(sl[0]));
ede51255 112 }
113 }
114
115 *sl_in = sl;
116 *cnt = c;
117}
118
8022df01 119const char *sym_use(const struct sl_item *sym)
120{
121 static char buf[256+3];
122 int ret;
123
124 ret = snprintf(buf, sizeof(buf), "%s%s",
125 sym->is_replace ? "" : "rm_", sym->name);
126 if (ret >= sizeof(buf)) {
127 printf("truncation detected: '%s'\n", buf);
128 exit(1);
129 }
130
131 return buf;
132}
133
ede51255 134int main(int argc, char *argv[])
135{
8022df01 136 struct sl_item *symlist, *sym, ssym = { NULL, };
ede51255 137 FILE *fout, *fin, *f;
138 int symlist_alloc;
139 int symlist_cnt;
140 char line[256];
141 char word[256];
142 char word2[256];
143 char word3[256];
144 char word4[256];
145 char *p;
8022df01 146 int i;
ede51255 147
148 if (argc != 5) {
149 // rmlist - prefix func with 'rm_', callsites with '_'
150 // renlist - prefix func and callsites with 'rm_'
151 printf("usage:\n%s <asmf_out> <asmf_in> <rmlist> <renlist>\n",
152 argv[0]);
153 return 1;
154 }
155
156 symlist_alloc = 16;
157 symlist_cnt = 0;
8022df01 158 symlist = calloc(symlist_alloc, sizeof(symlist[0]));
ede51255 159 my_assert_not(symlist, NULL);
160
161 f = fopen(argv[3], "r");
162 my_assert_not(f, NULL);
163 read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 1);
164 fclose(f);
165
166 f = fopen(argv[4], "r");
167 my_assert_not(f, NULL);
168 read_list(&symlist, &symlist_cnt, &symlist_alloc, f, 0);
169 fclose(f);
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++)
176 printf("%d '%s'\n", symlist[i].is_replace, symlist[i].name);
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);
212 if (sym != NULL) {
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);
229 if (sym != NULL) {
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);
246 if (sym != NULL) {
8022df01 247 fprintf(fout, "%s\tdd offset %s%s", word,
248 sym_use(sym), p);
ede51255 249 continue;
250 }
251 }
252
253pass:
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}