blob: 1054e9b08eb2ed4da6aba41e41d070cf7d647a92 [file] [log] [blame]
jseward2886b0e2004-01-04 03:46:11 +00001
nethercote1fe54502004-07-26 15:28:33 +00002/*--------------------------------------------------------------------*/
njn08a2e172005-06-21 22:47:54 +00003/*--- User-mode execve. pub_core_ume.h ---*/
nethercote1fe54502004-07-26 15:28:33 +00004/*--------------------------------------------------------------------*/
5
jseward2886b0e2004-01-04 03:46:11 +00006/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
jseward2886b0e2004-01-04 03:46:11 +00009
njn53612422005-03-12 16:22:54 +000010 Copyright (C) 2000-2005 Julian Seward
jseward2886b0e2004-01-04 03:46:11 +000011 jseward@acm.org
12
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.
17
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.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
njn08a2e172005-06-21 22:47:54 +000031#ifndef __PUB_CORE_UME_H
32#define __PUB_CORE_UME_H
33
34//--------------------------------------------------------------------
35// PURPOSE: This module implements user-mode execve, ie. program loading
36// and exec'ing. It is shared between stage1 and stage2.
37//--------------------------------------------------------------------
fitzhardinge7e343cd2003-12-16 02:14:00 +000038
39#include <elf.h>
40#include <sys/types.h>
41
nethercote1fe54502004-07-26 15:28:33 +000042/*------------------------------------------------------------*/
43/*--- General stuff ---*/
44/*------------------------------------------------------------*/
45
njn08a2e172005-06-21 22:47:54 +000046/* This is only here so it can be shared between stage1 and stage2 */
nethercote969ecf12004-10-13 17:29:01 +000047
sewardj45f4e7c2005-09-27 19:20:21 +000048/* JRS 9 Aug 05: both of these are apparently unused, except by
49 memcheck/tests/vgtest_ume.c. */
50//zz extern
51//zz void VG_(foreach_map)(int (*fn)(char *start, char *end,
52//zz const char *perm, off_t offset,
53//zz int maj, int min, int ino, void* extra),
54//zz void* extra);
55//zz
56//zz /* Jump to 'dst', but first set the stack pointer to 'stack'. Also,
57//zz clear all the integer registers before entering 'dst'. It's
58//zz important that the stack pointer is set to exactly 'stack' and not
59//zz (eg) stack - apparently_harmless_looking_small_offset. Basically
60//zz because the code at 'dst' might be wanting to scan the area above
61//zz 'stack' (viz, the auxv array), and putting spurious words on the
62//zz stack confuses it.
63//zz
64//zz This is only exported so that vgtest_ume.c can use it.
65//zz */
66//zz extern
67//zz __attribute__((noreturn))
68//zz void VG_(jump_and_switch_stacks) ( Addr stack, Addr dst );
nethercote107e1c02004-10-13 17:55:31 +000069
sewardj0c1a5962005-03-22 00:19:55 +000070
nethercote107e1c02004-10-13 17:55:31 +000071/*------------------------------------------------------------*/
72/*--- Loading ELF files ---*/
73/*------------------------------------------------------------*/
74
nethercoteea147e72004-07-26 15:43:57 +000075// Info needed to load and run a program. IN/INOUT/OUT refers to the
76// inputs/outputs of do_exec().
fitzhardinge7e343cd2003-12-16 02:14:00 +000077struct exeinfo
78{
nethercoteea147e72004-07-26 15:43:57 +000079 char** argv; // IN: the original argv
fitzhardinge7e343cd2003-12-16 02:14:00 +000080
nethercotea3c3cf22004-11-01 18:38:00 +000081 Addr exe_base; // INOUT: lowest (allowed) address of exe
82 Addr exe_end; // INOUT: highest (allowed) address
fitzhardinge7e343cd2003-12-16 02:14:00 +000083
nethercotea3c3cf22004-11-01 18:38:00 +000084 Addr phdr; // OUT: address phdr was mapped at
85 int phnum; // OUT: number of phdrs
86 Addr interp_base; // OUT: where interpreter (ld.so) was mapped
87 Addr entry; // OUT: entrypoint in main executable
88 Addr init_eip; // OUT: initial eip
89 Addr brkbase; // OUT: base address of brk segment
fitzhardinge7e343cd2003-12-16 02:14:00 +000090
nethercote1fe54502004-07-26 15:28:33 +000091 // These are the extra args added by #! scripts
nethercoteea147e72004-07-26 15:43:57 +000092 char* interp_name; // OUT: the interpreter name
93 char* interp_args; // OUT: the args for the interpreter
fitzhardinge7e343cd2003-12-16 02:14:00 +000094};
95
njn73750612005-10-14 03:11:30 +000096// Do a number of appropriate checks to see if the file looks executable by
97// the kernel: ie. it's a file, it's readable and executable, and it's in
98// either ELF or "#!" format. On success, 'out_fd' gets the fd of the file
99// if it's non-NULL. Otherwise the fd is closed.
100extern SysRes VG_(pre_exec_check)(const Char* exe_name, Int* out_fd);
101
nethercoteea147e72004-07-26 15:43:57 +0000102// Does everything short of actually running 'exe': finds the file,
103// checks execute permissions, sets up interpreter if program is a script,
104// reads headers, maps file into memory, and returns important info about
105// the program.
njn73750612005-10-14 03:11:30 +0000106extern Int VG_(do_exec)(const char *exe, struct exeinfo *info);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000107
nethercote1fe54502004-07-26 15:28:33 +0000108/*------------------------------------------------------------*/
nethercote1fe54502004-07-26 15:28:33 +0000109/*--- Finding and dealing with auxv ---*/
110/*------------------------------------------------------------*/
fitzhardinge7e343cd2003-12-16 02:14:00 +0000111
112struct ume_auxv
113{
sewardjb9bca7c2005-03-02 14:04:19 +0000114 Word a_type;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000115 union {
116 void *a_ptr;
sewardjb9bca7c2005-03-02 14:04:19 +0000117 Word a_val;
mueller5ed88f22004-01-06 16:02:29 +0000118 } u;
fitzhardinge7e343cd2003-12-16 02:14:00 +0000119};
120
njn62ff0f22005-06-21 23:03:36 +0000121extern struct ume_auxv *VG_(find_auxv)(UWord* orig_esp);
fitzhardinge7e343cd2003-12-16 02:14:00 +0000122
njn08a2e172005-06-21 22:47:54 +0000123#endif /* __PUB_CORE_UME_H */
nethercote1fe54502004-07-26 15:28:33 +0000124
125/*--------------------------------------------------------------------*/
njn08a2e172005-06-21 22:47:54 +0000126/*--- end ---*/
nethercote1fe54502004-07-26 15:28:33 +0000127/*--------------------------------------------------------------------*/