1 /* i386-openbsd.elf-main.c -- stub loader for OpenBSD x86 ELF executable
2
3 This file is part of the UPX executable compressor.
4
5 Copyright (C) 1996-2020 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 1996-2020 Laszlo Molnar
7 Copyright (C) 2000-2020 John F. Reiser
8 All Rights Reserved.
9
10 UPX and the UCL library are free software; you can redistribute them
11 and/or modify them under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; see the file COPYING.
22 If not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Markus F.X.J. Oberhumer Laszlo Molnar
26 <markus@oberhumer.com> <ezerotven+github@gmail.com>
27
28 John F. Reiser
29 <jreiser@users.sourceforge.net>
30 */
31
32
33 #include "include/bsd.h"
34
35
36 /*************************************************************************
37 // configuration section
38 **************************************************************************/
39
40 // In order to make it much easier to move this code at runtime and execute
41 // it at an address different from it load address: there must be no
42 // static data, and no string constants.
43
44 #if 1 /*{*/
45 #define DPRINTF(a) /* empty: no debug drivel */
46 #else /*}{*/
47 #include "stdarg.h"
48
49 static int
unsimal(unsigned x,char * ptr,int n)50 unsimal(unsigned x, char *ptr, int n)
51 {
52 if (10<=x) {
53 n = unsimal(x/10, ptr, n);
54 x %= 10;
55 }
56 ptr[n] = '0' + x;
57 return 1+ n;
58 }
59
60 static int
decimal(int x,char * ptr,int n)61 decimal(int x, char *ptr, int n)
62 {
63 if (x < 0) {
64 x = -x;
65 ptr[n++] = '-';
66 }
67 return unsimal(x, ptr, n);
68 }
69
70 extern char const *STR_hex();
71
72 static int
heximal(unsigned x,char * ptr,int n)73 heximal(unsigned x, char *ptr, int n)
74 {
75 if (16<=x) {
76 n = heximal(x>>4, ptr, n);
77 x &= 0xf;
78 }
79 ptr[n] = STR_hex()[x];
80 return 1+ n;
81 }
82
83
84 #define DPRINTF(a) dprintf a
85 extern char const *STR_0x();
86 extern char const *STR_xread();
87 extern char const *STR_unpackExtent();
88 extern char const *STR_make_hatch_arm();
89 extern char const *STR_auxv_up();
90 extern char const *STR_xfind_pages();
91 extern char const *STR_do_xmap();
92 extern char const *STR_upx_main();
93
94 static int
dprintf(char const * fmt,...)95 dprintf(char const *fmt, ...)
96 {
97 char c;
98 int n= 0;
99 char *ptr;
100 char buf[20];
101 va_list va; va_start(va, fmt);
102 ptr= &buf[0];
103 while (0!=(c= *fmt++)) if ('%'!=c) goto literal;
104 else switch (c= *fmt++) {
105 default: {
106 literal:
107 n+= write(2, fmt-1, 1);
108 } break;
109 case 0: goto done; /* early */
110 case 'u': {
111 n+= write(2, buf, unsimal(va_arg(va, unsigned), buf, 0));
112 } break;
113 case 'd': {
114 n+= write(2, buf, decimal(va_arg(va, int), buf, 0));
115 } break;
116 case 'p': /* same as 'x'; relies on sizeof(int)==sizeof(void *) */
117 case 'x': {
118 buf[0] = '0';
119 buf[1] = 'x';
120 n+= write(2, buf, heximal(va_arg(va, int), buf, 2));
121 } break;
122 }
123 done:
124 va_end(va);
125 return n;
126 }
127 #endif /*}*/
128
129 #define MAX_ELF_HDR 512 // Elf32_Ehdr + n*Elf32_Phdr must fit in this
130
131
132 /*************************************************************************
133 // "file" util
134 **************************************************************************/
135
136 struct Extent {
137 size_t size; // must be first to match size[0] uncompressed size
138 char *buf;
139 };
140
141
142 static void
143 #if (ACC_CC_GNUC >= 0x030300) && defined(__i386__) /*{*/
144 __attribute__((__noinline__, __used__, regparm(3), stdcall))
145 #endif /*}*/
xread(struct Extent * x,char * buf,size_t count)146 xread(struct Extent *x, char *buf, size_t count)
147 {
148 char *p=x->buf, *q=buf;
149 size_t j;
150 DPRINTF((STR_xread(), x, x->size, x->buf, buf, count));
151 if (x->size < count) {
152 exit(127);
153 }
154 for (j = count; 0!=j--; ++p, ++q) {
155 *q = *p;
156 }
157 x->buf += count;
158 x->size -= count;
159 }
160
161
162 /*************************************************************************
163 // util
164 **************************************************************************/
165
166 #if 1 //{ save space
167 #define ERR_LAB error: exit(127);
168 #define err_exit(a) goto error
169 #else //}{ save debugging time
170 #define ERR_LAB /*empty*/
171 static void
172 err_exit(int a) __attribute__ ((__noreturn__));
173 {
174 (void)a; // debugging convenience
175 exit(127);
176 }
177 #endif //}
178
179 /*************************************************************************
180 // UPX & NRV stuff
181 **************************************************************************/
182
183 typedef void f_unfilter(
184 nrv_byte *, // also addvalue
185 nrv_uint,
186 unsigned cto8, // junk in high 24 bits
187 unsigned ftid
188 );
189 typedef int f_expand(
190 const nrv_byte *, nrv_uint,
191 nrv_byte *, nrv_uint *, unsigned );
192
193 static void
unpackExtent(struct Extent * const xi,struct Extent * const xo,f_expand * const f_decompress,f_unfilter * f_unf)194 unpackExtent(
195 struct Extent *const xi, // input
196 struct Extent *const xo, // output
197 f_expand *const f_decompress,
198 f_unfilter *f_unf
199 )
200 {
201 DPRINTF((STR_unpackExtent(),
202 xi, xi->size, xi->buf, xo, xo->size, xo->buf, f_decompress, f_unf));
203 while (xo->size) {
204 struct b_info h;
205 // Note: if h.sz_unc == h.sz_cpr then the block was not
206 // compressible and is stored in its uncompressed form.
207
208 // Read and check block sizes.
209 xread(xi, (char *)&h, sizeof(h));
210 if (h.sz_unc == 0) { // uncompressed size 0 -> EOF
211 if (h.sz_cpr != UPX_MAGIC_LE32) // h.sz_cpr must be h->magic
212 err_exit(2);
213 if (xi->size != 0) // all bytes must be written
214 err_exit(3);
215 break;
216 }
217 if (h.sz_cpr <= 0) {
218 err_exit(4);
219 ERR_LAB
220 }
221 if (h.sz_cpr > h.sz_unc
222 || h.sz_unc > xo->size ) {
223 err_exit(5);
224 }
225 // Now we have:
226 // assert(h.sz_cpr <= h.sz_unc);
227 // assert(h.sz_unc > 0 && h.sz_unc <= blocksize);
228 // assert(h.sz_cpr > 0 && h.sz_cpr <= blocksize);
229
230 if (h.sz_cpr < h.sz_unc) { // Decompress block
231 nrv_uint out_len = h.sz_unc; // EOF for lzma
232 int const j = (*f_decompress)((unsigned char *)xi->buf, h.sz_cpr,
233 (unsigned char *)xo->buf, &out_len, *(int *)(void *)&h.b_method );
234 if (j != 0 || out_len != (nrv_uint)h.sz_unc)
235 err_exit(7);
236 // Skip Ehdr+Phdrs: separate 1st block, not filtered
237 if (h.b_ftid!=0 && f_unf // have filter
238 && ((512 < out_len) // this block is longer than Ehdr+Phdrs
239 || (xo->size==(unsigned)h.sz_unc) ) // block is last in Extent
240 ) {
241 (*f_unf)((unsigned char *)xo->buf, out_len, h.b_cto8, h.b_ftid);
242 }
243 xi->buf += h.sz_cpr;
244 xi->size -= h.sz_cpr;
245 }
246 else { // copy literal block
247 xread(xi, xo->buf, h.sz_cpr);
248 }
249 xo->buf += h.sz_unc;
250 xo->size -= h.sz_unc;
251 }
252 }
253
254 #if defined(__i386__) /*{*/
255 // Create (or find) an escape hatch to use when munmapping ourselves the stub.
256 // Called by do_xmap to create it; remembered in AT_NULL.d_val
257 static void *
make_hatch_x86(Elf32_Phdr const * const phdr,unsigned const reloc)258 make_hatch_x86(Elf32_Phdr const *const phdr, unsigned const reloc)
259 {
260 unsigned *hatch = 0;
261 if (phdr->p_type==PT_LOAD && phdr->p_flags & PF_X) {
262 // The format of the 'if' is
263 // if ( ( (hatch = loc1), test_loc1 )
264 // || ( (hatch = loc2), test_loc2 ) ) {
265 // action
266 // }
267 // which uses the comma to save bytes when test_locj involves locj
268 // and the action is the same when either test succeeds.
269
270 // Try page fragmentation just beyond .text .
271 if ( ( (hatch = (void *)(phdr->p_memsz + phdr->p_vaddr + reloc)),
272 ( phdr->p_memsz==phdr->p_filesz // don't pollute potential .bss
273 && 4<=(~PAGE_MASK & -(int)hatch) ) ) // space left on page
274 // Try Elf32_Ehdr.e_ident[12..15] . warning: 'const' cast away
275 || ( (hatch = (void *)(&((Elf32_Ehdr *)phdr->p_vaddr + reloc)->e_ident[12])),
276 (phdr->p_offset==0) ) ) {
277 // Omitting 'const' saves repeated literal in gcc.
278 unsigned /*const*/ escape = 0xc3c980cd; // "int $0x80; leave; ret"
279 // Don't store into read-only page if value is already there.
280 if (* (volatile unsigned*) hatch != escape) {
281 * hatch = escape;
282 }
283 }
284 }
285 return hatch;
286 }
287 #elif defined(__arm__) /*}{*/
288 static void *
make_hatch_arm(Elf32_Phdr const * const phdr,unsigned const reloc)289 make_hatch_arm(Elf32_Phdr const *const phdr, unsigned const reloc)
290 {
291 unsigned *hatch = 0;
292 DPRINTF((STR_make_hatch_arm(),phdr,reloc));
293 if (phdr->p_type==PT_LOAD && phdr->p_flags & PF_X) {
294 // The format of the 'if' is
295 // if ( ( (hatch = loc1), test_loc1 )
296 // || ( (hatch = loc2), test_loc2 ) ) {
297 // action
298 // }
299 // which uses the comma to save bytes when test_locj involves locj
300 // and the action is the same when either test succeeds.
301
302 // Try page fragmentation just beyond .text .
303 if ( ( (hatch = (void *)(phdr->p_memsz + phdr->p_vaddr + reloc)),
304 ( phdr->p_memsz==phdr->p_filesz // don't pollute potential .bss
305 && 8<=(~PAGE_MASK & -(int)hatch) ) ) // space left on page
306 // Try Elf32_Ehdr.e_ident[8..15] . warning: 'const' cast away
307 || ( (hatch = (void *)(&((Elf32_Ehdr *)phdr->p_vaddr + reloc)->e_ident[8])),
308 (phdr->p_offset==0) ) )
309 {
310 hatch[0]= 0xef90005b; // syscall __NR_unmap
311 hatch[1]= 0xe1a0f00e; // mov pc,lr
312 }
313 }
314 return hatch;
315 }
316 #endif /*}*/
317
318 static void
319 #if defined(__i386__) /*{*/
320 __attribute__((regparm(2), stdcall))
321 #endif /*}*/
upx_bzero(char * p,size_t len)322 upx_bzero(char *p, size_t len)
323 {
324 if (len) do {
325 *p++= 0;
326 } while (--len);
327 }
328 #define bzero upx_bzero
329
330
331 static void
332 #if defined(__i386__) /*{*/
333 __attribute__((regparm(3), stdcall))
334 #endif /*}*/
auxv_up(Elf32_auxv_t * av,unsigned const type,unsigned const value)335 auxv_up(Elf32_auxv_t *av, unsigned const type, unsigned const value)
336 {
337 DPRINTF((STR_auxv_up(),av,type,value));
338 if (av
339 #if defined(__i386__) /*{*/
340 && 0==(1&(int)av) /* PT_INTERP usually inhibits, except for hatch */
341 #endif /*}*/
342 )
343 for (;; ++av) {
344 if (av->a_type==type || (av->a_type==AT_IGNORE && type!=AT_NULL)) {
345 av->a_type = type;
346 av->a_un.a_val = value;
347 return;
348 }
349 if (av->a_type==AT_NULL) {
350 // We can't do this as part of the for loop because we overwrite
351 // AT_NULL too.
352 return;
353 }
354 }
355 }
356
357 // The PF_* and PROT_* bits are {1,2,4}; the conversion table fits in 32 bits.
358 #define REP8(x) \
359 ((x)|((x)<<4)|((x)<<8)|((x)<<12)|((x)<<16)|((x)<<20)|((x)<<24)|((x)<<28))
360 #define EXP8(y) \
361 ((1&(y)) ? 0xf0f0f0f0 : (2&(y)) ? 0xff00ff00 : (4&(y)) ? 0xffff0000 : 0)
362 #define PF_TO_PROT(pf) \
363 ((PROT_READ|PROT_WRITE|PROT_EXEC) & ( \
364 ( (REP8(PROT_EXEC ) & EXP8(PF_X)) \
365 |(REP8(PROT_READ ) & EXP8(PF_R)) \
366 |(REP8(PROT_WRITE) & EXP8(PF_W)) \
367 ) >> ((pf & (PF_R|PF_W|PF_X))<<2) ))
368
369
370 static unsigned
umax(unsigned a,unsigned b)371 umax(unsigned a, unsigned b)
372 {
373 return ((a<=b) ? b : a);
374 }
375
376 // OpenBSD 3.9 wants all .text from all modules together, then (above that)
377 // all .data from all modules together, in order to maximize the effectiveness
378 // of lowering the segment limit on %cs as an implementation of "no-execute
379 // .data". Thus OpenBSD 3.9 puts a gap of 0x20000000 (512 MiB) between the
380 // origins of .text and .data in each module. So, mapping ET_DYN must "level"
381 // the excursion in .text and .data of anything which preceded it,
382 // then add the new PT_LOAD.
383
384 static unsigned long // returns relocation constant
385 #if defined(__i386__) /*{*/
386 __attribute__((regparm(3), stdcall))
387 #endif /*}*/
xfind_pages(unsigned const e_type,Elf32_Phdr const * phdr,int phnum,Elf32_Addr old_hi[2])388 xfind_pages(unsigned const e_type, Elf32_Phdr const *phdr, int phnum,
389 Elf32_Addr old_hi[2])
390 {
391 size_t sz[2]= { 0u, 0u}; // [0] is .data; [1] is .text
392 size_t hi[2]= { 0u, 0u};
393 size_t lo[2]= {~0u, ~0u};
394 char *addr[2];
395 unsigned level= 0;
396 int j;
397
398 DPRINTF((STR_xfind_pages(), mflags, phdr, phnum, p_brk));
399 for (; --phnum>=0; ++phdr) if (PT_LOAD==phdr->p_type) {
400 unsigned const td = PF_X & phdr->p_flags; // requires 1==PF_X
401 if (lo[td] > phdr->p_vaddr) {
402 lo[td] = phdr->p_vaddr;
403 sz[td] = phdr->p_filesz;
404 }
405 if (hi[td] < (phdr->p_memsz + phdr->p_vaddr)) {
406 hi[td] = phdr->p_memsz + phdr->p_vaddr;
407 }
408 }
409 if ((ET_EXEC!=e_type) && 0x20000000u<=hi[0]) { // is split
410 level= (0xfc000000u & old_hi[1]) + // "segment base" of old .text
411 umax(0x03ffffffu & old_hi[1],
412 0x03ffffffu & old_hi[0] );
413 level= PAGE_MASK & (level - PAGE_MASK -1); // ALIGN_UP
414 }
415 for (j=0; j<=1; ++j) {
416 sz[j] += ~PAGE_MASK & lo[j]; // page fragment on lo edge
417 lo[j] -= ~PAGE_MASK & lo[j]; // round down to page boundary
418 old_hi[j] = level + hi[j];
419 hi[j] = PAGE_MASK & (hi[j] - lo[j] - PAGE_MASK -1); // page length
420 sz[j] = PAGE_MASK & (sz[j] - PAGE_MASK -1); // page length
421 addr[j] = mmap((void *)(level + lo[j]), hi[j], PROT_NONE,
422 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
423 }
424 return (unsigned long)addr[0] - lo[0];
425 }
426
427 static Elf32_Addr // entry address
do_xmap(int const fdi,Elf32_Ehdr const * const ehdr,Elf32_Addr old_hi[2],struct Extent * const xi,Elf32_auxv_t * const av,unsigned * p_reloc,f_unfilter * const f_unf)428 do_xmap(int const fdi, Elf32_Ehdr const *const ehdr,
429 Elf32_Addr old_hi[2], struct Extent *const xi,
430 Elf32_auxv_t *const av, unsigned *p_reloc, f_unfilter *const f_unf)
431 {
432 Elf32_Phdr const *phdr = (Elf32_Phdr const *) (ehdr->e_phoff +
433 (void const *)ehdr);
434 unsigned const reloc = xfind_pages(ehdr->e_type, phdr, ehdr->e_phnum, old_hi);
435 int j;
436
437 DPRINTF((STR_do_xmap(),
438 fdi, ehdr, xi, (xi? xi->size: 0), (xi? xi->buf: 0), av, p_reloc, f_unf));
439 for (j=0; j < ehdr->e_phnum; ++phdr, ++j)
440 if (PT_PHDR==phdr->p_type) {
441 if (xi) {
442 auxv_up(av, AT_PHDR, phdr->p_vaddr + reloc);
443 }
444 }
445 else if (PT_LOAD==phdr->p_type) {
446 unsigned const prot = PF_TO_PROT(phdr->p_flags);
447 struct Extent xo;
448 size_t mlen = xo.size = phdr->p_filesz;
449 char *addr = xo.buf = (char *)(phdr->p_vaddr + reloc);
450 char *haddr = phdr->p_memsz + addr;
451 size_t frag = (int)addr &~ PAGE_MASK;
452 mlen += frag;
453 addr -= frag;
454
455 if (addr != mmap(addr, mlen
456 #if defined(__i386__) /*{*/
457 // Decompressor can overrun the destination by 3 bytes.
458 + (xi ? 3 : 0)
459 #endif /*}*/
460 , prot | (xi ? PROT_WRITE : 0),
461 MAP_FIXED | MAP_PRIVATE | (xi ? MAP_ANONYMOUS : 0),
462 (xi ? -1 : fdi), phdr->p_offset - frag) ) {
463 err_exit(8);
464 }
465 if (xi) {
466 unpackExtent(xi, &xo, (f_expand *)fdi,
467 ((PROT_EXEC & prot) ? f_unf : 0) );
468 }
469 // Linux does not fixup the low end, so neither do we.
470 //if (PROT_WRITE & prot) {
471 // bzero(addr, frag); // fragment at lo end
472 //}
473 frag = (-mlen) &~ PAGE_MASK; // distance to next page boundary
474 if (PROT_WRITE & prot) { // note: read-only .bss not supported here
475 bzero(mlen+addr, frag); // fragment at hi end
476 }
477 if (xi) {
478 #if defined(__i386__) /*{*/
479 void *const hatch = make_hatch_x86(phdr, reloc);
480 if (0!=hatch) {
481 /* always update AT_NULL, especially for compressed PT_INTERP */
482 auxv_up((Elf32_auxv_t *)(~1 & (int)av), AT_NULL, (unsigned)hatch);
483 }
484 #elif defined(__arm__) /*}{*/
485 void *const hatch = make_hatch_arm(phdr, reloc);
486 if (0!=hatch) {
487 auxv_up((Elf32_auxv_t *)(void *)av, AT_NULL, (unsigned)hatch);
488 }
489 #endif /*}*/
490 if (0!=mprotect(addr, mlen, prot)) {
491 err_exit(10);
492 ERR_LAB
493 }
494 }
495 addr += mlen + frag; /* page boundary on hi end */
496 if (addr < haddr) { // need pages for .bss
497 if (addr != mmap(addr, haddr - addr, prot,
498 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 ) ) {
499 err_exit(9);
500 }
501 }
502 #if defined(__i386__) /*{*/
503 else if (xi) { // cleanup if decompressor overrun crosses page boundary
504 mlen = ~PAGE_MASK & (3+ mlen);
505 if (mlen<=3) { // page fragment was overrun buffer only
506 munmap(addr, mlen);
507 }
508 }
509 #endif /*}*/
510 }
511 if (!xi) { // 2nd call (PT_INTERP); close()+check is smaller here
512 if (0!=close(fdi)) {
513 err_exit(11);
514 }
515 }
516 if (0!=p_reloc) {
517 *p_reloc = reloc;
518 }
519 return ehdr->e_entry + reloc;
520 }
521
522
523 /*************************************************************************
524 // upx_main - called by our entry code
525 //
526 // This function is optimized for size.
527 **************************************************************************/
528
529 void *upx_main(
530 Elf32_auxv_t *const av,
531 unsigned const sz_compressed,
532 f_expand *const f_decompress,
533 f_unfilter */*const*/ f_unfilter,
534 struct Extent xo,
535 struct Extent xi,
536 unsigned const volatile dynbase
537 ) __asm__("upx_main");
538
upx_main(Elf32_auxv_t * const av,unsigned const sz_compressed,f_expand * const f_decompress,f_unfilter * f_unf,struct Extent xo,struct Extent xi,unsigned const volatile dynbase)539 void *upx_main(
540 Elf32_auxv_t *const av,
541 unsigned const sz_compressed,
542 f_expand *const f_decompress,
543 f_unfilter */*const*/ f_unf,
544 struct Extent xo, // {sz_unc, ehdr} for ELF headers
545 struct Extent xi, // {sz_cpr, &b_info} for ELF headers
546 unsigned const volatile dynbase // value+result: compiler must not change
547 )
548 {
549 Elf32_Ehdr *const ehdr = (Elf32_Ehdr *)(void *)xo.buf; // temp char[MAX_ELF_HDR+OVERHEAD]
550 Elf32_Phdr const *phdr = (Elf32_Phdr const *)(1+ ehdr);
551 Elf32_Addr old_hi[2]= {0u, 0u};
552 Elf32_Addr reloc;
553 Elf32_Addr entry;
554
555 // sizeof(Ehdr+Phdrs), compressed; including b_info header
556 size_t const sz_pckhdrs = xi.size;
557
558 DPRINTF((STR_upx_main(),
559 av, sz_compressed, f_decompress, f_unf, &xo, xo.size, xo.buf,
560 &xi, xi.size, xi.buf, dynbase));
561 #if defined(__i386__) /*{*/
562 f_unf = (f_unfilter *)(2+ (long)f_decompress);
563 #endif /*}*/
564
565 // Uncompress Ehdr and Phdrs.
566 unpackExtent(&xi, &xo, f_decompress, 0);
567
568 // Prepare to decompress the Elf headers again, into the first PT_LOAD.
569 xi.buf -= sz_pckhdrs;
570 xi.size = sz_compressed;
571
572 // Some kernels omit AT_PHNUM,AT_PHENT,AT_PHDR because this stub has no PT_INTERP.
573 // That is "too much" optimization. Linux 2.6.x seems to give all AT_*.
574 auxv_up(av, AT_PAGESZ, PAGE_SIZE);
575 auxv_up(av, AT_PHNUM , ehdr->e_phnum);
576 auxv_up(av, AT_PHENT , ehdr->e_phentsize);
577 auxv_up(av, AT_PHDR , dynbase + (unsigned)(1+(Elf32_Ehdr *)phdr->p_vaddr));
578 // AT_PHDR.a_un.a_val is set again by do_xmap if PT_PHDR is present.
579 // This is necessary for ET_DYN if|when we override a prelink address.
580
581 entry = do_xmap((int)f_decompress, ehdr, old_hi, &xi, av, &reloc, f_unf);
582 auxv_up(av, AT_ENTRY , entry); // might not be necessary?
583
584 { // Map PT_INTERP program interpreter
585 int j;
586 for (j=0; j < ehdr->e_phnum; ++phdr, ++j) if (PT_INTERP==phdr->p_type) {
587 int const fdi = open(reloc + (char const *)phdr->p_vaddr, O_RDONLY, 0);
588 if (0 > fdi) {
589 err_exit(18);
590 }
591 if (MAX_ELF_HDR!=read(fdi, (void *)ehdr, MAX_ELF_HDR)) {
592 ERR_LAB
593 err_exit(19);
594 }
595 entry = do_xmap(fdi, ehdr, old_hi, 0, 0, &reloc, 0);
596 auxv_up(av, AT_BASE, reloc);
597 break;
598 }
599 }
600
601 return (void *)entry;
602 }
603
604 /* vim:set ts=4 sw=4 et: */
605