xref: /freebsd/usr.sbin/btxld/btxld.c (revision eba230afba4932f02a1ca44efc797cf7499a5cb0)
11de7b4b8SPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
49c9f4492SRobert Nordier  * Copyright (c) 1998 Robert Nordier
59c9f4492SRobert Nordier  * All rights reserved.
69c9f4492SRobert Nordier  *
79c9f4492SRobert Nordier  * Redistribution and use in source and binary forms, with or without
89c9f4492SRobert Nordier  * modification, are permitted provided that the following conditions
99c9f4492SRobert Nordier  * are met:
109c9f4492SRobert Nordier  * 1. Redistributions of source code must retain the above copyright
119c9f4492SRobert Nordier  *    notice, this list of conditions and the following disclaimer.
129c9f4492SRobert Nordier  * 2. Redistributions in binary form must reproduce the above copyright
139c9f4492SRobert Nordier  *    notice, this list of conditions and the following disclaimer in the
149c9f4492SRobert Nordier  *    documentation and/or other materials provided with the distribution.
159c9f4492SRobert Nordier  *
169c9f4492SRobert Nordier  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
179c9f4492SRobert Nordier  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189c9f4492SRobert Nordier  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
199c9f4492SRobert Nordier  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
209c9f4492SRobert Nordier  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
219c9f4492SRobert Nordier  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
229c9f4492SRobert Nordier  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
239c9f4492SRobert Nordier  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
249c9f4492SRobert Nordier  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
259c9f4492SRobert Nordier  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
269c9f4492SRobert Nordier  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279c9f4492SRobert Nordier  */
289c9f4492SRobert Nordier 
29ac53b225SMike Barcroft #include <sys/param.h>
3049a39408SDavid E. O'Brien #include <sys/endian.h>
319c9f4492SRobert Nordier #include <sys/stat.h>
329c9f4492SRobert Nordier #include <sys/mman.h>
339c9f4492SRobert Nordier 
34093f1218SRuslan Ermilov /* XXX make this work as an i386/amd64 cross-tool */
35093f1218SRuslan Ermilov #include <machine/exec.h>
36093f1218SRuslan Ermilov #undef __LDPGSZ
37093f1218SRuslan Ermilov #define __LDPGSZ	4096
38093f1218SRuslan Ermilov 
3950385af9SXin LI #include <netinet/in.h>
4050385af9SXin LI 
4149a39408SDavid E. O'Brien #include <a.out.h>
429c9f4492SRobert Nordier #include <err.h>
439c9f4492SRobert Nordier #include <errno.h>
449c9f4492SRobert Nordier #include <fcntl.h>
459c9f4492SRobert Nordier #include <stdarg.h>
469c9f4492SRobert Nordier #include <stdio.h>
479c9f4492SRobert Nordier #include <stdlib.h>
489c9f4492SRobert Nordier #include <string.h>
499c9f4492SRobert Nordier #include <unistd.h>
509c9f4492SRobert Nordier 
519c9f4492SRobert Nordier #include "btx.h"
529c9f4492SRobert Nordier #include "elfh.h"
539c9f4492SRobert Nordier 
549c9f4492SRobert Nordier #define BTX_PATH		"/sys/boot/i386/btx"
559c9f4492SRobert Nordier 
569c9f4492SRobert Nordier #define I_LDR	0		/* BTX loader */
579c9f4492SRobert Nordier #define I_BTX	1		/* BTX kernel */
589c9f4492SRobert Nordier #define I_CLNT	2		/* Client program */
599c9f4492SRobert Nordier 
609c9f4492SRobert Nordier #define F_BIN	0		/* Binary */
619c9f4492SRobert Nordier #define F_AOUT	1		/* ZMAGIC a.out */
629c9f4492SRobert Nordier #define F_ELF	2		/* 32-bit ELF */
639c9f4492SRobert Nordier #define F_CNT	3		/* Number of formats */
649c9f4492SRobert Nordier 
659c9f4492SRobert Nordier #define IMPURE	1		/* Writable text */
669c9f4492SRobert Nordier #define MAXU32	0xffffffff	/* Maximum unsigned 32-bit quantity */
679c9f4492SRobert Nordier 
689c9f4492SRobert Nordier struct hdr {
69b0ca5f03SMarcel Moolenaar     uint32_t fmt;		/* Format */
70b0ca5f03SMarcel Moolenaar     uint32_t flags;		/* Bit flags */
71b0ca5f03SMarcel Moolenaar     uint32_t size;		/* Size of file */
72b0ca5f03SMarcel Moolenaar     uint32_t text;		/* Size of text segment */
73b0ca5f03SMarcel Moolenaar     uint32_t data;		/* Size of data segment */
74b0ca5f03SMarcel Moolenaar     uint32_t bss;		/* Size of bss segment */
75b0ca5f03SMarcel Moolenaar     uint32_t org;		/* Program origin */
76b0ca5f03SMarcel Moolenaar     uint32_t entry;		/* Program entry point */
779c9f4492SRobert Nordier };
789c9f4492SRobert Nordier 
799c9f4492SRobert Nordier static const char *const fmtlist[] = {"bin", "aout", "elf"};
809c9f4492SRobert Nordier 
819c9f4492SRobert Nordier static const char binfo[] =
829c9f4492SRobert Nordier     "kernel: ver=%u.%02u size=%x load=%x entry=%x map=%uM "
839c9f4492SRobert Nordier     "pgctl=%x:%x\n";
849c9f4492SRobert Nordier static const char cinfo[] =
859c9f4492SRobert Nordier     "client: fmt=%s size=%x text=%x data=%x bss=%x entry=%x\n";
869c9f4492SRobert Nordier static const char oinfo[] =
879c9f4492SRobert Nordier     "output: fmt=%s size=%x text=%x data=%x org=%x entry=%x\n";
889c9f4492SRobert Nordier 
899c9f4492SRobert Nordier static const char *lname =
909c9f4492SRobert Nordier     BTX_PATH "/btxldr/btxldr";	/* BTX loader */
919c9f4492SRobert Nordier static const char *bname =
929c9f4492SRobert Nordier     BTX_PATH "/btx/btx";	/* BTX kernel */
939c9f4492SRobert Nordier static const char *oname =
949c9f4492SRobert Nordier     "a.out";			/* Output filename */
959c9f4492SRobert Nordier 
969c9f4492SRobert Nordier static int ppage = -1;		/* First page present */
979c9f4492SRobert Nordier static int wpage = -1;		/* First page writable */
989c9f4492SRobert Nordier 
99b0ca5f03SMarcel Moolenaar static unsigned int format; 	/* Output format */
1009c9f4492SRobert Nordier 
1019c9f4492SRobert Nordier static uint32_t centry; 	/* Client entry address */
1029c9f4492SRobert Nordier static uint32_t lentry; 	/* Loader entry address */
1039c9f4492SRobert Nordier 
10438c931abSRobert Nordier static int Eflag;		/* Client entry option */
10538c931abSRobert Nordier 
1069c9f4492SRobert Nordier static int quiet;		/* Inhibit warnings */
1079c9f4492SRobert Nordier static int verbose;		/* Display information */
1089c9f4492SRobert Nordier 
1099c9f4492SRobert Nordier static const char *tname;	/* Temporary output file */
1109c9f4492SRobert Nordier static const char *fname;	/* Current input file */
1119c9f4492SRobert Nordier 
1129c9f4492SRobert Nordier static void cleanup(void);
1139c9f4492SRobert Nordier static void btxld(const char *);
1149c9f4492SRobert Nordier static void getbtx(int, struct btx_hdr *);
1159c9f4492SRobert Nordier static void gethdr(int, struct hdr *);
1169c9f4492SRobert Nordier static void puthdr(int, struct hdr *);
1179c9f4492SRobert Nordier static void copy(int, int, size_t, off_t);
1189c9f4492SRobert Nordier static size_t readx(int, void *, size_t, off_t);
1199c9f4492SRobert Nordier static void writex(int, const void *, size_t);
1209c9f4492SRobert Nordier static void seekx(int, off_t);
121b0ca5f03SMarcel Moolenaar static unsigned int optfmt(const char *);
1229c9f4492SRobert Nordier static uint32_t optaddr(const char *);
1239c9f4492SRobert Nordier static int optpage(const char *, int);
1249c9f4492SRobert Nordier static void Warn(const char *, const char *, ...);
125*72e1ea2fSAlfonso Gregory static void usage(void) __dead2;
1269c9f4492SRobert Nordier 
1279c9f4492SRobert Nordier /*
1289c9f4492SRobert Nordier  * A link editor for BTX clients.
1299c9f4492SRobert Nordier  */
1309c9f4492SRobert Nordier int
main(int argc,char * argv[])1319c9f4492SRobert Nordier main(int argc, char *argv[])
1329c9f4492SRobert Nordier {
1339c9f4492SRobert Nordier     int c;
1349c9f4492SRobert Nordier 
1359c9f4492SRobert Nordier     while ((c = getopt(argc, argv, "qvb:E:e:f:l:o:P:W:")) != -1)
1369c9f4492SRobert Nordier 	switch (c) {
1379c9f4492SRobert Nordier 	case 'q':
1389c9f4492SRobert Nordier 	    quiet = 1;
1399c9f4492SRobert Nordier 	    break;
1409c9f4492SRobert Nordier 	case 'v':
1419c9f4492SRobert Nordier 	    verbose = 1;
1429c9f4492SRobert Nordier 	    break;
1439c9f4492SRobert Nordier 	case 'b':
1449c9f4492SRobert Nordier 	    bname = optarg;
1459c9f4492SRobert Nordier 	    break;
1469c9f4492SRobert Nordier 	case 'E':
1479c9f4492SRobert Nordier 	    centry = optaddr(optarg);
14838c931abSRobert Nordier 	    Eflag = 1;
1499c9f4492SRobert Nordier 	    break;
1509c9f4492SRobert Nordier 	case 'e':
1519c9f4492SRobert Nordier 	    lentry = optaddr(optarg);
1529c9f4492SRobert Nordier 	    break;
1539c9f4492SRobert Nordier 	case 'f':
1549c9f4492SRobert Nordier 	    format = optfmt(optarg);
1559c9f4492SRobert Nordier 	    break;
1569c9f4492SRobert Nordier 	case 'l':
1579c9f4492SRobert Nordier 	    lname = optarg;
1589c9f4492SRobert Nordier 	    break;
1599c9f4492SRobert Nordier 	case 'o':
1609c9f4492SRobert Nordier 	    oname = optarg;
1619c9f4492SRobert Nordier 	    break;
1629c9f4492SRobert Nordier 	case 'P':
1639c9f4492SRobert Nordier 	    ppage = optpage(optarg, 1);
1649c9f4492SRobert Nordier 	    break;
1659c9f4492SRobert Nordier 	case 'W':
1669c9f4492SRobert Nordier 	    wpage = optpage(optarg, BTX_MAXCWR);
1679c9f4492SRobert Nordier 	    break;
1689c9f4492SRobert Nordier 	default:
1699c9f4492SRobert Nordier 	    usage();
1709c9f4492SRobert Nordier 	}
1719c9f4492SRobert Nordier     argc -= optind;
1729c9f4492SRobert Nordier     argv += optind;
1739c9f4492SRobert Nordier     if (argc != 1)
1749c9f4492SRobert Nordier 	usage();
1759c9f4492SRobert Nordier     atexit(cleanup);
1769c9f4492SRobert Nordier     btxld(*argv);
1779c9f4492SRobert Nordier     return 0;
1789c9f4492SRobert Nordier }
1799c9f4492SRobert Nordier 
1809c9f4492SRobert Nordier /*
1819c9f4492SRobert Nordier  * Clean up after errors.
1829c9f4492SRobert Nordier  */
1839c9f4492SRobert Nordier static void
cleanup(void)1849c9f4492SRobert Nordier cleanup(void)
1859c9f4492SRobert Nordier {
1869c9f4492SRobert Nordier     if (tname)
1871987e300SWarner Losh 	(void)remove(tname);
1889c9f4492SRobert Nordier }
1899c9f4492SRobert Nordier 
1909c9f4492SRobert Nordier /*
1919c9f4492SRobert Nordier  * Read the input files; write the output file; display information.
1929c9f4492SRobert Nordier  */
1939c9f4492SRobert Nordier static void
btxld(const char * iname)1949c9f4492SRobert Nordier btxld(const char *iname)
1959c9f4492SRobert Nordier {
1969c9f4492SRobert Nordier     char name[FILENAME_MAX];
1977148ee89SRuslan Ermilov     struct btx_hdr btx, btxle;
1989c9f4492SRobert Nordier     struct hdr ihdr, ohdr;
199b0ca5f03SMarcel Moolenaar     unsigned int ldr_size, cwr;
2009c9f4492SRobert Nordier     int fdi[3], fdo, i;
2019c9f4492SRobert Nordier 
202b0ca5f03SMarcel Moolenaar     ldr_size = 0;
203b0ca5f03SMarcel Moolenaar 
2049c9f4492SRobert Nordier     for (i = I_LDR; i <= I_CLNT; i++) {
2059c9f4492SRobert Nordier 	fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
2069c9f4492SRobert Nordier 	if ((fdi[i] = open(fname, O_RDONLY)) == -1)
2079c9f4492SRobert Nordier 	    err(2, "%s", fname);
2089c9f4492SRobert Nordier 	switch (i) {
2099c9f4492SRobert Nordier 	case I_LDR:
2109c9f4492SRobert Nordier 	    gethdr(fdi[i], &ihdr);
2119c9f4492SRobert Nordier 	    if (ihdr.fmt != F_BIN)
2129c9f4492SRobert Nordier 		Warn(fname, "Loader format is %s; processing as %s",
2139c9f4492SRobert Nordier 		     fmtlist[ihdr.fmt], fmtlist[F_BIN]);
2149c9f4492SRobert Nordier 	    ldr_size = ihdr.size;
2159c9f4492SRobert Nordier 	    break;
2169c9f4492SRobert Nordier 	case I_BTX:
2179c9f4492SRobert Nordier 	    getbtx(fdi[i], &btx);
2189c9f4492SRobert Nordier 	    break;
2199c9f4492SRobert Nordier 	case I_CLNT:
2209c9f4492SRobert Nordier 	    gethdr(fdi[i], &ihdr);
2219c9f4492SRobert Nordier 	    if (ihdr.org && ihdr.org != BTX_PGSIZE)
2229c9f4492SRobert Nordier 		Warn(fname,
2239c9f4492SRobert Nordier 		     "Client origin is 0x%x; expecting 0 or 0x%x",
2249c9f4492SRobert Nordier 		     ihdr.org, BTX_PGSIZE);
2259c9f4492SRobert Nordier 	}
2269c9f4492SRobert Nordier     }
2279c9f4492SRobert Nordier     memset(&ohdr, 0, sizeof(ohdr));
2289c9f4492SRobert Nordier     ohdr.fmt = format;
2299c9f4492SRobert Nordier     ohdr.text = ldr_size;
2309c9f4492SRobert Nordier     ohdr.data = btx.btx_textsz + ihdr.size;
2319c9f4492SRobert Nordier     ohdr.org = lentry;
2329c9f4492SRobert Nordier     ohdr.entry = lentry;
2339c9f4492SRobert Nordier     cwr = 0;
234b0ca5f03SMarcel Moolenaar     if (wpage > 0 || (wpage == -1 && !(ihdr.flags & IMPURE))) {
2359c9f4492SRobert Nordier 	if (wpage > 0)
2369c9f4492SRobert Nordier 	    cwr = wpage;
2379c9f4492SRobert Nordier 	else {
2389c9f4492SRobert Nordier 	    cwr = howmany(ihdr.text, BTX_PGSIZE);
2399c9f4492SRobert Nordier 	    if (cwr > BTX_MAXCWR)
2409c9f4492SRobert Nordier 		cwr = BTX_MAXCWR;
2419c9f4492SRobert Nordier 	}
242b0ca5f03SMarcel Moolenaar     }
2439c9f4492SRobert Nordier     if (ppage > 0 || (ppage && wpage && ihdr.org >= BTX_PGSIZE)) {
2449c9f4492SRobert Nordier 	btx.btx_flags |= BTX_MAPONE;
2459c9f4492SRobert Nordier 	if (!cwr)
2469c9f4492SRobert Nordier 	    cwr++;
2479c9f4492SRobert Nordier     }
2489c9f4492SRobert Nordier     btx.btx_pgctl -= cwr;
24938c931abSRobert Nordier     btx.btx_entry = Eflag ? centry : ihdr.entry;
25050385af9SXin LI     if ((size_t)snprintf(name, sizeof(name), "%s.tmp", oname) >= sizeof(name))
2519c9f4492SRobert Nordier 	errx(2, "%s: Filename too long", oname);
2529c9f4492SRobert Nordier     if ((fdo = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666)) == -1)
2539c9f4492SRobert Nordier 	err(2, "%s", name);
2549c9f4492SRobert Nordier     if (!(tname = strdup(name)))
2559c9f4492SRobert Nordier 	err(2, NULL);
2569c9f4492SRobert Nordier     puthdr(fdo, &ohdr);
2579c9f4492SRobert Nordier     for (i = I_LDR; i <= I_CLNT; i++) {
2589c9f4492SRobert Nordier 	fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
2599c9f4492SRobert Nordier 	switch (i) {
2609c9f4492SRobert Nordier 	case I_LDR:
2619c9f4492SRobert Nordier 	    copy(fdi[i], fdo, ldr_size, 0);
2629c9f4492SRobert Nordier 	    seekx(fdo, ohdr.size += ohdr.text);
2639c9f4492SRobert Nordier 	    break;
2649c9f4492SRobert Nordier 	case I_BTX:
2657148ee89SRuslan Ermilov 	    btxle = btx;
26649a39408SDavid E. O'Brien 	    btxle.btx_pgctl = htole16(btxle.btx_pgctl);
26749a39408SDavid E. O'Brien 	    btxle.btx_textsz = htole16(btxle.btx_textsz);
26849a39408SDavid E. O'Brien 	    btxle.btx_entry = htole32(btxle.btx_entry);
2697148ee89SRuslan Ermilov 	    writex(fdo, &btxle, sizeof(btxle));
2709c9f4492SRobert Nordier 	    copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx),
2719c9f4492SRobert Nordier 		 sizeof(btx));
2729c9f4492SRobert Nordier 	    break;
2739c9f4492SRobert Nordier 	case I_CLNT:
2749c9f4492SRobert Nordier 	    copy(fdi[i], fdo, ihdr.size, 0);
2759c9f4492SRobert Nordier 	    if (ftruncate(fdo, ohdr.size += ohdr.data))
2769c9f4492SRobert Nordier 		err(2, "%s", tname);
2779c9f4492SRobert Nordier 	}
2789c9f4492SRobert Nordier 	if (close(fdi[i]))
2799c9f4492SRobert Nordier 	    err(2, "%s", fname);
2809c9f4492SRobert Nordier     }
2819c9f4492SRobert Nordier     if (close(fdo))
2829c9f4492SRobert Nordier 	err(2, "%s", tname);
2839c9f4492SRobert Nordier     if (rename(tname, oname))
2849c9f4492SRobert Nordier 	err(2, "%s: Can't rename to %s", tname, oname);
2851987e300SWarner Losh     free((void*)(intptr_t)tname);
2869c9f4492SRobert Nordier     tname = NULL;
2879c9f4492SRobert Nordier     if (verbose) {
2889c9f4492SRobert Nordier 	printf(binfo, btx.btx_majver, btx.btx_minver, btx.btx_textsz,
2899c9f4492SRobert Nordier 	       BTX_ORIGIN(btx), BTX_ENTRY(btx), BTX_MAPPED(btx) *
2909c9f4492SRobert Nordier 	       BTX_PGSIZE / 0x100000, !!(btx.btx_flags & BTX_MAPONE),
2919c9f4492SRobert Nordier 	       BTX_MAPPED(btx) - btx.btx_pgctl - BTX_PGBASE /
2929c9f4492SRobert Nordier 	       BTX_PGSIZE - BTX_MAPPED(btx) * 4 / BTX_PGSIZE);
2939c9f4492SRobert Nordier 	printf(cinfo, fmtlist[ihdr.fmt], ihdr.size, ihdr.text,
2949c9f4492SRobert Nordier 	       ihdr.data, ihdr.bss, ihdr.entry);
2959c9f4492SRobert Nordier 	printf(oinfo, fmtlist[ohdr.fmt], ohdr.size, ohdr.text,
2969c9f4492SRobert Nordier 	       ohdr.data, ohdr.org, ohdr.entry);
2979c9f4492SRobert Nordier     }
2989c9f4492SRobert Nordier }
2999c9f4492SRobert Nordier 
3009c9f4492SRobert Nordier /*
3019c9f4492SRobert Nordier  * Read BTX file header.
3029c9f4492SRobert Nordier  */
3039c9f4492SRobert Nordier static void
getbtx(int fd,struct btx_hdr * btx)3049c9f4492SRobert Nordier getbtx(int fd, struct btx_hdr * btx)
3059c9f4492SRobert Nordier {
3069c9f4492SRobert Nordier     if (readx(fd, btx, sizeof(*btx), 0) != sizeof(*btx) ||
3079c9f4492SRobert Nordier 	btx->btx_magic[0] != BTX_MAG0 ||
3089c9f4492SRobert Nordier 	btx->btx_magic[1] != BTX_MAG1 ||
3099c9f4492SRobert Nordier 	btx->btx_magic[2] != BTX_MAG2)
3109c9f4492SRobert Nordier 	errx(1, "%s: Not a BTX kernel", fname);
31149a39408SDavid E. O'Brien     btx->btx_pgctl = le16toh(btx->btx_pgctl);
31249a39408SDavid E. O'Brien     btx->btx_textsz = le16toh(btx->btx_textsz);
31349a39408SDavid E. O'Brien     btx->btx_entry = le32toh(btx->btx_entry);
3149c9f4492SRobert Nordier }
3159c9f4492SRobert Nordier 
3169c9f4492SRobert Nordier /*
3179c9f4492SRobert Nordier  * Get file size and read a.out or ELF header.
3189c9f4492SRobert Nordier  */
3199c9f4492SRobert Nordier static void
gethdr(int fd,struct hdr * hdr)3209c9f4492SRobert Nordier gethdr(int fd, struct hdr *hdr)
3219c9f4492SRobert Nordier {
3229c9f4492SRobert Nordier     struct stat sb;
32349a39408SDavid E. O'Brien     const struct exec *ex;
3249c9f4492SRobert Nordier     const Elf32_Ehdr *ee;
3259c9f4492SRobert Nordier     const Elf32_Phdr *ep;
3269c9f4492SRobert Nordier     void *p;
327b0ca5f03SMarcel Moolenaar     unsigned int fmt, x, n, i;
3289c9f4492SRobert Nordier 
3299c9f4492SRobert Nordier     memset(hdr, 0, sizeof(*hdr));
3309c9f4492SRobert Nordier     if (fstat(fd, &sb))
3319c9f4492SRobert Nordier 	err(2, "%s", fname);
3329c9f4492SRobert Nordier     if (sb.st_size > MAXU32)
3339c9f4492SRobert Nordier 	errx(1, "%s: Too big", fname);
3349c9f4492SRobert Nordier     hdr->size = sb.st_size;
3358948542cSDoug Rabson     if (!hdr->size)
3368948542cSDoug Rabson 	return;
3379c9f4492SRobert Nordier     if ((p = mmap(NULL, hdr->size, PROT_READ, MAP_SHARED, fd,
3389c9f4492SRobert Nordier 		  0)) == MAP_FAILED)
3399c9f4492SRobert Nordier 	err(2, "%s", fname);
3409c9f4492SRobert Nordier     for (fmt = F_CNT - 1; !hdr->fmt && fmt; fmt--)
3419c9f4492SRobert Nordier 	switch (fmt) {
3429c9f4492SRobert Nordier 	case F_AOUT:
3439c9f4492SRobert Nordier 	    ex = p;
34449a39408SDavid E. O'Brien 	    if (hdr->size >= sizeof(struct exec) && !N_BADMAG(*ex)) {
3459c9f4492SRobert Nordier 		hdr->fmt = fmt;
34649a39408SDavid E. O'Brien 		x = N_GETMAGIC(*ex);
3479c9f4492SRobert Nordier 		if (x == OMAGIC || x == NMAGIC) {
3489c9f4492SRobert Nordier 		    if (x == NMAGIC)
3499c9f4492SRobert Nordier 			Warn(fname, "Treating %s NMAGIC as OMAGIC",
3509c9f4492SRobert Nordier 			     fmtlist[fmt]);
3519c9f4492SRobert Nordier 		    hdr->flags |= IMPURE;
3529c9f4492SRobert Nordier 		}
35349a39408SDavid E. O'Brien 		hdr->text = le32toh(ex->a_text);
35449a39408SDavid E. O'Brien 		hdr->data = le32toh(ex->a_data);
35549a39408SDavid E. O'Brien 		hdr->bss = le32toh(ex->a_bss);
35649a39408SDavid E. O'Brien 		hdr->entry = le32toh(ex->a_entry);
35749a39408SDavid E. O'Brien 		if (le32toh(ex->a_entry) >= BTX_PGSIZE)
3589c9f4492SRobert Nordier 		    hdr->org = BTX_PGSIZE;
3599c9f4492SRobert Nordier 	    }
3609c9f4492SRobert Nordier 	    break;
3619c9f4492SRobert Nordier 	case F_ELF:
3629c9f4492SRobert Nordier 	    ee = p;
3639c9f4492SRobert Nordier 	    if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) {
3649c9f4492SRobert Nordier 		hdr->fmt = fmt;
36549a39408SDavid E. O'Brien 		for (n = i = 0; i < le16toh(ee->e_phnum); i++) {
36649a39408SDavid E. O'Brien 		    ep = (void *)((uint8_t *)p + le32toh(ee->e_phoff) +
36749a39408SDavid E. O'Brien 				  le16toh(ee->e_phentsize) * i);
36849a39408SDavid E. O'Brien 		    if (le32toh(ep->p_type) == PT_LOAD)
3699c9f4492SRobert Nordier 			switch (n++) {
3709c9f4492SRobert Nordier 			case 0:
37149a39408SDavid E. O'Brien 			    hdr->text = le32toh(ep->p_filesz);
37249a39408SDavid E. O'Brien 			    hdr->org = le32toh(ep->p_paddr);
37349a39408SDavid E. O'Brien 			    if (le32toh(ep->p_flags) & PF_W)
3749c9f4492SRobert Nordier 				hdr->flags |= IMPURE;
3759c9f4492SRobert Nordier 			    break;
3769c9f4492SRobert Nordier 			case 1:
37749a39408SDavid E. O'Brien 			    hdr->data = le32toh(ep->p_filesz);
37849a39408SDavid E. O'Brien 			    hdr->bss = le32toh(ep->p_memsz) -
37949a39408SDavid E. O'Brien 				le32toh(ep->p_filesz);
3809c9f4492SRobert Nordier 			    break;
3819c9f4492SRobert Nordier 			case 2:
3829c9f4492SRobert Nordier 			    Warn(fname,
3839c9f4492SRobert Nordier 				 "Ignoring extra %s PT_LOAD segments",
3849c9f4492SRobert Nordier 				 fmtlist[fmt]);
3859c9f4492SRobert Nordier 			}
3869c9f4492SRobert Nordier 		}
38749a39408SDavid E. O'Brien 		hdr->entry = le32toh(ee->e_entry);
3889c9f4492SRobert Nordier 	    }
3899c9f4492SRobert Nordier 	}
3909c9f4492SRobert Nordier     if (munmap(p, hdr->size))
3919c9f4492SRobert Nordier 	err(2, "%s", fname);
3929c9f4492SRobert Nordier }
3939c9f4492SRobert Nordier 
3949c9f4492SRobert Nordier /*
3959c9f4492SRobert Nordier  * Write a.out or ELF header.
3969c9f4492SRobert Nordier  */
3979c9f4492SRobert Nordier static void
puthdr(int fd,struct hdr * hdr)3989c9f4492SRobert Nordier puthdr(int fd, struct hdr *hdr)
3999c9f4492SRobert Nordier {
40049a39408SDavid E. O'Brien     struct exec ex;
4019c9f4492SRobert Nordier     struct elfh eh;
4029c9f4492SRobert Nordier 
4039c9f4492SRobert Nordier     switch (hdr->fmt) {
4049c9f4492SRobert Nordier     case F_AOUT:
4059c9f4492SRobert Nordier 	memset(&ex, 0, sizeof(ex));
4069f850411SWarner Losh 	N_SETMAGIC(ex, ZMAGIC, MID_I386, 0);
40749a39408SDavid E. O'Brien 	hdr->text = N_ALIGN(ex, hdr->text);
40849a39408SDavid E. O'Brien 	ex.a_text = htole32(hdr->text);
40949a39408SDavid E. O'Brien 	hdr->data = N_ALIGN(ex, hdr->data);
41049a39408SDavid E. O'Brien 	ex.a_data = htole32(hdr->data);
41149a39408SDavid E. O'Brien 	ex.a_entry = htole32(hdr->entry);
4129c9f4492SRobert Nordier 	writex(fd, &ex, sizeof(ex));
41349a39408SDavid E. O'Brien 	hdr->size = N_ALIGN(ex, sizeof(ex));
4149c9f4492SRobert Nordier 	seekx(fd, hdr->size);
4159c9f4492SRobert Nordier 	break;
4169c9f4492SRobert Nordier     case F_ELF:
4179c9f4492SRobert Nordier 	eh = elfhdr;
41849a39408SDavid E. O'Brien 	eh.e.e_entry = htole32(hdr->entry);
41949a39408SDavid E. O'Brien 	eh.p[0].p_vaddr = eh.p[0].p_paddr = htole32(hdr->org);
42049a39408SDavid E. O'Brien 	eh.p[0].p_filesz = eh.p[0].p_memsz = htole32(hdr->text);
42149a39408SDavid E. O'Brien 	eh.p[1].p_offset = htole32(le32toh(eh.p[0].p_offset) +
42249a39408SDavid E. O'Brien 	    le32toh(eh.p[0].p_filesz));
4237148ee89SRuslan Ermilov 	eh.p[1].p_vaddr = eh.p[1].p_paddr =
424c4c510beSPedro F. Giffuni 	    htole32(roundup2(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
425419749b6SChristian Brueffer 	    4096));
42649a39408SDavid E. O'Brien 	eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
4279c9f4492SRobert Nordier 	eh.sh[2].sh_addr = eh.p[0].p_vaddr;
4289c9f4492SRobert Nordier 	eh.sh[2].sh_offset = eh.p[0].p_offset;
4299c9f4492SRobert Nordier 	eh.sh[2].sh_size = eh.p[0].p_filesz;
4309c9f4492SRobert Nordier 	eh.sh[3].sh_addr = eh.p[1].p_vaddr;
4319c9f4492SRobert Nordier 	eh.sh[3].sh_offset = eh.p[1].p_offset;
4329c9f4492SRobert Nordier 	eh.sh[3].sh_size = eh.p[1].p_filesz;
4339c9f4492SRobert Nordier 	writex(fd, &eh, sizeof(eh));
4349c9f4492SRobert Nordier 	hdr->size = sizeof(eh);
4359c9f4492SRobert Nordier     }
4369c9f4492SRobert Nordier }
4379c9f4492SRobert Nordier 
4389c9f4492SRobert Nordier /*
4399c9f4492SRobert Nordier  * Safe copy from input file to output file.
4409c9f4492SRobert Nordier  */
4419c9f4492SRobert Nordier static void
copy(int fdi,int fdo,size_t nbyte,off_t offset)4429c9f4492SRobert Nordier copy(int fdi, int fdo, size_t nbyte, off_t offset)
4439c9f4492SRobert Nordier {
4449c9f4492SRobert Nordier     char buf[8192];
4459c9f4492SRobert Nordier     size_t n;
4469c9f4492SRobert Nordier 
4479c9f4492SRobert Nordier     while (nbyte) {
4489c9f4492SRobert Nordier 	if ((n = sizeof(buf)) > nbyte)
4499c9f4492SRobert Nordier 	    n = nbyte;
4509c9f4492SRobert Nordier 	if (readx(fdi, buf, n, offset) != n)
4519c9f4492SRobert Nordier 	    errx(2, "%s: Short read", fname);
4529c9f4492SRobert Nordier 	writex(fdo, buf, n);
4539c9f4492SRobert Nordier 	nbyte -= n;
4549c9f4492SRobert Nordier 	offset = -1;
4559c9f4492SRobert Nordier     }
4569c9f4492SRobert Nordier }
4579c9f4492SRobert Nordier 
4589c9f4492SRobert Nordier /*
4599c9f4492SRobert Nordier  * Safe read from input file.
4609c9f4492SRobert Nordier  */
4619c9f4492SRobert Nordier static size_t
readx(int fd,void * buf,size_t nbyte,off_t offset)4629c9f4492SRobert Nordier readx(int fd, void *buf, size_t nbyte, off_t offset)
4639c9f4492SRobert Nordier {
4649c9f4492SRobert Nordier     ssize_t n;
4659c9f4492SRobert Nordier 
4669c9f4492SRobert Nordier     if (offset != -1 && lseek(fd, offset, SEEK_SET) != offset)
4679c9f4492SRobert Nordier 	err(2, "%s", fname);
4689c9f4492SRobert Nordier     if ((n = read(fd, buf, nbyte)) == -1)
4699c9f4492SRobert Nordier 	err(2, "%s", fname);
4709c9f4492SRobert Nordier     return n;
4719c9f4492SRobert Nordier }
4729c9f4492SRobert Nordier 
4739c9f4492SRobert Nordier /*
4749c9f4492SRobert Nordier  * Safe write to output file.
4759c9f4492SRobert Nordier  */
4769c9f4492SRobert Nordier static void
writex(int fd,const void * buf,size_t nbyte)4779c9f4492SRobert Nordier writex(int fd, const void *buf, size_t nbyte)
4789c9f4492SRobert Nordier {
4799c9f4492SRobert Nordier     ssize_t n;
4809c9f4492SRobert Nordier 
4819c9f4492SRobert Nordier     if ((n = write(fd, buf, nbyte)) == -1)
4829c9f4492SRobert Nordier 	err(2, "%s", tname);
48350385af9SXin LI     if ((size_t)n != nbyte)
4849c9f4492SRobert Nordier 	errx(2, "%s: Short write", tname);
4859c9f4492SRobert Nordier }
4869c9f4492SRobert Nordier 
4879c9f4492SRobert Nordier /*
4889c9f4492SRobert Nordier  * Safe seek in output file.
4899c9f4492SRobert Nordier  */
4909c9f4492SRobert Nordier static void
seekx(int fd,off_t offset)4919c9f4492SRobert Nordier seekx(int fd, off_t offset)
4929c9f4492SRobert Nordier {
4939c9f4492SRobert Nordier     if (lseek(fd, offset, SEEK_SET) != offset)
4949c9f4492SRobert Nordier 	err(2, "%s", tname);
4959c9f4492SRobert Nordier }
4969c9f4492SRobert Nordier 
4979c9f4492SRobert Nordier /*
4989c9f4492SRobert Nordier  * Convert an option argument to a format code.
4999c9f4492SRobert Nordier  */
500b0ca5f03SMarcel Moolenaar static unsigned int
optfmt(const char * arg)5019c9f4492SRobert Nordier optfmt(const char *arg)
5029c9f4492SRobert Nordier {
503b0ca5f03SMarcel Moolenaar     unsigned int i;
5049c9f4492SRobert Nordier 
5059c9f4492SRobert Nordier     for (i = 0; i < F_CNT && strcmp(arg, fmtlist[i]); i++);
5069c9f4492SRobert Nordier     if (i == F_CNT)
5079c9f4492SRobert Nordier 	errx(1, "%s: Unknown format", arg);
5089c9f4492SRobert Nordier     return i;
5099c9f4492SRobert Nordier }
5109c9f4492SRobert Nordier 
5119c9f4492SRobert Nordier /*
5129c9f4492SRobert Nordier  * Convert an option argument to an address.
5139c9f4492SRobert Nordier  */
5149c9f4492SRobert Nordier static uint32_t
optaddr(const char * arg)5159c9f4492SRobert Nordier optaddr(const char *arg)
5169c9f4492SRobert Nordier {
5179c9f4492SRobert Nordier     char *s;
5189c9f4492SRobert Nordier     unsigned long x;
5199c9f4492SRobert Nordier 
5209c9f4492SRobert Nordier     errno = 0;
5219c9f4492SRobert Nordier     x = strtoul(arg, &s, 0);
5229c9f4492SRobert Nordier     if (errno || !*arg || *s || x > MAXU32)
5239c9f4492SRobert Nordier 	errx(1, "%s: Illegal address", arg);
5249c9f4492SRobert Nordier     return x;
5259c9f4492SRobert Nordier }
5269c9f4492SRobert Nordier 
5279c9f4492SRobert Nordier /*
5289c9f4492SRobert Nordier  * Convert an option argument to a page number.
5299c9f4492SRobert Nordier  */
5309c9f4492SRobert Nordier static int
optpage(const char * arg,int hi)5319c9f4492SRobert Nordier optpage(const char *arg, int hi)
5329c9f4492SRobert Nordier {
5339c9f4492SRobert Nordier     char *s;
5349c9f4492SRobert Nordier     long x;
5359c9f4492SRobert Nordier 
5369c9f4492SRobert Nordier     errno = 0;
5379c9f4492SRobert Nordier     x = strtol(arg, &s, 0);
5389c9f4492SRobert Nordier     if (errno || !*arg || *s || x < 0 || x > hi)
5399c9f4492SRobert Nordier 	errx(1, "%s: Illegal page number", arg);
5409c9f4492SRobert Nordier     return x;
5419c9f4492SRobert Nordier }
5429c9f4492SRobert Nordier 
5439c9f4492SRobert Nordier /*
5449c9f4492SRobert Nordier  * Display a warning.
5459c9f4492SRobert Nordier  */
5469c9f4492SRobert Nordier static void
Warn(const char * locus,const char * fmt,...)5479c9f4492SRobert Nordier Warn(const char *locus, const char *fmt, ...)
5489c9f4492SRobert Nordier {
5499c9f4492SRobert Nordier     va_list ap;
5509c9f4492SRobert Nordier     char *s;
5519c9f4492SRobert Nordier 
5529c9f4492SRobert Nordier     if (!quiet) {
5539c9f4492SRobert Nordier 	asprintf(&s, "%s: Warning: %s", locus, fmt);
5549c9f4492SRobert Nordier 	va_start(ap, fmt);
5559c9f4492SRobert Nordier 	vwarnx(s, ap);
5569c9f4492SRobert Nordier 	va_end(ap);
5579c9f4492SRobert Nordier 	free(s);
5589c9f4492SRobert Nordier     }
5599c9f4492SRobert Nordier }
5609c9f4492SRobert Nordier 
5619c9f4492SRobert Nordier /*
5629c9f4492SRobert Nordier  * Display usage information.
5639c9f4492SRobert Nordier  */
5649c9f4492SRobert Nordier static void
usage(void)5659c9f4492SRobert Nordier usage(void)
5669c9f4492SRobert Nordier {
5679c9f4492SRobert Nordier     fprintf(stderr, "%s\n%s\n",
5689c9f4492SRobert Nordier     "usage: btxld [-qv] [-b file] [-E address] [-e address] [-f format]",
5699c9f4492SRobert Nordier     "             [-l file] [-o filename] [-P page] [-W page] file");
5709c9f4492SRobert Nordier     exit(1);
5719c9f4492SRobert Nordier }
572