xref: /plan9-contrib/sys/src/libmach/executable.c (revision d04cc87c700f6b47515a41857deb8d5e7a98c958)
1 #include	<u.h>
2 #include	<libc.h>
3 #include	<bio.h>
4 #include	<bootexec.h>
5 #include	<mach.h>
6 #include	"elf.h"
7 
8 /*
9  *	All a.out header types.  The dummy entry allows canonical
10  *	processing of the union as a sequence of longs
11  */
12 
13 typedef struct {
14 	union{
15 		struct {
16 			Exec;		/* a.out.h */
17 			uvlong hdr[1];
18 		};
19 		Ehdr;			/* elf.h */
20 		struct mipsexec;	/* bootexec.h */
21 		struct mips4kexec;	/* bootexec.h */
22 		struct sparcexec;	/* bootexec.h */
23 		struct nextexec;	/* bootexec.h */
24 	} e;
25 	long dummy;			/* padding to ensure extra long */
26 } ExecHdr;
27 
28 static	int	nextboot(int, Fhdr*, ExecHdr*);
29 static	int	sparcboot(int, Fhdr*, ExecHdr*);
30 static	int	mipsboot(int, Fhdr*, ExecHdr*);
31 static	int	mips4kboot(int, Fhdr*, ExecHdr*);
32 static	int	common(int, Fhdr*, ExecHdr*);
33 static	int	commonllp64(int, Fhdr*, ExecHdr*);
34 static	int	adotout(int, Fhdr*, ExecHdr*);
35 static	int	elfdotout(int, Fhdr*, ExecHdr*);
36 static	int	armdotout(int, Fhdr*, ExecHdr*);
37 static	void	setsym(Fhdr*, long, long, long, vlong);
38 static	void	setdata(Fhdr*, uvlong, long, vlong, long);
39 static	void	settext(Fhdr*, uvlong, uvlong, long, vlong);
40 static	void	hswal(void*, int, ulong(*)(ulong));
41 static	uvlong	_round(uvlong, ulong);
42 
43 /*
44  *	definition of per-executable file type structures
45  */
46 
47 typedef struct Exectable{
48 	long	magic;			/* big-endian magic number of file */
49 	char	*name;			/* executable identifier */
50 	char	*dlmname;		/* dynamically loadable module identifier */
51 	uchar	type;			/* Internal code */
52 	uchar	_magic;			/* _MAGIC() magic */
53 	Mach	*mach;			/* Per-machine data */
54 	long	hsize;			/* header size */
55 	ulong	(*swal)(ulong);		/* beswal or leswal */
56 	int	(*hparse)(int, Fhdr*, ExecHdr*);
57 } ExecTable;
58 
59 extern	Mach	mmips;
60 extern	Mach	mmips2le;
61 extern	Mach	mmips2be;
62 extern	Mach	msparc;
63 extern	Mach	msparc64;
64 extern	Mach	m68020;
65 extern	Mach	mi386;
66 extern	Mach	mamd64;
67 extern	Mach	marm;
68 extern	Mach	mpower;
69 extern	Mach	malpha;
70 
71 ExecTable exectab[] =
72 {
73 	{ V_MAGIC,			/* Mips v.out */
74 		"mips plan 9 executable BE",
75 		"mips plan 9 dlm BE",
76 		FMIPS,
77 		1,
78 		&mmips,
79 		sizeof(Exec),
80 		beswal,
81 		adotout },
82 	{ P_MAGIC,			/* Mips 0.out (r3k le) */
83 		"mips plan 9 executable LE",
84 		"mips plan 9 dlm LE",
85 		FMIPSLE,
86 		1,
87 		&mmips,
88 		sizeof(Exec),
89 		beswal,
90 		adotout },
91 	{ M_MAGIC,			/* Mips 4.out */
92 		"mips 4k plan 9 executable BE",
93 		"mips 4k plan 9 dlm BE",
94 		FMIPS2BE,
95 		1,
96 		&mmips2be,
97 		sizeof(Exec),
98 		beswal,
99 		adotout },
100 	{ N_MAGIC,			/* Mips 0.out */
101 		"mips 4k plan 9 executable LE",
102 		"mips 4k plan 9 dlm LE",
103 		FMIPS2LE,
104 		1,
105 		&mmips2le,
106 		sizeof(Exec),
107 		beswal,
108 		adotout },
109 	{ 0x160<<16,			/* Mips boot image */
110 		"mips plan 9 boot image",
111 		nil,
112 		FMIPSB,
113 		0,
114 		&mmips,
115 		sizeof(struct mipsexec),
116 		beswal,
117 		mipsboot },
118 	{ (0x160<<16)|3,		/* Mips boot image */
119 		"mips 4k plan 9 boot image",
120 		nil,
121 		FMIPSB,
122 		0,
123 		&mmips2be,
124 		sizeof(struct mips4kexec),
125 		beswal,
126 		mips4kboot },
127 	{ K_MAGIC,			/* Sparc k.out */
128 		"sparc plan 9 executable",
129 		"sparc plan 9 dlm",
130 		FSPARC,
131 		1,
132 		&msparc,
133 		sizeof(Exec),
134 		beswal,
135 		adotout },
136 	{ 0x01030107, 			/* Sparc boot image */
137 		"sparc plan 9 boot image",
138 		nil,
139 		FSPARCB,
140 		0,
141 		&msparc,
142 		sizeof(struct sparcexec),
143 		beswal,
144 		sparcboot },
145 	{ U_MAGIC,			/* Sparc64 u.out */
146 		"sparc64 plan 9 executable",
147 		"sparc64 plan 9 dlm",
148 		FSPARC64,
149 		1,
150 		&msparc64,
151 		sizeof(Exec),
152 		beswal,
153 		adotout },
154 	{ A_MAGIC,			/* 68020 2.out & boot image */
155 		"68020 plan 9 executable",
156 		"68020 plan 9 dlm",
157 		F68020,
158 		1,
159 		&m68020,
160 		sizeof(Exec),
161 		beswal,
162 		common },
163 	{ 0xFEEDFACE,			/* Next boot image */
164 		"next plan 9 boot image",
165 		nil,
166 		FNEXTB,
167 		0,
168 		&m68020,
169 		sizeof(struct nextexec),
170 		beswal,
171 		nextboot },
172 	{ I_MAGIC,			/* I386 8.out & boot image */
173 		"386 plan 9 executable",
174 		"386 plan 9 dlm",
175 		FI386,
176 		1,
177 		&mi386,
178 		sizeof(Exec),
179 		beswal,
180 		common },
181 	{ S_MAGIC,			/* amd64 6.out & boot image */
182 		"amd64 plan 9 executable",
183 		"amd64 plan 9 dlm",
184 		FAMD64,
185 		1,
186 		&mamd64,
187 		sizeof(Exec)+8,
188 		nil,
189 		commonllp64 },
190 	{ Q_MAGIC,			/* PowerPC q.out & boot image */
191 		"power plan 9 executable",
192 		"power plan 9 dlm",
193 		FPOWER,
194 		1,
195 		&mpower,
196 		sizeof(Exec),
197 		beswal,
198 		common },
199 	{ ELF_MAG,			/* any elf32 */
200 		"elf executable",
201 		nil,
202 		FNONE,
203 		0,
204 		&mi386,
205 		sizeof(Ehdr),
206 		nil,
207 		elfdotout },
208 	{ E_MAGIC,			/* Arm 5.out */
209 		"arm plan 9 executable",
210 		"arm plan 9 dlm",
211 		FARM,
212 		1,
213 		&marm,
214 		sizeof(Exec),
215 		beswal,
216 		common },
217 	{ (143<<16)|0413,		/* (Free|Net)BSD Arm */
218 		"arm *bsd executable",
219 		nil,
220 		FARM,
221 		0,
222 		&marm,
223 		sizeof(Exec),
224 		leswal,
225 		armdotout },
226 	{ L_MAGIC,			/* alpha 7.out */
227 		"alpha plan 9 executable",
228 		"alpha plan 9 dlm",
229 		FALPHA,
230 		1,
231 		&malpha,
232 		sizeof(Exec),
233 		beswal,
234 		common },
235 	{ 0x0700e0c3,			/* alpha boot image */
236 		"alpha plan 9 boot image",
237 		nil,
238 		FALPHA,
239 		0,
240 		&malpha,
241 		sizeof(Exec),
242 		beswal,
243 		common },
244 	{ 0 },
245 };
246 
247 Mach	*mach = &mi386;			/* Global current machine table */
248 
249 static ExecTable*
250 couldbe4k(ExecTable *mp)
251 {
252 	Dir *d;
253 	ExecTable *f;
254 
255 	if((d=dirstat("/proc/1/regs")) == nil)
256 		return mp;
257 	if(d->length < 32*8){		/* R3000 */
258 		free(d);
259 		return mp;
260 	}
261 	free(d);
262 	for (f = exectab; f->magic; f++)
263 		if(f->magic == M_MAGIC) {
264 			f->name = "mips plan 9 executable on mips2 kernel";
265 			return f;
266 		}
267 	return mp;
268 }
269 
270 int
271 crackhdr(int fd, Fhdr *fp)
272 {
273 	ExecTable *mp;
274 	ExecHdr d;
275 	int nb, ret;
276 	ulong magic;
277 
278 	fp->type = FNONE;
279 	nb = read(fd, (char *)&d.e, sizeof(d.e));
280 	if (nb <= 0)
281 		return 0;
282 
283 	ret = 0;
284 	magic = beswal(d.e.magic);		/* big-endian */
285 	for (mp = exectab; mp->magic; mp++) {
286 		if (nb < mp->hsize)
287 			continue;
288 
289 		/*
290 		 * The magic number has morphed into something
291 		 * with fields (the straw was DYN_MAGIC) so now
292 		 * a flag is needed in Fhdr to distinguish _MAGIC()
293 		 * magic numbers from foreign magic numbers.
294 		 *
295 		 * This code is creaking a bit and if it has to
296 		 * be modified/extended much more it's probably
297 		 * time to step back and redo it all.
298 		 */
299 		if(mp->_magic){
300 			if(mp->magic != (magic & ~DYN_MAGIC))
301 				continue;
302 
303 			if(mp->magic == V_MAGIC)
304 				mp = couldbe4k(mp);
305 
306 			if ((magic & DYN_MAGIC) && mp->dlmname != nil)
307 				fp->name = mp->dlmname;
308 			else
309 				fp->name = mp->name;
310 		}
311 		else{
312 			if(mp->magic != magic)
313 				continue;
314 			fp->name = mp->name;
315 		}
316 		fp->type = mp->type;
317 		fp->hdrsz = mp->hsize;		/* will be zero on bootables */
318 		fp->_magic = mp->_magic;
319 		fp->magic = magic;
320 
321 		mach = mp->mach;
322 		if(mp->swal != nil)
323 			hswal(&d, sizeof(d.e)/sizeof(ulong), mp->swal);
324 		ret = mp->hparse(fd, fp, &d);
325 		seek(fd, mp->hsize, 0);		/* seek to end of header */
326 		break;
327 	}
328 	if(mp->magic == 0)
329 		werrstr("unknown header type");
330 	return ret;
331 }
332 
333 /*
334  * Convert header to canonical form
335  */
336 static void
337 hswal(void *v, int n, ulong (*swap)(ulong))
338 {
339 	ulong *ulp;
340 
341 	for(ulp = v; n--; ulp++)
342 		*ulp = (*swap)(*ulp);
343 }
344 
345 /*
346  *	Crack a normal a.out-type header
347  */
348 static int
349 adotout(int fd, Fhdr *fp, ExecHdr *hp)
350 {
351 	long pgsize;
352 
353 	USED(fd);
354 	pgsize = mach->pgsize;
355 	settext(fp, hp->e.entry, pgsize+sizeof(Exec),
356 			hp->e.text, sizeof(Exec));
357 	setdata(fp, _round(pgsize+fp->txtsz+sizeof(Exec), pgsize),
358 		hp->e.data, fp->txtsz+sizeof(Exec), hp->e.bss);
359 	setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
360 	return 1;
361 }
362 
363 static void
364 commonboot(Fhdr *fp)
365 {
366 	if (!(fp->entry & mach->ktmask))
367 		return;
368 
369 	switch(fp->type) {				/* boot image */
370 	case F68020:
371 		fp->type = F68020B;
372 		fp->name = "68020 plan 9 boot image";
373 		break;
374 	case FI386:
375 		fp->type = FI386B;
376 		fp->txtaddr = (u32int)fp->entry;
377 		fp->name = "386 plan 9 boot image";
378 		fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
379 		break;
380 	case FARM:
381 		fp->txtaddr = mach->kbase+0x8010;
382 		fp->name = "ARM plan 9 boot image";
383 		fp->dataddr = fp->txtaddr+fp->txtsz;
384 		return;
385 	case FALPHA:
386 		fp->type = FALPHAB;
387 		fp->txtaddr = (u32int)fp->entry;
388 		fp->name = "alpha plan 9 boot image";
389 		fp->dataddr = fp->txtaddr+fp->txtsz;
390 		break;
391 	case FPOWER:
392 		fp->type = FPOWERB;
393 		fp->txtaddr = (u32int)fp->entry;
394 		fp->name = "power plan 9 boot image";
395 		fp->dataddr = fp->txtaddr+fp->txtsz;
396 		break;
397 	case FAMD64:
398 		fp->type = FAMD64B;
399 		fp->txtaddr = fp->entry;
400 		fp->name = "amd64 plan 9 boot image";
401 		fp->dataddr = _round(fp->txtaddr+fp->txtsz, mach->pgsize);
402 		break;
403 	default:
404 		return;
405 	}
406 	fp->hdrsz = 0;			/* header stripped */
407 }
408 
409 /*
410  *	_MAGIC() style headers and
411  *	alpha plan9-style bootable images for axp "headerless" boot
412  *
413  */
414 static int
415 common(int fd, Fhdr *fp, ExecHdr *hp)
416 {
417 	adotout(fd, fp, hp);
418 	if(hp->e.magic & DYN_MAGIC) {
419 		fp->txtaddr = 0;
420 		fp->dataddr = fp->txtsz;
421 		return 1;
422 	}
423 	commonboot(fp);
424 	return 1;
425 }
426 
427 static int
428 commonllp64(int, Fhdr *fp, ExecHdr *hp)
429 {
430 	long pgsize;
431 	uvlong entry;
432 
433 	hswal(&hp->e, sizeof(Exec)/sizeof(long), beswal);
434 	if(!(hp->e.magic & HDR_MAGIC))
435 		return 0;
436 
437 	/*
438 	 * There can be more magic here if the
439 	 * header ever needs more expansion.
440 	 * For now just catch use of any of the
441 	 * unused bits.
442 	 */
443 	if((hp->e.magic & ~DYN_MAGIC)>>16)
444 		return 0;
445 	entry = beswav(hp->e.hdr[0]);
446 
447 	pgsize = mach->pgsize;
448 	settext(fp, entry, pgsize+fp->hdrsz, hp->e.text, fp->hdrsz);
449 	setdata(fp, _round(pgsize+fp->txtsz+fp->hdrsz, pgsize),
450 		hp->e.data, fp->txtsz+fp->hdrsz, hp->e.bss);
451 	setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
452 
453 	if(hp->e.magic & DYN_MAGIC) {
454 		fp->txtaddr = 0;
455 		fp->dataddr = fp->txtsz;
456 		return 1;
457 	}
458 	commonboot(fp);
459 	return 1;
460 }
461 
462 /*
463  *	mips bootable image.
464  */
465 static int
466 mipsboot(int fd, Fhdr *fp, ExecHdr *hp)
467 {
468 	USED(fd);
469 	fp->type = FMIPSB;
470 	switch(hp->e.amagic) {
471 	default:
472 	case 0407:	/* some kind of mips */
473 		settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
474 			hp->e.tsize, sizeof(struct mipsexec)+4);
475 		setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
476 			fp->txtoff+hp->e.tsize, hp->e.bsize);
477 		break;
478 	case 0413:	/* some kind of mips */
479 		settext(fp, (u32int)hp->e.mentry, (u32int)hp->e.text_start,
480 			hp->e.tsize, 0);
481 		setdata(fp, (u32int)hp->e.data_start, hp->e.dsize,
482 			hp->e.tsize, hp->e.bsize);
483 		break;
484 	}
485 	setsym(fp, hp->e.nsyms, 0, hp->e.pcsize, hp->e.symptr);
486 	fp->hdrsz = 0;			/* header stripped */
487 	return 1;
488 }
489 
490 /*
491  *	mips4k bootable image.
492  */
493 static int
494 mips4kboot(int fd, Fhdr *fp, ExecHdr *hp)
495 {
496 	USED(fd);
497 	fp->type = FMIPSB;
498 	switch(hp->e.h.amagic) {
499 	default:
500 	case 0407:	/* some kind of mips */
501 		settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
502 			hp->e.h.tsize, sizeof(struct mips4kexec));
503 		setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
504 			fp->txtoff+hp->e.h.tsize, hp->e.h.bsize);
505 		break;
506 	case 0413:	/* some kind of mips */
507 		settext(fp, (u32int)hp->e.h.mentry, (u32int)hp->e.h.text_start,
508 			hp->e.h.tsize, 0);
509 		setdata(fp, (u32int)hp->e.h.data_start, hp->e.h.dsize,
510 			hp->e.h.tsize, hp->e.h.bsize);
511 		break;
512 	}
513 	setsym(fp, hp->e.h.nsyms, 0, hp->e.h.pcsize, hp->e.h.symptr);
514 	fp->hdrsz = 0;			/* header stripped */
515 	return 1;
516 }
517 
518 /*
519  *	sparc bootable image
520  */
521 static int
522 sparcboot(int fd, Fhdr *fp, ExecHdr *hp)
523 {
524 	USED(fd);
525 	fp->type = FSPARCB;
526 	settext(fp, hp->e.sentry, hp->e.sentry, hp->e.stext,
527 		sizeof(struct sparcexec));
528 	setdata(fp, hp->e.sentry+hp->e.stext, hp->e.sdata,
529 		fp->txtoff+hp->e.stext, hp->e.sbss);
530 	setsym(fp, hp->e.ssyms, 0, hp->e.sdrsize, fp->datoff+hp->e.sdata);
531 	fp->hdrsz = 0;			/* header stripped */
532 	return 1;
533 }
534 
535 /*
536  *	next bootable image
537  */
538 static int
539 nextboot(int fd, Fhdr *fp, ExecHdr *hp)
540 {
541 	USED(fd);
542 	fp->type = FNEXTB;
543 	settext(fp, hp->e.textc.vmaddr, hp->e.textc.vmaddr,
544 		hp->e.texts.size, hp->e.texts.offset);
545 	setdata(fp, hp->e.datac.vmaddr, hp->e.datas.size,
546 		hp->e.datas.offset, hp->e.bsss.size);
547 	setsym(fp, hp->e.symc.nsyms, hp->e.symc.spoff, hp->e.symc.pcoff,
548 		hp->e.symc.symoff);
549 	fp->hdrsz = 0;			/* header stripped */
550 	return 1;
551 }
552 
553 /*
554  * Elf32 binaries.
555  */
556 static int
557 elfdotout(int fd, Fhdr *fp, ExecHdr *hp)
558 {
559 
560 	ulong (*swal)(ulong);
561 	ushort (*swab)(ushort);
562 	Ehdr *ep;
563 	Phdr *ph;
564 	int i, it, id, is, phsz;
565 
566 	/* bitswap the header according to the DATA format */
567 	ep = &hp->e;
568 	if(ep->ident[CLASS] != ELFCLASS32) {
569 		werrstr("bad ELF class - not 32 bit");
570 		return 0;
571 	}
572 	if(ep->ident[DATA] == ELFDATA2LSB) {
573 		swab = leswab;
574 		swal = leswal;
575 	} else if(ep->ident[DATA] == ELFDATA2MSB) {
576 		swab = beswab;
577 		swal = beswal;
578 	} else {
579 		werrstr("bad ELF encoding - not big or little endian");
580 		return 0;
581 	}
582 
583 	ep->type = swab(ep->type);
584 	ep->machine = swab(ep->machine);
585 	ep->version = swal(ep->version);
586 	ep->elfentry = swal(ep->elfentry);
587 	ep->phoff = swal(ep->phoff);
588 	ep->shoff = swal(ep->shoff);
589 	ep->flags = swal(ep->flags);
590 	ep->ehsize = swab(ep->ehsize);
591 	ep->phentsize = swab(ep->phentsize);
592 	ep->phnum = swab(ep->phnum);
593 	ep->shentsize = swab(ep->shentsize);
594 	ep->shnum = swab(ep->shnum);
595 	ep->shstrndx = swab(ep->shstrndx);
596 	if(ep->type != EXEC || ep->version != CURRENT)
597 		return 0;
598 
599 	/* we could definitely support a lot more machines here */
600 	fp->magic = ELF_MAG;
601 	fp->hdrsz = (ep->ehsize+ep->phnum*ep->phentsize+16)&~15;
602 	switch(ep->machine) {
603 	case I386:
604 		mach = &mi386;
605 		fp->type = FI386;
606 		break;
607 	case MIPS:
608 		mach = &mmips;
609 		fp->type = FMIPS;
610 		break;
611 	case SPARC64:
612 		mach = &msparc64;
613 		fp->type = FSPARC64;
614 		break;
615 	case POWER:
616 		mach = &mpower;
617 		fp->type = FPOWER;
618 		break;
619 	case AMD64:
620 		mach = &mamd64;
621 		fp->type = FAMD64;
622 		break;
623 	default:
624 		return 0;
625 	}
626 
627 	if(ep->phentsize != sizeof(Phdr)) {
628 		werrstr("bad ELF header size");
629 		return 0;
630 	}
631 	phsz = sizeof(Phdr)*ep->phnum;
632 	ph = malloc(phsz);
633 	if(!ph)
634 		return 0;
635 	seek(fd, ep->phoff, 0);
636 	if(read(fd, ph, phsz) < 0) {
637 		free(ph);
638 		return 0;
639 	}
640 	hswal(ph, phsz/sizeof(ulong), swal);
641 
642 	/* find text, data and symbols and install them */
643 	it = id = is = -1;
644 	for(i = 0; i < ep->phnum; i++) {
645 		if(ph[i].type == LOAD
646 		&& (ph[i].flags & (R|X)) == (R|X) && it == -1)
647 			it = i;
648 		else if(ph[i].type == LOAD
649 		&& (ph[i].flags & (R|W)) == (R|W) && id == -1)
650 			id = i;
651 		else if(ph[i].type == NOPTYPE && is == -1)
652 			is = i;
653 	}
654 	if(it == -1 || id == -1) {
655 		/*
656 		 * The SPARC64 boot image is something of an ELF hack.
657 		 * Text+Data+BSS are represented by ph[0].  Symbols
658 		 * are represented by ph[1]:
659 		 *
660 		 *		filesz, memsz, vaddr, paddr, off
661 		 * ph[0] : txtsz+datsz, txtsz+datsz+bsssz, txtaddr-KZERO, datasize, txtoff
662 		 * ph[1] : symsz, lcsz, 0, 0, symoff
663 		 */
664 		if(ep->machine == SPARC64 && ep->phnum == 2) {
665 			ulong txtaddr, txtsz, dataddr, bsssz;
666 
667 			txtaddr = ph[0].vaddr | 0x80000000;
668 			txtsz = ph[0].filesz - ph[0].paddr;
669 			dataddr = txtaddr + txtsz;
670 			bsssz = ph[0].memsz - ph[0].filesz;
671 			settext(fp, ep->elfentry | 0x80000000, txtaddr, txtsz, ph[0].offset);
672 			setdata(fp, dataddr, ph[0].paddr, ph[0].offset + txtsz, bsssz);
673 			setsym(fp, ph[1].filesz, 0, ph[1].memsz, ph[1].offset);
674 			free(ph);
675 			return 1;
676 		}
677 
678 		werrstr("No TEXT or DATA sections");
679 		free(ph);
680 		return 0;
681 	}
682 
683 	settext(fp, ep->elfentry, ph[it].vaddr, ph[it].memsz, ph[it].offset);
684 	setdata(fp, ph[id].vaddr, ph[id].filesz, ph[id].offset, ph[id].memsz - ph[id].filesz);
685 	if(is != -1)
686 		setsym(fp, ph[is].filesz, 0, ph[is].memsz, ph[is].offset);
687 	free(ph);
688 	return 1;
689 }
690 
691 /*
692  * (Free|Net)BSD ARM header.
693  */
694 static int
695 armdotout(int fd, Fhdr *fp, ExecHdr *hp)
696 {
697 	uvlong kbase;
698 
699 	USED(fd);
700 	settext(fp, hp->e.entry, sizeof(Exec), hp->e.text, sizeof(Exec));
701 	setdata(fp, fp->txtsz, hp->e.data, fp->txtsz, hp->e.bss);
702 	setsym(fp, hp->e.syms, hp->e.spsz, hp->e.pcsz, fp->datoff+fp->datsz);
703 
704 	kbase = 0xF0000000;
705 	if ((fp->entry & kbase) == kbase) {		/* Boot image */
706 		fp->txtaddr = kbase+sizeof(Exec);
707 		fp->name = "ARM *BSD boot image";
708 		fp->hdrsz = 0;		/* header stripped */
709 		fp->dataddr = kbase+fp->txtsz;
710 	}
711 	return 1;
712 }
713 
714 static void
715 settext(Fhdr *fp, uvlong e, uvlong a, long s, vlong off)
716 {
717 	fp->txtaddr = a;
718 	fp->entry = e;
719 	fp->txtsz = s;
720 	fp->txtoff = off;
721 }
722 
723 static void
724 setdata(Fhdr *fp, uvlong a, long s, vlong off, long bss)
725 {
726 	fp->dataddr = a;
727 	fp->datsz = s;
728 	fp->datoff = off;
729 	fp->bsssz = bss;
730 }
731 
732 static void
733 setsym(Fhdr *fp, long symsz, long sppcsz, long lnpcsz, vlong symoff)
734 {
735 	fp->symsz = symsz;
736 	fp->symoff = symoff;
737 	fp->sppcsz = sppcsz;
738 	fp->sppcoff = fp->symoff+fp->symsz;
739 	fp->lnpcsz = lnpcsz;
740 	fp->lnpcoff = fp->sppcoff+fp->sppcsz;
741 }
742 
743 
744 static uvlong
745 _round(uvlong a, ulong b)
746 {
747 	uvlong w;
748 
749 	w = (a/b)*b;
750 	if (a!=w)
751 		w += b;
752 	return(w);
753 }
754