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