aout2hux.c revision 1.1 1 1.1 itohy /*
2 1.1 itohy * aout2hux - convert a.out executable to Human68k .x format
3 1.1 itohy *
4 1.1 itohy * Read two a.out format executables with different load addresses
5 1.1 itohy * and generate Human68k .x format executable.
6 1.1 itohy *
7 1.1 itohy * written by Yasha (ITOH Yasufumi)
8 1.1 itohy * public domain
9 1.1 itohy *
10 1.1 itohy * usage:
11 1.1 itohy * aout2hux [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2
12 1.1 itohy *
13 1.1 itohy * The input a.out files must be static OMAGIC/NMAGIC m68k executables.
14 1.1 itohy * Two a.out files must have different loading addresses.
15 1.1 itohy * Each of the load address must be a hexadecimal number.
16 1.1 itohy * Load address shall be multiple of 4 for as / ld of NetBSD/m68k.
17 1.1 itohy *
18 1.1 itohy * example:
19 1.1 itohy * % cc -N -static -Wl,-T,0 -o aout1 *.o
20 1.1 itohy * % cc -N -static -Wl,-T,10203040 -o aout2 *.o
21 1.1 itohy * % aout2hux -o foo.x aout1 0 aout2 10203040
22 1.1 itohy *
23 1.1 itohy * $NetBSD: aout2hux.c,v 1.1 1998/09/01 19:51:08 itohy Exp $
24 1.1 itohy */
25 1.1 itohy
26 1.1 itohy #include <sys/types.h>
27 1.1 itohy #ifndef NO_UNISTD
28 1.1 itohy # include <unistd.h>
29 1.1 itohy #endif
30 1.1 itohy #ifndef NO_STDLIB
31 1.1 itohy # include <stdlib.h>
32 1.1 itohy #endif
33 1.1 itohy #include <stdio.h>
34 1.1 itohy #include <string.h>
35 1.1 itohy
36 1.1 itohy #include "type_local.h"
37 1.1 itohy #include "aout68k.h"
38 1.1 itohy #include "hux.h"
39 1.1 itohy
40 1.1 itohy #ifndef DEFAULT_OUTPUT_FILE
41 1.1 itohy # define DEFAULT_OUTPUT_FILE "out.x"
42 1.1 itohy #endif
43 1.1 itohy
44 1.1 itohy #ifdef DEBUG
45 1.1 itohy # define DPRINTF(x) printf x
46 1.1 itohy #else
47 1.1 itohy # define DPRINTF(x)
48 1.1 itohy #endif
49 1.1 itohy
50 1.1 itohy unsigned get_uint16 PROTO((be_uint16_t *be));
51 1.1 itohy u_int32_t get_uint32 PROTO((be_uint32_t *be));
52 1.1 itohy void put_uint16 PROTO((be_uint16_t *be, unsigned v));
53 1.1 itohy void put_uint32 PROTO((be_uint32_t *be, u_int32_t v));
54 1.1 itohy void *do_realloc PROTO((void *p, size_t s));
55 1.1 itohy
56 1.1 itohy FILE *open_aout PROTO((const char *fn, struct aout_m68k *hdr));
57 1.1 itohy int check_2_aout_hdrs PROTO((struct aout_m68k *h1, struct aout_m68k *h2));
58 1.1 itohy int aout2hux PROTO((const char *fn1, const char *fn2,
59 1.1 itohy u_int32_t loadadr1, u_int32_t loadadr2, const char *fnx));
60 1.1 itohy int gethex PROTO((u_int32_t *pval, const char *str));
61 1.1 itohy void usage PROTO((const char *name));
62 1.1 itohy int main PROTO((int argc, char *argv[]));
63 1.1 itohy
64 1.1 itohy #if !defined(bzero) && defined(__SVR4)
65 1.1 itohy # define bzero(d, n) memset((d), 0, (n))
66 1.1 itohy #endif
67 1.1 itohy
68 1.1 itohy /*
69 1.1 itohy * read/write big-endian integer
70 1.1 itohy */
71 1.1 itohy
72 1.1 itohy unsigned
73 1.1 itohy get_uint16(be)
74 1.1 itohy be_uint16_t *be;
75 1.1 itohy {
76 1.1 itohy
77 1.1 itohy return be->val[0] << 8 | be->val[1];
78 1.1 itohy }
79 1.1 itohy
80 1.1 itohy u_int32_t
81 1.1 itohy get_uint32(be)
82 1.1 itohy be_uint32_t *be;
83 1.1 itohy {
84 1.1 itohy
85 1.1 itohy return be->val[0]<<24 | be->val[1]<<16 | be->val[2]<<8 | be->val[3];
86 1.1 itohy }
87 1.1 itohy
88 1.1 itohy void
89 1.1 itohy put_uint16(be, v)
90 1.1 itohy be_uint16_t *be;
91 1.1 itohy unsigned v;
92 1.1 itohy {
93 1.1 itohy
94 1.1 itohy be->val[0] = (u_int8_t) (v >> 8);
95 1.1 itohy be->val[1] = (u_int8_t) v;
96 1.1 itohy }
97 1.1 itohy
98 1.1 itohy void
99 1.1 itohy put_uint32(be, v)
100 1.1 itohy be_uint32_t *be;
101 1.1 itohy u_int32_t v;
102 1.1 itohy {
103 1.1 itohy
104 1.1 itohy be->val[0] = (u_int8_t) (v >> 24);
105 1.1 itohy be->val[1] = (u_int8_t) (v >> 16);
106 1.1 itohy be->val[2] = (u_int8_t) (v >> 8);
107 1.1 itohy be->val[3] = (u_int8_t) v;
108 1.1 itohy }
109 1.1 itohy
110 1.1 itohy void *
111 1.1 itohy do_realloc(p, s)
112 1.1 itohy void *p;
113 1.1 itohy size_t s;
114 1.1 itohy {
115 1.1 itohy
116 1.1 itohy p = p ? realloc(p, s) : malloc(s); /* for portability */
117 1.1 itohy
118 1.1 itohy if (!p) {
119 1.1 itohy fprintf(stderr, "malloc failed\n");
120 1.1 itohy exit(1);
121 1.1 itohy }
122 1.1 itohy
123 1.1 itohy return p;
124 1.1 itohy }
125 1.1 itohy
126 1.1 itohy /*
127 1.1 itohy * open an a.out and check the header
128 1.1 itohy */
129 1.1 itohy FILE *
130 1.1 itohy open_aout(fn, hdr)
131 1.1 itohy const char *fn;
132 1.1 itohy struct aout_m68k *hdr;
133 1.1 itohy {
134 1.1 itohy FILE *fp;
135 1.1 itohy int i;
136 1.1 itohy
137 1.1 itohy if (!(fp = fopen(fn, "r"))) {
138 1.1 itohy perror(fn);
139 1.1 itohy return (FILE *) NULL;
140 1.1 itohy }
141 1.1 itohy if (fread(hdr, sizeof(struct aout_m68k), 1, fp) != 1) {
142 1.1 itohy fprintf(stderr, "%s: can't read a.out header\n", fn);
143 1.1 itohy goto out;
144 1.1 itohy }
145 1.1 itohy
146 1.1 itohy if ((i = AOUT_GET_MAGIC(hdr)) != AOUT_OMAGIC && i != AOUT_NMAGIC) {
147 1.1 itohy fprintf(stderr, "%s: not a OMAGIC or NMAGIC a.out\n", fn);
148 1.1 itohy goto out;
149 1.1 itohy }
150 1.1 itohy
151 1.1 itohy if ((i = AOUT_GET_MID(hdr)) != AOUT_MID_M68K && i != AOUT_MID_M68K4K) {
152 1.1 itohy fprintf(stderr, "%s: wrong architecture (mid %d)\n", fn, i);
153 1.1 itohy goto out;
154 1.1 itohy }
155 1.1 itohy
156 1.1 itohy /* if unsolved relocations exist, not an executable but an object */
157 1.1 itohy if (hdr->a_trsize.hostval || hdr->a_drsize.hostval) {
158 1.1 itohy fprintf(stderr, "%s: not an executable (object file?)\n", fn);
159 1.1 itohy goto out;
160 1.1 itohy }
161 1.1 itohy
162 1.1 itohy if (AOUT_GET_FLAGS(hdr) & (AOUT_FLAG_PIC | AOUT_FLAG_DYNAMIC)) {
163 1.1 itohy fprintf(stderr, "%s: PIC and DYNAMIC are not supported\n", fn);
164 1.1 itohy goto out;
165 1.1 itohy }
166 1.1 itohy
167 1.1 itohy /* OK! */
168 1.1 itohy return fp;
169 1.1 itohy
170 1.1 itohy out: fclose(fp);
171 1.1 itohy return (FILE *) NULL;
172 1.1 itohy }
173 1.1 itohy
174 1.1 itohy /*
175 1.1 itohy * look at two a.out headers and check if they are compatible
176 1.1 itohy */
177 1.1 itohy int
178 1.1 itohy check_2_aout_hdrs(h1, h2)
179 1.1 itohy struct aout_m68k *h1, *h2;
180 1.1 itohy {
181 1.1 itohy
182 1.1 itohy if (/* compare magic */
183 1.1 itohy h1->a_magic.hostval != h2->a_magic.hostval ||
184 1.1 itohy /* compare section size */
185 1.1 itohy h1->a_text.hostval != h2->a_text.hostval ||
186 1.1 itohy h1->a_data.hostval != h2->a_data.hostval ||
187 1.1 itohy h1->a_bss.hostval != h2->a_bss.hostval)
188 1.1 itohy return -1;
189 1.1 itohy
190 1.1 itohy return 0;
191 1.1 itohy }
192 1.1 itohy
193 1.1 itohy /* allocation unit (in bytes) of relocation table */
194 1.1 itohy #define RELTBL_CHUNK 8192
195 1.1 itohy
196 1.1 itohy /*
197 1.1 itohy * add an entry to the relocation table
198 1.1 itohy */
199 1.1 itohy #define ADD_RELTBL(adr) \
200 1.1 itohy if (relsize + sizeof(struct relinf_l) > relallocsize) \
201 1.1 itohy reltbl = do_realloc(reltbl, relallocsize += RELTBL_CHUNK); \
202 1.1 itohy if ((adr) < reladdr + HUX_MINLREL) { \
203 1.1 itohy struct relinf_s *r = (struct relinf_s *)(reltbl + relsize); \
204 1.1 itohy put_uint16(&r->locoff_s, (unsigned)((adr) - reladdr)); \
205 1.1 itohy relsize += sizeof(struct relinf_s); \
206 1.1 itohy DPRINTF(("short")); \
207 1.1 itohy } else { \
208 1.1 itohy struct relinf_l *r = (struct relinf_l *)(reltbl + relsize); \
209 1.1 itohy put_uint16(&r->lrelmag, HUXLRELMAGIC); \
210 1.1 itohy put_uint32((be_uint32_t *)r->locoff_l, (adr) - reladdr); \
211 1.1 itohy relsize += sizeof(struct relinf_l); \
212 1.1 itohy DPRINTF(("long ")); \
213 1.1 itohy } \
214 1.1 itohy DPRINTF((" reloc 0x%06x", (adr))); \
215 1.1 itohy reladdr = (adr);
216 1.1 itohy
217 1.1 itohy #define ERR1 { if (ferror(fpa1)) perror(fn1); \
218 1.1 itohy else fprintf(stderr, "%s: unexpected EOF\n", fn1); \
219 1.1 itohy goto out; }
220 1.1 itohy #define ERR2 { if (ferror(fpa2)) perror(fn2); \
221 1.1 itohy else fprintf(stderr, "%s: unexpected EOF\n", fn2); \
222 1.1 itohy goto out; }
223 1.1 itohy #define ERRC { fprintf(stderr, "files %s and %s are inconsistent\n", \
224 1.1 itohy fn1, fn2); \
225 1.1 itohy goto out; }
226 1.1 itohy
227 1.1 itohy /*
228 1.1 itohy * read a.out files and output .x body
229 1.1 itohy * and create relocation table
230 1.1 itohy */
231 1.1 itohy #define CREATE_RELOCATION(segsize) \
232 1.1 itohy while (segsize > 0 || nbuf) { \
233 1.1 itohy if (nbuf == 0) { \
234 1.1 itohy if (fread(&b1.half[0], SIZE_16, 1, fpa1) != 1) \
235 1.1 itohy ERR1 \
236 1.1 itohy if (fread(&b2.half[0], SIZE_16, 1, fpa2) != 1) \
237 1.1 itohy ERR2 \
238 1.1 itohy nbuf = 1; \
239 1.1 itohy segsize -= SIZE_16; \
240 1.1 itohy } else if (nbuf == 1) { \
241 1.1 itohy if (segsize == 0) { \
242 1.1 itohy if (b1.half[0].hostval != b2.half[0].hostval) \
243 1.1 itohy ERRC \
244 1.1 itohy fwrite(&b1.half[0], SIZE_16, 1, fpx); \
245 1.1 itohy nbuf = 0; \
246 1.1 itohy addr += SIZE_16; \
247 1.1 itohy } else { \
248 1.1 itohy if (fread(&b1.half[1], SIZE_16, 1, fpa1) != 1)\
249 1.1 itohy ERR1 \
250 1.1 itohy if (fread(&b2.half[1], SIZE_16, 1, fpa2) != 1)\
251 1.1 itohy ERR2 \
252 1.1 itohy nbuf = 2; \
253 1.1 itohy segsize -= SIZE_16; \
254 1.1 itohy } \
255 1.1 itohy } else /* if (nbuf == 2) */ { \
256 1.1 itohy if (b1.hostval != b2.hostval && \
257 1.1 itohy get_uint32(&b1) - loadadr1 \
258 1.1 itohy == get_uint32(&b2) - loadadr2) {\
259 1.1 itohy /* do relocation */ \
260 1.1 itohy ADD_RELTBL(addr) \
261 1.1 itohy \
262 1.1 itohy put_uint32(&b1, get_uint32(&b1) - loadadr1); \
263 1.1 itohy DPRINTF((" v 0x%08x\t", get_uint32(&b1))); \
264 1.1 itohy fwrite(&b1, SIZE_32, 1, fpx); \
265 1.1 itohy nbuf = 0; \
266 1.1 itohy addr += SIZE_32; \
267 1.1 itohy } else if (b1.half[0].hostval == b2.half[0].hostval) {\
268 1.1 itohy fwrite(&b1.half[0], SIZE_16, 1, fpx); \
269 1.1 itohy addr += SIZE_16; \
270 1.1 itohy b1.half[0] = b1.half[1]; \
271 1.1 itohy b2.half[0] = b2.half[1]; \
272 1.1 itohy nbuf = 1; \
273 1.1 itohy } else \
274 1.1 itohy ERRC \
275 1.1 itohy } \
276 1.1 itohy }
277 1.1 itohy
278 1.1 itohy int
279 1.1 itohy aout2hux(fn1, fn2, loadadr1, loadadr2, fnx)
280 1.1 itohy const char *fn1, *fn2, *fnx;
281 1.1 itohy u_int32_t loadadr1, loadadr2;
282 1.1 itohy {
283 1.1 itohy int status = 1; /* the default is "failed" */
284 1.1 itohy FILE *fpa1 = NULL, *fpa2 = NULL;
285 1.1 itohy struct aout_m68k hdr1, hdr2;
286 1.1 itohy FILE *fpx = NULL;
287 1.1 itohy struct huxhdr xhdr;
288 1.1 itohy u_int32_t textsize, datasize, pagesize, paddingsize, execoff;
289 1.1 itohy
290 1.1 itohy /* for relocation */
291 1.1 itohy be_uint32_t b1, b2;
292 1.1 itohy int nbuf;
293 1.1 itohy u_int32_t addr;
294 1.1 itohy
295 1.1 itohy /* for relocation table */
296 1.1 itohy size_t relsize, relallocsize;
297 1.1 itohy u_int32_t reladdr;
298 1.1 itohy char *reltbl = NULL;
299 1.1 itohy
300 1.1 itohy
301 1.1 itohy /*
302 1.1 itohy * check load addresses
303 1.1 itohy */
304 1.1 itohy if (loadadr1 == loadadr2) {
305 1.1 itohy fprintf(stderr, "two load addresses must be different\n");
306 1.1 itohy return 1;
307 1.1 itohy }
308 1.1 itohy
309 1.1 itohy /*
310 1.1 itohy * open a.out files and check the headers
311 1.1 itohy */
312 1.1 itohy if (!(fpa1 = open_aout(fn1, &hdr1)) || !(fpa2 = open_aout(fn2, &hdr2)))
313 1.1 itohy goto out;
314 1.1 itohy
315 1.1 itohy /*
316 1.1 itohy * check for consistency
317 1.1 itohy */
318 1.1 itohy if (check_2_aout_hdrs(&hdr1, &hdr2)) {
319 1.1 itohy fprintf(stderr, "files %s and %s are incompatible\n",
320 1.1 itohy fn1, fn2);
321 1.1 itohy goto out;
322 1.1 itohy }
323 1.1 itohy /* check entry address */
324 1.1 itohy if (get_uint32(&hdr1.a_entry) - loadadr1 !=
325 1.1 itohy get_uint32(&hdr2.a_entry) - loadadr2) {
326 1.1 itohy fprintf(stderr, "address of %s or %s may be incorrect\n",
327 1.1 itohy fn1, fn2);
328 1.1 itohy goto out;
329 1.1 itohy }
330 1.1 itohy
331 1.1 itohy /*
332 1.1 itohy * get information from a.out header
333 1.1 itohy */
334 1.1 itohy textsize = get_uint32(&hdr1.a_text);
335 1.1 itohy pagesize = AOUT_PAGESIZE(&hdr1);
336 1.1 itohy paddingsize = ((textsize + pagesize - 1) & ~(pagesize - 1)) - textsize;
337 1.1 itohy datasize = get_uint32(&hdr1.a_data);
338 1.1 itohy execoff = get_uint32(&hdr1.a_entry) - loadadr1;
339 1.1 itohy
340 1.1 itohy DPRINTF(("text: %u, data: %u, page: %u, pad: %u, bss: %u, exec: %u\n",
341 1.1 itohy textsize, datasize, pagesize, paddingsize,
342 1.1 itohy get_uint32(&hdr1.a_bss), execoff));
343 1.1 itohy
344 1.1 itohy if (textsize & 1) {
345 1.1 itohy fprintf(stderr, "text size is not even\n");
346 1.1 itohy goto out;
347 1.1 itohy }
348 1.1 itohy if (datasize & 1) {
349 1.1 itohy fprintf(stderr, "data size is not even\n");
350 1.1 itohy goto out;
351 1.1 itohy }
352 1.1 itohy if (execoff >= textsize &&
353 1.1 itohy (execoff < textsize + paddingsize ||
354 1.1 itohy execoff >= textsize + paddingsize + datasize)) {
355 1.1 itohy fprintf(stderr, "exec addr is not in text or data segment\n");
356 1.1 itohy goto out;
357 1.1 itohy }
358 1.1 itohy
359 1.1 itohy /*
360 1.1 itohy * prepare for .x header
361 1.1 itohy */
362 1.1 itohy bzero((void *) &xhdr, sizeof xhdr);
363 1.1 itohy put_uint16(&xhdr.x_magic, HUXMAGIC);
364 1.1 itohy put_uint32(&xhdr.x_entry, execoff);
365 1.1 itohy put_uint32(&xhdr.x_text, textsize + paddingsize);
366 1.1 itohy xhdr.x_data.hostval = hdr1.a_data.hostval;
367 1.1 itohy xhdr.x_bss.hostval = hdr1.a_bss.hostval;
368 1.1 itohy
369 1.1 itohy /*
370 1.1 itohy * create output file
371 1.1 itohy */
372 1.1 itohy if (!(fpx = fopen(fnx, "w")) ||
373 1.1 itohy fseek(fpx, (off_t) sizeof xhdr, SEEK_SET)) { /* skip header */
374 1.1 itohy perror(fnx);
375 1.1 itohy goto out;
376 1.1 itohy }
377 1.1 itohy
378 1.1 itohy addr = 0;
379 1.1 itohy nbuf = 0;
380 1.1 itohy
381 1.1 itohy relsize = relallocsize = 0;
382 1.1 itohy reladdr = 0;
383 1.1 itohy
384 1.1 itohy /*
385 1.1 itohy * text segment
386 1.1 itohy */
387 1.1 itohy CREATE_RELOCATION(textsize)
388 1.1 itohy
389 1.1 itohy /*
390 1.1 itohy * page boundary
391 1.1 itohy */
392 1.1 itohy addr += paddingsize;
393 1.1 itohy while (paddingsize--)
394 1.1 itohy putc('\0', fpx);
395 1.1 itohy
396 1.1 itohy /*
397 1.1 itohy * data segment
398 1.1 itohy */
399 1.1 itohy CREATE_RELOCATION(datasize)
400 1.1 itohy
401 1.1 itohy /*
402 1.1 itohy * error check of the above
403 1.1 itohy */
404 1.1 itohy if (ferror(fpx)) {
405 1.1 itohy fprintf(stderr, "%s: write failure\n", fnx);
406 1.1 itohy goto out;
407 1.1 itohy }
408 1.1 itohy
409 1.1 itohy /*
410 1.1 itohy * write relocation table
411 1.1 itohy */
412 1.1 itohy if (relsize > 0) {
413 1.1 itohy DPRINTF(("\n"));
414 1.1 itohy if (fwrite(reltbl, 1, relsize, fpx) != relsize) {
415 1.1 itohy perror(fnx);
416 1.1 itohy goto out;
417 1.1 itohy }
418 1.1 itohy }
419 1.1 itohy
420 1.1 itohy /*
421 1.1 itohy * write .x header at the top of the output file
422 1.1 itohy */
423 1.1 itohy put_uint32(&xhdr.x_rsize, relsize);
424 1.1 itohy if (fseek(fpx, (off_t) 0, SEEK_SET) ||
425 1.1 itohy fwrite(&xhdr, sizeof xhdr, 1, fpx) != 1) {
426 1.1 itohy perror(fnx);
427 1.1 itohy goto out;
428 1.1 itohy }
429 1.1 itohy
430 1.1 itohy status = 0; /* all OK */
431 1.1 itohy
432 1.1 itohy out: /*
433 1.1 itohy * cleanup
434 1.1 itohy */
435 1.1 itohy if (fpa1)
436 1.1 itohy fclose(fpa1);
437 1.1 itohy if (fpa2)
438 1.1 itohy fclose(fpa2);
439 1.1 itohy if (fpx) {
440 1.1 itohy if (fclose(fpx) && status == 0) {
441 1.1 itohy /* Alas, final flush failed! */
442 1.1 itohy perror(fnx);
443 1.1 itohy status = 1;
444 1.1 itohy }
445 1.1 itohy if (status)
446 1.1 itohy remove(fnx);
447 1.1 itohy }
448 1.1 itohy if (reltbl)
449 1.1 itohy free(reltbl);
450 1.1 itohy
451 1.1 itohy return status;
452 1.1 itohy }
453 1.1 itohy
454 1.1 itohy #ifndef NO_BIST
455 1.1 itohy void bist PROTO((void));
456 1.1 itohy
457 1.1 itohy /*
458 1.1 itohy * built-in self test
459 1.1 itohy */
460 1.1 itohy void
461 1.1 itohy bist()
462 1.1 itohy {
463 1.1 itohy be_uint16_t be16;
464 1.1 itohy be_uint32_t be32;
465 1.1 itohy be_uint32_t be32x2[2];
466 1.1 itohy
467 1.1 itohy be16.val[0] = 0x12; be16.val[1] = 0x34;
468 1.1 itohy be32.val[0] = 0xfe; be32.val[1] = 0xdc;
469 1.1 itohy be32.val[2] = 0xba; be32.val[3] = 0x98;
470 1.1 itohy
471 1.1 itohy put_uint16(&be32x2[0].half[1], 0x4567);
472 1.1 itohy put_uint32(&be32x2[1], 0xa9876543);
473 1.1 itohy
474 1.1 itohy if (sizeof(u_int8_t) != 1 || sizeof(u_int16_t) != 2 ||
475 1.1 itohy sizeof(u_int32_t) != 4 ||
476 1.1 itohy SIZE_16 != 2 || SIZE_32 != 4 || sizeof be32x2 != 8 ||
477 1.1 itohy sizeof(struct relinf_s) != 2 || sizeof(struct relinf_l) != 6 ||
478 1.1 itohy get_uint16(&be16) != 0x1234 || get_uint32(&be32) != 0xfedcba98 ||
479 1.1 itohy get_uint16(&be32x2[0].half[1]) != 0x4567 ||
480 1.1 itohy get_uint32(&be32x2[1]) != 0xa9876543) {
481 1.1 itohy fprintf(stderr, "BIST failed\n");
482 1.1 itohy exit(1);
483 1.1 itohy }
484 1.1 itohy }
485 1.1 itohy #endif
486 1.1 itohy
487 1.1 itohy int
488 1.1 itohy gethex(pval, str)
489 1.1 itohy u_int32_t *pval;
490 1.1 itohy const char *str;
491 1.1 itohy {
492 1.1 itohy const unsigned char *p = (const unsigned char *) str;
493 1.1 itohy u_int32_t val;
494 1.1 itohy int over;
495 1.1 itohy
496 1.1 itohy /* skip leading "0x" if exists */
497 1.1 itohy if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
498 1.1 itohy p += 2;
499 1.1 itohy
500 1.1 itohy if (!*p)
501 1.1 itohy goto bad;
502 1.1 itohy
503 1.1 itohy for (val = 0, over = 0; *p; p++) {
504 1.1 itohy int digit;
505 1.1 itohy
506 1.1 itohy switch (*p) {
507 1.1 itohy case '0': case '1': case '2': case '3': case '4':
508 1.1 itohy case '5': case '6': case '7': case '8': case '9':
509 1.1 itohy digit = *p - '0';
510 1.1 itohy break;
511 1.1 itohy case 'a': case 'A': digit = 10; break;
512 1.1 itohy case 'b': case 'B': digit = 11; break;
513 1.1 itohy case 'c': case 'C': digit = 12; break;
514 1.1 itohy case 'd': case 'D': digit = 13; break;
515 1.1 itohy case 'e': case 'E': digit = 14; break;
516 1.1 itohy case 'f': case 'F': digit = 15; break;
517 1.1 itohy default:
518 1.1 itohy goto bad;
519 1.1 itohy }
520 1.1 itohy if (val >= 0x10000000 && !over)
521 1.1 itohy over = 1;
522 1.1 itohy val = (val << 4) | digit;
523 1.1 itohy }
524 1.1 itohy
525 1.1 itohy if (over)
526 1.1 itohy fprintf(stderr, "warning: %s: constant overflow\n", str);
527 1.1 itohy
528 1.1 itohy *pval = val;
529 1.1 itohy
530 1.1 itohy DPRINTF(("gethex: %s -> 0x%x\n", str, val));
531 1.1 itohy
532 1.1 itohy return 0;
533 1.1 itohy
534 1.1 itohy bad:
535 1.1 itohy fprintf(stderr, "%s: not a hexadecimal number\n", str);
536 1.1 itohy return 1;
537 1.1 itohy }
538 1.1 itohy
539 1.1 itohy void
540 1.1 itohy usage(name)
541 1.1 itohy const char *name;
542 1.1 itohy {
543 1.1 itohy
544 1.1 itohy fprintf(stderr, "\
545 1.1 itohy usage: %s [ -o output.x ] a.out1 loadaddr1 a.out2 loadaddr2\n\n\
546 1.1 itohy The input a.out files must be static OMAGIC/NMAGIC m68k executable.\n\
547 1.1 itohy Two a.out files must have different loading addresses.\n\
548 1.1 itohy Each of the load address must be a hexadecimal number.\n\
549 1.1 itohy The default output filename is \"%s\".\n" ,name, DEFAULT_OUTPUT_FILE);
550 1.1 itohy
551 1.1 itohy exit(1);
552 1.1 itohy }
553 1.1 itohy
554 1.1 itohy int
555 1.1 itohy main(argc, argv)
556 1.1 itohy int argc;
557 1.1 itohy char *argv[];
558 1.1 itohy {
559 1.1 itohy const char *outfile = DEFAULT_OUTPUT_FILE;
560 1.1 itohy u_int32_t adr1, adr2;
561 1.1 itohy
562 1.1 itohy #ifndef NO_BIST
563 1.1 itohy bist();
564 1.1 itohy #endif
565 1.1 itohy
566 1.1 itohy if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'o' && !argv[1][2]) {
567 1.1 itohy outfile = argv[2];
568 1.1 itohy argv += 2;
569 1.1 itohy argc -= 2;
570 1.1 itohy }
571 1.1 itohy
572 1.1 itohy if (argc != 5)
573 1.1 itohy usage(argv[0]);
574 1.1 itohy
575 1.1 itohy if (gethex(&adr1, argv[2]) || gethex(&adr2, argv[4]))
576 1.1 itohy usage(argv[0]);
577 1.1 itohy
578 1.1 itohy return aout2hux(argv[1], argv[3], adr1, adr2, outfile);
579 1.1 itohy }
580