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