• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*--------------------------------------------------------------------*/
3 /*--- begin                                           genoffsets.c ---*/
4 /*--------------------------------------------------------------------*/
5 
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9 
10    Copyright (C) 2004-2017 OpenWorks LLP
11       info@open-works.net
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., 51 Franklin Street, Fifth Floor, Boston, MA
26    02110-1301, USA.
27 
28    The GNU General Public License is contained in the file COPYING.
29 
30    Neither the names of the U.S. Department of Energy nor the
31    University of California nor the names of its contributors may be
32    used to endorse or promote products derived from this software
33    without prior written permission.
34 */
35 
36 #include <stdio.h>
37 
38 /* A program which, when compiled to assembly, exposes various guest
39    state offsets.  The program isn't executed, since that breaks
40    cross-compilation.
41 
42    It does rely on the assumption that 'my_offsetof(Ty,Field)' is
43    folded to a constant at a compile time, which seems a bit dodgy
44    to me.  On gcc4 it is possible to use __builtin_offsetof, which
45    sounds safer, but that doesn't exist on older gccs.  Oh Well.
46 */
47 
48 #include "../pub/libvex_basictypes.h"
49 #include "../pub/libvex_guest_x86.h"
50 #include "../pub/libvex_guest_amd64.h"
51 #include "../pub/libvex_guest_ppc32.h"
52 #include "../pub/libvex_guest_ppc64.h"
53 #include "../pub/libvex_guest_arm.h"
54 #include "../pub/libvex_guest_arm64.h"
55 #include "../pub/libvex_guest_s390x.h"
56 #include "../pub/libvex_guest_mips32.h"
57 #include "../pub/libvex_guest_mips64.h"
58 
59 #define VG_STRINGIFZ(__str)  #__str
60 #define VG_STRINGIFY(__str)  VG_STRINGIFZ(__str)
61 
62 #define my_offsetof(__type,__field) (&((__type*)0)->__field)
63 
64 /* This forces gcc to evaluate the my_offsetof call at compile time,
65    and then emits it in the assembly, along with the nonsense string
66    "xyzzy", for easy greppability.  Once this file is compiled to
67    assembly, the lines containing "xyzzy" are grepped out and sed-ed
68    to produce the final result.  See the Makefile rule for
69    pub/libvex_guest_offsets.h. */
70 #define GENOFFSET(_structUppercase,_structLowercase,_fieldname)  \
71    __asm__ __volatile__ ( \
72       "\n#define OFFSET_" \
73       VG_STRINGIFY(_structLowercase) "_" \
74       VG_STRINGIFY(_fieldname) \
75       " xyzzy%0\n" : /*out*/ \
76                    : /*in*/ "n" \
77          (my_offsetof(VexGuest##_structUppercase##State, \
78           guest_##_fieldname)) \
79    )
80 
81 void foo ( void );
82 __attribute__((noinline))
foo(void)83 void foo ( void )
84 {
85    // x86
86    GENOFFSET(X86,x86,EAX);
87    GENOFFSET(X86,x86,EBX);
88    GENOFFSET(X86,x86,ECX);
89    GENOFFSET(X86,x86,EDX);
90    GENOFFSET(X86,x86,ESI);
91    GENOFFSET(X86,x86,EDI);
92    GENOFFSET(X86,x86,EBP);
93    GENOFFSET(X86,x86,ESP);
94    GENOFFSET(X86,x86,EIP);
95    GENOFFSET(X86,x86,CS);
96    GENOFFSET(X86,x86,DS);
97    GENOFFSET(X86,x86,ES);
98    GENOFFSET(X86,x86,FS);
99    GENOFFSET(X86,x86,GS);
100    GENOFFSET(X86,x86,SS);
101 
102    // amd64
103    GENOFFSET(AMD64,amd64,RAX);
104    GENOFFSET(AMD64,amd64,RBX);
105    GENOFFSET(AMD64,amd64,RCX);
106    GENOFFSET(AMD64,amd64,RDX);
107    GENOFFSET(AMD64,amd64,RSI);
108    GENOFFSET(AMD64,amd64,RDI);
109    GENOFFSET(AMD64,amd64,RSP);
110    GENOFFSET(AMD64,amd64,RBP);
111    GENOFFSET(AMD64,amd64,R8);
112    GENOFFSET(AMD64,amd64,R9);
113    GENOFFSET(AMD64,amd64,R10);
114    GENOFFSET(AMD64,amd64,R11);
115    GENOFFSET(AMD64,amd64,R12);
116    GENOFFSET(AMD64,amd64,R13);
117    GENOFFSET(AMD64,amd64,R14);
118    GENOFFSET(AMD64,amd64,R15);
119    GENOFFSET(AMD64,amd64,RIP);
120 
121    // ppc32
122    GENOFFSET(PPC32,ppc32,GPR0);
123    GENOFFSET(PPC32,ppc32,GPR1);
124    GENOFFSET(PPC32,ppc32,GPR2);
125    GENOFFSET(PPC32,ppc32,GPR3);
126    GENOFFSET(PPC32,ppc32,GPR4);
127    GENOFFSET(PPC32,ppc32,GPR5);
128    GENOFFSET(PPC32,ppc32,GPR6);
129    GENOFFSET(PPC32,ppc32,GPR7);
130    GENOFFSET(PPC32,ppc32,GPR8);
131    GENOFFSET(PPC32,ppc32,GPR9);
132    GENOFFSET(PPC32,ppc32,GPR10);
133    GENOFFSET(PPC32,ppc32,CIA);
134    GENOFFSET(PPC32,ppc32,CR0_0);
135 
136    // ppc64
137    GENOFFSET(PPC64,ppc64,GPR0);
138    GENOFFSET(PPC64,ppc64,GPR1);
139    GENOFFSET(PPC64,ppc64,GPR2);
140    GENOFFSET(PPC64,ppc64,GPR3);
141    GENOFFSET(PPC64,ppc64,GPR4);
142    GENOFFSET(PPC64,ppc64,GPR5);
143    GENOFFSET(PPC64,ppc64,GPR6);
144    GENOFFSET(PPC64,ppc64,GPR7);
145    GENOFFSET(PPC64,ppc64,GPR8);
146    GENOFFSET(PPC64,ppc64,GPR9);
147    GENOFFSET(PPC64,ppc64,GPR10);
148    GENOFFSET(PPC64,ppc64,CIA);
149    GENOFFSET(PPC64,ppc64,CR0_0);
150 
151    // arm
152    GENOFFSET(ARM,arm,R0);
153    GENOFFSET(ARM,arm,R1);
154    GENOFFSET(ARM,arm,R2);
155    GENOFFSET(ARM,arm,R3);
156    GENOFFSET(ARM,arm,R4);
157    GENOFFSET(ARM,arm,R5);
158    GENOFFSET(ARM,arm,R7);
159    GENOFFSET(ARM,arm,R13);
160    GENOFFSET(ARM,arm,R14);
161    GENOFFSET(ARM,arm,R15T);
162 
163    // arm64
164    GENOFFSET(ARM64,arm64,X0);
165    GENOFFSET(ARM64,arm64,X1);
166    GENOFFSET(ARM64,arm64,X2);
167    GENOFFSET(ARM64,arm64,X3);
168    GENOFFSET(ARM64,arm64,X4);
169    GENOFFSET(ARM64,arm64,X5);
170    GENOFFSET(ARM64,arm64,X6);
171    GENOFFSET(ARM64,arm64,X7);
172    GENOFFSET(ARM64,arm64,X8);
173    GENOFFSET(ARM64,arm64,XSP);
174    GENOFFSET(ARM64,arm64,PC);
175 
176    // s390x
177    GENOFFSET(S390X,s390x,r2);
178    GENOFFSET(S390X,s390x,r3);
179    GENOFFSET(S390X,s390x,r4);
180    GENOFFSET(S390X,s390x,r5);
181    GENOFFSET(S390X,s390x,r6);
182    GENOFFSET(S390X,s390x,r7);
183    GENOFFSET(S390X,s390x,r15);
184    GENOFFSET(S390X,s390x,IA);
185    GENOFFSET(S390X,s390x,SYSNO);
186    GENOFFSET(S390X,s390x,IP_AT_SYSCALL);
187    GENOFFSET(S390X,s390x,fpc);
188    GENOFFSET(S390X,s390x,CC_OP);
189    GENOFFSET(S390X,s390x,CC_DEP1);
190    GENOFFSET(S390X,s390x,CC_DEP2);
191    GENOFFSET(S390X,s390x,CC_NDEP);
192 
193    // MIPS32
194    GENOFFSET(MIPS32,mips32,r0);
195    GENOFFSET(MIPS32,mips32,r1);
196    GENOFFSET(MIPS32,mips32,r2);
197    GENOFFSET(MIPS32,mips32,r3);
198    GENOFFSET(MIPS32,mips32,r4);
199    GENOFFSET(MIPS32,mips32,r5);
200    GENOFFSET(MIPS32,mips32,r6);
201    GENOFFSET(MIPS32,mips32,r7);
202    GENOFFSET(MIPS32,mips32,r8);
203    GENOFFSET(MIPS32,mips32,r9);
204    GENOFFSET(MIPS32,mips32,r10);
205    GENOFFSET(MIPS32,mips32,r11);
206    GENOFFSET(MIPS32,mips32,r12);
207    GENOFFSET(MIPS32,mips32,r13);
208    GENOFFSET(MIPS32,mips32,r14);
209    GENOFFSET(MIPS32,mips32,r15);
210    GENOFFSET(MIPS32,mips32,r15);
211    GENOFFSET(MIPS32,mips32,r17);
212    GENOFFSET(MIPS32,mips32,r18);
213    GENOFFSET(MIPS32,mips32,r19);
214    GENOFFSET(MIPS32,mips32,r20);
215    GENOFFSET(MIPS32,mips32,r21);
216    GENOFFSET(MIPS32,mips32,r22);
217    GENOFFSET(MIPS32,mips32,r23);
218    GENOFFSET(MIPS32,mips32,r24);
219    GENOFFSET(MIPS32,mips32,r25);
220    GENOFFSET(MIPS32,mips32,r26);
221    GENOFFSET(MIPS32,mips32,r27);
222    GENOFFSET(MIPS32,mips32,r28);
223    GENOFFSET(MIPS32,mips32,r29);
224    GENOFFSET(MIPS32,mips32,r30);
225    GENOFFSET(MIPS32,mips32,r31);
226    GENOFFSET(MIPS32,mips32,PC);
227    GENOFFSET(MIPS32,mips32,HI);
228    GENOFFSET(MIPS32,mips32,LO);
229 
230    // MIPS64
231    GENOFFSET(MIPS64,mips64,r0);
232    GENOFFSET(MIPS64,mips64,r1);
233    GENOFFSET(MIPS64,mips64,r2);
234    GENOFFSET(MIPS64,mips64,r3);
235    GENOFFSET(MIPS64,mips64,r4);
236    GENOFFSET(MIPS64,mips64,r5);
237    GENOFFSET(MIPS64,mips64,r6);
238    GENOFFSET(MIPS64,mips64,r7);
239    GENOFFSET(MIPS64,mips64,r8);
240    GENOFFSET(MIPS64,mips64,r9);
241    GENOFFSET(MIPS64,mips64,r10);
242    GENOFFSET(MIPS64,mips64,r11);
243    GENOFFSET(MIPS64,mips64,r12);
244    GENOFFSET(MIPS64,mips64,r13);
245    GENOFFSET(MIPS64,mips64,r14);
246    GENOFFSET(MIPS64,mips64,r15);
247    GENOFFSET(MIPS64,mips64,r15);
248    GENOFFSET(MIPS64,mips64,r17);
249    GENOFFSET(MIPS64,mips64,r18);
250    GENOFFSET(MIPS64,mips64,r19);
251    GENOFFSET(MIPS64,mips64,r20);
252    GENOFFSET(MIPS64,mips64,r21);
253    GENOFFSET(MIPS64,mips64,r22);
254    GENOFFSET(MIPS64,mips64,r23);
255    GENOFFSET(MIPS64,mips64,r24);
256    GENOFFSET(MIPS64,mips64,r25);
257    GENOFFSET(MIPS64,mips64,r26);
258    GENOFFSET(MIPS64,mips64,r27);
259    GENOFFSET(MIPS64,mips64,r28);
260    GENOFFSET(MIPS64,mips64,r29);
261    GENOFFSET(MIPS64,mips64,r30);
262    GENOFFSET(MIPS64,mips64,r31);
263    GENOFFSET(MIPS64,mips64,PC);
264    GENOFFSET(MIPS64,mips64,HI);
265    GENOFFSET(MIPS64,mips64,LO);
266 }
267 
268 /*--------------------------------------------------------------------*/
269 /*--- end                                             genoffsets.c ---*/
270 /*--------------------------------------------------------------------*/
271