1
2/*--------------------------------------------------------------------*/
3/*--- Create initial process image on for the client               ---*/
4/*---                                           pub_core_initimg.h ---*/
5/*--------------------------------------------------------------------*/
6
7/*
8   This file is part of Valgrind, a dynamic binary instrumentation
9   framework.
10
11   Copyright (C) 2006-2010 OpenWorks LLP
12      info@open-works.co.uk
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
32#ifndef __PUB_CORE_INITIMG_H
33#define __PUB_CORE_INITIMG_H
34
35
36//--------------------------------------------------------------------
37// PURPOSE: Map the client executable into memory, then set up its
38// stack, environment and data section, ready for execution.  Quite a
39// lot of work on Linux (ELF) but nearly a no-op on AIX (XCOFF) since
40// the AIX kernel does most of the work for us.
41//--------------------------------------------------------------------
42
43/* These are OS-specific and defined below. */
44typedef  struct _IICreateImageInfo    IICreateImageInfo;
45typedef  struct _IIFinaliseImageInfo  IIFinaliseImageInfo;
46
47/* This is a two stage process.  The first stage, which is most of the
48   work, creates the initial image in memory to the extent possible.
49   To do this it takes a bundle of information in an IICreateImageInfo
50   structure, which is gathered in an OS-specific way at startup.
51   This returns an IIFinaliseImageInfo structure: */
52extern
53IIFinaliseImageInfo VG_(ii_create_image)( IICreateImageInfo );
54
55/* Just before starting the client, we may need to make final
56   adjustments to its initial image.  Also we need to set up the VEX
57   guest state for thread 1 (the root thread) and copy in essential
58   starting values.  This is handed the IIFinaliseImageInfo created by
59   VG_(ii_create_image). */
60extern
61void VG_(ii_finalise_image)( IIFinaliseImageInfo );
62
63/* Note that both IICreateImageInfo and IIFinaliseImageInfo are
64   OS-specific.  We now go on to give instantiations of them
65   for supported OSes. */
66
67/* ------------------------- Linux ------------------------- */
68
69#if defined(VGO_linux)
70
71struct _IICreateImageInfo {
72   /* ------ Mandatory fields ------ */
73   HChar*  toolname;
74   Addr    sp_at_startup;
75   Addr    clstack_top;
76   /* ------ Per-OS fields ------ */
77   HChar** argv;
78   HChar** envp;
79};
80
81struct _IIFinaliseImageInfo {
82   /* ------ Mandatory fields ------ */
83   SizeT clstack_max_size;
84   Addr  initial_client_SP;
85   /* ------ Per-OS fields ------ */
86   Addr  initial_client_IP;
87   Addr  initial_client_TOC;
88   UInt* client_auxv;
89};
90
91
92/* ------------------------- AIX5 ------------------------- */
93
94#elif defined(VGO_aix5)
95
96/* First we need to define this auxiliary structure. */
97typedef
98   struct {
99      /* NOTE: VG_(ppc32/64_aix5_do_preloads_then_start_client) has
100         these offsets hardwired in.  Do not change them without
101         changing it too. */
102      /* system call numbers */
103      /*   0 */ UInt nr_load; /* is __NR___loadx for 32-bit,
104                                    __NR_kload for 64 */
105      /*   4 */ UInt nr_kwrite;
106      /*   8 */ UInt nr__exit;
107      /* offset/length of error message, if the preloads fail */
108      /*  12 */ UInt off_errmsg;
109      /*  16 */ UInt len_errmsg;
110      /* offsets from start of this struct to the the preload file
111         names */
112      /*  20 */ UInt off_preloadcorename;
113      /*  24 */ UInt off_preloadtoolname;
114      /*  28 */ UInt off_ld_preloadname;
115      /* Once the preloading is done, we'll need to restore the guest
116         state to what it needs to be at client startup.  Here's the
117         relevant info.  Are ULongs; for 32-bit the data is at the
118         lsb (high addressed) end. */
119      /*  32 */ ULong client_start;
120      /*  40 */ ULong r2;
121      /*  48 */ ULong r3;
122      /*  56 */ ULong r4;
123      /*  64 */ ULong r5;
124      /*  72 */ ULong r6;
125      /*  80 */ ULong r7;
126      /*  88 */ ULong r8;
127      /*  96 */ ULong r9;
128      /* 104 */ ULong r10;
129      /* If the loading fails, we'll want to call a diagnostic
130         function in C to figure out what happened.  Here's it's
131         function descriptor.  Note, this runs on the simd cpu
132         (a kludge, and will segfault in 64-bit mode). */
133      /* 112 */ void* p_diagnose_load_failure;
134   }
135   AIX5PreloadPage;
136
137struct _IICreateImageInfo {
138   /* ------ Mandatory fields ------ */
139   HChar* toolname;
140   Addr   sp_at_startup; /* Not used on AIX. */
141   Addr   clstack_top;   /* Not used on AIX. */
142   /* ------ Per-OS fields ------ */
143   /* Initial values for guest int registers (GPR0 .. GPR31, PC, CR,
144      LR, CTR, XER).  Passed to us from the launcher. */
145   ULong* intregs37;
146   /* AIX5Bootblock*, really */
147   void* bootblock;
148   /* Adler32 checksum of uncompressed data of compressed page. */
149   UInt adler32_exp;
150};
151
152struct _IIFinaliseImageInfo {
153   /* ------ Mandatory fields ------ */
154   SizeT clstack_max_size;
155   /* Initial value for SP (which is merely a copy of r1's value,
156      intregs37[1]). */
157   Addr initial_client_SP;
158   /* ------ Per-OS fields ------ */
159   /* Pointer to the preload page.  The preload page and this pointer
160      to it are set up by VG_(ii_create_image). */
161   AIX5PreloadPage* preloadpage;
162   /* Initial values for guest int registers (GPR0 .. GPR31, PC,
163      CR, LR, CTR, XER).  Copied from the CII. */
164   ULong* intregs37;
165   /* Address of the page compressed by the launcher. */
166   Addr compressed_page;
167   /* Adler32 checksum of uncompressed data of said page. */
168   UInt adler32_exp;
169};
170
171
172/* ------------------------- Darwin ------------------------- */
173
174#elif defined(VGO_darwin)
175
176struct _IICreateImageInfo {
177   /* ------ Mandatory fields ------ */
178   HChar*  toolname;
179   Addr    sp_at_startup;
180   Addr    clstack_top;
181   /* ------ Per-OS fields ------ */
182   HChar** argv;
183   HChar** envp;
184   Addr    entry;            /* &_start */
185   Addr    init_ip;          /* &__dyld_start, or copy of entry */
186   Addr    stack_start;      /* stack segment hot */
187   Addr    stack_end;        /* stack segment cold */
188   Addr    text;             /* executable's Mach header */
189   Bool    dynamic;          /* False iff executable is static */
190   HChar*  executable_path;  /* path passed to execve() */
191};
192
193struct _IIFinaliseImageInfo {
194   /* ------ Mandatory fields ------ */
195   SizeT clstack_max_size;
196   Addr  initial_client_SP;
197   /* ------ Per-OS fields ------ */
198   Addr  initial_client_IP;
199};
200
201
202#else
203#  error "Unknown OS"
204#endif
205
206
207#endif   // __PUB_CORE_INITIMG_H
208
209/*--------------------------------------------------------------------*/
210/*--- end                                                          ---*/
211/*--------------------------------------------------------------------*/
212