elf2bb.c revision 1.17.2.1 1 /* $NetBSD: elf2bb.c,v 1.17.2.1 2021/04/03 22:28:15 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996,2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ignatios Souvatzis.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38
39 #include <err.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include <sys/mman.h> /* of the machine we're running on */
47
48 #ifndef HAVE_NBTOOL_CONFIG_H
49 #include <sys/endian.h> /* of the machine we're running on */
50 #endif
51
52 #include <sys/exec_elf.h> /* TARGET */
53 #ifndef R_68K_32 /* XXX host not m68k XXX */
54 #define R_68K_32 1
55 #define R_68K_PC32 4
56 #define R_68K_PC16 5
57 #endif
58
59 #include "elf2bb.h"
60 #include "chksum.h"
61
62 void usage(void);
63 int intcmp(const void *, const void *);
64 int main(int argc, char *argv[]);
65
66 #ifdef DEBUG
67 #define dprintf(x) if (debug) printf x
68 #else
69 #define dprintf(x)
70 #endif
71 int debug;
72
73 #define BBSIZE 8192
74
75 char *progname;
76 int bbsize = BBSIZE;
77 u_int8_t *buffer;
78 u_int32_t *relbuf;
79 /* can't have more relocs than that*/
80
81 int
82 intcmp(const void *i, const void *j)
83 {
84 int r;
85
86 r = (*(u_int32_t *)i) < (*(u_int32_t *)j);
87
88 return 2*r-1;
89 }
90
91 int
92 main(int argc, char *argv[])
93 {
94 int ifd, ofd;
95 u_int mid, flags, magic;
96 void *image;
97 Elf32_Ehdr *eh;
98 Elf32_Shdr *sh;
99 char *shstrtab;
100 Elf32_Sym *symtab;
101 char *strtab;
102 int eval(Elf32_Sym *, u_int32_t *);
103 u_int32_t *lptr;
104 int i, l, delta;
105 u_int8_t *rpo;
106 u_int32_t oldaddr, addrdiff;
107 u_int32_t tsz, dsz, bsz, trsz, drsz, entry, relver;
108 u_int32_t pcrelsz, r32sz;
109 int sumsize = 16;
110 int c;
111 u_int32_t *sect_offset;
112 int undefsyms;
113 uint32_t tmp32;
114 uint16_t tmp16;
115 int Sflag = 0;
116
117 progname = argv[0];
118
119 /* insert getopt here, if needed */
120 while ((c = getopt(argc, argv, "dFS")) != -1)
121 switch(c) {
122 case 'F':
123 sumsize = 2;
124 break;
125 case 'S':
126 /* Dynamically size second-stage boot */
127 Sflag = 1;
128 break;
129 case 'd':
130 debug = 1;
131 break;
132 default:
133 usage();
134 }
135 argv += optind;
136 argc -= optind;
137
138 if (argc < 2)
139 usage();
140
141 ifd = open(argv[0], O_RDONLY, 0);
142 if (ifd < 0)
143 err(1, "Can't open %s", argv[0]);
144
145 image = mmap(0, 65536, PROT_READ, MAP_FILE|MAP_PRIVATE, ifd, 0);
146 if (image == MAP_FAILED)
147 err(1, "Can't mmap %s", argv[1]);
148
149 eh = (Elf32_Ehdr *)image; /* XXX endianness */
150
151 dprintf(("%04x sections, offset %08x\n", htobe16(eh->e_shnum), htobe32(eh->e_shoff)));
152 if (htobe16(eh->e_type) != ET_REL)
153 errx(1, "%s isn't a relocatable file, type=%d",
154 argv[0], htobe16(eh->e_type));
155 if (htobe16(eh->e_machine) != EM_68K)
156 errx(1, "%s isn't M68K, machine=%d", argv[0],
157 htobe16(eh->e_machine));
158
159 /* Calculate sizes from section headers. */
160 tsz = dsz = bsz = trsz = 0;
161 sh = (Elf32_Shdr *)(image + htobe32(eh->e_shoff));
162 shstrtab = (char *)(image + htobe32(sh[htobe16(eh->e_shstrndx)].sh_offset));
163 symtab = NULL; /*XXX*/
164 strtab = NULL; /*XXX*/
165 dprintf((" name type flags addr offset size align\n"));
166 for (i = 0; i < htobe16(eh->e_shnum); ++i) {
167 u_int32_t sh_size;
168
169 dprintf( ("%2d: %08x %-16s %08x %08x %08x %08x %08x %08x\n", i,
170 htobe32(sh[i].sh_name), shstrtab + htobe32(sh[i].sh_name),
171 htobe32(sh[i].sh_type),
172 htobe32(sh[i].sh_flags), htobe32(sh[i].sh_addr),
173 htobe32(sh[i].sh_offset), htobe32(sh[i].sh_size),
174 htobe32(sh[i].sh_addralign)));
175 sh_size = (htobe32(sh[i].sh_size) + htobe32(sh[i].sh_addralign) - 1) &
176 -htobe32(sh[i].sh_addralign);
177 /* If section allocates memory, add to text, data, or bss size. */
178 if (htobe32(sh[i].sh_flags) & SHF_ALLOC) {
179 if (htobe32(sh[i].sh_type) == SHT_PROGBITS) {
180 if (htobe32(sh[i].sh_flags) & SHF_WRITE)
181 dsz += sh_size;
182 else
183 tsz += sh_size;
184 } else
185 bsz += sh_size;
186 /* If it's relocations, add to relocation count */
187 } else if (htobe32(sh[i].sh_type) == SHT_RELA) {
188 trsz += htobe32(sh[i].sh_size);
189 }
190 /* Check for SHT_REL? */
191 /* Get symbol table location. */
192 else if (htobe32(sh[i].sh_type) == SHT_SYMTAB) {
193 symtab = (Elf32_Sym *)(image + htobe32(sh[i].sh_offset));
194 } else if (strcmp(".strtab", shstrtab + htobe32(sh[i].sh_name)) == 0) {
195 strtab = image + htobe32(sh[i].sh_offset);
196 }
197 }
198 dprintf(("tsz = 0x%x, dsz = 0x%x, bsz = 0x%x, total 0x%x\n",
199 tsz, dsz, bsz, tsz + dsz + bsz));
200
201 if (trsz == 0)
202 errx(1, "%s has no relocation records.", argv[0]);
203
204 dprintf(("%d relocs\n", trsz/12));
205
206 if (Sflag) {
207 /*
208 * For second-stage boot, there's no limit for binary size,
209 * and we dynamically scale it. However, it should be small
210 * enough so that
211 *
212 * (1) all R_68K_PC16 symbols get relocated, and
213 *
214 * (2) all values in our relocation table for R_68K_32
215 * symbols fit within 16-bit integer.
216 *
217 * Both will be checked by codes below.
218 *
219 * At the moment, (2) is satisfied with sufficient margin.
220 * But if it is not the case in the future, format for
221 * relocation table should be modified.
222 */
223 bbsize = roundup(tsz + dsz, 512);
224 sumsize = bbsize / 512;
225 } else {
226 /*
227 * We have one contiguous area allocated by the ROM to us.
228 */
229 if (tsz + dsz + bsz > bbsize)
230 errx(1, "%s: resulting image too big %d+%d+%d=%d",
231 argv[0], tsz, dsz, bsz, tsz + dsz + bsz);
232 }
233
234 buffer = NULL;
235 relbuf = NULL;
236
237 retry:
238 pcrelsz = r32sz = 0;
239
240 buffer = realloc(buffer, bbsize);
241 relbuf = realloc(relbuf, bbsize);
242 if (buffer == NULL || relbuf == NULL)
243 err(1, "Unable to allocate memory\n");
244
245 memset(buffer, 0, bbsize);
246
247 /* Allocate and load loadable sections */
248 sect_offset = (u_int32_t *)malloc(htobe16(eh->e_shnum) * sizeof(u_int32_t));
249 for (i = 0, l = 0; i < htobe16(eh->e_shnum); ++i) {
250 if (htobe32(sh[i].sh_flags) & SHF_ALLOC) {
251 dprintf(("vaddr 0x%04x size 0x%04x offset 0x%04x section %s\n",
252 l, htobe32(sh[i].sh_size), htobe32(sh[i].sh_offset),
253 shstrtab + htobe32(sh[i].sh_name)));
254 if (htobe32(sh[i].sh_type) == SHT_PROGBITS)
255 memcpy(buffer + l, image + htobe32(sh[i].sh_offset),
256 htobe32(sh[i].sh_size));
257 sect_offset[i] = l;
258 l += (htobe32(sh[i].sh_size) + htobe32(sh[i].sh_addralign) - 1) &
259 -htobe32(sh[i].sh_addralign);
260 }
261 }
262
263 /*
264 * Hm. This tool REALLY should understand more than one
265 * relocator version. For now, check that the relocator at
266 * the image start does understand what we output.
267 */
268 relver = htobe32(*(u_int32_t *)(buffer + 4));
269 switch (relver) {
270 default:
271 errx(1, "%s: unrecognized relocator version %d",
272 argv[0], relver);
273 /*NOTREACHED*/
274
275 case RELVER_RELATIVE_BYTES:
276 rpo = buffer + bbsize - 1;
277 delta = -1;
278 break;
279
280 case RELVER_RELATIVE_BYTES_FORWARD:
281 rpo = buffer + tsz + dsz;
282 delta = +1;
283 *(u_int16_t *)(buffer + 14) = htobe16(tsz + dsz);
284 break;
285 }
286
287 if (symtab == NULL)
288 errx(1, "No symbol table found");
289 /*
290 * Link sections and generate relocation data
291 * Nasty: .text, .rodata, .data, .bss sections are not linked
292 * Symbol table values relative to start of sections.
293 * For each relocation entry:
294 * Symbol value needs to be calculated: value + section offset
295 * Image data adjusted to calculated value of symbol + addend
296 * Add relocation table entry for 32-bit relocatable values
297 * PC-relative entries will be absolute and don't need relocation
298 */
299 undefsyms = 0;
300 for (i = 0; i < htobe16(eh->e_shnum); ++i) {
301 int n;
302 Elf32_Rela *ra;
303 u_int8_t *base;
304
305 if (htobe32(sh[i].sh_type) != SHT_RELA)
306 continue;
307 base = NULL;
308 if (strncmp(shstrtab + htobe32(sh[i].sh_name), ".rela", 5) != 0)
309 err(1, "bad relocation section name %s", shstrtab +
310 htobe32(sh[i].sh_name));
311 for (n = 0; n < htobe16(eh->e_shnum); ++n) {
312 if (strcmp(shstrtab + htobe32(sh[i].sh_name) + 5, shstrtab +
313 htobe32(sh[n].sh_name)) != 0)
314 continue;
315 base = buffer + sect_offset[n];
316 break;
317 }
318 if (base == NULL)
319 errx(1, "Can't find section for reloc %s", shstrtab +
320 htobe32(sh[i].sh_name));
321 ra = (Elf32_Rela *)(image + htobe32(sh[i].sh_offset));
322 for (n = 0; n < htobe32(sh[i].sh_size); n += sizeof(Elf32_Rela), ++ra) {
323 Elf32_Sym *s;
324 int value;
325
326 s = &symtab[ELF32_R_SYM(htobe32(ra->r_info))];
327 if (s->st_shndx == ELF_SYM_UNDEFINED) {
328 fprintf(stderr, "Undefined symbol: %s\n",
329 strtab + s->st_name);
330 ++undefsyms;
331 }
332 value = htobe32(ra->r_addend) + eval(s, sect_offset);
333 dprintf(("reloc %04x info %04x (type %d sym %d) add 0x%x val %x\n",
334 htobe32(ra->r_offset), htobe32(ra->r_info),
335 ELF32_R_TYPE(htobe32(ra->r_info)),
336 ELF32_R_SYM(htobe32(ra->r_info)),
337 htobe32(ra->r_addend), value));
338 switch (ELF32_R_TYPE(htobe32(ra->r_info))) {
339 case R_68K_32:
340 tmp32 = htobe32(value);
341 memcpy(base + htobe32(ra->r_offset), &tmp32,
342 sizeof(tmp32));
343 relbuf[r32sz++] = (base - buffer) + htobe32(ra->r_offset);
344 break;
345 case R_68K_PC32:
346 ++pcrelsz;
347 tmp32 = htobe32(value - htobe32(ra->r_offset));
348 memcpy(base + htobe32(ra->r_offset), &tmp32,
349 sizeof(tmp32));
350 break;
351 case R_68K_PC16:
352 ++pcrelsz;
353 value -= htobe32(ra->r_offset);
354 if (value < -0x8000 || value > 0x7fff)
355 errx(1, "PC-relative offset out of range: %x\n",
356 value);
357 tmp16 = htobe16(value);
358 memcpy(base + htobe32(ra->r_offset), &tmp16,
359 sizeof(tmp16));
360 break;
361 default:
362 errx(1, "Relocation type %d not supported",
363 ELF32_R_TYPE(htobe32(ra->r_info)));
364 }
365 }
366 }
367 dprintf(("%d PC-relative relocations, %d 32-bit relocations\n",
368 pcrelsz, r32sz));
369 printf("%d absolute reloc%s found, ", r32sz, r32sz==1?"":"s");
370
371 i = r32sz;
372 if (i > 1)
373 heapsort(relbuf, r32sz, 4, intcmp);
374
375 oldaddr = 0;
376
377 for (--i; i>=0; --i) {
378 dprintf(("0x%04x: ", relbuf[i]));
379 lptr = (u_int32_t *)&buffer[relbuf[i]];
380 addrdiff = relbuf[i] - oldaddr;
381 dprintf(("(0x%04x, 0x%04x): ", *lptr, addrdiff));
382 if (addrdiff > 0xffff) {
383 errx(1, "addrdiff overflows: relbuf = 0x%08x, "
384 "oldaddr = 0x%08x, abort.\n", relbuf[i], oldaddr);
385 } else if (addrdiff > 0xff) {
386 *rpo = 0;
387 if (delta > 0) {
388 ++rpo;
389 *rpo++ = (relbuf[i] >> 8) & 0xff;
390 *rpo++ = relbuf[i] & 0xff;
391 dprintf(("%02x%02x%02x\n",
392 rpo[-3], rpo[-2], rpo[-1]));
393 } else {
394 *--rpo = relbuf[i] & 0xff;
395 *--rpo = (relbuf[i] >> 8) & 0xff;
396 --rpo;
397 dprintf(("%02x%02x%02x\n",
398 rpo[0], rpo[1], rpo[2]));
399 }
400 } else {
401 *rpo = addrdiff;
402 dprintf(("%02x\n", *rpo));
403 rpo += delta;
404 }
405
406 oldaddr = relbuf[i];
407
408 if (delta < 0 ? rpo <= buffer+tsz+dsz
409 : rpo >= buffer + bbsize) {
410 printf("relocs don't fit, ");
411 if (Sflag) {
412 printf("retry.\n");
413 bbsize += 512;
414 sumsize++;
415 goto retry;
416 } else
417 errx(1, "abort.");
418 }
419 }
420 *rpo = 0; rpo += delta;
421 *rpo = 0; rpo += delta;
422 *rpo = 0; rpo += delta;
423
424 printf("using %d bytes, %d bytes remaining.\n", delta > 0 ?
425 rpo-buffer-tsz-dsz : buffer+bbsize-rpo, delta > 0 ?
426 buffer + bbsize - rpo : rpo - buffer - tsz - dsz);
427 /*
428 * RELOCs must fit into the bss area.
429 */
430 if (delta < 0 ? rpo <= buffer+tsz+dsz
431 : rpo >= buffer + bbsize) {
432 printf("relocs don't fit, ");
433 if (Sflag) {
434 printf("retry.\n");
435 bbsize += 512;
436 sumsize++;
437 goto retry;
438 } else
439 errx(1, "abort.");
440 }
441
442 if (undefsyms > 0)
443 errx(1, "Undefined symbols referenced");
444
445 ((u_int32_t *)buffer)[1] = 0;
446 ((u_int32_t *)buffer)[1] =
447 htobe32((0xffffffff - chksum((u_int32_t *)buffer, sumsize * 512 / 4)));
448
449 ofd = open(argv[1], O_CREAT|O_WRONLY, 0644);
450 if (ofd < 0)
451 err(1, "Can't open %s", argv[1]);
452
453 if (write(ofd, buffer, bbsize) != bbsize)
454 err(1, "Writing output file");
455
456 exit(0);
457 }
458
459 void
460 usage(void)
461 {
462 fprintf(stderr, "Usage: %s [-F] bootprog bootprog.bin\n",
463 progname);
464 exit(1);
465 /* NOTREACHED */
466 }
467
468 int
469 eval(Elf32_Sym *s, u_int32_t *o)
470 {
471 int value;
472
473 value = htobe32(s->st_value);
474 if (htobe16(s->st_shndx) < 0xf000)
475 value += o[htobe16(s->st_shndx)];
476 else
477 printf("eval: %x\n", htobe16(s->st_shndx));
478 return value;
479 }
480