elf2bb.c revision 1.1 1 /* $NetBSD: elf2bb.c,v 1.1 2001/12/19 06:51:05 mhitch Exp $ */
2
3 /*-
4 * Copyright (c) 1996 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40
41 #include <err.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include <sys/mman.h> /* of the machine we're running on */
49 #include <machine/endian.h> /* of the machine we're running on */
50
51 #include <elf.h> /* TARGET */
52 #ifndef R_68K_32 /* XXX host not m68k XXX */
53 #define R_68K_32 1
54 #define R_68K_PC32 4
55 #define R_68K_PC16 5
56 #endif
57
58 #include "elf2bb.h"
59 #include "chksum.h"
60
61 void usage __P((void));
62 int intcmp(const void *, const void *);
63 int main(int argc, char *argv[]);
64
65 #ifdef DEBUG
66 #define dprintf(x) if (debug) printf x
67 #else
68 #define dprintf(x)
69 #endif
70 int debug;
71
72 #define BBSIZE 8192
73
74 char *progname;
75 int bbsize = BBSIZE;
76 u_int8_t *buffer;
77 u_int32_t *relbuf;
78 /* can't have more relocs than that*/
79
80 int
81 intcmp(i, j)
82 const void *i, *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(argc, argv)
93 int argc;
94 char *argv[];
95 {
96 int ifd, ofd;
97 u_int mid, flags, magic;
98 caddr_t image;
99 Elf32_Ehdr *eh;
100 Elf32_Shdr *sh;
101 char *shstrtab;
102 Elf32_Sym *symtab;
103 char *strtab;
104 int eval(Elf32_Sym *, u_int32_t *);
105 u_int32_t *lptr;
106 int i, l, delta;
107 u_int8_t *rpo;
108 u_int32_t oldaddr, addrdiff;
109 u_int32_t tsz, dsz, bsz, trsz, drsz, entry, relver;
110 u_int32_t pcrelsz, r32sz;
111 int sumsize = 16;
112 int c;
113 u_int32_t *sect_offset;
114
115
116 progname = argv[0];
117
118 /* insert getopt here, if needed */
119 while ((c = getopt(argc, argv, "dFS:")) != -1)
120 switch(c) {
121 case 'F':
122 sumsize = 2;
123 break;
124 case 'S':
125 bbsize = (atoi(optarg) + 511) & ~511;
126 sumsize = bbsize / 512;
127 break;
128 case 'd':
129 debug = 1;
130 break;
131 default:
132 usage();
133 }
134 argv += optind;
135 argc -= optind;
136
137 if (argc < 2)
138 usage();
139
140 buffer = malloc(bbsize);
141 relbuf = (u_int32_t *)malloc(bbsize);
142 if (buffer == NULL || relbuf == NULL)
143 err(1, "Unable to allocate memory\n");
144
145 ifd = open(argv[0], O_RDONLY, 0);
146 if (ifd < 0)
147 err(1, "Can't open %s", argv[0]);
148
149 image = mmap(0, 65536, PROT_READ, MAP_FILE|MAP_PRIVATE, ifd, 0);
150 if (image == 0)
151 err(1, "Can't mmap %s", argv[1]);
152
153 eh = (Elf32_Ehdr *)image; /* XXX endianness */
154
155 dprintf(("%04x sections, offset %08x\n", htobe16(eh->e_shnum), htobe32(eh->e_shoff)));
156 if (htobe16(eh->e_type) != ET_REL)
157 errx(1, "%s isn't a relocatable file, type=%d\n",
158 argv[0], htobe16(eh->e_type));
159 if (htobe16(eh->e_machine) != EM_68K)
160 errx(1, "%s isn't M68K, machine=%d\n", argv[0],
161 htobe16(eh->e_machine));
162
163 tsz = dsz = bsz = trsz = pcrelsz = r32sz = 0;
164 sh = (Elf32_Shdr *)(image + htobe32(eh->e_shoff));
165 shstrtab = (char *)(image + htobe32(sh[htobe16(eh->e_shstrndx)].sh_offset));
166 symtab = NULL; /*XXX*/
167 strtab = NULL; /*XXX*/
168 dprintf((" name type flags addr offset size align\n"));
169 for (i = 0; i < htobe16(eh->e_shnum); ++i) {
170 u_int32_t sh_size;
171
172 dprintf( ("%2d: %08x %-16s %08x %08x %08x %08x %08x %08x\n", i,
173 htobe32(sh[i].sh_name), shstrtab + htobe32(sh[i].sh_name),
174 htobe32(sh[i].sh_type),
175 htobe32(sh[i].sh_flags), htobe32(sh[i].sh_addr),
176 htobe32(sh[i].sh_offset), htobe32(sh[i].sh_size),
177 htobe32(sh[i].sh_addralign)));
178 sh_size = (htobe32(sh[i].sh_size) + htobe32(sh[i].sh_addralign) - 1) &
179 -htobe32(sh[i].sh_addralign);
180 if (htobe32(sh[i].sh_type) == SHT_PROGBITS) {
181 if (htobe32(sh[i].sh_flags) & SHF_WRITE)
182 dsz += sh_size;
183 else
184 tsz += sh_size;
185 } else if (htobe32(sh[i].sh_type) == SHT_NOBITS &&
186 htobe32(sh[i].sh_flags) == (SHF_ALLOC | SHF_WRITE)) {
187 bsz += sh_size;;
188 } else if (htobe32(sh[i].sh_type) == SHT_RELA) {
189 trsz += htobe32(sh[i].sh_size);
190 }
191 /* Check for SHT_REL? */
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.\n", argv[0]);
203
204 dprintf(("%d relocs\n", trsz/12));
205
206 /*
207 * We have one contiguous area allocated by the ROM to us.
208 */
209 if (tsz+dsz+bsz > bbsize)
210 errx(1, "%s: resulting image too big\n", argv[0]);
211
212 memset(buffer, bbsize, 0);
213
214 /* Allocate and load loadable sections */
215 sect_offset = (u_int32_t *)malloc(htobe16(eh->e_shnum) * sizeof(u_int32_t));
216 for (i = 0, l = 0; i < htobe16(eh->e_shnum); ++i) {
217 if (htobe32(sh[i].sh_type) == SHT_PROGBITS ||
218 htobe32(sh[i].sh_type) == SHT_NOBITS) {
219 dprintf(("vaddr 0x%04x size 0x%04x offset 0x%04x section %s\n",
220 l, htobe32(sh[i].sh_size), htobe32(sh[i].sh_offset),
221 shstrtab + htobe32(sh[i].sh_name)));
222 if (htobe32(sh[i].sh_type) == SHT_PROGBITS)
223 memcpy(buffer + l, image + htobe32(sh[i].sh_offset),
224 htobe32(sh[i].sh_size));
225 sect_offset[i] = l;
226 l += (htobe32(sh[i].sh_size) + htobe32(sh[i].sh_addralign) - 1) &
227 -htobe32(sh[i].sh_addralign);
228 }
229 }
230
231 /*
232 * Hm. This tool REALLY should understand more than one
233 * relocator version. For now, check that the relocator at
234 * the image start does understand what we output.
235 */
236 relver = htobe32(*(u_int32_t *)(buffer + 4));
237 switch (relver) {
238 default:
239 errx(1, "%s: unrecognized relocator version %d\n",
240 argv[0], relver);
241 /*NOTREACHED*/
242
243 case RELVER_RELATIVE_BYTES:
244 rpo = buffer + bbsize - 1;
245 delta = -1;
246 break;
247
248 case RELVER_RELATIVE_BYTES_FORWARD:
249 rpo = buffer + tsz + dsz;
250 delta = +1;
251 *(u_int16_t *)(buffer + 14) = htobe16(tsz + dsz);
252 break;
253 }
254
255 if (symtab == NULL)
256 errx(1, "No symbol table found\n");
257 /*
258 * Link sections and generate relocation data
259 * Nasty: .text, .rodata, .data, .bss sections are not linked
260 * Symbol table values relative to start of sections.
261 * For each relocation entry:
262 * Symbol value needs to be calculated: value + section offset
263 * Image data adjusted to calculated value of symbol + addend
264 * Add relocation table entry for 32-bit relocatable values
265 * PC-relative entries will be absolute and don't need relocation
266 */
267 for (i = 0; i < htobe16(eh->e_shnum); ++i) {
268 int n;
269 Elf32_Rela *ra;
270 u_int8_t *base;
271
272 if (htobe32(sh[i].sh_type) != SHT_RELA)
273 continue;
274 base = NULL;
275 if (strncmp(shstrtab + htobe32(sh[i].sh_name), ".rela", 5) != 0)
276 err(1, "bad relocation section name %s", shstrtab +
277 htobe32(sh[i].sh_name));
278 for (n = 0; n < htobe16(eh->e_shnum); ++n) {
279 if (strcmp(shstrtab + htobe32(sh[i].sh_name) + 5, shstrtab +
280 htobe32(sh[n].sh_name)) != 0)
281 continue;
282 base = buffer + sect_offset[n];
283 break;
284 }
285 if (base == NULL)
286 errx(1, "Can't find section for reloc %s\n", shstrtab +
287 htobe32(sh[i].sh_name));
288 ra = (Elf32_Rela *)(image + htobe32(sh[i].sh_offset));
289 for (n = 0; n < htobe32(sh[i].sh_size); n += sizeof(Elf32_Rela), ++ra) {
290 int value;
291 value = htobe32(ra->r_addend) +
292 eval(&symtab[ELF32_R_SYM(htobe32(ra->r_info))], sect_offset);
293 dprintf(("reloc %04x info %04x (type %d sym %d) add 0x%x val %x\n",
294 htobe32(ra->r_offset), htobe32(ra->r_info),
295 ELF32_R_TYPE(htobe32(ra->r_info)),
296 ELF32_R_SYM(htobe32(ra->r_info)),
297 htobe32(ra->r_addend), value));
298 switch (ELF32_R_TYPE(htobe32(ra->r_info))) {
299 case R_68K_32:
300 *((u_int32_t *)(base + htobe32(ra->r_offset))) =
301 htobe32(value);
302 relbuf[r32sz++] = (base - buffer) + htobe32(ra->r_offset);
303 break;
304 case R_68K_PC32:
305 ++pcrelsz;
306 *((int32_t *)(base + htobe32(ra->r_offset))) =
307 htobe32(value - htobe32(ra->r_offset));
308 break;
309 case R_68K_PC16:
310 ++pcrelsz;
311 *((int16_t *)(base + htobe32(ra->r_offset))) =
312 htobe16(value - htobe32(ra->r_offset));
313 break;
314 default:
315 errx(1, "Relocation type %d not supported",
316 ELF32_R_TYPE(htobe32(ra->r_info)));
317 }
318 }
319 }
320 dprintf(("%d PC-relative relocations, %d 32-bit relocations\n",
321 pcrelsz, r32sz));
322 printf("%d absolute reloc%s found, ", r32sz, r32sz==1?"":"s");
323
324 i = r32sz;
325 if (i > 1)
326 heapsort(relbuf, r32sz, 4, intcmp);
327
328 oldaddr = 0;
329
330 for (--i; i>=0; --i) {
331 dprintf(("0x%04x: ", relbuf[i]));
332 lptr = (u_int32_t *)&buffer[relbuf[i]];
333 addrdiff = relbuf[i] - oldaddr;
334 dprintf(("(0x%04x, 0x%04x): ", *lptr, addrdiff));
335 if (addrdiff > 255) {
336 *rpo = 0;
337 if (delta > 0) {
338 ++rpo;
339 *rpo++ = (relbuf[i] >> 8) & 0xff;
340 *rpo++ = relbuf[i] & 0xff;
341 dprintf(("%02x%02x%02x\n",
342 rpo[-3], rpo[-2], rpo[-1]));
343 } else {
344 *--rpo = relbuf[i] & 0xff;
345 *--rpo = (relbuf[i] >> 8) & 0xff;
346 --rpo;
347 dprintf(("%02x%02x%02x\n",
348 rpo[0], rpo[1], rpo[2]));
349 }
350 } else {
351 *rpo = addrdiff;
352 dprintf(("%02x\n", *rpo));
353 rpo += delta;
354 }
355
356 oldaddr = relbuf[i];
357
358 if (delta < 0 ? rpo <= buffer+tsz+dsz
359 : rpo >= buffer + bbsize)
360 errx(1, "Relocs don't fit.");
361 }
362 *rpo = 0; rpo += delta;
363 *rpo = 0; rpo += delta;
364 *rpo = 0; rpo += delta;
365
366 printf("using %d bytes, %d bytes remaining.\n", delta > 0 ?
367 rpo-buffer-tsz-dsz : buffer+bbsize-rpo, delta > 0 ?
368 buffer + bbsize - rpo : rpo - buffer - tsz - dsz);
369 /*
370 * RELOCs must fit into the bss area.
371 */
372 if (delta < 0 ? rpo <= buffer+tsz+dsz
373 : rpo >= buffer + bbsize)
374 errx(1, "Relocs don't fit.");
375
376 ((u_int32_t *)buffer)[1] = 0;
377 ((u_int32_t *)buffer)[1] =
378 htobe32((0xffffffff - chksum((u_int32_t *)buffer, sumsize * 512 / 4)));
379
380 ofd = open(argv[1], O_CREAT|O_WRONLY, 0644);
381 if (ofd < 0)
382 err(1, "Can't open %s", argv[1]);
383
384 if (write(ofd, buffer, bbsize) != bbsize)
385 err(1, "Writing output file");
386
387 exit(0);
388 }
389
390 void
391 usage()
392 {
393 fprintf(stderr, "Usage: %s [-F] bootprog bootprog.bin\n",
394 progname);
395 exit(1);
396 /* NOTREACHED */
397 }
398
399 int
400 eval(Elf32_Sym *s, u_int32_t *o)
401 {
402 int value;
403
404 value = htobe32(s->st_value);
405 if (htobe16(s->st_shndx) < 0xf000)
406 value += o[htobe16(s->st_shndx)];
407 else
408 printf("eval: %x\n", htobe16(s->st_shndx));
409 return value;
410 }
411