Don't include pub_tool_tooliface.h in priv_types_n_macros.h
[valgrind.git] / coregrind / m_ume / elf.c
bloba850a50cfa56b22830cd54f635afef69bc25342a
2 /*--------------------------------------------------------------------*/
3 /*--- User-mode execve() for ELF executables m_ume_elf.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2000-2017 Julian Seward
11 jseward@acm.org
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
31 #include "pub_core_basics.h"
32 #include "pub_core_vki.h"
34 #include "pub_core_aspacemgr.h" // various mapping fns
35 #include "pub_core_debuglog.h"
36 #include "pub_core_libcassert.h" // VG_(exit), vg_assert
37 #include "pub_core_libcbase.h" // VG_(memcmp), etc
38 #include "pub_core_libcprint.h"
39 #include "pub_core_libcfile.h" // VG_(open) et al
40 #include "pub_core_machine.h" // VG_ELF_CLASS (XXX: which should be moved)
41 #include "pub_core_mallocfree.h" // VG_(malloc), VG_(free)
42 #include "pub_core_vkiscnums.h"
43 #include "pub_core_syscall.h" // VG_(strerror)
44 #include "pub_core_ume.h" // self
46 #include "priv_ume.h"
48 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
49 #if defined(VGO_linux)
50 # define _GNU_SOURCE
51 # define _FILE_OFFSET_BITS 64
52 #endif
53 /* This is for ELF types etc, and also the AT_ constants. */
54 #include <elf.h>
55 #if defined(VGO_solaris)
56 # include <sys/fasttrap.h> // PT_SUNWDTRACE_SIZE
57 # if defined(SOLARIS_PT_SUNDWTRACE_THRP)
58 # define PT_SUNWDTRACE_PROTECTION (PF_R)
59 # else
60 # define PT_SUNWDTRACE_PROTECTION (PF_R | PF_W | PF_X)
61 # endif
62 #endif
63 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
66 #if VG_WORDSIZE == 8
67 #define ESZ(x) Elf64_##x
68 #elif VG_WORDSIZE == 4
69 #define ESZ(x) Elf32_##x
70 #else
71 #error VG_WORDSIZE needs to ==4 or ==8
72 #endif
74 struct elfinfo
76 ESZ(Ehdr) e;
77 ESZ(Phdr) *p;
78 Int fd;
81 #if defined(VGO_linux)
84 arch_elf_pt_proc() - check a PT_LOPROC..PT_HIPROC ELF program header
85 @ehdr: The main ELF header
86 @phdr: The program header to check
87 @fd: The ELF file filedescriptor
88 @is_interpreter: True if the phdr is from the interpreter of the ELF
89 being loaded, else false.
90 @state: Architecture-specific state preserved throughout the process
91 of loading the ELF.
93 Inspects the program header phdr to validate its correctness and/or
94 suitability for the system. Called once per ELF program header in the
95 range PT_LOPROC to PT_HIPROC, for both the ELF being loaded and its
96 interpreter.
98 Return: Zero to proceed with the ELF load, non-zero to fail the ELF load
99 with that return code.
101 arch_check_elf()
102 @ehdr: The main ELF header
103 @has_interpreter: True if the ELF has an interpreter, else false.
104 @state: Architecture-specific state preserved throughout the process
105 of loading the ELF.
107 Provides a final opportunity for architecture code to reject the loading
108 of the ELF. This is called after all program headers to be checked by
109 arch_elf_pt_proc have been.
111 Return: Zero to proceed with the ELF load, non-zero to fail the ELF load
112 with that return code.
114 Ref: linux/fs/binfmt_elf.c
117 # if defined(VGP_mips32_linux)
119 /* Ref: linux/arch/mips/kernel/elf.c */
120 static inline Int arch_elf_pt_proc(ESZ(Ehdr) *ehdr,
121 ESZ(Phdr) *phdr,
122 Int fd, Bool is_interpreter,
123 struct vki_arch_elf_state *state)
125 struct vki_mips_elf_abiflags_v0 abiflags;
126 SysRes sres;
128 if ( (ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
129 (ehdr->e_flags & VKI_EF_MIPS_FP64) ) {
131 * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
132 * later if needed
134 if (is_interpreter)
135 state->interp_fp_abi = VKI_MIPS_ABI_FP_OLD_64;
136 else
137 state->fp_abi = VKI_MIPS_ABI_FP_OLD_64;
140 if (phdr->p_type != VKI_PT_MIPS_ABIFLAGS)
141 return 0;
143 if (phdr->p_filesz < sizeof(abiflags))
144 return VKI_EINVAL;
146 sres = VG_(pread)(fd, &abiflags, sizeof(abiflags), phdr->p_offset);
148 if (sr_isError(sres))
149 return sr_Err(sres);
151 if (sr_Res(sres) != sizeof(abiflags))
152 return VKI_EIO;
154 /* Record the required FP ABIs for use by arch_check_elf */
155 if (is_interpreter)
156 state->interp_fp_abi = abiflags.fp_abi;
157 else
158 state->fp_abi = abiflags.fp_abi;
160 return 0;
163 /* Ref: linux/arch/mips/kernel/elf.c */
164 static inline Int arch_check_elf(ESZ(Ehdr) *ehdr,
165 Bool has_interpreter,
166 struct vki_arch_elf_state *state)
168 struct mode_req {
169 Bool single;
170 Bool soft;
171 Bool fr1;
172 Bool frdefault;
173 Bool fre;
176 struct mode_req fpu_reqs[] = {
177 [VKI_MIPS_ABI_FP_ANY] = { True, True, True, True, True },
178 [VKI_MIPS_ABI_FP_DOUBLE] = { False, False, False, True, True },
179 [VKI_MIPS_ABI_FP_SINGLE] = { True, False, False, False, False },
180 [VKI_MIPS_ABI_FP_SOFT] = { False, True, False, False, False },
181 [VKI_MIPS_ABI_FP_OLD_64] = { False, False, False, False, False },
182 [VKI_MIPS_ABI_FP_XX] = { False, False, True, True, True },
183 [VKI_MIPS_ABI_FP_64] = { False, False, True, False, False },
184 [VKI_MIPS_ABI_FP_64A] = { False, False, True, False, True }
187 /* Mode requirements when .MIPS.abiflags is not present in the ELF.
188 Not present means that everything is acceptable except FR1. */
189 struct mode_req none_req = { True, True, False, True, True };
191 struct mode_req prog_req, interp_req;
192 Int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
193 Bool is_mips64;
195 VexArchInfo vai;
196 VG_(machine_get_VexArchInfo)(NULL, &vai);
198 fp_abi = state->fp_abi;
200 if (has_interpreter) {
201 interp_fp_abi = state->interp_fp_abi;
203 abi0 = VG_MIN(fp_abi, interp_fp_abi);
204 abi1 = VG_MAX(fp_abi, interp_fp_abi);
205 } else {
206 abi0 = abi1 = fp_abi;
209 is_mips64 = (ehdr->e_ident[EI_CLASS] == ELFCLASS64) ||
210 (ehdr->e_flags & EF_MIPS_ABI2);
212 if (is_mips64) {
213 /* MIPS64 code always uses FR=1, thus the default is easy */
214 state->overall_fp_mode = VKI_FP_FR1;
216 /* Disallow access to the various FPXX & FP64 ABIs */
217 max_abi = VKI_MIPS_ABI_FP_SOFT;
218 } else {
219 /* Default to a mode capable of running code expecting FR=0 */
221 /* TODO: Should be changed during implementation of MIPS-R6 support.
222 state->overall_fp_mode = cpu_has_mips_r6 ? VKI_FP_FRE : VKI_FP_FR0; */
223 state->overall_fp_mode = VKI_FP_FR0;
225 /* Allow all ABIs we know about */
226 max_abi = VKI_MIPS_ABI_FP_64A;
229 if ((abi0 > max_abi && abi0 != VKI_MIPS_ABI_FP_UNKNOWN) ||
230 (abi1 > max_abi && abi1 != VKI_MIPS_ABI_FP_UNKNOWN))
231 return VKI_ELIBBAD;
233 /* It's time to determine the FPU mode requirements */
234 prog_req = (abi0 == VKI_MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
235 interp_req = (abi1 == VKI_MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
237 /* Check whether the program's and interp's ABIs have a matching FPU
238 mode requirement. */
239 prog_req.single = interp_req.single && prog_req.single;
240 prog_req.soft = interp_req.soft && prog_req.soft;
241 prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
242 prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
243 prog_req.fre = interp_req.fre && prog_req.fre;
245 /* Determine the desired FPU mode
247 Decision making:
249 - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
250 means that we have a combination of program and interpreter
251 that inherently require the hybrid FP mode.
252 - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
253 fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
254 instructions so we don't care about the mode. We will simply use
255 the one preferred by the hardware. In fpxx case, that ABI can
256 handle both FR=1 and FR=0, so, again, we simply choose the one
257 preferred by the hardware. Next, if we only use single-precision
258 FPU instructions, and the default ABI FPU mode is not good
259 (ie single + any ABI combination), we set again the FPU mode to the
260 one is preferred by the hardware. Next, if we know that the code
261 will only use single-precision instructions, shown by single being
262 true but frdefault being false, then we again set the FPU mode to
263 the one that is preferred by the hardware.
264 - We want FP_FR1 if that's the only matching mode and the default one
265 is not good.
266 - Return with ELIBADD if we can't find a matching FPU mode. */
267 if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
268 state->overall_fp_mode = VKI_FP_FRE;
269 else if ((prog_req.fr1 && prog_req.frdefault) ||
270 (prog_req.single && !prog_req.frdefault))
271 state->overall_fp_mode = VEX_MIPS_HOST_FP_MODE(vai.hwcaps) ?
272 VKI_FP_FR1 : VKI_FP_FR0;
273 else if (prog_req.fr1)
274 state->overall_fp_mode = VKI_FP_FR1;
275 else if (!prog_req.fre && !prog_req.frdefault &&
276 !prog_req.fr1 && !prog_req.single && !prog_req.soft)
277 return VKI_ELIBBAD;
279 /* TODO: Currently, Valgrind doesn't support FRE and doesn't support FR1
280 emulation on FR0 system, so in those cases we are forced to
281 reject the ELF. */
282 if ((state->overall_fp_mode == VKI_FP_FRE) ||
283 ((state->overall_fp_mode == VKI_FP_FR1) &&
284 !VEX_MIPS_HOST_FP_MODE(vai.hwcaps)))
285 return VKI_ELIBBAD;
287 return 0;
290 # else
292 static inline Int arch_elf_pt_proc(ESZ(Ehdr) *ehdr,
293 ESZ(Phdr) *phdr,
294 Int fd, Bool is_interpreter,
295 struct vki_arch_elf_state *state)
297 /* Dummy implementation, always proceed */
298 return 0;
301 static inline Int arch_check_elf(ESZ(Ehdr) *ehdr,
302 Bool has_interpreter,
303 struct vki_arch_elf_state *state)
305 /* Dummy implementation, always proceed */
306 return 0;
309 # endif
310 #endif
312 static void check_mmap(SysRes res, Addr base, SizeT len)
314 if (sr_isError(res)) {
315 VG_(printf)("valgrind: mmap(0x%llx, %lld) failed in UME "
316 "with error %lu (%s).\n",
317 (ULong)base, (Long)len,
318 sr_Err(res), VG_(strerror)(sr_Err(res)) );
319 if (sr_Err(res) == VKI_EINVAL) {
320 VG_(printf)("valgrind: this can be caused by executables with "
321 "very large text, data or bss segments.\n");
323 VG_(exit)(1);
327 /*------------------------------------------------------------*/
328 /*--- Loading ELF files ---*/
329 /*------------------------------------------------------------*/
331 static
332 struct elfinfo *readelf(Int fd, const HChar *filename)
334 SysRes sres;
335 struct elfinfo *e = VG_(malloc)("ume.re.1", sizeof(*e));
336 Int phsz;
338 e->fd = fd;
340 sres = VG_(pread)(fd, &e->e, sizeof(e->e), 0);
341 if (sr_isError(sres) || sr_Res(sres) != sizeof(e->e)) {
342 VG_(printf)("valgrind: %s: can't read ELF header: %s\n",
343 filename, VG_(strerror)(sr_Err(sres)));
344 goto bad;
347 if (VG_(memcmp)(&e->e.e_ident[0], ELFMAG, SELFMAG) != 0) {
348 VG_(printf)("valgrind: %s: bad ELF magic number\n", filename);
349 goto bad;
351 if (e->e.e_ident[EI_CLASS] != VG_ELF_CLASS) {
352 VG_(printf)("valgrind: wrong ELF executable class "
353 "(eg. 32-bit instead of 64-bit)\n");
354 goto bad;
356 if (e->e.e_ident[EI_DATA] != VG_ELF_DATA2XXX) {
357 VG_(printf)("valgrind: executable has wrong endian-ness\n");
358 goto bad;
360 if (!(e->e.e_type == ET_EXEC || e->e.e_type == ET_DYN)) {
361 VG_(printf)("valgrind: this is not an executable\n");
362 goto bad;
365 if (e->e.e_machine != VG_ELF_MACHINE) {
366 VG_(printf)("valgrind: executable is not for "
367 "this architecture\n");
368 goto bad;
371 if (e->e.e_phentsize != sizeof(ESZ(Phdr))) {
372 VG_(printf)("valgrind: sizeof ELF Phdr wrong\n");
373 goto bad;
376 phsz = sizeof(ESZ(Phdr)) * e->e.e_phnum;
377 e->p = VG_(malloc)("ume.re.2", phsz);
379 sres = VG_(pread)(fd, e->p, phsz, e->e.e_phoff);
380 if (sr_isError(sres) || sr_Res(sres) != phsz) {
381 VG_(printf)("valgrind: can't read phdr: %s\n",
382 VG_(strerror)(sr_Err(sres)));
383 VG_(free)(e->p);
384 goto bad;
387 return e;
389 bad:
390 VG_(free)(e);
391 return NULL;
394 /* Map an ELF file. Returns the brk address. */
395 static
396 ESZ(Addr) mapelf(struct elfinfo *e, ESZ(Addr) base)
398 Int i;
399 SysRes res;
400 ESZ(Addr) elfbrk = 0;
402 for (i = 0; i < e->e.e_phnum; i++) {
403 ESZ(Phdr) *ph = &e->p[i];
404 ESZ(Addr) addr, brkaddr;
405 ESZ(Word) memsz;
407 if (ph->p_type != PT_LOAD)
408 continue;
410 addr = ph->p_vaddr+base;
411 memsz = ph->p_memsz;
412 brkaddr = addr+memsz;
414 if (brkaddr > elfbrk)
415 elfbrk = brkaddr;
418 for (i = 0; i < e->e.e_phnum; i++) {
419 ESZ(Phdr) *ph = &e->p[i];
420 ESZ(Addr) addr, bss, brkaddr;
421 ESZ(Off) off;
422 ESZ(Word) filesz;
423 ESZ(Word) memsz;
424 unsigned prot = 0;
426 if (ph->p_type != PT_LOAD)
427 continue;
429 if (ph->p_flags & PF_X) prot |= VKI_PROT_EXEC;
430 if (ph->p_flags & PF_W) prot |= VKI_PROT_WRITE;
431 if (ph->p_flags & PF_R) prot |= VKI_PROT_READ;
433 addr = ph->p_vaddr+base;
434 off = ph->p_offset;
435 filesz = ph->p_filesz;
436 bss = addr+filesz;
437 memsz = ph->p_memsz;
438 brkaddr = addr+memsz;
440 // Tom says: In the following, do what the Linux kernel does and only
441 // map the pages that are required instead of rounding everything to
442 // the specified alignment (ph->p_align). (AMD64 doesn't work if you
443 // use ph->p_align -- part of stage2's memory gets trashed somehow.)
445 // The condition handles the case of a zero-length segment.
446 if (VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr) > 0) {
447 if (0) VG_(debugLog)(0,"ume","mmap_file_fixed_client #1\n");
448 res = VG_(am_mmap_file_fixed_client)(
449 VG_PGROUNDDN(addr),
450 VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr),
451 prot, /*VKI_MAP_FIXED|VKI_MAP_PRIVATE, */
452 e->fd, VG_PGROUNDDN(off)
454 if (0) VG_(am_show_nsegments)(0,"after #1");
455 check_mmap(res, VG_PGROUNDDN(addr),
456 VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr));
459 // if memsz > filesz, fill the remainder with zeroed pages
460 if (memsz > filesz) {
461 UInt bytes;
463 bytes = VG_PGROUNDUP(brkaddr)-VG_PGROUNDUP(bss);
464 if (bytes > 0) {
465 if (0) VG_(debugLog)(0,"ume","mmap_anon_fixed_client #2\n");
466 res = VG_(am_mmap_anon_fixed_client)(
467 VG_PGROUNDUP(bss), bytes,
468 prot
470 if (0) VG_(am_show_nsegments)(0,"after #2");
471 check_mmap(res, VG_PGROUNDUP(bss), bytes);
474 bytes = bss & (VKI_PAGE_SIZE - 1);
476 // The 'prot' condition allows for a read-only bss
477 if ((prot & VKI_PROT_WRITE) && (bytes > 0)) {
478 bytes = VKI_PAGE_SIZE - bytes;
479 VG_(memset)((void *)bss, 0, bytes);
484 return elfbrk;
487 Bool VG_(match_ELF)(const void *hdr, SizeT len)
489 const ESZ(Ehdr) *e = hdr;
490 return (len > sizeof(*e)) && VG_(memcmp)(&e->e_ident[0], ELFMAG, SELFMAG) == 0;
494 /* load_ELF pulls an ELF executable into the address space, prepares
495 it for execution, and writes info about it into INFO. In
496 particular it fills in .init_eip, which is the starting point.
498 Returns zero on success, non-zero (a VKI_E.. value) on failure.
500 The sequence of activities is roughly as follows:
502 - use readelf() to extract program header info from the exe file.
504 - scan the program header, collecting info (not sure what all those
505 info-> fields are, or whether they are used, but still) and in
506 particular looking out fo the PT_INTERP header, which describes
507 the interpreter. If such a field is found, the space needed to
508 hold the interpreter is computed into interp_size.
510 - map the executable in, by calling mapelf(). This maps in all
511 loadable sections, and I _think_ also creates any .bss areas
512 required. mapelf() returns the address just beyond the end of
513 the furthest-along mapping it creates. The executable is mapped
514 starting at EBASE, which is usually read from it (eg, 0x8048000
515 etc) except if it's a PIE, in which case I'm not sure what
516 happens.
518 The returned address is recorded in info->brkbase as the start
519 point of the brk (data) segment, as it is traditional to place
520 the data segment just after the executable. Neither load_ELF nor
521 mapelf creates the brk segment, though: that is for the caller of
522 load_ELF to attend to.
524 - If the initial phdr scan didn't find any mention of an
525 interpreter (interp == NULL), this must be a statically linked
526 executable, and we're pretty much done.
528 - Otherwise, we need to use mapelf() a second time to load the
529 interpreter. The interpreter can go anywhere, but mapelf() wants
530 to be told a specific address to put it at. So an advisory query
531 is passed to aspacem, asking where it would put an anonymous
532 client mapping of size INTERP_SIZE. That address is then used
533 as the mapping address for the interpreter.
535 - The entry point in INFO is set to the interpreter's entry point,
536 and we're done. */
537 Int VG_(load_ELF)(Int fd, const HChar* name, /*MOD*/ExeInfo* info)
539 SysRes sres;
540 struct elfinfo *e;
541 struct elfinfo *interp = NULL;
542 ESZ(Addr) minaddr = ~0; /* lowest mapped address */
543 ESZ(Addr) maxaddr = 0; /* highest mapped address */
544 ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */
545 ESZ(Word) interp_size = 0; /* interpreter size */
546 /* ESZ(Word) interp_align = VKI_PAGE_SIZE; */ /* UNUSED */
547 Int i;
548 void *entry;
549 ESZ(Addr) ebase = 0;
550 # if defined(VGO_solaris)
551 ESZ(Addr) thrptr_addr = 0;
552 # endif
554 # if defined(VGO_linux)
555 Int retval;
556 # endif
558 # if defined(HAVE_PIE)
559 ebase = info->exe_base;
560 # endif
562 e = readelf(fd, name);
564 if (e == NULL)
565 return VKI_ENOEXEC;
567 /* The kernel maps position-independent executables at TASK_SIZE*2/3;
568 duplicate this behavior as close as we can. */
569 if (e->e.e_type == ET_DYN && ebase == 0) {
570 ebase = VG_PGROUNDDN(info->exe_base
571 + (info->exe_end - info->exe_base) * 2 / 3);
572 /* We really don't want to load PIEs at zero or too close. It
573 works, but it's unrobust (NULL pointer reads and writes
574 become legit, which is really bad) and causes problems for
575 exp-ptrcheck, which assumes all numbers below 1MB are
576 nonpointers. So, hackily, move it above 1MB. */
577 /* Later .. it appears ppc32-linux tries to put [vdso] at 1MB,
578 which totally screws things up, because nothing else can go
579 there. The size of [vdso] is around 2 or 3 pages, so bump
580 the hacky load address along by 8 * VKI_PAGE_SIZE to be safe. */
581 /* Later .. on mips64 we can't use 0x108000, because mapelf will
582 fail. */
583 # if defined(VGP_mips64_linux)
584 if (ebase < 0x100000)
585 ebase = 0x100000;
586 # else
587 vg_assert(VKI_PAGE_SIZE >= 4096); /* stay sane */
588 ESZ(Addr) hacky_load_address = 0x100000 + 8 * VKI_PAGE_SIZE;
589 if (ebase < hacky_load_address)
590 ebase = hacky_load_address;
591 # endif
593 # if defined(VGO_solaris)
594 /* Record for later use in AT_BASE. */
595 info->interp_offset = ebase;
596 # endif
599 info->phnum = e->e.e_phnum;
600 info->entry = e->e.e_entry + ebase;
601 info->phdr = 0;
602 info->stack_prot = VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC;
604 for (i = 0; i < e->e.e_phnum; i++) {
605 ESZ(Phdr) *ph = &e->p[i];
607 switch(ph->p_type) {
608 case PT_PHDR:
609 info->phdr = ph->p_vaddr + ebase;
610 # if defined(VGO_solaris)
611 info->real_phdr_present = True;
612 # endif
613 break;
615 case PT_LOAD:
616 if (ph->p_vaddr < minaddr)
617 minaddr = ph->p_vaddr;
618 if (ph->p_vaddr+ph->p_memsz > maxaddr)
619 maxaddr = ph->p_vaddr+ph->p_memsz;
620 break;
622 # if defined(VGO_solaris)
623 case PT_SUNWDTRACE:
624 if (ph->p_memsz < PT_SUNWDTRACE_SIZE) {
625 VG_(printf)("valgrind: m_ume.c: too small SUNWDTRACE size\n");
626 return VKI_ENOEXEC;
628 if ((ph->p_flags & (PF_R | PF_W | PF_X)) != PT_SUNWDTRACE_PROTECTION) {
629 VG_(printf)("valgrind: m_ume.c: SUNWDTRACE protection mismatch\n");
630 return VKI_ENOEXEC;
633 info->init_thrptr = ph->p_vaddr + ebase;
634 break;
635 # endif
637 case PT_INTERP: {
638 HChar *buf = VG_(malloc)("ume.LE.1", ph->p_filesz+1);
639 Int j;
640 Int intfd;
641 Int baseaddr_set;
643 VG_(pread)(fd, buf, ph->p_filesz, ph->p_offset);
644 buf[ph->p_filesz] = '\0';
646 #if defined(VGP_x86_freebsd)
647 sres._isError = True;
648 /* Hack. FreeBSD's kernel overloads the interpreter name. */
649 if (VG_(strcmp)(buf, "/libexec/ld-elf.so.1") == 0 ||
650 VG_(strcmp)(buf, "/usr/libexec/ld-elf.so.1") == 0) {
651 sres = VG_(open)("/libexec/ld-elf32.so.1", VKI_O_RDONLY, 0);
653 if (sr_isError(sres))
654 #endif
655 sres = VG_(open)(buf, VKI_O_RDONLY, 0);
656 //sres = VG_(open)("/usr/home/paulf/build/src/obj/usr/home/paulf/build/src/amd64.amd64/libexec/rtld-elf/ld-elf.so.1.full", VKI_O_RDONLY, 0);
657 if (sr_isError(sres)) {
658 VG_(printf)("valgrind: m_ume.c: can't open interpreter\n");
659 VG_(exit)(1);
661 intfd = sr_Res(sres);
663 interp = readelf(intfd, buf);
664 if (interp == NULL) {
665 VG_(printf)("valgrind: m_ume.c: can't read interpreter\n");
666 return 1;
668 VG_(free)(buf);
670 baseaddr_set = 0;
671 for (j = 0; j < interp->e.e_phnum; j++) {
672 ESZ(Phdr) *iph = &interp->p[j];
673 ESZ(Addr) end;
675 # if defined(VGO_solaris)
676 if (iph->p_type == PT_SUNWDTRACE) {
677 if (iph->p_memsz < PT_SUNWDTRACE_SIZE) {
678 VG_(printf)("valgrind: m_ume.c: too small SUNWDTRACE size\n");
679 return VKI_ENOEXEC;
681 if ((iph->p_flags & (PF_R | PF_W | PF_X))
682 != PT_SUNWDTRACE_PROTECTION) {
683 VG_(printf)("valgrind: m_ume.c: SUNWDTRACE protection "
684 "mismatch\n");
685 return VKI_ENOEXEC;
688 /* Store the thrptr value into a temporary because we do not
689 know yet where the interpreter is mapped. */
690 thrptr_addr = iph->p_vaddr;
692 # endif
694 # if defined(VGO_linux)
695 if ((iph->p_type >= PT_LOPROC) && (iph->p_type <= PT_HIPROC)) {
696 retval = arch_elf_pt_proc(&interp->e, iph, intfd, True,
697 info->arch_elf_state);
698 if (retval)
699 return retval;
701 # endif
703 if (iph->p_type != PT_LOAD || iph->p_memsz == 0)
704 continue;
706 if (!baseaddr_set) {
707 interp_addr = iph->p_vaddr;
708 /* interp_align = iph->p_align; */ /* UNUSED */
709 baseaddr_set = 1;
712 /* assumes that all segments in the interp are close */
713 end = (iph->p_vaddr - interp_addr) + iph->p_memsz;
715 if (end > interp_size)
716 interp_size = end;
718 break;
721 # if defined(PT_GNU_STACK) || defined(PT_SUNWSTACK)
722 # if defined(PT_GNU_STACK)
723 /* Android's elf.h doesn't appear to have PT_GNU_STACK. */
724 case PT_GNU_STACK:
725 # endif
726 # if defined(PT_SUNWSTACK)
727 /* Solaris-specific program header. */
728 case PT_SUNWSTACK:
729 # endif
730 if ((ph->p_flags & PF_X) == 0) info->stack_prot &= ~VKI_PROT_EXEC;
731 if ((ph->p_flags & PF_W) == 0) info->stack_prot &= ~VKI_PROT_WRITE;
732 if ((ph->p_flags & PF_R) == 0) info->stack_prot &= ~VKI_PROT_READ;
733 break;
734 # endif
736 # if defined(PT_SUNW_SYSSTAT)
737 /* Solaris-specific program header which requires link-time support. */
738 case PT_SUNW_SYSSTAT:
739 VG_(unimplemented)("Support for program header PT_SUNW_SYSSTAT.");
740 break;
741 # endif
742 # if defined(PT_SUNW_SYSSTAT_ZONE)
743 /* Solaris-specific program header which requires link-time support. */
744 case PT_SUNW_SYSSTAT_ZONE:
745 VG_(unimplemented)("Support for program header PT_SUNW_SYSSTAT_ZONE.");
746 break;
747 # endif
749 # if defined(VGO_linux)
750 case PT_LOPROC ... PT_HIPROC:
751 retval = arch_elf_pt_proc(&e->e, ph, fd, False, info->arch_elf_state);
752 if (retval)
753 return retval;
754 break;
755 # endif
757 default:
758 // do nothing
759 break;
763 # if defined(VGO_linux)
764 retval = arch_check_elf(&e->e,
765 interp != NULL,
766 info->arch_elf_state);
767 if (retval)
768 return retval;
769 # endif
771 if (info->phdr == 0)
772 info->phdr = minaddr + ebase + e->e.e_phoff;
774 if (info->exe_base != info->exe_end) {
775 if (minaddr >= maxaddr ||
776 (minaddr + ebase < info->exe_base ||
777 maxaddr + ebase > info->exe_end)) {
778 VG_(printf)("Executable range %p-%p is outside the\n"
779 "acceptable range %p-%p\n",
780 (char *)minaddr + ebase, (char *)maxaddr + ebase,
781 (char *)info->exe_base, (char *)info->exe_end);
782 return VKI_ENOMEM;
786 info->brkbase = mapelf(e, ebase); /* map the executable */
788 if (info->brkbase == 0)
789 return VKI_ENOMEM;
791 if (interp != NULL) {
792 /* reserve a chunk of address space for interpreter */
793 MapRequest mreq;
794 Addr advised;
795 Bool ok;
797 /* Don't actually reserve the space. Just get an advisory
798 indicating where it would be allocated, and pass that to
799 mapelf(), which in turn asks aspacem to do some fixed maps at
800 the specified address. This is a bit of hack, but it should
801 work because there should be no intervening transactions with
802 aspacem which could cause those fixed maps to fail.
804 Placement policy is:
806 if the interpreter asks to be loaded at zero
807 ignore that and put it wherever we like (mappings at zero
808 are bad news)
809 else
810 try and put it where it asks for, but if that doesn't work,
811 just put it anywhere.
813 if (interp_addr == 0) {
814 mreq.rkind = MAny;
815 mreq.start = 0;
816 mreq.len = interp_size;
817 } else {
818 mreq.rkind = MHint;
819 mreq.start = interp_addr;
820 mreq.len = interp_size;
823 advised = VG_(am_get_advisory)( &mreq, True/*client*/, &ok );
825 if (!ok) {
826 /* bomb out */
827 SysRes res = VG_(mk_SysRes_Error)(VKI_EINVAL);
828 if (0) VG_(printf)("reserve for interp: failed\n");
829 check_mmap(res, (Addr)interp_addr, interp_size);
830 /*NOTREACHED*/
833 (void)mapelf(interp, (ESZ(Addr))advised - interp_addr);
835 VG_(close)(interp->fd);
837 entry = (void *)(advised - interp_addr + interp->e.e_entry);
839 info->interp_offset = advised - interp_addr;
840 # if defined(VGO_solaris)
841 if (thrptr_addr)
842 info->init_thrptr = thrptr_addr + info->interp_offset;
843 # endif
845 VG_(free)(interp->p);
846 VG_(free)(interp);
847 } else {
848 entry = (void *)(ebase + e->e.e_entry);
850 # if defined(VGO_solaris)
851 if (e->e.e_type == ET_DYN)
852 info->ldsoexec = True;
853 # endif
856 info->exe_base = minaddr + ebase;
857 info->exe_end = maxaddr + ebase;
859 #if defined(VGP_ppc64be_linux)
860 /* On PPC64BE, ELF ver 1, a func ptr is represented by a TOC entry ptr.
861 This TOC entry contains three words; the first word is the function
862 address, the second word is the TOC ptr (r2), and the third word
863 is the static chain value. */
864 info->init_ip = ((ULong*)entry)[0];
865 info->init_toc = ((ULong*)entry)[1];
866 info->init_ip += info->interp_offset;
867 info->init_toc += info->interp_offset;
868 #elif defined(VGP_ppc64le_linux)
869 /* On PPC64LE, ELF ver 2. API doesn't use a func ptr */
870 info->init_ip = (Addr)entry;
871 info->init_toc = 0; /* meaningless on this platform */
872 #else
873 info->init_ip = (Addr)entry;
874 info->init_toc = 0; /* meaningless on this platform */
875 #endif
876 VG_(free)(e->p);
877 VG_(free)(e);
879 return 0;
882 #endif // defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
884 /*--------------------------------------------------------------------*/
885 /*--- end ---*/
886 /*--------------------------------------------------------------------*/