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