2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1998 Robert Nordier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/param.h>
30 #include <sys/endian.h>
34 /* XXX make this work as an i386/amd64 cross-tool */
35 #include <machine/exec.h>
39 #include <netinet/in.h>
54 #define BTX_PATH "/sys/boot/i386/btx"
56 #define I_LDR 0 /* BTX loader */
57 #define I_BTX 1 /* BTX kernel */
58 #define I_CLNT 2 /* Client program */
60 #define F_BIN 0 /* Binary */
61 #define F_AOUT 1 /* ZMAGIC a.out */
62 #define F_ELF 2 /* 32-bit ELF */
63 #define F_CNT 3 /* Number of formats */
65 #define IMPURE 1 /* Writable text */
66 #define MAXU32 0xffffffff /* Maximum unsigned 32-bit quantity */
69 uint32_t fmt; /* Format */
70 uint32_t flags; /* Bit flags */
71 uint32_t size; /* Size of file */
72 uint32_t text; /* Size of text segment */
73 uint32_t data; /* Size of data segment */
74 uint32_t bss; /* Size of bss segment */
75 uint32_t org; /* Program origin */
76 uint32_t entry; /* Program entry point */
79 static const char *const fmtlist[] = {"bin", "aout", "elf"};
81 static const char binfo[] =
82 "kernel: ver=%u.%02u size=%x load=%x entry=%x map=%uM "
84 static const char cinfo[] =
85 "client: fmt=%s size=%x text=%x data=%x bss=%x entry=%x\n";
86 static const char oinfo[] =
87 "output: fmt=%s size=%x text=%x data=%x org=%x entry=%x\n";
89 static const char *lname =
90 BTX_PATH "/btxldr/btxldr"; /* BTX loader */
91 static const char *bname =
92 BTX_PATH "/btx/btx"; /* BTX kernel */
93 static const char *oname =
94 "a.out"; /* Output filename */
96 static int ppage = -1; /* First page present */
97 static int wpage = -1; /* First page writable */
99 static unsigned int format; /* Output format */
101 static uint32_t centry; /* Client entry address */
102 static uint32_t lentry; /* Loader entry address */
104 static int Eflag; /* Client entry option */
106 static int quiet; /* Inhibit warnings */
107 static int verbose; /* Display information */
109 static const char *tname; /* Temporary output file */
110 static const char *fname; /* Current input file */
112 static void cleanup(void);
113 static void btxld(const char *);
114 static void getbtx(int, struct btx_hdr *);
115 static void gethdr(int, struct hdr *);
116 static void puthdr(int, struct hdr *);
117 static void copy(int, int, size_t, off_t);
118 static size_t readx(int, void *, size_t, off_t);
119 static void writex(int, const void *, size_t);
120 static void seekx(int, off_t);
121 static unsigned int optfmt(const char *);
122 static uint32_t optaddr(const char *);
123 static int optpage(const char *, int);
124 static void Warn(const char *, const char *, ...);
125 static void usage(void) __dead2;
128 * A link editor for BTX clients.
131 main(int argc, char *argv[])
135 while ((c = getopt(argc, argv, "qvb:E:e:f:l:o:P:W:")) != -1)
147 centry = optaddr(optarg);
151 lentry = optaddr(optarg);
154 format = optfmt(optarg);
163 ppage = optpage(optarg, 1);
166 wpage = optpage(optarg, BTX_MAXCWR);
181 * Clean up after errors.
191 * Read the input files; write the output file; display information.
194 btxld(const char *iname)
196 char name[FILENAME_MAX];
197 struct btx_hdr btx, btxle;
198 struct hdr ihdr, ohdr;
199 unsigned int ldr_size, cwr;
204 for (i = I_LDR; i <= I_CLNT; i++) {
205 fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
206 if ((fdi[i] = open(fname, O_RDONLY)) == -1)
210 gethdr(fdi[i], &ihdr);
211 if (ihdr.fmt != F_BIN)
212 Warn(fname, "Loader format is %s; processing as %s",
213 fmtlist[ihdr.fmt], fmtlist[F_BIN]);
214 ldr_size = ihdr.size;
217 getbtx(fdi[i], &btx);
220 gethdr(fdi[i], &ihdr);
221 if (ihdr.org && ihdr.org != BTX_PGSIZE)
223 "Client origin is 0x%x; expecting 0 or 0x%x",
224 ihdr.org, BTX_PGSIZE);
227 memset(&ohdr, 0, sizeof(ohdr));
229 ohdr.text = ldr_size;
230 ohdr.data = btx.btx_textsz + ihdr.size;
234 if (wpage > 0 || (wpage == -1 && !(ihdr.flags & IMPURE))) {
238 cwr = howmany(ihdr.text, BTX_PGSIZE);
239 if (cwr > BTX_MAXCWR)
243 if (ppage > 0 || (ppage && wpage && ihdr.org >= BTX_PGSIZE)) {
244 btx.btx_flags |= BTX_MAPONE;
248 btx.btx_pgctl -= cwr;
249 btx.btx_entry = Eflag ? centry : ihdr.entry;
250 if ((size_t)snprintf(name, sizeof(name), "%s.tmp", oname) >= sizeof(name))
251 errx(2, "%s: Filename too long", oname);
252 if ((fdo = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666)) == -1)
254 if (!(tname = strdup(name)))
257 for (i = I_LDR; i <= I_CLNT; i++) {
258 fname = i == I_LDR ? lname : i == I_BTX ? bname : iname;
261 copy(fdi[i], fdo, ldr_size, 0);
262 seekx(fdo, ohdr.size += ohdr.text);
266 btxle.btx_pgctl = htole16(btxle.btx_pgctl);
267 btxle.btx_textsz = htole16(btxle.btx_textsz);
268 btxle.btx_entry = htole32(btxle.btx_entry);
269 writex(fdo, &btxle, sizeof(btxle));
270 copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx),
274 copy(fdi[i], fdo, ihdr.size, 0);
275 if (ftruncate(fdo, ohdr.size += ohdr.data))
283 if (rename(tname, oname))
284 err(2, "%s: Can't rename to %s", tname, oname);
285 free((void*)(intptr_t)tname);
288 printf(binfo, btx.btx_majver, btx.btx_minver, btx.btx_textsz,
289 BTX_ORIGIN(btx), BTX_ENTRY(btx), BTX_MAPPED(btx) *
290 BTX_PGSIZE / 0x100000, !!(btx.btx_flags & BTX_MAPONE),
291 BTX_MAPPED(btx) - btx.btx_pgctl - BTX_PGBASE /
292 BTX_PGSIZE - BTX_MAPPED(btx) * 4 / BTX_PGSIZE);
293 printf(cinfo, fmtlist[ihdr.fmt], ihdr.size, ihdr.text,
294 ihdr.data, ihdr.bss, ihdr.entry);
295 printf(oinfo, fmtlist[ohdr.fmt], ohdr.size, ohdr.text,
296 ohdr.data, ohdr.org, ohdr.entry);
301 * Read BTX file header.
304 getbtx(int fd, struct btx_hdr * btx)
306 if (readx(fd, btx, sizeof(*btx), 0) != sizeof(*btx) ||
307 btx->btx_magic[0] != BTX_MAG0 ||
308 btx->btx_magic[1] != BTX_MAG1 ||
309 btx->btx_magic[2] != BTX_MAG2)
310 errx(1, "%s: Not a BTX kernel", fname);
311 btx->btx_pgctl = le16toh(btx->btx_pgctl);
312 btx->btx_textsz = le16toh(btx->btx_textsz);
313 btx->btx_entry = le32toh(btx->btx_entry);
317 * Get file size and read a.out or ELF header.
320 gethdr(int fd, struct hdr *hdr)
323 const struct exec *ex;
324 const Elf32_Ehdr *ee;
325 const Elf32_Phdr *ep;
327 unsigned int fmt, x, n, i;
329 memset(hdr, 0, sizeof(*hdr));
332 if (sb.st_size > MAXU32)
333 errx(1, "%s: Too big", fname);
334 hdr->size = sb.st_size;
337 if ((p = mmap(NULL, hdr->size, PROT_READ, MAP_SHARED, fd,
340 for (fmt = F_CNT - 1; !hdr->fmt && fmt; fmt--)
344 if (hdr->size >= sizeof(struct exec) && !N_BADMAG(*ex)) {
347 if (x == OMAGIC || x == NMAGIC) {
349 Warn(fname, "Treating %s NMAGIC as OMAGIC",
351 hdr->flags |= IMPURE;
353 hdr->text = le32toh(ex->a_text);
354 hdr->data = le32toh(ex->a_data);
355 hdr->bss = le32toh(ex->a_bss);
356 hdr->entry = le32toh(ex->a_entry);
357 if (le32toh(ex->a_entry) >= BTX_PGSIZE)
358 hdr->org = BTX_PGSIZE;
363 if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) {
365 for (n = i = 0; i < le16toh(ee->e_phnum); i++) {
366 ep = (void *)((uint8_t *)p + le32toh(ee->e_phoff) +
367 le16toh(ee->e_phentsize) * i);
368 if (le32toh(ep->p_type) == PT_LOAD)
371 hdr->text = le32toh(ep->p_filesz);
372 hdr->org = le32toh(ep->p_paddr);
373 if (le32toh(ep->p_flags) & PF_W)
374 hdr->flags |= IMPURE;
377 hdr->data = le32toh(ep->p_filesz);
378 hdr->bss = le32toh(ep->p_memsz) -
379 le32toh(ep->p_filesz);
383 "Ignoring extra %s PT_LOAD segments",
387 hdr->entry = le32toh(ee->e_entry);
390 if (munmap(p, hdr->size))
395 * Write a.out or ELF header.
398 puthdr(int fd, struct hdr *hdr)
405 memset(&ex, 0, sizeof(ex));
406 N_SETMAGIC(ex, ZMAGIC, MID_I386, 0);
407 hdr->text = N_ALIGN(ex, hdr->text);
408 ex.a_text = htole32(hdr->text);
409 hdr->data = N_ALIGN(ex, hdr->data);
410 ex.a_data = htole32(hdr->data);
411 ex.a_entry = htole32(hdr->entry);
412 writex(fd, &ex, sizeof(ex));
413 hdr->size = N_ALIGN(ex, sizeof(ex));
414 seekx(fd, hdr->size);
418 eh.e.e_entry = htole32(hdr->entry);
419 eh.p[0].p_vaddr = eh.p[0].p_paddr = htole32(hdr->org);
420 eh.p[0].p_filesz = eh.p[0].p_memsz = htole32(hdr->text);
421 eh.p[1].p_offset = htole32(le32toh(eh.p[0].p_offset) +
422 le32toh(eh.p[0].p_filesz));
423 eh.p[1].p_vaddr = eh.p[1].p_paddr =
424 htole32(roundup2(le32toh(eh.p[0].p_paddr) + le32toh(eh.p[0].p_memsz),
426 eh.p[1].p_filesz = eh.p[1].p_memsz = htole32(hdr->data);
427 eh.sh[2].sh_addr = eh.p[0].p_vaddr;
428 eh.sh[2].sh_offset = eh.p[0].p_offset;
429 eh.sh[2].sh_size = eh.p[0].p_filesz;
430 eh.sh[3].sh_addr = eh.p[1].p_vaddr;
431 eh.sh[3].sh_offset = eh.p[1].p_offset;
432 eh.sh[3].sh_size = eh.p[1].p_filesz;
433 writex(fd, &eh, sizeof(eh));
434 hdr->size = sizeof(eh);
439 * Safe copy from input file to output file.
442 copy(int fdi, int fdo, size_t nbyte, off_t offset)
448 if ((n = sizeof(buf)) > nbyte)
450 if (readx(fdi, buf, n, offset) != n)
451 errx(2, "%s: Short read", fname);
459 * Safe read from input file.
462 readx(int fd, void *buf, size_t nbyte, off_t offset)
466 if (offset != -1 && lseek(fd, offset, SEEK_SET) != offset)
468 if ((n = read(fd, buf, nbyte)) == -1)
474 * Safe write to output file.
477 writex(int fd, const void *buf, size_t nbyte)
481 if ((n = write(fd, buf, nbyte)) == -1)
483 if ((size_t)n != nbyte)
484 errx(2, "%s: Short write", tname);
488 * Safe seek in output file.
491 seekx(int fd, off_t offset)
493 if (lseek(fd, offset, SEEK_SET) != offset)
498 * Convert an option argument to a format code.
501 optfmt(const char *arg)
505 for (i = 0; i < F_CNT && strcmp(arg, fmtlist[i]); i++);
507 errx(1, "%s: Unknown format", arg);
512 * Convert an option argument to an address.
515 optaddr(const char *arg)
521 x = strtoul(arg, &s, 0);
522 if (errno || !*arg || *s || x > MAXU32)
523 errx(1, "%s: Illegal address", arg);
528 * Convert an option argument to a page number.
531 optpage(const char *arg, int hi)
537 x = strtol(arg, &s, 0);
538 if (errno || !*arg || *s || x < 0 || x > hi)
539 errx(1, "%s: Illegal page number", arg);
547 Warn(const char *locus, const char *fmt, ...)
553 asprintf(&s, "%s: Warning: %s", locus, fmt);
562 * Display usage information.
567 fprintf(stderr, "%s\n%s\n",
568 "usage: btxld [-qv] [-b file] [-E address] [-e address] [-f format]",
569 " [-l file] [-o filename] [-P page] [-W page] file");