blob: 1b8c67052cfeb25c10c37746e8f3e5cf9b05c027 [file] [log] [blame]
jseward2886b0e2004-01-04 03:46:11 +00001
nethercotebb1c9912004-01-04 16:43:23 +00002/*--------------------------------------------------------------------*/
nethercote107e1c02004-10-13 17:55:31 +00003/*--- User-mode execve(), and other stuff shared between stage1 ---*/
njn08a2e172005-06-21 22:47:54 +00004/*--- and stage2. m_ume.c ---*/
nethercotebb1c9912004-01-04 16:43:23 +00005/*--------------------------------------------------------------------*/
6
jseward2886b0e2004-01-04 03:46:11 +00007/*
njnb9c427c2004-12-01 14:14:42 +00008 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
jseward2886b0e2004-01-04 03:46:11 +000010
njn53612422005-03-12 16:22:54 +000011 Copyright (C) 2000-2005 Julian Seward
jseward2886b0e2004-01-04 03:46:11 +000012 jseward@acm.org
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
29 The GNU General Public License is contained in the file COPYING.
30*/
31
fitzhardinge7e343cd2003-12-16 02:14:00 +000032
33#define _GNU_SOURCE
34#define _FILE_OFFSET_BITS 64
35
njnf844cb62005-06-19 16:07:49 +000036// It seems that on SuSE 9.1 (x86) something in <fcntl.h> messes up stuff
37// acquired indirectly from vki-x86-linux.h. Therefore our headers must be
38// included ahead of the glibc ones. This fix is a kludge; the right
39// solution is to entirely remove the glibc dependency.
sewardj4b0c0712005-06-19 15:58:33 +000040#include "pub_core_basics.h"
njn73750612005-10-14 03:11:30 +000041#include "pub_core_aspacemgr.h" // various mapping fns
sewardj45f4e7c2005-09-27 19:20:21 +000042#include "pub_core_debuglog.h"
sewardj4b0c0712005-06-19 15:58:33 +000043#include "pub_core_libcbase.h"
44#include "pub_core_machine.h"
sewardj45f4e7c2005-09-27 19:20:21 +000045#include "pub_core_libcprint.h"
46#include "pub_core_libcfile.h" // VG_(close) et al
47#include "pub_core_libcproc.h" // VG_(geteuid), VG_(getegid)
48#include "pub_core_libcassert.h" // VG_(exit), vg_assert
sewardj45f4e7c2005-09-27 19:20:21 +000049#include "pub_core_mallocfree.h" // VG_(malloc), VG_(free)
njn73750612005-10-14 03:11:30 +000050#include "pub_core_syscall.h" // VG_(strerror)
sewardj45f4e7c2005-09-27 19:20:21 +000051#include "vki_unistd.h" // mmap-related constants
52
njn08a2e172005-06-21 22:47:54 +000053#include "pub_core_ume.h"
sewardj4b0c0712005-06-19 15:58:33 +000054
fitzhardinge7e343cd2003-12-16 02:14:00 +000055
sewardjfbd78b22005-06-19 16:14:06 +000056#if VG_WORDSIZE == 8
nethercote3f458152004-11-01 18:42:23 +000057#define ESZ(x) Elf64_##x
sewardjfbd78b22005-06-19 16:14:06 +000058#elif VG_WORDSIZE == 4
nethercote3f458152004-11-01 18:42:23 +000059#define ESZ(x) Elf32_##x
60#else
sewardjfbd78b22005-06-19 16:14:06 +000061#error VG_WORDSIZE needs to ==4 or ==8
nethercote3f458152004-11-01 18:42:23 +000062#endif
63
nethercote1fe54502004-07-26 15:28:33 +000064struct elfinfo
65{
66 ESZ(Ehdr) e;
67 ESZ(Phdr) *p;
njn73750612005-10-14 03:11:30 +000068 Int fd;
nethercote1fe54502004-07-26 15:28:33 +000069};
70
sewardj45f4e7c2005-09-27 19:20:21 +000071static void check_mmap(SysRes res, Addr base, SizeT len)
nethercotebfed1c82004-07-17 12:57:44 +000072{
sewardj45f4e7c2005-09-27 19:20:21 +000073 if (res.isError) {
74 VG_(printf)("valgrind: mmap(0x%llx, %lld) failed in UME.\n",
75 (ULong)base, (Long)len);
76 VG_(exit)(1);
nethercotebfed1c82004-07-17 12:57:44 +000077 }
78}
79
njnfcb7c3e2005-06-18 15:54:25 +000080/*------------------------------------------------------------*/
nethercote31779c72004-07-30 21:50:15 +000081/*--- Finding auxv on the stack ---*/
82/*------------------------------------------------------------*/
fitzhardinge7e343cd2003-12-16 02:14:00 +000083
njn62ff0f22005-06-21 23:03:36 +000084struct ume_auxv *VG_(find_auxv)(UWord* sp)
fitzhardinge7e343cd2003-12-16 02:14:00 +000085{
nethercoteebf1d862004-11-01 18:22:05 +000086 sp++; // skip argc (Nb: is word-sized, not int-sized!)
fitzhardinge7e343cd2003-12-16 02:14:00 +000087
nethercoteebf1d862004-11-01 18:22:05 +000088 while (*sp != 0) // skip argv
89 sp++;
90 sp++;
fitzhardinge7e343cd2003-12-16 02:14:00 +000091
nethercoteebf1d862004-11-01 18:22:05 +000092 while (*sp != 0) // skip env
93 sp++;
94 sp++;
fitzhardinge7e343cd2003-12-16 02:14:00 +000095
sewardj13247ca2005-12-30 22:52:20 +000096#if defined(VGA_ppc32) || defined(VGA_ppc64)
cerion85665ca2005-06-20 15:51:07 +000097# if defined AT_IGNOREPPC
98 while (*sp == AT_IGNOREPPC) // skip AT_IGNOREPPC entries
99 sp += 2;
100# endif
101#endif
102
nethercoteebf1d862004-11-01 18:22:05 +0000103 return (struct ume_auxv *)sp;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000104}
105
nethercote31779c72004-07-30 21:50:15 +0000106/*------------------------------------------------------------*/
107/*--- Loading ELF files ---*/
108/*------------------------------------------------------------*/
fitzhardinge7e343cd2003-12-16 02:14:00 +0000109
sewardj2c5ffbe2005-03-12 13:32:06 +0000110static
njn73750612005-10-14 03:11:30 +0000111struct elfinfo *readelf(Int fd, const char *filename)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000112{
sewardj45f4e7c2005-09-27 19:20:21 +0000113 SysRes sres;
114 struct elfinfo *e = VG_(malloc)(sizeof(*e));
njn73750612005-10-14 03:11:30 +0000115 Int phsz;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000116
sewardj45f4e7c2005-09-27 19:20:21 +0000117 vg_assert(e);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000118 e->fd = fd;
119
sewardj45f4e7c2005-09-27 19:20:21 +0000120 sres = VG_(pread)(fd, &e->e, sizeof(e->e), 0);
121 if (sres.isError || sres.val != sizeof(e->e)) {
122 VG_(printf)("valgrind: %s: can't read ELF header: %s\n",
123 filename, VG_(strerror)(sres.val));
njn12f266f2005-06-28 19:20:46 +0000124 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000125 }
126
sewardj45f4e7c2005-09-27 19:20:21 +0000127 if (VG_(memcmp)(&e->e.e_ident[0], ELFMAG, SELFMAG) != 0) {
128 VG_(printf)("valgrind: %s: bad ELF magic number\n", filename);
njn12f266f2005-06-28 19:20:46 +0000129 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000130 }
njnaf839f52005-06-23 03:27:57 +0000131 if (e->e.e_ident[EI_CLASS] != VG_ELF_CLASS) {
sewardj45f4e7c2005-09-27 19:20:21 +0000132 VG_(printf)("valgrind: wrong ELF executable class "
133 "(eg. 32-bit instead of 64-bit)\n");
njn12f266f2005-06-28 19:20:46 +0000134 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000135 }
sewardj6e340c72005-07-10 00:53:42 +0000136 if (e->e.e_ident[EI_DATA] != VG_ELF_DATA2XXX) {
sewardj45f4e7c2005-09-27 19:20:21 +0000137 VG_(printf)("valgrind: executable has wrong endian-ness\n");
njn12f266f2005-06-28 19:20:46 +0000138 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000139 }
140 if (!(e->e.e_type == ET_EXEC || e->e.e_type == ET_DYN)) {
sewardj45f4e7c2005-09-27 19:20:21 +0000141 VG_(printf)("valgrind: this is not an executable\n");
njn12f266f2005-06-28 19:20:46 +0000142 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000143 }
144
njnaf839f52005-06-23 03:27:57 +0000145 if (e->e.e_machine != VG_ELF_MACHINE) {
sewardj45f4e7c2005-09-27 19:20:21 +0000146 VG_(printf)("valgrind: executable is not for "
147 "this architecture\n");
njn12f266f2005-06-28 19:20:46 +0000148 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000149 }
150
151 if (e->e.e_phentsize != sizeof(ESZ(Phdr))) {
sewardj45f4e7c2005-09-27 19:20:21 +0000152 VG_(printf)("valgrind: sizeof ELF Phdr wrong\n");
njn12f266f2005-06-28 19:20:46 +0000153 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000154 }
155
156 phsz = sizeof(ESZ(Phdr)) * e->e.e_phnum;
sewardj45f4e7c2005-09-27 19:20:21 +0000157 e->p = VG_(malloc)(phsz);
158 vg_assert(e->p);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000159
sewardj45f4e7c2005-09-27 19:20:21 +0000160 sres = VG_(pread)(fd, e->p, phsz, e->e.e_phoff);
161 if (sres.isError || sres.val != phsz) {
162 VG_(printf)("valgrind: can't read phdr: %s\n",
163 VG_(strerror)(sres.val));
164 VG_(free)(e->p);
njn12f266f2005-06-28 19:20:46 +0000165 goto bad;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000166 }
167
168 return e;
njn12f266f2005-06-28 19:20:46 +0000169
170 bad:
sewardj45f4e7c2005-09-27 19:20:21 +0000171 VG_(free)(e);
njn12f266f2005-06-28 19:20:46 +0000172 return NULL;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000173}
174
fitzhardinge7e343cd2003-12-16 02:14:00 +0000175/* Map an ELF file. Returns the brk address. */
sewardj2c5ffbe2005-03-12 13:32:06 +0000176static
fitzhardingeb50068f2004-02-24 23:42:55 +0000177ESZ(Addr) mapelf(struct elfinfo *e, ESZ(Addr) base)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000178{
sewardj45f4e7c2005-09-27 19:20:21 +0000179 Int i;
180 SysRes res;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000181 ESZ(Addr) elfbrk = 0;
182
183 for(i = 0; i < e->e.e_phnum; i++) {
184 ESZ(Phdr) *ph = &e->p[i];
185 ESZ(Addr) addr, brkaddr;
186 ESZ(Word) memsz;
187
188 if (ph->p_type != PT_LOAD)
189 continue;
190
nethercote6c3cf412004-10-26 13:32:11 +0000191 addr = ph->p_vaddr+base;
192 memsz = ph->p_memsz;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000193 brkaddr = addr+memsz;
194
195 if (brkaddr > elfbrk)
196 elfbrk = brkaddr;
197 }
198
fitzhardinge7e343cd2003-12-16 02:14:00 +0000199 for(i = 0; i < e->e.e_phnum; i++) {
200 ESZ(Phdr) *ph = &e->p[i];
201 ESZ(Addr) addr, bss, brkaddr;
202 ESZ(Off) off;
203 ESZ(Word) filesz;
204 ESZ(Word) memsz;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000205 unsigned prot = 0;
206
207 if (ph->p_type != PT_LOAD)
208 continue;
209
sewardj45f4e7c2005-09-27 19:20:21 +0000210 if (ph->p_flags & PF_X) prot |= VKI_PROT_EXEC;
211 if (ph->p_flags & PF_W) prot |= VKI_PROT_WRITE;
212 if (ph->p_flags & PF_R) prot |= VKI_PROT_READ;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000213
nethercote6c3cf412004-10-26 13:32:11 +0000214 addr = ph->p_vaddr+base;
215 off = ph->p_offset;
216 filesz = ph->p_filesz;
217 bss = addr+filesz;
218 memsz = ph->p_memsz;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000219 brkaddr = addr+memsz;
220
njnf7fbe6c2004-11-30 11:40:24 +0000221 // Tom says: In the following, do what the Linux kernel does and only
222 // map the pages that are required instead of rounding everything to
223 // the specified alignment (ph->p_align). (AMD64 doesn't work if you
224 // use ph->p_align -- part of stage2's memory gets trashed somehow.)
njn098da062005-03-26 16:22:43 +0000225 //
226 // The condition handles the case of a zero-length segment.
njn13bfd852005-06-02 03:52:53 +0000227 if (VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr) > 0) {
sewardj45f4e7c2005-09-27 19:20:21 +0000228 if (0) VG_(debugLog)(0,"ume","mmap_file_fixed_client #1\n");
229 res = VG_(am_mmap_file_fixed_client)(
230 VG_PGROUNDDN(addr),
231 VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr),
232 prot, /*VKI_MAP_FIXED|VKI_MAP_PRIVATE, */
233 e->fd, VG_PGROUNDDN(off)
234 );
235 if (0) VG_(am_show_nsegments)(0,"after #1");
236 check_mmap(res, VG_PGROUNDDN(addr),
237 VG_PGROUNDUP(bss)-VG_PGROUNDDN(addr));
njn098da062005-03-26 16:22:43 +0000238 }
njnf7fbe6c2004-11-30 11:40:24 +0000239
240 // if memsz > filesz, fill the remainder with zeroed pages
fitzhardinge7e343cd2003-12-16 02:14:00 +0000241 if (memsz > filesz) {
242 UInt bytes;
243
njn13bfd852005-06-02 03:52:53 +0000244 bytes = VG_PGROUNDUP(brkaddr)-VG_PGROUNDUP(bss);
nethercotebfed1c82004-07-17 12:57:44 +0000245 if (bytes > 0) {
sewardj45f4e7c2005-09-27 19:20:21 +0000246 if (0) VG_(debugLog)(0,"ume","mmap_anon_fixed_client #2\n");
247 res = VG_(am_mmap_anon_fixed_client)(
248 VG_PGROUNDUP(bss), bytes,
249 prot
250 );
251 if (0) VG_(am_show_nsegments)(0,"after #2");
252 check_mmap(res, VG_PGROUNDUP(bss), bytes);
nethercotebfed1c82004-07-17 12:57:44 +0000253 }
fitzhardinge7e343cd2003-12-16 02:14:00 +0000254
nethercote73b526f2004-10-31 18:48:21 +0000255 bytes = bss & (VKI_PAGE_SIZE - 1);
njn098da062005-03-26 16:22:43 +0000256
257 // The 'prot' condition allows for a read-only bss
sewardj45f4e7c2005-09-27 19:20:21 +0000258 if ((prot & VKI_PROT_WRITE) && (bytes > 0)) {
nethercote73b526f2004-10-31 18:48:21 +0000259 bytes = VKI_PAGE_SIZE - bytes;
sewardj45f4e7c2005-09-27 19:20:21 +0000260 VG_(memset)((char *)bss, 0, bytes);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000261 }
262 }
263 }
264
265 return elfbrk;
266}
267
njn73750612005-10-14 03:11:30 +0000268static Bool match_ELF(const char *hdr, Int len)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000269{
270 ESZ(Ehdr) *e = (ESZ(Ehdr) *)hdr;
sewardj45f4e7c2005-09-27 19:20:21 +0000271 return (len > sizeof(*e)) && VG_(memcmp)(&e->e_ident[0], ELFMAG, SELFMAG) == 0;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000272}
273
sewardj45f4e7c2005-09-27 19:20:21 +0000274
275/* load_ELF pulls an ELF executable into the address space, prepares
276 it for execution, and writes info about it into INFO. In
277 particular it fills in .init_eip, which is the starting point.
278
279 Returns zero on success, non-zero (a VKI_E.. value) on failure.
280
281 The sequence of activities is roughly as follows:
282
283 - use readelf() to extract program header info from the exe file.
284
285 - scan the program header, collecting info (not sure what all those
286 info-> fields are, or whether they are used, but still) and in
287 particular looking out fo the PT_INTERP header, which describes
288 the interpreter. If such a field is found, the space needed to
289 hold the interpreter is computed into interp_size.
290
291 - map the executable in, by calling mapelf(). This maps in all
292 loadable sections, and I _think_ also creates any .bss areas
293 required. mapelf() returns the address just beyond the end of
294 the furthest-along mapping it creates. The executable is mapped
295 starting at EBASE, which is usually read from it (eg, 0x8048000
296 etc) except if it's a PIE, in which case I'm not sure what
297 happens.
298
299 The returned address is recorded in info->brkbase as the start
300 point of the brk (data) segment, as it is traditional to place
301 the data segment just after the executable. Neither load_ELF nor
302 mapelf creates the brk segment, though: that is for the caller of
303 load_ELF to attend to.
304
305 - If the initial phdr scan didn't find any mention of an
306 interpreter (interp == NULL), this must be a statically linked
307 executable, and we're pretty much done.
308
309 - Otherwise, we need to use mapelf() a second time to load the
310 interpreter. The interpreter can go anywhere, but mapelf() wants
311 to be told a specific address to put it at. So an advisory query
312 is passed to aspacem, asking where it would put an anonymous
313 client mapping of size INTERP_SIZE. That address is then used
314 as the mapping address for the interpreter.
315
316 - The entry point in INFO is set to the interpreter's entry point,
317 and we're done. */
sewardj13247ca2005-12-30 22:52:20 +0000318static Int load_ELF(Int fd, const HChar* name, /*MOD*/ExeInfo* info)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000319{
sewardj45f4e7c2005-09-27 19:20:21 +0000320 SysRes sres;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000321 struct elfinfo *e;
322 struct elfinfo *interp = NULL;
fitzhardingeca9bd9c2004-09-08 20:05:02 +0000323 ESZ(Addr) minaddr = ~0; /* lowest mapped address */
324 ESZ(Addr) maxaddr = 0; /* highest mapped address */
325 ESZ(Addr) interp_addr = 0; /* interpreter (ld.so) address */
326 ESZ(Word) interp_size = 0; /* interpreter size */
nethercote73b526f2004-10-31 18:48:21 +0000327 ESZ(Word) interp_align = VKI_PAGE_SIZE;
njn73750612005-10-14 03:11:30 +0000328 Int i;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000329 void *entry;
nethercote7f390022004-10-25 17:18:24 +0000330 ESZ(Addr) ebase = 0;
331
332#ifdef HAVE_PIE
333 ebase = info->exe_base;
334#endif
fitzhardinge7e343cd2003-12-16 02:14:00 +0000335
336 e = readelf(fd, name);
337
338 if (e == NULL)
sewardj45f4e7c2005-09-27 19:20:21 +0000339 return VKI_ENOEXEC;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000340
tomd6dd9912005-07-18 15:52:30 +0000341 /* The kernel maps position-independent executables at TASK_SIZE*2/3;
342 duplicate this behavior as close as we can. */
343 if (e->e.e_type == ET_DYN && ebase == 0) {
344 ebase = VG_PGROUNDDN(info->exe_base + (info->exe_end - info->exe_base) * 2 / 3);
345 }
346
fitzhardinge7e343cd2003-12-16 02:14:00 +0000347 info->phnum = e->e.e_phnum;
nethercote7f390022004-10-25 17:18:24 +0000348 info->entry = e->e.e_entry + ebase;
sewardjb5f6f512005-03-10 23:59:00 +0000349 info->phdr = 0;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000350
351 for(i = 0; i < e->e.e_phnum; i++) {
352 ESZ(Phdr) *ph = &e->p[i];
353
354 switch(ph->p_type) {
355 case PT_PHDR:
nethercote7f390022004-10-25 17:18:24 +0000356 info->phdr = ph->p_vaddr + ebase;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000357 break;
358
359 case PT_LOAD:
360 if (ph->p_vaddr < minaddr)
361 minaddr = ph->p_vaddr;
362 if (ph->p_vaddr+ph->p_memsz > maxaddr)
363 maxaddr = ph->p_vaddr+ph->p_memsz;
364 break;
365
366 case PT_INTERP: {
sewardj45f4e7c2005-09-27 19:20:21 +0000367 char *buf = VG_(malloc)(ph->p_filesz+1);
njn73750612005-10-14 03:11:30 +0000368 Int j;
369 Int intfd;
370 Int baseaddr_set;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000371
sewardj45f4e7c2005-09-27 19:20:21 +0000372 vg_assert(buf);
373 VG_(pread)(fd, buf, ph->p_filesz, ph->p_offset);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000374 buf[ph->p_filesz] = '\0';
375
sewardj45f4e7c2005-09-27 19:20:21 +0000376 sres = VG_(open)(buf, VKI_O_RDONLY, 0);
377 if (sres.isError) {
378 VG_(printf)("valgrind: m_ume.c: can't open interpreter\n");
379 VG_(exit)(1);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000380 }
sewardj45f4e7c2005-09-27 19:20:21 +0000381 intfd = sres.val;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000382
383 interp = readelf(intfd, buf);
384 if (interp == NULL) {
sewardj45f4e7c2005-09-27 19:20:21 +0000385 VG_(printf)("valgrind: m_ume.c: can't read interpreter\n");
fitzhardinge7e343cd2003-12-16 02:14:00 +0000386 return 1;
387 }
sewardj45f4e7c2005-09-27 19:20:21 +0000388 VG_(free)(buf);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000389
390 baseaddr_set = 0;
391 for(j = 0; j < interp->e.e_phnum; j++) {
392 ESZ(Phdr) *iph = &interp->p[j];
393 ESZ(Addr) end;
394
395 if (iph->p_type != PT_LOAD)
396 continue;
397
398 if (!baseaddr_set) {
nethercote7f390022004-10-25 17:18:24 +0000399 interp_addr = iph->p_vaddr;
400 interp_align = iph->p_align;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000401 baseaddr_set = 1;
402 }
403
404 /* assumes that all segments in the interp are close */
405 end = (iph->p_vaddr - interp_addr) + iph->p_memsz;
406
407 if (end > interp_size)
408 interp_size = end;
409 }
410 break;
nethercoteb24cbc82004-09-03 23:25:33 +0000411
412 default:
413 // do nothing
414 break;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000415 }
416 }
417 }
418
sewardjb5f6f512005-03-10 23:59:00 +0000419 if (info->phdr == 0)
tomd6dd9912005-07-18 15:52:30 +0000420 info->phdr = minaddr + ebase + e->e.e_phoff;
sewardjb5f6f512005-03-10 23:59:00 +0000421
fitzhardinge7e343cd2003-12-16 02:14:00 +0000422 if (info->exe_base != info->exe_end) {
423 if (minaddr >= maxaddr ||
nethercote7f390022004-10-25 17:18:24 +0000424 (minaddr + ebase < info->exe_base ||
425 maxaddr + ebase > info->exe_end)) {
sewardj45f4e7c2005-09-27 19:20:21 +0000426 VG_(printf)("Executable range %p-%p is outside the\n"
427 "acceptable range %p-%p\n",
tom151a6392005-11-11 12:30:36 +0000428 (char *)minaddr + ebase, (char *)maxaddr + ebase,
429 (char *)info->exe_base, (char *)info->exe_end);
sewardj45f4e7c2005-09-27 19:20:21 +0000430 return VKI_ENOMEM;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000431 }
432 }
433
nethercote7f390022004-10-25 17:18:24 +0000434 info->brkbase = mapelf(e, ebase); /* map the executable */
fitzhardinge7e343cd2003-12-16 02:14:00 +0000435
fitzhardinge92360792003-12-24 10:11:11 +0000436 if (info->brkbase == 0)
sewardj45f4e7c2005-09-27 19:20:21 +0000437 return VKI_ENOMEM;
fitzhardinge92360792003-12-24 10:11:11 +0000438
fitzhardinge7e343cd2003-12-16 02:14:00 +0000439 if (interp != NULL) {
440 /* reserve a chunk of address space for interpreter */
tom069e6412005-11-01 19:46:07 +0000441 MapRequest mreq;
sewardj45f4e7c2005-09-27 19:20:21 +0000442 Addr advised;
443 Bool ok;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000444
sewardj45f4e7c2005-09-27 19:20:21 +0000445 /* Don't actually reserve the space. Just get an advisory
446 indicating where it would be allocated, and pass that to
447 mapelf(), which in turn asks aspacem to do some fixed maps at
448 the specified address. This is a bit of hack, but it should
449 work because there should be no intervening transactions with
sewardj09f61732005-11-02 19:41:21 +0000450 aspacem which could cause those fixed maps to fail.
451
452 Placement policy is:
453
454 if the interpreter asks to be loaded at zero
455 ignore that and put it wherever we like (mappings at zero
456 are bad news)
457 else
458 try and put it where it asks for, but if that doesn't work,
459 just put it anywhere.
460 */
461 if (interp_addr == 0) {
462 mreq.rkind = MAny;
463 mreq.start = 0;
464 mreq.len = interp_size;
465 } else {
466 mreq.rkind = MHint;
467 mreq.start = interp_addr;
468 mreq.len = interp_size;
469 }
470
tom069e6412005-11-01 19:46:07 +0000471 advised = VG_(am_get_advisory)( &mreq, True/*client*/, &ok );
sewardj09f61732005-11-02 19:41:21 +0000472
sewardj45f4e7c2005-09-27 19:20:21 +0000473 if (!ok) {
474 /* bomb out */
475 SysRes res = VG_(mk_SysRes_Error)(VKI_EINVAL);
476 if (0) VG_(printf)("reserve for interp: failed\n");
477 check_mmap(res, (Addr)interp_addr, interp_size);
478 /*NOTREACHED*/
fitzhardinge7e343cd2003-12-16 02:14:00 +0000479 }
480
sewardj45f4e7c2005-09-27 19:20:21 +0000481 (void)mapelf(interp, (ESZ(Addr))advised - interp_addr);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000482
sewardj45f4e7c2005-09-27 19:20:21 +0000483 VG_(close)(interp->fd);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000484
sewardj45f4e7c2005-09-27 19:20:21 +0000485 entry = (void *)(advised - interp_addr + interp->e.e_entry);
486 info->interp_base = (ESZ(Addr))advised;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000487
sewardj45f4e7c2005-09-27 19:20:21 +0000488 VG_(free)(interp->p);
489 VG_(free)(interp);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000490 } else
tomd6dd9912005-07-18 15:52:30 +0000491 entry = (void *)(ebase + e->e.e_entry);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000492
nethercote7f390022004-10-25 17:18:24 +0000493 info->exe_base = minaddr + ebase;
494 info->exe_end = maxaddr + ebase;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000495
sewardjbd8951e2005-12-06 21:47:38 +0000496#if defined(VGP_ppc64_linux)
497 /* On PPC64, a func ptr is represented by a TOC entry ptr. This
498 TOC entry contains three words; the first word is the function
499 address, the second word is the TOC ptr (r2), and the third word
500 is the static chain value. */
sewardj13247ca2005-12-30 22:52:20 +0000501 info->init_ip = ((ULong*)entry)[0];
502 info->init_toc = ((ULong*)entry)[1];
sewardjbd8951e2005-12-06 21:47:38 +0000503#else
sewardj13247ca2005-12-30 22:52:20 +0000504 info->init_ip = (Addr)entry;
505 info->init_toc = 0; /* meaningless on this platform */
sewardjbd8951e2005-12-06 21:47:38 +0000506#endif
sewardj45f4e7c2005-09-27 19:20:21 +0000507 VG_(free)(e->p);
508 VG_(free)(e);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000509
510 return 0;
511}
512
513
njn73750612005-10-14 03:11:30 +0000514static Bool match_script(char *hdr, Int len)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000515{
njn73750612005-10-14 03:11:30 +0000516 Char* end = hdr + len;
517 Char* interp = hdr + 2;
518
519 // len < 4: need '#', '!', plus at least a '/' and one more char
520 if (len < 4) return False;
521 if (0 != VG_(memcmp)(hdr, "#!", 2)) return False;
522
523 // Find interpreter name, make sure it's an absolute path (starts with
524 // '/') and has at least one more char.
525 while (interp < end && VG_(isspace)(*interp)) interp++;
526 if (*interp != '/') return False; // absolute path only for interpreter
527 if (interp == end) return False; // nothing after the '/'
528
529 // Here we should get the full interpreter name and check it with
530 // check_executable(). See the "EXEC FAILED" failure when running shell
531 // for an example.
532
533 return True; // looks like a #! script
fitzhardinge7e343cd2003-12-16 02:14:00 +0000534}
535
njn73750612005-10-14 03:11:30 +0000536// Forward declaration.
sewardj13247ca2005-12-30 22:52:20 +0000537static Int do_exec_inner(const HChar* exe, ExeInfo* info);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000538
njn73750612005-10-14 03:11:30 +0000539/* returns: 0 = success, non-0 is failure */
sewardj13247ca2005-12-30 22:52:20 +0000540static Int load_script(Int fd, const HChar* name, ExeInfo* info)
njn73750612005-10-14 03:11:30 +0000541{
542 Char hdr[VKI_PAGE_SIZE];
543 Int len = VKI_PAGE_SIZE;
544 Int eol;
545 Char* interp;
546 Char* end;
547 Char* cp;
548 Char* arg = NULL;
549 SysRes res;
550
551 // Read the first part of the file.
552 res = VG_(pread)(fd, hdr, len, 0);
553 if (res.isError) {
554 VG_(close)(fd);
555 return VKI_EACCES;
556 } else {
557 len = res.val;
558 }
559
560 vg_assert('#' == hdr[0] && '!' == hdr[1]);
561
562 end = hdr + len;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000563 interp = hdr + 2;
njn73750612005-10-14 03:11:30 +0000564 while (interp < end && VG_(isspace)(*interp))
fitzhardinge7e343cd2003-12-16 02:14:00 +0000565 interp++;
566
njn73750612005-10-14 03:11:30 +0000567 vg_assert(*interp == '/'); /* absolute path only for interpreter */
fitzhardinge7e343cd2003-12-16 02:14:00 +0000568
569 /* skip over interpreter name */
njn73750612005-10-14 03:11:30 +0000570 for (cp = interp; cp < end && !VG_(isspace)(*cp); cp++)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000571 ;
572
573 eol = (*cp == '\n');
574
575 *cp++ = '\0';
576
577 if (!eol && cp < end) {
578 /* skip space before arg */
njn73750612005-10-14 03:11:30 +0000579 while (cp < end && VG_(isspace)(*cp))
fitzhardinge7e343cd2003-12-16 02:14:00 +0000580 cp++;
581
582 /* arg is from here to eol */
583 arg = cp;
584 while (cp < end && *cp != '\n')
585 cp++;
586 *cp = '\0';
587 }
588
sewardj45f4e7c2005-09-27 19:20:21 +0000589 info->interp_name = VG_(strdup)(interp);
590 vg_assert(NULL != info->interp_name);
nethercote71980f02004-01-24 18:18:54 +0000591 if (arg != NULL && *arg != '\0') {
sewardj45f4e7c2005-09-27 19:20:21 +0000592 info->interp_args = VG_(strdup)(arg);
593 vg_assert(NULL != info->interp_args);
nethercote71980f02004-01-24 18:18:54 +0000594 }
fitzhardinge7e343cd2003-12-16 02:14:00 +0000595
596 if (info->argv && info->argv[0] != NULL)
597 info->argv[0] = (char *)name;
598
599 if (0)
sewardj45f4e7c2005-09-27 19:20:21 +0000600 VG_(printf)("#! script: interp_name=\"%s\" interp_args=\"%s\"\n",
601 info->interp_name, info->interp_args);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000602
603 return do_exec_inner(interp, info);
604}
605
fitzhardingefd7da3a2004-09-08 20:05:29 +0000606
njn73750612005-10-14 03:11:30 +0000607typedef enum {
608 VG_EXE_FORMAT_ELF = 1,
609 VG_EXE_FORMAT_SCRIPT = 2,
610} ExeFormat;
fitzhardingefd7da3a2004-09-08 20:05:29 +0000611
njn73750612005-10-14 03:11:30 +0000612// Check the file looks executable.
sewardj13247ca2005-12-30 22:52:20 +0000613SysRes VG_(pre_exec_check)(const HChar* exe_name, Int* out_fd)
fitzhardingefd7da3a2004-09-08 20:05:29 +0000614{
njn73750612005-10-14 03:11:30 +0000615 Int fd, ret;
616 SysRes res;
617 Char buf[VKI_PAGE_SIZE];
618 SizeT bufsz = VKI_PAGE_SIZE, fsz;
fitzhardingefd7da3a2004-09-08 20:05:29 +0000619
njn73750612005-10-14 03:11:30 +0000620 // Check it's readable
621 res = VG_(open)(exe_name, VKI_O_RDONLY, 0);
622 if (res.isError) {
623 return res;
624 }
625 fd = res.val;
fitzhardingefd7da3a2004-09-08 20:05:29 +0000626
njn73750612005-10-14 03:11:30 +0000627 // Check we have execute permissions
628 ret = VG_(check_executable)((HChar*)exe_name);
629 if (0 != ret) {
630 VG_(close)(fd);
631 return VG_(mk_SysRes_Error)(ret);
fitzhardingefd7da3a2004-09-08 20:05:29 +0000632 }
633
njn73750612005-10-14 03:11:30 +0000634 fsz = VG_(fsize)(fd);
635 if (fsz < bufsz)
636 bufsz = fsz;
637
638 res = VG_(pread)(fd, buf, bufsz, 0);
639 if (res.isError || res.val != bufsz) {
640 VG_(close)(fd);
641 return VG_(mk_SysRes_Error)(VKI_EACCES);
642 }
643 bufsz = res.val;
644
645 if (match_ELF(buf, bufsz)) {
646 res = VG_(mk_SysRes_Success)(VG_EXE_FORMAT_ELF);
647 } else if (match_script(buf, bufsz)) {
648 res = VG_(mk_SysRes_Success)(VG_EXE_FORMAT_SCRIPT);
fitzhardingefd7da3a2004-09-08 20:05:29 +0000649 } else {
njn73750612005-10-14 03:11:30 +0000650 res = VG_(mk_SysRes_Error)(VKI_ENOEXEC);
fitzhardingefd7da3a2004-09-08 20:05:29 +0000651 }
652
njn73750612005-10-14 03:11:30 +0000653 // Write the 'out_fd' param if necessary, or close the file.
654 if (!res.isError && out_fd) {
655 *out_fd = fd;
656 } else {
657 VG_(close)(fd);
658 }
659
660 return res;
fitzhardingefd7da3a2004-09-08 20:05:29 +0000661}
662
njn73750612005-10-14 03:11:30 +0000663// returns: 0 = success, non-0 is failure
664//
665// We can execute only ELF binaries or scripts that begin with "#!". (Not,
666// for example, scripts that don't begin with "#!"; see the VG_(do_exec)()
667// invocation from m_main.c for how that's handled.)
sewardj13247ca2005-12-30 22:52:20 +0000668static Int do_exec_inner(const HChar *exe, ExeInfo* info)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000669{
njn73750612005-10-14 03:11:30 +0000670 SysRes res;
671 Int fd;
672 Int ret;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000673
njn73750612005-10-14 03:11:30 +0000674 res = VG_(pre_exec_check)(exe, &fd);
675 if (res.isError)
676 return res.val;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000677
njn73750612005-10-14 03:11:30 +0000678 switch (res.val) {
679 case VG_EXE_FORMAT_ELF: ret = load_ELF (fd, exe, info); break;
680 case VG_EXE_FORMAT_SCRIPT: ret = load_script(fd, exe, info); break;
681 default:
682 vg_assert2(0, "unrecognised VG_EXE_FORMAT value\n");
fitzhardinge7e343cd2003-12-16 02:14:00 +0000683 }
684
sewardj45f4e7c2005-09-27 19:20:21 +0000685 VG_(close)(fd);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000686
687 return ret;
688}
689
njn73750612005-10-14 03:11:30 +0000690
691static Bool is_hash_bang_file(Char* f)
692{
693 SysRes res = VG_(open)(f, VKI_O_RDONLY, 0);
694 if (!res.isError) {
695 Char buf[3] = {0,0,0};
696 Int fd = res.val;
697 Int n = VG_(read)(fd, buf, 2);
698 if (n == 2 && VG_STREQ("#!", buf))
699 return True;
700 }
701 return False;
702}
703
704// Look at the first 80 chars, and if any are greater than 127, it's binary.
705// This is crude, but should be good enough. Note that it fails on a
706// zero-length file, as we want.
707static Bool is_binary_file(Char* f)
708{
709 SysRes res = VG_(open)(f, VKI_O_RDONLY, 0);
710 if (!res.isError) {
711 UChar buf[80];
712 Int fd = res.val;
713 Int n = VG_(read)(fd, buf, 80);
714 Int i;
715 for (i = 0; i < n; i++) {
716 if (buf[i] > 127)
717 return True; // binary char found
718 }
719 return False;
720 } else {
721 // Something went wrong. This will only happen if we earlier
722 // succeeded in opening the file but fail here (eg. the file was
723 // deleted between then and now).
724 VG_(printf)("valgrind: %s: unknown error\n", f);
725 VG_(exit)(126); // 126 == NOEXEC
726 }
727}
728
729// If the do_exec fails we try to emulate what the shell does (I used
730// bash as a guide). It's worth noting that the shell can execute some
731// things that VG_(do_exec)() (which subsitutes for the kernel's exec())
732// will refuse to (eg. scripts lacking a "#!" prefix).
sewardj13247ca2005-12-30 22:52:20 +0000733static Int do_exec_shell_followup(Int ret, HChar* exe_name,
734 ExeInfo* info)
njn73750612005-10-14 03:11:30 +0000735{
736 Char* default_interp_name = "/bin/sh";
737 SysRes res;
738 struct vki_stat st;
739
740 if (VKI_ENOEXEC == ret) {
741 // It was an executable file, but in an unacceptable format. Probably
742 // is a shell script lacking the "#!" prefix; try to execute it so.
743
744 // Is it a binary file?
745 if (is_binary_file(exe_name)) {
746 VG_(printf)("valgrind: %s: cannot execute binary file\n", exe_name);
747 VG_(exit)(126); // 126 == NOEXEC
748 }
749
750 // Looks like a script. Run it with /bin/sh. This includes
751 // zero-length files.
752
753 info->interp_name = VG_(strdup)(default_interp_name);
754 info->interp_args = NULL;
755 if (info->argv && info->argv[0] != NULL)
756 info->argv[0] = (char *)exe_name;
757
758 ret = do_exec_inner(info->interp_name, info);
759
760 if (0 != ret) {
761 // Something went wrong with executing the default interpreter
762 VG_(printf)("valgrind: %s: bad interpreter (%s): %s\n",
763 exe_name, info->interp_name, VG_(strerror)(ret));
764 VG_(exit)(126); // 126 == NOEXEC
765 }
766
767 } else if (0 != ret) {
768 // Something else went wrong. Try to make the error more specific,
769 // and then print a message and abort.
770
771 // Was it a directory?
772 res = VG_(stat)(exe_name, &st);
773 if (!res.isError && VKI_S_ISDIR(st.st_mode)) {
774 VG_(printf)("valgrind: %s: is a directory\n", exe_name);
775
776 // Was it not executable?
777 } else if (0 != VG_(check_executable)(exe_name)) {
778 VG_(printf)("valgrind: %s: %s\n", exe_name, VG_(strerror)(ret));
779
780 // Did it start with "#!"? If so, it must have been a bad interpreter.
781 } else if (is_hash_bang_file(exe_name)) {
782 VG_(printf)("valgrind: %s: bad interpreter: %s\n",
783 exe_name, VG_(strerror)(ret));
784
785 // Otherwise it was something else.
786 } else {
787 VG_(printf)("valgrind: %s\n", exe_name, VG_(strerror)(ret));
788 }
789 // 126 means NOEXEC; I think this is Posix, and that in some cases we
790 // should be returning 127, meaning NOTFOUND. Oh well.
791 VG_(exit)(126);
792 }
793 return ret;
794}
795
796
797// This emulates the kernel's exec(). If it fails, it then emulates the
798// shell's handling of the situation.
nethercoteea147e72004-07-26 15:43:57 +0000799// See ume.h for an indication of which entries of 'info' are inputs, which
800// are outputs, and which are both.
sewardj45f4e7c2005-09-27 19:20:21 +0000801/* returns: 0 = success, non-0 is failure */
sewardj13247ca2005-12-30 22:52:20 +0000802Int VG_(do_exec)(const HChar* exe_name, ExeInfo* info)
fitzhardinge7e343cd2003-12-16 02:14:00 +0000803{
njn73750612005-10-14 03:11:30 +0000804 Int ret;
805
nethercoted6a56872004-07-26 15:32:47 +0000806 info->interp_name = NULL;
807 info->interp_args = NULL;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000808
njn73750612005-10-14 03:11:30 +0000809 ret = do_exec_inner(exe_name, info);
810
811 if (0 != ret) {
812 ret = do_exec_shell_followup(ret, (Char*)exe_name, info);
813 }
814 return ret;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000815}
nethercotebb1c9912004-01-04 16:43:23 +0000816
817/*--------------------------------------------------------------------*/
njn08a2e172005-06-21 22:47:54 +0000818/*--- end ---*/
nethercotebb1c9912004-01-04 16:43:23 +0000819/*--------------------------------------------------------------------*/