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